1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/i2c.h>
28 #include <linux/input.h>
29 #include <linux/intel-iommu.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dma-resv.h>
33 #include <linux/slab.h>
34 
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_atomic_uapi.h>
38 #include <drm/drm_dp_helper.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_rect.h>
44 
45 #include "display/intel_crt.h"
46 #include "display/intel_ddi.h"
47 #include "display/intel_dp.h"
48 #include "display/intel_dp_mst.h"
49 #include "display/intel_dsi.h"
50 #include "display/intel_dvo.h"
51 #include "display/intel_gmbus.h"
52 #include "display/intel_hdmi.h"
53 #include "display/intel_lvds.h"
54 #include "display/intel_sdvo.h"
55 #include "display/intel_tv.h"
56 #include "display/intel_vdsc.h"
57 
58 #include "gt/intel_rps.h"
59 
60 #include "i915_drv.h"
61 #include "i915_trace.h"
62 #include "intel_acpi.h"
63 #include "intel_atomic.h"
64 #include "intel_atomic_plane.h"
65 #include "intel_bw.h"
66 #include "intel_cdclk.h"
67 #include "intel_color.h"
68 #include "intel_display_types.h"
69 #include "intel_dp_link_training.h"
70 #include "intel_fbc.h"
71 #include "intel_fbdev.h"
72 #include "intel_fifo_underrun.h"
73 #include "intel_frontbuffer.h"
74 #include "intel_hdcp.h"
75 #include "intel_hotplug.h"
76 #include "intel_overlay.h"
77 #include "intel_pipe_crc.h"
78 #include "intel_pm.h"
79 #include "intel_psr.h"
80 #include "intel_quirks.h"
81 #include "intel_sideband.h"
82 #include "intel_sprite.h"
83 #include "intel_tc.h"
84 #include "intel_vga.h"
85 
86 /* Primary plane formats for gen <= 3 */
87 static const u32 i8xx_primary_formats[] = {
88 	DRM_FORMAT_C8,
89 	DRM_FORMAT_XRGB1555,
90 	DRM_FORMAT_RGB565,
91 	DRM_FORMAT_XRGB8888,
92 };
93 
94 /* Primary plane formats for ivb (no fp16 due to hw issue) */
95 static const u32 ivb_primary_formats[] = {
96 	DRM_FORMAT_C8,
97 	DRM_FORMAT_RGB565,
98 	DRM_FORMAT_XRGB8888,
99 	DRM_FORMAT_XBGR8888,
100 	DRM_FORMAT_XRGB2101010,
101 	DRM_FORMAT_XBGR2101010,
102 };
103 
104 /* Primary plane formats for gen >= 4, except ivb */
105 static const u32 i965_primary_formats[] = {
106 	DRM_FORMAT_C8,
107 	DRM_FORMAT_RGB565,
108 	DRM_FORMAT_XRGB8888,
109 	DRM_FORMAT_XBGR8888,
110 	DRM_FORMAT_XRGB2101010,
111 	DRM_FORMAT_XBGR2101010,
112 	DRM_FORMAT_XBGR16161616F,
113 };
114 
115 /* Primary plane formats for vlv/chv */
116 static const u32 vlv_primary_formats[] = {
117 	DRM_FORMAT_C8,
118 	DRM_FORMAT_RGB565,
119 	DRM_FORMAT_XRGB8888,
120 	DRM_FORMAT_XBGR8888,
121 	DRM_FORMAT_ARGB8888,
122 	DRM_FORMAT_ABGR8888,
123 	DRM_FORMAT_XRGB2101010,
124 	DRM_FORMAT_XBGR2101010,
125 	DRM_FORMAT_ARGB2101010,
126 	DRM_FORMAT_ABGR2101010,
127 	DRM_FORMAT_XBGR16161616F,
128 };
129 
130 static const u64 i9xx_format_modifiers[] = {
131 	I915_FORMAT_MOD_X_TILED,
132 	DRM_FORMAT_MOD_LINEAR,
133 	DRM_FORMAT_MOD_INVALID
134 };
135 
136 /* Cursor formats */
137 static const u32 intel_cursor_formats[] = {
138 	DRM_FORMAT_ARGB8888,
139 };
140 
141 static const u64 cursor_format_modifiers[] = {
142 	DRM_FORMAT_MOD_LINEAR,
143 	DRM_FORMAT_MOD_INVALID
144 };
145 
146 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
147 				struct intel_crtc_state *pipe_config);
148 static void ilk_pch_clock_get(struct intel_crtc *crtc,
149 			      struct intel_crtc_state *pipe_config);
150 
151 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
152 				  struct drm_i915_gem_object *obj,
153 				  struct drm_mode_fb_cmd2 *mode_cmd);
154 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
155 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
156 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
157 					 const struct intel_link_m_n *m_n,
158 					 const struct intel_link_m_n *m2_n2);
159 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
160 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
161 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
162 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
163 static void vlv_prepare_pll(struct intel_crtc *crtc,
164 			    const struct intel_crtc_state *pipe_config);
165 static void chv_prepare_pll(struct intel_crtc *crtc,
166 			    const struct intel_crtc_state *pipe_config);
167 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
168 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
169 static void intel_modeset_setup_hw_state(struct drm_device *dev,
170 					 struct drm_modeset_acquire_ctx *ctx);
171 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
172 
173 struct intel_limit {
174 	struct {
175 		int min, max;
176 	} dot, vco, n, m, m1, m2, p, p1;
177 
178 	struct {
179 		int dot_limit;
180 		int p2_slow, p2_fast;
181 	} p2;
182 };
183 
184 /* returns HPLL frequency in kHz */
185 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
186 {
187 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
188 
189 	/* Obtain SKU information */
190 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
191 		CCK_FUSE_HPLL_FREQ_MASK;
192 
193 	return vco_freq[hpll_freq] * 1000;
194 }
195 
196 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
197 		      const char *name, u32 reg, int ref_freq)
198 {
199 	u32 val;
200 	int divider;
201 
202 	val = vlv_cck_read(dev_priv, reg);
203 	divider = val & CCK_FREQUENCY_VALUES;
204 
205 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
206 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
207 		 "%s change in progress\n", name);
208 
209 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
210 }
211 
212 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
213 			   const char *name, u32 reg)
214 {
215 	int hpll;
216 
217 	vlv_cck_get(dev_priv);
218 
219 	if (dev_priv->hpll_freq == 0)
220 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
221 
222 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
223 
224 	vlv_cck_put(dev_priv);
225 
226 	return hpll;
227 }
228 
229 static void intel_update_czclk(struct drm_i915_private *dev_priv)
230 {
231 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
232 		return;
233 
234 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
235 						      CCK_CZ_CLOCK_CONTROL);
236 
237 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
238 		dev_priv->czclk_freq);
239 }
240 
241 /* units of 100MHz */
242 static u32 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
243 			       const struct intel_crtc_state *pipe_config)
244 {
245 	if (HAS_DDI(dev_priv))
246 		return pipe_config->port_clock; /* SPLL */
247 	else
248 		return dev_priv->fdi_pll_freq;
249 }
250 
251 static const struct intel_limit intel_limits_i8xx_dac = {
252 	.dot = { .min = 25000, .max = 350000 },
253 	.vco = { .min = 908000, .max = 1512000 },
254 	.n = { .min = 2, .max = 16 },
255 	.m = { .min = 96, .max = 140 },
256 	.m1 = { .min = 18, .max = 26 },
257 	.m2 = { .min = 6, .max = 16 },
258 	.p = { .min = 4, .max = 128 },
259 	.p1 = { .min = 2, .max = 33 },
260 	.p2 = { .dot_limit = 165000,
261 		.p2_slow = 4, .p2_fast = 2 },
262 };
263 
264 static const struct intel_limit intel_limits_i8xx_dvo = {
265 	.dot = { .min = 25000, .max = 350000 },
266 	.vco = { .min = 908000, .max = 1512000 },
267 	.n = { .min = 2, .max = 16 },
268 	.m = { .min = 96, .max = 140 },
269 	.m1 = { .min = 18, .max = 26 },
270 	.m2 = { .min = 6, .max = 16 },
271 	.p = { .min = 4, .max = 128 },
272 	.p1 = { .min = 2, .max = 33 },
273 	.p2 = { .dot_limit = 165000,
274 		.p2_slow = 4, .p2_fast = 4 },
275 };
276 
277 static const struct intel_limit intel_limits_i8xx_lvds = {
278 	.dot = { .min = 25000, .max = 350000 },
279 	.vco = { .min = 908000, .max = 1512000 },
280 	.n = { .min = 2, .max = 16 },
281 	.m = { .min = 96, .max = 140 },
282 	.m1 = { .min = 18, .max = 26 },
283 	.m2 = { .min = 6, .max = 16 },
284 	.p = { .min = 4, .max = 128 },
285 	.p1 = { .min = 1, .max = 6 },
286 	.p2 = { .dot_limit = 165000,
287 		.p2_slow = 14, .p2_fast = 7 },
288 };
289 
290 static const struct intel_limit intel_limits_i9xx_sdvo = {
291 	.dot = { .min = 20000, .max = 400000 },
292 	.vco = { .min = 1400000, .max = 2800000 },
293 	.n = { .min = 1, .max = 6 },
294 	.m = { .min = 70, .max = 120 },
295 	.m1 = { .min = 8, .max = 18 },
296 	.m2 = { .min = 3, .max = 7 },
297 	.p = { .min = 5, .max = 80 },
298 	.p1 = { .min = 1, .max = 8 },
299 	.p2 = { .dot_limit = 200000,
300 		.p2_slow = 10, .p2_fast = 5 },
301 };
302 
303 static const struct intel_limit intel_limits_i9xx_lvds = {
304 	.dot = { .min = 20000, .max = 400000 },
305 	.vco = { .min = 1400000, .max = 2800000 },
306 	.n = { .min = 1, .max = 6 },
307 	.m = { .min = 70, .max = 120 },
308 	.m1 = { .min = 8, .max = 18 },
309 	.m2 = { .min = 3, .max = 7 },
310 	.p = { .min = 7, .max = 98 },
311 	.p1 = { .min = 1, .max = 8 },
312 	.p2 = { .dot_limit = 112000,
313 		.p2_slow = 14, .p2_fast = 7 },
314 };
315 
316 
317 static const struct intel_limit intel_limits_g4x_sdvo = {
318 	.dot = { .min = 25000, .max = 270000 },
319 	.vco = { .min = 1750000, .max = 3500000},
320 	.n = { .min = 1, .max = 4 },
321 	.m = { .min = 104, .max = 138 },
322 	.m1 = { .min = 17, .max = 23 },
323 	.m2 = { .min = 5, .max = 11 },
324 	.p = { .min = 10, .max = 30 },
325 	.p1 = { .min = 1, .max = 3},
326 	.p2 = { .dot_limit = 270000,
327 		.p2_slow = 10,
328 		.p2_fast = 10
329 	},
330 };
331 
332 static const struct intel_limit intel_limits_g4x_hdmi = {
333 	.dot = { .min = 22000, .max = 400000 },
334 	.vco = { .min = 1750000, .max = 3500000},
335 	.n = { .min = 1, .max = 4 },
336 	.m = { .min = 104, .max = 138 },
337 	.m1 = { .min = 16, .max = 23 },
338 	.m2 = { .min = 5, .max = 11 },
339 	.p = { .min = 5, .max = 80 },
340 	.p1 = { .min = 1, .max = 8},
341 	.p2 = { .dot_limit = 165000,
342 		.p2_slow = 10, .p2_fast = 5 },
343 };
344 
345 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
346 	.dot = { .min = 20000, .max = 115000 },
347 	.vco = { .min = 1750000, .max = 3500000 },
348 	.n = { .min = 1, .max = 3 },
349 	.m = { .min = 104, .max = 138 },
350 	.m1 = { .min = 17, .max = 23 },
351 	.m2 = { .min = 5, .max = 11 },
352 	.p = { .min = 28, .max = 112 },
353 	.p1 = { .min = 2, .max = 8 },
354 	.p2 = { .dot_limit = 0,
355 		.p2_slow = 14, .p2_fast = 14
356 	},
357 };
358 
359 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
360 	.dot = { .min = 80000, .max = 224000 },
361 	.vco = { .min = 1750000, .max = 3500000 },
362 	.n = { .min = 1, .max = 3 },
363 	.m = { .min = 104, .max = 138 },
364 	.m1 = { .min = 17, .max = 23 },
365 	.m2 = { .min = 5, .max = 11 },
366 	.p = { .min = 14, .max = 42 },
367 	.p1 = { .min = 2, .max = 6 },
368 	.p2 = { .dot_limit = 0,
369 		.p2_slow = 7, .p2_fast = 7
370 	},
371 };
372 
373 static const struct intel_limit pnv_limits_sdvo = {
374 	.dot = { .min = 20000, .max = 400000},
375 	.vco = { .min = 1700000, .max = 3500000 },
376 	/* Pineview's Ncounter is a ring counter */
377 	.n = { .min = 3, .max = 6 },
378 	.m = { .min = 2, .max = 256 },
379 	/* Pineview only has one combined m divider, which we treat as m2. */
380 	.m1 = { .min = 0, .max = 0 },
381 	.m2 = { .min = 0, .max = 254 },
382 	.p = { .min = 5, .max = 80 },
383 	.p1 = { .min = 1, .max = 8 },
384 	.p2 = { .dot_limit = 200000,
385 		.p2_slow = 10, .p2_fast = 5 },
386 };
387 
388 static const struct intel_limit pnv_limits_lvds = {
389 	.dot = { .min = 20000, .max = 400000 },
390 	.vco = { .min = 1700000, .max = 3500000 },
391 	.n = { .min = 3, .max = 6 },
392 	.m = { .min = 2, .max = 256 },
393 	.m1 = { .min = 0, .max = 0 },
394 	.m2 = { .min = 0, .max = 254 },
395 	.p = { .min = 7, .max = 112 },
396 	.p1 = { .min = 1, .max = 8 },
397 	.p2 = { .dot_limit = 112000,
398 		.p2_slow = 14, .p2_fast = 14 },
399 };
400 
401 /* Ironlake / Sandybridge
402  *
403  * We calculate clock using (register_value + 2) for N/M1/M2, so here
404  * the range value for them is (actual_value - 2).
405  */
406 static const struct intel_limit ilk_limits_dac = {
407 	.dot = { .min = 25000, .max = 350000 },
408 	.vco = { .min = 1760000, .max = 3510000 },
409 	.n = { .min = 1, .max = 5 },
410 	.m = { .min = 79, .max = 127 },
411 	.m1 = { .min = 12, .max = 22 },
412 	.m2 = { .min = 5, .max = 9 },
413 	.p = { .min = 5, .max = 80 },
414 	.p1 = { .min = 1, .max = 8 },
415 	.p2 = { .dot_limit = 225000,
416 		.p2_slow = 10, .p2_fast = 5 },
417 };
418 
419 static const struct intel_limit ilk_limits_single_lvds = {
420 	.dot = { .min = 25000, .max = 350000 },
421 	.vco = { .min = 1760000, .max = 3510000 },
422 	.n = { .min = 1, .max = 3 },
423 	.m = { .min = 79, .max = 118 },
424 	.m1 = { .min = 12, .max = 22 },
425 	.m2 = { .min = 5, .max = 9 },
426 	.p = { .min = 28, .max = 112 },
427 	.p1 = { .min = 2, .max = 8 },
428 	.p2 = { .dot_limit = 225000,
429 		.p2_slow = 14, .p2_fast = 14 },
430 };
431 
432 static const struct intel_limit ilk_limits_dual_lvds = {
433 	.dot = { .min = 25000, .max = 350000 },
434 	.vco = { .min = 1760000, .max = 3510000 },
435 	.n = { .min = 1, .max = 3 },
436 	.m = { .min = 79, .max = 127 },
437 	.m1 = { .min = 12, .max = 22 },
438 	.m2 = { .min = 5, .max = 9 },
439 	.p = { .min = 14, .max = 56 },
440 	.p1 = { .min = 2, .max = 8 },
441 	.p2 = { .dot_limit = 225000,
442 		.p2_slow = 7, .p2_fast = 7 },
443 };
444 
445 /* LVDS 100mhz refclk limits. */
446 static const struct intel_limit ilk_limits_single_lvds_100m = {
447 	.dot = { .min = 25000, .max = 350000 },
448 	.vco = { .min = 1760000, .max = 3510000 },
449 	.n = { .min = 1, .max = 2 },
450 	.m = { .min = 79, .max = 126 },
451 	.m1 = { .min = 12, .max = 22 },
452 	.m2 = { .min = 5, .max = 9 },
453 	.p = { .min = 28, .max = 112 },
454 	.p1 = { .min = 2, .max = 8 },
455 	.p2 = { .dot_limit = 225000,
456 		.p2_slow = 14, .p2_fast = 14 },
457 };
458 
459 static const struct intel_limit ilk_limits_dual_lvds_100m = {
460 	.dot = { .min = 25000, .max = 350000 },
461 	.vco = { .min = 1760000, .max = 3510000 },
462 	.n = { .min = 1, .max = 3 },
463 	.m = { .min = 79, .max = 126 },
464 	.m1 = { .min = 12, .max = 22 },
465 	.m2 = { .min = 5, .max = 9 },
466 	.p = { .min = 14, .max = 42 },
467 	.p1 = { .min = 2, .max = 6 },
468 	.p2 = { .dot_limit = 225000,
469 		.p2_slow = 7, .p2_fast = 7 },
470 };
471 
472 static const struct intel_limit intel_limits_vlv = {
473 	 /*
474 	  * These are the data rate limits (measured in fast clocks)
475 	  * since those are the strictest limits we have. The fast
476 	  * clock and actual rate limits are more relaxed, so checking
477 	  * them would make no difference.
478 	  */
479 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
480 	.vco = { .min = 4000000, .max = 6000000 },
481 	.n = { .min = 1, .max = 7 },
482 	.m1 = { .min = 2, .max = 3 },
483 	.m2 = { .min = 11, .max = 156 },
484 	.p1 = { .min = 2, .max = 3 },
485 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
486 };
487 
488 static const struct intel_limit intel_limits_chv = {
489 	/*
490 	 * These are the data rate limits (measured in fast clocks)
491 	 * since those are the strictest limits we have.  The fast
492 	 * clock and actual rate limits are more relaxed, so checking
493 	 * them would make no difference.
494 	 */
495 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
496 	.vco = { .min = 4800000, .max = 6480000 },
497 	.n = { .min = 1, .max = 1 },
498 	.m1 = { .min = 2, .max = 2 },
499 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
500 	.p1 = { .min = 2, .max = 4 },
501 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
502 };
503 
504 static const struct intel_limit intel_limits_bxt = {
505 	/* FIXME: find real dot limits */
506 	.dot = { .min = 0, .max = INT_MAX },
507 	.vco = { .min = 4800000, .max = 6700000 },
508 	.n = { .min = 1, .max = 1 },
509 	.m1 = { .min = 2, .max = 2 },
510 	/* FIXME: find real m2 limits */
511 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
512 	.p1 = { .min = 2, .max = 4 },
513 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
514 };
515 
516 /* WA Display #0827: Gen9:all */
517 static void
518 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
519 {
520 	if (enable)
521 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
522 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
523 	else
524 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
525 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
526 }
527 
528 /* Wa_2006604312:icl,ehl */
529 static void
530 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
531 		       bool enable)
532 {
533 	if (enable)
534 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
535 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
536 	else
537 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
538 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
539 }
540 
541 static bool
542 needs_modeset(const struct intel_crtc_state *state)
543 {
544 	return drm_atomic_crtc_needs_modeset(&state->uapi);
545 }
546 
547 static bool
548 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
549 {
550 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
551 }
552 
553 static bool
554 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
555 {
556 	return crtc_state->sync_mode_slaves_mask != 0;
557 }
558 
559 bool
560 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
561 {
562 	return is_trans_port_sync_master(crtc_state) ||
563 		is_trans_port_sync_slave(crtc_state);
564 }
565 
566 /*
567  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
568  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
569  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
570  * The helpers' return value is the rate of the clock that is fed to the
571  * display engine's pipe which can be the above fast dot clock rate or a
572  * divided-down version of it.
573  */
574 /* m1 is reserved as 0 in Pineview, n is a ring counter */
575 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
576 {
577 	clock->m = clock->m2 + 2;
578 	clock->p = clock->p1 * clock->p2;
579 	if (WARN_ON(clock->n == 0 || clock->p == 0))
580 		return 0;
581 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
582 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
583 
584 	return clock->dot;
585 }
586 
587 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
588 {
589 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
590 }
591 
592 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
593 {
594 	clock->m = i9xx_dpll_compute_m(clock);
595 	clock->p = clock->p1 * clock->p2;
596 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
597 		return 0;
598 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
599 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
600 
601 	return clock->dot;
602 }
603 
604 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
605 {
606 	clock->m = clock->m1 * clock->m2;
607 	clock->p = clock->p1 * clock->p2;
608 	if (WARN_ON(clock->n == 0 || clock->p == 0))
609 		return 0;
610 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
611 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
612 
613 	return clock->dot / 5;
614 }
615 
616 int chv_calc_dpll_params(int refclk, struct dpll *clock)
617 {
618 	clock->m = clock->m1 * clock->m2;
619 	clock->p = clock->p1 * clock->p2;
620 	if (WARN_ON(clock->n == 0 || clock->p == 0))
621 		return 0;
622 	clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
623 					   clock->n << 22);
624 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
625 
626 	return clock->dot / 5;
627 }
628 
629 /*
630  * Returns whether the given set of divisors are valid for a given refclk with
631  * the given connectors.
632  */
633 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
634 			       const struct intel_limit *limit,
635 			       const struct dpll *clock)
636 {
637 	if (clock->n < limit->n.min || limit->n.max < clock->n)
638 		return false;
639 	if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
640 		return false;
641 	if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
642 		return false;
643 	if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
644 		return false;
645 
646 	if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
647 	    !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
648 		if (clock->m1 <= clock->m2)
649 			return false;
650 
651 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
652 	    !IS_GEN9_LP(dev_priv)) {
653 		if (clock->p < limit->p.min || limit->p.max < clock->p)
654 			return false;
655 		if (clock->m < limit->m.min || limit->m.max < clock->m)
656 			return false;
657 	}
658 
659 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
660 		return false;
661 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
662 	 * connector, etc., rather than just a single range.
663 	 */
664 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
665 		return false;
666 
667 	return true;
668 }
669 
670 static int
671 i9xx_select_p2_div(const struct intel_limit *limit,
672 		   const struct intel_crtc_state *crtc_state,
673 		   int target)
674 {
675 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
676 
677 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
678 		/*
679 		 * For LVDS just rely on its current settings for dual-channel.
680 		 * We haven't figured out how to reliably set up different
681 		 * single/dual channel state, if we even can.
682 		 */
683 		if (intel_is_dual_link_lvds(dev_priv))
684 			return limit->p2.p2_fast;
685 		else
686 			return limit->p2.p2_slow;
687 	} else {
688 		if (target < limit->p2.dot_limit)
689 			return limit->p2.p2_slow;
690 		else
691 			return limit->p2.p2_fast;
692 	}
693 }
694 
695 /*
696  * Returns a set of divisors for the desired target clock with the given
697  * refclk, or FALSE.  The returned values represent the clock equation:
698  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
699  *
700  * Target and reference clocks are specified in kHz.
701  *
702  * If match_clock is provided, then best_clock P divider must match the P
703  * divider from @match_clock used for LVDS downclocking.
704  */
705 static bool
706 i9xx_find_best_dpll(const struct intel_limit *limit,
707 		    struct intel_crtc_state *crtc_state,
708 		    int target, int refclk, struct dpll *match_clock,
709 		    struct dpll *best_clock)
710 {
711 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
712 	struct dpll clock;
713 	int err = target;
714 
715 	memset(best_clock, 0, sizeof(*best_clock));
716 
717 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
718 
719 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
720 	     clock.m1++) {
721 		for (clock.m2 = limit->m2.min;
722 		     clock.m2 <= limit->m2.max; clock.m2++) {
723 			if (clock.m2 >= clock.m1)
724 				break;
725 			for (clock.n = limit->n.min;
726 			     clock.n <= limit->n.max; clock.n++) {
727 				for (clock.p1 = limit->p1.min;
728 					clock.p1 <= limit->p1.max; clock.p1++) {
729 					int this_err;
730 
731 					i9xx_calc_dpll_params(refclk, &clock);
732 					if (!intel_pll_is_valid(to_i915(dev),
733 								limit,
734 								&clock))
735 						continue;
736 					if (match_clock &&
737 					    clock.p != match_clock->p)
738 						continue;
739 
740 					this_err = abs(clock.dot - target);
741 					if (this_err < err) {
742 						*best_clock = clock;
743 						err = this_err;
744 					}
745 				}
746 			}
747 		}
748 	}
749 
750 	return (err != target);
751 }
752 
753 /*
754  * Returns a set of divisors for the desired target clock with the given
755  * refclk, or FALSE.  The returned values represent the clock equation:
756  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
757  *
758  * Target and reference clocks are specified in kHz.
759  *
760  * If match_clock is provided, then best_clock P divider must match the P
761  * divider from @match_clock used for LVDS downclocking.
762  */
763 static bool
764 pnv_find_best_dpll(const struct intel_limit *limit,
765 		   struct intel_crtc_state *crtc_state,
766 		   int target, int refclk, struct dpll *match_clock,
767 		   struct dpll *best_clock)
768 {
769 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
770 	struct dpll clock;
771 	int err = target;
772 
773 	memset(best_clock, 0, sizeof(*best_clock));
774 
775 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
776 
777 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
778 	     clock.m1++) {
779 		for (clock.m2 = limit->m2.min;
780 		     clock.m2 <= limit->m2.max; clock.m2++) {
781 			for (clock.n = limit->n.min;
782 			     clock.n <= limit->n.max; clock.n++) {
783 				for (clock.p1 = limit->p1.min;
784 					clock.p1 <= limit->p1.max; clock.p1++) {
785 					int this_err;
786 
787 					pnv_calc_dpll_params(refclk, &clock);
788 					if (!intel_pll_is_valid(to_i915(dev),
789 								limit,
790 								&clock))
791 						continue;
792 					if (match_clock &&
793 					    clock.p != match_clock->p)
794 						continue;
795 
796 					this_err = abs(clock.dot - target);
797 					if (this_err < err) {
798 						*best_clock = clock;
799 						err = this_err;
800 					}
801 				}
802 			}
803 		}
804 	}
805 
806 	return (err != target);
807 }
808 
809 /*
810  * Returns a set of divisors for the desired target clock with the given
811  * refclk, or FALSE.  The returned values represent the clock equation:
812  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
813  *
814  * Target and reference clocks are specified in kHz.
815  *
816  * If match_clock is provided, then best_clock P divider must match the P
817  * divider from @match_clock used for LVDS downclocking.
818  */
819 static bool
820 g4x_find_best_dpll(const struct intel_limit *limit,
821 		   struct intel_crtc_state *crtc_state,
822 		   int target, int refclk, struct dpll *match_clock,
823 		   struct dpll *best_clock)
824 {
825 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
826 	struct dpll clock;
827 	int max_n;
828 	bool found = false;
829 	/* approximately equals target * 0.00585 */
830 	int err_most = (target >> 8) + (target >> 9);
831 
832 	memset(best_clock, 0, sizeof(*best_clock));
833 
834 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
835 
836 	max_n = limit->n.max;
837 	/* based on hardware requirement, prefer smaller n to precision */
838 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
839 		/* based on hardware requirement, prefere larger m1,m2 */
840 		for (clock.m1 = limit->m1.max;
841 		     clock.m1 >= limit->m1.min; clock.m1--) {
842 			for (clock.m2 = limit->m2.max;
843 			     clock.m2 >= limit->m2.min; clock.m2--) {
844 				for (clock.p1 = limit->p1.max;
845 				     clock.p1 >= limit->p1.min; clock.p1--) {
846 					int this_err;
847 
848 					i9xx_calc_dpll_params(refclk, &clock);
849 					if (!intel_pll_is_valid(to_i915(dev),
850 								limit,
851 								&clock))
852 						continue;
853 
854 					this_err = abs(clock.dot - target);
855 					if (this_err < err_most) {
856 						*best_clock = clock;
857 						err_most = this_err;
858 						max_n = clock.n;
859 						found = true;
860 					}
861 				}
862 			}
863 		}
864 	}
865 	return found;
866 }
867 
868 /*
869  * Check if the calculated PLL configuration is more optimal compared to the
870  * best configuration and error found so far. Return the calculated error.
871  */
872 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
873 			       const struct dpll *calculated_clock,
874 			       const struct dpll *best_clock,
875 			       unsigned int best_error_ppm,
876 			       unsigned int *error_ppm)
877 {
878 	/*
879 	 * For CHV ignore the error and consider only the P value.
880 	 * Prefer a bigger P value based on HW requirements.
881 	 */
882 	if (IS_CHERRYVIEW(to_i915(dev))) {
883 		*error_ppm = 0;
884 
885 		return calculated_clock->p > best_clock->p;
886 	}
887 
888 	if (drm_WARN_ON_ONCE(dev, !target_freq))
889 		return false;
890 
891 	*error_ppm = div_u64(1000000ULL *
892 				abs(target_freq - calculated_clock->dot),
893 			     target_freq);
894 	/*
895 	 * Prefer a better P value over a better (smaller) error if the error
896 	 * is small. Ensure this preference for future configurations too by
897 	 * setting the error to 0.
898 	 */
899 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
900 		*error_ppm = 0;
901 
902 		return true;
903 	}
904 
905 	return *error_ppm + 10 < best_error_ppm;
906 }
907 
908 /*
909  * Returns a set of divisors for the desired target clock with the given
910  * refclk, or FALSE.  The returned values represent the clock equation:
911  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
912  */
913 static bool
914 vlv_find_best_dpll(const struct intel_limit *limit,
915 		   struct intel_crtc_state *crtc_state,
916 		   int target, int refclk, struct dpll *match_clock,
917 		   struct dpll *best_clock)
918 {
919 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
920 	struct drm_device *dev = crtc->base.dev;
921 	struct dpll clock;
922 	unsigned int bestppm = 1000000;
923 	/* min update 19.2 MHz */
924 	int max_n = min(limit->n.max, refclk / 19200);
925 	bool found = false;
926 
927 	target *= 5; /* fast clock */
928 
929 	memset(best_clock, 0, sizeof(*best_clock));
930 
931 	/* based on hardware requirement, prefer smaller n to precision */
932 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
933 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
934 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
935 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
936 				clock.p = clock.p1 * clock.p2;
937 				/* based on hardware requirement, prefer bigger m1,m2 values */
938 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
939 					unsigned int ppm;
940 
941 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
942 								     refclk * clock.m1);
943 
944 					vlv_calc_dpll_params(refclk, &clock);
945 
946 					if (!intel_pll_is_valid(to_i915(dev),
947 								limit,
948 								&clock))
949 						continue;
950 
951 					if (!vlv_PLL_is_optimal(dev, target,
952 								&clock,
953 								best_clock,
954 								bestppm, &ppm))
955 						continue;
956 
957 					*best_clock = clock;
958 					bestppm = ppm;
959 					found = true;
960 				}
961 			}
962 		}
963 	}
964 
965 	return found;
966 }
967 
968 /*
969  * Returns a set of divisors for the desired target clock with the given
970  * refclk, or FALSE.  The returned values represent the clock equation:
971  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
972  */
973 static bool
974 chv_find_best_dpll(const struct intel_limit *limit,
975 		   struct intel_crtc_state *crtc_state,
976 		   int target, int refclk, struct dpll *match_clock,
977 		   struct dpll *best_clock)
978 {
979 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
980 	struct drm_device *dev = crtc->base.dev;
981 	unsigned int best_error_ppm;
982 	struct dpll clock;
983 	u64 m2;
984 	int found = false;
985 
986 	memset(best_clock, 0, sizeof(*best_clock));
987 	best_error_ppm = 1000000;
988 
989 	/*
990 	 * Based on hardware doc, the n always set to 1, and m1 always
991 	 * set to 2.  If requires to support 200Mhz refclk, we need to
992 	 * revisit this because n may not 1 anymore.
993 	 */
994 	clock.n = 1, clock.m1 = 2;
995 	target *= 5;	/* fast clock */
996 
997 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
998 		for (clock.p2 = limit->p2.p2_fast;
999 				clock.p2 >= limit->p2.p2_slow;
1000 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
1001 			unsigned int error_ppm;
1002 
1003 			clock.p = clock.p1 * clock.p2;
1004 
1005 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1006 						   refclk * clock.m1);
1007 
1008 			if (m2 > INT_MAX/clock.m1)
1009 				continue;
1010 
1011 			clock.m2 = m2;
1012 
1013 			chv_calc_dpll_params(refclk, &clock);
1014 
1015 			if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
1016 				continue;
1017 
1018 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1019 						best_error_ppm, &error_ppm))
1020 				continue;
1021 
1022 			*best_clock = clock;
1023 			best_error_ppm = error_ppm;
1024 			found = true;
1025 		}
1026 	}
1027 
1028 	return found;
1029 }
1030 
1031 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
1032 			struct dpll *best_clock)
1033 {
1034 	int refclk = 100000;
1035 	const struct intel_limit *limit = &intel_limits_bxt;
1036 
1037 	return chv_find_best_dpll(limit, crtc_state,
1038 				  crtc_state->port_clock, refclk,
1039 				  NULL, best_clock);
1040 }
1041 
1042 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1043 				    enum pipe pipe)
1044 {
1045 	i915_reg_t reg = PIPEDSL(pipe);
1046 	u32 line1, line2;
1047 	u32 line_mask;
1048 
1049 	if (IS_GEN(dev_priv, 2))
1050 		line_mask = DSL_LINEMASK_GEN2;
1051 	else
1052 		line_mask = DSL_LINEMASK_GEN3;
1053 
1054 	line1 = intel_de_read(dev_priv, reg) & line_mask;
1055 	msleep(5);
1056 	line2 = intel_de_read(dev_priv, reg) & line_mask;
1057 
1058 	return line1 != line2;
1059 }
1060 
1061 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1062 {
1063 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1064 	enum pipe pipe = crtc->pipe;
1065 
1066 	/* Wait for the display line to settle/start moving */
1067 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1068 		drm_err(&dev_priv->drm,
1069 			"pipe %c scanline %s wait timed out\n",
1070 			pipe_name(pipe), onoff(state));
1071 }
1072 
1073 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1074 {
1075 	wait_for_pipe_scanline_moving(crtc, false);
1076 }
1077 
1078 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1079 {
1080 	wait_for_pipe_scanline_moving(crtc, true);
1081 }
1082 
1083 static void
1084 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1085 {
1086 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1087 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1088 
1089 	if (INTEL_GEN(dev_priv) >= 4) {
1090 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1091 		i915_reg_t reg = PIPECONF(cpu_transcoder);
1092 
1093 		/* Wait for the Pipe State to go off */
1094 		if (intel_de_wait_for_clear(dev_priv, reg,
1095 					    I965_PIPECONF_ACTIVE, 100))
1096 			drm_WARN(&dev_priv->drm, 1,
1097 				 "pipe_off wait timed out\n");
1098 	} else {
1099 		intel_wait_for_pipe_scanline_stopped(crtc);
1100 	}
1101 }
1102 
1103 /* Only for pre-ILK configs */
1104 void assert_pll(struct drm_i915_private *dev_priv,
1105 		enum pipe pipe, bool state)
1106 {
1107 	u32 val;
1108 	bool cur_state;
1109 
1110 	val = intel_de_read(dev_priv, DPLL(pipe));
1111 	cur_state = !!(val & DPLL_VCO_ENABLE);
1112 	I915_STATE_WARN(cur_state != state,
1113 	     "PLL state assertion failure (expected %s, current %s)\n",
1114 			onoff(state), onoff(cur_state));
1115 }
1116 
1117 /* XXX: the dsi pll is shared between MIPI DSI ports */
1118 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1119 {
1120 	u32 val;
1121 	bool cur_state;
1122 
1123 	vlv_cck_get(dev_priv);
1124 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1125 	vlv_cck_put(dev_priv);
1126 
1127 	cur_state = val & DSI_PLL_VCO_EN;
1128 	I915_STATE_WARN(cur_state != state,
1129 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1130 			onoff(state), onoff(cur_state));
1131 }
1132 
1133 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1134 			  enum pipe pipe, bool state)
1135 {
1136 	bool cur_state;
1137 
1138 	if (HAS_DDI(dev_priv)) {
1139 		/*
1140 		 * DDI does not have a specific FDI_TX register.
1141 		 *
1142 		 * FDI is never fed from EDP transcoder
1143 		 * so pipe->transcoder cast is fine here.
1144 		 */
1145 		enum transcoder cpu_transcoder = (enum transcoder)pipe;
1146 		u32 val = intel_de_read(dev_priv,
1147 					TRANS_DDI_FUNC_CTL(cpu_transcoder));
1148 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1149 	} else {
1150 		u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1151 		cur_state = !!(val & FDI_TX_ENABLE);
1152 	}
1153 	I915_STATE_WARN(cur_state != state,
1154 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1155 			onoff(state), onoff(cur_state));
1156 }
1157 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1158 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1159 
1160 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1161 			  enum pipe pipe, bool state)
1162 {
1163 	u32 val;
1164 	bool cur_state;
1165 
1166 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1167 	cur_state = !!(val & FDI_RX_ENABLE);
1168 	I915_STATE_WARN(cur_state != state,
1169 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1170 			onoff(state), onoff(cur_state));
1171 }
1172 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1173 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1174 
1175 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1176 				      enum pipe pipe)
1177 {
1178 	u32 val;
1179 
1180 	/* ILK FDI PLL is always enabled */
1181 	if (IS_GEN(dev_priv, 5))
1182 		return;
1183 
1184 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1185 	if (HAS_DDI(dev_priv))
1186 		return;
1187 
1188 	val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1189 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1190 }
1191 
1192 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1193 		       enum pipe pipe, bool state)
1194 {
1195 	u32 val;
1196 	bool cur_state;
1197 
1198 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1199 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1200 	I915_STATE_WARN(cur_state != state,
1201 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1202 			onoff(state), onoff(cur_state));
1203 }
1204 
1205 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1206 {
1207 	i915_reg_t pp_reg;
1208 	u32 val;
1209 	enum pipe panel_pipe = INVALID_PIPE;
1210 	bool locked = true;
1211 
1212 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1213 		return;
1214 
1215 	if (HAS_PCH_SPLIT(dev_priv)) {
1216 		u32 port_sel;
1217 
1218 		pp_reg = PP_CONTROL(0);
1219 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1220 
1221 		switch (port_sel) {
1222 		case PANEL_PORT_SELECT_LVDS:
1223 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1224 			break;
1225 		case PANEL_PORT_SELECT_DPA:
1226 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1227 			break;
1228 		case PANEL_PORT_SELECT_DPC:
1229 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1230 			break;
1231 		case PANEL_PORT_SELECT_DPD:
1232 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1233 			break;
1234 		default:
1235 			MISSING_CASE(port_sel);
1236 			break;
1237 		}
1238 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1239 		/* presumably write lock depends on pipe, not port select */
1240 		pp_reg = PP_CONTROL(pipe);
1241 		panel_pipe = pipe;
1242 	} else {
1243 		u32 port_sel;
1244 
1245 		pp_reg = PP_CONTROL(0);
1246 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1247 
1248 		drm_WARN_ON(&dev_priv->drm,
1249 			    port_sel != PANEL_PORT_SELECT_LVDS);
1250 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1251 	}
1252 
1253 	val = intel_de_read(dev_priv, pp_reg);
1254 	if (!(val & PANEL_POWER_ON) ||
1255 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1256 		locked = false;
1257 
1258 	I915_STATE_WARN(panel_pipe == pipe && locked,
1259 	     "panel assertion failure, pipe %c regs locked\n",
1260 	     pipe_name(pipe));
1261 }
1262 
1263 void assert_pipe(struct drm_i915_private *dev_priv,
1264 		 enum transcoder cpu_transcoder, bool state)
1265 {
1266 	bool cur_state;
1267 	enum intel_display_power_domain power_domain;
1268 	intel_wakeref_t wakeref;
1269 
1270 	/* we keep both pipes enabled on 830 */
1271 	if (IS_I830(dev_priv))
1272 		state = true;
1273 
1274 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1275 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1276 	if (wakeref) {
1277 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1278 		cur_state = !!(val & PIPECONF_ENABLE);
1279 
1280 		intel_display_power_put(dev_priv, power_domain, wakeref);
1281 	} else {
1282 		cur_state = false;
1283 	}
1284 
1285 	I915_STATE_WARN(cur_state != state,
1286 			"transcoder %s assertion failure (expected %s, current %s)\n",
1287 			transcoder_name(cpu_transcoder),
1288 			onoff(state), onoff(cur_state));
1289 }
1290 
1291 static void assert_plane(struct intel_plane *plane, bool state)
1292 {
1293 	enum pipe pipe;
1294 	bool cur_state;
1295 
1296 	cur_state = plane->get_hw_state(plane, &pipe);
1297 
1298 	I915_STATE_WARN(cur_state != state,
1299 			"%s assertion failure (expected %s, current %s)\n",
1300 			plane->base.name, onoff(state), onoff(cur_state));
1301 }
1302 
1303 #define assert_plane_enabled(p) assert_plane(p, true)
1304 #define assert_plane_disabled(p) assert_plane(p, false)
1305 
1306 static void assert_planes_disabled(struct intel_crtc *crtc)
1307 {
1308 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1309 	struct intel_plane *plane;
1310 
1311 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1312 		assert_plane_disabled(plane);
1313 }
1314 
1315 static void assert_vblank_disabled(struct drm_crtc *crtc)
1316 {
1317 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1318 		drm_crtc_vblank_put(crtc);
1319 }
1320 
1321 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1322 				    enum pipe pipe)
1323 {
1324 	u32 val;
1325 	bool enabled;
1326 
1327 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
1328 	enabled = !!(val & TRANS_ENABLE);
1329 	I915_STATE_WARN(enabled,
1330 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1331 	     pipe_name(pipe));
1332 }
1333 
1334 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1335 				   enum pipe pipe, enum port port,
1336 				   i915_reg_t dp_reg)
1337 {
1338 	enum pipe port_pipe;
1339 	bool state;
1340 
1341 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1342 
1343 	I915_STATE_WARN(state && port_pipe == pipe,
1344 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
1345 			port_name(port), pipe_name(pipe));
1346 
1347 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1348 			"IBX PCH DP %c still using transcoder B\n",
1349 			port_name(port));
1350 }
1351 
1352 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1353 				     enum pipe pipe, enum port port,
1354 				     i915_reg_t hdmi_reg)
1355 {
1356 	enum pipe port_pipe;
1357 	bool state;
1358 
1359 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1360 
1361 	I915_STATE_WARN(state && port_pipe == pipe,
1362 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1363 			port_name(port), pipe_name(pipe));
1364 
1365 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1366 			"IBX PCH HDMI %c still using transcoder B\n",
1367 			port_name(port));
1368 }
1369 
1370 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1371 				      enum pipe pipe)
1372 {
1373 	enum pipe port_pipe;
1374 
1375 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1376 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1377 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1378 
1379 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1380 			port_pipe == pipe,
1381 			"PCH VGA enabled on transcoder %c, should be disabled\n",
1382 			pipe_name(pipe));
1383 
1384 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1385 			port_pipe == pipe,
1386 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
1387 			pipe_name(pipe));
1388 
1389 	/* PCH SDVOB multiplex with HDMIB */
1390 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1391 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1392 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1393 }
1394 
1395 static void _vlv_enable_pll(struct intel_crtc *crtc,
1396 			    const struct intel_crtc_state *pipe_config)
1397 {
1398 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1399 	enum pipe pipe = crtc->pipe;
1400 
1401 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1402 	intel_de_posting_read(dev_priv, DPLL(pipe));
1403 	udelay(150);
1404 
1405 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1406 		drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1407 }
1408 
1409 static void vlv_enable_pll(struct intel_crtc *crtc,
1410 			   const struct intel_crtc_state *pipe_config)
1411 {
1412 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1413 	enum pipe pipe = crtc->pipe;
1414 
1415 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1416 
1417 	/* PLL is protected by panel, make sure we can write it */
1418 	assert_panel_unlocked(dev_priv, pipe);
1419 
1420 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1421 		_vlv_enable_pll(crtc, pipe_config);
1422 
1423 	intel_de_write(dev_priv, DPLL_MD(pipe),
1424 		       pipe_config->dpll_hw_state.dpll_md);
1425 	intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1426 }
1427 
1428 
1429 static void _chv_enable_pll(struct intel_crtc *crtc,
1430 			    const struct intel_crtc_state *pipe_config)
1431 {
1432 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1433 	enum pipe pipe = crtc->pipe;
1434 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1435 	u32 tmp;
1436 
1437 	vlv_dpio_get(dev_priv);
1438 
1439 	/* Enable back the 10bit clock to display controller */
1440 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1441 	tmp |= DPIO_DCLKP_EN;
1442 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1443 
1444 	vlv_dpio_put(dev_priv);
1445 
1446 	/*
1447 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1448 	 */
1449 	udelay(1);
1450 
1451 	/* Enable PLL */
1452 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1453 
1454 	/* Check PLL is locked */
1455 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1456 		drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1457 }
1458 
1459 static void chv_enable_pll(struct intel_crtc *crtc,
1460 			   const struct intel_crtc_state *pipe_config)
1461 {
1462 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1463 	enum pipe pipe = crtc->pipe;
1464 
1465 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1466 
1467 	/* PLL is protected by panel, make sure we can write it */
1468 	assert_panel_unlocked(dev_priv, pipe);
1469 
1470 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1471 		_chv_enable_pll(crtc, pipe_config);
1472 
1473 	if (pipe != PIPE_A) {
1474 		/*
1475 		 * WaPixelRepeatModeFixForC0:chv
1476 		 *
1477 		 * DPLLCMD is AWOL. Use chicken bits to propagate
1478 		 * the value from DPLLBMD to either pipe B or C.
1479 		 */
1480 		intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1481 		intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1482 			       pipe_config->dpll_hw_state.dpll_md);
1483 		intel_de_write(dev_priv, CBR4_VLV, 0);
1484 		dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1485 
1486 		/*
1487 		 * DPLLB VGA mode also seems to cause problems.
1488 		 * We should always have it disabled.
1489 		 */
1490 		drm_WARN_ON(&dev_priv->drm,
1491 			    (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1492 			     DPLL_VGA_MODE_DIS) == 0);
1493 	} else {
1494 		intel_de_write(dev_priv, DPLL_MD(pipe),
1495 			       pipe_config->dpll_hw_state.dpll_md);
1496 		intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1497 	}
1498 }
1499 
1500 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1501 {
1502 	if (IS_I830(dev_priv))
1503 		return false;
1504 
1505 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1506 }
1507 
1508 static void i9xx_enable_pll(struct intel_crtc *crtc,
1509 			    const struct intel_crtc_state *crtc_state)
1510 {
1511 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1512 	i915_reg_t reg = DPLL(crtc->pipe);
1513 	u32 dpll = crtc_state->dpll_hw_state.dpll;
1514 	int i;
1515 
1516 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1517 
1518 	/* PLL is protected by panel, make sure we can write it */
1519 	if (i9xx_has_pps(dev_priv))
1520 		assert_panel_unlocked(dev_priv, crtc->pipe);
1521 
1522 	/*
1523 	 * Apparently we need to have VGA mode enabled prior to changing
1524 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1525 	 * dividers, even though the register value does change.
1526 	 */
1527 	intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1528 	intel_de_write(dev_priv, reg, dpll);
1529 
1530 	/* Wait for the clocks to stabilize. */
1531 	intel_de_posting_read(dev_priv, reg);
1532 	udelay(150);
1533 
1534 	if (INTEL_GEN(dev_priv) >= 4) {
1535 		intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1536 			       crtc_state->dpll_hw_state.dpll_md);
1537 	} else {
1538 		/* The pixel multiplier can only be updated once the
1539 		 * DPLL is enabled and the clocks are stable.
1540 		 *
1541 		 * So write it again.
1542 		 */
1543 		intel_de_write(dev_priv, reg, dpll);
1544 	}
1545 
1546 	/* We do this three times for luck */
1547 	for (i = 0; i < 3; i++) {
1548 		intel_de_write(dev_priv, reg, dpll);
1549 		intel_de_posting_read(dev_priv, reg);
1550 		udelay(150); /* wait for warmup */
1551 	}
1552 }
1553 
1554 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1555 {
1556 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1557 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1558 	enum pipe pipe = crtc->pipe;
1559 
1560 	/* Don't disable pipe or pipe PLLs if needed */
1561 	if (IS_I830(dev_priv))
1562 		return;
1563 
1564 	/* Make sure the pipe isn't still relying on us */
1565 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1566 
1567 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1568 	intel_de_posting_read(dev_priv, DPLL(pipe));
1569 }
1570 
1571 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1572 {
1573 	u32 val;
1574 
1575 	/* Make sure the pipe isn't still relying on us */
1576 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1577 
1578 	val = DPLL_INTEGRATED_REF_CLK_VLV |
1579 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1580 	if (pipe != PIPE_A)
1581 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1582 
1583 	intel_de_write(dev_priv, DPLL(pipe), val);
1584 	intel_de_posting_read(dev_priv, DPLL(pipe));
1585 }
1586 
1587 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1588 {
1589 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1590 	u32 val;
1591 
1592 	/* Make sure the pipe isn't still relying on us */
1593 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1594 
1595 	val = DPLL_SSC_REF_CLK_CHV |
1596 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1597 	if (pipe != PIPE_A)
1598 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1599 
1600 	intel_de_write(dev_priv, DPLL(pipe), val);
1601 	intel_de_posting_read(dev_priv, DPLL(pipe));
1602 
1603 	vlv_dpio_get(dev_priv);
1604 
1605 	/* Disable 10bit clock to display controller */
1606 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1607 	val &= ~DPIO_DCLKP_EN;
1608 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1609 
1610 	vlv_dpio_put(dev_priv);
1611 }
1612 
1613 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1614 			 struct intel_digital_port *dport,
1615 			 unsigned int expected_mask)
1616 {
1617 	u32 port_mask;
1618 	i915_reg_t dpll_reg;
1619 
1620 	switch (dport->base.port) {
1621 	case PORT_B:
1622 		port_mask = DPLL_PORTB_READY_MASK;
1623 		dpll_reg = DPLL(0);
1624 		break;
1625 	case PORT_C:
1626 		port_mask = DPLL_PORTC_READY_MASK;
1627 		dpll_reg = DPLL(0);
1628 		expected_mask <<= 4;
1629 		break;
1630 	case PORT_D:
1631 		port_mask = DPLL_PORTD_READY_MASK;
1632 		dpll_reg = DPIO_PHY_STATUS;
1633 		break;
1634 	default:
1635 		BUG();
1636 	}
1637 
1638 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
1639 				       port_mask, expected_mask, 1000))
1640 		drm_WARN(&dev_priv->drm, 1,
1641 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
1642 			 dport->base.base.base.id, dport->base.base.name,
1643 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
1644 			 expected_mask);
1645 }
1646 
1647 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1648 {
1649 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1650 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1651 	enum pipe pipe = crtc->pipe;
1652 	i915_reg_t reg;
1653 	u32 val, pipeconf_val;
1654 
1655 	/* Make sure PCH DPLL is enabled */
1656 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1657 
1658 	/* FDI must be feeding us bits for PCH ports */
1659 	assert_fdi_tx_enabled(dev_priv, pipe);
1660 	assert_fdi_rx_enabled(dev_priv, pipe);
1661 
1662 	if (HAS_PCH_CPT(dev_priv)) {
1663 		reg = TRANS_CHICKEN2(pipe);
1664 		val = intel_de_read(dev_priv, reg);
1665 		/*
1666 		 * Workaround: Set the timing override bit
1667 		 * before enabling the pch transcoder.
1668 		 */
1669 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1670 		/* Configure frame start delay to match the CPU */
1671 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1672 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1673 		intel_de_write(dev_priv, reg, val);
1674 	}
1675 
1676 	reg = PCH_TRANSCONF(pipe);
1677 	val = intel_de_read(dev_priv, reg);
1678 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
1679 
1680 	if (HAS_PCH_IBX(dev_priv)) {
1681 		/* Configure frame start delay to match the CPU */
1682 		val &= ~TRANS_FRAME_START_DELAY_MASK;
1683 		val |= TRANS_FRAME_START_DELAY(0);
1684 
1685 		/*
1686 		 * Make the BPC in transcoder be consistent with
1687 		 * that in pipeconf reg. For HDMI we must use 8bpc
1688 		 * here for both 8bpc and 12bpc.
1689 		 */
1690 		val &= ~PIPECONF_BPC_MASK;
1691 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1692 			val |= PIPECONF_8BPC;
1693 		else
1694 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1695 	}
1696 
1697 	val &= ~TRANS_INTERLACE_MASK;
1698 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1699 		if (HAS_PCH_IBX(dev_priv) &&
1700 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1701 			val |= TRANS_LEGACY_INTERLACED_ILK;
1702 		else
1703 			val |= TRANS_INTERLACED;
1704 	} else {
1705 		val |= TRANS_PROGRESSIVE;
1706 	}
1707 
1708 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
1709 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1710 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1711 			pipe_name(pipe));
1712 }
1713 
1714 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1715 				      enum transcoder cpu_transcoder)
1716 {
1717 	u32 val, pipeconf_val;
1718 
1719 	/* FDI must be feeding us bits for PCH ports */
1720 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1721 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
1722 
1723 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1724 	/* Workaround: set timing override bit. */
1725 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1726 	/* Configure frame start delay to match the CPU */
1727 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1728 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1729 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1730 
1731 	val = TRANS_ENABLE;
1732 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1733 
1734 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1735 	    PIPECONF_INTERLACED_ILK)
1736 		val |= TRANS_INTERLACED;
1737 	else
1738 		val |= TRANS_PROGRESSIVE;
1739 
1740 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1741 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1742 				  TRANS_STATE_ENABLE, 100))
1743 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
1744 }
1745 
1746 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1747 				       enum pipe pipe)
1748 {
1749 	i915_reg_t reg;
1750 	u32 val;
1751 
1752 	/* FDI relies on the transcoder */
1753 	assert_fdi_tx_disabled(dev_priv, pipe);
1754 	assert_fdi_rx_disabled(dev_priv, pipe);
1755 
1756 	/* Ports must be off as well */
1757 	assert_pch_ports_disabled(dev_priv, pipe);
1758 
1759 	reg = PCH_TRANSCONF(pipe);
1760 	val = intel_de_read(dev_priv, reg);
1761 	val &= ~TRANS_ENABLE;
1762 	intel_de_write(dev_priv, reg, val);
1763 	/* wait for PCH transcoder off, transcoder state */
1764 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1765 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1766 			pipe_name(pipe));
1767 
1768 	if (HAS_PCH_CPT(dev_priv)) {
1769 		/* Workaround: Clear the timing override chicken bit again. */
1770 		reg = TRANS_CHICKEN2(pipe);
1771 		val = intel_de_read(dev_priv, reg);
1772 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1773 		intel_de_write(dev_priv, reg, val);
1774 	}
1775 }
1776 
1777 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1778 {
1779 	u32 val;
1780 
1781 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
1782 	val &= ~TRANS_ENABLE;
1783 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1784 	/* wait for PCH transcoder off, transcoder state */
1785 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1786 				    TRANS_STATE_ENABLE, 50))
1787 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
1788 
1789 	/* Workaround: clear timing override bit. */
1790 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1791 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1792 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1793 }
1794 
1795 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1796 {
1797 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1798 
1799 	if (HAS_PCH_LPT(dev_priv))
1800 		return PIPE_A;
1801 	else
1802 		return crtc->pipe;
1803 }
1804 
1805 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1806 {
1807 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1808 
1809 	/*
1810 	 * On i965gm the hardware frame counter reads
1811 	 * zero when the TV encoder is enabled :(
1812 	 */
1813 	if (IS_I965GM(dev_priv) &&
1814 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1815 		return 0;
1816 
1817 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1818 		return 0xffffffff; /* full 32 bit counter */
1819 	else if (INTEL_GEN(dev_priv) >= 3)
1820 		return 0xffffff; /* only 24 bits of frame count */
1821 	else
1822 		return 0; /* Gen2 doesn't have a hardware frame counter */
1823 }
1824 
1825 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1826 {
1827 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1828 
1829 	assert_vblank_disabled(&crtc->base);
1830 	drm_crtc_set_max_vblank_count(&crtc->base,
1831 				      intel_crtc_max_vblank_count(crtc_state));
1832 	drm_crtc_vblank_on(&crtc->base);
1833 }
1834 
1835 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
1836 {
1837 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1838 
1839 	drm_crtc_vblank_off(&crtc->base);
1840 	assert_vblank_disabled(&crtc->base);
1841 }
1842 
1843 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1844 {
1845 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1846 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1847 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1848 	enum pipe pipe = crtc->pipe;
1849 	i915_reg_t reg;
1850 	u32 val;
1851 
1852 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
1853 
1854 	assert_planes_disabled(crtc);
1855 
1856 	/*
1857 	 * A pipe without a PLL won't actually be able to drive bits from
1858 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1859 	 * need the check.
1860 	 */
1861 	if (HAS_GMCH(dev_priv)) {
1862 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1863 			assert_dsi_pll_enabled(dev_priv);
1864 		else
1865 			assert_pll_enabled(dev_priv, pipe);
1866 	} else {
1867 		if (new_crtc_state->has_pch_encoder) {
1868 			/* if driving the PCH, we need FDI enabled */
1869 			assert_fdi_rx_pll_enabled(dev_priv,
1870 						  intel_crtc_pch_transcoder(crtc));
1871 			assert_fdi_tx_pll_enabled(dev_priv,
1872 						  (enum pipe) cpu_transcoder);
1873 		}
1874 		/* FIXME: assert CPU port conditions for SNB+ */
1875 	}
1876 
1877 	trace_intel_pipe_enable(crtc);
1878 
1879 	reg = PIPECONF(cpu_transcoder);
1880 	val = intel_de_read(dev_priv, reg);
1881 	if (val & PIPECONF_ENABLE) {
1882 		/* we keep both pipes enabled on 830 */
1883 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
1884 		return;
1885 	}
1886 
1887 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1888 	intel_de_posting_read(dev_priv, reg);
1889 
1890 	/*
1891 	 * Until the pipe starts PIPEDSL reads will return a stale value,
1892 	 * which causes an apparent vblank timestamp jump when PIPEDSL
1893 	 * resets to its proper value. That also messes up the frame count
1894 	 * when it's derived from the timestamps. So let's wait for the
1895 	 * pipe to start properly before we call drm_crtc_vblank_on()
1896 	 */
1897 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1898 		intel_wait_for_pipe_scanline_moving(crtc);
1899 }
1900 
1901 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1902 {
1903 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1904 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1905 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1906 	enum pipe pipe = crtc->pipe;
1907 	i915_reg_t reg;
1908 	u32 val;
1909 
1910 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1911 
1912 	/*
1913 	 * Make sure planes won't keep trying to pump pixels to us,
1914 	 * or we might hang the display.
1915 	 */
1916 	assert_planes_disabled(crtc);
1917 
1918 	trace_intel_pipe_disable(crtc);
1919 
1920 	reg = PIPECONF(cpu_transcoder);
1921 	val = intel_de_read(dev_priv, reg);
1922 	if ((val & PIPECONF_ENABLE) == 0)
1923 		return;
1924 
1925 	/*
1926 	 * Double wide has implications for planes
1927 	 * so best keep it disabled when not needed.
1928 	 */
1929 	if (old_crtc_state->double_wide)
1930 		val &= ~PIPECONF_DOUBLE_WIDE;
1931 
1932 	/* Don't disable pipe or pipe PLLs if needed */
1933 	if (!IS_I830(dev_priv))
1934 		val &= ~PIPECONF_ENABLE;
1935 
1936 	intel_de_write(dev_priv, reg, val);
1937 	if ((val & PIPECONF_ENABLE) == 0)
1938 		intel_wait_for_pipe_off(old_crtc_state);
1939 }
1940 
1941 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1942 {
1943 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1944 }
1945 
1946 static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1947 {
1948 	if (!is_ccs_modifier(fb->modifier))
1949 		return false;
1950 
1951 	return plane >= fb->format->num_planes / 2;
1952 }
1953 
1954 static bool is_gen12_ccs_modifier(u64 modifier)
1955 {
1956 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1957 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1958 
1959 }
1960 
1961 static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1962 {
1963 	return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1964 }
1965 
1966 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1967 {
1968 	if (is_ccs_modifier(fb->modifier))
1969 		return is_ccs_plane(fb, plane);
1970 
1971 	return plane == 1;
1972 }
1973 
1974 static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1975 {
1976 	drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1977 		    (main_plane && main_plane >= fb->format->num_planes / 2));
1978 
1979 	return fb->format->num_planes / 2 + main_plane;
1980 }
1981 
1982 static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1983 {
1984 	drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1985 		    ccs_plane < fb->format->num_planes / 2);
1986 
1987 	return ccs_plane - fb->format->num_planes / 2;
1988 }
1989 
1990 /* Return either the main plane's CCS or - if not a CCS FB - UV plane */
1991 int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
1992 {
1993 	if (is_ccs_modifier(fb->modifier))
1994 		return main_to_ccs_plane(fb, main_plane);
1995 
1996 	return 1;
1997 }
1998 
1999 bool
2000 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
2001 				    uint64_t modifier)
2002 {
2003 	return info->is_yuv &&
2004 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2005 }
2006 
2007 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2008 				   int color_plane)
2009 {
2010 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2011 	       color_plane == 1;
2012 }
2013 
2014 static unsigned int
2015 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
2016 {
2017 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2018 	unsigned int cpp = fb->format->cpp[color_plane];
2019 
2020 	switch (fb->modifier) {
2021 	case DRM_FORMAT_MOD_LINEAR:
2022 		return intel_tile_size(dev_priv);
2023 	case I915_FORMAT_MOD_X_TILED:
2024 		if (IS_GEN(dev_priv, 2))
2025 			return 128;
2026 		else
2027 			return 512;
2028 	case I915_FORMAT_MOD_Y_TILED_CCS:
2029 		if (is_ccs_plane(fb, color_plane))
2030 			return 128;
2031 		/* fall through */
2032 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2033 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2034 		if (is_ccs_plane(fb, color_plane))
2035 			return 64;
2036 		/* fall through */
2037 	case I915_FORMAT_MOD_Y_TILED:
2038 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
2039 			return 128;
2040 		else
2041 			return 512;
2042 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2043 		if (is_ccs_plane(fb, color_plane))
2044 			return 128;
2045 		/* fall through */
2046 	case I915_FORMAT_MOD_Yf_TILED:
2047 		switch (cpp) {
2048 		case 1:
2049 			return 64;
2050 		case 2:
2051 		case 4:
2052 			return 128;
2053 		case 8:
2054 		case 16:
2055 			return 256;
2056 		default:
2057 			MISSING_CASE(cpp);
2058 			return cpp;
2059 		}
2060 		break;
2061 	default:
2062 		MISSING_CASE(fb->modifier);
2063 		return cpp;
2064 	}
2065 }
2066 
2067 static unsigned int
2068 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
2069 {
2070 	if (is_gen12_ccs_plane(fb, color_plane))
2071 		return 1;
2072 
2073 	return intel_tile_size(to_i915(fb->dev)) /
2074 		intel_tile_width_bytes(fb, color_plane);
2075 }
2076 
2077 /* Return the tile dimensions in pixel units */
2078 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
2079 			    unsigned int *tile_width,
2080 			    unsigned int *tile_height)
2081 {
2082 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2083 	unsigned int cpp = fb->format->cpp[color_plane];
2084 
2085 	*tile_width = tile_width_bytes / cpp;
2086 	*tile_height = intel_tile_height(fb, color_plane);
2087 }
2088 
2089 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2090 					int color_plane)
2091 {
2092 	unsigned int tile_width, tile_height;
2093 
2094 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2095 
2096 	return fb->pitches[color_plane] * tile_height;
2097 }
2098 
2099 unsigned int
2100 intel_fb_align_height(const struct drm_framebuffer *fb,
2101 		      int color_plane, unsigned int height)
2102 {
2103 	unsigned int tile_height = intel_tile_height(fb, color_plane);
2104 
2105 	return ALIGN(height, tile_height);
2106 }
2107 
2108 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2109 {
2110 	unsigned int size = 0;
2111 	int i;
2112 
2113 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2114 		size += rot_info->plane[i].width * rot_info->plane[i].height;
2115 
2116 	return size;
2117 }
2118 
2119 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2120 {
2121 	unsigned int size = 0;
2122 	int i;
2123 
2124 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2125 		size += rem_info->plane[i].width * rem_info->plane[i].height;
2126 
2127 	return size;
2128 }
2129 
2130 static void
2131 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2132 			const struct drm_framebuffer *fb,
2133 			unsigned int rotation)
2134 {
2135 	view->type = I915_GGTT_VIEW_NORMAL;
2136 	if (drm_rotation_90_or_270(rotation)) {
2137 		view->type = I915_GGTT_VIEW_ROTATED;
2138 		view->rotated = to_intel_framebuffer(fb)->rot_info;
2139 	}
2140 }
2141 
2142 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2143 {
2144 	if (IS_I830(dev_priv))
2145 		return 16 * 1024;
2146 	else if (IS_I85X(dev_priv))
2147 		return 256;
2148 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2149 		return 32;
2150 	else
2151 		return 4 * 1024;
2152 }
2153 
2154 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2155 {
2156 	if (INTEL_GEN(dev_priv) >= 9)
2157 		return 256 * 1024;
2158 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2159 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2160 		return 128 * 1024;
2161 	else if (INTEL_GEN(dev_priv) >= 4)
2162 		return 4 * 1024;
2163 	else
2164 		return 0;
2165 }
2166 
2167 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2168 					 int color_plane)
2169 {
2170 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2171 
2172 	/* AUX_DIST needs only 4K alignment */
2173 	if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2174 	    is_ccs_plane(fb, color_plane))
2175 		return 4096;
2176 
2177 	switch (fb->modifier) {
2178 	case DRM_FORMAT_MOD_LINEAR:
2179 		return intel_linear_alignment(dev_priv);
2180 	case I915_FORMAT_MOD_X_TILED:
2181 		if (INTEL_GEN(dev_priv) >= 9)
2182 			return 256 * 1024;
2183 		return 0;
2184 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2185 		if (is_semiplanar_uv_plane(fb, color_plane))
2186 			return intel_tile_row_size(fb, color_plane);
2187 		/* Fall-through */
2188 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2189 		return 16 * 1024;
2190 	case I915_FORMAT_MOD_Y_TILED_CCS:
2191 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2192 	case I915_FORMAT_MOD_Y_TILED:
2193 		if (INTEL_GEN(dev_priv) >= 12 &&
2194 		    is_semiplanar_uv_plane(fb, color_plane))
2195 			return intel_tile_row_size(fb, color_plane);
2196 		/* Fall-through */
2197 	case I915_FORMAT_MOD_Yf_TILED:
2198 		return 1 * 1024 * 1024;
2199 	default:
2200 		MISSING_CASE(fb->modifier);
2201 		return 0;
2202 	}
2203 }
2204 
2205 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2206 {
2207 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2208 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2209 
2210 	return INTEL_GEN(dev_priv) < 4 ||
2211 		(plane->has_fbc &&
2212 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2213 }
2214 
2215 struct i915_vma *
2216 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2217 			   const struct i915_ggtt_view *view,
2218 			   bool uses_fence,
2219 			   unsigned long *out_flags)
2220 {
2221 	struct drm_device *dev = fb->dev;
2222 	struct drm_i915_private *dev_priv = to_i915(dev);
2223 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2224 	intel_wakeref_t wakeref;
2225 	struct i915_vma *vma;
2226 	unsigned int pinctl;
2227 	u32 alignment;
2228 
2229 	if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
2230 		return ERR_PTR(-EINVAL);
2231 
2232 	alignment = intel_surf_alignment(fb, 0);
2233 	if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
2234 		return ERR_PTR(-EINVAL);
2235 
2236 	/* Note that the w/a also requires 64 PTE of padding following the
2237 	 * bo. We currently fill all unused PTE with the shadow page and so
2238 	 * we should always have valid PTE following the scanout preventing
2239 	 * the VT-d warning.
2240 	 */
2241 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2242 		alignment = 256 * 1024;
2243 
2244 	/*
2245 	 * Global gtt pte registers are special registers which actually forward
2246 	 * writes to a chunk of system memory. Which means that there is no risk
2247 	 * that the register values disappear as soon as we call
2248 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2249 	 * pin/unpin/fence and not more.
2250 	 */
2251 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2252 
2253 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2254 
2255 	/*
2256 	 * Valleyview is definitely limited to scanning out the first
2257 	 * 512MiB. Lets presume this behaviour was inherited from the
2258 	 * g4x display engine and that all earlier gen are similarly
2259 	 * limited. Testing suggests that it is a little more
2260 	 * complicated than this. For example, Cherryview appears quite
2261 	 * happy to scanout from anywhere within its global aperture.
2262 	 */
2263 	pinctl = 0;
2264 	if (HAS_GMCH(dev_priv))
2265 		pinctl |= PIN_MAPPABLE;
2266 
2267 	vma = i915_gem_object_pin_to_display_plane(obj,
2268 						   alignment, view, pinctl);
2269 	if (IS_ERR(vma))
2270 		goto err;
2271 
2272 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2273 		int ret;
2274 
2275 		/*
2276 		 * Install a fence for tiled scan-out. Pre-i965 always needs a
2277 		 * fence, whereas 965+ only requires a fence if using
2278 		 * framebuffer compression.  For simplicity, we always, when
2279 		 * possible, install a fence as the cost is not that onerous.
2280 		 *
2281 		 * If we fail to fence the tiled scanout, then either the
2282 		 * modeset will reject the change (which is highly unlikely as
2283 		 * the affected systems, all but one, do not have unmappable
2284 		 * space) or we will not be able to enable full powersaving
2285 		 * techniques (also likely not to apply due to various limits
2286 		 * FBC and the like impose on the size of the buffer, which
2287 		 * presumably we violated anyway with this unmappable buffer).
2288 		 * Anyway, it is presumably better to stumble onwards with
2289 		 * something and try to run the system in a "less than optimal"
2290 		 * mode that matches the user configuration.
2291 		 */
2292 		ret = i915_vma_pin_fence(vma);
2293 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2294 			i915_gem_object_unpin_from_display_plane(vma);
2295 			vma = ERR_PTR(ret);
2296 			goto err;
2297 		}
2298 
2299 		if (ret == 0 && vma->fence)
2300 			*out_flags |= PLANE_HAS_FENCE;
2301 	}
2302 
2303 	i915_vma_get(vma);
2304 err:
2305 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2306 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2307 	return vma;
2308 }
2309 
2310 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2311 {
2312 	i915_gem_object_lock(vma->obj);
2313 	if (flags & PLANE_HAS_FENCE)
2314 		i915_vma_unpin_fence(vma);
2315 	i915_gem_object_unpin_from_display_plane(vma);
2316 	i915_gem_object_unlock(vma->obj);
2317 
2318 	i915_vma_put(vma);
2319 }
2320 
2321 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2322 			  unsigned int rotation)
2323 {
2324 	if (drm_rotation_90_or_270(rotation))
2325 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2326 	else
2327 		return fb->pitches[color_plane];
2328 }
2329 
2330 /*
2331  * Convert the x/y offsets into a linear offset.
2332  * Only valid with 0/180 degree rotation, which is fine since linear
2333  * offset is only used with linear buffers on pre-hsw and tiled buffers
2334  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2335  */
2336 u32 intel_fb_xy_to_linear(int x, int y,
2337 			  const struct intel_plane_state *state,
2338 			  int color_plane)
2339 {
2340 	const struct drm_framebuffer *fb = state->hw.fb;
2341 	unsigned int cpp = fb->format->cpp[color_plane];
2342 	unsigned int pitch = state->color_plane[color_plane].stride;
2343 
2344 	return y * pitch + x * cpp;
2345 }
2346 
2347 /*
2348  * Add the x/y offsets derived from fb->offsets[] to the user
2349  * specified plane src x/y offsets. The resulting x/y offsets
2350  * specify the start of scanout from the beginning of the gtt mapping.
2351  */
2352 void intel_add_fb_offsets(int *x, int *y,
2353 			  const struct intel_plane_state *state,
2354 			  int color_plane)
2355 
2356 {
2357 	*x += state->color_plane[color_plane].x;
2358 	*y += state->color_plane[color_plane].y;
2359 }
2360 
2361 static u32 intel_adjust_tile_offset(int *x, int *y,
2362 				    unsigned int tile_width,
2363 				    unsigned int tile_height,
2364 				    unsigned int tile_size,
2365 				    unsigned int pitch_tiles,
2366 				    u32 old_offset,
2367 				    u32 new_offset)
2368 {
2369 	unsigned int pitch_pixels = pitch_tiles * tile_width;
2370 	unsigned int tiles;
2371 
2372 	WARN_ON(old_offset & (tile_size - 1));
2373 	WARN_ON(new_offset & (tile_size - 1));
2374 	WARN_ON(new_offset > old_offset);
2375 
2376 	tiles = (old_offset - new_offset) / tile_size;
2377 
2378 	*y += tiles / pitch_tiles * tile_height;
2379 	*x += tiles % pitch_tiles * tile_width;
2380 
2381 	/* minimize x in case it got needlessly big */
2382 	*y += *x / pitch_pixels * tile_height;
2383 	*x %= pitch_pixels;
2384 
2385 	return new_offset;
2386 }
2387 
2388 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2389 {
2390 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2391 	       is_gen12_ccs_plane(fb, color_plane);
2392 }
2393 
2394 static u32 intel_adjust_aligned_offset(int *x, int *y,
2395 				       const struct drm_framebuffer *fb,
2396 				       int color_plane,
2397 				       unsigned int rotation,
2398 				       unsigned int pitch,
2399 				       u32 old_offset, u32 new_offset)
2400 {
2401 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2402 	unsigned int cpp = fb->format->cpp[color_plane];
2403 
2404 	drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
2405 
2406 	if (!is_surface_linear(fb, color_plane)) {
2407 		unsigned int tile_size, tile_width, tile_height;
2408 		unsigned int pitch_tiles;
2409 
2410 		tile_size = intel_tile_size(dev_priv);
2411 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2412 
2413 		if (drm_rotation_90_or_270(rotation)) {
2414 			pitch_tiles = pitch / tile_height;
2415 			swap(tile_width, tile_height);
2416 		} else {
2417 			pitch_tiles = pitch / (tile_width * cpp);
2418 		}
2419 
2420 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2421 					 tile_size, pitch_tiles,
2422 					 old_offset, new_offset);
2423 	} else {
2424 		old_offset += *y * pitch + *x * cpp;
2425 
2426 		*y = (old_offset - new_offset) / pitch;
2427 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
2428 	}
2429 
2430 	return new_offset;
2431 }
2432 
2433 /*
2434  * Adjust the tile offset by moving the difference into
2435  * the x/y offsets.
2436  */
2437 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2438 					     const struct intel_plane_state *state,
2439 					     int color_plane,
2440 					     u32 old_offset, u32 new_offset)
2441 {
2442 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2443 					   state->hw.rotation,
2444 					   state->color_plane[color_plane].stride,
2445 					   old_offset, new_offset);
2446 }
2447 
2448 /*
2449  * Computes the aligned offset to the base tile and adjusts
2450  * x, y. bytes per pixel is assumed to be a power-of-two.
2451  *
2452  * In the 90/270 rotated case, x and y are assumed
2453  * to be already rotated to match the rotated GTT view, and
2454  * pitch is the tile_height aligned framebuffer height.
2455  *
2456  * This function is used when computing the derived information
2457  * under intel_framebuffer, so using any of that information
2458  * here is not allowed. Anything under drm_framebuffer can be
2459  * used. This is why the user has to pass in the pitch since it
2460  * is specified in the rotated orientation.
2461  */
2462 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2463 					int *x, int *y,
2464 					const struct drm_framebuffer *fb,
2465 					int color_plane,
2466 					unsigned int pitch,
2467 					unsigned int rotation,
2468 					u32 alignment)
2469 {
2470 	unsigned int cpp = fb->format->cpp[color_plane];
2471 	u32 offset, offset_aligned;
2472 
2473 	if (!is_surface_linear(fb, color_plane)) {
2474 		unsigned int tile_size, tile_width, tile_height;
2475 		unsigned int tile_rows, tiles, pitch_tiles;
2476 
2477 		tile_size = intel_tile_size(dev_priv);
2478 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2479 
2480 		if (drm_rotation_90_or_270(rotation)) {
2481 			pitch_tiles = pitch / tile_height;
2482 			swap(tile_width, tile_height);
2483 		} else {
2484 			pitch_tiles = pitch / (tile_width * cpp);
2485 		}
2486 
2487 		tile_rows = *y / tile_height;
2488 		*y %= tile_height;
2489 
2490 		tiles = *x / tile_width;
2491 		*x %= tile_width;
2492 
2493 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2494 
2495 		offset_aligned = offset;
2496 		if (alignment)
2497 			offset_aligned = rounddown(offset_aligned, alignment);
2498 
2499 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2500 					 tile_size, pitch_tiles,
2501 					 offset, offset_aligned);
2502 	} else {
2503 		offset = *y * pitch + *x * cpp;
2504 		offset_aligned = offset;
2505 		if (alignment) {
2506 			offset_aligned = rounddown(offset_aligned, alignment);
2507 			*y = (offset % alignment) / pitch;
2508 			*x = ((offset % alignment) - *y * pitch) / cpp;
2509 		} else {
2510 			*y = *x = 0;
2511 		}
2512 	}
2513 
2514 	return offset_aligned;
2515 }
2516 
2517 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2518 					      const struct intel_plane_state *state,
2519 					      int color_plane)
2520 {
2521 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
2522 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2523 	const struct drm_framebuffer *fb = state->hw.fb;
2524 	unsigned int rotation = state->hw.rotation;
2525 	int pitch = state->color_plane[color_plane].stride;
2526 	u32 alignment;
2527 
2528 	if (intel_plane->id == PLANE_CURSOR)
2529 		alignment = intel_cursor_alignment(dev_priv);
2530 	else
2531 		alignment = intel_surf_alignment(fb, color_plane);
2532 
2533 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2534 					    pitch, rotation, alignment);
2535 }
2536 
2537 /* Convert the fb->offset[] into x/y offsets */
2538 static int intel_fb_offset_to_xy(int *x, int *y,
2539 				 const struct drm_framebuffer *fb,
2540 				 int color_plane)
2541 {
2542 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2543 	unsigned int height;
2544 	u32 alignment;
2545 
2546 	if (INTEL_GEN(dev_priv) >= 12 &&
2547 	    is_semiplanar_uv_plane(fb, color_plane))
2548 		alignment = intel_tile_row_size(fb, color_plane);
2549 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2550 		alignment = intel_tile_size(dev_priv);
2551 	else
2552 		alignment = 0;
2553 
2554 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
2555 		drm_dbg_kms(&dev_priv->drm,
2556 			    "Misaligned offset 0x%08x for color plane %d\n",
2557 			    fb->offsets[color_plane], color_plane);
2558 		return -EINVAL;
2559 	}
2560 
2561 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2562 	height = ALIGN(height, intel_tile_height(fb, color_plane));
2563 
2564 	/* Catch potential overflows early */
2565 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2566 			    fb->offsets[color_plane])) {
2567 		drm_dbg_kms(&dev_priv->drm,
2568 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
2569 			    fb->offsets[color_plane], fb->pitches[color_plane],
2570 			    color_plane);
2571 		return -ERANGE;
2572 	}
2573 
2574 	*x = 0;
2575 	*y = 0;
2576 
2577 	intel_adjust_aligned_offset(x, y,
2578 				    fb, color_plane, DRM_MODE_ROTATE_0,
2579 				    fb->pitches[color_plane],
2580 				    fb->offsets[color_plane], 0);
2581 
2582 	return 0;
2583 }
2584 
2585 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2586 {
2587 	switch (fb_modifier) {
2588 	case I915_FORMAT_MOD_X_TILED:
2589 		return I915_TILING_X;
2590 	case I915_FORMAT_MOD_Y_TILED:
2591 	case I915_FORMAT_MOD_Y_TILED_CCS:
2592 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2593 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2594 		return I915_TILING_Y;
2595 	default:
2596 		return I915_TILING_NONE;
2597 	}
2598 }
2599 
2600 /*
2601  * From the Sky Lake PRM:
2602  * "The Color Control Surface (CCS) contains the compression status of
2603  *  the cache-line pairs. The compression state of the cache-line pair
2604  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2605  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2606  *  cache-line-pairs. CCS is always Y tiled."
2607  *
2608  * Since cache line pairs refers to horizontally adjacent cache lines,
2609  * each cache line in the CCS corresponds to an area of 32x16 cache
2610  * lines on the main surface. Since each pixel is 4 bytes, this gives
2611  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2612  * main surface.
2613  */
2614 static const struct drm_format_info skl_ccs_formats[] = {
2615 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2616 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2617 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2618 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2619 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2620 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2621 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2622 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2623 };
2624 
2625 /*
2626  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2627  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2628  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2629  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2630  * the main surface.
2631  */
2632 static const struct drm_format_info gen12_ccs_formats[] = {
2633 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2634 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2635 	  .hsub = 1, .vsub = 1, },
2636 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2637 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2638 	  .hsub = 1, .vsub = 1, },
2639 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2640 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2641 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2642 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2643 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2644 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2645 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
2646 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2647 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2648 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
2649 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2650 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2651 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
2652 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2653 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2654 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
2655 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2656 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2657 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
2658 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2659 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2660 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
2661 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2662 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2663 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
2664 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2665 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2666 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
2667 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2668 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2669 };
2670 
2671 static const struct drm_format_info *
2672 lookup_format_info(const struct drm_format_info formats[],
2673 		   int num_formats, u32 format)
2674 {
2675 	int i;
2676 
2677 	for (i = 0; i < num_formats; i++) {
2678 		if (formats[i].format == format)
2679 			return &formats[i];
2680 	}
2681 
2682 	return NULL;
2683 }
2684 
2685 static const struct drm_format_info *
2686 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2687 {
2688 	switch (cmd->modifier[0]) {
2689 	case I915_FORMAT_MOD_Y_TILED_CCS:
2690 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2691 		return lookup_format_info(skl_ccs_formats,
2692 					  ARRAY_SIZE(skl_ccs_formats),
2693 					  cmd->pixel_format);
2694 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2695 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2696 		return lookup_format_info(gen12_ccs_formats,
2697 					  ARRAY_SIZE(gen12_ccs_formats),
2698 					  cmd->pixel_format);
2699 	default:
2700 		return NULL;
2701 	}
2702 }
2703 
2704 bool is_ccs_modifier(u64 modifier)
2705 {
2706 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2707 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
2708 	       modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2709 	       modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2710 }
2711 
2712 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2713 {
2714 	return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2715 			    512) * 64;
2716 }
2717 
2718 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2719 			      u32 pixel_format, u64 modifier)
2720 {
2721 	struct intel_crtc *crtc;
2722 	struct intel_plane *plane;
2723 
2724 	/*
2725 	 * We assume the primary plane for pipe A has
2726 	 * the highest stride limits of them all,
2727 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
2728 	 */
2729 	crtc = intel_get_first_crtc(dev_priv);
2730 	if (!crtc)
2731 		return 0;
2732 
2733 	plane = to_intel_plane(crtc->base.primary);
2734 
2735 	return plane->max_stride(plane, pixel_format, modifier,
2736 				 DRM_MODE_ROTATE_0);
2737 }
2738 
2739 static
2740 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2741 			u32 pixel_format, u64 modifier)
2742 {
2743 	/*
2744 	 * Arbitrary limit for gen4+ chosen to match the
2745 	 * render engine max stride.
2746 	 *
2747 	 * The new CCS hash mode makes remapping impossible
2748 	 */
2749 	if (!is_ccs_modifier(modifier)) {
2750 		if (INTEL_GEN(dev_priv) >= 7)
2751 			return 256*1024;
2752 		else if (INTEL_GEN(dev_priv) >= 4)
2753 			return 128*1024;
2754 	}
2755 
2756 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2757 }
2758 
2759 static u32
2760 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2761 {
2762 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2763 	u32 tile_width;
2764 
2765 	if (is_surface_linear(fb, color_plane)) {
2766 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2767 							   fb->format->format,
2768 							   fb->modifier);
2769 
2770 		/*
2771 		 * To make remapping with linear generally feasible
2772 		 * we need the stride to be page aligned.
2773 		 */
2774 		if (fb->pitches[color_plane] > max_stride &&
2775 		    !is_ccs_modifier(fb->modifier))
2776 			return intel_tile_size(dev_priv);
2777 		else
2778 			return 64;
2779 	}
2780 
2781 	tile_width = intel_tile_width_bytes(fb, color_plane);
2782 	if (is_ccs_modifier(fb->modifier)) {
2783 		/*
2784 		 * Display WA #0531: skl,bxt,kbl,glk
2785 		 *
2786 		 * Render decompression and plane width > 3840
2787 		 * combined with horizontal panning requires the
2788 		 * plane stride to be a multiple of 4. We'll just
2789 		 * require the entire fb to accommodate that to avoid
2790 		 * potential runtime errors at plane configuration time.
2791 		 */
2792 		if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
2793 			tile_width *= 4;
2794 		/*
2795 		 * The main surface pitch must be padded to a multiple of four
2796 		 * tile widths.
2797 		 */
2798 		else if (INTEL_GEN(dev_priv) >= 12)
2799 			tile_width *= 4;
2800 	}
2801 	return tile_width;
2802 }
2803 
2804 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2805 {
2806 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2807 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2808 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2809 	int i;
2810 
2811 	/* We don't want to deal with remapping with cursors */
2812 	if (plane->id == PLANE_CURSOR)
2813 		return false;
2814 
2815 	/*
2816 	 * The display engine limits already match/exceed the
2817 	 * render engine limits, so not much point in remapping.
2818 	 * Would also need to deal with the fence POT alignment
2819 	 * and gen2 2KiB GTT tile size.
2820 	 */
2821 	if (INTEL_GEN(dev_priv) < 4)
2822 		return false;
2823 
2824 	/*
2825 	 * The new CCS hash mode isn't compatible with remapping as
2826 	 * the virtual address of the pages affects the compressed data.
2827 	 */
2828 	if (is_ccs_modifier(fb->modifier))
2829 		return false;
2830 
2831 	/* Linear needs a page aligned stride for remapping */
2832 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2833 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
2834 
2835 		for (i = 0; i < fb->format->num_planes; i++) {
2836 			if (fb->pitches[i] & alignment)
2837 				return false;
2838 		}
2839 	}
2840 
2841 	return true;
2842 }
2843 
2844 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2845 {
2846 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2847 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2848 	unsigned int rotation = plane_state->hw.rotation;
2849 	u32 stride, max_stride;
2850 
2851 	/*
2852 	 * No remapping for invisible planes since we don't have
2853 	 * an actual source viewport to remap.
2854 	 */
2855 	if (!plane_state->uapi.visible)
2856 		return false;
2857 
2858 	if (!intel_plane_can_remap(plane_state))
2859 		return false;
2860 
2861 	/*
2862 	 * FIXME: aux plane limits on gen9+ are
2863 	 * unclear in Bspec, for now no checking.
2864 	 */
2865 	stride = intel_fb_pitch(fb, 0, rotation);
2866 	max_stride = plane->max_stride(plane, fb->format->format,
2867 				       fb->modifier, rotation);
2868 
2869 	return stride > max_stride;
2870 }
2871 
2872 static void
2873 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2874 			       const struct drm_framebuffer *fb,
2875 			       int color_plane)
2876 {
2877 	int main_plane;
2878 
2879 	if (color_plane == 0) {
2880 		*hsub = 1;
2881 		*vsub = 1;
2882 
2883 		return;
2884 	}
2885 
2886 	/*
2887 	 * TODO: Deduct the subsampling from the char block for all CCS
2888 	 * formats and planes.
2889 	 */
2890 	if (!is_gen12_ccs_plane(fb, color_plane)) {
2891 		*hsub = fb->format->hsub;
2892 		*vsub = fb->format->vsub;
2893 
2894 		return;
2895 	}
2896 
2897 	main_plane = ccs_to_main_plane(fb, color_plane);
2898 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
2899 		drm_format_info_block_width(fb->format, main_plane);
2900 
2901 	/*
2902 	 * The min stride check in the core framebuffer_check() function
2903 	 * assumes that format->hsub applies to every plane except for the
2904 	 * first plane. That's incorrect for the CCS AUX plane of the first
2905 	 * plane, but for the above check to pass we must define the block
2906 	 * width with that subsampling applied to it. Adjust the width here
2907 	 * accordingly, so we can calculate the actual subsampling factor.
2908 	 */
2909 	if (main_plane == 0)
2910 		*hsub *= fb->format->hsub;
2911 
2912 	*vsub = 32;
2913 }
2914 static int
2915 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2916 {
2917 	struct drm_i915_private *i915 = to_i915(fb->dev);
2918 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2919 	int main_plane;
2920 	int hsub, vsub;
2921 	int tile_width, tile_height;
2922 	int ccs_x, ccs_y;
2923 	int main_x, main_y;
2924 
2925 	if (!is_ccs_plane(fb, ccs_plane))
2926 		return 0;
2927 
2928 	intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2929 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2930 
2931 	tile_width *= hsub;
2932 	tile_height *= vsub;
2933 
2934 	ccs_x = (x * hsub) % tile_width;
2935 	ccs_y = (y * vsub) % tile_height;
2936 
2937 	main_plane = ccs_to_main_plane(fb, ccs_plane);
2938 	main_x = intel_fb->normal[main_plane].x % tile_width;
2939 	main_y = intel_fb->normal[main_plane].y % tile_height;
2940 
2941 	/*
2942 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2943 	 * x/y offsets must match between CCS and the main surface.
2944 	 */
2945 	if (main_x != ccs_x || main_y != ccs_y) {
2946 		drm_dbg_kms(&i915->drm,
2947 			      "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2948 			      main_x, main_y,
2949 			      ccs_x, ccs_y,
2950 			      intel_fb->normal[main_plane].x,
2951 			      intel_fb->normal[main_plane].y,
2952 			      x, y);
2953 		return -EINVAL;
2954 	}
2955 
2956 	return 0;
2957 }
2958 
2959 static void
2960 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2961 {
2962 	int main_plane = is_ccs_plane(fb, color_plane) ?
2963 			 ccs_to_main_plane(fb, color_plane) : 0;
2964 	int main_hsub, main_vsub;
2965 	int hsub, vsub;
2966 
2967 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
2968 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
2969 	*w = fb->width / main_hsub / hsub;
2970 	*h = fb->height / main_vsub / vsub;
2971 }
2972 
2973 /*
2974  * Setup the rotated view for an FB plane and return the size the GTT mapping
2975  * requires for this view.
2976  */
2977 static u32
2978 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2979 		  u32 gtt_offset_rotated, int x, int y,
2980 		  unsigned int width, unsigned int height,
2981 		  unsigned int tile_size,
2982 		  unsigned int tile_width, unsigned int tile_height,
2983 		  struct drm_framebuffer *fb)
2984 {
2985 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2986 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2987 	unsigned int pitch_tiles;
2988 	struct drm_rect r;
2989 
2990 	/* Y or Yf modifiers required for 90/270 rotation */
2991 	if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
2992 	    fb->modifier != I915_FORMAT_MOD_Yf_TILED)
2993 		return 0;
2994 
2995 	if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
2996 		return 0;
2997 
2998 	rot_info->plane[plane] = *plane_info;
2999 
3000 	intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
3001 
3002 	/* rotate the x/y offsets to match the GTT view */
3003 	drm_rect_init(&r, x, y, width, height);
3004 	drm_rect_rotate(&r,
3005 			plane_info->width * tile_width,
3006 			plane_info->height * tile_height,
3007 			DRM_MODE_ROTATE_270);
3008 	x = r.x1;
3009 	y = r.y1;
3010 
3011 	/* rotate the tile dimensions to match the GTT view */
3012 	pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3013 	swap(tile_width, tile_height);
3014 
3015 	/*
3016 	 * We only keep the x/y offsets, so push all of the
3017 	 * gtt offset into the x/y offsets.
3018 	 */
3019 	intel_adjust_tile_offset(&x, &y,
3020 				 tile_width, tile_height,
3021 				 tile_size, pitch_tiles,
3022 				 gtt_offset_rotated * tile_size, 0);
3023 
3024 	/*
3025 	 * First pixel of the framebuffer from
3026 	 * the start of the rotated gtt mapping.
3027 	 */
3028 	intel_fb->rotated[plane].x = x;
3029 	intel_fb->rotated[plane].y = y;
3030 
3031 	return plane_info->width * plane_info->height;
3032 }
3033 
3034 static int
3035 intel_fill_fb_info(struct drm_i915_private *dev_priv,
3036 		   struct drm_framebuffer *fb)
3037 {
3038 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3039 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3040 	u32 gtt_offset_rotated = 0;
3041 	unsigned int max_size = 0;
3042 	int i, num_planes = fb->format->num_planes;
3043 	unsigned int tile_size = intel_tile_size(dev_priv);
3044 
3045 	for (i = 0; i < num_planes; i++) {
3046 		unsigned int width, height;
3047 		unsigned int cpp, size;
3048 		u32 offset;
3049 		int x, y;
3050 		int ret;
3051 
3052 		cpp = fb->format->cpp[i];
3053 		intel_fb_plane_dims(&width, &height, fb, i);
3054 
3055 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3056 		if (ret) {
3057 			drm_dbg_kms(&dev_priv->drm,
3058 				    "bad fb plane %d offset: 0x%x\n",
3059 				    i, fb->offsets[i]);
3060 			return ret;
3061 		}
3062 
3063 		ret = intel_fb_check_ccs_xy(fb, i, x, y);
3064 		if (ret)
3065 			return ret;
3066 
3067 		/*
3068 		 * The fence (if used) is aligned to the start of the object
3069 		 * so having the framebuffer wrap around across the edge of the
3070 		 * fenced region doesn't really work. We have no API to configure
3071 		 * the fence start offset within the object (nor could we probably
3072 		 * on gen2/3). So it's just easier if we just require that the
3073 		 * fb layout agrees with the fence layout. We already check that the
3074 		 * fb stride matches the fence stride elsewhere.
3075 		 */
3076 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
3077 		    (x + width) * cpp > fb->pitches[i]) {
3078 			drm_dbg_kms(&dev_priv->drm,
3079 				    "bad fb plane %d offset: 0x%x\n",
3080 				     i, fb->offsets[i]);
3081 			return -EINVAL;
3082 		}
3083 
3084 		/*
3085 		 * First pixel of the framebuffer from
3086 		 * the start of the normal gtt mapping.
3087 		 */
3088 		intel_fb->normal[i].x = x;
3089 		intel_fb->normal[i].y = y;
3090 
3091 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3092 						      fb->pitches[i],
3093 						      DRM_MODE_ROTATE_0,
3094 						      tile_size);
3095 		offset /= tile_size;
3096 
3097 		if (!is_surface_linear(fb, i)) {
3098 			struct intel_remapped_plane_info plane_info;
3099 			unsigned int tile_width, tile_height;
3100 
3101 			intel_tile_dims(fb, i, &tile_width, &tile_height);
3102 
3103 			plane_info.offset = offset;
3104 			plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3105 							 tile_width * cpp);
3106 			plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3107 			plane_info.height = DIV_ROUND_UP(y + height,
3108 							 tile_height);
3109 
3110 			/* how many tiles does this plane need */
3111 			size = plane_info.stride * plane_info.height;
3112 			/*
3113 			 * If the plane isn't horizontally tile aligned,
3114 			 * we need one more tile.
3115 			 */
3116 			if (x != 0)
3117 				size++;
3118 
3119 			gtt_offset_rotated +=
3120 				setup_fb_rotation(i, &plane_info,
3121 						  gtt_offset_rotated,
3122 						  x, y, width, height,
3123 						  tile_size,
3124 						  tile_width, tile_height,
3125 						  fb);
3126 		} else {
3127 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3128 					    x * cpp, tile_size);
3129 		}
3130 
3131 		/* how many tiles in total needed in the bo */
3132 		max_size = max(max_size, offset + size);
3133 	}
3134 
3135 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
3136 		drm_dbg_kms(&dev_priv->drm,
3137 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3138 			    mul_u32_u32(max_size, tile_size), obj->base.size);
3139 		return -EINVAL;
3140 	}
3141 
3142 	return 0;
3143 }
3144 
3145 static void
3146 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3147 {
3148 	struct drm_i915_private *dev_priv =
3149 		to_i915(plane_state->uapi.plane->dev);
3150 	struct drm_framebuffer *fb = plane_state->hw.fb;
3151 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3152 	struct intel_rotation_info *info = &plane_state->view.rotated;
3153 	unsigned int rotation = plane_state->hw.rotation;
3154 	int i, num_planes = fb->format->num_planes;
3155 	unsigned int tile_size = intel_tile_size(dev_priv);
3156 	unsigned int src_x, src_y;
3157 	unsigned int src_w, src_h;
3158 	u32 gtt_offset = 0;
3159 
3160 	memset(&plane_state->view, 0, sizeof(plane_state->view));
3161 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3162 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3163 
3164 	src_x = plane_state->uapi.src.x1 >> 16;
3165 	src_y = plane_state->uapi.src.y1 >> 16;
3166 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3167 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
3168 
3169 	drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
3170 
3171 	/* Make src coordinates relative to the viewport */
3172 	drm_rect_translate(&plane_state->uapi.src,
3173 			   -(src_x << 16), -(src_y << 16));
3174 
3175 	/* Rotate src coordinates to match rotated GTT view */
3176 	if (drm_rotation_90_or_270(rotation))
3177 		drm_rect_rotate(&plane_state->uapi.src,
3178 				src_w << 16, src_h << 16,
3179 				DRM_MODE_ROTATE_270);
3180 
3181 	for (i = 0; i < num_planes; i++) {
3182 		unsigned int hsub = i ? fb->format->hsub : 1;
3183 		unsigned int vsub = i ? fb->format->vsub : 1;
3184 		unsigned int cpp = fb->format->cpp[i];
3185 		unsigned int tile_width, tile_height;
3186 		unsigned int width, height;
3187 		unsigned int pitch_tiles;
3188 		unsigned int x, y;
3189 		u32 offset;
3190 
3191 		intel_tile_dims(fb, i, &tile_width, &tile_height);
3192 
3193 		x = src_x / hsub;
3194 		y = src_y / vsub;
3195 		width = src_w / hsub;
3196 		height = src_h / vsub;
3197 
3198 		/*
3199 		 * First pixel of the src viewport from the
3200 		 * start of the normal gtt mapping.
3201 		 */
3202 		x += intel_fb->normal[i].x;
3203 		y += intel_fb->normal[i].y;
3204 
3205 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3206 						      fb, i, fb->pitches[i],
3207 						      DRM_MODE_ROTATE_0, tile_size);
3208 		offset /= tile_size;
3209 
3210 		drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
3211 		info->plane[i].offset = offset;
3212 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3213 						     tile_width * cpp);
3214 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3215 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3216 
3217 		if (drm_rotation_90_or_270(rotation)) {
3218 			struct drm_rect r;
3219 
3220 			/* rotate the x/y offsets to match the GTT view */
3221 			drm_rect_init(&r, x, y, width, height);
3222 			drm_rect_rotate(&r,
3223 					info->plane[i].width * tile_width,
3224 					info->plane[i].height * tile_height,
3225 					DRM_MODE_ROTATE_270);
3226 			x = r.x1;
3227 			y = r.y1;
3228 
3229 			pitch_tiles = info->plane[i].height;
3230 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3231 
3232 			/* rotate the tile dimensions to match the GTT view */
3233 			swap(tile_width, tile_height);
3234 		} else {
3235 			pitch_tiles = info->plane[i].width;
3236 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3237 		}
3238 
3239 		/*
3240 		 * We only keep the x/y offsets, so push all of the
3241 		 * gtt offset into the x/y offsets.
3242 		 */
3243 		intel_adjust_tile_offset(&x, &y,
3244 					 tile_width, tile_height,
3245 					 tile_size, pitch_tiles,
3246 					 gtt_offset * tile_size, 0);
3247 
3248 		gtt_offset += info->plane[i].width * info->plane[i].height;
3249 
3250 		plane_state->color_plane[i].offset = 0;
3251 		plane_state->color_plane[i].x = x;
3252 		plane_state->color_plane[i].y = y;
3253 	}
3254 }
3255 
3256 static int
3257 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3258 {
3259 	const struct intel_framebuffer *fb =
3260 		to_intel_framebuffer(plane_state->hw.fb);
3261 	unsigned int rotation = plane_state->hw.rotation;
3262 	int i, num_planes;
3263 
3264 	if (!fb)
3265 		return 0;
3266 
3267 	num_planes = fb->base.format->num_planes;
3268 
3269 	if (intel_plane_needs_remap(plane_state)) {
3270 		intel_plane_remap_gtt(plane_state);
3271 
3272 		/*
3273 		 * Sometimes even remapping can't overcome
3274 		 * the stride limitations :( Can happen with
3275 		 * big plane sizes and suitably misaligned
3276 		 * offsets.
3277 		 */
3278 		return intel_plane_check_stride(plane_state);
3279 	}
3280 
3281 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3282 
3283 	for (i = 0; i < num_planes; i++) {
3284 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3285 		plane_state->color_plane[i].offset = 0;
3286 
3287 		if (drm_rotation_90_or_270(rotation)) {
3288 			plane_state->color_plane[i].x = fb->rotated[i].x;
3289 			plane_state->color_plane[i].y = fb->rotated[i].y;
3290 		} else {
3291 			plane_state->color_plane[i].x = fb->normal[i].x;
3292 			plane_state->color_plane[i].y = fb->normal[i].y;
3293 		}
3294 	}
3295 
3296 	/* Rotate src coordinates to match rotated GTT view */
3297 	if (drm_rotation_90_or_270(rotation))
3298 		drm_rect_rotate(&plane_state->uapi.src,
3299 				fb->base.width << 16, fb->base.height << 16,
3300 				DRM_MODE_ROTATE_270);
3301 
3302 	return intel_plane_check_stride(plane_state);
3303 }
3304 
3305 static int i9xx_format_to_fourcc(int format)
3306 {
3307 	switch (format) {
3308 	case DISPPLANE_8BPP:
3309 		return DRM_FORMAT_C8;
3310 	case DISPPLANE_BGRA555:
3311 		return DRM_FORMAT_ARGB1555;
3312 	case DISPPLANE_BGRX555:
3313 		return DRM_FORMAT_XRGB1555;
3314 	case DISPPLANE_BGRX565:
3315 		return DRM_FORMAT_RGB565;
3316 	default:
3317 	case DISPPLANE_BGRX888:
3318 		return DRM_FORMAT_XRGB8888;
3319 	case DISPPLANE_RGBX888:
3320 		return DRM_FORMAT_XBGR8888;
3321 	case DISPPLANE_BGRA888:
3322 		return DRM_FORMAT_ARGB8888;
3323 	case DISPPLANE_RGBA888:
3324 		return DRM_FORMAT_ABGR8888;
3325 	case DISPPLANE_BGRX101010:
3326 		return DRM_FORMAT_XRGB2101010;
3327 	case DISPPLANE_RGBX101010:
3328 		return DRM_FORMAT_XBGR2101010;
3329 	case DISPPLANE_BGRA101010:
3330 		return DRM_FORMAT_ARGB2101010;
3331 	case DISPPLANE_RGBA101010:
3332 		return DRM_FORMAT_ABGR2101010;
3333 	case DISPPLANE_RGBX161616:
3334 		return DRM_FORMAT_XBGR16161616F;
3335 	}
3336 }
3337 
3338 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
3339 {
3340 	switch (format) {
3341 	case PLANE_CTL_FORMAT_RGB_565:
3342 		return DRM_FORMAT_RGB565;
3343 	case PLANE_CTL_FORMAT_NV12:
3344 		return DRM_FORMAT_NV12;
3345 	case PLANE_CTL_FORMAT_XYUV:
3346 		return DRM_FORMAT_XYUV8888;
3347 	case PLANE_CTL_FORMAT_P010:
3348 		return DRM_FORMAT_P010;
3349 	case PLANE_CTL_FORMAT_P012:
3350 		return DRM_FORMAT_P012;
3351 	case PLANE_CTL_FORMAT_P016:
3352 		return DRM_FORMAT_P016;
3353 	case PLANE_CTL_FORMAT_Y210:
3354 		return DRM_FORMAT_Y210;
3355 	case PLANE_CTL_FORMAT_Y212:
3356 		return DRM_FORMAT_Y212;
3357 	case PLANE_CTL_FORMAT_Y216:
3358 		return DRM_FORMAT_Y216;
3359 	case PLANE_CTL_FORMAT_Y410:
3360 		return DRM_FORMAT_XVYU2101010;
3361 	case PLANE_CTL_FORMAT_Y412:
3362 		return DRM_FORMAT_XVYU12_16161616;
3363 	case PLANE_CTL_FORMAT_Y416:
3364 		return DRM_FORMAT_XVYU16161616;
3365 	default:
3366 	case PLANE_CTL_FORMAT_XRGB_8888:
3367 		if (rgb_order) {
3368 			if (alpha)
3369 				return DRM_FORMAT_ABGR8888;
3370 			else
3371 				return DRM_FORMAT_XBGR8888;
3372 		} else {
3373 			if (alpha)
3374 				return DRM_FORMAT_ARGB8888;
3375 			else
3376 				return DRM_FORMAT_XRGB8888;
3377 		}
3378 	case PLANE_CTL_FORMAT_XRGB_2101010:
3379 		if (rgb_order) {
3380 			if (alpha)
3381 				return DRM_FORMAT_ABGR2101010;
3382 			else
3383 				return DRM_FORMAT_XBGR2101010;
3384 		} else {
3385 			if (alpha)
3386 				return DRM_FORMAT_ARGB2101010;
3387 			else
3388 				return DRM_FORMAT_XRGB2101010;
3389 		}
3390 	case PLANE_CTL_FORMAT_XRGB_16161616F:
3391 		if (rgb_order) {
3392 			if (alpha)
3393 				return DRM_FORMAT_ABGR16161616F;
3394 			else
3395 				return DRM_FORMAT_XBGR16161616F;
3396 		} else {
3397 			if (alpha)
3398 				return DRM_FORMAT_ARGB16161616F;
3399 			else
3400 				return DRM_FORMAT_XRGB16161616F;
3401 		}
3402 	}
3403 }
3404 
3405 static struct i915_vma *
3406 initial_plane_vma(struct drm_i915_private *i915,
3407 		  struct intel_initial_plane_config *plane_config)
3408 {
3409 	struct drm_i915_gem_object *obj;
3410 	struct i915_vma *vma;
3411 	u32 base, size;
3412 
3413 	if (plane_config->size == 0)
3414 		return NULL;
3415 
3416 	base = round_down(plane_config->base,
3417 			  I915_GTT_MIN_ALIGNMENT);
3418 	size = round_up(plane_config->base + plane_config->size,
3419 			I915_GTT_MIN_ALIGNMENT);
3420 	size -= base;
3421 
3422 	/*
3423 	 * If the FB is too big, just don't use it since fbdev is not very
3424 	 * important and we should probably use that space with FBC or other
3425 	 * features.
3426 	 */
3427 	if (size * 2 > i915->stolen_usable_size)
3428 		return NULL;
3429 
3430 	obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3431 	if (IS_ERR(obj))
3432 		return NULL;
3433 
3434 	switch (plane_config->tiling) {
3435 	case I915_TILING_NONE:
3436 		break;
3437 	case I915_TILING_X:
3438 	case I915_TILING_Y:
3439 		obj->tiling_and_stride =
3440 			plane_config->fb->base.pitches[0] |
3441 			plane_config->tiling;
3442 		break;
3443 	default:
3444 		MISSING_CASE(plane_config->tiling);
3445 		goto err_obj;
3446 	}
3447 
3448 	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3449 	if (IS_ERR(vma))
3450 		goto err_obj;
3451 
3452 	if (i915_ggtt_pin(vma, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
3453 		goto err_obj;
3454 
3455 	if (i915_gem_object_is_tiled(obj) &&
3456 	    !i915_vma_is_map_and_fenceable(vma))
3457 		goto err_obj;
3458 
3459 	return vma;
3460 
3461 err_obj:
3462 	i915_gem_object_put(obj);
3463 	return NULL;
3464 }
3465 
3466 static bool
3467 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3468 			      struct intel_initial_plane_config *plane_config)
3469 {
3470 	struct drm_device *dev = crtc->base.dev;
3471 	struct drm_i915_private *dev_priv = to_i915(dev);
3472 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3473 	struct drm_framebuffer *fb = &plane_config->fb->base;
3474 	struct i915_vma *vma;
3475 
3476 	switch (fb->modifier) {
3477 	case DRM_FORMAT_MOD_LINEAR:
3478 	case I915_FORMAT_MOD_X_TILED:
3479 	case I915_FORMAT_MOD_Y_TILED:
3480 		break;
3481 	default:
3482 		drm_dbg(&dev_priv->drm,
3483 			"Unsupported modifier for initial FB: 0x%llx\n",
3484 			fb->modifier);
3485 		return false;
3486 	}
3487 
3488 	vma = initial_plane_vma(dev_priv, plane_config);
3489 	if (!vma)
3490 		return false;
3491 
3492 	mode_cmd.pixel_format = fb->format->format;
3493 	mode_cmd.width = fb->width;
3494 	mode_cmd.height = fb->height;
3495 	mode_cmd.pitches[0] = fb->pitches[0];
3496 	mode_cmd.modifier[0] = fb->modifier;
3497 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3498 
3499 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
3500 				   vma->obj, &mode_cmd)) {
3501 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
3502 		goto err_vma;
3503 	}
3504 
3505 	plane_config->vma = vma;
3506 	return true;
3507 
3508 err_vma:
3509 	i915_vma_put(vma);
3510 	return false;
3511 }
3512 
3513 static void
3514 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3515 			struct intel_plane_state *plane_state,
3516 			bool visible)
3517 {
3518 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3519 
3520 	plane_state->uapi.visible = visible;
3521 
3522 	if (visible)
3523 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
3524 	else
3525 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
3526 }
3527 
3528 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3529 {
3530 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3531 	struct drm_plane *plane;
3532 
3533 	/*
3534 	 * Active_planes aliases if multiple "primary" or cursor planes
3535 	 * have been used on the same (or wrong) pipe. plane_mask uses
3536 	 * unique ids, hence we can use that to reconstruct active_planes.
3537 	 */
3538 	crtc_state->active_planes = 0;
3539 
3540 	drm_for_each_plane_mask(plane, &dev_priv->drm,
3541 				crtc_state->uapi.plane_mask)
3542 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3543 }
3544 
3545 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3546 					 struct intel_plane *plane)
3547 {
3548 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3549 	struct intel_crtc_state *crtc_state =
3550 		to_intel_crtc_state(crtc->base.state);
3551 	struct intel_plane_state *plane_state =
3552 		to_intel_plane_state(plane->base.state);
3553 
3554 	drm_dbg_kms(&dev_priv->drm,
3555 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3556 		    plane->base.base.id, plane->base.name,
3557 		    crtc->base.base.id, crtc->base.name);
3558 
3559 	intel_set_plane_visible(crtc_state, plane_state, false);
3560 	fixup_active_planes(crtc_state);
3561 	crtc_state->data_rate[plane->id] = 0;
3562 	crtc_state->min_cdclk[plane->id] = 0;
3563 
3564 	if (plane->id == PLANE_PRIMARY)
3565 		hsw_disable_ips(crtc_state);
3566 
3567 	/*
3568 	 * Vblank time updates from the shadow to live plane control register
3569 	 * are blocked if the memory self-refresh mode is active at that
3570 	 * moment. So to make sure the plane gets truly disabled, disable
3571 	 * first the self-refresh mode. The self-refresh enable bit in turn
3572 	 * will be checked/applied by the HW only at the next frame start
3573 	 * event which is after the vblank start event, so we need to have a
3574 	 * wait-for-vblank between disabling the plane and the pipe.
3575 	 */
3576 	if (HAS_GMCH(dev_priv) &&
3577 	    intel_set_memory_cxsr(dev_priv, false))
3578 		intel_wait_for_vblank(dev_priv, crtc->pipe);
3579 
3580 	/*
3581 	 * Gen2 reports pipe underruns whenever all planes are disabled.
3582 	 * So disable underrun reporting before all the planes get disabled.
3583 	 */
3584 	if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3585 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3586 
3587 	intel_disable_plane(plane, crtc_state);
3588 }
3589 
3590 static struct intel_frontbuffer *
3591 to_intel_frontbuffer(struct drm_framebuffer *fb)
3592 {
3593 	return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3594 }
3595 
3596 static void
3597 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3598 			     struct intel_initial_plane_config *plane_config)
3599 {
3600 	struct drm_device *dev = intel_crtc->base.dev;
3601 	struct drm_i915_private *dev_priv = to_i915(dev);
3602 	struct drm_crtc *c;
3603 	struct drm_plane *primary = intel_crtc->base.primary;
3604 	struct drm_plane_state *plane_state = primary->state;
3605 	struct intel_plane *intel_plane = to_intel_plane(primary);
3606 	struct intel_plane_state *intel_state =
3607 		to_intel_plane_state(plane_state);
3608 	struct drm_framebuffer *fb;
3609 	struct i915_vma *vma;
3610 
3611 	if (!plane_config->fb)
3612 		return;
3613 
3614 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3615 		fb = &plane_config->fb->base;
3616 		vma = plane_config->vma;
3617 		goto valid_fb;
3618 	}
3619 
3620 	/*
3621 	 * Failed to alloc the obj, check to see if we should share
3622 	 * an fb with another CRTC instead
3623 	 */
3624 	for_each_crtc(dev, c) {
3625 		struct intel_plane_state *state;
3626 
3627 		if (c == &intel_crtc->base)
3628 			continue;
3629 
3630 		if (!to_intel_crtc(c)->active)
3631 			continue;
3632 
3633 		state = to_intel_plane_state(c->primary->state);
3634 		if (!state->vma)
3635 			continue;
3636 
3637 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
3638 			fb = state->hw.fb;
3639 			vma = state->vma;
3640 			goto valid_fb;
3641 		}
3642 	}
3643 
3644 	/*
3645 	 * We've failed to reconstruct the BIOS FB.  Current display state
3646 	 * indicates that the primary plane is visible, but has a NULL FB,
3647 	 * which will lead to problems later if we don't fix it up.  The
3648 	 * simplest solution is to just disable the primary plane now and
3649 	 * pretend the BIOS never had it enabled.
3650 	 */
3651 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
3652 
3653 	return;
3654 
3655 valid_fb:
3656 	intel_state->hw.rotation = plane_config->rotation;
3657 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
3658 				intel_state->hw.rotation);
3659 	intel_state->color_plane[0].stride =
3660 		intel_fb_pitch(fb, 0, intel_state->hw.rotation);
3661 
3662 	__i915_vma_pin(vma);
3663 	intel_state->vma = i915_vma_get(vma);
3664 	if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3665 		if (vma->fence)
3666 			intel_state->flags |= PLANE_HAS_FENCE;
3667 
3668 	plane_state->src_x = 0;
3669 	plane_state->src_y = 0;
3670 	plane_state->src_w = fb->width << 16;
3671 	plane_state->src_h = fb->height << 16;
3672 
3673 	plane_state->crtc_x = 0;
3674 	plane_state->crtc_y = 0;
3675 	plane_state->crtc_w = fb->width;
3676 	plane_state->crtc_h = fb->height;
3677 
3678 	intel_state->uapi.src = drm_plane_state_src(plane_state);
3679 	intel_state->uapi.dst = drm_plane_state_dest(plane_state);
3680 
3681 	if (plane_config->tiling)
3682 		dev_priv->preserve_bios_swizzle = true;
3683 
3684 	plane_state->fb = fb;
3685 	drm_framebuffer_get(fb);
3686 
3687 	plane_state->crtc = &intel_crtc->base;
3688 	intel_plane_copy_uapi_to_hw_state(intel_state, intel_state);
3689 
3690 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3691 
3692 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3693 		  &to_intel_frontbuffer(fb)->bits);
3694 }
3695 
3696 static int skl_max_plane_width(const struct drm_framebuffer *fb,
3697 			       int color_plane,
3698 			       unsigned int rotation)
3699 {
3700 	int cpp = fb->format->cpp[color_plane];
3701 
3702 	switch (fb->modifier) {
3703 	case DRM_FORMAT_MOD_LINEAR:
3704 	case I915_FORMAT_MOD_X_TILED:
3705 		/*
3706 		 * Validated limit is 4k, but has 5k should
3707 		 * work apart from the following features:
3708 		 * - Ytile (already limited to 4k)
3709 		 * - FP16 (already limited to 4k)
3710 		 * - render compression (already limited to 4k)
3711 		 * - KVMR sprite and cursor (don't care)
3712 		 * - horizontal panning (TODO verify this)
3713 		 * - pipe and plane scaling (TODO verify this)
3714 		 */
3715 		if (cpp == 8)
3716 			return 4096;
3717 		else
3718 			return 5120;
3719 	case I915_FORMAT_MOD_Y_TILED_CCS:
3720 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3721 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
3722 		/* FIXME AUX plane? */
3723 	case I915_FORMAT_MOD_Y_TILED:
3724 	case I915_FORMAT_MOD_Yf_TILED:
3725 		if (cpp == 8)
3726 			return 2048;
3727 		else
3728 			return 4096;
3729 	default:
3730 		MISSING_CASE(fb->modifier);
3731 		return 2048;
3732 	}
3733 }
3734 
3735 static int glk_max_plane_width(const struct drm_framebuffer *fb,
3736 			       int color_plane,
3737 			       unsigned int rotation)
3738 {
3739 	int cpp = fb->format->cpp[color_plane];
3740 
3741 	switch (fb->modifier) {
3742 	case DRM_FORMAT_MOD_LINEAR:
3743 	case I915_FORMAT_MOD_X_TILED:
3744 		if (cpp == 8)
3745 			return 4096;
3746 		else
3747 			return 5120;
3748 	case I915_FORMAT_MOD_Y_TILED_CCS:
3749 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3750 		/* FIXME AUX plane? */
3751 	case I915_FORMAT_MOD_Y_TILED:
3752 	case I915_FORMAT_MOD_Yf_TILED:
3753 		if (cpp == 8)
3754 			return 2048;
3755 		else
3756 			return 5120;
3757 	default:
3758 		MISSING_CASE(fb->modifier);
3759 		return 2048;
3760 	}
3761 }
3762 
3763 static int icl_max_plane_width(const struct drm_framebuffer *fb,
3764 			       int color_plane,
3765 			       unsigned int rotation)
3766 {
3767 	return 5120;
3768 }
3769 
3770 static int skl_max_plane_height(void)
3771 {
3772 	return 4096;
3773 }
3774 
3775 static int icl_max_plane_height(void)
3776 {
3777 	return 4320;
3778 }
3779 
3780 static bool
3781 skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3782 			       int main_x, int main_y, u32 main_offset,
3783 			       int ccs_plane)
3784 {
3785 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3786 	int aux_x = plane_state->color_plane[ccs_plane].x;
3787 	int aux_y = plane_state->color_plane[ccs_plane].y;
3788 	u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3789 	u32 alignment = intel_surf_alignment(fb, ccs_plane);
3790 	int hsub;
3791 	int vsub;
3792 
3793 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3794 	while (aux_offset >= main_offset && aux_y <= main_y) {
3795 		int x, y;
3796 
3797 		if (aux_x == main_x && aux_y == main_y)
3798 			break;
3799 
3800 		if (aux_offset == 0)
3801 			break;
3802 
3803 		x = aux_x / hsub;
3804 		y = aux_y / vsub;
3805 		aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3806 							       plane_state,
3807 							       ccs_plane,
3808 							       aux_offset,
3809 							       aux_offset -
3810 								alignment);
3811 		aux_x = x * hsub + aux_x % hsub;
3812 		aux_y = y * vsub + aux_y % vsub;
3813 	}
3814 
3815 	if (aux_x != main_x || aux_y != main_y)
3816 		return false;
3817 
3818 	plane_state->color_plane[ccs_plane].offset = aux_offset;
3819 	plane_state->color_plane[ccs_plane].x = aux_x;
3820 	plane_state->color_plane[ccs_plane].y = aux_y;
3821 
3822 	return true;
3823 }
3824 
3825 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3826 {
3827 	struct drm_i915_private *dev_priv = to_i915(plane_state->uapi.plane->dev);
3828 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3829 	unsigned int rotation = plane_state->hw.rotation;
3830 	int x = plane_state->uapi.src.x1 >> 16;
3831 	int y = plane_state->uapi.src.y1 >> 16;
3832 	int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3833 	int h = drm_rect_height(&plane_state->uapi.src) >> 16;
3834 	int max_width;
3835 	int max_height;
3836 	u32 alignment;
3837 	u32 offset;
3838 	int aux_plane = intel_main_to_aux_plane(fb, 0);
3839 	u32 aux_offset = plane_state->color_plane[aux_plane].offset;
3840 
3841 	if (INTEL_GEN(dev_priv) >= 11)
3842 		max_width = icl_max_plane_width(fb, 0, rotation);
3843 	else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
3844 		max_width = glk_max_plane_width(fb, 0, rotation);
3845 	else
3846 		max_width = skl_max_plane_width(fb, 0, rotation);
3847 
3848 	if (INTEL_GEN(dev_priv) >= 11)
3849 		max_height = icl_max_plane_height();
3850 	else
3851 		max_height = skl_max_plane_height();
3852 
3853 	if (w > max_width || h > max_height) {
3854 		drm_dbg_kms(&dev_priv->drm,
3855 			    "requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3856 			    w, h, max_width, max_height);
3857 		return -EINVAL;
3858 	}
3859 
3860 	intel_add_fb_offsets(&x, &y, plane_state, 0);
3861 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3862 	alignment = intel_surf_alignment(fb, 0);
3863 	if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
3864 		return -EINVAL;
3865 
3866 	/*
3867 	 * AUX surface offset is specified as the distance from the
3868 	 * main surface offset, and it must be non-negative. Make
3869 	 * sure that is what we will get.
3870 	 */
3871 	if (offset > aux_offset)
3872 		offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3873 							   offset, aux_offset & ~(alignment - 1));
3874 
3875 	/*
3876 	 * When using an X-tiled surface, the plane blows up
3877 	 * if the x offset + width exceed the stride.
3878 	 *
3879 	 * TODO: linear and Y-tiled seem fine, Yf untested,
3880 	 */
3881 	if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3882 		int cpp = fb->format->cpp[0];
3883 
3884 		while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3885 			if (offset == 0) {
3886 				drm_dbg_kms(&dev_priv->drm,
3887 					    "Unable to find suitable display surface offset due to X-tiling\n");
3888 				return -EINVAL;
3889 			}
3890 
3891 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3892 								   offset, offset - alignment);
3893 		}
3894 	}
3895 
3896 	/*
3897 	 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3898 	 * they match with the main surface x/y offsets.
3899 	 */
3900 	if (is_ccs_modifier(fb->modifier)) {
3901 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3902 						       offset, aux_plane)) {
3903 			if (offset == 0)
3904 				break;
3905 
3906 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3907 								   offset, offset - alignment);
3908 		}
3909 
3910 		if (x != plane_state->color_plane[aux_plane].x ||
3911 		    y != plane_state->color_plane[aux_plane].y) {
3912 			drm_dbg_kms(&dev_priv->drm,
3913 				    "Unable to find suitable display surface offset due to CCS\n");
3914 			return -EINVAL;
3915 		}
3916 	}
3917 
3918 	plane_state->color_plane[0].offset = offset;
3919 	plane_state->color_plane[0].x = x;
3920 	plane_state->color_plane[0].y = y;
3921 
3922 	/*
3923 	 * Put the final coordinates back so that the src
3924 	 * coordinate checks will see the right values.
3925 	 */
3926 	drm_rect_translate_to(&plane_state->uapi.src,
3927 			      x << 16, y << 16);
3928 
3929 	return 0;
3930 }
3931 
3932 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3933 {
3934 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
3935 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3936 	unsigned int rotation = plane_state->hw.rotation;
3937 	int uv_plane = 1;
3938 	int max_width = skl_max_plane_width(fb, uv_plane, rotation);
3939 	int max_height = 4096;
3940 	int x = plane_state->uapi.src.x1 >> 17;
3941 	int y = plane_state->uapi.src.y1 >> 17;
3942 	int w = drm_rect_width(&plane_state->uapi.src) >> 17;
3943 	int h = drm_rect_height(&plane_state->uapi.src) >> 17;
3944 	u32 offset;
3945 
3946 	intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
3947 	offset = intel_plane_compute_aligned_offset(&x, &y,
3948 						    plane_state, uv_plane);
3949 
3950 	/* FIXME not quite sure how/if these apply to the chroma plane */
3951 	if (w > max_width || h > max_height) {
3952 		drm_dbg_kms(&i915->drm,
3953 			    "CbCr source size %dx%d too big (limit %dx%d)\n",
3954 			    w, h, max_width, max_height);
3955 		return -EINVAL;
3956 	}
3957 
3958 	if (is_ccs_modifier(fb->modifier)) {
3959 		int ccs_plane = main_to_ccs_plane(fb, uv_plane);
3960 		int aux_offset = plane_state->color_plane[ccs_plane].offset;
3961 		int alignment = intel_surf_alignment(fb, uv_plane);
3962 
3963 		if (offset > aux_offset)
3964 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3965 								   plane_state,
3966 								   uv_plane,
3967 								   offset,
3968 								   aux_offset & ~(alignment - 1));
3969 
3970 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3971 						       offset, ccs_plane)) {
3972 			if (offset == 0)
3973 				break;
3974 
3975 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3976 								   plane_state,
3977 								   uv_plane,
3978 								   offset, offset - alignment);
3979 		}
3980 
3981 		if (x != plane_state->color_plane[ccs_plane].x ||
3982 		    y != plane_state->color_plane[ccs_plane].y) {
3983 			drm_dbg_kms(&i915->drm,
3984 				    "Unable to find suitable display surface offset due to CCS\n");
3985 			return -EINVAL;
3986 		}
3987 	}
3988 
3989 	plane_state->color_plane[uv_plane].offset = offset;
3990 	plane_state->color_plane[uv_plane].x = x;
3991 	plane_state->color_plane[uv_plane].y = y;
3992 
3993 	return 0;
3994 }
3995 
3996 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3997 {
3998 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3999 	int src_x = plane_state->uapi.src.x1 >> 16;
4000 	int src_y = plane_state->uapi.src.y1 >> 16;
4001 	u32 offset;
4002 	int ccs_plane;
4003 
4004 	for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
4005 		int main_hsub, main_vsub;
4006 		int hsub, vsub;
4007 		int x, y;
4008 
4009 		if (!is_ccs_plane(fb, ccs_plane))
4010 			continue;
4011 
4012 		intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
4013 					       ccs_to_main_plane(fb, ccs_plane));
4014 		intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
4015 
4016 		hsub *= main_hsub;
4017 		vsub *= main_vsub;
4018 		x = src_x / hsub;
4019 		y = src_y / vsub;
4020 
4021 		intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
4022 
4023 		offset = intel_plane_compute_aligned_offset(&x, &y,
4024 							    plane_state,
4025 							    ccs_plane);
4026 
4027 		plane_state->color_plane[ccs_plane].offset = offset;
4028 		plane_state->color_plane[ccs_plane].x = (x * hsub +
4029 							 src_x % hsub) /
4030 							main_hsub;
4031 		plane_state->color_plane[ccs_plane].y = (y * vsub +
4032 							 src_y % vsub) /
4033 							main_vsub;
4034 	}
4035 
4036 	return 0;
4037 }
4038 
4039 int skl_check_plane_surface(struct intel_plane_state *plane_state)
4040 {
4041 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4042 	int ret;
4043 	bool needs_aux = false;
4044 
4045 	ret = intel_plane_compute_gtt(plane_state);
4046 	if (ret)
4047 		return ret;
4048 
4049 	if (!plane_state->uapi.visible)
4050 		return 0;
4051 
4052 	/*
4053 	 * Handle the AUX surface first since the main surface setup depends on
4054 	 * it.
4055 	 */
4056 	if (is_ccs_modifier(fb->modifier)) {
4057 		needs_aux = true;
4058 		ret = skl_check_ccs_aux_surface(plane_state);
4059 		if (ret)
4060 			return ret;
4061 	}
4062 
4063 	if (intel_format_info_is_yuv_semiplanar(fb->format,
4064 						fb->modifier)) {
4065 		needs_aux = true;
4066 		ret = skl_check_nv12_aux_surface(plane_state);
4067 		if (ret)
4068 			return ret;
4069 	}
4070 
4071 	if (!needs_aux) {
4072 		int i;
4073 
4074 		for (i = 1; i < fb->format->num_planes; i++) {
4075 			plane_state->color_plane[i].offset = ~0xfff;
4076 			plane_state->color_plane[i].x = 0;
4077 			plane_state->color_plane[i].y = 0;
4078 		}
4079 	}
4080 
4081 	ret = skl_check_main_surface(plane_state);
4082 	if (ret)
4083 		return ret;
4084 
4085 	return 0;
4086 }
4087 
4088 static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4089 			     const struct intel_plane_state *plane_state,
4090 			     unsigned int *num, unsigned int *den)
4091 {
4092 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4093 	unsigned int cpp = fb->format->cpp[0];
4094 
4095 	/*
4096 	 * g4x bspec says 64bpp pixel rate can't exceed 80%
4097 	 * of cdclk when the sprite plane is enabled on the
4098 	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
4099 	 * never allowed to exceed 80% of cdclk. Let's just go
4100 	 * with the ilk/snb limit always.
4101 	 */
4102 	if (cpp == 8) {
4103 		*num = 10;
4104 		*den = 8;
4105 	} else {
4106 		*num = 1;
4107 		*den = 1;
4108 	}
4109 }
4110 
4111 static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4112 				const struct intel_plane_state *plane_state)
4113 {
4114 	unsigned int pixel_rate;
4115 	unsigned int num, den;
4116 
4117 	/*
4118 	 * Note that crtc_state->pixel_rate accounts for both
4119 	 * horizontal and vertical panel fitter downscaling factors.
4120 	 * Pre-HSW bspec tells us to only consider the horizontal
4121 	 * downscaling factor here. We ignore that and just consider
4122 	 * both for simplicity.
4123 	 */
4124 	pixel_rate = crtc_state->pixel_rate;
4125 
4126 	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4127 
4128 	/* two pixels per clock with double wide pipe */
4129 	if (crtc_state->double_wide)
4130 		den *= 2;
4131 
4132 	return DIV_ROUND_UP(pixel_rate * num, den);
4133 }
4134 
4135 unsigned int
4136 i9xx_plane_max_stride(struct intel_plane *plane,
4137 		      u32 pixel_format, u64 modifier,
4138 		      unsigned int rotation)
4139 {
4140 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4141 
4142 	if (!HAS_GMCH(dev_priv)) {
4143 		return 32*1024;
4144 	} else if (INTEL_GEN(dev_priv) >= 4) {
4145 		if (modifier == I915_FORMAT_MOD_X_TILED)
4146 			return 16*1024;
4147 		else
4148 			return 32*1024;
4149 	} else if (INTEL_GEN(dev_priv) >= 3) {
4150 		if (modifier == I915_FORMAT_MOD_X_TILED)
4151 			return 8*1024;
4152 		else
4153 			return 16*1024;
4154 	} else {
4155 		if (plane->i9xx_plane == PLANE_C)
4156 			return 4*1024;
4157 		else
4158 			return 8*1024;
4159 	}
4160 }
4161 
4162 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4163 {
4164 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4165 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4166 	u32 dspcntr = 0;
4167 
4168 	if (crtc_state->gamma_enable)
4169 		dspcntr |= DISPPLANE_GAMMA_ENABLE;
4170 
4171 	if (crtc_state->csc_enable)
4172 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4173 
4174 	if (INTEL_GEN(dev_priv) < 5)
4175 		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4176 
4177 	return dspcntr;
4178 }
4179 
4180 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4181 			  const struct intel_plane_state *plane_state)
4182 {
4183 	struct drm_i915_private *dev_priv =
4184 		to_i915(plane_state->uapi.plane->dev);
4185 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4186 	unsigned int rotation = plane_state->hw.rotation;
4187 	u32 dspcntr;
4188 
4189 	dspcntr = DISPLAY_PLANE_ENABLE;
4190 
4191 	if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4192 	    IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
4193 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
4194 
4195 	switch (fb->format->format) {
4196 	case DRM_FORMAT_C8:
4197 		dspcntr |= DISPPLANE_8BPP;
4198 		break;
4199 	case DRM_FORMAT_XRGB1555:
4200 		dspcntr |= DISPPLANE_BGRX555;
4201 		break;
4202 	case DRM_FORMAT_ARGB1555:
4203 		dspcntr |= DISPPLANE_BGRA555;
4204 		break;
4205 	case DRM_FORMAT_RGB565:
4206 		dspcntr |= DISPPLANE_BGRX565;
4207 		break;
4208 	case DRM_FORMAT_XRGB8888:
4209 		dspcntr |= DISPPLANE_BGRX888;
4210 		break;
4211 	case DRM_FORMAT_XBGR8888:
4212 		dspcntr |= DISPPLANE_RGBX888;
4213 		break;
4214 	case DRM_FORMAT_ARGB8888:
4215 		dspcntr |= DISPPLANE_BGRA888;
4216 		break;
4217 	case DRM_FORMAT_ABGR8888:
4218 		dspcntr |= DISPPLANE_RGBA888;
4219 		break;
4220 	case DRM_FORMAT_XRGB2101010:
4221 		dspcntr |= DISPPLANE_BGRX101010;
4222 		break;
4223 	case DRM_FORMAT_XBGR2101010:
4224 		dspcntr |= DISPPLANE_RGBX101010;
4225 		break;
4226 	case DRM_FORMAT_ARGB2101010:
4227 		dspcntr |= DISPPLANE_BGRA101010;
4228 		break;
4229 	case DRM_FORMAT_ABGR2101010:
4230 		dspcntr |= DISPPLANE_RGBA101010;
4231 		break;
4232 	case DRM_FORMAT_XBGR16161616F:
4233 		dspcntr |= DISPPLANE_RGBX161616;
4234 		break;
4235 	default:
4236 		MISSING_CASE(fb->format->format);
4237 		return 0;
4238 	}
4239 
4240 	if (INTEL_GEN(dev_priv) >= 4 &&
4241 	    fb->modifier == I915_FORMAT_MOD_X_TILED)
4242 		dspcntr |= DISPPLANE_TILED;
4243 
4244 	if (rotation & DRM_MODE_ROTATE_180)
4245 		dspcntr |= DISPPLANE_ROTATE_180;
4246 
4247 	if (rotation & DRM_MODE_REFLECT_X)
4248 		dspcntr |= DISPPLANE_MIRROR;
4249 
4250 	return dspcntr;
4251 }
4252 
4253 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
4254 {
4255 	struct drm_i915_private *dev_priv =
4256 		to_i915(plane_state->uapi.plane->dev);
4257 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4258 	int src_x, src_y, src_w;
4259 	u32 offset;
4260 	int ret;
4261 
4262 	ret = intel_plane_compute_gtt(plane_state);
4263 	if (ret)
4264 		return ret;
4265 
4266 	if (!plane_state->uapi.visible)
4267 		return 0;
4268 
4269 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4270 	src_x = plane_state->uapi.src.x1 >> 16;
4271 	src_y = plane_state->uapi.src.y1 >> 16;
4272 
4273 	/* Undocumented hardware limit on i965/g4x/vlv/chv */
4274 	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4275 		return -EINVAL;
4276 
4277 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
4278 
4279 	if (INTEL_GEN(dev_priv) >= 4)
4280 		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4281 							    plane_state, 0);
4282 	else
4283 		offset = 0;
4284 
4285 	/*
4286 	 * Put the final coordinates back so that the src
4287 	 * coordinate checks will see the right values.
4288 	 */
4289 	drm_rect_translate_to(&plane_state->uapi.src,
4290 			      src_x << 16, src_y << 16);
4291 
4292 	/* HSW/BDW do this automagically in hardware */
4293 	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
4294 		unsigned int rotation = plane_state->hw.rotation;
4295 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4296 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4297 
4298 		if (rotation & DRM_MODE_ROTATE_180) {
4299 			src_x += src_w - 1;
4300 			src_y += src_h - 1;
4301 		} else if (rotation & DRM_MODE_REFLECT_X) {
4302 			src_x += src_w - 1;
4303 		}
4304 	}
4305 
4306 	plane_state->color_plane[0].offset = offset;
4307 	plane_state->color_plane[0].x = src_x;
4308 	plane_state->color_plane[0].y = src_y;
4309 
4310 	return 0;
4311 }
4312 
4313 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4314 {
4315 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4316 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4317 
4318 	if (IS_CHERRYVIEW(dev_priv))
4319 		return i9xx_plane == PLANE_B;
4320 	else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4321 		return false;
4322 	else if (IS_GEN(dev_priv, 4))
4323 		return i9xx_plane == PLANE_C;
4324 	else
4325 		return i9xx_plane == PLANE_B ||
4326 			i9xx_plane == PLANE_C;
4327 }
4328 
4329 static int
4330 i9xx_plane_check(struct intel_crtc_state *crtc_state,
4331 		 struct intel_plane_state *plane_state)
4332 {
4333 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4334 	int ret;
4335 
4336 	ret = chv_plane_check_rotation(plane_state);
4337 	if (ret)
4338 		return ret;
4339 
4340 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
4341 						  &crtc_state->uapi,
4342 						  DRM_PLANE_HELPER_NO_SCALING,
4343 						  DRM_PLANE_HELPER_NO_SCALING,
4344 						  i9xx_plane_has_windowing(plane),
4345 						  true);
4346 	if (ret)
4347 		return ret;
4348 
4349 	ret = i9xx_check_plane_surface(plane_state);
4350 	if (ret)
4351 		return ret;
4352 
4353 	if (!plane_state->uapi.visible)
4354 		return 0;
4355 
4356 	ret = intel_plane_check_src_coordinates(plane_state);
4357 	if (ret)
4358 		return ret;
4359 
4360 	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4361 
4362 	return 0;
4363 }
4364 
4365 static void i9xx_update_plane(struct intel_plane *plane,
4366 			      const struct intel_crtc_state *crtc_state,
4367 			      const struct intel_plane_state *plane_state)
4368 {
4369 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4370 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4371 	u32 linear_offset;
4372 	int x = plane_state->color_plane[0].x;
4373 	int y = plane_state->color_plane[0].y;
4374 	int crtc_x = plane_state->uapi.dst.x1;
4375 	int crtc_y = plane_state->uapi.dst.y1;
4376 	int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4377 	int crtc_h = drm_rect_height(&plane_state->uapi.dst);
4378 	unsigned long irqflags;
4379 	u32 dspaddr_offset;
4380 	u32 dspcntr;
4381 
4382 	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
4383 
4384 	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
4385 
4386 	if (INTEL_GEN(dev_priv) >= 4)
4387 		dspaddr_offset = plane_state->color_plane[0].offset;
4388 	else
4389 		dspaddr_offset = linear_offset;
4390 
4391 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4392 
4393 	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4394 			  plane_state->color_plane[0].stride);
4395 
4396 	if (INTEL_GEN(dev_priv) < 4) {
4397 		/*
4398 		 * PLANE_A doesn't actually have a full window
4399 		 * generator but let's assume we still need to
4400 		 * program whatever is there.
4401 		 */
4402 		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4403 				  (crtc_y << 16) | crtc_x);
4404 		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4405 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4406 	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
4407 		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4408 				  (crtc_y << 16) | crtc_x);
4409 		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4410 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4411 		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
4412 	}
4413 
4414 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
4415 		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4416 				  (y << 16) | x);
4417 	} else if (INTEL_GEN(dev_priv) >= 4) {
4418 		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4419 				  linear_offset);
4420 		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4421 				  (y << 16) | x);
4422 	}
4423 
4424 	/*
4425 	 * The control register self-arms if the plane was previously
4426 	 * disabled. Try to make the plane enable atomic by writing
4427 	 * the control register just before the surface register.
4428 	 */
4429 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4430 	if (INTEL_GEN(dev_priv) >= 4)
4431 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4432 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4433 	else
4434 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4435 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4436 
4437 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4438 }
4439 
4440 static void i9xx_disable_plane(struct intel_plane *plane,
4441 			       const struct intel_crtc_state *crtc_state)
4442 {
4443 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4444 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4445 	unsigned long irqflags;
4446 	u32 dspcntr;
4447 
4448 	/*
4449 	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4450 	 * enable on ilk+ affect the pipe bottom color as
4451 	 * well, so we must configure them even if the plane
4452 	 * is disabled.
4453 	 *
4454 	 * On pre-g4x there is no way to gamma correct the
4455 	 * pipe bottom color but we'll keep on doing this
4456 	 * anyway so that the crtc state readout works correctly.
4457 	 */
4458 	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
4459 
4460 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4461 
4462 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4463 	if (INTEL_GEN(dev_priv) >= 4)
4464 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
4465 	else
4466 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
4467 
4468 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4469 }
4470 
4471 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4472 				    enum pipe *pipe)
4473 {
4474 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4475 	enum intel_display_power_domain power_domain;
4476 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4477 	intel_wakeref_t wakeref;
4478 	bool ret;
4479 	u32 val;
4480 
4481 	/*
4482 	 * Not 100% correct for planes that can move between pipes,
4483 	 * but that's only the case for gen2-4 which don't have any
4484 	 * display power wells.
4485 	 */
4486 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
4487 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4488 	if (!wakeref)
4489 		return false;
4490 
4491 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4492 
4493 	ret = val & DISPLAY_PLANE_ENABLE;
4494 
4495 	if (INTEL_GEN(dev_priv) >= 5)
4496 		*pipe = plane->pipe;
4497 	else
4498 		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4499 			DISPPLANE_SEL_PIPE_SHIFT;
4500 
4501 	intel_display_power_put(dev_priv, power_domain, wakeref);
4502 
4503 	return ret;
4504 }
4505 
4506 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4507 {
4508 	struct drm_device *dev = intel_crtc->base.dev;
4509 	struct drm_i915_private *dev_priv = to_i915(dev);
4510 	unsigned long irqflags;
4511 
4512 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4513 
4514 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4515 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4516 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
4517 
4518 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4519 }
4520 
4521 /*
4522  * This function detaches (aka. unbinds) unused scalers in hardware
4523  */
4524 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
4525 {
4526 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4527 	const struct intel_crtc_scaler_state *scaler_state =
4528 		&crtc_state->scaler_state;
4529 	int i;
4530 
4531 	/* loop through and disable scalers that aren't in use */
4532 	for (i = 0; i < intel_crtc->num_scalers; i++) {
4533 		if (!scaler_state->scalers[i].in_use)
4534 			skl_detach_scaler(intel_crtc, i);
4535 	}
4536 }
4537 
4538 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4539 					  int color_plane, unsigned int rotation)
4540 {
4541 	/*
4542 	 * The stride is either expressed as a multiple of 64 bytes chunks for
4543 	 * linear buffers or in number of tiles for tiled buffers.
4544 	 */
4545 	if (is_surface_linear(fb, color_plane))
4546 		return 64;
4547 	else if (drm_rotation_90_or_270(rotation))
4548 		return intel_tile_height(fb, color_plane);
4549 	else
4550 		return intel_tile_width_bytes(fb, color_plane);
4551 }
4552 
4553 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
4554 		     int color_plane)
4555 {
4556 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4557 	unsigned int rotation = plane_state->hw.rotation;
4558 	u32 stride = plane_state->color_plane[color_plane].stride;
4559 
4560 	if (color_plane >= fb->format->num_planes)
4561 		return 0;
4562 
4563 	return stride / skl_plane_stride_mult(fb, color_plane, rotation);
4564 }
4565 
4566 static u32 skl_plane_ctl_format(u32 pixel_format)
4567 {
4568 	switch (pixel_format) {
4569 	case DRM_FORMAT_C8:
4570 		return PLANE_CTL_FORMAT_INDEXED;
4571 	case DRM_FORMAT_RGB565:
4572 		return PLANE_CTL_FORMAT_RGB_565;
4573 	case DRM_FORMAT_XBGR8888:
4574 	case DRM_FORMAT_ABGR8888:
4575 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
4576 	case DRM_FORMAT_XRGB8888:
4577 	case DRM_FORMAT_ARGB8888:
4578 		return PLANE_CTL_FORMAT_XRGB_8888;
4579 	case DRM_FORMAT_XBGR2101010:
4580 	case DRM_FORMAT_ABGR2101010:
4581 		return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
4582 	case DRM_FORMAT_XRGB2101010:
4583 	case DRM_FORMAT_ARGB2101010:
4584 		return PLANE_CTL_FORMAT_XRGB_2101010;
4585 	case DRM_FORMAT_XBGR16161616F:
4586 	case DRM_FORMAT_ABGR16161616F:
4587 		return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4588 	case DRM_FORMAT_XRGB16161616F:
4589 	case DRM_FORMAT_ARGB16161616F:
4590 		return PLANE_CTL_FORMAT_XRGB_16161616F;
4591 	case DRM_FORMAT_XYUV8888:
4592 		return PLANE_CTL_FORMAT_XYUV;
4593 	case DRM_FORMAT_YUYV:
4594 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
4595 	case DRM_FORMAT_YVYU:
4596 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
4597 	case DRM_FORMAT_UYVY:
4598 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
4599 	case DRM_FORMAT_VYUY:
4600 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
4601 	case DRM_FORMAT_NV12:
4602 		return PLANE_CTL_FORMAT_NV12;
4603 	case DRM_FORMAT_P010:
4604 		return PLANE_CTL_FORMAT_P010;
4605 	case DRM_FORMAT_P012:
4606 		return PLANE_CTL_FORMAT_P012;
4607 	case DRM_FORMAT_P016:
4608 		return PLANE_CTL_FORMAT_P016;
4609 	case DRM_FORMAT_Y210:
4610 		return PLANE_CTL_FORMAT_Y210;
4611 	case DRM_FORMAT_Y212:
4612 		return PLANE_CTL_FORMAT_Y212;
4613 	case DRM_FORMAT_Y216:
4614 		return PLANE_CTL_FORMAT_Y216;
4615 	case DRM_FORMAT_XVYU2101010:
4616 		return PLANE_CTL_FORMAT_Y410;
4617 	case DRM_FORMAT_XVYU12_16161616:
4618 		return PLANE_CTL_FORMAT_Y412;
4619 	case DRM_FORMAT_XVYU16161616:
4620 		return PLANE_CTL_FORMAT_Y416;
4621 	default:
4622 		MISSING_CASE(pixel_format);
4623 	}
4624 
4625 	return 0;
4626 }
4627 
4628 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4629 {
4630 	if (!plane_state->hw.fb->format->has_alpha)
4631 		return PLANE_CTL_ALPHA_DISABLE;
4632 
4633 	switch (plane_state->hw.pixel_blend_mode) {
4634 	case DRM_MODE_BLEND_PIXEL_NONE:
4635 		return PLANE_CTL_ALPHA_DISABLE;
4636 	case DRM_MODE_BLEND_PREMULTI:
4637 		return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4638 	case DRM_MODE_BLEND_COVERAGE:
4639 		return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4640 	default:
4641 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4642 		return PLANE_CTL_ALPHA_DISABLE;
4643 	}
4644 }
4645 
4646 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4647 {
4648 	if (!plane_state->hw.fb->format->has_alpha)
4649 		return PLANE_COLOR_ALPHA_DISABLE;
4650 
4651 	switch (plane_state->hw.pixel_blend_mode) {
4652 	case DRM_MODE_BLEND_PIXEL_NONE:
4653 		return PLANE_COLOR_ALPHA_DISABLE;
4654 	case DRM_MODE_BLEND_PREMULTI:
4655 		return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4656 	case DRM_MODE_BLEND_COVERAGE:
4657 		return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4658 	default:
4659 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4660 		return PLANE_COLOR_ALPHA_DISABLE;
4661 	}
4662 }
4663 
4664 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4665 {
4666 	switch (fb_modifier) {
4667 	case DRM_FORMAT_MOD_LINEAR:
4668 		break;
4669 	case I915_FORMAT_MOD_X_TILED:
4670 		return PLANE_CTL_TILED_X;
4671 	case I915_FORMAT_MOD_Y_TILED:
4672 		return PLANE_CTL_TILED_Y;
4673 	case I915_FORMAT_MOD_Y_TILED_CCS:
4674 		return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4675 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4676 		return PLANE_CTL_TILED_Y |
4677 		       PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4678 		       PLANE_CTL_CLEAR_COLOR_DISABLE;
4679 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4680 		return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
4681 	case I915_FORMAT_MOD_Yf_TILED:
4682 		return PLANE_CTL_TILED_YF;
4683 	case I915_FORMAT_MOD_Yf_TILED_CCS:
4684 		return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4685 	default:
4686 		MISSING_CASE(fb_modifier);
4687 	}
4688 
4689 	return 0;
4690 }
4691 
4692 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4693 {
4694 	switch (rotate) {
4695 	case DRM_MODE_ROTATE_0:
4696 		break;
4697 	/*
4698 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4699 	 * while i915 HW rotation is clockwise, thats why this swapping.
4700 	 */
4701 	case DRM_MODE_ROTATE_90:
4702 		return PLANE_CTL_ROTATE_270;
4703 	case DRM_MODE_ROTATE_180:
4704 		return PLANE_CTL_ROTATE_180;
4705 	case DRM_MODE_ROTATE_270:
4706 		return PLANE_CTL_ROTATE_90;
4707 	default:
4708 		MISSING_CASE(rotate);
4709 	}
4710 
4711 	return 0;
4712 }
4713 
4714 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4715 {
4716 	switch (reflect) {
4717 	case 0:
4718 		break;
4719 	case DRM_MODE_REFLECT_X:
4720 		return PLANE_CTL_FLIP_HORIZONTAL;
4721 	case DRM_MODE_REFLECT_Y:
4722 	default:
4723 		MISSING_CASE(reflect);
4724 	}
4725 
4726 	return 0;
4727 }
4728 
4729 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4730 {
4731 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4732 	u32 plane_ctl = 0;
4733 
4734 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4735 		return plane_ctl;
4736 
4737 	if (crtc_state->gamma_enable)
4738 		plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4739 
4740 	if (crtc_state->csc_enable)
4741 		plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4742 
4743 	return plane_ctl;
4744 }
4745 
4746 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4747 		  const struct intel_plane_state *plane_state)
4748 {
4749 	struct drm_i915_private *dev_priv =
4750 		to_i915(plane_state->uapi.plane->dev);
4751 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4752 	unsigned int rotation = plane_state->hw.rotation;
4753 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4754 	u32 plane_ctl;
4755 
4756 	plane_ctl = PLANE_CTL_ENABLE;
4757 
4758 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4759 		plane_ctl |= skl_plane_ctl_alpha(plane_state);
4760 		plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4761 
4762 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4763 			plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4764 
4765 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4766 			plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4767 	}
4768 
4769 	plane_ctl |= skl_plane_ctl_format(fb->format->format);
4770 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4771 	plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4772 
4773 	if (INTEL_GEN(dev_priv) >= 10)
4774 		plane_ctl |= cnl_plane_ctl_flip(rotation &
4775 						DRM_MODE_REFLECT_MASK);
4776 
4777 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
4778 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4779 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
4780 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4781 
4782 	return plane_ctl;
4783 }
4784 
4785 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4786 {
4787 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4788 	u32 plane_color_ctl = 0;
4789 
4790 	if (INTEL_GEN(dev_priv) >= 11)
4791 		return plane_color_ctl;
4792 
4793 	if (crtc_state->gamma_enable)
4794 		plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4795 
4796 	if (crtc_state->csc_enable)
4797 		plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4798 
4799 	return plane_color_ctl;
4800 }
4801 
4802 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4803 			const struct intel_plane_state *plane_state)
4804 {
4805 	struct drm_i915_private *dev_priv =
4806 		to_i915(plane_state->uapi.plane->dev);
4807 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4808 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4809 	u32 plane_color_ctl = 0;
4810 
4811 	plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4812 	plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4813 
4814 	if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4815 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4816 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4817 		else
4818 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
4819 
4820 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4821 			plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4822 	} else if (fb->format->is_yuv) {
4823 		plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4824 	}
4825 
4826 	return plane_color_ctl;
4827 }
4828 
4829 static int
4830 __intel_display_resume(struct drm_device *dev,
4831 		       struct drm_atomic_state *state,
4832 		       struct drm_modeset_acquire_ctx *ctx)
4833 {
4834 	struct drm_crtc_state *crtc_state;
4835 	struct drm_crtc *crtc;
4836 	int i, ret;
4837 
4838 	intel_modeset_setup_hw_state(dev, ctx);
4839 	intel_vga_redisable(to_i915(dev));
4840 
4841 	if (!state)
4842 		return 0;
4843 
4844 	/*
4845 	 * We've duplicated the state, pointers to the old state are invalid.
4846 	 *
4847 	 * Don't attempt to use the old state until we commit the duplicated state.
4848 	 */
4849 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4850 		/*
4851 		 * Force recalculation even if we restore
4852 		 * current state. With fast modeset this may not result
4853 		 * in a modeset when the state is compatible.
4854 		 */
4855 		crtc_state->mode_changed = true;
4856 	}
4857 
4858 	/* ignore any reset values/BIOS leftovers in the WM registers */
4859 	if (!HAS_GMCH(to_i915(dev)))
4860 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
4861 
4862 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4863 
4864 	drm_WARN_ON(dev, ret == -EDEADLK);
4865 	return ret;
4866 }
4867 
4868 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4869 {
4870 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4871 		intel_has_gpu_reset(&dev_priv->gt));
4872 }
4873 
4874 void intel_prepare_reset(struct drm_i915_private *dev_priv)
4875 {
4876 	struct drm_device *dev = &dev_priv->drm;
4877 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4878 	struct drm_atomic_state *state;
4879 	int ret;
4880 
4881 	/* reset doesn't touch the display */
4882 	if (!i915_modparams.force_reset_modeset_test &&
4883 	    !gpu_reset_clobbers_display(dev_priv))
4884 		return;
4885 
4886 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
4887 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4888 	smp_mb__after_atomic();
4889 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4890 
4891 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4892 		drm_dbg_kms(&dev_priv->drm,
4893 			    "Modeset potentially stuck, unbreaking through wedging\n");
4894 		intel_gt_set_wedged(&dev_priv->gt);
4895 	}
4896 
4897 	/*
4898 	 * Need mode_config.mutex so that we don't
4899 	 * trample ongoing ->detect() and whatnot.
4900 	 */
4901 	mutex_lock(&dev->mode_config.mutex);
4902 	drm_modeset_acquire_init(ctx, 0);
4903 	while (1) {
4904 		ret = drm_modeset_lock_all_ctx(dev, ctx);
4905 		if (ret != -EDEADLK)
4906 			break;
4907 
4908 		drm_modeset_backoff(ctx);
4909 	}
4910 	/*
4911 	 * Disabling the crtcs gracefully seems nicer. Also the
4912 	 * g33 docs say we should at least disable all the planes.
4913 	 */
4914 	state = drm_atomic_helper_duplicate_state(dev, ctx);
4915 	if (IS_ERR(state)) {
4916 		ret = PTR_ERR(state);
4917 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4918 			ret);
4919 		return;
4920 	}
4921 
4922 	ret = drm_atomic_helper_disable_all(dev, ctx);
4923 	if (ret) {
4924 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4925 			ret);
4926 		drm_atomic_state_put(state);
4927 		return;
4928 	}
4929 
4930 	dev_priv->modeset_restore_state = state;
4931 	state->acquire_ctx = ctx;
4932 }
4933 
4934 void intel_finish_reset(struct drm_i915_private *dev_priv)
4935 {
4936 	struct drm_device *dev = &dev_priv->drm;
4937 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4938 	struct drm_atomic_state *state;
4939 	int ret;
4940 
4941 	/* reset doesn't touch the display */
4942 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
4943 		return;
4944 
4945 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
4946 	if (!state)
4947 		goto unlock;
4948 
4949 	/* reset doesn't touch the display */
4950 	if (!gpu_reset_clobbers_display(dev_priv)) {
4951 		/* for testing only restore the display */
4952 		ret = __intel_display_resume(dev, state, ctx);
4953 		if (ret)
4954 			drm_err(&dev_priv->drm,
4955 				"Restoring old state failed with %i\n", ret);
4956 	} else {
4957 		/*
4958 		 * The display has been reset as well,
4959 		 * so need a full re-initialization.
4960 		 */
4961 		intel_pps_unlock_regs_wa(dev_priv);
4962 		intel_modeset_init_hw(dev_priv);
4963 		intel_init_clock_gating(dev_priv);
4964 
4965 		spin_lock_irq(&dev_priv->irq_lock);
4966 		if (dev_priv->display.hpd_irq_setup)
4967 			dev_priv->display.hpd_irq_setup(dev_priv);
4968 		spin_unlock_irq(&dev_priv->irq_lock);
4969 
4970 		ret = __intel_display_resume(dev, state, ctx);
4971 		if (ret)
4972 			drm_err(&dev_priv->drm,
4973 				"Restoring old state failed with %i\n", ret);
4974 
4975 		intel_hpd_init(dev_priv);
4976 	}
4977 
4978 	drm_atomic_state_put(state);
4979 unlock:
4980 	drm_modeset_drop_locks(ctx);
4981 	drm_modeset_acquire_fini(ctx);
4982 	mutex_unlock(&dev->mode_config.mutex);
4983 
4984 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4985 }
4986 
4987 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4988 {
4989 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4990 	enum pipe pipe = crtc->pipe;
4991 	u32 tmp;
4992 
4993 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
4994 
4995 	/*
4996 	 * Display WA #1153: icl
4997 	 * enable hardware to bypass the alpha math
4998 	 * and rounding for per-pixel values 00 and 0xff
4999 	 */
5000 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
5001 	/*
5002 	 * Display WA # 1605353570: icl
5003 	 * Set the pixel rounding bit to 1 for allowing
5004 	 * passthrough of Frame buffer pixels unmodified
5005 	 * across pipe
5006 	 */
5007 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
5008 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
5009 }
5010 
5011 static void intel_fdi_normal_train(struct intel_crtc *crtc)
5012 {
5013 	struct drm_device *dev = crtc->base.dev;
5014 	struct drm_i915_private *dev_priv = to_i915(dev);
5015 	enum pipe pipe = crtc->pipe;
5016 	i915_reg_t reg;
5017 	u32 temp;
5018 
5019 	/* enable normal train */
5020 	reg = FDI_TX_CTL(pipe);
5021 	temp = intel_de_read(dev_priv, reg);
5022 	if (IS_IVYBRIDGE(dev_priv)) {
5023 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5024 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
5025 	} else {
5026 		temp &= ~FDI_LINK_TRAIN_NONE;
5027 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
5028 	}
5029 	intel_de_write(dev_priv, reg, temp);
5030 
5031 	reg = FDI_RX_CTL(pipe);
5032 	temp = intel_de_read(dev_priv, reg);
5033 	if (HAS_PCH_CPT(dev_priv)) {
5034 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5035 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5036 	} else {
5037 		temp &= ~FDI_LINK_TRAIN_NONE;
5038 		temp |= FDI_LINK_TRAIN_NONE;
5039 	}
5040 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5041 
5042 	/* wait one idle pattern time */
5043 	intel_de_posting_read(dev_priv, reg);
5044 	udelay(1000);
5045 
5046 	/* IVB wants error correction enabled */
5047 	if (IS_IVYBRIDGE(dev_priv))
5048 		intel_de_write(dev_priv, reg,
5049 		               intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5050 }
5051 
5052 /* The FDI link training functions for ILK/Ibexpeak. */
5053 static void ilk_fdi_link_train(struct intel_crtc *crtc,
5054 			       const struct intel_crtc_state *crtc_state)
5055 {
5056 	struct drm_device *dev = crtc->base.dev;
5057 	struct drm_i915_private *dev_priv = to_i915(dev);
5058 	enum pipe pipe = crtc->pipe;
5059 	i915_reg_t reg;
5060 	u32 temp, tries;
5061 
5062 	/* FDI needs bits from pipe first */
5063 	assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
5064 
5065 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5066 	   for train result */
5067 	reg = FDI_RX_IMR(pipe);
5068 	temp = intel_de_read(dev_priv, reg);
5069 	temp &= ~FDI_RX_SYMBOL_LOCK;
5070 	temp &= ~FDI_RX_BIT_LOCK;
5071 	intel_de_write(dev_priv, reg, temp);
5072 	intel_de_read(dev_priv, reg);
5073 	udelay(150);
5074 
5075 	/* enable CPU FDI TX and PCH FDI RX */
5076 	reg = FDI_TX_CTL(pipe);
5077 	temp = intel_de_read(dev_priv, reg);
5078 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5079 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5080 	temp &= ~FDI_LINK_TRAIN_NONE;
5081 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5082 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5083 
5084 	reg = FDI_RX_CTL(pipe);
5085 	temp = intel_de_read(dev_priv, reg);
5086 	temp &= ~FDI_LINK_TRAIN_NONE;
5087 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5088 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5089 
5090 	intel_de_posting_read(dev_priv, reg);
5091 	udelay(150);
5092 
5093 	/* Ironlake workaround, enable clock pointer after FDI enable*/
5094 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5095 		       FDI_RX_PHASE_SYNC_POINTER_OVR);
5096 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5097 		       FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5098 
5099 	reg = FDI_RX_IIR(pipe);
5100 	for (tries = 0; tries < 5; tries++) {
5101 		temp = intel_de_read(dev_priv, reg);
5102 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5103 
5104 		if ((temp & FDI_RX_BIT_LOCK)) {
5105 			drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
5106 			intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
5107 			break;
5108 		}
5109 	}
5110 	if (tries == 5)
5111 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5112 
5113 	/* Train 2 */
5114 	reg = FDI_TX_CTL(pipe);
5115 	temp = intel_de_read(dev_priv, reg);
5116 	temp &= ~FDI_LINK_TRAIN_NONE;
5117 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5118 	intel_de_write(dev_priv, reg, temp);
5119 
5120 	reg = FDI_RX_CTL(pipe);
5121 	temp = intel_de_read(dev_priv, reg);
5122 	temp &= ~FDI_LINK_TRAIN_NONE;
5123 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5124 	intel_de_write(dev_priv, reg, temp);
5125 
5126 	intel_de_posting_read(dev_priv, reg);
5127 	udelay(150);
5128 
5129 	reg = FDI_RX_IIR(pipe);
5130 	for (tries = 0; tries < 5; tries++) {
5131 		temp = intel_de_read(dev_priv, reg);
5132 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5133 
5134 		if (temp & FDI_RX_SYMBOL_LOCK) {
5135 			intel_de_write(dev_priv, reg,
5136 				       temp | FDI_RX_SYMBOL_LOCK);
5137 			drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
5138 			break;
5139 		}
5140 	}
5141 	if (tries == 5)
5142 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5143 
5144 	drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5145 
5146 }
5147 
5148 static const int snb_b_fdi_train_param[] = {
5149 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5150 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5151 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5152 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5153 };
5154 
5155 /* The FDI link training functions for SNB/Cougarpoint. */
5156 static void gen6_fdi_link_train(struct intel_crtc *crtc,
5157 				const struct intel_crtc_state *crtc_state)
5158 {
5159 	struct drm_device *dev = crtc->base.dev;
5160 	struct drm_i915_private *dev_priv = to_i915(dev);
5161 	enum pipe pipe = crtc->pipe;
5162 	i915_reg_t reg;
5163 	u32 temp, i, retry;
5164 
5165 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5166 	   for train result */
5167 	reg = FDI_RX_IMR(pipe);
5168 	temp = intel_de_read(dev_priv, reg);
5169 	temp &= ~FDI_RX_SYMBOL_LOCK;
5170 	temp &= ~FDI_RX_BIT_LOCK;
5171 	intel_de_write(dev_priv, reg, temp);
5172 
5173 	intel_de_posting_read(dev_priv, reg);
5174 	udelay(150);
5175 
5176 	/* enable CPU FDI TX and PCH FDI RX */
5177 	reg = FDI_TX_CTL(pipe);
5178 	temp = intel_de_read(dev_priv, reg);
5179 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5180 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5181 	temp &= ~FDI_LINK_TRAIN_NONE;
5182 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5183 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5184 	/* SNB-B */
5185 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5186 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5187 
5188 	intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5189 		       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5190 
5191 	reg = FDI_RX_CTL(pipe);
5192 	temp = intel_de_read(dev_priv, reg);
5193 	if (HAS_PCH_CPT(dev_priv)) {
5194 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5195 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5196 	} else {
5197 		temp &= ~FDI_LINK_TRAIN_NONE;
5198 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5199 	}
5200 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5201 
5202 	intel_de_posting_read(dev_priv, reg);
5203 	udelay(150);
5204 
5205 	for (i = 0; i < 4; i++) {
5206 		reg = FDI_TX_CTL(pipe);
5207 		temp = intel_de_read(dev_priv, reg);
5208 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5209 		temp |= snb_b_fdi_train_param[i];
5210 		intel_de_write(dev_priv, reg, temp);
5211 
5212 		intel_de_posting_read(dev_priv, reg);
5213 		udelay(500);
5214 
5215 		for (retry = 0; retry < 5; retry++) {
5216 			reg = FDI_RX_IIR(pipe);
5217 			temp = intel_de_read(dev_priv, reg);
5218 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5219 			if (temp & FDI_RX_BIT_LOCK) {
5220 				intel_de_write(dev_priv, reg,
5221 					       temp | FDI_RX_BIT_LOCK);
5222 				drm_dbg_kms(&dev_priv->drm,
5223 					    "FDI train 1 done.\n");
5224 				break;
5225 			}
5226 			udelay(50);
5227 		}
5228 		if (retry < 5)
5229 			break;
5230 	}
5231 	if (i == 4)
5232 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5233 
5234 	/* Train 2 */
5235 	reg = FDI_TX_CTL(pipe);
5236 	temp = intel_de_read(dev_priv, reg);
5237 	temp &= ~FDI_LINK_TRAIN_NONE;
5238 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5239 	if (IS_GEN(dev_priv, 6)) {
5240 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5241 		/* SNB-B */
5242 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5243 	}
5244 	intel_de_write(dev_priv, reg, temp);
5245 
5246 	reg = FDI_RX_CTL(pipe);
5247 	temp = intel_de_read(dev_priv, reg);
5248 	if (HAS_PCH_CPT(dev_priv)) {
5249 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5250 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5251 	} else {
5252 		temp &= ~FDI_LINK_TRAIN_NONE;
5253 		temp |= FDI_LINK_TRAIN_PATTERN_2;
5254 	}
5255 	intel_de_write(dev_priv, reg, temp);
5256 
5257 	intel_de_posting_read(dev_priv, reg);
5258 	udelay(150);
5259 
5260 	for (i = 0; i < 4; i++) {
5261 		reg = FDI_TX_CTL(pipe);
5262 		temp = intel_de_read(dev_priv, reg);
5263 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5264 		temp |= snb_b_fdi_train_param[i];
5265 		intel_de_write(dev_priv, reg, temp);
5266 
5267 		intel_de_posting_read(dev_priv, reg);
5268 		udelay(500);
5269 
5270 		for (retry = 0; retry < 5; retry++) {
5271 			reg = FDI_RX_IIR(pipe);
5272 			temp = intel_de_read(dev_priv, reg);
5273 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5274 			if (temp & FDI_RX_SYMBOL_LOCK) {
5275 				intel_de_write(dev_priv, reg,
5276 					       temp | FDI_RX_SYMBOL_LOCK);
5277 				drm_dbg_kms(&dev_priv->drm,
5278 					    "FDI train 2 done.\n");
5279 				break;
5280 			}
5281 			udelay(50);
5282 		}
5283 		if (retry < 5)
5284 			break;
5285 	}
5286 	if (i == 4)
5287 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5288 
5289 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5290 }
5291 
5292 /* Manual link training for Ivy Bridge A0 parts */
5293 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5294 				      const struct intel_crtc_state *crtc_state)
5295 {
5296 	struct drm_device *dev = crtc->base.dev;
5297 	struct drm_i915_private *dev_priv = to_i915(dev);
5298 	enum pipe pipe = crtc->pipe;
5299 	i915_reg_t reg;
5300 	u32 temp, i, j;
5301 
5302 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5303 	   for train result */
5304 	reg = FDI_RX_IMR(pipe);
5305 	temp = intel_de_read(dev_priv, reg);
5306 	temp &= ~FDI_RX_SYMBOL_LOCK;
5307 	temp &= ~FDI_RX_BIT_LOCK;
5308 	intel_de_write(dev_priv, reg, temp);
5309 
5310 	intel_de_posting_read(dev_priv, reg);
5311 	udelay(150);
5312 
5313 	drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
5314 		    intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
5315 
5316 	/* Try each vswing and preemphasis setting twice before moving on */
5317 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5318 		/* disable first in case we need to retry */
5319 		reg = FDI_TX_CTL(pipe);
5320 		temp = intel_de_read(dev_priv, reg);
5321 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5322 		temp &= ~FDI_TX_ENABLE;
5323 		intel_de_write(dev_priv, reg, temp);
5324 
5325 		reg = FDI_RX_CTL(pipe);
5326 		temp = intel_de_read(dev_priv, reg);
5327 		temp &= ~FDI_LINK_TRAIN_AUTO;
5328 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5329 		temp &= ~FDI_RX_ENABLE;
5330 		intel_de_write(dev_priv, reg, temp);
5331 
5332 		/* enable CPU FDI TX and PCH FDI RX */
5333 		reg = FDI_TX_CTL(pipe);
5334 		temp = intel_de_read(dev_priv, reg);
5335 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
5336 		temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5337 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
5338 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5339 		temp |= snb_b_fdi_train_param[j/2];
5340 		temp |= FDI_COMPOSITE_SYNC;
5341 		intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5342 
5343 		intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5344 			       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5345 
5346 		reg = FDI_RX_CTL(pipe);
5347 		temp = intel_de_read(dev_priv, reg);
5348 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5349 		temp |= FDI_COMPOSITE_SYNC;
5350 		intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5351 
5352 		intel_de_posting_read(dev_priv, reg);
5353 		udelay(1); /* should be 0.5us */
5354 
5355 		for (i = 0; i < 4; i++) {
5356 			reg = FDI_RX_IIR(pipe);
5357 			temp = intel_de_read(dev_priv, reg);
5358 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5359 
5360 			if (temp & FDI_RX_BIT_LOCK ||
5361 			    (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5362 				intel_de_write(dev_priv, reg,
5363 					       temp | FDI_RX_BIT_LOCK);
5364 				drm_dbg_kms(&dev_priv->drm,
5365 					    "FDI train 1 done, level %i.\n",
5366 					    i);
5367 				break;
5368 			}
5369 			udelay(1); /* should be 0.5us */
5370 		}
5371 		if (i == 4) {
5372 			drm_dbg_kms(&dev_priv->drm,
5373 				    "FDI train 1 fail on vswing %d\n", j / 2);
5374 			continue;
5375 		}
5376 
5377 		/* Train 2 */
5378 		reg = FDI_TX_CTL(pipe);
5379 		temp = intel_de_read(dev_priv, reg);
5380 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5381 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
5382 		intel_de_write(dev_priv, reg, temp);
5383 
5384 		reg = FDI_RX_CTL(pipe);
5385 		temp = intel_de_read(dev_priv, reg);
5386 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5387 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5388 		intel_de_write(dev_priv, reg, temp);
5389 
5390 		intel_de_posting_read(dev_priv, reg);
5391 		udelay(2); /* should be 1.5us */
5392 
5393 		for (i = 0; i < 4; i++) {
5394 			reg = FDI_RX_IIR(pipe);
5395 			temp = intel_de_read(dev_priv, reg);
5396 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5397 
5398 			if (temp & FDI_RX_SYMBOL_LOCK ||
5399 			    (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5400 				intel_de_write(dev_priv, reg,
5401 					       temp | FDI_RX_SYMBOL_LOCK);
5402 				drm_dbg_kms(&dev_priv->drm,
5403 					    "FDI train 2 done, level %i.\n",
5404 					    i);
5405 				goto train_done;
5406 			}
5407 			udelay(2); /* should be 1.5us */
5408 		}
5409 		if (i == 4)
5410 			drm_dbg_kms(&dev_priv->drm,
5411 				    "FDI train 2 fail on vswing %d\n", j / 2);
5412 	}
5413 
5414 train_done:
5415 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5416 }
5417 
5418 static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
5419 {
5420 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
5421 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5422 	enum pipe pipe = intel_crtc->pipe;
5423 	i915_reg_t reg;
5424 	u32 temp;
5425 
5426 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5427 	reg = FDI_RX_CTL(pipe);
5428 	temp = intel_de_read(dev_priv, reg);
5429 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
5430 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5431 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5432 	intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5433 
5434 	intel_de_posting_read(dev_priv, reg);
5435 	udelay(200);
5436 
5437 	/* Switch from Rawclk to PCDclk */
5438 	temp = intel_de_read(dev_priv, reg);
5439 	intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5440 
5441 	intel_de_posting_read(dev_priv, reg);
5442 	udelay(200);
5443 
5444 	/* Enable CPU FDI TX PLL, always on for Ironlake */
5445 	reg = FDI_TX_CTL(pipe);
5446 	temp = intel_de_read(dev_priv, reg);
5447 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
5448 		intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5449 
5450 		intel_de_posting_read(dev_priv, reg);
5451 		udelay(100);
5452 	}
5453 }
5454 
5455 static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
5456 {
5457 	struct drm_device *dev = intel_crtc->base.dev;
5458 	struct drm_i915_private *dev_priv = to_i915(dev);
5459 	enum pipe pipe = intel_crtc->pipe;
5460 	i915_reg_t reg;
5461 	u32 temp;
5462 
5463 	/* Switch from PCDclk to Rawclk */
5464 	reg = FDI_RX_CTL(pipe);
5465 	temp = intel_de_read(dev_priv, reg);
5466 	intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
5467 
5468 	/* Disable CPU FDI TX PLL */
5469 	reg = FDI_TX_CTL(pipe);
5470 	temp = intel_de_read(dev_priv, reg);
5471 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
5472 
5473 	intel_de_posting_read(dev_priv, reg);
5474 	udelay(100);
5475 
5476 	reg = FDI_RX_CTL(pipe);
5477 	temp = intel_de_read(dev_priv, reg);
5478 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
5479 
5480 	/* Wait for the clocks to turn off. */
5481 	intel_de_posting_read(dev_priv, reg);
5482 	udelay(100);
5483 }
5484 
5485 static void ilk_fdi_disable(struct intel_crtc *crtc)
5486 {
5487 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5488 	enum pipe pipe = crtc->pipe;
5489 	i915_reg_t reg;
5490 	u32 temp;
5491 
5492 	/* disable CPU FDI tx and PCH FDI rx */
5493 	reg = FDI_TX_CTL(pipe);
5494 	temp = intel_de_read(dev_priv, reg);
5495 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5496 	intel_de_posting_read(dev_priv, reg);
5497 
5498 	reg = FDI_RX_CTL(pipe);
5499 	temp = intel_de_read(dev_priv, reg);
5500 	temp &= ~(0x7 << 16);
5501 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5502 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
5503 
5504 	intel_de_posting_read(dev_priv, reg);
5505 	udelay(100);
5506 
5507 	/* Ironlake workaround, disable clock pointer after downing FDI */
5508 	if (HAS_PCH_IBX(dev_priv))
5509 		intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5510 			       FDI_RX_PHASE_SYNC_POINTER_OVR);
5511 
5512 	/* still set train pattern 1 */
5513 	reg = FDI_TX_CTL(pipe);
5514 	temp = intel_de_read(dev_priv, reg);
5515 	temp &= ~FDI_LINK_TRAIN_NONE;
5516 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5517 	intel_de_write(dev_priv, reg, temp);
5518 
5519 	reg = FDI_RX_CTL(pipe);
5520 	temp = intel_de_read(dev_priv, reg);
5521 	if (HAS_PCH_CPT(dev_priv)) {
5522 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5523 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5524 	} else {
5525 		temp &= ~FDI_LINK_TRAIN_NONE;
5526 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5527 	}
5528 	/* BPC in FDI rx is consistent with that in PIPECONF */
5529 	temp &= ~(0x07 << 16);
5530 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5531 	intel_de_write(dev_priv, reg, temp);
5532 
5533 	intel_de_posting_read(dev_priv, reg);
5534 	udelay(100);
5535 }
5536 
5537 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5538 {
5539 	struct drm_crtc *crtc;
5540 	bool cleanup_done;
5541 
5542 	drm_for_each_crtc(crtc, &dev_priv->drm) {
5543 		struct drm_crtc_commit *commit;
5544 		spin_lock(&crtc->commit_lock);
5545 		commit = list_first_entry_or_null(&crtc->commit_list,
5546 						  struct drm_crtc_commit, commit_entry);
5547 		cleanup_done = commit ?
5548 			try_wait_for_completion(&commit->cleanup_done) : true;
5549 		spin_unlock(&crtc->commit_lock);
5550 
5551 		if (cleanup_done)
5552 			continue;
5553 
5554 		drm_crtc_wait_one_vblank(crtc);
5555 
5556 		return true;
5557 	}
5558 
5559 	return false;
5560 }
5561 
5562 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
5563 {
5564 	u32 temp;
5565 
5566 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
5567 
5568 	mutex_lock(&dev_priv->sb_lock);
5569 
5570 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5571 	temp |= SBI_SSCCTL_DISABLE;
5572 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5573 
5574 	mutex_unlock(&dev_priv->sb_lock);
5575 }
5576 
5577 /* Program iCLKIP clock to the desired frequency */
5578 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
5579 {
5580 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5581 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5582 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
5583 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
5584 	u32 temp;
5585 
5586 	lpt_disable_iclkip(dev_priv);
5587 
5588 	/* The iCLK virtual clock root frequency is in MHz,
5589 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
5590 	 * divisors, it is necessary to divide one by another, so we
5591 	 * convert the virtual clock precision to KHz here for higher
5592 	 * precision.
5593 	 */
5594 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
5595 		u32 iclk_virtual_root_freq = 172800 * 1000;
5596 		u32 iclk_pi_range = 64;
5597 		u32 desired_divisor;
5598 
5599 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5600 						    clock << auxdiv);
5601 		divsel = (desired_divisor / iclk_pi_range) - 2;
5602 		phaseinc = desired_divisor % iclk_pi_range;
5603 
5604 		/*
5605 		 * Near 20MHz is a corner case which is
5606 		 * out of range for the 7-bit divisor
5607 		 */
5608 		if (divsel <= 0x7f)
5609 			break;
5610 	}
5611 
5612 	/* This should not happen with any sane values */
5613 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5614 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5615 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5616 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5617 
5618 	drm_dbg_kms(&dev_priv->drm,
5619 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5620 		    clock, auxdiv, divsel, phasedir, phaseinc);
5621 
5622 	mutex_lock(&dev_priv->sb_lock);
5623 
5624 	/* Program SSCDIVINTPHASE6 */
5625 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5626 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5627 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5628 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5629 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5630 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5631 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5632 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5633 
5634 	/* Program SSCAUXDIV */
5635 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5636 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5637 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5638 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5639 
5640 	/* Enable modulator and associated divider */
5641 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5642 	temp &= ~SBI_SSCCTL_DISABLE;
5643 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5644 
5645 	mutex_unlock(&dev_priv->sb_lock);
5646 
5647 	/* Wait for initialization time */
5648 	udelay(24);
5649 
5650 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5651 }
5652 
5653 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5654 {
5655 	u32 divsel, phaseinc, auxdiv;
5656 	u32 iclk_virtual_root_freq = 172800 * 1000;
5657 	u32 iclk_pi_range = 64;
5658 	u32 desired_divisor;
5659 	u32 temp;
5660 
5661 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5662 		return 0;
5663 
5664 	mutex_lock(&dev_priv->sb_lock);
5665 
5666 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5667 	if (temp & SBI_SSCCTL_DISABLE) {
5668 		mutex_unlock(&dev_priv->sb_lock);
5669 		return 0;
5670 	}
5671 
5672 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5673 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5674 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5675 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5676 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5677 
5678 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5679 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5680 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5681 
5682 	mutex_unlock(&dev_priv->sb_lock);
5683 
5684 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5685 
5686 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5687 				 desired_divisor << auxdiv);
5688 }
5689 
5690 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5691 					   enum pipe pch_transcoder)
5692 {
5693 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5694 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5695 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5696 
5697 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5698 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5699 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5700 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5701 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5702 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
5703 
5704 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5705 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5706 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5707 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5708 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5709 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5710 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5711 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
5712 }
5713 
5714 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5715 {
5716 	u32 temp;
5717 
5718 	temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
5719 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5720 		return;
5721 
5722 	drm_WARN_ON(&dev_priv->drm,
5723 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5724 		    FDI_RX_ENABLE);
5725 	drm_WARN_ON(&dev_priv->drm,
5726 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5727 		    FDI_RX_ENABLE);
5728 
5729 	temp &= ~FDI_BC_BIFURCATION_SELECT;
5730 	if (enable)
5731 		temp |= FDI_BC_BIFURCATION_SELECT;
5732 
5733 	drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5734 		    enable ? "en" : "dis");
5735 	intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5736 	intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
5737 }
5738 
5739 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5740 {
5741 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5742 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5743 
5744 	switch (crtc->pipe) {
5745 	case PIPE_A:
5746 		break;
5747 	case PIPE_B:
5748 		if (crtc_state->fdi_lanes > 2)
5749 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
5750 		else
5751 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
5752 
5753 		break;
5754 	case PIPE_C:
5755 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
5756 
5757 		break;
5758 	default:
5759 		BUG();
5760 	}
5761 }
5762 
5763 /*
5764  * Finds the encoder associated with the given CRTC. This can only be
5765  * used when we know that the CRTC isn't feeding multiple encoders!
5766  */
5767 static struct intel_encoder *
5768 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5769 			   const struct intel_crtc_state *crtc_state)
5770 {
5771 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5772 	const struct drm_connector_state *connector_state;
5773 	const struct drm_connector *connector;
5774 	struct intel_encoder *encoder = NULL;
5775 	int num_encoders = 0;
5776 	int i;
5777 
5778 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5779 		if (connector_state->crtc != &crtc->base)
5780 			continue;
5781 
5782 		encoder = to_intel_encoder(connector_state->best_encoder);
5783 		num_encoders++;
5784 	}
5785 
5786 	drm_WARN(encoder->base.dev, num_encoders != 1,
5787 		 "%d encoders for pipe %c\n",
5788 		 num_encoders, pipe_name(crtc->pipe));
5789 
5790 	return encoder;
5791 }
5792 
5793 /*
5794  * Enable PCH resources required for PCH ports:
5795  *   - PCH PLLs
5796  *   - FDI training & RX/TX
5797  *   - update transcoder timings
5798  *   - DP transcoding bits
5799  *   - transcoder
5800  */
5801 static void ilk_pch_enable(const struct intel_atomic_state *state,
5802 			   const struct intel_crtc_state *crtc_state)
5803 {
5804 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5805 	struct drm_device *dev = crtc->base.dev;
5806 	struct drm_i915_private *dev_priv = to_i915(dev);
5807 	enum pipe pipe = crtc->pipe;
5808 	u32 temp;
5809 
5810 	assert_pch_transcoder_disabled(dev_priv, pipe);
5811 
5812 	if (IS_IVYBRIDGE(dev_priv))
5813 		ivb_update_fdi_bc_bifurcation(crtc_state);
5814 
5815 	/* Write the TU size bits before fdi link training, so that error
5816 	 * detection works. */
5817 	intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5818 		       intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5819 
5820 	/* For PCH output, training FDI link */
5821 	dev_priv->display.fdi_link_train(crtc, crtc_state);
5822 
5823 	/* We need to program the right clock selection before writing the pixel
5824 	 * mutliplier into the DPLL. */
5825 	if (HAS_PCH_CPT(dev_priv)) {
5826 		u32 sel;
5827 
5828 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5829 		temp |= TRANS_DPLL_ENABLE(pipe);
5830 		sel = TRANS_DPLLB_SEL(pipe);
5831 		if (crtc_state->shared_dpll ==
5832 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5833 			temp |= sel;
5834 		else
5835 			temp &= ~sel;
5836 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
5837 	}
5838 
5839 	/* XXX: pch pll's can be enabled any time before we enable the PCH
5840 	 * transcoder, and we actually should do this to not upset any PCH
5841 	 * transcoder that already use the clock when we share it.
5842 	 *
5843 	 * Note that enable_shared_dpll tries to do the right thing, but
5844 	 * get_shared_dpll unconditionally resets the pll - we need that to have
5845 	 * the right LVDS enable sequence. */
5846 	intel_enable_shared_dpll(crtc_state);
5847 
5848 	/* set transcoder timing, panel must allow it */
5849 	assert_panel_unlocked(dev_priv, pipe);
5850 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
5851 
5852 	intel_fdi_normal_train(crtc);
5853 
5854 	/* For PCH DP, enable TRANS_DP_CTL */
5855 	if (HAS_PCH_CPT(dev_priv) &&
5856 	    intel_crtc_has_dp_encoder(crtc_state)) {
5857 		const struct drm_display_mode *adjusted_mode =
5858 			&crtc_state->hw.adjusted_mode;
5859 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5860 		i915_reg_t reg = TRANS_DP_CTL(pipe);
5861 		enum port port;
5862 
5863 		temp = intel_de_read(dev_priv, reg);
5864 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
5865 			  TRANS_DP_SYNC_MASK |
5866 			  TRANS_DP_BPC_MASK);
5867 		temp |= TRANS_DP_OUTPUT_ENABLE;
5868 		temp |= bpc << 9; /* same format but at 11:9 */
5869 
5870 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5871 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5872 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5873 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5874 
5875 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5876 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
5877 		temp |= TRANS_DP_PORT_SEL(port);
5878 
5879 		intel_de_write(dev_priv, reg, temp);
5880 	}
5881 
5882 	ilk_enable_pch_transcoder(crtc_state);
5883 }
5884 
5885 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
5886 {
5887 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5888 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5889 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5890 
5891 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5892 
5893 	lpt_program_iclkip(crtc_state);
5894 
5895 	/* Set transcoder timing. */
5896 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
5897 
5898 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5899 }
5900 
5901 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5902 			       enum pipe pipe)
5903 {
5904 	i915_reg_t dslreg = PIPEDSL(pipe);
5905 	u32 temp;
5906 
5907 	temp = intel_de_read(dev_priv, dslreg);
5908 	udelay(500);
5909 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5910 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
5911 			drm_err(&dev_priv->drm,
5912 				"mode set failed: pipe %c stuck\n",
5913 				pipe_name(pipe));
5914 	}
5915 }
5916 
5917 /*
5918  * The hardware phase 0.0 refers to the center of the pixel.
5919  * We want to start from the top/left edge which is phase
5920  * -0.5. That matches how the hardware calculates the scaling
5921  * factors (from top-left of the first pixel to bottom-right
5922  * of the last pixel, as opposed to the pixel centers).
5923  *
5924  * For 4:2:0 subsampled chroma planes we obviously have to
5925  * adjust that so that the chroma sample position lands in
5926  * the right spot.
5927  *
5928  * Note that for packed YCbCr 4:2:2 formats there is no way to
5929  * control chroma siting. The hardware simply replicates the
5930  * chroma samples for both of the luma samples, and thus we don't
5931  * actually get the expected MPEG2 chroma siting convention :(
5932  * The same behaviour is observed on pre-SKL platforms as well.
5933  *
5934  * Theory behind the formula (note that we ignore sub-pixel
5935  * source coordinates):
5936  * s = source sample position
5937  * d = destination sample position
5938  *
5939  * Downscaling 4:1:
5940  * -0.5
5941  * | 0.0
5942  * | |     1.5 (initial phase)
5943  * | |     |
5944  * v v     v
5945  * | s | s | s | s |
5946  * |       d       |
5947  *
5948  * Upscaling 1:4:
5949  * -0.5
5950  * | -0.375 (initial phase)
5951  * | |     0.0
5952  * | |     |
5953  * v v     v
5954  * |       s       |
5955  * | d | d | d | d |
5956  */
5957 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
5958 {
5959 	int phase = -0x8000;
5960 	u16 trip = 0;
5961 
5962 	if (chroma_cosited)
5963 		phase += (sub - 1) * 0x8000 / sub;
5964 
5965 	phase += scale / (2 * sub);
5966 
5967 	/*
5968 	 * Hardware initial phase limited to [-0.5:1.5].
5969 	 * Since the max hardware scale factor is 3.0, we
5970 	 * should never actually excdeed 1.0 here.
5971 	 */
5972 	WARN_ON(phase < -0x8000 || phase > 0x18000);
5973 
5974 	if (phase < 0)
5975 		phase = 0x10000 + phase;
5976 	else
5977 		trip = PS_PHASE_TRIP;
5978 
5979 	return ((phase >> 2) & PS_PHASE_MASK) | trip;
5980 }
5981 
5982 #define SKL_MIN_SRC_W 8
5983 #define SKL_MAX_SRC_W 4096
5984 #define SKL_MIN_SRC_H 8
5985 #define SKL_MAX_SRC_H 4096
5986 #define SKL_MIN_DST_W 8
5987 #define SKL_MAX_DST_W 4096
5988 #define SKL_MIN_DST_H 8
5989 #define SKL_MAX_DST_H 4096
5990 #define ICL_MAX_SRC_W 5120
5991 #define ICL_MAX_SRC_H 4096
5992 #define ICL_MAX_DST_W 5120
5993 #define ICL_MAX_DST_H 4096
5994 #define SKL_MIN_YUV_420_SRC_W 16
5995 #define SKL_MIN_YUV_420_SRC_H 16
5996 
5997 static int
5998 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
5999 		  unsigned int scaler_user, int *scaler_id,
6000 		  int src_w, int src_h, int dst_w, int dst_h,
6001 		  const struct drm_format_info *format,
6002 		  u64 modifier, bool need_scaler)
6003 {
6004 	struct intel_crtc_scaler_state *scaler_state =
6005 		&crtc_state->scaler_state;
6006 	struct intel_crtc *intel_crtc =
6007 		to_intel_crtc(crtc_state->uapi.crtc);
6008 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
6009 	const struct drm_display_mode *adjusted_mode =
6010 		&crtc_state->hw.adjusted_mode;
6011 
6012 	/*
6013 	 * Src coordinates are already rotated by 270 degrees for
6014 	 * the 90/270 degree plane rotation cases (to match the
6015 	 * GTT mapping), hence no need to account for rotation here.
6016 	 */
6017 	if (src_w != dst_w || src_h != dst_h)
6018 		need_scaler = true;
6019 
6020 	/*
6021 	 * Scaling/fitting not supported in IF-ID mode in GEN9+
6022 	 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6023 	 * Once NV12 is enabled, handle it here while allocating scaler
6024 	 * for NV12.
6025 	 */
6026 	if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
6027 	    need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6028 		drm_dbg_kms(&dev_priv->drm,
6029 			    "Pipe/Plane scaling not supported with IF-ID mode\n");
6030 		return -EINVAL;
6031 	}
6032 
6033 	/*
6034 	 * if plane is being disabled or scaler is no more required or force detach
6035 	 *  - free scaler binded to this plane/crtc
6036 	 *  - in order to do this, update crtc->scaler_usage
6037 	 *
6038 	 * Here scaler state in crtc_state is set free so that
6039 	 * scaler can be assigned to other user. Actual register
6040 	 * update to free the scaler is done in plane/panel-fit programming.
6041 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6042 	 */
6043 	if (force_detach || !need_scaler) {
6044 		if (*scaler_id >= 0) {
6045 			scaler_state->scaler_users &= ~(1 << scaler_user);
6046 			scaler_state->scalers[*scaler_id].in_use = 0;
6047 
6048 			drm_dbg_kms(&dev_priv->drm,
6049 				    "scaler_user index %u.%u: "
6050 				    "Staged freeing scaler id %d scaler_users = 0x%x\n",
6051 				    intel_crtc->pipe, scaler_user, *scaler_id,
6052 				    scaler_state->scaler_users);
6053 			*scaler_id = -1;
6054 		}
6055 		return 0;
6056 	}
6057 
6058 	if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
6059 	    (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
6060 		drm_dbg_kms(&dev_priv->drm,
6061 			    "Planar YUV: src dimensions not met\n");
6062 		return -EINVAL;
6063 	}
6064 
6065 	/* range checks */
6066 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
6067 	    dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
6068 	    (INTEL_GEN(dev_priv) >= 11 &&
6069 	     (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6070 	      dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
6071 	    (INTEL_GEN(dev_priv) < 11 &&
6072 	     (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6073 	      dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H)))	{
6074 		drm_dbg_kms(&dev_priv->drm,
6075 			    "scaler_user index %u.%u: src %ux%u dst %ux%u "
6076 			    "size is out of scaler range\n",
6077 			    intel_crtc->pipe, scaler_user, src_w, src_h,
6078 			    dst_w, dst_h);
6079 		return -EINVAL;
6080 	}
6081 
6082 	/* mark this plane as a scaler user in crtc_state */
6083 	scaler_state->scaler_users |= (1 << scaler_user);
6084 	drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6085 		    "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6086 		    intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6087 		    scaler_state->scaler_users);
6088 
6089 	return 0;
6090 }
6091 
6092 static int skl_update_scaler_crtc(struct intel_crtc_state *crtc_state)
6093 {
6094 	const struct drm_display_mode *adjusted_mode =
6095 		&crtc_state->hw.adjusted_mode;
6096 	int width, height;
6097 
6098 	if (crtc_state->pch_pfit.enabled) {
6099 		width = drm_rect_width(&crtc_state->pch_pfit.dst);
6100 		height = drm_rect_height(&crtc_state->pch_pfit.dst);
6101 	} else {
6102 		width = adjusted_mode->crtc_hdisplay;
6103 		height = adjusted_mode->crtc_vdisplay;
6104 	}
6105 
6106 	return skl_update_scaler(crtc_state, !crtc_state->hw.active,
6107 				 SKL_CRTC_INDEX,
6108 				 &crtc_state->scaler_state.scaler_id,
6109 				 crtc_state->pipe_src_w, crtc_state->pipe_src_h,
6110 				 width, height, NULL, 0,
6111 				 crtc_state->pch_pfit.enabled);
6112 }
6113 
6114 /**
6115  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
6116  * @crtc_state: crtc's scaler state
6117  * @plane_state: atomic plane state to update
6118  *
6119  * Return
6120  *     0 - scaler_usage updated successfully
6121  *    error - requested scaling cannot be supported or other error condition
6122  */
6123 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6124 				   struct intel_plane_state *plane_state)
6125 {
6126 	struct intel_plane *intel_plane =
6127 		to_intel_plane(plane_state->uapi.plane);
6128 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
6129 	struct drm_framebuffer *fb = plane_state->hw.fb;
6130 	int ret;
6131 	bool force_detach = !fb || !plane_state->uapi.visible;
6132 	bool need_scaler = false;
6133 
6134 	/* Pre-gen11 and SDR planes always need a scaler for planar formats. */
6135 	if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
6136 	    fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
6137 		need_scaler = true;
6138 
6139 	ret = skl_update_scaler(crtc_state, force_detach,
6140 				drm_plane_index(&intel_plane->base),
6141 				&plane_state->scaler_id,
6142 				drm_rect_width(&plane_state->uapi.src) >> 16,
6143 				drm_rect_height(&plane_state->uapi.src) >> 16,
6144 				drm_rect_width(&plane_state->uapi.dst),
6145 				drm_rect_height(&plane_state->uapi.dst),
6146 				fb ? fb->format : NULL,
6147 				fb ? fb->modifier : 0,
6148 				need_scaler);
6149 
6150 	if (ret || plane_state->scaler_id < 0)
6151 		return ret;
6152 
6153 	/* check colorkey */
6154 	if (plane_state->ckey.flags) {
6155 		drm_dbg_kms(&dev_priv->drm,
6156 			    "[PLANE:%d:%s] scaling with color key not allowed",
6157 			    intel_plane->base.base.id,
6158 			    intel_plane->base.name);
6159 		return -EINVAL;
6160 	}
6161 
6162 	/* Check src format */
6163 	switch (fb->format->format) {
6164 	case DRM_FORMAT_RGB565:
6165 	case DRM_FORMAT_XBGR8888:
6166 	case DRM_FORMAT_XRGB8888:
6167 	case DRM_FORMAT_ABGR8888:
6168 	case DRM_FORMAT_ARGB8888:
6169 	case DRM_FORMAT_XRGB2101010:
6170 	case DRM_FORMAT_XBGR2101010:
6171 	case DRM_FORMAT_ARGB2101010:
6172 	case DRM_FORMAT_ABGR2101010:
6173 	case DRM_FORMAT_YUYV:
6174 	case DRM_FORMAT_YVYU:
6175 	case DRM_FORMAT_UYVY:
6176 	case DRM_FORMAT_VYUY:
6177 	case DRM_FORMAT_NV12:
6178 	case DRM_FORMAT_XYUV8888:
6179 	case DRM_FORMAT_P010:
6180 	case DRM_FORMAT_P012:
6181 	case DRM_FORMAT_P016:
6182 	case DRM_FORMAT_Y210:
6183 	case DRM_FORMAT_Y212:
6184 	case DRM_FORMAT_Y216:
6185 	case DRM_FORMAT_XVYU2101010:
6186 	case DRM_FORMAT_XVYU12_16161616:
6187 	case DRM_FORMAT_XVYU16161616:
6188 		break;
6189 	case DRM_FORMAT_XBGR16161616F:
6190 	case DRM_FORMAT_ABGR16161616F:
6191 	case DRM_FORMAT_XRGB16161616F:
6192 	case DRM_FORMAT_ARGB16161616F:
6193 		if (INTEL_GEN(dev_priv) >= 11)
6194 			break;
6195 		/* fall through */
6196 	default:
6197 		drm_dbg_kms(&dev_priv->drm,
6198 			    "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6199 			    intel_plane->base.base.id, intel_plane->base.name,
6200 			    fb->base.id, fb->format->format);
6201 		return -EINVAL;
6202 	}
6203 
6204 	return 0;
6205 }
6206 
6207 void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
6208 {
6209 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6210 	int i;
6211 
6212 	for (i = 0; i < crtc->num_scalers; i++)
6213 		skl_detach_scaler(crtc, i);
6214 }
6215 
6216 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
6217 {
6218 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6219 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6220 	const struct intel_crtc_scaler_state *scaler_state =
6221 		&crtc_state->scaler_state;
6222 	struct drm_rect src = {
6223 		.x2 = crtc_state->pipe_src_w << 16,
6224 		.y2 = crtc_state->pipe_src_h << 16,
6225 	};
6226 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6227 	u16 uv_rgb_hphase, uv_rgb_vphase;
6228 	enum pipe pipe = crtc->pipe;
6229 	int width = drm_rect_width(dst);
6230 	int height = drm_rect_height(dst);
6231 	int x = dst->x1;
6232 	int y = dst->y1;
6233 	int hscale, vscale;
6234 	unsigned long irqflags;
6235 	int id;
6236 
6237 	if (!crtc_state->pch_pfit.enabled)
6238 		return;
6239 
6240 	if (drm_WARN_ON(&dev_priv->drm,
6241 			crtc_state->scaler_state.scaler_id < 0))
6242 		return;
6243 
6244 	hscale = drm_rect_calc_hscale(&src, dst, 0, INT_MAX);
6245 	vscale = drm_rect_calc_vscale(&src, dst, 0, INT_MAX);
6246 
6247 	uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6248 	uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
6249 
6250 	id = scaler_state->scaler_id;
6251 
6252 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6253 
6254 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
6255 			  PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
6256 	intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6257 			  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6258 	intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6259 			  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6260 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
6261 			  x << 16 | y);
6262 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
6263 			  width << 16 | height);
6264 
6265 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6266 }
6267 
6268 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
6269 {
6270 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6271 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6272 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6273 	enum pipe pipe = crtc->pipe;
6274 	int width = drm_rect_width(dst);
6275 	int height = drm_rect_height(dst);
6276 	int x = dst->x1;
6277 	int y = dst->y1;
6278 
6279 	if (!crtc_state->pch_pfit.enabled)
6280 		return;
6281 
6282 	/* Force use of hard-coded filter coefficients
6283 	 * as some pre-programmed values are broken,
6284 	 * e.g. x201.
6285 	 */
6286 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6287 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6288 			       PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6289 	else
6290 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6291 			       PF_FILTER_MED_3x3);
6292 	intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
6293 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
6294 }
6295 
6296 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
6297 {
6298 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6299 	struct drm_device *dev = crtc->base.dev;
6300 	struct drm_i915_private *dev_priv = to_i915(dev);
6301 
6302 	if (!crtc_state->ips_enabled)
6303 		return;
6304 
6305 	/*
6306 	 * We can only enable IPS after we enable a plane and wait for a vblank
6307 	 * This function is called from post_plane_update, which is run after
6308 	 * a vblank wait.
6309 	 */
6310 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
6311 
6312 	if (IS_BROADWELL(dev_priv)) {
6313 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6314 							 IPS_ENABLE | IPS_PCODE_CONTROL));
6315 		/* Quoting Art Runyan: "its not safe to expect any particular
6316 		 * value in IPS_CTL bit 31 after enabling IPS through the
6317 		 * mailbox." Moreover, the mailbox may return a bogus state,
6318 		 * so we need to just enable it and continue on.
6319 		 */
6320 	} else {
6321 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
6322 		/* The bit only becomes 1 in the next vblank, so this wait here
6323 		 * is essentially intel_wait_for_vblank. If we don't have this
6324 		 * and don't wait for vblanks until the end of crtc_enable, then
6325 		 * the HW state readout code will complain that the expected
6326 		 * IPS_CTL value is not the one we read. */
6327 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
6328 			drm_err(&dev_priv->drm,
6329 				"Timed out waiting for IPS enable\n");
6330 	}
6331 }
6332 
6333 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
6334 {
6335 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6336 	struct drm_device *dev = crtc->base.dev;
6337 	struct drm_i915_private *dev_priv = to_i915(dev);
6338 
6339 	if (!crtc_state->ips_enabled)
6340 		return;
6341 
6342 	if (IS_BROADWELL(dev_priv)) {
6343 		drm_WARN_ON(dev,
6344 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
6345 		/*
6346 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
6347 		 * 42ms timeout value leads to occasional timeouts so use 100ms
6348 		 * instead.
6349 		 */
6350 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
6351 			drm_err(&dev_priv->drm,
6352 				"Timed out waiting for IPS disable\n");
6353 	} else {
6354 		intel_de_write(dev_priv, IPS_CTL, 0);
6355 		intel_de_posting_read(dev_priv, IPS_CTL);
6356 	}
6357 
6358 	/* We need to wait for a vblank before we can disable the plane. */
6359 	intel_wait_for_vblank(dev_priv, crtc->pipe);
6360 }
6361 
6362 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
6363 {
6364 	if (intel_crtc->overlay)
6365 		(void) intel_overlay_switch_off(intel_crtc->overlay);
6366 
6367 	/* Let userspace switch the overlay on again. In most cases userspace
6368 	 * has to recompute where to put it anyway.
6369 	 */
6370 }
6371 
6372 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6373 				       const struct intel_crtc_state *new_crtc_state)
6374 {
6375 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6376 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6377 
6378 	if (!old_crtc_state->ips_enabled)
6379 		return false;
6380 
6381 	if (needs_modeset(new_crtc_state))
6382 		return true;
6383 
6384 	/*
6385 	 * Workaround : Do not read or write the pipe palette/gamma data while
6386 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6387 	 *
6388 	 * Disable IPS before we program the LUT.
6389 	 */
6390 	if (IS_HASWELL(dev_priv) &&
6391 	    (new_crtc_state->uapi.color_mgmt_changed ||
6392 	     new_crtc_state->update_pipe) &&
6393 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6394 		return true;
6395 
6396 	return !new_crtc_state->ips_enabled;
6397 }
6398 
6399 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6400 				       const struct intel_crtc_state *new_crtc_state)
6401 {
6402 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6403 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6404 
6405 	if (!new_crtc_state->ips_enabled)
6406 		return false;
6407 
6408 	if (needs_modeset(new_crtc_state))
6409 		return true;
6410 
6411 	/*
6412 	 * Workaround : Do not read or write the pipe palette/gamma data while
6413 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6414 	 *
6415 	 * Re-enable IPS after the LUT has been programmed.
6416 	 */
6417 	if (IS_HASWELL(dev_priv) &&
6418 	    (new_crtc_state->uapi.color_mgmt_changed ||
6419 	     new_crtc_state->update_pipe) &&
6420 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6421 		return true;
6422 
6423 	/*
6424 	 * We can't read out IPS on broadwell, assume the worst and
6425 	 * forcibly enable IPS on the first fastset.
6426 	 */
6427 	if (new_crtc_state->update_pipe &&
6428 	    old_crtc_state->hw.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
6429 		return true;
6430 
6431 	return !old_crtc_state->ips_enabled;
6432 }
6433 
6434 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
6435 {
6436 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6437 
6438 	if (!crtc_state->nv12_planes)
6439 		return false;
6440 
6441 	/* WA Display #0827: Gen9:all */
6442 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
6443 		return true;
6444 
6445 	return false;
6446 }
6447 
6448 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
6449 {
6450 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6451 
6452 	/* Wa_2006604312:icl,ehl */
6453 	if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
6454 		return true;
6455 
6456 	return false;
6457 }
6458 
6459 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6460 			    const struct intel_crtc_state *new_crtc_state)
6461 {
6462 	return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6463 		new_crtc_state->active_planes;
6464 }
6465 
6466 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6467 			     const struct intel_crtc_state *new_crtc_state)
6468 {
6469 	return old_crtc_state->active_planes &&
6470 		(!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6471 }
6472 
6473 static void intel_post_plane_update(struct intel_atomic_state *state,
6474 				    struct intel_crtc *crtc)
6475 {
6476 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6477 	const struct intel_crtc_state *old_crtc_state =
6478 		intel_atomic_get_old_crtc_state(state, crtc);
6479 	const struct intel_crtc_state *new_crtc_state =
6480 		intel_atomic_get_new_crtc_state(state, crtc);
6481 	enum pipe pipe = crtc->pipe;
6482 
6483 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
6484 
6485 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
6486 		intel_update_watermarks(crtc);
6487 
6488 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6489 		hsw_enable_ips(new_crtc_state);
6490 
6491 	intel_fbc_post_update(state, crtc);
6492 
6493 	if (needs_nv12_wa(old_crtc_state) &&
6494 	    !needs_nv12_wa(new_crtc_state))
6495 		skl_wa_827(dev_priv, pipe, false);
6496 
6497 	if (needs_scalerclk_wa(old_crtc_state) &&
6498 	    !needs_scalerclk_wa(new_crtc_state))
6499 		icl_wa_scalerclkgating(dev_priv, pipe, false);
6500 }
6501 
6502 static void intel_pre_plane_update(struct intel_atomic_state *state,
6503 				   struct intel_crtc *crtc)
6504 {
6505 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6506 	const struct intel_crtc_state *old_crtc_state =
6507 		intel_atomic_get_old_crtc_state(state, crtc);
6508 	const struct intel_crtc_state *new_crtc_state =
6509 		intel_atomic_get_new_crtc_state(state, crtc);
6510 	enum pipe pipe = crtc->pipe;
6511 
6512 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
6513 		hsw_disable_ips(old_crtc_state);
6514 
6515 	if (intel_fbc_pre_update(state, crtc))
6516 		intel_wait_for_vblank(dev_priv, pipe);
6517 
6518 	/* Display WA 827 */
6519 	if (!needs_nv12_wa(old_crtc_state) &&
6520 	    needs_nv12_wa(new_crtc_state))
6521 		skl_wa_827(dev_priv, pipe, true);
6522 
6523 	/* Wa_2006604312:icl,ehl */
6524 	if (!needs_scalerclk_wa(old_crtc_state) &&
6525 	    needs_scalerclk_wa(new_crtc_state))
6526 		icl_wa_scalerclkgating(dev_priv, pipe, true);
6527 
6528 	/*
6529 	 * Vblank time updates from the shadow to live plane control register
6530 	 * are blocked if the memory self-refresh mode is active at that
6531 	 * moment. So to make sure the plane gets truly disabled, disable
6532 	 * first the self-refresh mode. The self-refresh enable bit in turn
6533 	 * will be checked/applied by the HW only at the next frame start
6534 	 * event which is after the vblank start event, so we need to have a
6535 	 * wait-for-vblank between disabling the plane and the pipe.
6536 	 */
6537 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
6538 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
6539 		intel_wait_for_vblank(dev_priv, pipe);
6540 
6541 	/*
6542 	 * IVB workaround: must disable low power watermarks for at least
6543 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
6544 	 * when scaling is disabled.
6545 	 *
6546 	 * WaCxSRDisabledForSpriteScaling:ivb
6547 	 */
6548 	if (old_crtc_state->hw.active &&
6549 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
6550 		intel_wait_for_vblank(dev_priv, pipe);
6551 
6552 	/*
6553 	 * If we're doing a modeset we don't need to do any
6554 	 * pre-vblank watermark programming here.
6555 	 */
6556 	if (!needs_modeset(new_crtc_state)) {
6557 		/*
6558 		 * For platforms that support atomic watermarks, program the
6559 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6560 		 * will be the intermediate values that are safe for both pre- and
6561 		 * post- vblank; when vblank happens, the 'active' values will be set
6562 		 * to the final 'target' values and we'll do this again to get the
6563 		 * optimal watermarks.  For gen9+ platforms, the values we program here
6564 		 * will be the final target values which will get automatically latched
6565 		 * at vblank time; no further programming will be necessary.
6566 		 *
6567 		 * If a platform hasn't been transitioned to atomic watermarks yet,
6568 		 * we'll continue to update watermarks the old way, if flags tell
6569 		 * us to.
6570 		 */
6571 		if (dev_priv->display.initial_watermarks)
6572 			dev_priv->display.initial_watermarks(state, crtc);
6573 		else if (new_crtc_state->update_wm_pre)
6574 			intel_update_watermarks(crtc);
6575 	}
6576 
6577 	/*
6578 	 * Gen2 reports pipe underruns whenever all planes are disabled.
6579 	 * So disable underrun reporting before all the planes get disabled.
6580 	 *
6581 	 * We do this after .initial_watermarks() so that we have a
6582 	 * chance of catching underruns with the intermediate watermarks
6583 	 * vs. the old plane configuration.
6584 	 */
6585 	if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6586 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6587 }
6588 
6589 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6590 				      struct intel_crtc *crtc)
6591 {
6592 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6593 	const struct intel_crtc_state *new_crtc_state =
6594 		intel_atomic_get_new_crtc_state(state, crtc);
6595 	unsigned int update_mask = new_crtc_state->update_planes;
6596 	const struct intel_plane_state *old_plane_state;
6597 	struct intel_plane *plane;
6598 	unsigned fb_bits = 0;
6599 	int i;
6600 
6601 	intel_crtc_dpms_overlay_disable(crtc);
6602 
6603 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6604 		if (crtc->pipe != plane->pipe ||
6605 		    !(update_mask & BIT(plane->id)))
6606 			continue;
6607 
6608 		intel_disable_plane(plane, new_crtc_state);
6609 
6610 		if (old_plane_state->uapi.visible)
6611 			fb_bits |= plane->frontbuffer_bit;
6612 	}
6613 
6614 	intel_frontbuffer_flip(dev_priv, fb_bits);
6615 }
6616 
6617 /*
6618  * intel_connector_primary_encoder - get the primary encoder for a connector
6619  * @connector: connector for which to return the encoder
6620  *
6621  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6622  * all connectors to their encoder, except for DP-MST connectors which have
6623  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6624  * pointed to by as many DP-MST connectors as there are pipes.
6625  */
6626 static struct intel_encoder *
6627 intel_connector_primary_encoder(struct intel_connector *connector)
6628 {
6629 	struct intel_encoder *encoder;
6630 
6631 	if (connector->mst_port)
6632 		return &dp_to_dig_port(connector->mst_port)->base;
6633 
6634 	encoder = intel_attached_encoder(connector);
6635 	drm_WARN_ON(connector->base.dev, !encoder);
6636 
6637 	return encoder;
6638 }
6639 
6640 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6641 {
6642 	struct drm_connector_state *new_conn_state;
6643 	struct drm_connector *connector;
6644 	int i;
6645 
6646 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6647 					i) {
6648 		struct intel_connector *intel_connector;
6649 		struct intel_encoder *encoder;
6650 		struct intel_crtc *crtc;
6651 
6652 		if (!intel_connector_needs_modeset(state, connector))
6653 			continue;
6654 
6655 		intel_connector = to_intel_connector(connector);
6656 		encoder = intel_connector_primary_encoder(intel_connector);
6657 		if (!encoder->update_prepare)
6658 			continue;
6659 
6660 		crtc = new_conn_state->crtc ?
6661 			to_intel_crtc(new_conn_state->crtc) : NULL;
6662 		encoder->update_prepare(state, encoder, crtc);
6663 	}
6664 }
6665 
6666 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6667 {
6668 	struct drm_connector_state *new_conn_state;
6669 	struct drm_connector *connector;
6670 	int i;
6671 
6672 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6673 					i) {
6674 		struct intel_connector *intel_connector;
6675 		struct intel_encoder *encoder;
6676 		struct intel_crtc *crtc;
6677 
6678 		if (!intel_connector_needs_modeset(state, connector))
6679 			continue;
6680 
6681 		intel_connector = to_intel_connector(connector);
6682 		encoder = intel_connector_primary_encoder(intel_connector);
6683 		if (!encoder->update_complete)
6684 			continue;
6685 
6686 		crtc = new_conn_state->crtc ?
6687 			to_intel_crtc(new_conn_state->crtc) : NULL;
6688 		encoder->update_complete(state, encoder, crtc);
6689 	}
6690 }
6691 
6692 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6693 					  struct intel_crtc *crtc)
6694 {
6695 	const struct intel_crtc_state *crtc_state =
6696 		intel_atomic_get_new_crtc_state(state, crtc);
6697 	const struct drm_connector_state *conn_state;
6698 	struct drm_connector *conn;
6699 	int i;
6700 
6701 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6702 		struct intel_encoder *encoder =
6703 			to_intel_encoder(conn_state->best_encoder);
6704 
6705 		if (conn_state->crtc != &crtc->base)
6706 			continue;
6707 
6708 		if (encoder->pre_pll_enable)
6709 			encoder->pre_pll_enable(state, encoder,
6710 						crtc_state, conn_state);
6711 	}
6712 }
6713 
6714 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6715 				      struct intel_crtc *crtc)
6716 {
6717 	const struct intel_crtc_state *crtc_state =
6718 		intel_atomic_get_new_crtc_state(state, crtc);
6719 	const struct drm_connector_state *conn_state;
6720 	struct drm_connector *conn;
6721 	int i;
6722 
6723 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6724 		struct intel_encoder *encoder =
6725 			to_intel_encoder(conn_state->best_encoder);
6726 
6727 		if (conn_state->crtc != &crtc->base)
6728 			continue;
6729 
6730 		if (encoder->pre_enable)
6731 			encoder->pre_enable(state, encoder,
6732 					    crtc_state, conn_state);
6733 	}
6734 }
6735 
6736 static void intel_encoders_enable(struct intel_atomic_state *state,
6737 				  struct intel_crtc *crtc)
6738 {
6739 	const struct intel_crtc_state *crtc_state =
6740 		intel_atomic_get_new_crtc_state(state, crtc);
6741 	const struct drm_connector_state *conn_state;
6742 	struct drm_connector *conn;
6743 	int i;
6744 
6745 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6746 		struct intel_encoder *encoder =
6747 			to_intel_encoder(conn_state->best_encoder);
6748 
6749 		if (conn_state->crtc != &crtc->base)
6750 			continue;
6751 
6752 		if (encoder->enable)
6753 			encoder->enable(state, encoder,
6754 					crtc_state, conn_state);
6755 		intel_opregion_notify_encoder(encoder, true);
6756 	}
6757 }
6758 
6759 static void intel_encoders_disable(struct intel_atomic_state *state,
6760 				   struct intel_crtc *crtc)
6761 {
6762 	const struct intel_crtc_state *old_crtc_state =
6763 		intel_atomic_get_old_crtc_state(state, crtc);
6764 	const struct drm_connector_state *old_conn_state;
6765 	struct drm_connector *conn;
6766 	int i;
6767 
6768 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6769 		struct intel_encoder *encoder =
6770 			to_intel_encoder(old_conn_state->best_encoder);
6771 
6772 		if (old_conn_state->crtc != &crtc->base)
6773 			continue;
6774 
6775 		intel_opregion_notify_encoder(encoder, false);
6776 		if (encoder->disable)
6777 			encoder->disable(state, encoder,
6778 					 old_crtc_state, old_conn_state);
6779 	}
6780 }
6781 
6782 static void intel_encoders_post_disable(struct intel_atomic_state *state,
6783 					struct intel_crtc *crtc)
6784 {
6785 	const struct intel_crtc_state *old_crtc_state =
6786 		intel_atomic_get_old_crtc_state(state, crtc);
6787 	const struct drm_connector_state *old_conn_state;
6788 	struct drm_connector *conn;
6789 	int i;
6790 
6791 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6792 		struct intel_encoder *encoder =
6793 			to_intel_encoder(old_conn_state->best_encoder);
6794 
6795 		if (old_conn_state->crtc != &crtc->base)
6796 			continue;
6797 
6798 		if (encoder->post_disable)
6799 			encoder->post_disable(state, encoder,
6800 					      old_crtc_state, old_conn_state);
6801 	}
6802 }
6803 
6804 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6805 					    struct intel_crtc *crtc)
6806 {
6807 	const struct intel_crtc_state *old_crtc_state =
6808 		intel_atomic_get_old_crtc_state(state, crtc);
6809 	const struct drm_connector_state *old_conn_state;
6810 	struct drm_connector *conn;
6811 	int i;
6812 
6813 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6814 		struct intel_encoder *encoder =
6815 			to_intel_encoder(old_conn_state->best_encoder);
6816 
6817 		if (old_conn_state->crtc != &crtc->base)
6818 			continue;
6819 
6820 		if (encoder->post_pll_disable)
6821 			encoder->post_pll_disable(state, encoder,
6822 						  old_crtc_state, old_conn_state);
6823 	}
6824 }
6825 
6826 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6827 				       struct intel_crtc *crtc)
6828 {
6829 	const struct intel_crtc_state *crtc_state =
6830 		intel_atomic_get_new_crtc_state(state, crtc);
6831 	const struct drm_connector_state *conn_state;
6832 	struct drm_connector *conn;
6833 	int i;
6834 
6835 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6836 		struct intel_encoder *encoder =
6837 			to_intel_encoder(conn_state->best_encoder);
6838 
6839 		if (conn_state->crtc != &crtc->base)
6840 			continue;
6841 
6842 		if (encoder->update_pipe)
6843 			encoder->update_pipe(state, encoder,
6844 					     crtc_state, conn_state);
6845 	}
6846 }
6847 
6848 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6849 {
6850 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6851 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6852 
6853 	plane->disable_plane(plane, crtc_state);
6854 }
6855 
6856 static void ilk_crtc_enable(struct intel_atomic_state *state,
6857 			    struct intel_crtc *crtc)
6858 {
6859 	const struct intel_crtc_state *new_crtc_state =
6860 		intel_atomic_get_new_crtc_state(state, crtc);
6861 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6862 	enum pipe pipe = crtc->pipe;
6863 
6864 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
6865 		return;
6866 
6867 	/*
6868 	 * Sometimes spurious CPU pipe underruns happen during FDI
6869 	 * training, at least with VGA+HDMI cloning. Suppress them.
6870 	 *
6871 	 * On ILK we get an occasional spurious CPU pipe underruns
6872 	 * between eDP port A enable and vdd enable. Also PCH port
6873 	 * enable seems to result in the occasional CPU pipe underrun.
6874 	 *
6875 	 * Spurious PCH underruns also occur during PCH enabling.
6876 	 */
6877 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6878 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
6879 
6880 	if (new_crtc_state->has_pch_encoder)
6881 		intel_prepare_shared_dpll(new_crtc_state);
6882 
6883 	if (intel_crtc_has_dp_encoder(new_crtc_state))
6884 		intel_dp_set_m_n(new_crtc_state, M1_N1);
6885 
6886 	intel_set_pipe_timings(new_crtc_state);
6887 	intel_set_pipe_src_size(new_crtc_state);
6888 
6889 	if (new_crtc_state->has_pch_encoder)
6890 		intel_cpu_transcoder_set_m_n(new_crtc_state,
6891 					     &new_crtc_state->fdi_m_n, NULL);
6892 
6893 	ilk_set_pipeconf(new_crtc_state);
6894 
6895 	crtc->active = true;
6896 
6897 	intel_encoders_pre_enable(state, crtc);
6898 
6899 	if (new_crtc_state->has_pch_encoder) {
6900 		/* Note: FDI PLL enabling _must_ be done before we enable the
6901 		 * cpu pipes, hence this is separate from all the other fdi/pch
6902 		 * enabling. */
6903 		ilk_fdi_pll_enable(new_crtc_state);
6904 	} else {
6905 		assert_fdi_tx_disabled(dev_priv, pipe);
6906 		assert_fdi_rx_disabled(dev_priv, pipe);
6907 	}
6908 
6909 	ilk_pfit_enable(new_crtc_state);
6910 
6911 	/*
6912 	 * On ILK+ LUT must be loaded before the pipe is running but with
6913 	 * clocks enabled
6914 	 */
6915 	intel_color_load_luts(new_crtc_state);
6916 	intel_color_commit(new_crtc_state);
6917 	/* update DSPCNTR to configure gamma for pipe bottom color */
6918 	intel_disable_primary_plane(new_crtc_state);
6919 
6920 	if (dev_priv->display.initial_watermarks)
6921 		dev_priv->display.initial_watermarks(state, crtc);
6922 	intel_enable_pipe(new_crtc_state);
6923 
6924 	if (new_crtc_state->has_pch_encoder)
6925 		ilk_pch_enable(state, new_crtc_state);
6926 
6927 	intel_crtc_vblank_on(new_crtc_state);
6928 
6929 	intel_encoders_enable(state, crtc);
6930 
6931 	if (HAS_PCH_CPT(dev_priv))
6932 		cpt_verify_modeset(dev_priv, pipe);
6933 
6934 	/*
6935 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
6936 	 * And a second vblank wait is needed at least on ILK with
6937 	 * some interlaced HDMI modes. Let's do the double wait always
6938 	 * in case there are more corner cases we don't know about.
6939 	 */
6940 	if (new_crtc_state->has_pch_encoder) {
6941 		intel_wait_for_vblank(dev_priv, pipe);
6942 		intel_wait_for_vblank(dev_priv, pipe);
6943 	}
6944 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6945 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6946 }
6947 
6948 /* IPS only exists on ULT machines and is tied to pipe A. */
6949 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
6950 {
6951 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
6952 }
6953 
6954 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
6955 					    enum pipe pipe, bool apply)
6956 {
6957 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
6958 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
6959 
6960 	if (apply)
6961 		val |= mask;
6962 	else
6963 		val &= ~mask;
6964 
6965 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
6966 }
6967 
6968 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
6969 {
6970 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6971 	enum pipe pipe = crtc->pipe;
6972 	u32 val;
6973 
6974 	val = MBUS_DBOX_A_CREDIT(2);
6975 
6976 	if (INTEL_GEN(dev_priv) >= 12) {
6977 		val |= MBUS_DBOX_BW_CREDIT(2);
6978 		val |= MBUS_DBOX_B_CREDIT(12);
6979 	} else {
6980 		val |= MBUS_DBOX_BW_CREDIT(1);
6981 		val |= MBUS_DBOX_B_CREDIT(8);
6982 	}
6983 
6984 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
6985 }
6986 
6987 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
6988 {
6989 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6990 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6991 
6992 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
6993 		       HSW_LINETIME(crtc_state->linetime) |
6994 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
6995 }
6996 
6997 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
6998 {
6999 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7000 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7001 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7002 	u32 val;
7003 
7004 	val = intel_de_read(dev_priv, reg);
7005 	val &= ~HSW_FRAME_START_DELAY_MASK;
7006 	val |= HSW_FRAME_START_DELAY(0);
7007 	intel_de_write(dev_priv, reg, val);
7008 }
7009 
7010 static void hsw_crtc_enable(struct intel_atomic_state *state,
7011 			    struct intel_crtc *crtc)
7012 {
7013 	const struct intel_crtc_state *new_crtc_state =
7014 		intel_atomic_get_new_crtc_state(state, crtc);
7015 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7016 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
7017 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
7018 	bool psl_clkgate_wa;
7019 
7020 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7021 		return;
7022 
7023 	intel_encoders_pre_pll_enable(state, crtc);
7024 
7025 	if (new_crtc_state->shared_dpll)
7026 		intel_enable_shared_dpll(new_crtc_state);
7027 
7028 	intel_encoders_pre_enable(state, crtc);
7029 
7030 	if (!transcoder_is_dsi(cpu_transcoder))
7031 		intel_set_pipe_timings(new_crtc_state);
7032 
7033 	intel_set_pipe_src_size(new_crtc_state);
7034 
7035 	if (cpu_transcoder != TRANSCODER_EDP &&
7036 	    !transcoder_is_dsi(cpu_transcoder))
7037 		intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7038 			       new_crtc_state->pixel_multiplier - 1);
7039 
7040 	if (new_crtc_state->has_pch_encoder)
7041 		intel_cpu_transcoder_set_m_n(new_crtc_state,
7042 					     &new_crtc_state->fdi_m_n, NULL);
7043 
7044 	if (!transcoder_is_dsi(cpu_transcoder)) {
7045 		hsw_set_frame_start_delay(new_crtc_state);
7046 		hsw_set_pipeconf(new_crtc_state);
7047 	}
7048 
7049 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7050 		bdw_set_pipemisc(new_crtc_state);
7051 
7052 	crtc->active = true;
7053 
7054 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7055 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
7056 		new_crtc_state->pch_pfit.enabled;
7057 	if (psl_clkgate_wa)
7058 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7059 
7060 	if (INTEL_GEN(dev_priv) >= 9)
7061 		skl_pfit_enable(new_crtc_state);
7062 	else
7063 		ilk_pfit_enable(new_crtc_state);
7064 
7065 	/*
7066 	 * On ILK+ LUT must be loaded before the pipe is running but with
7067 	 * clocks enabled
7068 	 */
7069 	intel_color_load_luts(new_crtc_state);
7070 	intel_color_commit(new_crtc_state);
7071 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
7072 	if (INTEL_GEN(dev_priv) < 9)
7073 		intel_disable_primary_plane(new_crtc_state);
7074 
7075 	hsw_set_linetime_wm(new_crtc_state);
7076 
7077 	if (INTEL_GEN(dev_priv) >= 11)
7078 		icl_set_pipe_chicken(crtc);
7079 
7080 	if (dev_priv->display.initial_watermarks)
7081 		dev_priv->display.initial_watermarks(state, crtc);
7082 
7083 	if (INTEL_GEN(dev_priv) >= 11)
7084 		icl_pipe_mbus_enable(crtc);
7085 
7086 	intel_encoders_enable(state, crtc);
7087 
7088 	if (psl_clkgate_wa) {
7089 		intel_wait_for_vblank(dev_priv, pipe);
7090 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7091 	}
7092 
7093 	/* If we change the relative order between pipe/planes enabling, we need
7094 	 * to change the workaround. */
7095 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
7096 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
7097 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7098 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7099 	}
7100 }
7101 
7102 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7103 {
7104 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7105 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7106 	enum pipe pipe = crtc->pipe;
7107 
7108 	/* To avoid upsetting the power well on haswell only disable the pfit if
7109 	 * it's in use. The hw state code will make sure we get this right. */
7110 	if (!old_crtc_state->pch_pfit.enabled)
7111 		return;
7112 
7113 	intel_de_write(dev_priv, PF_CTL(pipe), 0);
7114 	intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7115 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
7116 }
7117 
7118 static void ilk_crtc_disable(struct intel_atomic_state *state,
7119 			     struct intel_crtc *crtc)
7120 {
7121 	const struct intel_crtc_state *old_crtc_state =
7122 		intel_atomic_get_old_crtc_state(state, crtc);
7123 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7124 	enum pipe pipe = crtc->pipe;
7125 
7126 	/*
7127 	 * Sometimes spurious CPU pipe underruns happen when the
7128 	 * pipe is already disabled, but FDI RX/TX is still enabled.
7129 	 * Happens at least with VGA+HDMI cloning. Suppress them.
7130 	 */
7131 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7132 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7133 
7134 	intel_encoders_disable(state, crtc);
7135 
7136 	intel_crtc_vblank_off(old_crtc_state);
7137 
7138 	intel_disable_pipe(old_crtc_state);
7139 
7140 	ilk_pfit_disable(old_crtc_state);
7141 
7142 	if (old_crtc_state->has_pch_encoder)
7143 		ilk_fdi_disable(crtc);
7144 
7145 	intel_encoders_post_disable(state, crtc);
7146 
7147 	if (old_crtc_state->has_pch_encoder) {
7148 		ilk_disable_pch_transcoder(dev_priv, pipe);
7149 
7150 		if (HAS_PCH_CPT(dev_priv)) {
7151 			i915_reg_t reg;
7152 			u32 temp;
7153 
7154 			/* disable TRANS_DP_CTL */
7155 			reg = TRANS_DP_CTL(pipe);
7156 			temp = intel_de_read(dev_priv, reg);
7157 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7158 				  TRANS_DP_PORT_SEL_MASK);
7159 			temp |= TRANS_DP_PORT_SEL_NONE;
7160 			intel_de_write(dev_priv, reg, temp);
7161 
7162 			/* disable DPLL_SEL */
7163 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
7164 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
7165 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
7166 		}
7167 
7168 		ilk_fdi_pll_disable(crtc);
7169 	}
7170 
7171 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7172 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7173 }
7174 
7175 static void hsw_crtc_disable(struct intel_atomic_state *state,
7176 			     struct intel_crtc *crtc)
7177 {
7178 	/*
7179 	 * FIXME collapse everything to one hook.
7180 	 * Need care with mst->ddi interactions.
7181 	 */
7182 	intel_encoders_disable(state, crtc);
7183 	intel_encoders_post_disable(state, crtc);
7184 }
7185 
7186 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
7187 {
7188 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7189 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7190 
7191 	if (!crtc_state->gmch_pfit.control)
7192 		return;
7193 
7194 	/*
7195 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
7196 	 * according to register description and PRM.
7197 	 */
7198 	drm_WARN_ON(&dev_priv->drm,
7199 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
7200 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
7201 
7202 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7203 		       crtc_state->gmch_pfit.pgm_ratios);
7204 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
7205 
7206 	/* Border color in case we don't scale up to the full screen. Black by
7207 	 * default, change to something else for debugging. */
7208 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
7209 }
7210 
7211 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7212 {
7213 	if (phy == PHY_NONE)
7214 		return false;
7215 
7216 	if (IS_ELKHARTLAKE(dev_priv))
7217 		return phy <= PHY_C;
7218 
7219 	if (INTEL_GEN(dev_priv) >= 11)
7220 		return phy <= PHY_B;
7221 
7222 	return false;
7223 }
7224 
7225 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7226 {
7227 	if (INTEL_GEN(dev_priv) >= 12)
7228 		return phy >= PHY_D && phy <= PHY_I;
7229 
7230 	if (INTEL_GEN(dev_priv) >= 11 && !IS_ELKHARTLAKE(dev_priv))
7231 		return phy >= PHY_C && phy <= PHY_F;
7232 
7233 	return false;
7234 }
7235 
7236 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7237 {
7238 	if (IS_ELKHARTLAKE(i915) && port == PORT_D)
7239 		return PHY_A;
7240 
7241 	return (enum phy)port;
7242 }
7243 
7244 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7245 {
7246 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
7247 		return PORT_TC_NONE;
7248 
7249 	if (INTEL_GEN(dev_priv) >= 12)
7250 		return port - PORT_D;
7251 
7252 	return port - PORT_C;
7253 }
7254 
7255 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
7256 {
7257 	switch (port) {
7258 	case PORT_A:
7259 		return POWER_DOMAIN_PORT_DDI_A_LANES;
7260 	case PORT_B:
7261 		return POWER_DOMAIN_PORT_DDI_B_LANES;
7262 	case PORT_C:
7263 		return POWER_DOMAIN_PORT_DDI_C_LANES;
7264 	case PORT_D:
7265 		return POWER_DOMAIN_PORT_DDI_D_LANES;
7266 	case PORT_E:
7267 		return POWER_DOMAIN_PORT_DDI_E_LANES;
7268 	case PORT_F:
7269 		return POWER_DOMAIN_PORT_DDI_F_LANES;
7270 	case PORT_G:
7271 		return POWER_DOMAIN_PORT_DDI_G_LANES;
7272 	default:
7273 		MISSING_CASE(port);
7274 		return POWER_DOMAIN_PORT_OTHER;
7275 	}
7276 }
7277 
7278 enum intel_display_power_domain
7279 intel_aux_power_domain(struct intel_digital_port *dig_port)
7280 {
7281 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
7282 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
7283 
7284 	if (intel_phy_is_tc(dev_priv, phy) &&
7285 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
7286 		switch (dig_port->aux_ch) {
7287 		case AUX_CH_C:
7288 			return POWER_DOMAIN_AUX_C_TBT;
7289 		case AUX_CH_D:
7290 			return POWER_DOMAIN_AUX_D_TBT;
7291 		case AUX_CH_E:
7292 			return POWER_DOMAIN_AUX_E_TBT;
7293 		case AUX_CH_F:
7294 			return POWER_DOMAIN_AUX_F_TBT;
7295 		case AUX_CH_G:
7296 			return POWER_DOMAIN_AUX_G_TBT;
7297 		default:
7298 			MISSING_CASE(dig_port->aux_ch);
7299 			return POWER_DOMAIN_AUX_C_TBT;
7300 		}
7301 	}
7302 
7303 	return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
7304 }
7305 
7306 /*
7307  * Converts aux_ch to power_domain without caring about TBT ports for that use
7308  * intel_aux_power_domain()
7309  */
7310 enum intel_display_power_domain
7311 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
7312 {
7313 	switch (aux_ch) {
7314 	case AUX_CH_A:
7315 		return POWER_DOMAIN_AUX_A;
7316 	case AUX_CH_B:
7317 		return POWER_DOMAIN_AUX_B;
7318 	case AUX_CH_C:
7319 		return POWER_DOMAIN_AUX_C;
7320 	case AUX_CH_D:
7321 		return POWER_DOMAIN_AUX_D;
7322 	case AUX_CH_E:
7323 		return POWER_DOMAIN_AUX_E;
7324 	case AUX_CH_F:
7325 		return POWER_DOMAIN_AUX_F;
7326 	case AUX_CH_G:
7327 		return POWER_DOMAIN_AUX_G;
7328 	default:
7329 		MISSING_CASE(aux_ch);
7330 		return POWER_DOMAIN_AUX_A;
7331 	}
7332 }
7333 
7334 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7335 {
7336 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7337 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7338 	struct drm_encoder *encoder;
7339 	enum pipe pipe = crtc->pipe;
7340 	u64 mask;
7341 	enum transcoder transcoder = crtc_state->cpu_transcoder;
7342 
7343 	if (!crtc_state->hw.active)
7344 		return 0;
7345 
7346 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7347 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
7348 	if (crtc_state->pch_pfit.enabled ||
7349 	    crtc_state->pch_pfit.force_thru)
7350 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
7351 
7352 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
7353 				  crtc_state->uapi.encoder_mask) {
7354 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7355 
7356 		mask |= BIT_ULL(intel_encoder->power_domain);
7357 	}
7358 
7359 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
7360 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
7361 
7362 	if (crtc_state->shared_dpll)
7363 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
7364 
7365 	return mask;
7366 }
7367 
7368 static u64
7369 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7370 {
7371 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7372 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7373 	enum intel_display_power_domain domain;
7374 	u64 domains, new_domains, old_domains;
7375 
7376 	old_domains = crtc->enabled_power_domains;
7377 	crtc->enabled_power_domains = new_domains =
7378 		get_crtc_power_domains(crtc_state);
7379 
7380 	domains = new_domains & ~old_domains;
7381 
7382 	for_each_power_domain(domain, domains)
7383 		intel_display_power_get(dev_priv, domain);
7384 
7385 	return old_domains & ~new_domains;
7386 }
7387 
7388 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
7389 				      u64 domains)
7390 {
7391 	enum intel_display_power_domain domain;
7392 
7393 	for_each_power_domain(domain, domains)
7394 		intel_display_power_put_unchecked(dev_priv, domain);
7395 }
7396 
7397 static void valleyview_crtc_enable(struct intel_atomic_state *state,
7398 				   struct intel_crtc *crtc)
7399 {
7400 	const struct intel_crtc_state *new_crtc_state =
7401 		intel_atomic_get_new_crtc_state(state, crtc);
7402 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7403 	enum pipe pipe = crtc->pipe;
7404 
7405 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7406 		return;
7407 
7408 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7409 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7410 
7411 	intel_set_pipe_timings(new_crtc_state);
7412 	intel_set_pipe_src_size(new_crtc_state);
7413 
7414 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
7415 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7416 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
7417 	}
7418 
7419 	i9xx_set_pipeconf(new_crtc_state);
7420 
7421 	crtc->active = true;
7422 
7423 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7424 
7425 	intel_encoders_pre_pll_enable(state, crtc);
7426 
7427 	if (IS_CHERRYVIEW(dev_priv)) {
7428 		chv_prepare_pll(crtc, new_crtc_state);
7429 		chv_enable_pll(crtc, new_crtc_state);
7430 	} else {
7431 		vlv_prepare_pll(crtc, new_crtc_state);
7432 		vlv_enable_pll(crtc, new_crtc_state);
7433 	}
7434 
7435 	intel_encoders_pre_enable(state, crtc);
7436 
7437 	i9xx_pfit_enable(new_crtc_state);
7438 
7439 	intel_color_load_luts(new_crtc_state);
7440 	intel_color_commit(new_crtc_state);
7441 	/* update DSPCNTR to configure gamma for pipe bottom color */
7442 	intel_disable_primary_plane(new_crtc_state);
7443 
7444 	dev_priv->display.initial_watermarks(state, crtc);
7445 	intel_enable_pipe(new_crtc_state);
7446 
7447 	intel_crtc_vblank_on(new_crtc_state);
7448 
7449 	intel_encoders_enable(state, crtc);
7450 }
7451 
7452 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
7453 {
7454 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7455 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7456 
7457 	intel_de_write(dev_priv, FP0(crtc->pipe),
7458 		       crtc_state->dpll_hw_state.fp0);
7459 	intel_de_write(dev_priv, FP1(crtc->pipe),
7460 		       crtc_state->dpll_hw_state.fp1);
7461 }
7462 
7463 static void i9xx_crtc_enable(struct intel_atomic_state *state,
7464 			     struct intel_crtc *crtc)
7465 {
7466 	const struct intel_crtc_state *new_crtc_state =
7467 		intel_atomic_get_new_crtc_state(state, crtc);
7468 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7469 	enum pipe pipe = crtc->pipe;
7470 
7471 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7472 		return;
7473 
7474 	i9xx_set_pll_dividers(new_crtc_state);
7475 
7476 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7477 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7478 
7479 	intel_set_pipe_timings(new_crtc_state);
7480 	intel_set_pipe_src_size(new_crtc_state);
7481 
7482 	i9xx_set_pipeconf(new_crtc_state);
7483 
7484 	crtc->active = true;
7485 
7486 	if (!IS_GEN(dev_priv, 2))
7487 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7488 
7489 	intel_encoders_pre_enable(state, crtc);
7490 
7491 	i9xx_enable_pll(crtc, new_crtc_state);
7492 
7493 	i9xx_pfit_enable(new_crtc_state);
7494 
7495 	intel_color_load_luts(new_crtc_state);
7496 	intel_color_commit(new_crtc_state);
7497 	/* update DSPCNTR to configure gamma for pipe bottom color */
7498 	intel_disable_primary_plane(new_crtc_state);
7499 
7500 	if (dev_priv->display.initial_watermarks)
7501 		dev_priv->display.initial_watermarks(state, crtc);
7502 	else
7503 		intel_update_watermarks(crtc);
7504 	intel_enable_pipe(new_crtc_state);
7505 
7506 	intel_crtc_vblank_on(new_crtc_state);
7507 
7508 	intel_encoders_enable(state, crtc);
7509 }
7510 
7511 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7512 {
7513 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7514 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7515 
7516 	if (!old_crtc_state->gmch_pfit.control)
7517 		return;
7518 
7519 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7520 
7521 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
7522 		    intel_de_read(dev_priv, PFIT_CONTROL));
7523 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
7524 }
7525 
7526 static void i9xx_crtc_disable(struct intel_atomic_state *state,
7527 			      struct intel_crtc *crtc)
7528 {
7529 	struct intel_crtc_state *old_crtc_state =
7530 		intel_atomic_get_old_crtc_state(state, crtc);
7531 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7532 	enum pipe pipe = crtc->pipe;
7533 
7534 	/*
7535 	 * On gen2 planes are double buffered but the pipe isn't, so we must
7536 	 * wait for planes to fully turn off before disabling the pipe.
7537 	 */
7538 	if (IS_GEN(dev_priv, 2))
7539 		intel_wait_for_vblank(dev_priv, pipe);
7540 
7541 	intel_encoders_disable(state, crtc);
7542 
7543 	intel_crtc_vblank_off(old_crtc_state);
7544 
7545 	intel_disable_pipe(old_crtc_state);
7546 
7547 	i9xx_pfit_disable(old_crtc_state);
7548 
7549 	intel_encoders_post_disable(state, crtc);
7550 
7551 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7552 		if (IS_CHERRYVIEW(dev_priv))
7553 			chv_disable_pll(dev_priv, pipe);
7554 		else if (IS_VALLEYVIEW(dev_priv))
7555 			vlv_disable_pll(dev_priv, pipe);
7556 		else
7557 			i9xx_disable_pll(old_crtc_state);
7558 	}
7559 
7560 	intel_encoders_post_pll_disable(state, crtc);
7561 
7562 	if (!IS_GEN(dev_priv, 2))
7563 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7564 
7565 	if (!dev_priv->display.initial_watermarks)
7566 		intel_update_watermarks(crtc);
7567 
7568 	/* clock the pipe down to 640x480@60 to potentially save power */
7569 	if (IS_I830(dev_priv))
7570 		i830_enable_pipe(dev_priv, pipe);
7571 }
7572 
7573 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
7574 					struct drm_modeset_acquire_ctx *ctx)
7575 {
7576 	struct intel_encoder *encoder;
7577 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7578 	struct intel_bw_state *bw_state =
7579 		to_intel_bw_state(dev_priv->bw_obj.state);
7580 	struct intel_cdclk_state *cdclk_state =
7581 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
7582 	struct intel_crtc_state *crtc_state =
7583 		to_intel_crtc_state(crtc->base.state);
7584 	enum intel_display_power_domain domain;
7585 	struct intel_plane *plane;
7586 	struct drm_atomic_state *state;
7587 	struct intel_crtc_state *temp_crtc_state;
7588 	enum pipe pipe = crtc->pipe;
7589 	u64 domains;
7590 	int ret;
7591 
7592 	if (!crtc_state->hw.active)
7593 		return;
7594 
7595 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7596 		const struct intel_plane_state *plane_state =
7597 			to_intel_plane_state(plane->base.state);
7598 
7599 		if (plane_state->uapi.visible)
7600 			intel_plane_disable_noatomic(crtc, plane);
7601 	}
7602 
7603 	state = drm_atomic_state_alloc(&dev_priv->drm);
7604 	if (!state) {
7605 		drm_dbg_kms(&dev_priv->drm,
7606 			    "failed to disable [CRTC:%d:%s], out of memory",
7607 			    crtc->base.base.id, crtc->base.name);
7608 		return;
7609 	}
7610 
7611 	state->acquire_ctx = ctx;
7612 
7613 	/* Everything's already locked, -EDEADLK can't happen. */
7614 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7615 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
7616 
7617 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
7618 
7619 	dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
7620 
7621 	drm_atomic_state_put(state);
7622 
7623 	drm_dbg_kms(&dev_priv->drm,
7624 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7625 		    crtc->base.base.id, crtc->base.name);
7626 
7627 	crtc->active = false;
7628 	crtc->base.enabled = false;
7629 
7630 	drm_WARN_ON(&dev_priv->drm,
7631 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
7632 	crtc_state->uapi.active = false;
7633 	crtc_state->uapi.connector_mask = 0;
7634 	crtc_state->uapi.encoder_mask = 0;
7635 	intel_crtc_free_hw_state(crtc_state);
7636 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
7637 
7638 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
7639 		encoder->base.crtc = NULL;
7640 
7641 	intel_fbc_disable(crtc);
7642 	intel_update_watermarks(crtc);
7643 	intel_disable_shared_dpll(crtc_state);
7644 
7645 	domains = crtc->enabled_power_domains;
7646 	for_each_power_domain(domain, domains)
7647 		intel_display_power_put_unchecked(dev_priv, domain);
7648 	crtc->enabled_power_domains = 0;
7649 
7650 	dev_priv->active_pipes &= ~BIT(pipe);
7651 	cdclk_state->min_cdclk[pipe] = 0;
7652 	cdclk_state->min_voltage_level[pipe] = 0;
7653 	cdclk_state->active_pipes &= ~BIT(pipe);
7654 
7655 	bw_state->data_rate[pipe] = 0;
7656 	bw_state->num_active_planes[pipe] = 0;
7657 }
7658 
7659 /*
7660  * turn all crtc's off, but do not adjust state
7661  * This has to be paired with a call to intel_modeset_setup_hw_state.
7662  */
7663 int intel_display_suspend(struct drm_device *dev)
7664 {
7665 	struct drm_i915_private *dev_priv = to_i915(dev);
7666 	struct drm_atomic_state *state;
7667 	int ret;
7668 
7669 	state = drm_atomic_helper_suspend(dev);
7670 	ret = PTR_ERR_OR_ZERO(state);
7671 	if (ret)
7672 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7673 			ret);
7674 	else
7675 		dev_priv->modeset_restore_state = state;
7676 	return ret;
7677 }
7678 
7679 void intel_encoder_destroy(struct drm_encoder *encoder)
7680 {
7681 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7682 
7683 	drm_encoder_cleanup(encoder);
7684 	kfree(intel_encoder);
7685 }
7686 
7687 /* Cross check the actual hw state with our own modeset state tracking (and it's
7688  * internal consistency). */
7689 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7690 					 struct drm_connector_state *conn_state)
7691 {
7692 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7693 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
7694 
7695 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7696 		    connector->base.base.id, connector->base.name);
7697 
7698 	if (connector->get_hw_state(connector)) {
7699 		struct intel_encoder *encoder = intel_attached_encoder(connector);
7700 
7701 		I915_STATE_WARN(!crtc_state,
7702 			 "connector enabled without attached crtc\n");
7703 
7704 		if (!crtc_state)
7705 			return;
7706 
7707 		I915_STATE_WARN(!crtc_state->hw.active,
7708 				"connector is active, but attached crtc isn't\n");
7709 
7710 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7711 			return;
7712 
7713 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7714 			"atomic encoder doesn't match attached encoder\n");
7715 
7716 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7717 			"attached encoder crtc differs from connector crtc\n");
7718 	} else {
7719 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7720 				"attached crtc is active, but connector isn't\n");
7721 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7722 			"best encoder set without crtc!\n");
7723 	}
7724 }
7725 
7726 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7727 {
7728 	if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
7729 		return crtc_state->fdi_lanes;
7730 
7731 	return 0;
7732 }
7733 
7734 static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7735 			       struct intel_crtc_state *pipe_config)
7736 {
7737 	struct drm_i915_private *dev_priv = to_i915(dev);
7738 	struct drm_atomic_state *state = pipe_config->uapi.state;
7739 	struct intel_crtc *other_crtc;
7740 	struct intel_crtc_state *other_crtc_state;
7741 
7742 	drm_dbg_kms(&dev_priv->drm,
7743 		    "checking fdi config on pipe %c, lanes %i\n",
7744 		    pipe_name(pipe), pipe_config->fdi_lanes);
7745 	if (pipe_config->fdi_lanes > 4) {
7746 		drm_dbg_kms(&dev_priv->drm,
7747 			    "invalid fdi lane config on pipe %c: %i lanes\n",
7748 			    pipe_name(pipe), pipe_config->fdi_lanes);
7749 		return -EINVAL;
7750 	}
7751 
7752 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7753 		if (pipe_config->fdi_lanes > 2) {
7754 			drm_dbg_kms(&dev_priv->drm,
7755 				    "only 2 lanes on haswell, required: %i lanes\n",
7756 				    pipe_config->fdi_lanes);
7757 			return -EINVAL;
7758 		} else {
7759 			return 0;
7760 		}
7761 	}
7762 
7763 	if (INTEL_NUM_PIPES(dev_priv) == 2)
7764 		return 0;
7765 
7766 	/* Ivybridge 3 pipe is really complicated */
7767 	switch (pipe) {
7768 	case PIPE_A:
7769 		return 0;
7770 	case PIPE_B:
7771 		if (pipe_config->fdi_lanes <= 2)
7772 			return 0;
7773 
7774 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
7775 		other_crtc_state =
7776 			intel_atomic_get_crtc_state(state, other_crtc);
7777 		if (IS_ERR(other_crtc_state))
7778 			return PTR_ERR(other_crtc_state);
7779 
7780 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
7781 			drm_dbg_kms(&dev_priv->drm,
7782 				    "invalid shared fdi lane config on pipe %c: %i lanes\n",
7783 				    pipe_name(pipe), pipe_config->fdi_lanes);
7784 			return -EINVAL;
7785 		}
7786 		return 0;
7787 	case PIPE_C:
7788 		if (pipe_config->fdi_lanes > 2) {
7789 			drm_dbg_kms(&dev_priv->drm,
7790 				    "only 2 lanes on pipe %c: required %i lanes\n",
7791 				    pipe_name(pipe), pipe_config->fdi_lanes);
7792 			return -EINVAL;
7793 		}
7794 
7795 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
7796 		other_crtc_state =
7797 			intel_atomic_get_crtc_state(state, other_crtc);
7798 		if (IS_ERR(other_crtc_state))
7799 			return PTR_ERR(other_crtc_state);
7800 
7801 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
7802 			drm_dbg_kms(&dev_priv->drm,
7803 				    "fdi link B uses too many lanes to enable link C\n");
7804 			return -EINVAL;
7805 		}
7806 		return 0;
7807 	default:
7808 		BUG();
7809 	}
7810 }
7811 
7812 #define RETRY 1
7813 static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
7814 				  struct intel_crtc_state *pipe_config)
7815 {
7816 	struct drm_device *dev = intel_crtc->base.dev;
7817 	struct drm_i915_private *i915 = to_i915(dev);
7818 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7819 	int lane, link_bw, fdi_dotclock, ret;
7820 	bool needs_recompute = false;
7821 
7822 retry:
7823 	/* FDI is a binary signal running at ~2.7GHz, encoding
7824 	 * each output octet as 10 bits. The actual frequency
7825 	 * is stored as a divider into a 100MHz clock, and the
7826 	 * mode pixel clock is stored in units of 1KHz.
7827 	 * Hence the bw of each lane in terms of the mode signal
7828 	 * is:
7829 	 */
7830 	link_bw = intel_fdi_link_freq(i915, pipe_config);
7831 
7832 	fdi_dotclock = adjusted_mode->crtc_clock;
7833 
7834 	lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
7835 				      pipe_config->pipe_bpp);
7836 
7837 	pipe_config->fdi_lanes = lane;
7838 
7839 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
7840 			       link_bw, &pipe_config->fdi_m_n, false, false);
7841 
7842 	ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
7843 	if (ret == -EDEADLK)
7844 		return ret;
7845 
7846 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
7847 		pipe_config->pipe_bpp -= 2*3;
7848 		drm_dbg_kms(&i915->drm,
7849 			    "fdi link bw constraint, reducing pipe bpp to %i\n",
7850 			    pipe_config->pipe_bpp);
7851 		needs_recompute = true;
7852 		pipe_config->bw_constrained = true;
7853 
7854 		goto retry;
7855 	}
7856 
7857 	if (needs_recompute)
7858 		return RETRY;
7859 
7860 	return ret;
7861 }
7862 
7863 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
7864 {
7865 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7866 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7867 
7868 	/* IPS only exists on ULT machines and is tied to pipe A. */
7869 	if (!hsw_crtc_supports_ips(crtc))
7870 		return false;
7871 
7872 	if (!i915_modparams.enable_ips)
7873 		return false;
7874 
7875 	if (crtc_state->pipe_bpp > 24)
7876 		return false;
7877 
7878 	/*
7879 	 * We compare against max which means we must take
7880 	 * the increased cdclk requirement into account when
7881 	 * calculating the new cdclk.
7882 	 *
7883 	 * Should measure whether using a lower cdclk w/o IPS
7884 	 */
7885 	if (IS_BROADWELL(dev_priv) &&
7886 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
7887 		return false;
7888 
7889 	return true;
7890 }
7891 
7892 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7893 {
7894 	struct drm_i915_private *dev_priv =
7895 		to_i915(crtc_state->uapi.crtc->dev);
7896 	struct intel_atomic_state *state =
7897 		to_intel_atomic_state(crtc_state->uapi.state);
7898 
7899 	crtc_state->ips_enabled = false;
7900 
7901 	if (!hsw_crtc_state_ips_capable(crtc_state))
7902 		return 0;
7903 
7904 	/*
7905 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
7906 	 * enabled and disabled dynamically based on package C states,
7907 	 * user space can't make reliable use of the CRCs, so let's just
7908 	 * completely disable it.
7909 	 */
7910 	if (crtc_state->crc_enabled)
7911 		return 0;
7912 
7913 	/* IPS should be fine as long as at least one plane is enabled. */
7914 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
7915 		return 0;
7916 
7917 	if (IS_BROADWELL(dev_priv)) {
7918 		const struct intel_cdclk_state *cdclk_state;
7919 
7920 		cdclk_state = intel_atomic_get_cdclk_state(state);
7921 		if (IS_ERR(cdclk_state))
7922 			return PTR_ERR(cdclk_state);
7923 
7924 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
7925 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
7926 			return 0;
7927 	}
7928 
7929 	crtc_state->ips_enabled = true;
7930 
7931 	return 0;
7932 }
7933 
7934 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7935 {
7936 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7937 
7938 	/* GDG double wide on either pipe, otherwise pipe A only */
7939 	return INTEL_GEN(dev_priv) < 4 &&
7940 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7941 }
7942 
7943 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
7944 {
7945 	u32 pixel_rate = crtc_state->hw.adjusted_mode.crtc_clock;
7946 	unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
7947 
7948 	/*
7949 	 * We only use IF-ID interlacing. If we ever use
7950 	 * PF-ID we'll need to adjust the pixel_rate here.
7951 	 */
7952 
7953 	if (!crtc_state->pch_pfit.enabled)
7954 		return pixel_rate;
7955 
7956 	pipe_w = crtc_state->pipe_src_w;
7957 	pipe_h = crtc_state->pipe_src_h;
7958 
7959 	pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
7960 	pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
7961 
7962 	if (pipe_w < pfit_w)
7963 		pipe_w = pfit_w;
7964 	if (pipe_h < pfit_h)
7965 		pipe_h = pfit_h;
7966 
7967 	if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
7968 			!pfit_w || !pfit_h))
7969 		return pixel_rate;
7970 
7971 	return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
7972 		       pfit_w * pfit_h);
7973 }
7974 
7975 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
7976 {
7977 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7978 
7979 	if (HAS_GMCH(dev_priv))
7980 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
7981 		crtc_state->pixel_rate =
7982 			crtc_state->hw.adjusted_mode.crtc_clock;
7983 	else
7984 		crtc_state->pixel_rate =
7985 			ilk_pipe_pixel_rate(crtc_state);
7986 }
7987 
7988 static int intel_crtc_compute_config(struct intel_crtc *crtc,
7989 				     struct intel_crtc_state *pipe_config)
7990 {
7991 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7992 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7993 	int clock_limit = dev_priv->max_dotclk_freq;
7994 
7995 	if (INTEL_GEN(dev_priv) < 4) {
7996 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
7997 
7998 		/*
7999 		 * Enable double wide mode when the dot clock
8000 		 * is > 90% of the (display) core speed.
8001 		 */
8002 		if (intel_crtc_supports_double_wide(crtc) &&
8003 		    adjusted_mode->crtc_clock > clock_limit) {
8004 			clock_limit = dev_priv->max_dotclk_freq;
8005 			pipe_config->double_wide = true;
8006 		}
8007 	}
8008 
8009 	if (adjusted_mode->crtc_clock > clock_limit) {
8010 		drm_dbg_kms(&dev_priv->drm,
8011 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
8012 			    adjusted_mode->crtc_clock, clock_limit,
8013 			    yesno(pipe_config->double_wide));
8014 		return -EINVAL;
8015 	}
8016 
8017 	if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8018 	     pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
8019 	     pipe_config->hw.ctm) {
8020 		/*
8021 		 * There is only one pipe CSC unit per pipe, and we need that
8022 		 * for output conversion from RGB->YCBCR. So if CTM is already
8023 		 * applied we can't support YCBCR420 output.
8024 		 */
8025 		drm_dbg_kms(&dev_priv->drm,
8026 			    "YCBCR420 and CTM together are not possible\n");
8027 		return -EINVAL;
8028 	}
8029 
8030 	/*
8031 	 * Pipe horizontal size must be even in:
8032 	 * - DVO ganged mode
8033 	 * - LVDS dual channel mode
8034 	 * - Double wide pipe
8035 	 */
8036 	if (pipe_config->pipe_src_w & 1) {
8037 		if (pipe_config->double_wide) {
8038 			drm_dbg_kms(&dev_priv->drm,
8039 				    "Odd pipe source width not supported with double wide pipe\n");
8040 			return -EINVAL;
8041 		}
8042 
8043 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
8044 		    intel_is_dual_link_lvds(dev_priv)) {
8045 			drm_dbg_kms(&dev_priv->drm,
8046 				    "Odd pipe source width not supported with dual link LVDS\n");
8047 			return -EINVAL;
8048 		}
8049 	}
8050 
8051 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
8052 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8053 	 */
8054 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8055 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
8056 		return -EINVAL;
8057 
8058 	intel_crtc_compute_pixel_rate(pipe_config);
8059 
8060 	if (pipe_config->has_pch_encoder)
8061 		return ilk_fdi_compute_config(crtc, pipe_config);
8062 
8063 	return 0;
8064 }
8065 
8066 static void
8067 intel_reduce_m_n_ratio(u32 *num, u32 *den)
8068 {
8069 	while (*num > DATA_LINK_M_N_MASK ||
8070 	       *den > DATA_LINK_M_N_MASK) {
8071 		*num >>= 1;
8072 		*den >>= 1;
8073 	}
8074 }
8075 
8076 static void compute_m_n(unsigned int m, unsigned int n,
8077 			u32 *ret_m, u32 *ret_n,
8078 			bool constant_n)
8079 {
8080 	/*
8081 	 * Several DP dongles in particular seem to be fussy about
8082 	 * too large link M/N values. Give N value as 0x8000 that
8083 	 * should be acceptable by specific devices. 0x8000 is the
8084 	 * specified fixed N value for asynchronous clock mode,
8085 	 * which the devices expect also in synchronous clock mode.
8086 	 */
8087 	if (constant_n)
8088 		*ret_n = 0x8000;
8089 	else
8090 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
8091 
8092 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
8093 	intel_reduce_m_n_ratio(ret_m, ret_n);
8094 }
8095 
8096 void
8097 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
8098 		       int pixel_clock, int link_clock,
8099 		       struct intel_link_m_n *m_n,
8100 		       bool constant_n, bool fec_enable)
8101 {
8102 	u32 data_clock = bits_per_pixel * pixel_clock;
8103 
8104 	if (fec_enable)
8105 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
8106 
8107 	m_n->tu = 64;
8108 	compute_m_n(data_clock,
8109 		    link_clock * nlanes * 8,
8110 		    &m_n->gmch_m, &m_n->gmch_n,
8111 		    constant_n);
8112 
8113 	compute_m_n(pixel_clock, link_clock,
8114 		    &m_n->link_m, &m_n->link_n,
8115 		    constant_n);
8116 }
8117 
8118 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8119 {
8120 	/*
8121 	 * There may be no VBT; and if the BIOS enabled SSC we can
8122 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
8123 	 * BIOS isn't using it, don't assume it will work even if the VBT
8124 	 * indicates as much.
8125 	 */
8126 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
8127 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8128 						       PCH_DREF_CONTROL) &
8129 			DREF_SSC1_ENABLE;
8130 
8131 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
8132 			drm_dbg_kms(&dev_priv->drm,
8133 				    "SSC %s by BIOS, overriding VBT which says %s\n",
8134 				    enableddisabled(bios_lvds_use_ssc),
8135 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
8136 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8137 		}
8138 	}
8139 }
8140 
8141 static bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
8142 {
8143 	if (i915_modparams.panel_use_ssc >= 0)
8144 		return i915_modparams.panel_use_ssc != 0;
8145 	return dev_priv->vbt.lvds_use_ssc
8146 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
8147 }
8148 
8149 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
8150 {
8151 	return (1 << dpll->n) << 16 | dpll->m2;
8152 }
8153 
8154 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
8155 {
8156 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
8157 }
8158 
8159 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
8160 				     struct intel_crtc_state *crtc_state,
8161 				     struct dpll *reduced_clock)
8162 {
8163 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8164 	u32 fp, fp2 = 0;
8165 
8166 	if (IS_PINEVIEW(dev_priv)) {
8167 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
8168 		if (reduced_clock)
8169 			fp2 = pnv_dpll_compute_fp(reduced_clock);
8170 	} else {
8171 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8172 		if (reduced_clock)
8173 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
8174 	}
8175 
8176 	crtc_state->dpll_hw_state.fp0 = fp;
8177 
8178 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8179 	    reduced_clock) {
8180 		crtc_state->dpll_hw_state.fp1 = fp2;
8181 	} else {
8182 		crtc_state->dpll_hw_state.fp1 = fp;
8183 	}
8184 }
8185 
8186 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8187 		pipe)
8188 {
8189 	u32 reg_val;
8190 
8191 	/*
8192 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8193 	 * and set it to a reasonable value instead.
8194 	 */
8195 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8196 	reg_val &= 0xffffff00;
8197 	reg_val |= 0x00000030;
8198 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8199 
8200 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8201 	reg_val &= 0x00ffffff;
8202 	reg_val |= 0x8c000000;
8203 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8204 
8205 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8206 	reg_val &= 0xffffff00;
8207 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8208 
8209 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8210 	reg_val &= 0x00ffffff;
8211 	reg_val |= 0xb0000000;
8212 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8213 }
8214 
8215 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8216 					 const struct intel_link_m_n *m_n)
8217 {
8218 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8219 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8220 	enum pipe pipe = crtc->pipe;
8221 
8222 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8223 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
8224 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8225 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8226 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
8227 }
8228 
8229 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8230 				 enum transcoder transcoder)
8231 {
8232 	if (IS_HASWELL(dev_priv))
8233 		return transcoder == TRANSCODER_EDP;
8234 
8235 	/*
8236 	 * Strictly speaking some registers are available before
8237 	 * gen7, but we only support DRRS on gen7+
8238 	 */
8239 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
8240 }
8241 
8242 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8243 					 const struct intel_link_m_n *m_n,
8244 					 const struct intel_link_m_n *m2_n2)
8245 {
8246 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8247 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8248 	enum pipe pipe = crtc->pipe;
8249 	enum transcoder transcoder = crtc_state->cpu_transcoder;
8250 
8251 	if (INTEL_GEN(dev_priv) >= 5) {
8252 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8253 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8254 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8255 			       m_n->gmch_n);
8256 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8257 			       m_n->link_m);
8258 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8259 			       m_n->link_n);
8260 		/*
8261 		 *  M2_N2 registers are set only if DRRS is supported
8262 		 * (to make sure the registers are not unnecessarily accessed).
8263 		 */
8264 		if (m2_n2 && crtc_state->has_drrs &&
8265 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
8266 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8267 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8268 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8269 				       m2_n2->gmch_n);
8270 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8271 				       m2_n2->link_m);
8272 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8273 				       m2_n2->link_n);
8274 		}
8275 	} else {
8276 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8277 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8278 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8279 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8280 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
8281 	}
8282 }
8283 
8284 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
8285 {
8286 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
8287 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
8288 
8289 	if (m_n == M1_N1) {
8290 		dp_m_n = &crtc_state->dp_m_n;
8291 		dp_m2_n2 = &crtc_state->dp_m2_n2;
8292 	} else if (m_n == M2_N2) {
8293 
8294 		/*
8295 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
8296 		 * needs to be programmed into M1_N1.
8297 		 */
8298 		dp_m_n = &crtc_state->dp_m2_n2;
8299 	} else {
8300 		drm_err(&i915->drm, "Unsupported divider value\n");
8301 		return;
8302 	}
8303 
8304 	if (crtc_state->has_pch_encoder)
8305 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
8306 	else
8307 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
8308 }
8309 
8310 static void vlv_compute_dpll(struct intel_crtc *crtc,
8311 			     struct intel_crtc_state *pipe_config)
8312 {
8313 	pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
8314 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8315 	if (crtc->pipe != PIPE_A)
8316 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8317 
8318 	/* DPLL not used with DSI, but still need the rest set up */
8319 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8320 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8321 			DPLL_EXT_BUFFER_ENABLE_VLV;
8322 
8323 	pipe_config->dpll_hw_state.dpll_md =
8324 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8325 }
8326 
8327 static void chv_compute_dpll(struct intel_crtc *crtc,
8328 			     struct intel_crtc_state *pipe_config)
8329 {
8330 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
8331 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8332 	if (crtc->pipe != PIPE_A)
8333 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8334 
8335 	/* DPLL not used with DSI, but still need the rest set up */
8336 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8337 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8338 
8339 	pipe_config->dpll_hw_state.dpll_md =
8340 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8341 }
8342 
8343 static void vlv_prepare_pll(struct intel_crtc *crtc,
8344 			    const struct intel_crtc_state *pipe_config)
8345 {
8346 	struct drm_device *dev = crtc->base.dev;
8347 	struct drm_i915_private *dev_priv = to_i915(dev);
8348 	enum pipe pipe = crtc->pipe;
8349 	u32 mdiv;
8350 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
8351 	u32 coreclk, reg_val;
8352 
8353 	/* Enable Refclk */
8354 	intel_de_write(dev_priv, DPLL(pipe),
8355 		       pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
8356 
8357 	/* No need to actually set up the DPLL with DSI */
8358 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8359 		return;
8360 
8361 	vlv_dpio_get(dev_priv);
8362 
8363 	bestn = pipe_config->dpll.n;
8364 	bestm1 = pipe_config->dpll.m1;
8365 	bestm2 = pipe_config->dpll.m2;
8366 	bestp1 = pipe_config->dpll.p1;
8367 	bestp2 = pipe_config->dpll.p2;
8368 
8369 	/* See eDP HDMI DPIO driver vbios notes doc */
8370 
8371 	/* PLL B needs special handling */
8372 	if (pipe == PIPE_B)
8373 		vlv_pllb_recal_opamp(dev_priv, pipe);
8374 
8375 	/* Set up Tx target for periodic Rcomp update */
8376 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
8377 
8378 	/* Disable target IRef on PLL */
8379 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
8380 	reg_val &= 0x00ffffff;
8381 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
8382 
8383 	/* Disable fast lock */
8384 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
8385 
8386 	/* Set idtafcrecal before PLL is enabled */
8387 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8388 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8389 	mdiv |= ((bestn << DPIO_N_SHIFT));
8390 	mdiv |= (1 << DPIO_K_SHIFT);
8391 
8392 	/*
8393 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8394 	 * but we don't support that).
8395 	 * Note: don't use the DAC post divider as it seems unstable.
8396 	 */
8397 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
8398 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8399 
8400 	mdiv |= DPIO_ENABLE_CALIBRATION;
8401 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8402 
8403 	/* Set HBR and RBR LPF coefficients */
8404 	if (pipe_config->port_clock == 162000 ||
8405 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8406 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
8407 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8408 				 0x009f0003);
8409 	else
8410 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8411 				 0x00d0000f);
8412 
8413 	if (intel_crtc_has_dp_encoder(pipe_config)) {
8414 		/* Use SSC source */
8415 		if (pipe == PIPE_A)
8416 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8417 					 0x0df40000);
8418 		else
8419 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8420 					 0x0df70000);
8421 	} else { /* HDMI or VGA */
8422 		/* Use bend source */
8423 		if (pipe == PIPE_A)
8424 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8425 					 0x0df70000);
8426 		else
8427 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8428 					 0x0df40000);
8429 	}
8430 
8431 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
8432 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
8433 	if (intel_crtc_has_dp_encoder(pipe_config))
8434 		coreclk |= 0x01000000;
8435 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
8436 
8437 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
8438 
8439 	vlv_dpio_put(dev_priv);
8440 }
8441 
8442 static void chv_prepare_pll(struct intel_crtc *crtc,
8443 			    const struct intel_crtc_state *pipe_config)
8444 {
8445 	struct drm_device *dev = crtc->base.dev;
8446 	struct drm_i915_private *dev_priv = to_i915(dev);
8447 	enum pipe pipe = crtc->pipe;
8448 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8449 	u32 loopfilter, tribuf_calcntr;
8450 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
8451 	u32 dpio_val;
8452 	int vco;
8453 
8454 	/* Enable Refclk and SSC */
8455 	intel_de_write(dev_priv, DPLL(pipe),
8456 		       pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8457 
8458 	/* No need to actually set up the DPLL with DSI */
8459 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8460 		return;
8461 
8462 	bestn = pipe_config->dpll.n;
8463 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8464 	bestm1 = pipe_config->dpll.m1;
8465 	bestm2 = pipe_config->dpll.m2 >> 22;
8466 	bestp1 = pipe_config->dpll.p1;
8467 	bestp2 = pipe_config->dpll.p2;
8468 	vco = pipe_config->dpll.vco;
8469 	dpio_val = 0;
8470 	loopfilter = 0;
8471 
8472 	vlv_dpio_get(dev_priv);
8473 
8474 	/* p1 and p2 divider */
8475 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8476 			5 << DPIO_CHV_S1_DIV_SHIFT |
8477 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8478 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8479 			1 << DPIO_CHV_K_DIV_SHIFT);
8480 
8481 	/* Feedback post-divider - m2 */
8482 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8483 
8484 	/* Feedback refclk divider - n and m1 */
8485 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8486 			DPIO_CHV_M1_DIV_BY_2 |
8487 			1 << DPIO_CHV_N_DIV_SHIFT);
8488 
8489 	/* M2 fraction division */
8490 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
8491 
8492 	/* M2 fraction division enable */
8493 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8494 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8495 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8496 	if (bestm2_frac)
8497 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8498 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
8499 
8500 	/* Program digital lock detect threshold */
8501 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8502 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8503 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8504 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8505 	if (!bestm2_frac)
8506 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8507 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8508 
8509 	/* Loop filter */
8510 	if (vco == 5400000) {
8511 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8512 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8513 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8514 		tribuf_calcntr = 0x9;
8515 	} else if (vco <= 6200000) {
8516 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8517 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8518 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8519 		tribuf_calcntr = 0x9;
8520 	} else if (vco <= 6480000) {
8521 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8522 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8523 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8524 		tribuf_calcntr = 0x8;
8525 	} else {
8526 		/* Not supported. Apply the same limits as in the max case */
8527 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8528 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8529 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8530 		tribuf_calcntr = 0;
8531 	}
8532 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8533 
8534 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
8535 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8536 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8537 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8538 
8539 	/* AFC Recal */
8540 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8541 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8542 			DPIO_AFC_RECAL);
8543 
8544 	vlv_dpio_put(dev_priv);
8545 }
8546 
8547 /**
8548  * vlv_force_pll_on - forcibly enable just the PLL
8549  * @dev_priv: i915 private structure
8550  * @pipe: pipe PLL to enable
8551  * @dpll: PLL configuration
8552  *
8553  * Enable the PLL for @pipe using the supplied @dpll config. To be used
8554  * in cases where we need the PLL enabled even when @pipe is not going to
8555  * be enabled.
8556  */
8557 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
8558 		     const struct dpll *dpll)
8559 {
8560 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
8561 	struct intel_crtc_state *pipe_config;
8562 
8563 	pipe_config = intel_crtc_state_alloc(crtc);
8564 	if (!pipe_config)
8565 		return -ENOMEM;
8566 
8567 	pipe_config->cpu_transcoder = (enum transcoder)pipe;
8568 	pipe_config->pixel_multiplier = 1;
8569 	pipe_config->dpll = *dpll;
8570 
8571 	if (IS_CHERRYVIEW(dev_priv)) {
8572 		chv_compute_dpll(crtc, pipe_config);
8573 		chv_prepare_pll(crtc, pipe_config);
8574 		chv_enable_pll(crtc, pipe_config);
8575 	} else {
8576 		vlv_compute_dpll(crtc, pipe_config);
8577 		vlv_prepare_pll(crtc, pipe_config);
8578 		vlv_enable_pll(crtc, pipe_config);
8579 	}
8580 
8581 	kfree(pipe_config);
8582 
8583 	return 0;
8584 }
8585 
8586 /**
8587  * vlv_force_pll_off - forcibly disable just the PLL
8588  * @dev_priv: i915 private structure
8589  * @pipe: pipe PLL to disable
8590  *
8591  * Disable the PLL for @pipe. To be used in cases where we need
8592  * the PLL enabled even when @pipe is not going to be enabled.
8593  */
8594 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
8595 {
8596 	if (IS_CHERRYVIEW(dev_priv))
8597 		chv_disable_pll(dev_priv, pipe);
8598 	else
8599 		vlv_disable_pll(dev_priv, pipe);
8600 }
8601 
8602 static void i9xx_compute_dpll(struct intel_crtc *crtc,
8603 			      struct intel_crtc_state *crtc_state,
8604 			      struct dpll *reduced_clock)
8605 {
8606 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8607 	u32 dpll;
8608 	struct dpll *clock = &crtc_state->dpll;
8609 
8610 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8611 
8612 	dpll = DPLL_VGA_MODE_DIS;
8613 
8614 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
8615 		dpll |= DPLLB_MODE_LVDS;
8616 	else
8617 		dpll |= DPLLB_MODE_DAC_SERIAL;
8618 
8619 	if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8620 	    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8621 		dpll |= (crtc_state->pixel_multiplier - 1)
8622 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
8623 	}
8624 
8625 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8626 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8627 		dpll |= DPLL_SDVO_HIGH_SPEED;
8628 
8629 	if (intel_crtc_has_dp_encoder(crtc_state))
8630 		dpll |= DPLL_SDVO_HIGH_SPEED;
8631 
8632 	/* compute bitmask from p1 value */
8633 	if (IS_PINEVIEW(dev_priv))
8634 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8635 	else {
8636 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8637 		if (IS_G4X(dev_priv) && reduced_clock)
8638 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8639 	}
8640 	switch (clock->p2) {
8641 	case 5:
8642 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8643 		break;
8644 	case 7:
8645 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8646 		break;
8647 	case 10:
8648 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8649 		break;
8650 	case 14:
8651 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8652 		break;
8653 	}
8654 	if (INTEL_GEN(dev_priv) >= 4)
8655 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8656 
8657 	if (crtc_state->sdvo_tv_clock)
8658 		dpll |= PLL_REF_INPUT_TVCLKINBC;
8659 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8660 		 intel_panel_use_ssc(dev_priv))
8661 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8662 	else
8663 		dpll |= PLL_REF_INPUT_DREFCLK;
8664 
8665 	dpll |= DPLL_VCO_ENABLE;
8666 	crtc_state->dpll_hw_state.dpll = dpll;
8667 
8668 	if (INTEL_GEN(dev_priv) >= 4) {
8669 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8670 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
8671 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
8672 	}
8673 }
8674 
8675 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8676 			      struct intel_crtc_state *crtc_state,
8677 			      struct dpll *reduced_clock)
8678 {
8679 	struct drm_device *dev = crtc->base.dev;
8680 	struct drm_i915_private *dev_priv = to_i915(dev);
8681 	u32 dpll;
8682 	struct dpll *clock = &crtc_state->dpll;
8683 
8684 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8685 
8686 	dpll = DPLL_VGA_MODE_DIS;
8687 
8688 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8689 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8690 	} else {
8691 		if (clock->p1 == 2)
8692 			dpll |= PLL_P1_DIVIDE_BY_TWO;
8693 		else
8694 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8695 		if (clock->p2 == 4)
8696 			dpll |= PLL_P2_DIVIDE_BY_4;
8697 	}
8698 
8699 	/*
8700 	 * Bspec:
8701 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8702 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8703 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8704 	 *  Enable) must be set to “1” in both the DPLL A Control Register
8705 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8706 	 *
8707 	 * For simplicity We simply keep both bits always enabled in
8708 	 * both DPLLS. The spec says we should disable the DVO 2X clock
8709 	 * when not needed, but this seems to work fine in practice.
8710 	 */
8711 	if (IS_I830(dev_priv) ||
8712 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8713 		dpll |= DPLL_DVO_2X_MODE;
8714 
8715 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8716 	    intel_panel_use_ssc(dev_priv))
8717 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8718 	else
8719 		dpll |= PLL_REF_INPUT_DREFCLK;
8720 
8721 	dpll |= DPLL_VCO_ENABLE;
8722 	crtc_state->dpll_hw_state.dpll = dpll;
8723 }
8724 
8725 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
8726 {
8727 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8728 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8729 	enum pipe pipe = crtc->pipe;
8730 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8731 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8732 	u32 crtc_vtotal, crtc_vblank_end;
8733 	int vsyncshift = 0;
8734 
8735 	/* We need to be careful not to changed the adjusted mode, for otherwise
8736 	 * the hw state checker will get angry at the mismatch. */
8737 	crtc_vtotal = adjusted_mode->crtc_vtotal;
8738 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8739 
8740 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8741 		/* the chip adds 2 halflines automatically */
8742 		crtc_vtotal -= 1;
8743 		crtc_vblank_end -= 1;
8744 
8745 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8746 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8747 		else
8748 			vsyncshift = adjusted_mode->crtc_hsync_start -
8749 				adjusted_mode->crtc_htotal / 2;
8750 		if (vsyncshift < 0)
8751 			vsyncshift += adjusted_mode->crtc_htotal;
8752 	}
8753 
8754 	if (INTEL_GEN(dev_priv) > 3)
8755 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
8756 		               vsyncshift);
8757 
8758 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
8759 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
8760 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
8761 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
8762 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
8763 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
8764 
8765 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
8766 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
8767 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
8768 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
8769 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
8770 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
8771 
8772 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8773 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8774 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8775 	 * bits. */
8776 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
8777 	    (pipe == PIPE_B || pipe == PIPE_C))
8778 		intel_de_write(dev_priv, VTOTAL(pipe),
8779 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
8780 
8781 }
8782 
8783 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
8784 {
8785 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8786 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8787 	enum pipe pipe = crtc->pipe;
8788 
8789 	/* pipesrc controls the size that is scaled from, which should
8790 	 * always be the user's requested size.
8791 	 */
8792 	intel_de_write(dev_priv, PIPESRC(pipe),
8793 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
8794 }
8795 
8796 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
8797 {
8798 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8799 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8800 
8801 	if (IS_GEN(dev_priv, 2))
8802 		return false;
8803 
8804 	if (INTEL_GEN(dev_priv) >= 9 ||
8805 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
8806 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
8807 	else
8808 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
8809 }
8810 
8811 static void intel_get_pipe_timings(struct intel_crtc *crtc,
8812 				   struct intel_crtc_state *pipe_config)
8813 {
8814 	struct drm_device *dev = crtc->base.dev;
8815 	struct drm_i915_private *dev_priv = to_i915(dev);
8816 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8817 	u32 tmp;
8818 
8819 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
8820 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8821 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
8822 
8823 	if (!transcoder_is_dsi(cpu_transcoder)) {
8824 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
8825 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
8826 							(tmp & 0xffff) + 1;
8827 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
8828 						((tmp >> 16) & 0xffff) + 1;
8829 	}
8830 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
8831 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8832 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
8833 
8834 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
8835 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8836 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
8837 
8838 	if (!transcoder_is_dsi(cpu_transcoder)) {
8839 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
8840 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
8841 							(tmp & 0xffff) + 1;
8842 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
8843 						((tmp >> 16) & 0xffff) + 1;
8844 	}
8845 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
8846 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8847 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
8848 
8849 	if (intel_pipe_is_interlaced(pipe_config)) {
8850 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8851 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
8852 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
8853 	}
8854 }
8855 
8856 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8857 				    struct intel_crtc_state *pipe_config)
8858 {
8859 	struct drm_device *dev = crtc->base.dev;
8860 	struct drm_i915_private *dev_priv = to_i915(dev);
8861 	u32 tmp;
8862 
8863 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
8864 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8865 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8866 
8867 	pipe_config->hw.mode.vdisplay = pipe_config->pipe_src_h;
8868 	pipe_config->hw.mode.hdisplay = pipe_config->pipe_src_w;
8869 }
8870 
8871 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
8872 				 struct intel_crtc_state *pipe_config)
8873 {
8874 	mode->hdisplay = pipe_config->hw.adjusted_mode.crtc_hdisplay;
8875 	mode->htotal = pipe_config->hw.adjusted_mode.crtc_htotal;
8876 	mode->hsync_start = pipe_config->hw.adjusted_mode.crtc_hsync_start;
8877 	mode->hsync_end = pipe_config->hw.adjusted_mode.crtc_hsync_end;
8878 
8879 	mode->vdisplay = pipe_config->hw.adjusted_mode.crtc_vdisplay;
8880 	mode->vtotal = pipe_config->hw.adjusted_mode.crtc_vtotal;
8881 	mode->vsync_start = pipe_config->hw.adjusted_mode.crtc_vsync_start;
8882 	mode->vsync_end = pipe_config->hw.adjusted_mode.crtc_vsync_end;
8883 
8884 	mode->flags = pipe_config->hw.adjusted_mode.flags;
8885 	mode->type = DRM_MODE_TYPE_DRIVER;
8886 
8887 	mode->clock = pipe_config->hw.adjusted_mode.crtc_clock;
8888 
8889 	mode->vrefresh = drm_mode_vrefresh(mode);
8890 	drm_mode_set_name(mode);
8891 }
8892 
8893 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
8894 {
8895 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8896 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8897 	u32 pipeconf;
8898 
8899 	pipeconf = 0;
8900 
8901 	/* we keep both pipes enabled on 830 */
8902 	if (IS_I830(dev_priv))
8903 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
8904 
8905 	if (crtc_state->double_wide)
8906 		pipeconf |= PIPECONF_DOUBLE_WIDE;
8907 
8908 	/* only g4x and later have fancy bpc/dither controls */
8909 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8910 	    IS_CHERRYVIEW(dev_priv)) {
8911 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
8912 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
8913 			pipeconf |= PIPECONF_DITHER_EN |
8914 				    PIPECONF_DITHER_TYPE_SP;
8915 
8916 		switch (crtc_state->pipe_bpp) {
8917 		case 18:
8918 			pipeconf |= PIPECONF_6BPC;
8919 			break;
8920 		case 24:
8921 			pipeconf |= PIPECONF_8BPC;
8922 			break;
8923 		case 30:
8924 			pipeconf |= PIPECONF_10BPC;
8925 			break;
8926 		default:
8927 			/* Case prevented by intel_choose_pipe_bpp_dither. */
8928 			BUG();
8929 		}
8930 	}
8931 
8932 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
8933 		if (INTEL_GEN(dev_priv) < 4 ||
8934 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8935 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8936 		else
8937 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8938 	} else {
8939 		pipeconf |= PIPECONF_PROGRESSIVE;
8940 	}
8941 
8942 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
8943 	     crtc_state->limited_color_range)
8944 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
8945 
8946 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
8947 
8948 	pipeconf |= PIPECONF_FRAME_START_DELAY(0);
8949 
8950 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
8951 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
8952 }
8953 
8954 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8955 				   struct intel_crtc_state *crtc_state)
8956 {
8957 	struct drm_device *dev = crtc->base.dev;
8958 	struct drm_i915_private *dev_priv = to_i915(dev);
8959 	const struct intel_limit *limit;
8960 	int refclk = 48000;
8961 
8962 	memset(&crtc_state->dpll_hw_state, 0,
8963 	       sizeof(crtc_state->dpll_hw_state));
8964 
8965 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8966 		if (intel_panel_use_ssc(dev_priv)) {
8967 			refclk = dev_priv->vbt.lvds_ssc_freq;
8968 			drm_dbg_kms(&dev_priv->drm,
8969 				    "using SSC reference clock of %d kHz\n",
8970 				    refclk);
8971 		}
8972 
8973 		limit = &intel_limits_i8xx_lvds;
8974 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
8975 		limit = &intel_limits_i8xx_dvo;
8976 	} else {
8977 		limit = &intel_limits_i8xx_dac;
8978 	}
8979 
8980 	if (!crtc_state->clock_set &&
8981 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8982 				 refclk, NULL, &crtc_state->dpll)) {
8983 		drm_err(&dev_priv->drm,
8984 			"Couldn't find PLL settings for mode!\n");
8985 		return -EINVAL;
8986 	}
8987 
8988 	i8xx_compute_dpll(crtc, crtc_state, NULL);
8989 
8990 	return 0;
8991 }
8992 
8993 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8994 				  struct intel_crtc_state *crtc_state)
8995 {
8996 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8997 	const struct intel_limit *limit;
8998 	int refclk = 96000;
8999 
9000 	memset(&crtc_state->dpll_hw_state, 0,
9001 	       sizeof(crtc_state->dpll_hw_state));
9002 
9003 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9004 		if (intel_panel_use_ssc(dev_priv)) {
9005 			refclk = dev_priv->vbt.lvds_ssc_freq;
9006 			drm_dbg_kms(&dev_priv->drm,
9007 				    "using SSC reference clock of %d kHz\n",
9008 				    refclk);
9009 		}
9010 
9011 		if (intel_is_dual_link_lvds(dev_priv))
9012 			limit = &intel_limits_g4x_dual_channel_lvds;
9013 		else
9014 			limit = &intel_limits_g4x_single_channel_lvds;
9015 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9016 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
9017 		limit = &intel_limits_g4x_hdmi;
9018 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
9019 		limit = &intel_limits_g4x_sdvo;
9020 	} else {
9021 		/* The option is for other outputs */
9022 		limit = &intel_limits_i9xx_sdvo;
9023 	}
9024 
9025 	if (!crtc_state->clock_set &&
9026 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9027 				refclk, NULL, &crtc_state->dpll)) {
9028 		drm_err(&dev_priv->drm,
9029 			"Couldn't find PLL settings for mode!\n");
9030 		return -EINVAL;
9031 	}
9032 
9033 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9034 
9035 	return 0;
9036 }
9037 
9038 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9039 				  struct intel_crtc_state *crtc_state)
9040 {
9041 	struct drm_device *dev = crtc->base.dev;
9042 	struct drm_i915_private *dev_priv = to_i915(dev);
9043 	const struct intel_limit *limit;
9044 	int refclk = 96000;
9045 
9046 	memset(&crtc_state->dpll_hw_state, 0,
9047 	       sizeof(crtc_state->dpll_hw_state));
9048 
9049 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9050 		if (intel_panel_use_ssc(dev_priv)) {
9051 			refclk = dev_priv->vbt.lvds_ssc_freq;
9052 			drm_dbg_kms(&dev_priv->drm,
9053 				    "using SSC reference clock of %d kHz\n",
9054 				    refclk);
9055 		}
9056 
9057 		limit = &pnv_limits_lvds;
9058 	} else {
9059 		limit = &pnv_limits_sdvo;
9060 	}
9061 
9062 	if (!crtc_state->clock_set &&
9063 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9064 				refclk, NULL, &crtc_state->dpll)) {
9065 		drm_err(&dev_priv->drm,
9066 			"Couldn't find PLL settings for mode!\n");
9067 		return -EINVAL;
9068 	}
9069 
9070 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9071 
9072 	return 0;
9073 }
9074 
9075 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9076 				   struct intel_crtc_state *crtc_state)
9077 {
9078 	struct drm_device *dev = crtc->base.dev;
9079 	struct drm_i915_private *dev_priv = to_i915(dev);
9080 	const struct intel_limit *limit;
9081 	int refclk = 96000;
9082 
9083 	memset(&crtc_state->dpll_hw_state, 0,
9084 	       sizeof(crtc_state->dpll_hw_state));
9085 
9086 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9087 		if (intel_panel_use_ssc(dev_priv)) {
9088 			refclk = dev_priv->vbt.lvds_ssc_freq;
9089 			drm_dbg_kms(&dev_priv->drm,
9090 				    "using SSC reference clock of %d kHz\n",
9091 				    refclk);
9092 		}
9093 
9094 		limit = &intel_limits_i9xx_lvds;
9095 	} else {
9096 		limit = &intel_limits_i9xx_sdvo;
9097 	}
9098 
9099 	if (!crtc_state->clock_set &&
9100 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9101 				 refclk, NULL, &crtc_state->dpll)) {
9102 		drm_err(&dev_priv->drm,
9103 			"Couldn't find PLL settings for mode!\n");
9104 		return -EINVAL;
9105 	}
9106 
9107 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9108 
9109 	return 0;
9110 }
9111 
9112 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9113 				  struct intel_crtc_state *crtc_state)
9114 {
9115 	int refclk = 100000;
9116 	const struct intel_limit *limit = &intel_limits_chv;
9117 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9118 
9119 	memset(&crtc_state->dpll_hw_state, 0,
9120 	       sizeof(crtc_state->dpll_hw_state));
9121 
9122 	if (!crtc_state->clock_set &&
9123 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9124 				refclk, NULL, &crtc_state->dpll)) {
9125 		drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
9126 		return -EINVAL;
9127 	}
9128 
9129 	chv_compute_dpll(crtc, crtc_state);
9130 
9131 	return 0;
9132 }
9133 
9134 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9135 				  struct intel_crtc_state *crtc_state)
9136 {
9137 	int refclk = 100000;
9138 	const struct intel_limit *limit = &intel_limits_vlv;
9139 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9140 
9141 	memset(&crtc_state->dpll_hw_state, 0,
9142 	       sizeof(crtc_state->dpll_hw_state));
9143 
9144 	if (!crtc_state->clock_set &&
9145 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9146 				refclk, NULL, &crtc_state->dpll)) {
9147 		drm_err(&i915->drm,  "Couldn't find PLL settings for mode!\n");
9148 		return -EINVAL;
9149 	}
9150 
9151 	vlv_compute_dpll(crtc, crtc_state);
9152 
9153 	return 0;
9154 }
9155 
9156 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9157 {
9158 	if (IS_I830(dev_priv))
9159 		return false;
9160 
9161 	return INTEL_GEN(dev_priv) >= 4 ||
9162 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9163 }
9164 
9165 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
9166 {
9167 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9168 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9169 	u32 tmp;
9170 
9171 	if (!i9xx_has_pfit(dev_priv))
9172 		return;
9173 
9174 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
9175 	if (!(tmp & PFIT_ENABLE))
9176 		return;
9177 
9178 	/* Check whether the pfit is attached to our pipe. */
9179 	if (INTEL_GEN(dev_priv) < 4) {
9180 		if (crtc->pipe != PIPE_B)
9181 			return;
9182 	} else {
9183 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9184 			return;
9185 	}
9186 
9187 	crtc_state->gmch_pfit.control = tmp;
9188 	crtc_state->gmch_pfit.pgm_ratios =
9189 		intel_de_read(dev_priv, PFIT_PGM_RATIOS);
9190 }
9191 
9192 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
9193 			       struct intel_crtc_state *pipe_config)
9194 {
9195 	struct drm_device *dev = crtc->base.dev;
9196 	struct drm_i915_private *dev_priv = to_i915(dev);
9197 	enum pipe pipe = crtc->pipe;
9198 	struct dpll clock;
9199 	u32 mdiv;
9200 	int refclk = 100000;
9201 
9202 	/* In case of DSI, DPLL will not be used */
9203 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9204 		return;
9205 
9206 	vlv_dpio_get(dev_priv);
9207 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
9208 	vlv_dpio_put(dev_priv);
9209 
9210 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9211 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
9212 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9213 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9214 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9215 
9216 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
9217 }
9218 
9219 static void
9220 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9221 			      struct intel_initial_plane_config *plane_config)
9222 {
9223 	struct drm_device *dev = crtc->base.dev;
9224 	struct drm_i915_private *dev_priv = to_i915(dev);
9225 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9226 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9227 	enum pipe pipe;
9228 	u32 val, base, offset;
9229 	int fourcc, pixel_format;
9230 	unsigned int aligned_height;
9231 	struct drm_framebuffer *fb;
9232 	struct intel_framebuffer *intel_fb;
9233 
9234 	if (!plane->get_hw_state(plane, &pipe))
9235 		return;
9236 
9237 	drm_WARN_ON(dev, pipe != crtc->pipe);
9238 
9239 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9240 	if (!intel_fb) {
9241 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
9242 		return;
9243 	}
9244 
9245 	fb = &intel_fb->base;
9246 
9247 	fb->dev = dev;
9248 
9249 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9250 
9251 	if (INTEL_GEN(dev_priv) >= 4) {
9252 		if (val & DISPPLANE_TILED) {
9253 			plane_config->tiling = I915_TILING_X;
9254 			fb->modifier = I915_FORMAT_MOD_X_TILED;
9255 		}
9256 
9257 		if (val & DISPPLANE_ROTATE_180)
9258 			plane_config->rotation = DRM_MODE_ROTATE_180;
9259 	}
9260 
9261 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9262 	    val & DISPPLANE_MIRROR)
9263 		plane_config->rotation |= DRM_MODE_REFLECT_X;
9264 
9265 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9266 	fourcc = i9xx_format_to_fourcc(pixel_format);
9267 	fb->format = drm_format_info(fourcc);
9268 
9269 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
9270 		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9271 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9272 	} else if (INTEL_GEN(dev_priv) >= 4) {
9273 		if (plane_config->tiling)
9274 			offset = intel_de_read(dev_priv,
9275 					       DSPTILEOFF(i9xx_plane));
9276 		else
9277 			offset = intel_de_read(dev_priv,
9278 					       DSPLINOFF(i9xx_plane));
9279 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9280 	} else {
9281 		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
9282 	}
9283 	plane_config->base = base;
9284 
9285 	val = intel_de_read(dev_priv, PIPESRC(pipe));
9286 	fb->width = ((val >> 16) & 0xfff) + 1;
9287 	fb->height = ((val >> 0) & 0xfff) + 1;
9288 
9289 	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
9290 	fb->pitches[0] = val & 0xffffffc0;
9291 
9292 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
9293 
9294 	plane_config->size = fb->pitches[0] * aligned_height;
9295 
9296 	drm_dbg_kms(&dev_priv->drm,
9297 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9298 		    crtc->base.name, plane->base.name, fb->width, fb->height,
9299 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
9300 		    plane_config->size);
9301 
9302 	plane_config->fb = intel_fb;
9303 }
9304 
9305 static void chv_crtc_clock_get(struct intel_crtc *crtc,
9306 			       struct intel_crtc_state *pipe_config)
9307 {
9308 	struct drm_device *dev = crtc->base.dev;
9309 	struct drm_i915_private *dev_priv = to_i915(dev);
9310 	enum pipe pipe = crtc->pipe;
9311 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
9312 	struct dpll clock;
9313 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
9314 	int refclk = 100000;
9315 
9316 	/* In case of DSI, DPLL will not be used */
9317 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9318 		return;
9319 
9320 	vlv_dpio_get(dev_priv);
9321 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9322 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9323 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9324 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
9325 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
9326 	vlv_dpio_put(dev_priv);
9327 
9328 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
9329 	clock.m2 = (pll_dw0 & 0xff) << 22;
9330 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9331 		clock.m2 |= pll_dw2 & 0x3fffff;
9332 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9333 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9334 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9335 
9336 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
9337 }
9338 
9339 static enum intel_output_format
9340 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
9341 {
9342 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9343 	u32 tmp;
9344 
9345 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
9346 
9347 	if (tmp & PIPEMISC_YUV420_ENABLE) {
9348 		/* We support 4:2:0 in full blend mode only */
9349 		drm_WARN_ON(&dev_priv->drm,
9350 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
9351 
9352 		return INTEL_OUTPUT_FORMAT_YCBCR420;
9353 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9354 		return INTEL_OUTPUT_FORMAT_YCBCR444;
9355 	} else {
9356 		return INTEL_OUTPUT_FORMAT_RGB;
9357 	}
9358 }
9359 
9360 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9361 {
9362 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9363 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9364 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9365 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9366 	u32 tmp;
9367 
9368 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9369 
9370 	if (tmp & DISPPLANE_GAMMA_ENABLE)
9371 		crtc_state->gamma_enable = true;
9372 
9373 	if (!HAS_GMCH(dev_priv) &&
9374 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
9375 		crtc_state->csc_enable = true;
9376 }
9377 
9378 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
9379 				 struct intel_crtc_state *pipe_config)
9380 {
9381 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9382 	enum intel_display_power_domain power_domain;
9383 	intel_wakeref_t wakeref;
9384 	u32 tmp;
9385 	bool ret;
9386 
9387 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9388 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9389 	if (!wakeref)
9390 		return false;
9391 
9392 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9393 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9394 	pipe_config->shared_dpll = NULL;
9395 
9396 	ret = false;
9397 
9398 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
9399 	if (!(tmp & PIPECONF_ENABLE))
9400 		goto out;
9401 
9402 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9403 	    IS_CHERRYVIEW(dev_priv)) {
9404 		switch (tmp & PIPECONF_BPC_MASK) {
9405 		case PIPECONF_6BPC:
9406 			pipe_config->pipe_bpp = 18;
9407 			break;
9408 		case PIPECONF_8BPC:
9409 			pipe_config->pipe_bpp = 24;
9410 			break;
9411 		case PIPECONF_10BPC:
9412 			pipe_config->pipe_bpp = 30;
9413 			break;
9414 		default:
9415 			break;
9416 		}
9417 	}
9418 
9419 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9420 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
9421 		pipe_config->limited_color_range = true;
9422 
9423 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9424 		PIPECONF_GAMMA_MODE_SHIFT;
9425 
9426 	if (IS_CHERRYVIEW(dev_priv))
9427 		pipe_config->cgm_mode = intel_de_read(dev_priv,
9428 						      CGM_PIPE_MODE(crtc->pipe));
9429 
9430 	i9xx_get_pipe_color_config(pipe_config);
9431 	intel_color_get_config(pipe_config);
9432 
9433 	if (INTEL_GEN(dev_priv) < 4)
9434 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9435 
9436 	intel_get_pipe_timings(crtc, pipe_config);
9437 	intel_get_pipe_src_size(crtc, pipe_config);
9438 
9439 	i9xx_get_pfit_config(pipe_config);
9440 
9441 	if (INTEL_GEN(dev_priv) >= 4) {
9442 		/* No way to read it out on pipes B and C */
9443 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
9444 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
9445 		else
9446 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
9447 		pipe_config->pixel_multiplier =
9448 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9449 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
9450 		pipe_config->dpll_hw_state.dpll_md = tmp;
9451 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
9452 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
9453 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
9454 		pipe_config->pixel_multiplier =
9455 			((tmp & SDVO_MULTIPLIER_MASK)
9456 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9457 	} else {
9458 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
9459 		 * port and will be fixed up in the encoder->get_config
9460 		 * function. */
9461 		pipe_config->pixel_multiplier = 1;
9462 	}
9463 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9464 							DPLL(crtc->pipe));
9465 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
9466 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9467 							       FP0(crtc->pipe));
9468 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9469 							       FP1(crtc->pipe));
9470 	} else {
9471 		/* Mask out read-only status bits. */
9472 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9473 						     DPLL_PORTC_READY_MASK |
9474 						     DPLL_PORTB_READY_MASK);
9475 	}
9476 
9477 	if (IS_CHERRYVIEW(dev_priv))
9478 		chv_crtc_clock_get(crtc, pipe_config);
9479 	else if (IS_VALLEYVIEW(dev_priv))
9480 		vlv_crtc_clock_get(crtc, pipe_config);
9481 	else
9482 		i9xx_crtc_clock_get(crtc, pipe_config);
9483 
9484 	/*
9485 	 * Normally the dotclock is filled in by the encoder .get_config()
9486 	 * but in case the pipe is enabled w/o any ports we need a sane
9487 	 * default.
9488 	 */
9489 	pipe_config->hw.adjusted_mode.crtc_clock =
9490 		pipe_config->port_clock / pipe_config->pixel_multiplier;
9491 
9492 	ret = true;
9493 
9494 out:
9495 	intel_display_power_put(dev_priv, power_domain, wakeref);
9496 
9497 	return ret;
9498 }
9499 
9500 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
9501 {
9502 	struct intel_encoder *encoder;
9503 	int i;
9504 	u32 val, final;
9505 	bool has_lvds = false;
9506 	bool has_cpu_edp = false;
9507 	bool has_panel = false;
9508 	bool has_ck505 = false;
9509 	bool can_ssc = false;
9510 	bool using_ssc_source = false;
9511 
9512 	/* We need to take the global config into account */
9513 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9514 		switch (encoder->type) {
9515 		case INTEL_OUTPUT_LVDS:
9516 			has_panel = true;
9517 			has_lvds = true;
9518 			break;
9519 		case INTEL_OUTPUT_EDP:
9520 			has_panel = true;
9521 			if (encoder->port == PORT_A)
9522 				has_cpu_edp = true;
9523 			break;
9524 		default:
9525 			break;
9526 		}
9527 	}
9528 
9529 	if (HAS_PCH_IBX(dev_priv)) {
9530 		has_ck505 = dev_priv->vbt.display_clock_mode;
9531 		can_ssc = has_ck505;
9532 	} else {
9533 		has_ck505 = false;
9534 		can_ssc = true;
9535 	}
9536 
9537 	/* Check if any DPLLs are using the SSC source */
9538 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
9539 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
9540 
9541 		if (!(temp & DPLL_VCO_ENABLE))
9542 			continue;
9543 
9544 		if ((temp & PLL_REF_INPUT_MASK) ==
9545 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9546 			using_ssc_source = true;
9547 			break;
9548 		}
9549 	}
9550 
9551 	drm_dbg_kms(&dev_priv->drm,
9552 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9553 		    has_panel, has_lvds, has_ck505, using_ssc_source);
9554 
9555 	/* Ironlake: try to setup display ref clock before DPLL
9556 	 * enabling. This is only under driver's control after
9557 	 * PCH B stepping, previous chipset stepping should be
9558 	 * ignoring this setting.
9559 	 */
9560 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
9561 
9562 	/* As we must carefully and slowly disable/enable each source in turn,
9563 	 * compute the final state we want first and check if we need to
9564 	 * make any changes at all.
9565 	 */
9566 	final = val;
9567 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
9568 	if (has_ck505)
9569 		final |= DREF_NONSPREAD_CK505_ENABLE;
9570 	else
9571 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
9572 
9573 	final &= ~DREF_SSC_SOURCE_MASK;
9574 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9575 	final &= ~DREF_SSC1_ENABLE;
9576 
9577 	if (has_panel) {
9578 		final |= DREF_SSC_SOURCE_ENABLE;
9579 
9580 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
9581 			final |= DREF_SSC1_ENABLE;
9582 
9583 		if (has_cpu_edp) {
9584 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
9585 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9586 			else
9587 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9588 		} else
9589 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9590 	} else if (using_ssc_source) {
9591 		final |= DREF_SSC_SOURCE_ENABLE;
9592 		final |= DREF_SSC1_ENABLE;
9593 	}
9594 
9595 	if (final == val)
9596 		return;
9597 
9598 	/* Always enable nonspread source */
9599 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
9600 
9601 	if (has_ck505)
9602 		val |= DREF_NONSPREAD_CK505_ENABLE;
9603 	else
9604 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
9605 
9606 	if (has_panel) {
9607 		val &= ~DREF_SSC_SOURCE_MASK;
9608 		val |= DREF_SSC_SOURCE_ENABLE;
9609 
9610 		/* SSC must be turned on before enabling the CPU output  */
9611 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9612 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
9613 			val |= DREF_SSC1_ENABLE;
9614 		} else
9615 			val &= ~DREF_SSC1_ENABLE;
9616 
9617 		/* Get SSC going before enabling the outputs */
9618 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9619 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9620 		udelay(200);
9621 
9622 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9623 
9624 		/* Enable CPU source on CPU attached eDP */
9625 		if (has_cpu_edp) {
9626 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9627 				drm_dbg_kms(&dev_priv->drm,
9628 					    "Using SSC on eDP\n");
9629 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9630 			} else
9631 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9632 		} else
9633 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9634 
9635 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9636 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9637 		udelay(200);
9638 	} else {
9639 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
9640 
9641 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9642 
9643 		/* Turn off CPU output */
9644 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9645 
9646 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9647 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9648 		udelay(200);
9649 
9650 		if (!using_ssc_source) {
9651 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
9652 
9653 			/* Turn off the SSC source */
9654 			val &= ~DREF_SSC_SOURCE_MASK;
9655 			val |= DREF_SSC_SOURCE_DISABLE;
9656 
9657 			/* Turn off SSC1 */
9658 			val &= ~DREF_SSC1_ENABLE;
9659 
9660 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9661 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9662 			udelay(200);
9663 		}
9664 	}
9665 
9666 	BUG_ON(val != final);
9667 }
9668 
9669 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9670 {
9671 	u32 tmp;
9672 
9673 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9674 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9675 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9676 
9677 	if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9678 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9679 		drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
9680 
9681 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9682 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9683 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9684 
9685 	if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9686 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9687 		drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
9688 }
9689 
9690 /* WaMPhyProgramming:hsw */
9691 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9692 {
9693 	u32 tmp;
9694 
9695 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9696 	tmp &= ~(0xFF << 24);
9697 	tmp |= (0x12 << 24);
9698 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9699 
9700 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9701 	tmp |= (1 << 11);
9702 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9703 
9704 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9705 	tmp |= (1 << 11);
9706 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9707 
9708 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9709 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9710 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9711 
9712 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9713 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9714 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9715 
9716 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9717 	tmp &= ~(7 << 13);
9718 	tmp |= (5 << 13);
9719 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9720 
9721 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9722 	tmp &= ~(7 << 13);
9723 	tmp |= (5 << 13);
9724 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9725 
9726 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9727 	tmp &= ~0xFF;
9728 	tmp |= 0x1C;
9729 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9730 
9731 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9732 	tmp &= ~0xFF;
9733 	tmp |= 0x1C;
9734 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9735 
9736 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9737 	tmp &= ~(0xFF << 16);
9738 	tmp |= (0x1C << 16);
9739 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9740 
9741 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9742 	tmp &= ~(0xFF << 16);
9743 	tmp |= (0x1C << 16);
9744 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9745 
9746 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9747 	tmp |= (1 << 27);
9748 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9749 
9750 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9751 	tmp |= (1 << 27);
9752 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
9753 
9754 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9755 	tmp &= ~(0xF << 28);
9756 	tmp |= (4 << 28);
9757 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
9758 
9759 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9760 	tmp &= ~(0xF << 28);
9761 	tmp |= (4 << 28);
9762 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
9763 }
9764 
9765 /* Implements 3 different sequences from BSpec chapter "Display iCLK
9766  * Programming" based on the parameters passed:
9767  * - Sequence to enable CLKOUT_DP
9768  * - Sequence to enable CLKOUT_DP without spread
9769  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9770  */
9771 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9772 				 bool with_spread, bool with_fdi)
9773 {
9774 	u32 reg, tmp;
9775 
9776 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
9777 		     "FDI requires downspread\n"))
9778 		with_spread = true;
9779 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
9780 		     with_fdi, "LP PCH doesn't have FDI\n"))
9781 		with_fdi = false;
9782 
9783 	mutex_lock(&dev_priv->sb_lock);
9784 
9785 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9786 	tmp &= ~SBI_SSCCTL_DISABLE;
9787 	tmp |= SBI_SSCCTL_PATHALT;
9788 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9789 
9790 	udelay(24);
9791 
9792 	if (with_spread) {
9793 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9794 		tmp &= ~SBI_SSCCTL_PATHALT;
9795 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9796 
9797 		if (with_fdi) {
9798 			lpt_reset_fdi_mphy(dev_priv);
9799 			lpt_program_fdi_mphy(dev_priv);
9800 		}
9801 	}
9802 
9803 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9804 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9805 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9806 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9807 
9808 	mutex_unlock(&dev_priv->sb_lock);
9809 }
9810 
9811 /* Sequence to disable CLKOUT_DP */
9812 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
9813 {
9814 	u32 reg, tmp;
9815 
9816 	mutex_lock(&dev_priv->sb_lock);
9817 
9818 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9819 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9820 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9821 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9822 
9823 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9824 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
9825 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
9826 			tmp |= SBI_SSCCTL_PATHALT;
9827 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9828 			udelay(32);
9829 		}
9830 		tmp |= SBI_SSCCTL_DISABLE;
9831 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9832 	}
9833 
9834 	mutex_unlock(&dev_priv->sb_lock);
9835 }
9836 
9837 #define BEND_IDX(steps) ((50 + (steps)) / 5)
9838 
9839 static const u16 sscdivintphase[] = {
9840 	[BEND_IDX( 50)] = 0x3B23,
9841 	[BEND_IDX( 45)] = 0x3B23,
9842 	[BEND_IDX( 40)] = 0x3C23,
9843 	[BEND_IDX( 35)] = 0x3C23,
9844 	[BEND_IDX( 30)] = 0x3D23,
9845 	[BEND_IDX( 25)] = 0x3D23,
9846 	[BEND_IDX( 20)] = 0x3E23,
9847 	[BEND_IDX( 15)] = 0x3E23,
9848 	[BEND_IDX( 10)] = 0x3F23,
9849 	[BEND_IDX(  5)] = 0x3F23,
9850 	[BEND_IDX(  0)] = 0x0025,
9851 	[BEND_IDX( -5)] = 0x0025,
9852 	[BEND_IDX(-10)] = 0x0125,
9853 	[BEND_IDX(-15)] = 0x0125,
9854 	[BEND_IDX(-20)] = 0x0225,
9855 	[BEND_IDX(-25)] = 0x0225,
9856 	[BEND_IDX(-30)] = 0x0325,
9857 	[BEND_IDX(-35)] = 0x0325,
9858 	[BEND_IDX(-40)] = 0x0425,
9859 	[BEND_IDX(-45)] = 0x0425,
9860 	[BEND_IDX(-50)] = 0x0525,
9861 };
9862 
9863 /*
9864  * Bend CLKOUT_DP
9865  * steps -50 to 50 inclusive, in steps of 5
9866  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9867  * change in clock period = -(steps / 10) * 5.787 ps
9868  */
9869 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9870 {
9871 	u32 tmp;
9872 	int idx = BEND_IDX(steps);
9873 
9874 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
9875 		return;
9876 
9877 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
9878 		return;
9879 
9880 	mutex_lock(&dev_priv->sb_lock);
9881 
9882 	if (steps % 10 != 0)
9883 		tmp = 0xAAAAAAAB;
9884 	else
9885 		tmp = 0x00000000;
9886 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9887 
9888 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9889 	tmp &= 0xffff0000;
9890 	tmp |= sscdivintphase[idx];
9891 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9892 
9893 	mutex_unlock(&dev_priv->sb_lock);
9894 }
9895 
9896 #undef BEND_IDX
9897 
9898 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
9899 {
9900 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9901 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
9902 
9903 	if ((ctl & SPLL_PLL_ENABLE) == 0)
9904 		return false;
9905 
9906 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
9907 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9908 		return true;
9909 
9910 	if (IS_BROADWELL(dev_priv) &&
9911 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
9912 		return true;
9913 
9914 	return false;
9915 }
9916 
9917 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
9918 			       enum intel_dpll_id id)
9919 {
9920 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9921 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
9922 
9923 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
9924 		return false;
9925 
9926 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
9927 		return true;
9928 
9929 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
9930 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
9931 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9932 		return true;
9933 
9934 	return false;
9935 }
9936 
9937 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
9938 {
9939 	struct intel_encoder *encoder;
9940 	bool has_fdi = false;
9941 
9942 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9943 		switch (encoder->type) {
9944 		case INTEL_OUTPUT_ANALOG:
9945 			has_fdi = true;
9946 			break;
9947 		default:
9948 			break;
9949 		}
9950 	}
9951 
9952 	/*
9953 	 * The BIOS may have decided to use the PCH SSC
9954 	 * reference so we must not disable it until the
9955 	 * relevant PLLs have stopped relying on it. We'll
9956 	 * just leave the PCH SSC reference enabled in case
9957 	 * any active PLL is using it. It will get disabled
9958 	 * after runtime suspend if we don't have FDI.
9959 	 *
9960 	 * TODO: Move the whole reference clock handling
9961 	 * to the modeset sequence proper so that we can
9962 	 * actually enable/disable/reconfigure these things
9963 	 * safely. To do that we need to introduce a real
9964 	 * clock hierarchy. That would also allow us to do
9965 	 * clock bending finally.
9966 	 */
9967 	dev_priv->pch_ssc_use = 0;
9968 
9969 	if (spll_uses_pch_ssc(dev_priv)) {
9970 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
9971 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
9972 	}
9973 
9974 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
9975 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
9976 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
9977 	}
9978 
9979 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
9980 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
9981 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
9982 	}
9983 
9984 	if (dev_priv->pch_ssc_use)
9985 		return;
9986 
9987 	if (has_fdi) {
9988 		lpt_bend_clkout_dp(dev_priv, 0);
9989 		lpt_enable_clkout_dp(dev_priv, true, true);
9990 	} else {
9991 		lpt_disable_clkout_dp(dev_priv);
9992 	}
9993 }
9994 
9995 /*
9996  * Initialize reference clocks when the driver loads
9997  */
9998 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
9999 {
10000 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
10001 		ilk_init_pch_refclk(dev_priv);
10002 	else if (HAS_PCH_LPT(dev_priv))
10003 		lpt_init_pch_refclk(dev_priv);
10004 }
10005 
10006 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
10007 {
10008 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10009 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10010 	enum pipe pipe = crtc->pipe;
10011 	u32 val;
10012 
10013 	val = 0;
10014 
10015 	switch (crtc_state->pipe_bpp) {
10016 	case 18:
10017 		val |= PIPECONF_6BPC;
10018 		break;
10019 	case 24:
10020 		val |= PIPECONF_8BPC;
10021 		break;
10022 	case 30:
10023 		val |= PIPECONF_10BPC;
10024 		break;
10025 	case 36:
10026 		val |= PIPECONF_12BPC;
10027 		break;
10028 	default:
10029 		/* Case prevented by intel_choose_pipe_bpp_dither. */
10030 		BUG();
10031 	}
10032 
10033 	if (crtc_state->dither)
10034 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10035 
10036 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10037 		val |= PIPECONF_INTERLACED_ILK;
10038 	else
10039 		val |= PIPECONF_PROGRESSIVE;
10040 
10041 	/*
10042 	 * This would end up with an odd purple hue over
10043 	 * the entire display. Make sure we don't do it.
10044 	 */
10045 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10046 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
10047 
10048 	if (crtc_state->limited_color_range)
10049 		val |= PIPECONF_COLOR_RANGE_SELECT;
10050 
10051 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10052 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10053 
10054 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10055 
10056 	val |= PIPECONF_FRAME_START_DELAY(0);
10057 
10058 	intel_de_write(dev_priv, PIPECONF(pipe), val);
10059 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
10060 }
10061 
10062 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
10063 {
10064 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10065 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10066 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
10067 	u32 val = 0;
10068 
10069 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
10070 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10071 
10072 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10073 		val |= PIPECONF_INTERLACED_ILK;
10074 	else
10075 		val |= PIPECONF_PROGRESSIVE;
10076 
10077 	if (IS_HASWELL(dev_priv) &&
10078 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10079 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10080 
10081 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10082 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
10083 }
10084 
10085 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
10086 {
10087 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10088 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10089 	u32 val = 0;
10090 
10091 	switch (crtc_state->pipe_bpp) {
10092 	case 18:
10093 		val |= PIPEMISC_DITHER_6_BPC;
10094 		break;
10095 	case 24:
10096 		val |= PIPEMISC_DITHER_8_BPC;
10097 		break;
10098 	case 30:
10099 		val |= PIPEMISC_DITHER_10_BPC;
10100 		break;
10101 	case 36:
10102 		val |= PIPEMISC_DITHER_12_BPC;
10103 		break;
10104 	default:
10105 		MISSING_CASE(crtc_state->pipe_bpp);
10106 		break;
10107 	}
10108 
10109 	if (crtc_state->dither)
10110 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
10111 
10112 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10113 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10114 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
10115 
10116 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10117 		val |= PIPEMISC_YUV420_ENABLE |
10118 			PIPEMISC_YUV420_MODE_FULL_BLEND;
10119 
10120 	if (INTEL_GEN(dev_priv) >= 11 &&
10121 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10122 					   BIT(PLANE_CURSOR))) == 0)
10123 		val |= PIPEMISC_HDR_MODE_PRECISION;
10124 
10125 	if (INTEL_GEN(dev_priv) >= 12)
10126 		val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10127 
10128 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
10129 }
10130 
10131 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10132 {
10133 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10134 	u32 tmp;
10135 
10136 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
10137 
10138 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10139 	case PIPEMISC_DITHER_6_BPC:
10140 		return 18;
10141 	case PIPEMISC_DITHER_8_BPC:
10142 		return 24;
10143 	case PIPEMISC_DITHER_10_BPC:
10144 		return 30;
10145 	case PIPEMISC_DITHER_12_BPC:
10146 		return 36;
10147 	default:
10148 		MISSING_CASE(tmp);
10149 		return 0;
10150 	}
10151 }
10152 
10153 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
10154 {
10155 	/*
10156 	 * Account for spread spectrum to avoid
10157 	 * oversubscribing the link. Max center spread
10158 	 * is 2.5%; use 5% for safety's sake.
10159 	 */
10160 	u32 bps = target_clock * bpp * 21 / 20;
10161 	return DIV_ROUND_UP(bps, link_bw * 8);
10162 }
10163 
10164 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
10165 {
10166 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
10167 }
10168 
10169 static void ilk_compute_dpll(struct intel_crtc *crtc,
10170 			     struct intel_crtc_state *crtc_state,
10171 			     struct dpll *reduced_clock)
10172 {
10173 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10174 	u32 dpll, fp, fp2;
10175 	int factor;
10176 
10177 	/* Enable autotuning of the PLL clock (if permissible) */
10178 	factor = 21;
10179 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10180 		if ((intel_panel_use_ssc(dev_priv) &&
10181 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
10182 		    (HAS_PCH_IBX(dev_priv) &&
10183 		     intel_is_dual_link_lvds(dev_priv)))
10184 			factor = 25;
10185 	} else if (crtc_state->sdvo_tv_clock) {
10186 		factor = 20;
10187 	}
10188 
10189 	fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10190 
10191 	if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
10192 		fp |= FP_CB_TUNE;
10193 
10194 	if (reduced_clock) {
10195 		fp2 = i9xx_dpll_compute_fp(reduced_clock);
10196 
10197 		if (reduced_clock->m < factor * reduced_clock->n)
10198 			fp2 |= FP_CB_TUNE;
10199 	} else {
10200 		fp2 = fp;
10201 	}
10202 
10203 	dpll = 0;
10204 
10205 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
10206 		dpll |= DPLLB_MODE_LVDS;
10207 	else
10208 		dpll |= DPLLB_MODE_DAC_SERIAL;
10209 
10210 	dpll |= (crtc_state->pixel_multiplier - 1)
10211 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
10212 
10213 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10214 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
10215 		dpll |= DPLL_SDVO_HIGH_SPEED;
10216 
10217 	if (intel_crtc_has_dp_encoder(crtc_state))
10218 		dpll |= DPLL_SDVO_HIGH_SPEED;
10219 
10220 	/*
10221 	 * The high speed IO clock is only really required for
10222 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10223 	 * possible to share the DPLL between CRT and HDMI. Enabling
10224 	 * the clock needlessly does no real harm, except use up a
10225 	 * bit of power potentially.
10226 	 *
10227 	 * We'll limit this to IVB with 3 pipes, since it has only two
10228 	 * DPLLs and so DPLL sharing is the only way to get three pipes
10229 	 * driving PCH ports at the same time. On SNB we could do this,
10230 	 * and potentially avoid enabling the second DPLL, but it's not
10231 	 * clear if it''s a win or loss power wise. No point in doing
10232 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10233 	 */
10234 	if (INTEL_NUM_PIPES(dev_priv) == 3 &&
10235 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10236 		dpll |= DPLL_SDVO_HIGH_SPEED;
10237 
10238 	/* compute bitmask from p1 value */
10239 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
10240 	/* also FPA1 */
10241 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
10242 
10243 	switch (crtc_state->dpll.p2) {
10244 	case 5:
10245 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10246 		break;
10247 	case 7:
10248 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10249 		break;
10250 	case 10:
10251 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10252 		break;
10253 	case 14:
10254 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10255 		break;
10256 	}
10257 
10258 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10259 	    intel_panel_use_ssc(dev_priv))
10260 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
10261 	else
10262 		dpll |= PLL_REF_INPUT_DREFCLK;
10263 
10264 	dpll |= DPLL_VCO_ENABLE;
10265 
10266 	crtc_state->dpll_hw_state.dpll = dpll;
10267 	crtc_state->dpll_hw_state.fp0 = fp;
10268 	crtc_state->dpll_hw_state.fp1 = fp2;
10269 }
10270 
10271 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10272 				  struct intel_crtc_state *crtc_state)
10273 {
10274 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10275 	struct intel_atomic_state *state =
10276 		to_intel_atomic_state(crtc_state->uapi.state);
10277 	const struct intel_limit *limit;
10278 	int refclk = 120000;
10279 
10280 	memset(&crtc_state->dpll_hw_state, 0,
10281 	       sizeof(crtc_state->dpll_hw_state));
10282 
10283 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10284 	if (!crtc_state->has_pch_encoder)
10285 		return 0;
10286 
10287 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10288 		if (intel_panel_use_ssc(dev_priv)) {
10289 			drm_dbg_kms(&dev_priv->drm,
10290 				    "using SSC reference clock of %d kHz\n",
10291 				    dev_priv->vbt.lvds_ssc_freq);
10292 			refclk = dev_priv->vbt.lvds_ssc_freq;
10293 		}
10294 
10295 		if (intel_is_dual_link_lvds(dev_priv)) {
10296 			if (refclk == 100000)
10297 				limit = &ilk_limits_dual_lvds_100m;
10298 			else
10299 				limit = &ilk_limits_dual_lvds;
10300 		} else {
10301 			if (refclk == 100000)
10302 				limit = &ilk_limits_single_lvds_100m;
10303 			else
10304 				limit = &ilk_limits_single_lvds;
10305 		}
10306 	} else {
10307 		limit = &ilk_limits_dac;
10308 	}
10309 
10310 	if (!crtc_state->clock_set &&
10311 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10312 				refclk, NULL, &crtc_state->dpll)) {
10313 		drm_err(&dev_priv->drm,
10314 			"Couldn't find PLL settings for mode!\n");
10315 		return -EINVAL;
10316 	}
10317 
10318 	ilk_compute_dpll(crtc, crtc_state, NULL);
10319 
10320 	if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
10321 		drm_dbg_kms(&dev_priv->drm,
10322 			    "failed to find PLL for pipe %c\n",
10323 			    pipe_name(crtc->pipe));
10324 		return -EINVAL;
10325 	}
10326 
10327 	return 0;
10328 }
10329 
10330 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10331 					 struct intel_link_m_n *m_n)
10332 {
10333 	struct drm_device *dev = crtc->base.dev;
10334 	struct drm_i915_private *dev_priv = to_i915(dev);
10335 	enum pipe pipe = crtc->pipe;
10336 
10337 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10338 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10339 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10340 		& ~TU_SIZE_MASK;
10341 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10342 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10343 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10344 }
10345 
10346 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10347 					 enum transcoder transcoder,
10348 					 struct intel_link_m_n *m_n,
10349 					 struct intel_link_m_n *m2_n2)
10350 {
10351 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10352 	enum pipe pipe = crtc->pipe;
10353 
10354 	if (INTEL_GEN(dev_priv) >= 5) {
10355 		m_n->link_m = intel_de_read(dev_priv,
10356 					    PIPE_LINK_M1(transcoder));
10357 		m_n->link_n = intel_de_read(dev_priv,
10358 					    PIPE_LINK_N1(transcoder));
10359 		m_n->gmch_m = intel_de_read(dev_priv,
10360 					    PIPE_DATA_M1(transcoder))
10361 			& ~TU_SIZE_MASK;
10362 		m_n->gmch_n = intel_de_read(dev_priv,
10363 					    PIPE_DATA_N1(transcoder));
10364 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
10365 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10366 
10367 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
10368 			m2_n2->link_m = intel_de_read(dev_priv,
10369 						      PIPE_LINK_M2(transcoder));
10370 			m2_n2->link_n =	intel_de_read(dev_priv,
10371 							     PIPE_LINK_N2(transcoder));
10372 			m2_n2->gmch_m =	intel_de_read(dev_priv,
10373 							     PIPE_DATA_M2(transcoder))
10374 					& ~TU_SIZE_MASK;
10375 			m2_n2->gmch_n =	intel_de_read(dev_priv,
10376 							     PIPE_DATA_N2(transcoder));
10377 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
10378 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10379 		}
10380 	} else {
10381 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10382 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10383 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10384 			& ~TU_SIZE_MASK;
10385 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10386 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10387 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10388 	}
10389 }
10390 
10391 void intel_dp_get_m_n(struct intel_crtc *crtc,
10392 		      struct intel_crtc_state *pipe_config)
10393 {
10394 	if (pipe_config->has_pch_encoder)
10395 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10396 	else
10397 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10398 					     &pipe_config->dp_m_n,
10399 					     &pipe_config->dp_m2_n2);
10400 }
10401 
10402 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10403 				   struct intel_crtc_state *pipe_config)
10404 {
10405 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10406 				     &pipe_config->fdi_m_n, NULL);
10407 }
10408 
10409 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
10410 				  u32 pos, u32 size)
10411 {
10412 	drm_rect_init(&crtc_state->pch_pfit.dst,
10413 		      pos >> 16, pos & 0xffff,
10414 		      size >> 16, size & 0xffff);
10415 }
10416 
10417 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
10418 {
10419 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10420 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10421 	struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
10422 	int id = -1;
10423 	int i;
10424 
10425 	/* find scaler attached to this pipe */
10426 	for (i = 0; i < crtc->num_scalers; i++) {
10427 		u32 ctl, pos, size;
10428 
10429 		ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10430 		if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
10431 			continue;
10432 
10433 		id = i;
10434 		crtc_state->pch_pfit.enabled = true;
10435 
10436 		pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
10437 		size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
10438 
10439 		ilk_get_pfit_pos_size(crtc_state, pos, size);
10440 
10441 		scaler_state->scalers[i].in_use = true;
10442 		break;
10443 	}
10444 
10445 	scaler_state->scaler_id = id;
10446 	if (id >= 0)
10447 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
10448 	else
10449 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
10450 }
10451 
10452 static void
10453 skl_get_initial_plane_config(struct intel_crtc *crtc,
10454 			     struct intel_initial_plane_config *plane_config)
10455 {
10456 	struct drm_device *dev = crtc->base.dev;
10457 	struct drm_i915_private *dev_priv = to_i915(dev);
10458 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10459 	enum plane_id plane_id = plane->id;
10460 	enum pipe pipe;
10461 	u32 val, base, offset, stride_mult, tiling, alpha;
10462 	int fourcc, pixel_format;
10463 	unsigned int aligned_height;
10464 	struct drm_framebuffer *fb;
10465 	struct intel_framebuffer *intel_fb;
10466 
10467 	if (!plane->get_hw_state(plane, &pipe))
10468 		return;
10469 
10470 	drm_WARN_ON(dev, pipe != crtc->pipe);
10471 
10472 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10473 	if (!intel_fb) {
10474 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
10475 		return;
10476 	}
10477 
10478 	fb = &intel_fb->base;
10479 
10480 	fb->dev = dev;
10481 
10482 	val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
10483 
10484 	if (INTEL_GEN(dev_priv) >= 11)
10485 		pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10486 	else
10487 		pixel_format = val & PLANE_CTL_FORMAT_MASK;
10488 
10489 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
10490 		alpha = intel_de_read(dev_priv,
10491 				      PLANE_COLOR_CTL(pipe, plane_id));
10492 		alpha &= PLANE_COLOR_ALPHA_MASK;
10493 	} else {
10494 		alpha = val & PLANE_CTL_ALPHA_MASK;
10495 	}
10496 
10497 	fourcc = skl_format_to_fourcc(pixel_format,
10498 				      val & PLANE_CTL_ORDER_RGBX, alpha);
10499 	fb->format = drm_format_info(fourcc);
10500 
10501 	tiling = val & PLANE_CTL_TILED_MASK;
10502 	switch (tiling) {
10503 	case PLANE_CTL_TILED_LINEAR:
10504 		fb->modifier = DRM_FORMAT_MOD_LINEAR;
10505 		break;
10506 	case PLANE_CTL_TILED_X:
10507 		plane_config->tiling = I915_TILING_X;
10508 		fb->modifier = I915_FORMAT_MOD_X_TILED;
10509 		break;
10510 	case PLANE_CTL_TILED_Y:
10511 		plane_config->tiling = I915_TILING_Y;
10512 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10513 			fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10514 				I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10515 				I915_FORMAT_MOD_Y_TILED_CCS;
10516 		else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10517 			fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
10518 		else
10519 			fb->modifier = I915_FORMAT_MOD_Y_TILED;
10520 		break;
10521 	case PLANE_CTL_TILED_YF:
10522 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10523 			fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10524 		else
10525 			fb->modifier = I915_FORMAT_MOD_Yf_TILED;
10526 		break;
10527 	default:
10528 		MISSING_CASE(tiling);
10529 		goto error;
10530 	}
10531 
10532 	/*
10533 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10534 	 * while i915 HW rotation is clockwise, thats why this swapping.
10535 	 */
10536 	switch (val & PLANE_CTL_ROTATE_MASK) {
10537 	case PLANE_CTL_ROTATE_0:
10538 		plane_config->rotation = DRM_MODE_ROTATE_0;
10539 		break;
10540 	case PLANE_CTL_ROTATE_90:
10541 		plane_config->rotation = DRM_MODE_ROTATE_270;
10542 		break;
10543 	case PLANE_CTL_ROTATE_180:
10544 		plane_config->rotation = DRM_MODE_ROTATE_180;
10545 		break;
10546 	case PLANE_CTL_ROTATE_270:
10547 		plane_config->rotation = DRM_MODE_ROTATE_90;
10548 		break;
10549 	}
10550 
10551 	if (INTEL_GEN(dev_priv) >= 10 &&
10552 	    val & PLANE_CTL_FLIP_HORIZONTAL)
10553 		plane_config->rotation |= DRM_MODE_REFLECT_X;
10554 
10555 	base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
10556 	plane_config->base = base;
10557 
10558 	offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
10559 
10560 	val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
10561 	fb->height = ((val >> 16) & 0xffff) + 1;
10562 	fb->width = ((val >> 0) & 0xffff) + 1;
10563 
10564 	val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
10565 	stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
10566 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
10567 
10568 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
10569 
10570 	plane_config->size = fb->pitches[0] * aligned_height;
10571 
10572 	drm_dbg_kms(&dev_priv->drm,
10573 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10574 		    crtc->base.name, plane->base.name, fb->width, fb->height,
10575 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
10576 		    plane_config->size);
10577 
10578 	plane_config->fb = intel_fb;
10579 	return;
10580 
10581 error:
10582 	kfree(intel_fb);
10583 }
10584 
10585 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
10586 {
10587 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10588 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10589 	u32 ctl, pos, size;
10590 
10591 	ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10592 	if ((ctl & PF_ENABLE) == 0)
10593 		return;
10594 
10595 	crtc_state->pch_pfit.enabled = true;
10596 
10597 	pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
10598 	size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
10599 
10600 	ilk_get_pfit_pos_size(crtc_state, pos, size);
10601 
10602 	/*
10603 	 * We currently do not free assignements of panel fitters on
10604 	 * ivb/hsw (since we don't use the higher upscaling modes which
10605 	 * differentiates them) so just WARN about this case for now.
10606 	 */
10607 	drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
10608 		    (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
10609 }
10610 
10611 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10612 				struct intel_crtc_state *pipe_config)
10613 {
10614 	struct drm_device *dev = crtc->base.dev;
10615 	struct drm_i915_private *dev_priv = to_i915(dev);
10616 	enum intel_display_power_domain power_domain;
10617 	intel_wakeref_t wakeref;
10618 	u32 tmp;
10619 	bool ret;
10620 
10621 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10622 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10623 	if (!wakeref)
10624 		return false;
10625 
10626 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10627 	pipe_config->shared_dpll = NULL;
10628 
10629 	ret = false;
10630 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
10631 	if (!(tmp & PIPECONF_ENABLE))
10632 		goto out;
10633 
10634 	switch (tmp & PIPECONF_BPC_MASK) {
10635 	case PIPECONF_6BPC:
10636 		pipe_config->pipe_bpp = 18;
10637 		break;
10638 	case PIPECONF_8BPC:
10639 		pipe_config->pipe_bpp = 24;
10640 		break;
10641 	case PIPECONF_10BPC:
10642 		pipe_config->pipe_bpp = 30;
10643 		break;
10644 	case PIPECONF_12BPC:
10645 		pipe_config->pipe_bpp = 36;
10646 		break;
10647 	default:
10648 		break;
10649 	}
10650 
10651 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10652 		pipe_config->limited_color_range = true;
10653 
10654 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10655 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10656 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10657 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10658 		break;
10659 	default:
10660 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10661 		break;
10662 	}
10663 
10664 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10665 		PIPECONF_GAMMA_MODE_SHIFT;
10666 
10667 	pipe_config->csc_mode = intel_de_read(dev_priv,
10668 					      PIPE_CSC_MODE(crtc->pipe));
10669 
10670 	i9xx_get_pipe_color_config(pipe_config);
10671 	intel_color_get_config(pipe_config);
10672 
10673 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
10674 		struct intel_shared_dpll *pll;
10675 		enum intel_dpll_id pll_id;
10676 
10677 		pipe_config->has_pch_encoder = true;
10678 
10679 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
10680 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10681 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
10682 
10683 		ilk_get_fdi_m_n_config(crtc, pipe_config);
10684 
10685 		if (HAS_PCH_IBX(dev_priv)) {
10686 			/*
10687 			 * The pipe->pch transcoder and pch transcoder->pll
10688 			 * mapping is fixed.
10689 			 */
10690 			pll_id = (enum intel_dpll_id) crtc->pipe;
10691 		} else {
10692 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
10693 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
10694 				pll_id = DPLL_ID_PCH_PLL_B;
10695 			else
10696 				pll_id= DPLL_ID_PCH_PLL_A;
10697 		}
10698 
10699 		pipe_config->shared_dpll =
10700 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
10701 		pll = pipe_config->shared_dpll;
10702 
10703 		drm_WARN_ON(dev, !pll->info->funcs->get_hw_state(dev_priv, pll,
10704 						 &pipe_config->dpll_hw_state));
10705 
10706 		tmp = pipe_config->dpll_hw_state.dpll;
10707 		pipe_config->pixel_multiplier =
10708 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10709 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10710 
10711 		ilk_pch_clock_get(crtc, pipe_config);
10712 	} else {
10713 		pipe_config->pixel_multiplier = 1;
10714 	}
10715 
10716 	intel_get_pipe_timings(crtc, pipe_config);
10717 	intel_get_pipe_src_size(crtc, pipe_config);
10718 
10719 	ilk_get_pfit_config(pipe_config);
10720 
10721 	ret = true;
10722 
10723 out:
10724 	intel_display_power_put(dev_priv, power_domain, wakeref);
10725 
10726 	return ret;
10727 }
10728 
10729 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
10730 				  struct intel_crtc_state *crtc_state)
10731 {
10732 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10733 	struct intel_atomic_state *state =
10734 		to_intel_atomic_state(crtc_state->uapi.state);
10735 
10736 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10737 	    INTEL_GEN(dev_priv) >= 11) {
10738 		struct intel_encoder *encoder =
10739 			intel_get_crtc_new_encoder(state, crtc_state);
10740 
10741 		if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10742 			drm_dbg_kms(&dev_priv->drm,
10743 				    "failed to find PLL for pipe %c\n",
10744 				    pipe_name(crtc->pipe));
10745 			return -EINVAL;
10746 		}
10747 	}
10748 
10749 	return 0;
10750 }
10751 
10752 static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10753 			    struct intel_crtc_state *pipe_config)
10754 {
10755 	enum intel_dpll_id id;
10756 	u32 temp;
10757 
10758 	temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
10759 	id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
10760 
10761 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
10762 		return;
10763 
10764 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10765 }
10766 
10767 static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10768 			    struct intel_crtc_state *pipe_config)
10769 {
10770 	enum phy phy = intel_port_to_phy(dev_priv, port);
10771 	enum icl_port_dpll_id port_dpll_id;
10772 	enum intel_dpll_id id;
10773 	u32 temp;
10774 
10775 	if (intel_phy_is_combo(dev_priv, phy)) {
10776 		temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) &
10777 			ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10778 		id = temp >> ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10779 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10780 	} else if (intel_phy_is_tc(dev_priv, phy)) {
10781 		u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
10782 
10783 		if (clk_sel == DDI_CLK_SEL_MG) {
10784 			id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
10785 								    port));
10786 			port_dpll_id = ICL_PORT_DPLL_MG_PHY;
10787 		} else {
10788 			drm_WARN_ON(&dev_priv->drm,
10789 				    clk_sel < DDI_CLK_SEL_TBT_162);
10790 			id = DPLL_ID_ICL_TBTPLL;
10791 			port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10792 		}
10793 	} else {
10794 		drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
10795 		return;
10796 	}
10797 
10798 	pipe_config->icl_port_dplls[port_dpll_id].pll =
10799 		intel_get_shared_dpll_by_id(dev_priv, id);
10800 
10801 	icl_set_active_port_dpll(pipe_config, port_dpll_id);
10802 }
10803 
10804 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10805 				enum port port,
10806 				struct intel_crtc_state *pipe_config)
10807 {
10808 	enum intel_dpll_id id;
10809 
10810 	switch (port) {
10811 	case PORT_A:
10812 		id = DPLL_ID_SKL_DPLL0;
10813 		break;
10814 	case PORT_B:
10815 		id = DPLL_ID_SKL_DPLL1;
10816 		break;
10817 	case PORT_C:
10818 		id = DPLL_ID_SKL_DPLL2;
10819 		break;
10820 	default:
10821 		drm_err(&dev_priv->drm, "Incorrect port type\n");
10822 		return;
10823 	}
10824 
10825 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10826 }
10827 
10828 static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10829 			    struct intel_crtc_state *pipe_config)
10830 {
10831 	enum intel_dpll_id id;
10832 	u32 temp;
10833 
10834 	temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10835 	id = temp >> (port * 3 + 1);
10836 
10837 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
10838 		return;
10839 
10840 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10841 }
10842 
10843 static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10844 			    struct intel_crtc_state *pipe_config)
10845 {
10846 	enum intel_dpll_id id;
10847 	u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
10848 
10849 	switch (ddi_pll_sel) {
10850 	case PORT_CLK_SEL_WRPLL1:
10851 		id = DPLL_ID_WRPLL1;
10852 		break;
10853 	case PORT_CLK_SEL_WRPLL2:
10854 		id = DPLL_ID_WRPLL2;
10855 		break;
10856 	case PORT_CLK_SEL_SPLL:
10857 		id = DPLL_ID_SPLL;
10858 		break;
10859 	case PORT_CLK_SEL_LCPLL_810:
10860 		id = DPLL_ID_LCPLL_810;
10861 		break;
10862 	case PORT_CLK_SEL_LCPLL_1350:
10863 		id = DPLL_ID_LCPLL_1350;
10864 		break;
10865 	case PORT_CLK_SEL_LCPLL_2700:
10866 		id = DPLL_ID_LCPLL_2700;
10867 		break;
10868 	default:
10869 		MISSING_CASE(ddi_pll_sel);
10870 		/* fall through */
10871 	case PORT_CLK_SEL_NONE:
10872 		return;
10873 	}
10874 
10875 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10876 }
10877 
10878 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10879 				     struct intel_crtc_state *pipe_config,
10880 				     u64 *power_domain_mask,
10881 				     intel_wakeref_t *wakerefs)
10882 {
10883 	struct drm_device *dev = crtc->base.dev;
10884 	struct drm_i915_private *dev_priv = to_i915(dev);
10885 	enum intel_display_power_domain power_domain;
10886 	unsigned long panel_transcoder_mask = 0;
10887 	unsigned long enabled_panel_transcoders = 0;
10888 	enum transcoder panel_transcoder;
10889 	intel_wakeref_t wf;
10890 	u32 tmp;
10891 
10892 	if (INTEL_GEN(dev_priv) >= 11)
10893 		panel_transcoder_mask |=
10894 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
10895 
10896 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP))
10897 		panel_transcoder_mask |= BIT(TRANSCODER_EDP);
10898 
10899 	/*
10900 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
10901 	 * and DSI transcoders handled below.
10902 	 */
10903 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10904 
10905 	/*
10906 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10907 	 * consistency and less surprising code; it's in always on power).
10908 	 */
10909 	for_each_set_bit(panel_transcoder,
10910 			 &panel_transcoder_mask,
10911 			 ARRAY_SIZE(INTEL_INFO(dev_priv)->trans_offsets)) {
10912 		bool force_thru = false;
10913 		enum pipe trans_pipe;
10914 
10915 		tmp = intel_de_read(dev_priv,
10916 				    TRANS_DDI_FUNC_CTL(panel_transcoder));
10917 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
10918 			continue;
10919 
10920 		/*
10921 		 * Log all enabled ones, only use the first one.
10922 		 *
10923 		 * FIXME: This won't work for two separate DSI displays.
10924 		 */
10925 		enabled_panel_transcoders |= BIT(panel_transcoder);
10926 		if (enabled_panel_transcoders != BIT(panel_transcoder))
10927 			continue;
10928 
10929 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10930 		default:
10931 			drm_WARN(dev, 1,
10932 				 "unknown pipe linked to transcoder %s\n",
10933 				 transcoder_name(panel_transcoder));
10934 			/* fall through */
10935 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
10936 			force_thru = true;
10937 			/* fall through */
10938 		case TRANS_DDI_EDP_INPUT_A_ON:
10939 			trans_pipe = PIPE_A;
10940 			break;
10941 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
10942 			trans_pipe = PIPE_B;
10943 			break;
10944 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
10945 			trans_pipe = PIPE_C;
10946 			break;
10947 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
10948 			trans_pipe = PIPE_D;
10949 			break;
10950 		}
10951 
10952 		if (trans_pipe == crtc->pipe) {
10953 			pipe_config->cpu_transcoder = panel_transcoder;
10954 			pipe_config->pch_pfit.force_thru = force_thru;
10955 		}
10956 	}
10957 
10958 	/*
10959 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
10960 	 */
10961 	drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
10962 		    enabled_panel_transcoders != BIT(TRANSCODER_EDP));
10963 
10964 	power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10965 	drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
10966 
10967 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10968 	if (!wf)
10969 		return false;
10970 
10971 	wakerefs[power_domain] = wf;
10972 	*power_domain_mask |= BIT_ULL(power_domain);
10973 
10974 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
10975 
10976 	return tmp & PIPECONF_ENABLE;
10977 }
10978 
10979 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10980 					 struct intel_crtc_state *pipe_config,
10981 					 u64 *power_domain_mask,
10982 					 intel_wakeref_t *wakerefs)
10983 {
10984 	struct drm_device *dev = crtc->base.dev;
10985 	struct drm_i915_private *dev_priv = to_i915(dev);
10986 	enum intel_display_power_domain power_domain;
10987 	enum transcoder cpu_transcoder;
10988 	intel_wakeref_t wf;
10989 	enum port port;
10990 	u32 tmp;
10991 
10992 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10993 		if (port == PORT_A)
10994 			cpu_transcoder = TRANSCODER_DSI_A;
10995 		else
10996 			cpu_transcoder = TRANSCODER_DSI_C;
10997 
10998 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10999 		drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
11000 
11001 		wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11002 		if (!wf)
11003 			continue;
11004 
11005 		wakerefs[power_domain] = wf;
11006 		*power_domain_mask |= BIT_ULL(power_domain);
11007 
11008 		/*
11009 		 * The PLL needs to be enabled with a valid divider
11010 		 * configuration, otherwise accessing DSI registers will hang
11011 		 * the machine. See BSpec North Display Engine
11012 		 * registers/MIPI[BXT]. We can break out here early, since we
11013 		 * need the same DSI PLL to be enabled for both DSI ports.
11014 		 */
11015 		if (!bxt_dsi_pll_is_enabled(dev_priv))
11016 			break;
11017 
11018 		/* XXX: this works for video mode only */
11019 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
11020 		if (!(tmp & DPI_ENABLE))
11021 			continue;
11022 
11023 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
11024 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11025 			continue;
11026 
11027 		pipe_config->cpu_transcoder = cpu_transcoder;
11028 		break;
11029 	}
11030 
11031 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
11032 }
11033 
11034 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11035 				   struct intel_crtc_state *pipe_config)
11036 {
11037 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11038 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
11039 	struct intel_shared_dpll *pll;
11040 	enum port port;
11041 	u32 tmp;
11042 
11043 	if (transcoder_is_dsi(cpu_transcoder)) {
11044 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11045 						PORT_A : PORT_B;
11046 	} else {
11047 		tmp = intel_de_read(dev_priv,
11048 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
11049 		if (INTEL_GEN(dev_priv) >= 12)
11050 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11051 		else
11052 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11053 	}
11054 
11055 	if (INTEL_GEN(dev_priv) >= 11)
11056 		icl_get_ddi_pll(dev_priv, port, pipe_config);
11057 	else if (IS_CANNONLAKE(dev_priv))
11058 		cnl_get_ddi_pll(dev_priv, port, pipe_config);
11059 	else if (IS_GEN9_BC(dev_priv))
11060 		skl_get_ddi_pll(dev_priv, port, pipe_config);
11061 	else if (IS_GEN9_LP(dev_priv))
11062 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
11063 	else
11064 		hsw_get_ddi_pll(dev_priv, port, pipe_config);
11065 
11066 	pll = pipe_config->shared_dpll;
11067 	if (pll) {
11068 		drm_WARN_ON(&dev_priv->drm,
11069 			    !pll->info->funcs->get_hw_state(dev_priv, pll,
11070 						&pipe_config->dpll_hw_state));
11071 	}
11072 
11073 	/*
11074 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11075 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
11076 	 * the PCH transcoder is on.
11077 	 */
11078 	if (INTEL_GEN(dev_priv) < 9 &&
11079 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
11080 		pipe_config->has_pch_encoder = true;
11081 
11082 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
11083 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11084 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
11085 
11086 		ilk_get_fdi_m_n_config(crtc, pipe_config);
11087 	}
11088 }
11089 
11090 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11091 				struct intel_crtc_state *pipe_config)
11092 {
11093 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11094 	intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
11095 	enum intel_display_power_domain power_domain;
11096 	u64 power_domain_mask;
11097 	bool active;
11098 	u32 tmp;
11099 
11100 	pipe_config->master_transcoder = INVALID_TRANSCODER;
11101 
11102 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
11103 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11104 	if (!wf)
11105 		return false;
11106 
11107 	wakerefs[power_domain] = wf;
11108 	power_domain_mask = BIT_ULL(power_domain);
11109 
11110 	pipe_config->shared_dpll = NULL;
11111 
11112 	active = hsw_get_transcoder_state(crtc, pipe_config,
11113 					  &power_domain_mask, wakerefs);
11114 
11115 	if (IS_GEN9_LP(dev_priv) &&
11116 	    bxt_get_dsi_transcoder_state(crtc, pipe_config,
11117 					 &power_domain_mask, wakerefs)) {
11118 		drm_WARN_ON(&dev_priv->drm, active);
11119 		active = true;
11120 	}
11121 
11122 	if (!active)
11123 		goto out;
11124 
11125 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
11126 	    INTEL_GEN(dev_priv) >= 11) {
11127 		hsw_get_ddi_port_state(crtc, pipe_config);
11128 		intel_get_pipe_timings(crtc, pipe_config);
11129 	}
11130 
11131 	intel_get_pipe_src_size(crtc, pipe_config);
11132 
11133 	if (IS_HASWELL(dev_priv)) {
11134 		u32 tmp = intel_de_read(dev_priv,
11135 					PIPECONF(pipe_config->cpu_transcoder));
11136 
11137 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11138 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11139 		else
11140 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11141 	} else {
11142 		pipe_config->output_format =
11143 			bdw_get_pipemisc_output_format(crtc);
11144 
11145 		/*
11146 		 * Currently there is no interface defined to
11147 		 * check user preference between RGB/YCBCR444
11148 		 * or YCBCR420. So the only possible case for
11149 		 * YCBCR444 usage is driving YCBCR420 output
11150 		 * with LSPCON, when pipe is configured for
11151 		 * YCBCR444 output and LSPCON takes care of
11152 		 * downsampling it.
11153 		 */
11154 		pipe_config->lspcon_downsampling =
11155 			pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444;
11156 	}
11157 
11158 	pipe_config->gamma_mode = intel_de_read(dev_priv,
11159 						GAMMA_MODE(crtc->pipe));
11160 
11161 	pipe_config->csc_mode = intel_de_read(dev_priv,
11162 					      PIPE_CSC_MODE(crtc->pipe));
11163 
11164 	if (INTEL_GEN(dev_priv) >= 9) {
11165 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
11166 
11167 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11168 			pipe_config->gamma_enable = true;
11169 
11170 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11171 			pipe_config->csc_enable = true;
11172 	} else {
11173 		i9xx_get_pipe_color_config(pipe_config);
11174 	}
11175 
11176 	intel_color_get_config(pipe_config);
11177 
11178 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11179 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11180 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11181 		pipe_config->ips_linetime =
11182 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11183 
11184 	power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
11185 	drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
11186 
11187 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11188 	if (wf) {
11189 		wakerefs[power_domain] = wf;
11190 		power_domain_mask |= BIT_ULL(power_domain);
11191 
11192 		if (INTEL_GEN(dev_priv) >= 9)
11193 			skl_get_pfit_config(pipe_config);
11194 		else
11195 			ilk_get_pfit_config(pipe_config);
11196 	}
11197 
11198 	if (hsw_crtc_supports_ips(crtc)) {
11199 		if (IS_HASWELL(dev_priv))
11200 			pipe_config->ips_enabled = intel_de_read(dev_priv,
11201 								 IPS_CTL) & IPS_ENABLE;
11202 		else {
11203 			/*
11204 			 * We cannot readout IPS state on broadwell, set to
11205 			 * true so we can set it to a defined state on first
11206 			 * commit.
11207 			 */
11208 			pipe_config->ips_enabled = true;
11209 		}
11210 	}
11211 
11212 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
11213 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
11214 		pipe_config->pixel_multiplier =
11215 			intel_de_read(dev_priv,
11216 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
11217 	} else {
11218 		pipe_config->pixel_multiplier = 1;
11219 	}
11220 
11221 out:
11222 	for_each_power_domain(power_domain, power_domain_mask)
11223 		intel_display_power_put(dev_priv,
11224 					power_domain, wakerefs[power_domain]);
11225 
11226 	return active;
11227 }
11228 
11229 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
11230 {
11231 	struct drm_i915_private *dev_priv =
11232 		to_i915(plane_state->uapi.plane->dev);
11233 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11234 	const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11235 	u32 base;
11236 
11237 	if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
11238 		base = sg_dma_address(obj->mm.pages->sgl);
11239 	else
11240 		base = intel_plane_ggtt_offset(plane_state);
11241 
11242 	return base + plane_state->color_plane[0].offset;
11243 }
11244 
11245 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11246 {
11247 	int x = plane_state->uapi.dst.x1;
11248 	int y = plane_state->uapi.dst.y1;
11249 	u32 pos = 0;
11250 
11251 	if (x < 0) {
11252 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11253 		x = -x;
11254 	}
11255 	pos |= x << CURSOR_X_SHIFT;
11256 
11257 	if (y < 0) {
11258 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11259 		y = -y;
11260 	}
11261 	pos |= y << CURSOR_Y_SHIFT;
11262 
11263 	return pos;
11264 }
11265 
11266 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11267 {
11268 	const struct drm_mode_config *config =
11269 		&plane_state->uapi.plane->dev->mode_config;
11270 	int width = drm_rect_width(&plane_state->uapi.dst);
11271 	int height = drm_rect_height(&plane_state->uapi.dst);
11272 
11273 	return width > 0 && width <= config->cursor_width &&
11274 		height > 0 && height <= config->cursor_height;
11275 }
11276 
11277 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
11278 {
11279 	struct drm_i915_private *dev_priv =
11280 		to_i915(plane_state->uapi.plane->dev);
11281 	unsigned int rotation = plane_state->hw.rotation;
11282 	int src_x, src_y;
11283 	u32 offset;
11284 	int ret;
11285 
11286 	ret = intel_plane_compute_gtt(plane_state);
11287 	if (ret)
11288 		return ret;
11289 
11290 	if (!plane_state->uapi.visible)
11291 		return 0;
11292 
11293 	src_x = plane_state->uapi.src.x1 >> 16;
11294 	src_y = plane_state->uapi.src.y1 >> 16;
11295 
11296 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11297 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11298 						    plane_state, 0);
11299 
11300 	if (src_x != 0 || src_y != 0) {
11301 		drm_dbg_kms(&dev_priv->drm,
11302 			    "Arbitrary cursor panning not supported\n");
11303 		return -EINVAL;
11304 	}
11305 
11306 	/*
11307 	 * Put the final coordinates back so that the src
11308 	 * coordinate checks will see the right values.
11309 	 */
11310 	drm_rect_translate_to(&plane_state->uapi.src,
11311 			      src_x << 16, src_y << 16);
11312 
11313 	/* ILK+ do this automagically in hardware */
11314 	if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
11315 		const struct drm_framebuffer *fb = plane_state->hw.fb;
11316 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11317 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
11318 
11319 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
11320 	}
11321 
11322 	plane_state->color_plane[0].offset = offset;
11323 	plane_state->color_plane[0].x = src_x;
11324 	plane_state->color_plane[0].y = src_y;
11325 
11326 	return 0;
11327 }
11328 
11329 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11330 			      struct intel_plane_state *plane_state)
11331 {
11332 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11333 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11334 	int ret;
11335 
11336 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
11337 		drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
11338 		return -EINVAL;
11339 	}
11340 
11341 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
11342 						  &crtc_state->uapi,
11343 						  DRM_PLANE_HELPER_NO_SCALING,
11344 						  DRM_PLANE_HELPER_NO_SCALING,
11345 						  true, true);
11346 	if (ret)
11347 		return ret;
11348 
11349 	/* Use the unclipped src/dst rectangles, which we program to hw */
11350 	plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11351 	plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
11352 
11353 	ret = intel_cursor_check_surface(plane_state);
11354 	if (ret)
11355 		return ret;
11356 
11357 	if (!plane_state->uapi.visible)
11358 		return 0;
11359 
11360 	ret = intel_plane_check_src_coordinates(plane_state);
11361 	if (ret)
11362 		return ret;
11363 
11364 	return 0;
11365 }
11366 
11367 static unsigned int
11368 i845_cursor_max_stride(struct intel_plane *plane,
11369 		       u32 pixel_format, u64 modifier,
11370 		       unsigned int rotation)
11371 {
11372 	return 2048;
11373 }
11374 
11375 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11376 {
11377 	u32 cntl = 0;
11378 
11379 	if (crtc_state->gamma_enable)
11380 		cntl |= CURSOR_GAMMA_ENABLE;
11381 
11382 	return cntl;
11383 }
11384 
11385 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11386 			   const struct intel_plane_state *plane_state)
11387 {
11388 	return CURSOR_ENABLE |
11389 		CURSOR_FORMAT_ARGB |
11390 		CURSOR_STRIDE(plane_state->color_plane[0].stride);
11391 }
11392 
11393 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11394 {
11395 	int width = drm_rect_width(&plane_state->uapi.dst);
11396 
11397 	/*
11398 	 * 845g/865g are only limited by the width of their cursors,
11399 	 * the height is arbitrary up to the precision of the register.
11400 	 */
11401 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
11402 }
11403 
11404 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
11405 			     struct intel_plane_state *plane_state)
11406 {
11407 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11408 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11409 	int ret;
11410 
11411 	ret = intel_check_cursor(crtc_state, plane_state);
11412 	if (ret)
11413 		return ret;
11414 
11415 	/* if we want to turn off the cursor ignore width and height */
11416 	if (!fb)
11417 		return 0;
11418 
11419 	/* Check for which cursor types we support */
11420 	if (!i845_cursor_size_ok(plane_state)) {
11421 		drm_dbg_kms(&i915->drm,
11422 			    "Cursor dimension %dx%d not supported\n",
11423 			    drm_rect_width(&plane_state->uapi.dst),
11424 			    drm_rect_height(&plane_state->uapi.dst));
11425 		return -EINVAL;
11426 	}
11427 
11428 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11429 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11430 
11431 	switch (fb->pitches[0]) {
11432 	case 256:
11433 	case 512:
11434 	case 1024:
11435 	case 2048:
11436 		break;
11437 	default:
11438 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11439 			     fb->pitches[0]);
11440 		return -EINVAL;
11441 	}
11442 
11443 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11444 
11445 	return 0;
11446 }
11447 
11448 static void i845_update_cursor(struct intel_plane *plane,
11449 			       const struct intel_crtc_state *crtc_state,
11450 			       const struct intel_plane_state *plane_state)
11451 {
11452 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11453 	u32 cntl = 0, base = 0, pos = 0, size = 0;
11454 	unsigned long irqflags;
11455 
11456 	if (plane_state && plane_state->uapi.visible) {
11457 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11458 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
11459 
11460 		cntl = plane_state->ctl |
11461 			i845_cursor_ctl_crtc(crtc_state);
11462 
11463 		size = (height << 12) | width;
11464 
11465 		base = intel_cursor_base(plane_state);
11466 		pos = intel_cursor_position(plane_state);
11467 	}
11468 
11469 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11470 
11471 	/* On these chipsets we can only modify the base/size/stride
11472 	 * whilst the cursor is disabled.
11473 	 */
11474 	if (plane->cursor.base != base ||
11475 	    plane->cursor.size != size ||
11476 	    plane->cursor.cntl != cntl) {
11477 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11478 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11479 		intel_de_write_fw(dev_priv, CURSIZE, size);
11480 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11481 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
11482 
11483 		plane->cursor.base = base;
11484 		plane->cursor.size = size;
11485 		plane->cursor.cntl = cntl;
11486 	} else {
11487 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11488 	}
11489 
11490 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11491 }
11492 
11493 static void i845_disable_cursor(struct intel_plane *plane,
11494 				const struct intel_crtc_state *crtc_state)
11495 {
11496 	i845_update_cursor(plane, crtc_state, NULL);
11497 }
11498 
11499 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11500 				     enum pipe *pipe)
11501 {
11502 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11503 	enum intel_display_power_domain power_domain;
11504 	intel_wakeref_t wakeref;
11505 	bool ret;
11506 
11507 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
11508 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11509 	if (!wakeref)
11510 		return false;
11511 
11512 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
11513 
11514 	*pipe = PIPE_A;
11515 
11516 	intel_display_power_put(dev_priv, power_domain, wakeref);
11517 
11518 	return ret;
11519 }
11520 
11521 static unsigned int
11522 i9xx_cursor_max_stride(struct intel_plane *plane,
11523 		       u32 pixel_format, u64 modifier,
11524 		       unsigned int rotation)
11525 {
11526 	return plane->base.dev->mode_config.cursor_width * 4;
11527 }
11528 
11529 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11530 {
11531 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11532 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11533 	u32 cntl = 0;
11534 
11535 	if (INTEL_GEN(dev_priv) >= 11)
11536 		return cntl;
11537 
11538 	if (crtc_state->gamma_enable)
11539 		cntl = MCURSOR_GAMMA_ENABLE;
11540 
11541 	if (crtc_state->csc_enable)
11542 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
11543 
11544 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11545 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
11546 
11547 	return cntl;
11548 }
11549 
11550 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11551 			   const struct intel_plane_state *plane_state)
11552 {
11553 	struct drm_i915_private *dev_priv =
11554 		to_i915(plane_state->uapi.plane->dev);
11555 	u32 cntl = 0;
11556 
11557 	if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11558 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11559 
11560 	switch (drm_rect_width(&plane_state->uapi.dst)) {
11561 	case 64:
11562 		cntl |= MCURSOR_MODE_64_ARGB_AX;
11563 		break;
11564 	case 128:
11565 		cntl |= MCURSOR_MODE_128_ARGB_AX;
11566 		break;
11567 	case 256:
11568 		cntl |= MCURSOR_MODE_256_ARGB_AX;
11569 		break;
11570 	default:
11571 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
11572 		return 0;
11573 	}
11574 
11575 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
11576 		cntl |= MCURSOR_ROTATE_180;
11577 
11578 	return cntl;
11579 }
11580 
11581 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
11582 {
11583 	struct drm_i915_private *dev_priv =
11584 		to_i915(plane_state->uapi.plane->dev);
11585 	int width = drm_rect_width(&plane_state->uapi.dst);
11586 	int height = drm_rect_height(&plane_state->uapi.dst);
11587 
11588 	if (!intel_cursor_size_ok(plane_state))
11589 		return false;
11590 
11591 	/* Cursor width is limited to a few power-of-two sizes */
11592 	switch (width) {
11593 	case 256:
11594 	case 128:
11595 	case 64:
11596 		break;
11597 	default:
11598 		return false;
11599 	}
11600 
11601 	/*
11602 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11603 	 * height from 8 lines up to the cursor width, when the
11604 	 * cursor is not rotated. Everything else requires square
11605 	 * cursors.
11606 	 */
11607 	if (HAS_CUR_FBC(dev_priv) &&
11608 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
11609 		if (height < 8 || height > width)
11610 			return false;
11611 	} else {
11612 		if (height != width)
11613 			return false;
11614 	}
11615 
11616 	return true;
11617 }
11618 
11619 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
11620 			     struct intel_plane_state *plane_state)
11621 {
11622 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11623 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11624 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11625 	enum pipe pipe = plane->pipe;
11626 	int ret;
11627 
11628 	ret = intel_check_cursor(crtc_state, plane_state);
11629 	if (ret)
11630 		return ret;
11631 
11632 	/* if we want to turn off the cursor ignore width and height */
11633 	if (!fb)
11634 		return 0;
11635 
11636 	/* Check for which cursor types we support */
11637 	if (!i9xx_cursor_size_ok(plane_state)) {
11638 		drm_dbg(&dev_priv->drm,
11639 			"Cursor dimension %dx%d not supported\n",
11640 			drm_rect_width(&plane_state->uapi.dst),
11641 			drm_rect_height(&plane_state->uapi.dst));
11642 		return -EINVAL;
11643 	}
11644 
11645 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
11646 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11647 
11648 	if (fb->pitches[0] !=
11649 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
11650 		drm_dbg_kms(&dev_priv->drm,
11651 			    "Invalid cursor stride (%u) (cursor width %d)\n",
11652 			    fb->pitches[0],
11653 			    drm_rect_width(&plane_state->uapi.dst));
11654 		return -EINVAL;
11655 	}
11656 
11657 	/*
11658 	 * There's something wrong with the cursor on CHV pipe C.
11659 	 * If it straddles the left edge of the screen then
11660 	 * moving it away from the edge or disabling it often
11661 	 * results in a pipe underrun, and often that can lead to
11662 	 * dead pipe (constant underrun reported, and it scans
11663 	 * out just a solid color). To recover from that, the
11664 	 * display power well must be turned off and on again.
11665 	 * Refuse the put the cursor into that compromised position.
11666 	 */
11667 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
11668 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
11669 		drm_dbg_kms(&dev_priv->drm,
11670 			    "CHV cursor C not allowed to straddle the left screen edge\n");
11671 		return -EINVAL;
11672 	}
11673 
11674 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
11675 
11676 	return 0;
11677 }
11678 
11679 static void i9xx_update_cursor(struct intel_plane *plane,
11680 			       const struct intel_crtc_state *crtc_state,
11681 			       const struct intel_plane_state *plane_state)
11682 {
11683 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11684 	enum pipe pipe = plane->pipe;
11685 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
11686 	unsigned long irqflags;
11687 
11688 	if (plane_state && plane_state->uapi.visible) {
11689 		unsigned width = drm_rect_width(&plane_state->uapi.dst);
11690 		unsigned height = drm_rect_height(&plane_state->uapi.dst);
11691 
11692 		cntl = plane_state->ctl |
11693 			i9xx_cursor_ctl_crtc(crtc_state);
11694 
11695 		if (width != height)
11696 			fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
11697 
11698 		base = intel_cursor_base(plane_state);
11699 		pos = intel_cursor_position(plane_state);
11700 	}
11701 
11702 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11703 
11704 	/*
11705 	 * On some platforms writing CURCNTR first will also
11706 	 * cause CURPOS to be armed by the CURBASE write.
11707 	 * Without the CURCNTR write the CURPOS write would
11708 	 * arm itself. Thus we always update CURCNTR before
11709 	 * CURPOS.
11710 	 *
11711 	 * On other platforms CURPOS always requires the
11712 	 * CURBASE write to arm the update. Additonally
11713 	 * a write to any of the cursor register will cancel
11714 	 * an already armed cursor update. Thus leaving out
11715 	 * the CURBASE write after CURPOS could lead to a
11716 	 * cursor that doesn't appear to move, or even change
11717 	 * shape. Thus we always write CURBASE.
11718 	 *
11719 	 * The other registers are armed by by the CURBASE write
11720 	 * except when the plane is getting enabled at which time
11721 	 * the CURCNTR write arms the update.
11722 	 */
11723 
11724 	if (INTEL_GEN(dev_priv) >= 9)
11725 		skl_write_cursor_wm(plane, crtc_state);
11726 
11727 	if (plane->cursor.base != base ||
11728 	    plane->cursor.size != fbc_ctl ||
11729 	    plane->cursor.cntl != cntl) {
11730 		if (HAS_CUR_FBC(dev_priv))
11731 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
11732 					  fbc_ctl);
11733 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
11734 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11735 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11736 
11737 		plane->cursor.base = base;
11738 		plane->cursor.size = fbc_ctl;
11739 		plane->cursor.cntl = cntl;
11740 	} else {
11741 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11742 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11743 	}
11744 
11745 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11746 }
11747 
11748 static void i9xx_disable_cursor(struct intel_plane *plane,
11749 				const struct intel_crtc_state *crtc_state)
11750 {
11751 	i9xx_update_cursor(plane, crtc_state, NULL);
11752 }
11753 
11754 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
11755 				     enum pipe *pipe)
11756 {
11757 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11758 	enum intel_display_power_domain power_domain;
11759 	intel_wakeref_t wakeref;
11760 	bool ret;
11761 	u32 val;
11762 
11763 	/*
11764 	 * Not 100% correct for planes that can move between pipes,
11765 	 * but that's only the case for gen2-3 which don't have any
11766 	 * display power wells.
11767 	 */
11768 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
11769 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11770 	if (!wakeref)
11771 		return false;
11772 
11773 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
11774 
11775 	ret = val & MCURSOR_MODE;
11776 
11777 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
11778 		*pipe = plane->pipe;
11779 	else
11780 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
11781 			MCURSOR_PIPE_SELECT_SHIFT;
11782 
11783 	intel_display_power_put(dev_priv, power_domain, wakeref);
11784 
11785 	return ret;
11786 }
11787 
11788 /* VESA 640x480x72Hz mode to set on the pipe */
11789 static const struct drm_display_mode load_detect_mode = {
11790 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11791 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11792 };
11793 
11794 struct drm_framebuffer *
11795 intel_framebuffer_create(struct drm_i915_gem_object *obj,
11796 			 struct drm_mode_fb_cmd2 *mode_cmd)
11797 {
11798 	struct intel_framebuffer *intel_fb;
11799 	int ret;
11800 
11801 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
11802 	if (!intel_fb)
11803 		return ERR_PTR(-ENOMEM);
11804 
11805 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
11806 	if (ret)
11807 		goto err;
11808 
11809 	return &intel_fb->base;
11810 
11811 err:
11812 	kfree(intel_fb);
11813 	return ERR_PTR(ret);
11814 }
11815 
11816 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
11817 					struct drm_crtc *crtc)
11818 {
11819 	struct drm_plane *plane;
11820 	struct drm_plane_state *plane_state;
11821 	int ret, i;
11822 
11823 	ret = drm_atomic_add_affected_planes(state, crtc);
11824 	if (ret)
11825 		return ret;
11826 
11827 	for_each_new_plane_in_state(state, plane, plane_state, i) {
11828 		if (plane_state->crtc != crtc)
11829 			continue;
11830 
11831 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
11832 		if (ret)
11833 			return ret;
11834 
11835 		drm_atomic_set_fb_for_plane(plane_state, NULL);
11836 	}
11837 
11838 	return 0;
11839 }
11840 
11841 int intel_get_load_detect_pipe(struct drm_connector *connector,
11842 			       struct intel_load_detect_pipe *old,
11843 			       struct drm_modeset_acquire_ctx *ctx)
11844 {
11845 	struct intel_crtc *intel_crtc;
11846 	struct intel_encoder *intel_encoder =
11847 		intel_attached_encoder(to_intel_connector(connector));
11848 	struct drm_crtc *possible_crtc;
11849 	struct drm_encoder *encoder = &intel_encoder->base;
11850 	struct drm_crtc *crtc = NULL;
11851 	struct drm_device *dev = encoder->dev;
11852 	struct drm_i915_private *dev_priv = to_i915(dev);
11853 	struct drm_mode_config *config = &dev->mode_config;
11854 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
11855 	struct drm_connector_state *connector_state;
11856 	struct intel_crtc_state *crtc_state;
11857 	int ret, i = -1;
11858 
11859 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
11860 		    connector->base.id, connector->name,
11861 		    encoder->base.id, encoder->name);
11862 
11863 	old->restore_state = NULL;
11864 
11865 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
11866 
11867 	/*
11868 	 * Algorithm gets a little messy:
11869 	 *
11870 	 *   - if the connector already has an assigned crtc, use it (but make
11871 	 *     sure it's on first)
11872 	 *
11873 	 *   - try to find the first unused crtc that can drive this connector,
11874 	 *     and use that if we find one
11875 	 */
11876 
11877 	/* See if we already have a CRTC for this connector */
11878 	if (connector->state->crtc) {
11879 		crtc = connector->state->crtc;
11880 
11881 		ret = drm_modeset_lock(&crtc->mutex, ctx);
11882 		if (ret)
11883 			goto fail;
11884 
11885 		/* Make sure the crtc and connector are running */
11886 		goto found;
11887 	}
11888 
11889 	/* Find an unused one (if possible) */
11890 	for_each_crtc(dev, possible_crtc) {
11891 		i++;
11892 		if (!(encoder->possible_crtcs & (1 << i)))
11893 			continue;
11894 
11895 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11896 		if (ret)
11897 			goto fail;
11898 
11899 		if (possible_crtc->state->enable) {
11900 			drm_modeset_unlock(&possible_crtc->mutex);
11901 			continue;
11902 		}
11903 
11904 		crtc = possible_crtc;
11905 		break;
11906 	}
11907 
11908 	/*
11909 	 * If we didn't find an unused CRTC, don't use any.
11910 	 */
11911 	if (!crtc) {
11912 		drm_dbg_kms(&dev_priv->drm,
11913 			    "no pipe available for load-detect\n");
11914 		ret = -ENODEV;
11915 		goto fail;
11916 	}
11917 
11918 found:
11919 	intel_crtc = to_intel_crtc(crtc);
11920 
11921 	state = drm_atomic_state_alloc(dev);
11922 	restore_state = drm_atomic_state_alloc(dev);
11923 	if (!state || !restore_state) {
11924 		ret = -ENOMEM;
11925 		goto fail;
11926 	}
11927 
11928 	state->acquire_ctx = ctx;
11929 	restore_state->acquire_ctx = ctx;
11930 
11931 	connector_state = drm_atomic_get_connector_state(state, connector);
11932 	if (IS_ERR(connector_state)) {
11933 		ret = PTR_ERR(connector_state);
11934 		goto fail;
11935 	}
11936 
11937 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11938 	if (ret)
11939 		goto fail;
11940 
11941 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11942 	if (IS_ERR(crtc_state)) {
11943 		ret = PTR_ERR(crtc_state);
11944 		goto fail;
11945 	}
11946 
11947 	crtc_state->uapi.active = true;
11948 
11949 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
11950 					   &load_detect_mode);
11951 	if (ret)
11952 		goto fail;
11953 
11954 	ret = intel_modeset_disable_planes(state, crtc);
11955 	if (ret)
11956 		goto fail;
11957 
11958 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11959 	if (!ret)
11960 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11961 	if (!ret)
11962 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
11963 	if (ret) {
11964 		drm_dbg_kms(&dev_priv->drm,
11965 			    "Failed to create a copy of old state to restore: %i\n",
11966 			    ret);
11967 		goto fail;
11968 	}
11969 
11970 	ret = drm_atomic_commit(state);
11971 	if (ret) {
11972 		drm_dbg_kms(&dev_priv->drm,
11973 			    "failed to set mode on load-detect pipe\n");
11974 		goto fail;
11975 	}
11976 
11977 	old->restore_state = restore_state;
11978 	drm_atomic_state_put(state);
11979 
11980 	/* let the connector get through one full cycle before testing */
11981 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
11982 	return true;
11983 
11984 fail:
11985 	if (state) {
11986 		drm_atomic_state_put(state);
11987 		state = NULL;
11988 	}
11989 	if (restore_state) {
11990 		drm_atomic_state_put(restore_state);
11991 		restore_state = NULL;
11992 	}
11993 
11994 	if (ret == -EDEADLK)
11995 		return ret;
11996 
11997 	return false;
11998 }
11999 
12000 void intel_release_load_detect_pipe(struct drm_connector *connector,
12001 				    struct intel_load_detect_pipe *old,
12002 				    struct drm_modeset_acquire_ctx *ctx)
12003 {
12004 	struct intel_encoder *intel_encoder =
12005 		intel_attached_encoder(to_intel_connector(connector));
12006 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
12007 	struct drm_encoder *encoder = &intel_encoder->base;
12008 	struct drm_atomic_state *state = old->restore_state;
12009 	int ret;
12010 
12011 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12012 		    connector->base.id, connector->name,
12013 		    encoder->base.id, encoder->name);
12014 
12015 	if (!state)
12016 		return;
12017 
12018 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
12019 	if (ret)
12020 		drm_dbg_kms(&i915->drm,
12021 			    "Couldn't release load detect pipe: %i\n", ret);
12022 	drm_atomic_state_put(state);
12023 }
12024 
12025 static int i9xx_pll_refclk(struct drm_device *dev,
12026 			   const struct intel_crtc_state *pipe_config)
12027 {
12028 	struct drm_i915_private *dev_priv = to_i915(dev);
12029 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12030 
12031 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
12032 		return dev_priv->vbt.lvds_ssc_freq;
12033 	else if (HAS_PCH_SPLIT(dev_priv))
12034 		return 120000;
12035 	else if (!IS_GEN(dev_priv, 2))
12036 		return 96000;
12037 	else
12038 		return 48000;
12039 }
12040 
12041 /* Returns the clock of the currently programmed mode of the given pipe. */
12042 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
12043 				struct intel_crtc_state *pipe_config)
12044 {
12045 	struct drm_device *dev = crtc->base.dev;
12046 	struct drm_i915_private *dev_priv = to_i915(dev);
12047 	enum pipe pipe = crtc->pipe;
12048 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12049 	u32 fp;
12050 	struct dpll clock;
12051 	int port_clock;
12052 	int refclk = i9xx_pll_refclk(dev, pipe_config);
12053 
12054 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
12055 		fp = pipe_config->dpll_hw_state.fp0;
12056 	else
12057 		fp = pipe_config->dpll_hw_state.fp1;
12058 
12059 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
12060 	if (IS_PINEVIEW(dev_priv)) {
12061 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12062 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
12063 	} else {
12064 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12065 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12066 	}
12067 
12068 	if (!IS_GEN(dev_priv, 2)) {
12069 		if (IS_PINEVIEW(dev_priv))
12070 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12071 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
12072 		else
12073 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
12074 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
12075 
12076 		switch (dpll & DPLL_MODE_MASK) {
12077 		case DPLLB_MODE_DAC_SERIAL:
12078 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12079 				5 : 10;
12080 			break;
12081 		case DPLLB_MODE_LVDS:
12082 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12083 				7 : 14;
12084 			break;
12085 		default:
12086 			drm_dbg_kms(&dev_priv->drm,
12087 				    "Unknown DPLL mode %08x in programmed "
12088 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
12089 			return;
12090 		}
12091 
12092 		if (IS_PINEVIEW(dev_priv))
12093 			port_clock = pnv_calc_dpll_params(refclk, &clock);
12094 		else
12095 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
12096 	} else {
12097 		u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12098 								 LVDS);
12099 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
12100 
12101 		if (is_lvds) {
12102 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12103 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
12104 
12105 			if (lvds & LVDS_CLKB_POWER_UP)
12106 				clock.p2 = 7;
12107 			else
12108 				clock.p2 = 14;
12109 		} else {
12110 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
12111 				clock.p1 = 2;
12112 			else {
12113 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12114 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12115 			}
12116 			if (dpll & PLL_P2_DIVIDE_BY_4)
12117 				clock.p2 = 4;
12118 			else
12119 				clock.p2 = 2;
12120 		}
12121 
12122 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
12123 	}
12124 
12125 	/*
12126 	 * This value includes pixel_multiplier. We will use
12127 	 * port_clock to compute adjusted_mode.crtc_clock in the
12128 	 * encoder's get_config() function.
12129 	 */
12130 	pipe_config->port_clock = port_clock;
12131 }
12132 
12133 int intel_dotclock_calculate(int link_freq,
12134 			     const struct intel_link_m_n *m_n)
12135 {
12136 	/*
12137 	 * The calculation for the data clock is:
12138 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
12139 	 * But we want to avoid losing precison if possible, so:
12140 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
12141 	 *
12142 	 * and the link clock is simpler:
12143 	 * link_clock = (m * link_clock) / n
12144 	 */
12145 
12146 	if (!m_n->link_n)
12147 		return 0;
12148 
12149 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
12150 }
12151 
12152 static void ilk_pch_clock_get(struct intel_crtc *crtc,
12153 			      struct intel_crtc_state *pipe_config)
12154 {
12155 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12156 
12157 	/* read out port_clock from the DPLL */
12158 	i9xx_crtc_clock_get(crtc, pipe_config);
12159 
12160 	/*
12161 	 * In case there is an active pipe without active ports,
12162 	 * we may need some idea for the dotclock anyway.
12163 	 * Calculate one based on the FDI configuration.
12164 	 */
12165 	pipe_config->hw.adjusted_mode.crtc_clock =
12166 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12167 					 &pipe_config->fdi_m_n);
12168 }
12169 
12170 static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12171 				   struct intel_crtc *crtc)
12172 {
12173 	memset(crtc_state, 0, sizeof(*crtc_state));
12174 
12175 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12176 
12177 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12178 	crtc_state->master_transcoder = INVALID_TRANSCODER;
12179 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12180 	crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12181 	crtc_state->scaler_state.scaler_id = -1;
12182 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
12183 }
12184 
12185 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12186 {
12187 	struct intel_crtc_state *crtc_state;
12188 
12189 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12190 
12191 	if (crtc_state)
12192 		intel_crtc_state_reset(crtc_state, crtc);
12193 
12194 	return crtc_state;
12195 }
12196 
12197 /* Returns the currently programmed mode of the given encoder. */
12198 struct drm_display_mode *
12199 intel_encoder_current_mode(struct intel_encoder *encoder)
12200 {
12201 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12202 	struct intel_crtc_state *crtc_state;
12203 	struct drm_display_mode *mode;
12204 	struct intel_crtc *crtc;
12205 	enum pipe pipe;
12206 
12207 	if (!encoder->get_hw_state(encoder, &pipe))
12208 		return NULL;
12209 
12210 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12211 
12212 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12213 	if (!mode)
12214 		return NULL;
12215 
12216 	crtc_state = intel_crtc_state_alloc(crtc);
12217 	if (!crtc_state) {
12218 		kfree(mode);
12219 		return NULL;
12220 	}
12221 
12222 	if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
12223 		kfree(crtc_state);
12224 		kfree(mode);
12225 		return NULL;
12226 	}
12227 
12228 	encoder->get_config(encoder, crtc_state);
12229 
12230 	intel_mode_from_pipe_config(mode, crtc_state);
12231 
12232 	kfree(crtc_state);
12233 
12234 	return mode;
12235 }
12236 
12237 static void intel_crtc_destroy(struct drm_crtc *crtc)
12238 {
12239 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12240 
12241 	drm_crtc_cleanup(crtc);
12242 	kfree(intel_crtc);
12243 }
12244 
12245 /**
12246  * intel_wm_need_update - Check whether watermarks need updating
12247  * @cur: current plane state
12248  * @new: new plane state
12249  *
12250  * Check current plane state versus the new one to determine whether
12251  * watermarks need to be recalculated.
12252  *
12253  * Returns true or false.
12254  */
12255 static bool intel_wm_need_update(const struct intel_plane_state *cur,
12256 				 struct intel_plane_state *new)
12257 {
12258 	/* Update watermarks on tiling or size changes. */
12259 	if (new->uapi.visible != cur->uapi.visible)
12260 		return true;
12261 
12262 	if (!cur->hw.fb || !new->hw.fb)
12263 		return false;
12264 
12265 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12266 	    cur->hw.rotation != new->hw.rotation ||
12267 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12268 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12269 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12270 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
12271 		return true;
12272 
12273 	return false;
12274 }
12275 
12276 static bool needs_scaling(const struct intel_plane_state *state)
12277 {
12278 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
12279 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
12280 	int dst_w = drm_rect_width(&state->uapi.dst);
12281 	int dst_h = drm_rect_height(&state->uapi.dst);
12282 
12283 	return (src_w != dst_w || src_h != dst_h);
12284 }
12285 
12286 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
12287 				    struct intel_crtc_state *crtc_state,
12288 				    const struct intel_plane_state *old_plane_state,
12289 				    struct intel_plane_state *plane_state)
12290 {
12291 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12292 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12293 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12294 	bool mode_changed = needs_modeset(crtc_state);
12295 	bool was_crtc_enabled = old_crtc_state->hw.active;
12296 	bool is_crtc_enabled = crtc_state->hw.active;
12297 	bool turn_off, turn_on, visible, was_visible;
12298 	int ret;
12299 
12300 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
12301 		ret = skl_update_scaler_plane(crtc_state, plane_state);
12302 		if (ret)
12303 			return ret;
12304 	}
12305 
12306 	was_visible = old_plane_state->uapi.visible;
12307 	visible = plane_state->uapi.visible;
12308 
12309 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
12310 		was_visible = false;
12311 
12312 	/*
12313 	 * Visibility is calculated as if the crtc was on, but
12314 	 * after scaler setup everything depends on it being off
12315 	 * when the crtc isn't active.
12316 	 *
12317 	 * FIXME this is wrong for watermarks. Watermarks should also
12318 	 * be computed as if the pipe would be active. Perhaps move
12319 	 * per-plane wm computation to the .check_plane() hook, and
12320 	 * only combine the results from all planes in the current place?
12321 	 */
12322 	if (!is_crtc_enabled) {
12323 		intel_plane_set_invisible(crtc_state, plane_state);
12324 		visible = false;
12325 	}
12326 
12327 	if (!was_visible && !visible)
12328 		return 0;
12329 
12330 	turn_off = was_visible && (!visible || mode_changed);
12331 	turn_on = visible && (!was_visible || mode_changed);
12332 
12333 	drm_dbg_atomic(&dev_priv->drm,
12334 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12335 		       crtc->base.base.id, crtc->base.name,
12336 		       plane->base.base.id, plane->base.name,
12337 		       was_visible, visible,
12338 		       turn_off, turn_on, mode_changed);
12339 
12340 	if (turn_on) {
12341 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12342 			crtc_state->update_wm_pre = true;
12343 
12344 		/* must disable cxsr around plane enable/disable */
12345 		if (plane->id != PLANE_CURSOR)
12346 			crtc_state->disable_cxsr = true;
12347 	} else if (turn_off) {
12348 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12349 			crtc_state->update_wm_post = true;
12350 
12351 		/* must disable cxsr around plane enable/disable */
12352 		if (plane->id != PLANE_CURSOR)
12353 			crtc_state->disable_cxsr = true;
12354 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
12355 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
12356 			/* FIXME bollocks */
12357 			crtc_state->update_wm_pre = true;
12358 			crtc_state->update_wm_post = true;
12359 		}
12360 	}
12361 
12362 	if (visible || was_visible)
12363 		crtc_state->fb_bits |= plane->frontbuffer_bit;
12364 
12365 	/*
12366 	 * ILK/SNB DVSACNTR/Sprite Enable
12367 	 * IVB SPR_CTL/Sprite Enable
12368 	 * "When in Self Refresh Big FIFO mode, a write to enable the
12369 	 *  plane will be internally buffered and delayed while Big FIFO
12370 	 *  mode is exiting."
12371 	 *
12372 	 * Which means that enabling the sprite can take an extra frame
12373 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
12374 	 * down to LP0 and wait for vblank in order to make sure the
12375 	 * sprite gets enabled on the next vblank after the register write.
12376 	 * Doing otherwise would risk enabling the sprite one frame after
12377 	 * we've already signalled flip completion. We can resume LP1+
12378 	 * once the sprite has been enabled.
12379 	 *
12380 	 *
12381 	 * WaCxSRDisabledForSpriteScaling:ivb
12382 	 * IVB SPR_SCALE/Scaling Enable
12383 	 * "Low Power watermarks must be disabled for at least one
12384 	 *  frame before enabling sprite scaling, and kept disabled
12385 	 *  until sprite scaling is disabled."
12386 	 *
12387 	 * ILK/SNB DVSASCALE/Scaling Enable
12388 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
12389 	 *  masked off while Big FIFO mode is exiting."
12390 	 *
12391 	 * Despite the w/a only being listed for IVB we assume that
12392 	 * the ILK/SNB note has similar ramifications, hence we apply
12393 	 * the w/a on all three platforms.
12394 	 *
12395 	 * With experimental results seems this is needed also for primary
12396 	 * plane, not only sprite plane.
12397 	 */
12398 	if (plane->id != PLANE_CURSOR &&
12399 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
12400 	     IS_IVYBRIDGE(dev_priv)) &&
12401 	    (turn_on || (!needs_scaling(old_plane_state) &&
12402 			 needs_scaling(plane_state))))
12403 		crtc_state->disable_lp_wm = true;
12404 
12405 	return 0;
12406 }
12407 
12408 static bool encoders_cloneable(const struct intel_encoder *a,
12409 			       const struct intel_encoder *b)
12410 {
12411 	/* masks could be asymmetric, so check both ways */
12412 	return a == b || (a->cloneable & (1 << b->type) &&
12413 			  b->cloneable & (1 << a->type));
12414 }
12415 
12416 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12417 					 struct intel_crtc *crtc,
12418 					 struct intel_encoder *encoder)
12419 {
12420 	struct intel_encoder *source_encoder;
12421 	struct drm_connector *connector;
12422 	struct drm_connector_state *connector_state;
12423 	int i;
12424 
12425 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12426 		if (connector_state->crtc != &crtc->base)
12427 			continue;
12428 
12429 		source_encoder =
12430 			to_intel_encoder(connector_state->best_encoder);
12431 		if (!encoders_cloneable(encoder, source_encoder))
12432 			return false;
12433 	}
12434 
12435 	return true;
12436 }
12437 
12438 static int icl_add_linked_planes(struct intel_atomic_state *state)
12439 {
12440 	struct intel_plane *plane, *linked;
12441 	struct intel_plane_state *plane_state, *linked_plane_state;
12442 	int i;
12443 
12444 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12445 		linked = plane_state->planar_linked_plane;
12446 
12447 		if (!linked)
12448 			continue;
12449 
12450 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
12451 		if (IS_ERR(linked_plane_state))
12452 			return PTR_ERR(linked_plane_state);
12453 
12454 		drm_WARN_ON(state->base.dev,
12455 			    linked_plane_state->planar_linked_plane != plane);
12456 		drm_WARN_ON(state->base.dev,
12457 			    linked_plane_state->planar_slave == plane_state->planar_slave);
12458 	}
12459 
12460 	return 0;
12461 }
12462 
12463 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12464 {
12465 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12466 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12467 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
12468 	struct intel_plane *plane, *linked;
12469 	struct intel_plane_state *plane_state;
12470 	int i;
12471 
12472 	if (INTEL_GEN(dev_priv) < 11)
12473 		return 0;
12474 
12475 	/*
12476 	 * Destroy all old plane links and make the slave plane invisible
12477 	 * in the crtc_state->active_planes mask.
12478 	 */
12479 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12480 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
12481 			continue;
12482 
12483 		plane_state->planar_linked_plane = NULL;
12484 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
12485 			crtc_state->active_planes &= ~BIT(plane->id);
12486 			crtc_state->update_planes |= BIT(plane->id);
12487 		}
12488 
12489 		plane_state->planar_slave = false;
12490 	}
12491 
12492 	if (!crtc_state->nv12_planes)
12493 		return 0;
12494 
12495 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12496 		struct intel_plane_state *linked_state = NULL;
12497 
12498 		if (plane->pipe != crtc->pipe ||
12499 		    !(crtc_state->nv12_planes & BIT(plane->id)))
12500 			continue;
12501 
12502 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
12503 			if (!icl_is_nv12_y_plane(linked->id))
12504 				continue;
12505 
12506 			if (crtc_state->active_planes & BIT(linked->id))
12507 				continue;
12508 
12509 			linked_state = intel_atomic_get_plane_state(state, linked);
12510 			if (IS_ERR(linked_state))
12511 				return PTR_ERR(linked_state);
12512 
12513 			break;
12514 		}
12515 
12516 		if (!linked_state) {
12517 			drm_dbg_kms(&dev_priv->drm,
12518 				    "Need %d free Y planes for planar YUV\n",
12519 				    hweight8(crtc_state->nv12_planes));
12520 
12521 			return -EINVAL;
12522 		}
12523 
12524 		plane_state->planar_linked_plane = linked;
12525 
12526 		linked_state->planar_slave = true;
12527 		linked_state->planar_linked_plane = plane;
12528 		crtc_state->active_planes |= BIT(linked->id);
12529 		crtc_state->update_planes |= BIT(linked->id);
12530 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12531 			    linked->base.name, plane->base.name);
12532 
12533 		/* Copy parameters to slave plane */
12534 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12535 		linked_state->color_ctl = plane_state->color_ctl;
12536 		linked_state->view = plane_state->view;
12537 		memcpy(linked_state->color_plane, plane_state->color_plane,
12538 		       sizeof(linked_state->color_plane));
12539 
12540 		intel_plane_copy_uapi_to_hw_state(linked_state, plane_state);
12541 		linked_state->uapi.src = plane_state->uapi.src;
12542 		linked_state->uapi.dst = plane_state->uapi.dst;
12543 
12544 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
12545 			if (linked->id == PLANE_SPRITE5)
12546 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12547 			else if (linked->id == PLANE_SPRITE4)
12548 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
12549 			else
12550 				MISSING_CASE(linked->id);
12551 		}
12552 	}
12553 
12554 	return 0;
12555 }
12556 
12557 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12558 {
12559 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
12560 	struct intel_atomic_state *state =
12561 		to_intel_atomic_state(new_crtc_state->uapi.state);
12562 	const struct intel_crtc_state *old_crtc_state =
12563 		intel_atomic_get_old_crtc_state(state, crtc);
12564 
12565 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12566 }
12567 
12568 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12569 {
12570 	const struct drm_display_mode *adjusted_mode =
12571 		&crtc_state->hw.adjusted_mode;
12572 
12573 	if (!crtc_state->hw.enable)
12574 		return 0;
12575 
12576 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12577 				 adjusted_mode->crtc_clock);
12578 }
12579 
12580 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12581 			       const struct intel_cdclk_state *cdclk_state)
12582 {
12583 	const struct drm_display_mode *adjusted_mode =
12584 		&crtc_state->hw.adjusted_mode;
12585 
12586 	if (!crtc_state->hw.enable)
12587 		return 0;
12588 
12589 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12590 				 cdclk_state->logical.cdclk);
12591 }
12592 
12593 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12594 {
12595 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12596 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12597 	const struct drm_display_mode *adjusted_mode =
12598 		&crtc_state->hw.adjusted_mode;
12599 	u16 linetime_wm;
12600 
12601 	if (!crtc_state->hw.enable)
12602 		return 0;
12603 
12604 	linetime_wm = DIV_ROUND_UP(adjusted_mode->crtc_htotal * 1000 * 8,
12605 				   crtc_state->pixel_rate);
12606 
12607 	/* Display WA #1135: BXT:ALL GLK:ALL */
12608 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12609 		linetime_wm /= 2;
12610 
12611 	return linetime_wm;
12612 }
12613 
12614 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12615 				   struct intel_crtc *crtc)
12616 {
12617 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12618 	struct intel_crtc_state *crtc_state =
12619 		intel_atomic_get_new_crtc_state(state, crtc);
12620 	const struct intel_cdclk_state *cdclk_state;
12621 
12622 	if (INTEL_GEN(dev_priv) >= 9)
12623 		crtc_state->linetime = skl_linetime_wm(crtc_state);
12624 	else
12625 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
12626 
12627 	if (!hsw_crtc_supports_ips(crtc))
12628 		return 0;
12629 
12630 	cdclk_state = intel_atomic_get_cdclk_state(state);
12631 	if (IS_ERR(cdclk_state))
12632 		return PTR_ERR(cdclk_state);
12633 
12634 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
12635 						       cdclk_state);
12636 
12637 	return 0;
12638 }
12639 
12640 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
12641 				   struct intel_crtc *crtc)
12642 {
12643 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12644 	struct intel_crtc_state *crtc_state =
12645 		intel_atomic_get_new_crtc_state(state, crtc);
12646 	bool mode_changed = needs_modeset(crtc_state);
12647 	int ret;
12648 
12649 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
12650 	    mode_changed && !crtc_state->hw.active)
12651 		crtc_state->update_wm_post = true;
12652 
12653 	if (mode_changed && crtc_state->hw.enable &&
12654 	    dev_priv->display.crtc_compute_clock &&
12655 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
12656 		ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
12657 		if (ret)
12658 			return ret;
12659 	}
12660 
12661 	/*
12662 	 * May need to update pipe gamma enable bits
12663 	 * when C8 planes are getting enabled/disabled.
12664 	 */
12665 	if (c8_planes_changed(crtc_state))
12666 		crtc_state->uapi.color_mgmt_changed = true;
12667 
12668 	if (mode_changed || crtc_state->update_pipe ||
12669 	    crtc_state->uapi.color_mgmt_changed) {
12670 		ret = intel_color_check(crtc_state);
12671 		if (ret)
12672 			return ret;
12673 	}
12674 
12675 	if (dev_priv->display.compute_pipe_wm) {
12676 		ret = dev_priv->display.compute_pipe_wm(crtc_state);
12677 		if (ret) {
12678 			drm_dbg_kms(&dev_priv->drm,
12679 				    "Target pipe watermarks are invalid\n");
12680 			return ret;
12681 		}
12682 	}
12683 
12684 	if (dev_priv->display.compute_intermediate_wm) {
12685 		if (drm_WARN_ON(&dev_priv->drm,
12686 				!dev_priv->display.compute_pipe_wm))
12687 			return 0;
12688 
12689 		/*
12690 		 * Calculate 'intermediate' watermarks that satisfy both the
12691 		 * old state and the new state.  We can program these
12692 		 * immediately.
12693 		 */
12694 		ret = dev_priv->display.compute_intermediate_wm(crtc_state);
12695 		if (ret) {
12696 			drm_dbg_kms(&dev_priv->drm,
12697 				    "No valid intermediate pipe watermarks are possible\n");
12698 			return ret;
12699 		}
12700 	}
12701 
12702 	if (INTEL_GEN(dev_priv) >= 9) {
12703 		if (mode_changed || crtc_state->update_pipe) {
12704 			ret = skl_update_scaler_crtc(crtc_state);
12705 			if (ret)
12706 				return ret;
12707 		}
12708 
12709 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
12710 		if (ret)
12711 			return ret;
12712 	}
12713 
12714 	if (HAS_IPS(dev_priv)) {
12715 		ret = hsw_compute_ips_config(crtc_state);
12716 		if (ret)
12717 			return ret;
12718 	}
12719 
12720 	if (INTEL_GEN(dev_priv) >= 9 ||
12721 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12722 		ret = hsw_compute_linetime_wm(state, crtc);
12723 		if (ret)
12724 			return ret;
12725 
12726 	}
12727 
12728 	return 0;
12729 }
12730 
12731 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12732 {
12733 	struct intel_connector *connector;
12734 	struct drm_connector_list_iter conn_iter;
12735 
12736 	drm_connector_list_iter_begin(dev, &conn_iter);
12737 	for_each_intel_connector_iter(connector, &conn_iter) {
12738 		if (connector->base.state->crtc)
12739 			drm_connector_put(&connector->base);
12740 
12741 		if (connector->base.encoder) {
12742 			connector->base.state->best_encoder =
12743 				connector->base.encoder;
12744 			connector->base.state->crtc =
12745 				connector->base.encoder->crtc;
12746 
12747 			drm_connector_get(&connector->base);
12748 		} else {
12749 			connector->base.state->best_encoder = NULL;
12750 			connector->base.state->crtc = NULL;
12751 		}
12752 	}
12753 	drm_connector_list_iter_end(&conn_iter);
12754 }
12755 
12756 static int
12757 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
12758 		      struct intel_crtc_state *pipe_config)
12759 {
12760 	struct drm_connector *connector = conn_state->connector;
12761 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12762 	const struct drm_display_info *info = &connector->display_info;
12763 	int bpp;
12764 
12765 	switch (conn_state->max_bpc) {
12766 	case 6 ... 7:
12767 		bpp = 6 * 3;
12768 		break;
12769 	case 8 ... 9:
12770 		bpp = 8 * 3;
12771 		break;
12772 	case 10 ... 11:
12773 		bpp = 10 * 3;
12774 		break;
12775 	case 12:
12776 		bpp = 12 * 3;
12777 		break;
12778 	default:
12779 		return -EINVAL;
12780 	}
12781 
12782 	if (bpp < pipe_config->pipe_bpp) {
12783 		drm_dbg_kms(&i915->drm,
12784 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
12785 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
12786 			    connector->base.id, connector->name,
12787 			    bpp, 3 * info->bpc,
12788 			    3 * conn_state->max_requested_bpc,
12789 			    pipe_config->pipe_bpp);
12790 
12791 		pipe_config->pipe_bpp = bpp;
12792 	}
12793 
12794 	return 0;
12795 }
12796 
12797 static int
12798 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
12799 			  struct intel_crtc_state *pipe_config)
12800 {
12801 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12802 	struct drm_atomic_state *state = pipe_config->uapi.state;
12803 	struct drm_connector *connector;
12804 	struct drm_connector_state *connector_state;
12805 	int bpp, i;
12806 
12807 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12808 	    IS_CHERRYVIEW(dev_priv)))
12809 		bpp = 10*3;
12810 	else if (INTEL_GEN(dev_priv) >= 5)
12811 		bpp = 12*3;
12812 	else
12813 		bpp = 8*3;
12814 
12815 	pipe_config->pipe_bpp = bpp;
12816 
12817 	/* Clamp display bpp to connector max bpp */
12818 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12819 		int ret;
12820 
12821 		if (connector_state->crtc != &crtc->base)
12822 			continue;
12823 
12824 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
12825 		if (ret)
12826 			return ret;
12827 	}
12828 
12829 	return 0;
12830 }
12831 
12832 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
12833 				    const struct drm_display_mode *mode)
12834 {
12835 	drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
12836 		    "type: 0x%x flags: 0x%x\n",
12837 		    mode->crtc_clock,
12838 		    mode->crtc_hdisplay, mode->crtc_hsync_start,
12839 		    mode->crtc_hsync_end, mode->crtc_htotal,
12840 		    mode->crtc_vdisplay, mode->crtc_vsync_start,
12841 		    mode->crtc_vsync_end, mode->crtc_vtotal,
12842 		    mode->type, mode->flags);
12843 }
12844 
12845 static void
12846 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
12847 		      const char *id, unsigned int lane_count,
12848 		      const struct intel_link_m_n *m_n)
12849 {
12850 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12851 
12852 	drm_dbg_kms(&i915->drm,
12853 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12854 		    id, lane_count,
12855 		    m_n->gmch_m, m_n->gmch_n,
12856 		    m_n->link_m, m_n->link_n, m_n->tu);
12857 }
12858 
12859 static void
12860 intel_dump_infoframe(struct drm_i915_private *dev_priv,
12861 		     const union hdmi_infoframe *frame)
12862 {
12863 	if (!drm_debug_enabled(DRM_UT_KMS))
12864 		return;
12865 
12866 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
12867 }
12868 
12869 static void
12870 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
12871 		      const struct drm_dp_vsc_sdp *vsc)
12872 {
12873 	if (!drm_debug_enabled(DRM_UT_KMS))
12874 		return;
12875 
12876 	drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
12877 }
12878 
12879 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
12880 
12881 static const char * const output_type_str[] = {
12882 	OUTPUT_TYPE(UNUSED),
12883 	OUTPUT_TYPE(ANALOG),
12884 	OUTPUT_TYPE(DVO),
12885 	OUTPUT_TYPE(SDVO),
12886 	OUTPUT_TYPE(LVDS),
12887 	OUTPUT_TYPE(TVOUT),
12888 	OUTPUT_TYPE(HDMI),
12889 	OUTPUT_TYPE(DP),
12890 	OUTPUT_TYPE(EDP),
12891 	OUTPUT_TYPE(DSI),
12892 	OUTPUT_TYPE(DDI),
12893 	OUTPUT_TYPE(DP_MST),
12894 };
12895 
12896 #undef OUTPUT_TYPE
12897 
12898 static void snprintf_output_types(char *buf, size_t len,
12899 				  unsigned int output_types)
12900 {
12901 	char *str = buf;
12902 	int i;
12903 
12904 	str[0] = '\0';
12905 
12906 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
12907 		int r;
12908 
12909 		if ((output_types & BIT(i)) == 0)
12910 			continue;
12911 
12912 		r = snprintf(str, len, "%s%s",
12913 			     str != buf ? "," : "", output_type_str[i]);
12914 		if (r >= len)
12915 			break;
12916 		str += r;
12917 		len -= r;
12918 
12919 		output_types &= ~BIT(i);
12920 	}
12921 
12922 	WARN_ON_ONCE(output_types != 0);
12923 }
12924 
12925 static const char * const output_format_str[] = {
12926 	[INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
12927 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
12928 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
12929 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
12930 };
12931 
12932 static const char *output_formats(enum intel_output_format format)
12933 {
12934 	if (format >= ARRAY_SIZE(output_format_str))
12935 		format = INTEL_OUTPUT_FORMAT_INVALID;
12936 	return output_format_str[format];
12937 }
12938 
12939 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
12940 {
12941 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12942 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
12943 	const struct drm_framebuffer *fb = plane_state->hw.fb;
12944 	struct drm_format_name_buf format_name;
12945 
12946 	if (!fb) {
12947 		drm_dbg_kms(&i915->drm,
12948 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
12949 			    plane->base.base.id, plane->base.name,
12950 			    yesno(plane_state->uapi.visible));
12951 		return;
12952 	}
12953 
12954 	drm_dbg_kms(&i915->drm,
12955 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s, visible: %s\n",
12956 		    plane->base.base.id, plane->base.name,
12957 		    fb->base.id, fb->width, fb->height,
12958 		    drm_get_format_name(fb->format->format, &format_name),
12959 		    yesno(plane_state->uapi.visible));
12960 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
12961 		    plane_state->hw.rotation, plane_state->scaler_id);
12962 	if (plane_state->uapi.visible)
12963 		drm_dbg_kms(&i915->drm,
12964 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
12965 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
12966 			    DRM_RECT_ARG(&plane_state->uapi.dst));
12967 }
12968 
12969 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
12970 				   struct intel_atomic_state *state,
12971 				   const char *context)
12972 {
12973 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
12974 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12975 	const struct intel_plane_state *plane_state;
12976 	struct intel_plane *plane;
12977 	char buf[64];
12978 	int i;
12979 
12980 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
12981 		    crtc->base.base.id, crtc->base.name,
12982 		    yesno(pipe_config->hw.enable), context);
12983 
12984 	if (!pipe_config->hw.enable)
12985 		goto dump_planes;
12986 
12987 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
12988 	drm_dbg_kms(&dev_priv->drm,
12989 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
12990 		    yesno(pipe_config->hw.active),
12991 		    buf, pipe_config->output_types,
12992 		    output_formats(pipe_config->output_format));
12993 
12994 	drm_dbg_kms(&dev_priv->drm,
12995 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12996 		    transcoder_name(pipe_config->cpu_transcoder),
12997 		    pipe_config->pipe_bpp, pipe_config->dither);
12998 
12999 	drm_dbg_kms(&dev_priv->drm,
13000 		    "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
13001 		    transcoder_name(pipe_config->master_transcoder),
13002 		    pipe_config->sync_mode_slaves_mask);
13003 
13004 	if (pipe_config->has_pch_encoder)
13005 		intel_dump_m_n_config(pipe_config, "fdi",
13006 				      pipe_config->fdi_lanes,
13007 				      &pipe_config->fdi_m_n);
13008 
13009 	if (intel_crtc_has_dp_encoder(pipe_config)) {
13010 		intel_dump_m_n_config(pipe_config, "dp m_n",
13011 				pipe_config->lane_count, &pipe_config->dp_m_n);
13012 		if (pipe_config->has_drrs)
13013 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
13014 					      pipe_config->lane_count,
13015 					      &pipe_config->dp_m2_n2);
13016 	}
13017 
13018 	drm_dbg_kms(&dev_priv->drm,
13019 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13020 		    pipe_config->has_audio, pipe_config->has_infoframe,
13021 		    pipe_config->infoframes.enable);
13022 
13023 	if (pipe_config->infoframes.enable &
13024 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
13025 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13026 			    pipe_config->infoframes.gcp);
13027 	if (pipe_config->infoframes.enable &
13028 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13029 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13030 	if (pipe_config->infoframes.enable &
13031 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13032 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13033 	if (pipe_config->infoframes.enable &
13034 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13035 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
13036 	if (pipe_config->infoframes.enable &
13037 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
13038 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13039 	if (pipe_config->infoframes.enable &
13040 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
13041 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13042 	if (pipe_config->infoframes.enable &
13043 	    intel_hdmi_infoframe_enable(DP_SDP_VSC))
13044 		intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
13045 
13046 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
13047 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
13048 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
13049 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
13050 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
13051 	drm_dbg_kms(&dev_priv->drm,
13052 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13053 		    pipe_config->port_clock,
13054 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13055 		    pipe_config->pixel_rate);
13056 
13057 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13058 		    pipe_config->linetime, pipe_config->ips_linetime);
13059 
13060 	if (INTEL_GEN(dev_priv) >= 9)
13061 		drm_dbg_kms(&dev_priv->drm,
13062 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13063 			    crtc->num_scalers,
13064 			    pipe_config->scaler_state.scaler_users,
13065 			    pipe_config->scaler_state.scaler_id);
13066 
13067 	if (HAS_GMCH(dev_priv))
13068 		drm_dbg_kms(&dev_priv->drm,
13069 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13070 			    pipe_config->gmch_pfit.control,
13071 			    pipe_config->gmch_pfit.pgm_ratios,
13072 			    pipe_config->gmch_pfit.lvds_border_bits);
13073 	else
13074 		drm_dbg_kms(&dev_priv->drm,
13075 			    "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
13076 			    DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
13077 			    enableddisabled(pipe_config->pch_pfit.enabled),
13078 			    yesno(pipe_config->pch_pfit.force_thru));
13079 
13080 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13081 		    pipe_config->ips_enabled, pipe_config->double_wide);
13082 
13083 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
13084 
13085 	if (IS_CHERRYVIEW(dev_priv))
13086 		drm_dbg_kms(&dev_priv->drm,
13087 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13088 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
13089 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13090 	else
13091 		drm_dbg_kms(&dev_priv->drm,
13092 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13093 			    pipe_config->csc_mode, pipe_config->gamma_mode,
13094 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13095 
13096 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13097 		    transcoder_name(pipe_config->mst_master_transcoder));
13098 
13099 dump_planes:
13100 	if (!state)
13101 		return;
13102 
13103 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13104 		if (plane->pipe == crtc->pipe)
13105 			intel_dump_plane_state(plane_state);
13106 	}
13107 }
13108 
13109 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
13110 {
13111 	struct drm_device *dev = state->base.dev;
13112 	struct drm_connector *connector;
13113 	struct drm_connector_list_iter conn_iter;
13114 	unsigned int used_ports = 0;
13115 	unsigned int used_mst_ports = 0;
13116 	bool ret = true;
13117 
13118 	/*
13119 	 * We're going to peek into connector->state,
13120 	 * hence connection_mutex must be held.
13121 	 */
13122 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13123 
13124 	/*
13125 	 * Walk the connector list instead of the encoder
13126 	 * list to detect the problem on ddi platforms
13127 	 * where there's just one encoder per digital port.
13128 	 */
13129 	drm_connector_list_iter_begin(dev, &conn_iter);
13130 	drm_for_each_connector_iter(connector, &conn_iter) {
13131 		struct drm_connector_state *connector_state;
13132 		struct intel_encoder *encoder;
13133 
13134 		connector_state =
13135 			drm_atomic_get_new_connector_state(&state->base,
13136 							   connector);
13137 		if (!connector_state)
13138 			connector_state = connector->state;
13139 
13140 		if (!connector_state->best_encoder)
13141 			continue;
13142 
13143 		encoder = to_intel_encoder(connector_state->best_encoder);
13144 
13145 		drm_WARN_ON(dev, !connector_state->crtc);
13146 
13147 		switch (encoder->type) {
13148 		case INTEL_OUTPUT_DDI:
13149 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
13150 				break;
13151 			/* else, fall through */
13152 		case INTEL_OUTPUT_DP:
13153 		case INTEL_OUTPUT_HDMI:
13154 		case INTEL_OUTPUT_EDP:
13155 			/* the same port mustn't appear more than once */
13156 			if (used_ports & BIT(encoder->port))
13157 				ret = false;
13158 
13159 			used_ports |= BIT(encoder->port);
13160 			break;
13161 		case INTEL_OUTPUT_DP_MST:
13162 			used_mst_ports |=
13163 				1 << encoder->port;
13164 			break;
13165 		default:
13166 			break;
13167 		}
13168 	}
13169 	drm_connector_list_iter_end(&conn_iter);
13170 
13171 	/* can't mix MST and SST/HDMI on the same port */
13172 	if (used_ports & used_mst_ports)
13173 		return false;
13174 
13175 	return ret;
13176 }
13177 
13178 static void
13179 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_crtc_state *crtc_state)
13180 {
13181 	intel_crtc_copy_color_blobs(crtc_state);
13182 }
13183 
13184 static void
13185 intel_crtc_copy_uapi_to_hw_state(struct intel_crtc_state *crtc_state)
13186 {
13187 	crtc_state->hw.enable = crtc_state->uapi.enable;
13188 	crtc_state->hw.active = crtc_state->uapi.active;
13189 	crtc_state->hw.mode = crtc_state->uapi.mode;
13190 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
13191 	intel_crtc_copy_uapi_to_hw_state_nomodeset(crtc_state);
13192 }
13193 
13194 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13195 {
13196 	crtc_state->uapi.enable = crtc_state->hw.enable;
13197 	crtc_state->uapi.active = crtc_state->hw.active;
13198 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
13199 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
13200 
13201 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
13202 
13203 	/* copy color blobs to uapi */
13204 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13205 				  crtc_state->hw.degamma_lut);
13206 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13207 				  crtc_state->hw.gamma_lut);
13208 	drm_property_replace_blob(&crtc_state->uapi.ctm,
13209 				  crtc_state->hw.ctm);
13210 }
13211 
13212 static int
13213 intel_crtc_prepare_cleared_state(struct intel_crtc_state *crtc_state)
13214 {
13215 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13216 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13217 	struct intel_crtc_state *saved_state;
13218 
13219 	saved_state = intel_crtc_state_alloc(crtc);
13220 	if (!saved_state)
13221 		return -ENOMEM;
13222 
13223 	/* free the old crtc_state->hw members */
13224 	intel_crtc_free_hw_state(crtc_state);
13225 
13226 	/* FIXME: before the switch to atomic started, a new pipe_config was
13227 	 * kzalloc'd. Code that depends on any field being zero should be
13228 	 * fixed, so that the crtc_state can be safely duplicated. For now,
13229 	 * only fields that are know to not cause problems are preserved. */
13230 
13231 	saved_state->uapi = crtc_state->uapi;
13232 	saved_state->scaler_state = crtc_state->scaler_state;
13233 	saved_state->shared_dpll = crtc_state->shared_dpll;
13234 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13235 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13236 	       sizeof(saved_state->icl_port_dplls));
13237 	saved_state->crc_enabled = crtc_state->crc_enabled;
13238 	if (IS_G4X(dev_priv) ||
13239 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13240 		saved_state->wm = crtc_state->wm;
13241 
13242 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13243 	kfree(saved_state);
13244 
13245 	intel_crtc_copy_uapi_to_hw_state(crtc_state);
13246 
13247 	return 0;
13248 }
13249 
13250 static int
13251 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
13252 {
13253 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
13254 	struct drm_atomic_state *state = pipe_config->uapi.state;
13255 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13256 	struct drm_connector *connector;
13257 	struct drm_connector_state *connector_state;
13258 	int base_bpp, ret, i;
13259 	bool retry = true;
13260 
13261 	pipe_config->cpu_transcoder =
13262 		(enum transcoder) to_intel_crtc(crtc)->pipe;
13263 
13264 	/*
13265 	 * Sanitize sync polarity flags based on requested ones. If neither
13266 	 * positive or negative polarity is requested, treat this as meaning
13267 	 * negative polarity.
13268 	 */
13269 	if (!(pipe_config->hw.adjusted_mode.flags &
13270 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
13271 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
13272 
13273 	if (!(pipe_config->hw.adjusted_mode.flags &
13274 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
13275 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
13276 
13277 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13278 					pipe_config);
13279 	if (ret)
13280 		return ret;
13281 
13282 	base_bpp = pipe_config->pipe_bpp;
13283 
13284 	/*
13285 	 * Determine the real pipe dimensions. Note that stereo modes can
13286 	 * increase the actual pipe size due to the frame doubling and
13287 	 * insertion of additional space for blanks between the frame. This
13288 	 * is stored in the crtc timings. We use the requested mode to do this
13289 	 * computation to clearly distinguish it from the adjusted mode, which
13290 	 * can be changed by the connectors in the below retry loop.
13291 	 */
13292 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
13293 			       &pipe_config->pipe_src_w,
13294 			       &pipe_config->pipe_src_h);
13295 
13296 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13297 		struct intel_encoder *encoder =
13298 			to_intel_encoder(connector_state->best_encoder);
13299 
13300 		if (connector_state->crtc != crtc)
13301 			continue;
13302 
13303 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13304 			drm_dbg_kms(&i915->drm,
13305 				    "rejecting invalid cloning configuration\n");
13306 			return -EINVAL;
13307 		}
13308 
13309 		/*
13310 		 * Determine output_types before calling the .compute_config()
13311 		 * hooks so that the hooks can use this information safely.
13312 		 */
13313 		if (encoder->compute_output_type)
13314 			pipe_config->output_types |=
13315 				BIT(encoder->compute_output_type(encoder, pipe_config,
13316 								 connector_state));
13317 		else
13318 			pipe_config->output_types |= BIT(encoder->type);
13319 	}
13320 
13321 encoder_retry:
13322 	/* Ensure the port clock defaults are reset when retrying. */
13323 	pipe_config->port_clock = 0;
13324 	pipe_config->pixel_multiplier = 1;
13325 
13326 	/* Fill in default crtc timings, allow encoders to overwrite them. */
13327 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
13328 			      CRTC_STEREO_DOUBLE);
13329 
13330 	/* Pass our mode to the connectors and the CRTC to give them a chance to
13331 	 * adjust it according to limitations or connector properties, and also
13332 	 * a chance to reject the mode entirely.
13333 	 */
13334 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13335 		struct intel_encoder *encoder =
13336 			to_intel_encoder(connector_state->best_encoder);
13337 
13338 		if (connector_state->crtc != crtc)
13339 			continue;
13340 
13341 		ret = encoder->compute_config(encoder, pipe_config,
13342 					      connector_state);
13343 		if (ret < 0) {
13344 			if (ret != -EDEADLK)
13345 				drm_dbg_kms(&i915->drm,
13346 					    "Encoder config failure: %d\n",
13347 					    ret);
13348 			return ret;
13349 		}
13350 	}
13351 
13352 	/* Set default port clock if not overwritten by the encoder. Needs to be
13353 	 * done afterwards in case the encoder adjusts the mode. */
13354 	if (!pipe_config->port_clock)
13355 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
13356 			* pipe_config->pixel_multiplier;
13357 
13358 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
13359 	if (ret == -EDEADLK)
13360 		return ret;
13361 	if (ret < 0) {
13362 		drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
13363 		return ret;
13364 	}
13365 
13366 	if (ret == RETRY) {
13367 		if (drm_WARN(&i915->drm, !retry,
13368 			     "loop in pipe configuration computation\n"))
13369 			return -EINVAL;
13370 
13371 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
13372 		retry = false;
13373 		goto encoder_retry;
13374 	}
13375 
13376 	/* Dithering seems to not pass-through bits correctly when it should, so
13377 	 * only enable it on 6bpc panels and when its not a compliance
13378 	 * test requesting 6bpc video pattern.
13379 	 */
13380 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13381 		!pipe_config->dither_force_disable;
13382 	drm_dbg_kms(&i915->drm,
13383 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13384 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
13385 
13386 	/*
13387 	 * Make drm_calc_timestamping_constants in
13388 	 * drm_atomic_helper_update_legacy_modeset_state() happy
13389 	 */
13390 	pipe_config->uapi.adjusted_mode = pipe_config->hw.adjusted_mode;
13391 
13392 	return 0;
13393 }
13394 
13395 static int
13396 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13397 {
13398 	struct intel_atomic_state *state =
13399 		to_intel_atomic_state(crtc_state->uapi.state);
13400 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13401 	struct drm_connector_state *conn_state;
13402 	struct drm_connector *connector;
13403 	int i;
13404 
13405 	for_each_new_connector_in_state(&state->base, connector,
13406 					conn_state, i) {
13407 		struct intel_encoder *encoder =
13408 			to_intel_encoder(conn_state->best_encoder);
13409 		int ret;
13410 
13411 		if (conn_state->crtc != &crtc->base ||
13412 		    !encoder->compute_config_late)
13413 			continue;
13414 
13415 		ret = encoder->compute_config_late(encoder, crtc_state,
13416 						   conn_state);
13417 		if (ret)
13418 			return ret;
13419 	}
13420 
13421 	return 0;
13422 }
13423 
13424 bool intel_fuzzy_clock_check(int clock1, int clock2)
13425 {
13426 	int diff;
13427 
13428 	if (clock1 == clock2)
13429 		return true;
13430 
13431 	if (!clock1 || !clock2)
13432 		return false;
13433 
13434 	diff = abs(clock1 - clock2);
13435 
13436 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13437 		return true;
13438 
13439 	return false;
13440 }
13441 
13442 static bool
13443 intel_compare_m_n(unsigned int m, unsigned int n,
13444 		  unsigned int m2, unsigned int n2,
13445 		  bool exact)
13446 {
13447 	if (m == m2 && n == n2)
13448 		return true;
13449 
13450 	if (exact || !m || !n || !m2 || !n2)
13451 		return false;
13452 
13453 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13454 
13455 	if (n > n2) {
13456 		while (n > n2) {
13457 			m2 <<= 1;
13458 			n2 <<= 1;
13459 		}
13460 	} else if (n < n2) {
13461 		while (n < n2) {
13462 			m <<= 1;
13463 			n <<= 1;
13464 		}
13465 	}
13466 
13467 	if (n != n2)
13468 		return false;
13469 
13470 	return intel_fuzzy_clock_check(m, m2);
13471 }
13472 
13473 static bool
13474 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13475 		       const struct intel_link_m_n *m2_n2,
13476 		       bool exact)
13477 {
13478 	return m_n->tu == m2_n2->tu &&
13479 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13480 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13481 		intel_compare_m_n(m_n->link_m, m_n->link_n,
13482 				  m2_n2->link_m, m2_n2->link_n, exact);
13483 }
13484 
13485 static bool
13486 intel_compare_infoframe(const union hdmi_infoframe *a,
13487 			const union hdmi_infoframe *b)
13488 {
13489 	return memcmp(a, b, sizeof(*a)) == 0;
13490 }
13491 
13492 static bool
13493 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
13494 			 const struct drm_dp_vsc_sdp *b)
13495 {
13496 	return memcmp(a, b, sizeof(*a)) == 0;
13497 }
13498 
13499 static void
13500 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13501 			       bool fastset, const char *name,
13502 			       const union hdmi_infoframe *a,
13503 			       const union hdmi_infoframe *b)
13504 {
13505 	if (fastset) {
13506 		if (!drm_debug_enabled(DRM_UT_KMS))
13507 			return;
13508 
13509 		drm_dbg_kms(&dev_priv->drm,
13510 			    "fastset mismatch in %s infoframe\n", name);
13511 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13512 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
13513 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13514 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13515 	} else {
13516 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13517 		drm_err(&dev_priv->drm, "expected:\n");
13518 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
13519 		drm_err(&dev_priv->drm, "found:\n");
13520 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13521 	}
13522 }
13523 
13524 static void
13525 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
13526 				bool fastset, const char *name,
13527 				const struct drm_dp_vsc_sdp *a,
13528 				const struct drm_dp_vsc_sdp *b)
13529 {
13530 	if (fastset) {
13531 		if (!drm_debug_enabled(DRM_UT_KMS))
13532 			return;
13533 
13534 		drm_dbg_kms(&dev_priv->drm,
13535 			    "fastset mismatch in %s dp sdp\n", name);
13536 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13537 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
13538 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13539 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
13540 	} else {
13541 		drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
13542 		drm_err(&dev_priv->drm, "expected:\n");
13543 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
13544 		drm_err(&dev_priv->drm, "found:\n");
13545 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
13546 	}
13547 }
13548 
13549 static void __printf(4, 5)
13550 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13551 		     const char *name, const char *format, ...)
13552 {
13553 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
13554 	struct va_format vaf;
13555 	va_list args;
13556 
13557 	va_start(args, format);
13558 	vaf.fmt = format;
13559 	vaf.va = &args;
13560 
13561 	if (fastset)
13562 		drm_dbg_kms(&i915->drm,
13563 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
13564 			    crtc->base.base.id, crtc->base.name, name, &vaf);
13565 	else
13566 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
13567 			crtc->base.base.id, crtc->base.name, name, &vaf);
13568 
13569 	va_end(args);
13570 }
13571 
13572 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
13573 {
13574 	if (i915_modparams.fastboot != -1)
13575 		return i915_modparams.fastboot;
13576 
13577 	/* Enable fastboot by default on Skylake and newer */
13578 	if (INTEL_GEN(dev_priv) >= 9)
13579 		return true;
13580 
13581 	/* Enable fastboot by default on VLV and CHV */
13582 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13583 		return true;
13584 
13585 	/* Disabled by default on all others */
13586 	return false;
13587 }
13588 
13589 static bool
13590 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
13591 			  const struct intel_crtc_state *pipe_config,
13592 			  bool fastset)
13593 {
13594 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
13595 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13596 	bool ret = true;
13597 	u32 bp_gamma = 0;
13598 	bool fixup_inherited = fastset &&
13599 		(current_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
13600 		!(pipe_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED);
13601 
13602 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
13603 		drm_dbg_kms(&dev_priv->drm,
13604 			    "initial modeset and fastboot not set\n");
13605 		ret = false;
13606 	}
13607 
13608 #define PIPE_CONF_CHECK_X(name) do { \
13609 	if (current_config->name != pipe_config->name) { \
13610 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13611 				     "(expected 0x%08x, found 0x%08x)", \
13612 				     current_config->name, \
13613 				     pipe_config->name); \
13614 		ret = false; \
13615 	} \
13616 } while (0)
13617 
13618 #define PIPE_CONF_CHECK_I(name) do { \
13619 	if (current_config->name != pipe_config->name) { \
13620 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13621 				     "(expected %i, found %i)", \
13622 				     current_config->name, \
13623 				     pipe_config->name); \
13624 		ret = false; \
13625 	} \
13626 } while (0)
13627 
13628 #define PIPE_CONF_CHECK_BOOL(name) do { \
13629 	if (current_config->name != pipe_config->name) { \
13630 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
13631 				     "(expected %s, found %s)", \
13632 				     yesno(current_config->name), \
13633 				     yesno(pipe_config->name)); \
13634 		ret = false; \
13635 	} \
13636 } while (0)
13637 
13638 /*
13639  * Checks state where we only read out the enabling, but not the entire
13640  * state itself (like full infoframes or ELD for audio). These states
13641  * require a full modeset on bootup to fix up.
13642  */
13643 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
13644 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
13645 		PIPE_CONF_CHECK_BOOL(name); \
13646 	} else { \
13647 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13648 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
13649 				     yesno(current_config->name), \
13650 				     yesno(pipe_config->name)); \
13651 		ret = false; \
13652 	} \
13653 } while (0)
13654 
13655 #define PIPE_CONF_CHECK_P(name) do { \
13656 	if (current_config->name != pipe_config->name) { \
13657 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13658 				     "(expected %p, found %p)", \
13659 				     current_config->name, \
13660 				     pipe_config->name); \
13661 		ret = false; \
13662 	} \
13663 } while (0)
13664 
13665 #define PIPE_CONF_CHECK_M_N(name) do { \
13666 	if (!intel_compare_link_m_n(&current_config->name, \
13667 				    &pipe_config->name,\
13668 				    !fastset)) { \
13669 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13670 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13671 				     "found tu %i, gmch %i/%i link %i/%i)", \
13672 				     current_config->name.tu, \
13673 				     current_config->name.gmch_m, \
13674 				     current_config->name.gmch_n, \
13675 				     current_config->name.link_m, \
13676 				     current_config->name.link_n, \
13677 				     pipe_config->name.tu, \
13678 				     pipe_config->name.gmch_m, \
13679 				     pipe_config->name.gmch_n, \
13680 				     pipe_config->name.link_m, \
13681 				     pipe_config->name.link_n); \
13682 		ret = false; \
13683 	} \
13684 } while (0)
13685 
13686 /* This is required for BDW+ where there is only one set of registers for
13687  * switching between high and low RR.
13688  * This macro can be used whenever a comparison has to be made between one
13689  * hw state and multiple sw state variables.
13690  */
13691 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
13692 	if (!intel_compare_link_m_n(&current_config->name, \
13693 				    &pipe_config->name, !fastset) && \
13694 	    !intel_compare_link_m_n(&current_config->alt_name, \
13695 				    &pipe_config->name, !fastset)) { \
13696 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13697 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13698 				     "or tu %i gmch %i/%i link %i/%i, " \
13699 				     "found tu %i, gmch %i/%i link %i/%i)", \
13700 				     current_config->name.tu, \
13701 				     current_config->name.gmch_m, \
13702 				     current_config->name.gmch_n, \
13703 				     current_config->name.link_m, \
13704 				     current_config->name.link_n, \
13705 				     current_config->alt_name.tu, \
13706 				     current_config->alt_name.gmch_m, \
13707 				     current_config->alt_name.gmch_n, \
13708 				     current_config->alt_name.link_m, \
13709 				     current_config->alt_name.link_n, \
13710 				     pipe_config->name.tu, \
13711 				     pipe_config->name.gmch_m, \
13712 				     pipe_config->name.gmch_n, \
13713 				     pipe_config->name.link_m, \
13714 				     pipe_config->name.link_n); \
13715 		ret = false; \
13716 	} \
13717 } while (0)
13718 
13719 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
13720 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
13721 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13722 				     "(%x) (expected %i, found %i)", \
13723 				     (mask), \
13724 				     current_config->name & (mask), \
13725 				     pipe_config->name & (mask)); \
13726 		ret = false; \
13727 	} \
13728 } while (0)
13729 
13730 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
13731 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
13732 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13733 				     "(expected %i, found %i)", \
13734 				     current_config->name, \
13735 				     pipe_config->name); \
13736 		ret = false; \
13737 	} \
13738 } while (0)
13739 
13740 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
13741 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
13742 				     &pipe_config->infoframes.name)) { \
13743 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
13744 					       &current_config->infoframes.name, \
13745 					       &pipe_config->infoframes.name); \
13746 		ret = false; \
13747 	} \
13748 } while (0)
13749 
13750 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
13751 	if (!current_config->has_psr && !pipe_config->has_psr && \
13752 	    !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
13753 				      &pipe_config->infoframes.name)) { \
13754 		pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
13755 						&current_config->infoframes.name, \
13756 						&pipe_config->infoframes.name); \
13757 		ret = false; \
13758 	} \
13759 } while (0)
13760 
13761 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
13762 	if (current_config->name1 != pipe_config->name1) { \
13763 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
13764 				"(expected %i, found %i, won't compare lut values)", \
13765 				current_config->name1, \
13766 				pipe_config->name1); \
13767 		ret = false;\
13768 	} else { \
13769 		if (!intel_color_lut_equal(current_config->name2, \
13770 					pipe_config->name2, pipe_config->name1, \
13771 					bit_precision)) { \
13772 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
13773 					"hw_state doesn't match sw_state"); \
13774 			ret = false; \
13775 		} \
13776 	} \
13777 } while (0)
13778 
13779 #define PIPE_CONF_QUIRK(quirk) \
13780 	((current_config->quirks | pipe_config->quirks) & (quirk))
13781 
13782 	PIPE_CONF_CHECK_I(cpu_transcoder);
13783 
13784 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
13785 	PIPE_CONF_CHECK_I(fdi_lanes);
13786 	PIPE_CONF_CHECK_M_N(fdi_m_n);
13787 
13788 	PIPE_CONF_CHECK_I(lane_count);
13789 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
13790 
13791 	if (INTEL_GEN(dev_priv) < 8) {
13792 		PIPE_CONF_CHECK_M_N(dp_m_n);
13793 
13794 		if (current_config->has_drrs)
13795 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
13796 	} else
13797 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
13798 
13799 	PIPE_CONF_CHECK_X(output_types);
13800 
13801 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
13802 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
13803 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
13804 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
13805 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
13806 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
13807 
13808 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
13809 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
13810 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
13811 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
13812 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
13813 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
13814 
13815 	PIPE_CONF_CHECK_I(pixel_multiplier);
13816 	PIPE_CONF_CHECK_I(output_format);
13817 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
13818 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
13819 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13820 		PIPE_CONF_CHECK_BOOL(limited_color_range);
13821 
13822 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
13823 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
13824 	PIPE_CONF_CHECK_BOOL(has_infoframe);
13825 	PIPE_CONF_CHECK_BOOL(fec_enable);
13826 
13827 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
13828 
13829 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13830 			      DRM_MODE_FLAG_INTERLACE);
13831 
13832 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
13833 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13834 				      DRM_MODE_FLAG_PHSYNC);
13835 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13836 				      DRM_MODE_FLAG_NHSYNC);
13837 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13838 				      DRM_MODE_FLAG_PVSYNC);
13839 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13840 				      DRM_MODE_FLAG_NVSYNC);
13841 	}
13842 
13843 	PIPE_CONF_CHECK_X(gmch_pfit.control);
13844 	/* pfit ratios are autocomputed by the hw on gen4+ */
13845 	if (INTEL_GEN(dev_priv) < 4)
13846 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
13847 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
13848 
13849 	/*
13850 	 * Changing the EDP transcoder input mux
13851 	 * (A_ONOFF vs. A_ON) requires a full modeset.
13852 	 */
13853 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
13854 
13855 	if (!fastset) {
13856 		PIPE_CONF_CHECK_I(pipe_src_w);
13857 		PIPE_CONF_CHECK_I(pipe_src_h);
13858 
13859 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
13860 		if (current_config->pch_pfit.enabled) {
13861 			PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
13862 			PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
13863 			PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
13864 			PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
13865 		}
13866 
13867 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13868 		PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
13869 
13870 		PIPE_CONF_CHECK_X(gamma_mode);
13871 		if (IS_CHERRYVIEW(dev_priv))
13872 			PIPE_CONF_CHECK_X(cgm_mode);
13873 		else
13874 			PIPE_CONF_CHECK_X(csc_mode);
13875 		PIPE_CONF_CHECK_BOOL(gamma_enable);
13876 		PIPE_CONF_CHECK_BOOL(csc_enable);
13877 
13878 		PIPE_CONF_CHECK_I(linetime);
13879 		PIPE_CONF_CHECK_I(ips_linetime);
13880 
13881 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
13882 		if (bp_gamma)
13883 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
13884 	}
13885 
13886 	PIPE_CONF_CHECK_BOOL(double_wide);
13887 
13888 	PIPE_CONF_CHECK_P(shared_dpll);
13889 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
13890 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
13891 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13892 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
13893 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
13894 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
13895 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13896 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13897 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
13898 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
13899 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
13900 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
13901 	PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
13902 	PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
13903 	PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
13904 	PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
13905 	PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
13906 	PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
13907 	PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
13908 	PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
13909 	PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
13910 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
13911 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
13912 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
13913 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
13914 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
13915 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
13916 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
13917 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
13918 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
13919 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
13920 
13921 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13922 	PIPE_CONF_CHECK_X(dsi_pll.div);
13923 
13924 	if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
13925 		PIPE_CONF_CHECK_I(pipe_bpp);
13926 
13927 	PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
13928 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
13929 
13930 	PIPE_CONF_CHECK_I(min_voltage_level);
13931 
13932 	PIPE_CONF_CHECK_X(infoframes.enable);
13933 	PIPE_CONF_CHECK_X(infoframes.gcp);
13934 	PIPE_CONF_CHECK_INFOFRAME(avi);
13935 	PIPE_CONF_CHECK_INFOFRAME(spd);
13936 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
13937 	PIPE_CONF_CHECK_INFOFRAME(drm);
13938 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
13939 
13940 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
13941 	PIPE_CONF_CHECK_I(master_transcoder);
13942 
13943 	PIPE_CONF_CHECK_I(dsc.compression_enable);
13944 	PIPE_CONF_CHECK_I(dsc.dsc_split);
13945 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
13946 
13947 	PIPE_CONF_CHECK_I(mst_master_transcoder);
13948 
13949 #undef PIPE_CONF_CHECK_X
13950 #undef PIPE_CONF_CHECK_I
13951 #undef PIPE_CONF_CHECK_BOOL
13952 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
13953 #undef PIPE_CONF_CHECK_P
13954 #undef PIPE_CONF_CHECK_FLAGS
13955 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
13956 #undef PIPE_CONF_CHECK_COLOR_LUT
13957 #undef PIPE_CONF_QUIRK
13958 
13959 	return ret;
13960 }
13961 
13962 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13963 					   const struct intel_crtc_state *pipe_config)
13964 {
13965 	if (pipe_config->has_pch_encoder) {
13966 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
13967 							    &pipe_config->fdi_m_n);
13968 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
13969 
13970 		/*
13971 		 * FDI already provided one idea for the dotclock.
13972 		 * Yell if the encoder disagrees.
13973 		 */
13974 		drm_WARN(&dev_priv->drm,
13975 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13976 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13977 			 fdi_dotclock, dotclock);
13978 	}
13979 }
13980 
13981 static void verify_wm_state(struct intel_crtc *crtc,
13982 			    struct intel_crtc_state *new_crtc_state)
13983 {
13984 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13985 	struct skl_hw_state {
13986 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
13987 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
13988 		struct skl_pipe_wm wm;
13989 	} *hw;
13990 	struct skl_pipe_wm *sw_wm;
13991 	struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
13992 	u8 hw_enabled_slices;
13993 	const enum pipe pipe = crtc->pipe;
13994 	int plane, level, max_level = ilk_wm_max_level(dev_priv);
13995 
13996 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
13997 		return;
13998 
13999 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
14000 	if (!hw)
14001 		return;
14002 
14003 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
14004 	sw_wm = &new_crtc_state->wm.skl.optimal;
14005 
14006 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
14007 
14008 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
14009 
14010 	if (INTEL_GEN(dev_priv) >= 11 &&
14011 	    hw_enabled_slices != dev_priv->enabled_dbuf_slices_mask)
14012 		drm_err(&dev_priv->drm,
14013 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
14014 			dev_priv->enabled_dbuf_slices_mask,
14015 			hw_enabled_slices);
14016 
14017 	/* planes */
14018 	for_each_universal_plane(dev_priv, pipe, plane) {
14019 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14020 
14021 		hw_plane_wm = &hw->wm.planes[plane];
14022 		sw_plane_wm = &sw_wm->planes[plane];
14023 
14024 		/* Watermarks */
14025 		for (level = 0; level <= max_level; level++) {
14026 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14027 						&sw_plane_wm->wm[level]) ||
14028 			    (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14029 							       &sw_plane_wm->sagv_wm0)))
14030 				continue;
14031 
14032 			drm_err(&dev_priv->drm,
14033 				"mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14034 				pipe_name(pipe), plane + 1, level,
14035 				sw_plane_wm->wm[level].plane_en,
14036 				sw_plane_wm->wm[level].plane_res_b,
14037 				sw_plane_wm->wm[level].plane_res_l,
14038 				hw_plane_wm->wm[level].plane_en,
14039 				hw_plane_wm->wm[level].plane_res_b,
14040 				hw_plane_wm->wm[level].plane_res_l);
14041 		}
14042 
14043 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14044 					 &sw_plane_wm->trans_wm)) {
14045 			drm_err(&dev_priv->drm,
14046 				"mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14047 				pipe_name(pipe), plane + 1,
14048 				sw_plane_wm->trans_wm.plane_en,
14049 				sw_plane_wm->trans_wm.plane_res_b,
14050 				sw_plane_wm->trans_wm.plane_res_l,
14051 				hw_plane_wm->trans_wm.plane_en,
14052 				hw_plane_wm->trans_wm.plane_res_b,
14053 				hw_plane_wm->trans_wm.plane_res_l);
14054 		}
14055 
14056 		/* DDB */
14057 		hw_ddb_entry = &hw->ddb_y[plane];
14058 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
14059 
14060 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14061 			drm_err(&dev_priv->drm,
14062 				"mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14063 				pipe_name(pipe), plane + 1,
14064 				sw_ddb_entry->start, sw_ddb_entry->end,
14065 				hw_ddb_entry->start, hw_ddb_entry->end);
14066 		}
14067 	}
14068 
14069 	/*
14070 	 * cursor
14071 	 * If the cursor plane isn't active, we may not have updated it's ddb
14072 	 * allocation. In that case since the ddb allocation will be updated
14073 	 * once the plane becomes visible, we can skip this check
14074 	 */
14075 	if (1) {
14076 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14077 
14078 		hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
14079 		sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14080 
14081 		/* Watermarks */
14082 		for (level = 0; level <= max_level; level++) {
14083 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14084 						&sw_plane_wm->wm[level]) ||
14085 			    (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14086 							       &sw_plane_wm->sagv_wm0)))
14087 				continue;
14088 
14089 			drm_err(&dev_priv->drm,
14090 				"mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14091 				pipe_name(pipe), level,
14092 				sw_plane_wm->wm[level].plane_en,
14093 				sw_plane_wm->wm[level].plane_res_b,
14094 				sw_plane_wm->wm[level].plane_res_l,
14095 				hw_plane_wm->wm[level].plane_en,
14096 				hw_plane_wm->wm[level].plane_res_b,
14097 				hw_plane_wm->wm[level].plane_res_l);
14098 		}
14099 
14100 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14101 					 &sw_plane_wm->trans_wm)) {
14102 			drm_err(&dev_priv->drm,
14103 				"mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14104 				pipe_name(pipe),
14105 				sw_plane_wm->trans_wm.plane_en,
14106 				sw_plane_wm->trans_wm.plane_res_b,
14107 				sw_plane_wm->trans_wm.plane_res_l,
14108 				hw_plane_wm->trans_wm.plane_en,
14109 				hw_plane_wm->trans_wm.plane_res_b,
14110 				hw_plane_wm->trans_wm.plane_res_l);
14111 		}
14112 
14113 		/* DDB */
14114 		hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
14115 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
14116 
14117 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14118 			drm_err(&dev_priv->drm,
14119 				"mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14120 				pipe_name(pipe),
14121 				sw_ddb_entry->start, sw_ddb_entry->end,
14122 				hw_ddb_entry->start, hw_ddb_entry->end);
14123 		}
14124 	}
14125 
14126 	kfree(hw);
14127 }
14128 
14129 static void
14130 verify_connector_state(struct intel_atomic_state *state,
14131 		       struct intel_crtc *crtc)
14132 {
14133 	struct drm_connector *connector;
14134 	struct drm_connector_state *new_conn_state;
14135 	int i;
14136 
14137 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
14138 		struct drm_encoder *encoder = connector->encoder;
14139 		struct intel_crtc_state *crtc_state = NULL;
14140 
14141 		if (new_conn_state->crtc != &crtc->base)
14142 			continue;
14143 
14144 		if (crtc)
14145 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
14146 
14147 		intel_connector_verify_state(crtc_state, new_conn_state);
14148 
14149 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
14150 		     "connector's atomic encoder doesn't match legacy encoder\n");
14151 	}
14152 }
14153 
14154 static void
14155 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
14156 {
14157 	struct intel_encoder *encoder;
14158 	struct drm_connector *connector;
14159 	struct drm_connector_state *old_conn_state, *new_conn_state;
14160 	int i;
14161 
14162 	for_each_intel_encoder(&dev_priv->drm, encoder) {
14163 		bool enabled = false, found = false;
14164 		enum pipe pipe;
14165 
14166 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14167 			    encoder->base.base.id,
14168 			    encoder->base.name);
14169 
14170 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
14171 						   new_conn_state, i) {
14172 			if (old_conn_state->best_encoder == &encoder->base)
14173 				found = true;
14174 
14175 			if (new_conn_state->best_encoder != &encoder->base)
14176 				continue;
14177 			found = enabled = true;
14178 
14179 			I915_STATE_WARN(new_conn_state->crtc !=
14180 					encoder->base.crtc,
14181 			     "connector's crtc doesn't match encoder crtc\n");
14182 		}
14183 
14184 		if (!found)
14185 			continue;
14186 
14187 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
14188 		     "encoder's enabled state mismatch "
14189 		     "(expected %i, found %i)\n",
14190 		     !!encoder->base.crtc, enabled);
14191 
14192 		if (!encoder->base.crtc) {
14193 			bool active;
14194 
14195 			active = encoder->get_hw_state(encoder, &pipe);
14196 			I915_STATE_WARN(active,
14197 			     "encoder detached but still enabled on pipe %c.\n",
14198 			     pipe_name(pipe));
14199 		}
14200 	}
14201 }
14202 
14203 static void
14204 verify_crtc_state(struct intel_crtc *crtc,
14205 		  struct intel_crtc_state *old_crtc_state,
14206 		  struct intel_crtc_state *new_crtc_state)
14207 {
14208 	struct drm_device *dev = crtc->base.dev;
14209 	struct drm_i915_private *dev_priv = to_i915(dev);
14210 	struct intel_encoder *encoder;
14211 	struct intel_crtc_state *pipe_config = old_crtc_state;
14212 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
14213 	bool active;
14214 
14215 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
14216 	intel_crtc_free_hw_state(old_crtc_state);
14217 	intel_crtc_state_reset(old_crtc_state, crtc);
14218 	old_crtc_state->uapi.state = state;
14219 
14220 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14221 		    crtc->base.name);
14222 
14223 	active = dev_priv->display.get_pipe_config(crtc, pipe_config);
14224 
14225 	/* we keep both pipes enabled on 830 */
14226 	if (IS_I830(dev_priv))
14227 		active = new_crtc_state->hw.active;
14228 
14229 	I915_STATE_WARN(new_crtc_state->hw.active != active,
14230 			"crtc active state doesn't match with hw state "
14231 			"(expected %i, found %i)\n",
14232 			new_crtc_state->hw.active, active);
14233 
14234 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14235 			"transitional active state does not match atomic hw state "
14236 			"(expected %i, found %i)\n",
14237 			new_crtc_state->hw.active, crtc->active);
14238 
14239 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14240 		enum pipe pipe;
14241 
14242 		active = encoder->get_hw_state(encoder, &pipe);
14243 		I915_STATE_WARN(active != new_crtc_state->hw.active,
14244 				"[ENCODER:%i] active %i with crtc active %i\n",
14245 				encoder->base.base.id, active,
14246 				new_crtc_state->hw.active);
14247 
14248 		I915_STATE_WARN(active && crtc->pipe != pipe,
14249 				"Encoder connected to wrong pipe %c\n",
14250 				pipe_name(pipe));
14251 
14252 		if (active)
14253 			encoder->get_config(encoder, pipe_config);
14254 	}
14255 
14256 	intel_crtc_compute_pixel_rate(pipe_config);
14257 
14258 	if (!new_crtc_state->hw.active)
14259 		return;
14260 
14261 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
14262 
14263 	if (!intel_pipe_config_compare(new_crtc_state,
14264 				       pipe_config, false)) {
14265 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
14266 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
14267 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
14268 	}
14269 }
14270 
14271 static void
14272 intel_verify_planes(struct intel_atomic_state *state)
14273 {
14274 	struct intel_plane *plane;
14275 	const struct intel_plane_state *plane_state;
14276 	int i;
14277 
14278 	for_each_new_intel_plane_in_state(state, plane,
14279 					  plane_state, i)
14280 		assert_plane(plane, plane_state->planar_slave ||
14281 			     plane_state->uapi.visible);
14282 }
14283 
14284 static void
14285 verify_single_dpll_state(struct drm_i915_private *dev_priv,
14286 			 struct intel_shared_dpll *pll,
14287 			 struct intel_crtc *crtc,
14288 			 struct intel_crtc_state *new_crtc_state)
14289 {
14290 	struct intel_dpll_hw_state dpll_hw_state;
14291 	unsigned int crtc_mask;
14292 	bool active;
14293 
14294 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
14295 
14296 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
14297 
14298 	active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
14299 
14300 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
14301 		I915_STATE_WARN(!pll->on && pll->active_mask,
14302 		     "pll in active use but not on in sw tracking\n");
14303 		I915_STATE_WARN(pll->on && !pll->active_mask,
14304 		     "pll is on but not used by any active crtc\n");
14305 		I915_STATE_WARN(pll->on != active,
14306 		     "pll on state mismatch (expected %i, found %i)\n",
14307 		     pll->on, active);
14308 	}
14309 
14310 	if (!crtc) {
14311 		I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
14312 				"more active pll users than references: %x vs %x\n",
14313 				pll->active_mask, pll->state.crtc_mask);
14314 
14315 		return;
14316 	}
14317 
14318 	crtc_mask = drm_crtc_mask(&crtc->base);
14319 
14320 	if (new_crtc_state->hw.active)
14321 		I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14322 				"pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
14323 				pipe_name(crtc->pipe), pll->active_mask);
14324 	else
14325 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14326 				"pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
14327 				pipe_name(crtc->pipe), pll->active_mask);
14328 
14329 	I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
14330 			"pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
14331 			crtc_mask, pll->state.crtc_mask);
14332 
14333 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
14334 					  &dpll_hw_state,
14335 					  sizeof(dpll_hw_state)),
14336 			"pll hw state mismatch\n");
14337 }
14338 
14339 static void
14340 verify_shared_dpll_state(struct intel_crtc *crtc,
14341 			 struct intel_crtc_state *old_crtc_state,
14342 			 struct intel_crtc_state *new_crtc_state)
14343 {
14344 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14345 
14346 	if (new_crtc_state->shared_dpll)
14347 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
14348 
14349 	if (old_crtc_state->shared_dpll &&
14350 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14351 		unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14352 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
14353 
14354 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14355 				"pll active mismatch (didn't expect pipe %c in active mask)\n",
14356 				pipe_name(crtc->pipe));
14357 		I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
14358 				"pll enabled crtcs mismatch (found %x in enabled mask)\n",
14359 				pipe_name(crtc->pipe));
14360 	}
14361 }
14362 
14363 static void
14364 intel_modeset_verify_crtc(struct intel_crtc *crtc,
14365 			  struct intel_atomic_state *state,
14366 			  struct intel_crtc_state *old_crtc_state,
14367 			  struct intel_crtc_state *new_crtc_state)
14368 {
14369 	if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
14370 		return;
14371 
14372 	verify_wm_state(crtc, new_crtc_state);
14373 	verify_connector_state(state, crtc);
14374 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14375 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
14376 }
14377 
14378 static void
14379 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
14380 {
14381 	int i;
14382 
14383 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14384 		verify_single_dpll_state(dev_priv,
14385 					 &dev_priv->dpll.shared_dplls[i],
14386 					 NULL, NULL);
14387 }
14388 
14389 static void
14390 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
14391 			      struct intel_atomic_state *state)
14392 {
14393 	verify_encoder_state(dev_priv, state);
14394 	verify_connector_state(state, NULL);
14395 	verify_disabled_dpll_state(dev_priv);
14396 }
14397 
14398 static void
14399 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
14400 {
14401 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
14402 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14403 	const struct drm_display_mode *adjusted_mode =
14404 		&crtc_state->hw.adjusted_mode;
14405 
14406 	drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
14407 
14408 	/*
14409 	 * The scanline counter increments at the leading edge of hsync.
14410 	 *
14411 	 * On most platforms it starts counting from vtotal-1 on the
14412 	 * first active line. That means the scanline counter value is
14413 	 * always one less than what we would expect. Ie. just after
14414 	 * start of vblank, which also occurs at start of hsync (on the
14415 	 * last active line), the scanline counter will read vblank_start-1.
14416 	 *
14417 	 * On gen2 the scanline counter starts counting from 1 instead
14418 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14419 	 * to keep the value positive), instead of adding one.
14420 	 *
14421 	 * On HSW+ the behaviour of the scanline counter depends on the output
14422 	 * type. For DP ports it behaves like most other platforms, but on HDMI
14423 	 * there's an extra 1 line difference. So we need to add two instead of
14424 	 * one to the value.
14425 	 *
14426 	 * On VLV/CHV DSI the scanline counter would appear to increment
14427 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14428 	 * that means we can't tell whether we're in vblank or not while
14429 	 * we're on that particular line. We must still set scanline_offset
14430 	 * to 1 so that the vblank timestamps come out correct when we query
14431 	 * the scanline counter from within the vblank interrupt handler.
14432 	 * However if queried just before the start of vblank we'll get an
14433 	 * answer that's slightly in the future.
14434 	 */
14435 	if (IS_GEN(dev_priv, 2)) {
14436 		int vtotal;
14437 
14438 		vtotal = adjusted_mode->crtc_vtotal;
14439 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
14440 			vtotal /= 2;
14441 
14442 		crtc->scanline_offset = vtotal - 1;
14443 	} else if (HAS_DDI(dev_priv) &&
14444 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
14445 		crtc->scanline_offset = 2;
14446 	} else {
14447 		crtc->scanline_offset = 1;
14448 	}
14449 }
14450 
14451 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
14452 {
14453 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14454 	struct intel_crtc_state *new_crtc_state;
14455 	struct intel_crtc *crtc;
14456 	int i;
14457 
14458 	if (!dev_priv->display.crtc_compute_clock)
14459 		return;
14460 
14461 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14462 		if (!needs_modeset(new_crtc_state))
14463 			continue;
14464 
14465 		intel_release_shared_dplls(state, crtc);
14466 	}
14467 }
14468 
14469 /*
14470  * This implements the workaround described in the "notes" section of the mode
14471  * set sequence documentation. When going from no pipes or single pipe to
14472  * multiple pipes, and planes are enabled after the pipe, we need to wait at
14473  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14474  */
14475 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
14476 {
14477 	struct intel_crtc_state *crtc_state;
14478 	struct intel_crtc *crtc;
14479 	struct intel_crtc_state *first_crtc_state = NULL;
14480 	struct intel_crtc_state *other_crtc_state = NULL;
14481 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14482 	int i;
14483 
14484 	/* look at all crtc's that are going to be enabled in during modeset */
14485 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14486 		if (!crtc_state->hw.active ||
14487 		    !needs_modeset(crtc_state))
14488 			continue;
14489 
14490 		if (first_crtc_state) {
14491 			other_crtc_state = crtc_state;
14492 			break;
14493 		} else {
14494 			first_crtc_state = crtc_state;
14495 			first_pipe = crtc->pipe;
14496 		}
14497 	}
14498 
14499 	/* No workaround needed? */
14500 	if (!first_crtc_state)
14501 		return 0;
14502 
14503 	/* w/a possibly needed, check how many crtc's are already enabled. */
14504 	for_each_intel_crtc(state->base.dev, crtc) {
14505 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14506 		if (IS_ERR(crtc_state))
14507 			return PTR_ERR(crtc_state);
14508 
14509 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
14510 
14511 		if (!crtc_state->hw.active ||
14512 		    needs_modeset(crtc_state))
14513 			continue;
14514 
14515 		/* 2 or more enabled crtcs means no need for w/a */
14516 		if (enabled_pipe != INVALID_PIPE)
14517 			return 0;
14518 
14519 		enabled_pipe = crtc->pipe;
14520 	}
14521 
14522 	if (enabled_pipe != INVALID_PIPE)
14523 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
14524 	else if (other_crtc_state)
14525 		other_crtc_state->hsw_workaround_pipe = first_pipe;
14526 
14527 	return 0;
14528 }
14529 
14530 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
14531 			   u8 active_pipes)
14532 {
14533 	const struct intel_crtc_state *crtc_state;
14534 	struct intel_crtc *crtc;
14535 	int i;
14536 
14537 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14538 		if (crtc_state->hw.active)
14539 			active_pipes |= BIT(crtc->pipe);
14540 		else
14541 			active_pipes &= ~BIT(crtc->pipe);
14542 	}
14543 
14544 	return active_pipes;
14545 }
14546 
14547 static int intel_modeset_checks(struct intel_atomic_state *state)
14548 {
14549 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14550 	int ret;
14551 
14552 	state->modeset = true;
14553 	state->active_pipes = intel_calc_active_pipes(state, dev_priv->active_pipes);
14554 
14555 	state->active_pipe_changes = state->active_pipes ^ dev_priv->active_pipes;
14556 
14557 	if (state->active_pipe_changes) {
14558 		ret = _intel_atomic_lock_global_state(state);
14559 		if (ret)
14560 			return ret;
14561 	}
14562 
14563 	ret = intel_modeset_calc_cdclk(state);
14564 	if (ret)
14565 		return ret;
14566 
14567 	intel_modeset_clear_plls(state);
14568 
14569 	if (IS_HASWELL(dev_priv))
14570 		return hsw_mode_set_planes_workaround(state);
14571 
14572 	return 0;
14573 }
14574 
14575 /*
14576  * Handle calculation of various watermark data at the end of the atomic check
14577  * phase.  The code here should be run after the per-crtc and per-plane 'check'
14578  * handlers to ensure that all derived state has been updated.
14579  */
14580 static int calc_watermark_data(struct intel_atomic_state *state)
14581 {
14582 	struct drm_device *dev = state->base.dev;
14583 	struct drm_i915_private *dev_priv = to_i915(dev);
14584 
14585 	/* Is there platform-specific watermark information to calculate? */
14586 	if (dev_priv->display.compute_global_watermarks)
14587 		return dev_priv->display.compute_global_watermarks(state);
14588 
14589 	return 0;
14590 }
14591 
14592 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
14593 				     struct intel_crtc_state *new_crtc_state)
14594 {
14595 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
14596 		return;
14597 
14598 	new_crtc_state->uapi.mode_changed = false;
14599 	new_crtc_state->update_pipe = true;
14600 }
14601 
14602 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
14603 				    struct intel_crtc_state *new_crtc_state)
14604 {
14605 	/*
14606 	 * If we're not doing the full modeset we want to
14607 	 * keep the current M/N values as they may be
14608 	 * sufficiently different to the computed values
14609 	 * to cause problems.
14610 	 *
14611 	 * FIXME: should really copy more fuzzy state here
14612 	 */
14613 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
14614 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
14615 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
14616 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
14617 }
14618 
14619 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
14620 					  struct intel_crtc *crtc,
14621 					  u8 plane_ids_mask)
14622 {
14623 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14624 	struct intel_plane *plane;
14625 
14626 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14627 		struct intel_plane_state *plane_state;
14628 
14629 		if ((plane_ids_mask & BIT(plane->id)) == 0)
14630 			continue;
14631 
14632 		plane_state = intel_atomic_get_plane_state(state, plane);
14633 		if (IS_ERR(plane_state))
14634 			return PTR_ERR(plane_state);
14635 	}
14636 
14637 	return 0;
14638 }
14639 
14640 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
14641 {
14642 	/* See {hsw,vlv,ivb}_plane_ratio() */
14643 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
14644 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
14645 		IS_IVYBRIDGE(dev_priv);
14646 }
14647 
14648 static int intel_atomic_check_planes(struct intel_atomic_state *state,
14649 				     bool *need_cdclk_calc)
14650 {
14651 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14652 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14653 	struct intel_plane_state *plane_state;
14654 	struct intel_plane *plane;
14655 	struct intel_crtc *crtc;
14656 	int i, ret;
14657 
14658 	ret = icl_add_linked_planes(state);
14659 	if (ret)
14660 		return ret;
14661 
14662 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14663 		ret = intel_plane_atomic_check(state, plane);
14664 		if (ret) {
14665 			drm_dbg_atomic(&dev_priv->drm,
14666 				       "[PLANE:%d:%s] atomic driver check failed\n",
14667 				       plane->base.base.id, plane->base.name);
14668 			return ret;
14669 		}
14670 	}
14671 
14672 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14673 					    new_crtc_state, i) {
14674 		u8 old_active_planes, new_active_planes;
14675 
14676 		ret = icl_check_nv12_planes(new_crtc_state);
14677 		if (ret)
14678 			return ret;
14679 
14680 		/*
14681 		 * On some platforms the number of active planes affects
14682 		 * the planes' minimum cdclk calculation. Add such planes
14683 		 * to the state before we compute the minimum cdclk.
14684 		 */
14685 		if (!active_planes_affects_min_cdclk(dev_priv))
14686 			continue;
14687 
14688 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14689 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14690 
14691 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
14692 			continue;
14693 
14694 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
14695 		if (ret)
14696 			return ret;
14697 	}
14698 
14699 	/*
14700 	 * active_planes bitmask has been updated, and potentially
14701 	 * affected planes are part of the state. We can now
14702 	 * compute the minimum cdclk for each plane.
14703 	 */
14704 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14705 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
14706 		if (ret)
14707 			return ret;
14708 	}
14709 
14710 	return 0;
14711 }
14712 
14713 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
14714 {
14715 	struct intel_crtc_state *crtc_state;
14716 	struct intel_crtc *crtc;
14717 	int i;
14718 
14719 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14720 		int ret = intel_crtc_atomic_check(state, crtc);
14721 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
14722 		if (ret) {
14723 			drm_dbg_atomic(&i915->drm,
14724 				       "[CRTC:%d:%s] atomic driver check failed\n",
14725 				       crtc->base.base.id, crtc->base.name);
14726 			return ret;
14727 		}
14728 	}
14729 
14730 	return 0;
14731 }
14732 
14733 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
14734 					       u8 transcoders)
14735 {
14736 	const struct intel_crtc_state *new_crtc_state;
14737 	struct intel_crtc *crtc;
14738 	int i;
14739 
14740 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14741 		if (new_crtc_state->hw.enable &&
14742 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
14743 		    needs_modeset(new_crtc_state))
14744 			return true;
14745 	}
14746 
14747 	return false;
14748 }
14749 
14750 /**
14751  * intel_atomic_check - validate state object
14752  * @dev: drm device
14753  * @_state: state to validate
14754  */
14755 static int intel_atomic_check(struct drm_device *dev,
14756 			      struct drm_atomic_state *_state)
14757 {
14758 	struct drm_i915_private *dev_priv = to_i915(dev);
14759 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
14760 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14761 	struct intel_cdclk_state *new_cdclk_state;
14762 	struct intel_crtc *crtc;
14763 	int ret, i;
14764 	bool any_ms = false;
14765 
14766 	/* Catch I915_MODE_FLAG_INHERITED */
14767 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14768 					    new_crtc_state, i) {
14769 		if (new_crtc_state->uapi.mode.private_flags !=
14770 		    old_crtc_state->uapi.mode.private_flags)
14771 			new_crtc_state->uapi.mode_changed = true;
14772 	}
14773 
14774 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
14775 	if (ret)
14776 		goto fail;
14777 
14778 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14779 					    new_crtc_state, i) {
14780 		if (!needs_modeset(new_crtc_state)) {
14781 			/* Light copy */
14782 			intel_crtc_copy_uapi_to_hw_state_nomodeset(new_crtc_state);
14783 
14784 			continue;
14785 		}
14786 
14787 		ret = intel_crtc_prepare_cleared_state(new_crtc_state);
14788 		if (ret)
14789 			goto fail;
14790 
14791 		if (!new_crtc_state->hw.enable)
14792 			continue;
14793 
14794 		ret = intel_modeset_pipe_config(new_crtc_state);
14795 		if (ret)
14796 			goto fail;
14797 	}
14798 
14799 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14800 					    new_crtc_state, i) {
14801 		if (!needs_modeset(new_crtc_state))
14802 			continue;
14803 
14804 		ret = intel_modeset_pipe_config_late(new_crtc_state);
14805 		if (ret)
14806 			goto fail;
14807 
14808 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
14809 	}
14810 
14811 	/**
14812 	 * Check if fastset is allowed by external dependencies like other
14813 	 * pipes and transcoders.
14814 	 *
14815 	 * Right now it only forces a fullmodeset when the MST master
14816 	 * transcoder did not changed but the pipe of the master transcoder
14817 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
14818 	 * in case of port synced crtcs, if one of the synced crtcs
14819 	 * needs a full modeset, all other synced crtcs should be
14820 	 * forced a full modeset.
14821 	 */
14822 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14823 		if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
14824 			continue;
14825 
14826 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
14827 			enum transcoder master = new_crtc_state->mst_master_transcoder;
14828 
14829 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
14830 				new_crtc_state->uapi.mode_changed = true;
14831 				new_crtc_state->update_pipe = false;
14832 			}
14833 		}
14834 
14835 		if (is_trans_port_sync_mode(new_crtc_state)) {
14836 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
14837 
14838 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
14839 				trans |= BIT(new_crtc_state->master_transcoder);
14840 
14841 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
14842 				new_crtc_state->uapi.mode_changed = true;
14843 				new_crtc_state->update_pipe = false;
14844 			}
14845 		}
14846 	}
14847 
14848 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14849 					    new_crtc_state, i) {
14850 		if (needs_modeset(new_crtc_state)) {
14851 			any_ms = true;
14852 			continue;
14853 		}
14854 
14855 		if (!new_crtc_state->update_pipe)
14856 			continue;
14857 
14858 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
14859 	}
14860 
14861 	if (any_ms && !check_digital_port_conflicts(state)) {
14862 		drm_dbg_kms(&dev_priv->drm,
14863 			    "rejecting conflicting digital port configuration\n");
14864 		ret = EINVAL;
14865 		goto fail;
14866 	}
14867 
14868 	ret = drm_dp_mst_atomic_check(&state->base);
14869 	if (ret)
14870 		goto fail;
14871 
14872 	ret = intel_atomic_check_planes(state, &any_ms);
14873 	if (ret)
14874 		goto fail;
14875 
14876 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
14877 	if (new_cdclk_state && new_cdclk_state->force_min_cdclk_changed)
14878 		any_ms = true;
14879 
14880 	/*
14881 	 * distrust_bios_wm will force a full dbuf recomputation
14882 	 * but the hardware state will only get updated accordingly
14883 	 * if state->modeset==true. Hence distrust_bios_wm==true &&
14884 	 * state->modeset==false is an invalid combination which
14885 	 * would cause the hardware and software dbuf state to get
14886 	 * out of sync. We must prevent that.
14887 	 *
14888 	 * FIXME clean up this mess and introduce better
14889 	 * state tracking for dbuf.
14890 	 */
14891 	if (dev_priv->wm.distrust_bios_wm)
14892 		any_ms = true;
14893 
14894 	if (any_ms) {
14895 		ret = intel_modeset_checks(state);
14896 		if (ret)
14897 			goto fail;
14898 	}
14899 
14900 	ret = intel_atomic_check_crtcs(state);
14901 	if (ret)
14902 		goto fail;
14903 
14904 	intel_fbc_choose_crtc(dev_priv, state);
14905 	ret = calc_watermark_data(state);
14906 	if (ret)
14907 		goto fail;
14908 
14909 	ret = intel_bw_atomic_check(state);
14910 	if (ret)
14911 		goto fail;
14912 
14913 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14914 					    new_crtc_state, i) {
14915 		if (!needs_modeset(new_crtc_state) &&
14916 		    !new_crtc_state->update_pipe)
14917 			continue;
14918 
14919 		intel_dump_pipe_config(new_crtc_state, state,
14920 				       needs_modeset(new_crtc_state) ?
14921 				       "[modeset]" : "[fastset]");
14922 	}
14923 
14924 	return 0;
14925 
14926  fail:
14927 	if (ret == -EDEADLK)
14928 		return ret;
14929 
14930 	/*
14931 	 * FIXME would probably be nice to know which crtc specifically
14932 	 * caused the failure, in cases where we can pinpoint it.
14933 	 */
14934 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14935 					    new_crtc_state, i)
14936 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
14937 
14938 	return ret;
14939 }
14940 
14941 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
14942 {
14943 	return drm_atomic_helper_prepare_planes(state->base.dev,
14944 						&state->base);
14945 }
14946 
14947 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14948 {
14949 	struct drm_device *dev = crtc->base.dev;
14950 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
14951 
14952 	if (!vblank->max_vblank_count)
14953 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
14954 
14955 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
14956 }
14957 
14958 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
14959 				  struct intel_crtc_state *crtc_state)
14960 {
14961 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14962 
14963 	if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
14964 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
14965 
14966 	if (crtc_state->has_pch_encoder) {
14967 		enum pipe pch_transcoder =
14968 			intel_crtc_pch_transcoder(crtc);
14969 
14970 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
14971 	}
14972 }
14973 
14974 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
14975 			       const struct intel_crtc_state *new_crtc_state)
14976 {
14977 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
14978 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14979 
14980 	/*
14981 	 * Update pipe size and adjust fitter if needed: the reason for this is
14982 	 * that in compute_mode_changes we check the native mode (not the pfit
14983 	 * mode) to see if we can flip rather than do a full mode set. In the
14984 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
14985 	 * pfit state, we'll end up with a big fb scanned out into the wrong
14986 	 * sized surface.
14987 	 */
14988 	intel_set_pipe_src_size(new_crtc_state);
14989 
14990 	/* on skylake this is done by detaching scalers */
14991 	if (INTEL_GEN(dev_priv) >= 9) {
14992 		skl_detach_scalers(new_crtc_state);
14993 
14994 		if (new_crtc_state->pch_pfit.enabled)
14995 			skl_pfit_enable(new_crtc_state);
14996 	} else if (HAS_PCH_SPLIT(dev_priv)) {
14997 		if (new_crtc_state->pch_pfit.enabled)
14998 			ilk_pfit_enable(new_crtc_state);
14999 		else if (old_crtc_state->pch_pfit.enabled)
15000 			ilk_pfit_disable(old_crtc_state);
15001 	}
15002 
15003 	/*
15004 	 * The register is supposedly single buffered so perhaps
15005 	 * not 100% correct to do this here. But SKL+ calculate
15006 	 * this based on the adjust pixel rate so pfit changes do
15007 	 * affect it and so it must be updated for fastsets.
15008 	 * HSW/BDW only really need this here for fastboot, after
15009 	 * that the value should not change without a full modeset.
15010 	 */
15011 	if (INTEL_GEN(dev_priv) >= 9 ||
15012 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
15013 		hsw_set_linetime_wm(new_crtc_state);
15014 
15015 	if (INTEL_GEN(dev_priv) >= 11)
15016 		icl_set_pipe_chicken(crtc);
15017 }
15018 
15019 static void commit_pipe_config(struct intel_atomic_state *state,
15020 			       struct intel_crtc *crtc)
15021 {
15022 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15023 	const struct intel_crtc_state *old_crtc_state =
15024 		intel_atomic_get_old_crtc_state(state, crtc);
15025 	const struct intel_crtc_state *new_crtc_state =
15026 		intel_atomic_get_new_crtc_state(state, crtc);
15027 	bool modeset = needs_modeset(new_crtc_state);
15028 
15029 	/*
15030 	 * During modesets pipe configuration was programmed as the
15031 	 * CRTC was enabled.
15032 	 */
15033 	if (!modeset) {
15034 		if (new_crtc_state->uapi.color_mgmt_changed ||
15035 		    new_crtc_state->update_pipe)
15036 			intel_color_commit(new_crtc_state);
15037 
15038 		if (INTEL_GEN(dev_priv) >= 9)
15039 			skl_detach_scalers(new_crtc_state);
15040 
15041 		if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15042 			bdw_set_pipemisc(new_crtc_state);
15043 
15044 		if (new_crtc_state->update_pipe)
15045 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
15046 	}
15047 
15048 	if (dev_priv->display.atomic_update_watermarks)
15049 		dev_priv->display.atomic_update_watermarks(state, crtc);
15050 }
15051 
15052 static void intel_enable_crtc(struct intel_atomic_state *state,
15053 			      struct intel_crtc *crtc)
15054 {
15055 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15056 	const struct intel_crtc_state *new_crtc_state =
15057 		intel_atomic_get_new_crtc_state(state, crtc);
15058 
15059 	if (!needs_modeset(new_crtc_state))
15060 		return;
15061 
15062 	intel_crtc_update_active_timings(new_crtc_state);
15063 
15064 	dev_priv->display.crtc_enable(state, crtc);
15065 
15066 	/* vblanks work again, re-enable pipe CRC. */
15067 	intel_crtc_enable_pipe_crc(crtc);
15068 }
15069 
15070 static void intel_update_crtc(struct intel_atomic_state *state,
15071 			      struct intel_crtc *crtc)
15072 {
15073 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15074 	const struct intel_crtc_state *old_crtc_state =
15075 		intel_atomic_get_old_crtc_state(state, crtc);
15076 	struct intel_crtc_state *new_crtc_state =
15077 		intel_atomic_get_new_crtc_state(state, crtc);
15078 	bool modeset = needs_modeset(new_crtc_state);
15079 
15080 	if (!modeset) {
15081 		if (new_crtc_state->preload_luts &&
15082 		    (new_crtc_state->uapi.color_mgmt_changed ||
15083 		     new_crtc_state->update_pipe))
15084 			intel_color_load_luts(new_crtc_state);
15085 
15086 		intel_pre_plane_update(state, crtc);
15087 
15088 		if (new_crtc_state->update_pipe)
15089 			intel_encoders_update_pipe(state, crtc);
15090 	}
15091 
15092 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15093 		intel_fbc_disable(crtc);
15094 	else
15095 		intel_fbc_enable(state, crtc);
15096 
15097 	/* Perform vblank evasion around commit operation */
15098 	intel_pipe_update_start(new_crtc_state);
15099 
15100 	commit_pipe_config(state, crtc);
15101 
15102 	if (INTEL_GEN(dev_priv) >= 9)
15103 		skl_update_planes_on_crtc(state, crtc);
15104 	else
15105 		i9xx_update_planes_on_crtc(state, crtc);
15106 
15107 	intel_pipe_update_end(new_crtc_state);
15108 
15109 	/*
15110 	 * We usually enable FIFO underrun interrupts as part of the
15111 	 * CRTC enable sequence during modesets.  But when we inherit a
15112 	 * valid pipe configuration from the BIOS we need to take care
15113 	 * of enabling them on the CRTC's first fastset.
15114 	 */
15115 	if (new_crtc_state->update_pipe && !modeset &&
15116 	    old_crtc_state->hw.mode.private_flags & I915_MODE_FLAG_INHERITED)
15117 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15118 }
15119 
15120 
15121 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15122 					  struct intel_crtc_state *old_crtc_state,
15123 					  struct intel_crtc_state *new_crtc_state,
15124 					  struct intel_crtc *crtc)
15125 {
15126 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15127 
15128 	intel_crtc_disable_planes(state, crtc);
15129 
15130 	/*
15131 	 * We need to disable pipe CRC before disabling the pipe,
15132 	 * or we race against vblank off.
15133 	 */
15134 	intel_crtc_disable_pipe_crc(crtc);
15135 
15136 	dev_priv->display.crtc_disable(state, crtc);
15137 	crtc->active = false;
15138 	intel_fbc_disable(crtc);
15139 	intel_disable_shared_dpll(old_crtc_state);
15140 
15141 	/* FIXME unify this for all platforms */
15142 	if (!new_crtc_state->hw.active &&
15143 	    !HAS_GMCH(dev_priv) &&
15144 	    dev_priv->display.initial_watermarks)
15145 		dev_priv->display.initial_watermarks(state, crtc);
15146 }
15147 
15148 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15149 {
15150 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15151 	struct intel_crtc *crtc;
15152 	u32 handled = 0;
15153 	int i;
15154 
15155 	/* Only disable port sync and MST slaves */
15156 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15157 					    new_crtc_state, i) {
15158 		if (!needs_modeset(new_crtc_state))
15159 			continue;
15160 
15161 		if (!old_crtc_state->hw.active)
15162 			continue;
15163 
15164 		/* In case of Transcoder port Sync master slave CRTCs can be
15165 		 * assigned in any order and we need to make sure that
15166 		 * slave CRTCs are disabled first and then master CRTC since
15167 		 * Slave vblanks are masked till Master Vblanks.
15168 		 */
15169 		if (!is_trans_port_sync_slave(old_crtc_state) &&
15170 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
15171 			continue;
15172 
15173 		intel_pre_plane_update(state, crtc);
15174 		intel_old_crtc_state_disables(state, old_crtc_state,
15175 					      new_crtc_state, crtc);
15176 		handled |= BIT(crtc->pipe);
15177 	}
15178 
15179 	/* Disable everything else left on */
15180 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15181 					    new_crtc_state, i) {
15182 		if (!needs_modeset(new_crtc_state) ||
15183 		    (handled & BIT(crtc->pipe)))
15184 			continue;
15185 
15186 		intel_pre_plane_update(state, crtc);
15187 		if (old_crtc_state->hw.active)
15188 			intel_old_crtc_state_disables(state, old_crtc_state,
15189 						      new_crtc_state, crtc);
15190 	}
15191 }
15192 
15193 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
15194 {
15195 	struct intel_crtc_state *new_crtc_state;
15196 	struct intel_crtc *crtc;
15197 	int i;
15198 
15199 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15200 		if (!new_crtc_state->hw.active)
15201 			continue;
15202 
15203 		intel_enable_crtc(state, crtc);
15204 		intel_update_crtc(state, crtc);
15205 	}
15206 }
15207 
15208 static void icl_dbuf_slice_pre_update(struct intel_atomic_state *state)
15209 {
15210 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15211 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15212 	u8 required_slices = state->enabled_dbuf_slices_mask;
15213 	u8 slices_union = hw_enabled_slices | required_slices;
15214 
15215 	/* If 2nd DBuf slice required, enable it here */
15216 	if (INTEL_GEN(dev_priv) >= 11 && slices_union != hw_enabled_slices)
15217 		icl_dbuf_slices_update(dev_priv, slices_union);
15218 }
15219 
15220 static void icl_dbuf_slice_post_update(struct intel_atomic_state *state)
15221 {
15222 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15223 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15224 	u8 required_slices = state->enabled_dbuf_slices_mask;
15225 
15226 	/* If 2nd DBuf slice is no more required disable it */
15227 	if (INTEL_GEN(dev_priv) >= 11 && required_slices != hw_enabled_slices)
15228 		icl_dbuf_slices_update(dev_priv, required_slices);
15229 }
15230 
15231 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
15232 {
15233 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15234 	struct intel_crtc *crtc;
15235 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15236 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
15237 	u8 update_pipes = 0, modeset_pipes = 0;
15238 	int i;
15239 
15240 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15241 		enum pipe pipe = crtc->pipe;
15242 
15243 		if (!new_crtc_state->hw.active)
15244 			continue;
15245 
15246 		/* ignore allocations for crtc's that have been turned off. */
15247 		if (!needs_modeset(new_crtc_state)) {
15248 			entries[pipe] = old_crtc_state->wm.skl.ddb;
15249 			update_pipes |= BIT(pipe);
15250 		} else {
15251 			modeset_pipes |= BIT(pipe);
15252 		}
15253 	}
15254 
15255 	/*
15256 	 * Whenever the number of active pipes changes, we need to make sure we
15257 	 * update the pipes in the right order so that their ddb allocations
15258 	 * never overlap with each other between CRTC updates. Otherwise we'll
15259 	 * cause pipe underruns and other bad stuff.
15260 	 *
15261 	 * So first lets enable all pipes that do not need a fullmodeset as
15262 	 * those don't have any external dependency.
15263 	 */
15264 	while (update_pipes) {
15265 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15266 						    new_crtc_state, i) {
15267 			enum pipe pipe = crtc->pipe;
15268 
15269 			if ((update_pipes & BIT(pipe)) == 0)
15270 				continue;
15271 
15272 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15273 							entries, I915_MAX_PIPES, pipe))
15274 				continue;
15275 
15276 			entries[pipe] = new_crtc_state->wm.skl.ddb;
15277 			update_pipes &= ~BIT(pipe);
15278 
15279 			intel_update_crtc(state, crtc);
15280 
15281 			/*
15282 			 * If this is an already active pipe, it's DDB changed,
15283 			 * and this isn't the last pipe that needs updating
15284 			 * then we need to wait for a vblank to pass for the
15285 			 * new ddb allocation to take effect.
15286 			 */
15287 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
15288 						 &old_crtc_state->wm.skl.ddb) &&
15289 			    (update_pipes | modeset_pipes))
15290 				intel_wait_for_vblank(dev_priv, pipe);
15291 		}
15292 	}
15293 
15294 	update_pipes = modeset_pipes;
15295 
15296 	/*
15297 	 * Enable all pipes that needs a modeset and do not depends on other
15298 	 * pipes
15299 	 */
15300 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15301 		enum pipe pipe = crtc->pipe;
15302 
15303 		if ((modeset_pipes & BIT(pipe)) == 0)
15304 			continue;
15305 
15306 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
15307 		    is_trans_port_sync_master(new_crtc_state))
15308 			continue;
15309 
15310 		modeset_pipes &= ~BIT(pipe);
15311 
15312 		intel_enable_crtc(state, crtc);
15313 	}
15314 
15315 	/*
15316 	 * Then we enable all remaining pipes that depend on other
15317 	 * pipes: MST slaves and port sync masters.
15318 	 */
15319 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15320 		enum pipe pipe = crtc->pipe;
15321 
15322 		if ((modeset_pipes & BIT(pipe)) == 0)
15323 			continue;
15324 
15325 		modeset_pipes &= ~BIT(pipe);
15326 
15327 		intel_enable_crtc(state, crtc);
15328 	}
15329 
15330 	/*
15331 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
15332 	 */
15333 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15334 		enum pipe pipe = crtc->pipe;
15335 
15336 		if ((update_pipes & BIT(pipe)) == 0)
15337 			continue;
15338 
15339 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15340 									entries, I915_MAX_PIPES, pipe));
15341 
15342 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15343 		update_pipes &= ~BIT(pipe);
15344 
15345 		intel_update_crtc(state, crtc);
15346 	}
15347 
15348 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
15349 	drm_WARN_ON(&dev_priv->drm, update_pipes);
15350 }
15351 
15352 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
15353 {
15354 	struct intel_atomic_state *state, *next;
15355 	struct llist_node *freed;
15356 
15357 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
15358 	llist_for_each_entry_safe(state, next, freed, freed)
15359 		drm_atomic_state_put(&state->base);
15360 }
15361 
15362 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
15363 {
15364 	struct drm_i915_private *dev_priv =
15365 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
15366 
15367 	intel_atomic_helper_free_state(dev_priv);
15368 }
15369 
15370 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
15371 {
15372 	struct wait_queue_entry wait_fence, wait_reset;
15373 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
15374 
15375 	init_wait_entry(&wait_fence, 0);
15376 	init_wait_entry(&wait_reset, 0);
15377 	for (;;) {
15378 		prepare_to_wait(&intel_state->commit_ready.wait,
15379 				&wait_fence, TASK_UNINTERRUPTIBLE);
15380 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15381 					      I915_RESET_MODESET),
15382 				&wait_reset, TASK_UNINTERRUPTIBLE);
15383 
15384 
15385 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
15386 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
15387 			break;
15388 
15389 		schedule();
15390 	}
15391 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
15392 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15393 				  I915_RESET_MODESET),
15394 		    &wait_reset);
15395 }
15396 
15397 static void intel_atomic_cleanup_work(struct work_struct *work)
15398 {
15399 	struct drm_atomic_state *state =
15400 		container_of(work, struct drm_atomic_state, commit_work);
15401 	struct drm_i915_private *i915 = to_i915(state->dev);
15402 
15403 	drm_atomic_helper_cleanup_planes(&i915->drm, state);
15404 	drm_atomic_helper_commit_cleanup_done(state);
15405 	drm_atomic_state_put(state);
15406 
15407 	intel_atomic_helper_free_state(i915);
15408 }
15409 
15410 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
15411 {
15412 	struct drm_device *dev = state->base.dev;
15413 	struct drm_i915_private *dev_priv = to_i915(dev);
15414 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15415 	struct intel_crtc *crtc;
15416 	u64 put_domains[I915_MAX_PIPES] = {};
15417 	intel_wakeref_t wakeref = 0;
15418 	int i;
15419 
15420 	intel_atomic_commit_fence_wait(state);
15421 
15422 	drm_atomic_helper_wait_for_dependencies(&state->base);
15423 
15424 	if (state->modeset)
15425 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
15426 
15427 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15428 					    new_crtc_state, i) {
15429 		if (needs_modeset(new_crtc_state) ||
15430 		    new_crtc_state->update_pipe) {
15431 
15432 			put_domains[crtc->pipe] =
15433 				modeset_get_crtc_power_domains(new_crtc_state);
15434 		}
15435 	}
15436 
15437 	intel_commit_modeset_disables(state);
15438 
15439 	/* FIXME: Eventually get rid of our crtc->config pointer */
15440 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15441 		crtc->config = new_crtc_state;
15442 
15443 	if (state->modeset) {
15444 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
15445 
15446 		intel_set_cdclk_pre_plane_update(state);
15447 
15448 		intel_modeset_verify_disabled(dev_priv, state);
15449 	}
15450 
15451 	intel_sagv_pre_plane_update(state);
15452 
15453 	/* Complete the events for pipes that have now been disabled */
15454 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15455 		bool modeset = needs_modeset(new_crtc_state);
15456 
15457 		/* Complete events for now disable pipes here. */
15458 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
15459 			spin_lock_irq(&dev->event_lock);
15460 			drm_crtc_send_vblank_event(&crtc->base,
15461 						   new_crtc_state->uapi.event);
15462 			spin_unlock_irq(&dev->event_lock);
15463 
15464 			new_crtc_state->uapi.event = NULL;
15465 		}
15466 	}
15467 
15468 	if (state->modeset)
15469 		intel_encoders_update_prepare(state);
15470 
15471 	/* Enable all new slices, we might need */
15472 	if (state->modeset)
15473 		icl_dbuf_slice_pre_update(state);
15474 
15475 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
15476 	dev_priv->display.commit_modeset_enables(state);
15477 
15478 	if (state->modeset) {
15479 		intel_encoders_update_complete(state);
15480 
15481 		intel_set_cdclk_post_plane_update(state);
15482 	}
15483 
15484 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
15485 	 * already, but still need the state for the delayed optimization. To
15486 	 * fix this:
15487 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
15488 	 * - schedule that vblank worker _before_ calling hw_done
15489 	 * - at the start of commit_tail, cancel it _synchrously
15490 	 * - switch over to the vblank wait helper in the core after that since
15491 	 *   we don't need out special handling any more.
15492 	 */
15493 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
15494 
15495 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15496 		if (new_crtc_state->hw.active &&
15497 		    !needs_modeset(new_crtc_state) &&
15498 		    !new_crtc_state->preload_luts &&
15499 		    (new_crtc_state->uapi.color_mgmt_changed ||
15500 		     new_crtc_state->update_pipe))
15501 			intel_color_load_luts(new_crtc_state);
15502 	}
15503 
15504 	/*
15505 	 * Now that the vblank has passed, we can go ahead and program the
15506 	 * optimal watermarks on platforms that need two-step watermark
15507 	 * programming.
15508 	 *
15509 	 * TODO: Move this (and other cleanup) to an async worker eventually.
15510 	 */
15511 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15512 					    new_crtc_state, i) {
15513 		/*
15514 		 * Gen2 reports pipe underruns whenever all planes are disabled.
15515 		 * So re-enable underrun reporting after some planes get enabled.
15516 		 *
15517 		 * We do this before .optimize_watermarks() so that we have a
15518 		 * chance of catching underruns with the intermediate watermarks
15519 		 * vs. the new plane configuration.
15520 		 */
15521 		if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
15522 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15523 
15524 		if (dev_priv->display.optimize_watermarks)
15525 			dev_priv->display.optimize_watermarks(state, crtc);
15526 	}
15527 
15528 	/* Disable all slices, we don't need */
15529 	if (state->modeset)
15530 		icl_dbuf_slice_post_update(state);
15531 
15532 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15533 		intel_post_plane_update(state, crtc);
15534 
15535 		if (put_domains[i])
15536 			modeset_put_power_domains(dev_priv, put_domains[i]);
15537 
15538 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
15539 	}
15540 
15541 	/* Underruns don't always raise interrupts, so check manually */
15542 	intel_check_cpu_fifo_underruns(dev_priv);
15543 	intel_check_pch_fifo_underruns(dev_priv);
15544 
15545 	if (state->modeset)
15546 		intel_verify_planes(state);
15547 
15548 	intel_sagv_post_plane_update(state);
15549 
15550 	drm_atomic_helper_commit_hw_done(&state->base);
15551 
15552 	if (state->modeset) {
15553 		/* As one of the primary mmio accessors, KMS has a high
15554 		 * likelihood of triggering bugs in unclaimed access. After we
15555 		 * finish modesetting, see if an error has been flagged, and if
15556 		 * so enable debugging for the next modeset - and hope we catch
15557 		 * the culprit.
15558 		 */
15559 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
15560 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
15561 	}
15562 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15563 
15564 	/*
15565 	 * Defer the cleanup of the old state to a separate worker to not
15566 	 * impede the current task (userspace for blocking modesets) that
15567 	 * are executed inline. For out-of-line asynchronous modesets/flips,
15568 	 * deferring to a new worker seems overkill, but we would place a
15569 	 * schedule point (cond_resched()) here anyway to keep latencies
15570 	 * down.
15571 	 */
15572 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
15573 	queue_work(system_highpri_wq, &state->base.commit_work);
15574 }
15575 
15576 static void intel_atomic_commit_work(struct work_struct *work)
15577 {
15578 	struct intel_atomic_state *state =
15579 		container_of(work, struct intel_atomic_state, base.commit_work);
15580 
15581 	intel_atomic_commit_tail(state);
15582 }
15583 
15584 static int __i915_sw_fence_call
15585 intel_atomic_commit_ready(struct i915_sw_fence *fence,
15586 			  enum i915_sw_fence_notify notify)
15587 {
15588 	struct intel_atomic_state *state =
15589 		container_of(fence, struct intel_atomic_state, commit_ready);
15590 
15591 	switch (notify) {
15592 	case FENCE_COMPLETE:
15593 		/* we do blocking waits in the worker, nothing to do here */
15594 		break;
15595 	case FENCE_FREE:
15596 		{
15597 			struct intel_atomic_helper *helper =
15598 				&to_i915(state->base.dev)->atomic_helper;
15599 
15600 			if (llist_add(&state->freed, &helper->free_list))
15601 				schedule_work(&helper->free_work);
15602 			break;
15603 		}
15604 	}
15605 
15606 	return NOTIFY_DONE;
15607 }
15608 
15609 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
15610 {
15611 	struct intel_plane_state *old_plane_state, *new_plane_state;
15612 	struct intel_plane *plane;
15613 	int i;
15614 
15615 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15616 					     new_plane_state, i)
15617 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
15618 					to_intel_frontbuffer(new_plane_state->hw.fb),
15619 					plane->frontbuffer_bit);
15620 }
15621 
15622 static void assert_global_state_locked(struct drm_i915_private *dev_priv)
15623 {
15624 	struct intel_crtc *crtc;
15625 
15626 	for_each_intel_crtc(&dev_priv->drm, crtc)
15627 		drm_modeset_lock_assert_held(&crtc->base.mutex);
15628 }
15629 
15630 static int intel_atomic_commit(struct drm_device *dev,
15631 			       struct drm_atomic_state *_state,
15632 			       bool nonblock)
15633 {
15634 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
15635 	struct drm_i915_private *dev_priv = to_i915(dev);
15636 	int ret = 0;
15637 
15638 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
15639 
15640 	drm_atomic_state_get(&state->base);
15641 	i915_sw_fence_init(&state->commit_ready,
15642 			   intel_atomic_commit_ready);
15643 
15644 	/*
15645 	 * The intel_legacy_cursor_update() fast path takes care
15646 	 * of avoiding the vblank waits for simple cursor
15647 	 * movement and flips. For cursor on/off and size changes,
15648 	 * we want to perform the vblank waits so that watermark
15649 	 * updates happen during the correct frames. Gen9+ have
15650 	 * double buffered watermarks and so shouldn't need this.
15651 	 *
15652 	 * Unset state->legacy_cursor_update before the call to
15653 	 * drm_atomic_helper_setup_commit() because otherwise
15654 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
15655 	 * we get FIFO underruns because we didn't wait
15656 	 * for vblank.
15657 	 *
15658 	 * FIXME doing watermarks and fb cleanup from a vblank worker
15659 	 * (assuming we had any) would solve these problems.
15660 	 */
15661 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
15662 		struct intel_crtc_state *new_crtc_state;
15663 		struct intel_crtc *crtc;
15664 		int i;
15665 
15666 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15667 			if (new_crtc_state->wm.need_postvbl_update ||
15668 			    new_crtc_state->update_wm_post)
15669 				state->base.legacy_cursor_update = false;
15670 	}
15671 
15672 	ret = intel_atomic_prepare_commit(state);
15673 	if (ret) {
15674 		drm_dbg_atomic(&dev_priv->drm,
15675 			       "Preparing state failed with %i\n", ret);
15676 		i915_sw_fence_commit(&state->commit_ready);
15677 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15678 		return ret;
15679 	}
15680 
15681 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
15682 	if (!ret)
15683 		ret = drm_atomic_helper_swap_state(&state->base, true);
15684 	if (!ret)
15685 		intel_atomic_swap_global_state(state);
15686 
15687 	if (ret) {
15688 		i915_sw_fence_commit(&state->commit_ready);
15689 
15690 		drm_atomic_helper_cleanup_planes(dev, &state->base);
15691 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15692 		return ret;
15693 	}
15694 	dev_priv->wm.distrust_bios_wm = false;
15695 	intel_shared_dpll_swap_state(state);
15696 	intel_atomic_track_fbs(state);
15697 
15698 	if (state->global_state_changed) {
15699 		assert_global_state_locked(dev_priv);
15700 
15701 		dev_priv->active_pipes = state->active_pipes;
15702 	}
15703 
15704 	drm_atomic_state_get(&state->base);
15705 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
15706 
15707 	i915_sw_fence_commit(&state->commit_ready);
15708 	if (nonblock && state->modeset) {
15709 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
15710 	} else if (nonblock) {
15711 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
15712 	} else {
15713 		if (state->modeset)
15714 			flush_workqueue(dev_priv->modeset_wq);
15715 		intel_atomic_commit_tail(state);
15716 	}
15717 
15718 	return 0;
15719 }
15720 
15721 struct wait_rps_boost {
15722 	struct wait_queue_entry wait;
15723 
15724 	struct drm_crtc *crtc;
15725 	struct i915_request *request;
15726 };
15727 
15728 static int do_rps_boost(struct wait_queue_entry *_wait,
15729 			unsigned mode, int sync, void *key)
15730 {
15731 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
15732 	struct i915_request *rq = wait->request;
15733 
15734 	/*
15735 	 * If we missed the vblank, but the request is already running it
15736 	 * is reasonable to assume that it will complete before the next
15737 	 * vblank without our intervention, so leave RPS alone.
15738 	 */
15739 	if (!i915_request_started(rq))
15740 		intel_rps_boost(rq);
15741 	i915_request_put(rq);
15742 
15743 	drm_crtc_vblank_put(wait->crtc);
15744 
15745 	list_del(&wait->wait.entry);
15746 	kfree(wait);
15747 	return 1;
15748 }
15749 
15750 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
15751 				       struct dma_fence *fence)
15752 {
15753 	struct wait_rps_boost *wait;
15754 
15755 	if (!dma_fence_is_i915(fence))
15756 		return;
15757 
15758 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
15759 		return;
15760 
15761 	if (drm_crtc_vblank_get(crtc))
15762 		return;
15763 
15764 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
15765 	if (!wait) {
15766 		drm_crtc_vblank_put(crtc);
15767 		return;
15768 	}
15769 
15770 	wait->request = to_request(dma_fence_get(fence));
15771 	wait->crtc = crtc;
15772 
15773 	wait->wait.func = do_rps_boost;
15774 	wait->wait.flags = 0;
15775 
15776 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
15777 }
15778 
15779 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
15780 {
15781 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
15782 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15783 	struct drm_framebuffer *fb = plane_state->hw.fb;
15784 	struct i915_vma *vma;
15785 
15786 	if (plane->id == PLANE_CURSOR &&
15787 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
15788 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
15789 		const int align = intel_cursor_alignment(dev_priv);
15790 		int err;
15791 
15792 		err = i915_gem_object_attach_phys(obj, align);
15793 		if (err)
15794 			return err;
15795 	}
15796 
15797 	vma = intel_pin_and_fence_fb_obj(fb,
15798 					 &plane_state->view,
15799 					 intel_plane_uses_fence(plane_state),
15800 					 &plane_state->flags);
15801 	if (IS_ERR(vma))
15802 		return PTR_ERR(vma);
15803 
15804 	plane_state->vma = vma;
15805 
15806 	return 0;
15807 }
15808 
15809 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
15810 {
15811 	struct i915_vma *vma;
15812 
15813 	vma = fetch_and_zero(&old_plane_state->vma);
15814 	if (vma)
15815 		intel_unpin_fb_vma(vma, old_plane_state->flags);
15816 }
15817 
15818 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
15819 {
15820 	struct i915_sched_attr attr = {
15821 		.priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
15822 	};
15823 
15824 	i915_gem_object_wait_priority(obj, 0, &attr);
15825 }
15826 
15827 /**
15828  * intel_prepare_plane_fb - Prepare fb for usage on plane
15829  * @_plane: drm plane to prepare for
15830  * @_new_plane_state: the plane state being prepared
15831  *
15832  * Prepares a framebuffer for usage on a display plane.  Generally this
15833  * involves pinning the underlying object and updating the frontbuffer tracking
15834  * bits.  Some older platforms need special physical address handling for
15835  * cursor planes.
15836  *
15837  * Returns 0 on success, negative error code on failure.
15838  */
15839 int
15840 intel_prepare_plane_fb(struct drm_plane *_plane,
15841 		       struct drm_plane_state *_new_plane_state)
15842 {
15843 	struct intel_plane *plane = to_intel_plane(_plane);
15844 	struct intel_plane_state *new_plane_state =
15845 		to_intel_plane_state(_new_plane_state);
15846 	struct intel_atomic_state *state =
15847 		to_intel_atomic_state(new_plane_state->uapi.state);
15848 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15849 	const struct intel_plane_state *old_plane_state =
15850 		intel_atomic_get_old_plane_state(state, plane);
15851 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
15852 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
15853 	int ret;
15854 
15855 	if (old_obj) {
15856 		const struct intel_crtc_state *crtc_state =
15857 			intel_atomic_get_new_crtc_state(state,
15858 							to_intel_crtc(old_plane_state->hw.crtc));
15859 
15860 		/* Big Hammer, we also need to ensure that any pending
15861 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
15862 		 * current scanout is retired before unpinning the old
15863 		 * framebuffer. Note that we rely on userspace rendering
15864 		 * into the buffer attached to the pipe they are waiting
15865 		 * on. If not, userspace generates a GPU hang with IPEHR
15866 		 * point to the MI_WAIT_FOR_EVENT.
15867 		 *
15868 		 * This should only fail upon a hung GPU, in which case we
15869 		 * can safely continue.
15870 		 */
15871 		if (needs_modeset(crtc_state)) {
15872 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
15873 							      old_obj->base.resv, NULL,
15874 							      false, 0,
15875 							      GFP_KERNEL);
15876 			if (ret < 0)
15877 				return ret;
15878 		}
15879 	}
15880 
15881 	if (new_plane_state->uapi.fence) { /* explicit fencing */
15882 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
15883 						    new_plane_state->uapi.fence,
15884 						    i915_fence_timeout(dev_priv),
15885 						    GFP_KERNEL);
15886 		if (ret < 0)
15887 			return ret;
15888 	}
15889 
15890 	if (!obj)
15891 		return 0;
15892 
15893 	ret = i915_gem_object_pin_pages(obj);
15894 	if (ret)
15895 		return ret;
15896 
15897 	ret = intel_plane_pin_fb(new_plane_state);
15898 
15899 	i915_gem_object_unpin_pages(obj);
15900 	if (ret)
15901 		return ret;
15902 
15903 	fb_obj_bump_render_priority(obj);
15904 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
15905 
15906 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
15907 		struct dma_fence *fence;
15908 
15909 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
15910 						      obj->base.resv, NULL,
15911 						      false,
15912 						      i915_fence_timeout(dev_priv),
15913 						      GFP_KERNEL);
15914 		if (ret < 0)
15915 			goto unpin_fb;
15916 
15917 		fence = dma_resv_get_excl_rcu(obj->base.resv);
15918 		if (fence) {
15919 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
15920 						   fence);
15921 			dma_fence_put(fence);
15922 		}
15923 	} else {
15924 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
15925 					   new_plane_state->uapi.fence);
15926 	}
15927 
15928 	/*
15929 	 * We declare pageflips to be interactive and so merit a small bias
15930 	 * towards upclocking to deliver the frame on time. By only changing
15931 	 * the RPS thresholds to sample more regularly and aim for higher
15932 	 * clocks we can hopefully deliver low power workloads (like kodi)
15933 	 * that are not quite steady state without resorting to forcing
15934 	 * maximum clocks following a vblank miss (see do_rps_boost()).
15935 	 */
15936 	if (!state->rps_interactive) {
15937 		intel_rps_mark_interactive(&dev_priv->gt.rps, true);
15938 		state->rps_interactive = true;
15939 	}
15940 
15941 	return 0;
15942 
15943 unpin_fb:
15944 	intel_plane_unpin_fb(new_plane_state);
15945 
15946 	return ret;
15947 }
15948 
15949 /**
15950  * intel_cleanup_plane_fb - Cleans up an fb after plane use
15951  * @plane: drm plane to clean up for
15952  * @_old_plane_state: the state from the previous modeset
15953  *
15954  * Cleans up a framebuffer that has just been removed from a plane.
15955  */
15956 void
15957 intel_cleanup_plane_fb(struct drm_plane *plane,
15958 		       struct drm_plane_state *_old_plane_state)
15959 {
15960 	struct intel_plane_state *old_plane_state =
15961 		to_intel_plane_state(_old_plane_state);
15962 	struct intel_atomic_state *state =
15963 		to_intel_atomic_state(old_plane_state->uapi.state);
15964 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
15965 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
15966 
15967 	if (!obj)
15968 		return;
15969 
15970 	if (state->rps_interactive) {
15971 		intel_rps_mark_interactive(&dev_priv->gt.rps, false);
15972 		state->rps_interactive = false;
15973 	}
15974 
15975 	/* Should only be called after a successful intel_prepare_plane_fb()! */
15976 	intel_plane_unpin_fb(old_plane_state);
15977 }
15978 
15979 /**
15980  * intel_plane_destroy - destroy a plane
15981  * @plane: plane to destroy
15982  *
15983  * Common destruction function for all types of planes (primary, cursor,
15984  * sprite).
15985  */
15986 void intel_plane_destroy(struct drm_plane *plane)
15987 {
15988 	drm_plane_cleanup(plane);
15989 	kfree(to_intel_plane(plane));
15990 }
15991 
15992 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
15993 					    u32 format, u64 modifier)
15994 {
15995 	switch (modifier) {
15996 	case DRM_FORMAT_MOD_LINEAR:
15997 	case I915_FORMAT_MOD_X_TILED:
15998 		break;
15999 	default:
16000 		return false;
16001 	}
16002 
16003 	switch (format) {
16004 	case DRM_FORMAT_C8:
16005 	case DRM_FORMAT_RGB565:
16006 	case DRM_FORMAT_XRGB1555:
16007 	case DRM_FORMAT_XRGB8888:
16008 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16009 			modifier == I915_FORMAT_MOD_X_TILED;
16010 	default:
16011 		return false;
16012 	}
16013 }
16014 
16015 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16016 					    u32 format, u64 modifier)
16017 {
16018 	switch (modifier) {
16019 	case DRM_FORMAT_MOD_LINEAR:
16020 	case I915_FORMAT_MOD_X_TILED:
16021 		break;
16022 	default:
16023 		return false;
16024 	}
16025 
16026 	switch (format) {
16027 	case DRM_FORMAT_C8:
16028 	case DRM_FORMAT_RGB565:
16029 	case DRM_FORMAT_XRGB8888:
16030 	case DRM_FORMAT_XBGR8888:
16031 	case DRM_FORMAT_ARGB8888:
16032 	case DRM_FORMAT_ABGR8888:
16033 	case DRM_FORMAT_XRGB2101010:
16034 	case DRM_FORMAT_XBGR2101010:
16035 	case DRM_FORMAT_ARGB2101010:
16036 	case DRM_FORMAT_ABGR2101010:
16037 	case DRM_FORMAT_XBGR16161616F:
16038 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16039 			modifier == I915_FORMAT_MOD_X_TILED;
16040 	default:
16041 		return false;
16042 	}
16043 }
16044 
16045 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16046 					      u32 format, u64 modifier)
16047 {
16048 	return modifier == DRM_FORMAT_MOD_LINEAR &&
16049 		format == DRM_FORMAT_ARGB8888;
16050 }
16051 
16052 static const struct drm_plane_funcs i965_plane_funcs = {
16053 	.update_plane = drm_atomic_helper_update_plane,
16054 	.disable_plane = drm_atomic_helper_disable_plane,
16055 	.destroy = intel_plane_destroy,
16056 	.atomic_duplicate_state = intel_plane_duplicate_state,
16057 	.atomic_destroy_state = intel_plane_destroy_state,
16058 	.format_mod_supported = i965_plane_format_mod_supported,
16059 };
16060 
16061 static const struct drm_plane_funcs i8xx_plane_funcs = {
16062 	.update_plane = drm_atomic_helper_update_plane,
16063 	.disable_plane = drm_atomic_helper_disable_plane,
16064 	.destroy = intel_plane_destroy,
16065 	.atomic_duplicate_state = intel_plane_duplicate_state,
16066 	.atomic_destroy_state = intel_plane_destroy_state,
16067 	.format_mod_supported = i8xx_plane_format_mod_supported,
16068 };
16069 
16070 static int
16071 intel_legacy_cursor_update(struct drm_plane *_plane,
16072 			   struct drm_crtc *_crtc,
16073 			   struct drm_framebuffer *fb,
16074 			   int crtc_x, int crtc_y,
16075 			   unsigned int crtc_w, unsigned int crtc_h,
16076 			   u32 src_x, u32 src_y,
16077 			   u32 src_w, u32 src_h,
16078 			   struct drm_modeset_acquire_ctx *ctx)
16079 {
16080 	struct intel_plane *plane = to_intel_plane(_plane);
16081 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
16082 	struct intel_plane_state *old_plane_state =
16083 		to_intel_plane_state(plane->base.state);
16084 	struct intel_plane_state *new_plane_state;
16085 	struct intel_crtc_state *crtc_state =
16086 		to_intel_crtc_state(crtc->base.state);
16087 	struct intel_crtc_state *new_crtc_state;
16088 	int ret;
16089 
16090 	/*
16091 	 * When crtc is inactive or there is a modeset pending,
16092 	 * wait for it to complete in the slowpath
16093 	 */
16094 	if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
16095 	    crtc_state->update_pipe)
16096 		goto slow;
16097 
16098 	/*
16099 	 * Don't do an async update if there is an outstanding commit modifying
16100 	 * the plane.  This prevents our async update's changes from getting
16101 	 * overridden by a previous synchronous update's state.
16102 	 */
16103 	if (old_plane_state->uapi.commit &&
16104 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
16105 		goto slow;
16106 
16107 	/*
16108 	 * If any parameters change that may affect watermarks,
16109 	 * take the slowpath. Only changing fb or position should be
16110 	 * in the fastpath.
16111 	 */
16112 	if (old_plane_state->uapi.crtc != &crtc->base ||
16113 	    old_plane_state->uapi.src_w != src_w ||
16114 	    old_plane_state->uapi.src_h != src_h ||
16115 	    old_plane_state->uapi.crtc_w != crtc_w ||
16116 	    old_plane_state->uapi.crtc_h != crtc_h ||
16117 	    !old_plane_state->uapi.fb != !fb)
16118 		goto slow;
16119 
16120 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
16121 	if (!new_plane_state)
16122 		return -ENOMEM;
16123 
16124 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
16125 	if (!new_crtc_state) {
16126 		ret = -ENOMEM;
16127 		goto out_free;
16128 	}
16129 
16130 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
16131 
16132 	new_plane_state->uapi.src_x = src_x;
16133 	new_plane_state->uapi.src_y = src_y;
16134 	new_plane_state->uapi.src_w = src_w;
16135 	new_plane_state->uapi.src_h = src_h;
16136 	new_plane_state->uapi.crtc_x = crtc_x;
16137 	new_plane_state->uapi.crtc_y = crtc_y;
16138 	new_plane_state->uapi.crtc_w = crtc_w;
16139 	new_plane_state->uapi.crtc_h = crtc_h;
16140 
16141 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state);
16142 
16143 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
16144 						  old_plane_state, new_plane_state);
16145 	if (ret)
16146 		goto out_free;
16147 
16148 	ret = intel_plane_pin_fb(new_plane_state);
16149 	if (ret)
16150 		goto out_free;
16151 
16152 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16153 				ORIGIN_FLIP);
16154 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16155 				to_intel_frontbuffer(new_plane_state->hw.fb),
16156 				plane->frontbuffer_bit);
16157 
16158 	/* Swap plane state */
16159 	plane->base.state = &new_plane_state->uapi;
16160 
16161 	/*
16162 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
16163 	 * page flip that's running simultaneously. If we swap crtc_state and
16164 	 * destroy the old state, we will cause a use-after-free there.
16165 	 *
16166 	 * Only update active_planes, which is needed for our internal
16167 	 * bookkeeping. Either value will do the right thing when updating
16168 	 * planes atomically. If the cursor was part of the atomic update then
16169 	 * we would have taken the slowpath.
16170 	 */
16171 	crtc_state->active_planes = new_crtc_state->active_planes;
16172 
16173 	if (new_plane_state->uapi.visible)
16174 		intel_update_plane(plane, crtc_state, new_plane_state);
16175 	else
16176 		intel_disable_plane(plane, crtc_state);
16177 
16178 	intel_plane_unpin_fb(old_plane_state);
16179 
16180 out_free:
16181 	if (new_crtc_state)
16182 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
16183 	if (ret)
16184 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
16185 	else
16186 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
16187 	return ret;
16188 
16189 slow:
16190 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
16191 					      crtc_x, crtc_y, crtc_w, crtc_h,
16192 					      src_x, src_y, src_w, src_h, ctx);
16193 }
16194 
16195 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
16196 	.update_plane = intel_legacy_cursor_update,
16197 	.disable_plane = drm_atomic_helper_disable_plane,
16198 	.destroy = intel_plane_destroy,
16199 	.atomic_duplicate_state = intel_plane_duplicate_state,
16200 	.atomic_destroy_state = intel_plane_destroy_state,
16201 	.format_mod_supported = intel_cursor_format_mod_supported,
16202 };
16203 
16204 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
16205 			       enum i9xx_plane_id i9xx_plane)
16206 {
16207 	if (!HAS_FBC(dev_priv))
16208 		return false;
16209 
16210 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16211 		return i9xx_plane == PLANE_A; /* tied to pipe A */
16212 	else if (IS_IVYBRIDGE(dev_priv))
16213 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
16214 			i9xx_plane == PLANE_C;
16215 	else if (INTEL_GEN(dev_priv) >= 4)
16216 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
16217 	else
16218 		return i9xx_plane == PLANE_A;
16219 }
16220 
16221 static struct intel_plane *
16222 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
16223 {
16224 	struct intel_plane *plane;
16225 	const struct drm_plane_funcs *plane_funcs;
16226 	unsigned int supported_rotations;
16227 	const u32 *formats;
16228 	int num_formats;
16229 	int ret, zpos;
16230 
16231 	if (INTEL_GEN(dev_priv) >= 9)
16232 		return skl_universal_plane_create(dev_priv, pipe,
16233 						  PLANE_PRIMARY);
16234 
16235 	plane = intel_plane_alloc();
16236 	if (IS_ERR(plane))
16237 		return plane;
16238 
16239 	plane->pipe = pipe;
16240 	/*
16241 	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
16242 	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
16243 	 */
16244 	if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
16245 		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
16246 	else
16247 		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
16248 	plane->id = PLANE_PRIMARY;
16249 	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
16250 
16251 	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
16252 	if (plane->has_fbc) {
16253 		struct intel_fbc *fbc = &dev_priv->fbc;
16254 
16255 		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
16256 	}
16257 
16258 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16259 		formats = vlv_primary_formats;
16260 		num_formats = ARRAY_SIZE(vlv_primary_formats);
16261 	} else if (INTEL_GEN(dev_priv) >= 4) {
16262 		/*
16263 		 * WaFP16GammaEnabling:ivb
16264 		 * "Workaround : When using the 64-bit format, the plane
16265 		 *  output on each color channel has one quarter amplitude.
16266 		 *  It can be brought up to full amplitude by using pipe
16267 		 *  gamma correction or pipe color space conversion to
16268 		 *  multiply the plane output by four."
16269 		 *
16270 		 * There is no dedicated plane gamma for the primary plane,
16271 		 * and using the pipe gamma/csc could conflict with other
16272 		 * planes, so we choose not to expose fp16 on IVB primary
16273 		 * planes. HSW primary planes no longer have this problem.
16274 		 */
16275 		if (IS_IVYBRIDGE(dev_priv)) {
16276 			formats = ivb_primary_formats;
16277 			num_formats = ARRAY_SIZE(ivb_primary_formats);
16278 		} else {
16279 			formats = i965_primary_formats;
16280 			num_formats = ARRAY_SIZE(i965_primary_formats);
16281 		}
16282 	} else {
16283 		formats = i8xx_primary_formats;
16284 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
16285 	}
16286 
16287 	if (INTEL_GEN(dev_priv) >= 4)
16288 		plane_funcs = &i965_plane_funcs;
16289 	else
16290 		plane_funcs = &i8xx_plane_funcs;
16291 
16292 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16293 		plane->min_cdclk = vlv_plane_min_cdclk;
16294 	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16295 		plane->min_cdclk = hsw_plane_min_cdclk;
16296 	else if (IS_IVYBRIDGE(dev_priv))
16297 		plane->min_cdclk = ivb_plane_min_cdclk;
16298 	else
16299 		plane->min_cdclk = i9xx_plane_min_cdclk;
16300 
16301 	plane->max_stride = i9xx_plane_max_stride;
16302 	plane->update_plane = i9xx_update_plane;
16303 	plane->disable_plane = i9xx_disable_plane;
16304 	plane->get_hw_state = i9xx_plane_get_hw_state;
16305 	plane->check_plane = i9xx_plane_check;
16306 
16307 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16308 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16309 					       0, plane_funcs,
16310 					       formats, num_formats,
16311 					       i9xx_format_modifiers,
16312 					       DRM_PLANE_TYPE_PRIMARY,
16313 					       "primary %c", pipe_name(pipe));
16314 	else
16315 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16316 					       0, plane_funcs,
16317 					       formats, num_formats,
16318 					       i9xx_format_modifiers,
16319 					       DRM_PLANE_TYPE_PRIMARY,
16320 					       "plane %c",
16321 					       plane_name(plane->i9xx_plane));
16322 	if (ret)
16323 		goto fail;
16324 
16325 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
16326 		supported_rotations =
16327 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
16328 			DRM_MODE_REFLECT_X;
16329 	} else if (INTEL_GEN(dev_priv) >= 4) {
16330 		supported_rotations =
16331 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
16332 	} else {
16333 		supported_rotations = DRM_MODE_ROTATE_0;
16334 	}
16335 
16336 	if (INTEL_GEN(dev_priv) >= 4)
16337 		drm_plane_create_rotation_property(&plane->base,
16338 						   DRM_MODE_ROTATE_0,
16339 						   supported_rotations);
16340 
16341 	zpos = 0;
16342 	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
16343 
16344 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
16345 
16346 	return plane;
16347 
16348 fail:
16349 	intel_plane_free(plane);
16350 
16351 	return ERR_PTR(ret);
16352 }
16353 
16354 static struct intel_plane *
16355 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
16356 			  enum pipe pipe)
16357 {
16358 	struct intel_plane *cursor;
16359 	int ret, zpos;
16360 
16361 	cursor = intel_plane_alloc();
16362 	if (IS_ERR(cursor))
16363 		return cursor;
16364 
16365 	cursor->pipe = pipe;
16366 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
16367 	cursor->id = PLANE_CURSOR;
16368 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
16369 
16370 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16371 		cursor->max_stride = i845_cursor_max_stride;
16372 		cursor->update_plane = i845_update_cursor;
16373 		cursor->disable_plane = i845_disable_cursor;
16374 		cursor->get_hw_state = i845_cursor_get_hw_state;
16375 		cursor->check_plane = i845_check_cursor;
16376 	} else {
16377 		cursor->max_stride = i9xx_cursor_max_stride;
16378 		cursor->update_plane = i9xx_update_cursor;
16379 		cursor->disable_plane = i9xx_disable_cursor;
16380 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
16381 		cursor->check_plane = i9xx_check_cursor;
16382 	}
16383 
16384 	cursor->cursor.base = ~0;
16385 	cursor->cursor.cntl = ~0;
16386 
16387 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
16388 		cursor->cursor.size = ~0;
16389 
16390 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
16391 				       0, &intel_cursor_plane_funcs,
16392 				       intel_cursor_formats,
16393 				       ARRAY_SIZE(intel_cursor_formats),
16394 				       cursor_format_modifiers,
16395 				       DRM_PLANE_TYPE_CURSOR,
16396 				       "cursor %c", pipe_name(pipe));
16397 	if (ret)
16398 		goto fail;
16399 
16400 	if (INTEL_GEN(dev_priv) >= 4)
16401 		drm_plane_create_rotation_property(&cursor->base,
16402 						   DRM_MODE_ROTATE_0,
16403 						   DRM_MODE_ROTATE_0 |
16404 						   DRM_MODE_ROTATE_180);
16405 
16406 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
16407 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
16408 
16409 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
16410 
16411 	return cursor;
16412 
16413 fail:
16414 	intel_plane_free(cursor);
16415 
16416 	return ERR_PTR(ret);
16417 }
16418 
16419 #define INTEL_CRTC_FUNCS \
16420 	.gamma_set = drm_atomic_helper_legacy_gamma_set, \
16421 	.set_config = drm_atomic_helper_set_config, \
16422 	.destroy = intel_crtc_destroy, \
16423 	.page_flip = drm_atomic_helper_page_flip, \
16424 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
16425 	.atomic_destroy_state = intel_crtc_destroy_state, \
16426 	.set_crc_source = intel_crtc_set_crc_source, \
16427 	.verify_crc_source = intel_crtc_verify_crc_source, \
16428 	.get_crc_sources = intel_crtc_get_crc_sources
16429 
16430 static const struct drm_crtc_funcs bdw_crtc_funcs = {
16431 	INTEL_CRTC_FUNCS,
16432 
16433 	.get_vblank_counter = g4x_get_vblank_counter,
16434 	.enable_vblank = bdw_enable_vblank,
16435 	.disable_vblank = bdw_disable_vblank,
16436 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16437 };
16438 
16439 static const struct drm_crtc_funcs ilk_crtc_funcs = {
16440 	INTEL_CRTC_FUNCS,
16441 
16442 	.get_vblank_counter = g4x_get_vblank_counter,
16443 	.enable_vblank = ilk_enable_vblank,
16444 	.disable_vblank = ilk_disable_vblank,
16445 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16446 };
16447 
16448 static const struct drm_crtc_funcs g4x_crtc_funcs = {
16449 	INTEL_CRTC_FUNCS,
16450 
16451 	.get_vblank_counter = g4x_get_vblank_counter,
16452 	.enable_vblank = i965_enable_vblank,
16453 	.disable_vblank = i965_disable_vblank,
16454 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16455 };
16456 
16457 static const struct drm_crtc_funcs i965_crtc_funcs = {
16458 	INTEL_CRTC_FUNCS,
16459 
16460 	.get_vblank_counter = i915_get_vblank_counter,
16461 	.enable_vblank = i965_enable_vblank,
16462 	.disable_vblank = i965_disable_vblank,
16463 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16464 };
16465 
16466 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
16467 	INTEL_CRTC_FUNCS,
16468 
16469 	.get_vblank_counter = i915_get_vblank_counter,
16470 	.enable_vblank = i915gm_enable_vblank,
16471 	.disable_vblank = i915gm_disable_vblank,
16472 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16473 };
16474 
16475 static const struct drm_crtc_funcs i915_crtc_funcs = {
16476 	INTEL_CRTC_FUNCS,
16477 
16478 	.get_vblank_counter = i915_get_vblank_counter,
16479 	.enable_vblank = i8xx_enable_vblank,
16480 	.disable_vblank = i8xx_disable_vblank,
16481 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16482 };
16483 
16484 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
16485 	INTEL_CRTC_FUNCS,
16486 
16487 	/* no hw vblank counter */
16488 	.enable_vblank = i8xx_enable_vblank,
16489 	.disable_vblank = i8xx_disable_vblank,
16490 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16491 };
16492 
16493 static struct intel_crtc *intel_crtc_alloc(void)
16494 {
16495 	struct intel_crtc_state *crtc_state;
16496 	struct intel_crtc *crtc;
16497 
16498 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
16499 	if (!crtc)
16500 		return ERR_PTR(-ENOMEM);
16501 
16502 	crtc_state = intel_crtc_state_alloc(crtc);
16503 	if (!crtc_state) {
16504 		kfree(crtc);
16505 		return ERR_PTR(-ENOMEM);
16506 	}
16507 
16508 	crtc->base.state = &crtc_state->uapi;
16509 	crtc->config = crtc_state;
16510 
16511 	return crtc;
16512 }
16513 
16514 static void intel_crtc_free(struct intel_crtc *crtc)
16515 {
16516 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
16517 	kfree(crtc);
16518 }
16519 
16520 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
16521 {
16522 	struct intel_plane *plane;
16523 
16524 	for_each_intel_plane(&dev_priv->drm, plane) {
16525 		struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
16526 								  plane->pipe);
16527 
16528 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
16529 	}
16530 }
16531 
16532 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
16533 {
16534 	struct intel_plane *primary, *cursor;
16535 	const struct drm_crtc_funcs *funcs;
16536 	struct intel_crtc *crtc;
16537 	int sprite, ret;
16538 
16539 	crtc = intel_crtc_alloc();
16540 	if (IS_ERR(crtc))
16541 		return PTR_ERR(crtc);
16542 
16543 	crtc->pipe = pipe;
16544 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
16545 
16546 	primary = intel_primary_plane_create(dev_priv, pipe);
16547 	if (IS_ERR(primary)) {
16548 		ret = PTR_ERR(primary);
16549 		goto fail;
16550 	}
16551 	crtc->plane_ids_mask |= BIT(primary->id);
16552 
16553 	for_each_sprite(dev_priv, pipe, sprite) {
16554 		struct intel_plane *plane;
16555 
16556 		plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
16557 		if (IS_ERR(plane)) {
16558 			ret = PTR_ERR(plane);
16559 			goto fail;
16560 		}
16561 		crtc->plane_ids_mask |= BIT(plane->id);
16562 	}
16563 
16564 	cursor = intel_cursor_plane_create(dev_priv, pipe);
16565 	if (IS_ERR(cursor)) {
16566 		ret = PTR_ERR(cursor);
16567 		goto fail;
16568 	}
16569 	crtc->plane_ids_mask |= BIT(cursor->id);
16570 
16571 	if (HAS_GMCH(dev_priv)) {
16572 		if (IS_CHERRYVIEW(dev_priv) ||
16573 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
16574 			funcs = &g4x_crtc_funcs;
16575 		else if (IS_GEN(dev_priv, 4))
16576 			funcs = &i965_crtc_funcs;
16577 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
16578 			funcs = &i915gm_crtc_funcs;
16579 		else if (IS_GEN(dev_priv, 3))
16580 			funcs = &i915_crtc_funcs;
16581 		else
16582 			funcs = &i8xx_crtc_funcs;
16583 	} else {
16584 		if (INTEL_GEN(dev_priv) >= 8)
16585 			funcs = &bdw_crtc_funcs;
16586 		else
16587 			funcs = &ilk_crtc_funcs;
16588 	}
16589 
16590 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
16591 					&primary->base, &cursor->base,
16592 					funcs, "pipe %c", pipe_name(pipe));
16593 	if (ret)
16594 		goto fail;
16595 
16596 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
16597 	       dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
16598 	dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
16599 
16600 	if (INTEL_GEN(dev_priv) < 9) {
16601 		enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
16602 
16603 		BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
16604 		       dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
16605 		dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
16606 	}
16607 
16608 	intel_color_init(crtc);
16609 
16610 	intel_crtc_crc_init(crtc);
16611 
16612 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
16613 
16614 	return 0;
16615 
16616 fail:
16617 	intel_crtc_free(crtc);
16618 
16619 	return ret;
16620 }
16621 
16622 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
16623 				      struct drm_file *file)
16624 {
16625 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
16626 	struct drm_crtc *drmmode_crtc;
16627 	struct intel_crtc *crtc;
16628 
16629 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
16630 	if (!drmmode_crtc)
16631 		return -ENOENT;
16632 
16633 	crtc = to_intel_crtc(drmmode_crtc);
16634 	pipe_from_crtc_id->pipe = crtc->pipe;
16635 
16636 	return 0;
16637 }
16638 
16639 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
16640 {
16641 	struct drm_device *dev = encoder->base.dev;
16642 	struct intel_encoder *source_encoder;
16643 	u32 possible_clones = 0;
16644 
16645 	for_each_intel_encoder(dev, source_encoder) {
16646 		if (encoders_cloneable(encoder, source_encoder))
16647 			possible_clones |= drm_encoder_mask(&source_encoder->base);
16648 	}
16649 
16650 	return possible_clones;
16651 }
16652 
16653 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
16654 {
16655 	struct drm_device *dev = encoder->base.dev;
16656 	struct intel_crtc *crtc;
16657 	u32 possible_crtcs = 0;
16658 
16659 	for_each_intel_crtc(dev, crtc) {
16660 		if (encoder->pipe_mask & BIT(crtc->pipe))
16661 			possible_crtcs |= drm_crtc_mask(&crtc->base);
16662 	}
16663 
16664 	return possible_crtcs;
16665 }
16666 
16667 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
16668 {
16669 	if (!IS_MOBILE(dev_priv))
16670 		return false;
16671 
16672 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
16673 		return false;
16674 
16675 	if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
16676 		return false;
16677 
16678 	return true;
16679 }
16680 
16681 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
16682 {
16683 	if (INTEL_GEN(dev_priv) >= 9)
16684 		return false;
16685 
16686 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
16687 		return false;
16688 
16689 	if (HAS_PCH_LPT_H(dev_priv) &&
16690 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
16691 		return false;
16692 
16693 	/* DDI E can't be used if DDI A requires 4 lanes */
16694 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
16695 		return false;
16696 
16697 	if (!dev_priv->vbt.int_crt_support)
16698 		return false;
16699 
16700 	return true;
16701 }
16702 
16703 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
16704 {
16705 	int pps_num;
16706 	int pps_idx;
16707 
16708 	if (HAS_DDI(dev_priv))
16709 		return;
16710 	/*
16711 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
16712 	 * everywhere where registers can be write protected.
16713 	 */
16714 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16715 		pps_num = 2;
16716 	else
16717 		pps_num = 1;
16718 
16719 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
16720 		u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
16721 
16722 		val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
16723 		intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
16724 	}
16725 }
16726 
16727 static void intel_pps_init(struct drm_i915_private *dev_priv)
16728 {
16729 	if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
16730 		dev_priv->pps_mmio_base = PCH_PPS_BASE;
16731 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16732 		dev_priv->pps_mmio_base = VLV_PPS_BASE;
16733 	else
16734 		dev_priv->pps_mmio_base = PPS_BASE;
16735 
16736 	intel_pps_unlock_regs_wa(dev_priv);
16737 }
16738 
16739 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
16740 {
16741 	struct intel_encoder *encoder;
16742 	bool dpd_is_edp = false;
16743 
16744 	intel_pps_init(dev_priv);
16745 
16746 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
16747 		return;
16748 
16749 	if (INTEL_GEN(dev_priv) >= 12) {
16750 		intel_ddi_init(dev_priv, PORT_A);
16751 		intel_ddi_init(dev_priv, PORT_B);
16752 		intel_ddi_init(dev_priv, PORT_D);
16753 		intel_ddi_init(dev_priv, PORT_E);
16754 		intel_ddi_init(dev_priv, PORT_F);
16755 		intel_ddi_init(dev_priv, PORT_G);
16756 		intel_ddi_init(dev_priv, PORT_H);
16757 		intel_ddi_init(dev_priv, PORT_I);
16758 		icl_dsi_init(dev_priv);
16759 	} else if (IS_ELKHARTLAKE(dev_priv)) {
16760 		intel_ddi_init(dev_priv, PORT_A);
16761 		intel_ddi_init(dev_priv, PORT_B);
16762 		intel_ddi_init(dev_priv, PORT_C);
16763 		intel_ddi_init(dev_priv, PORT_D);
16764 		icl_dsi_init(dev_priv);
16765 	} else if (IS_GEN(dev_priv, 11)) {
16766 		intel_ddi_init(dev_priv, PORT_A);
16767 		intel_ddi_init(dev_priv, PORT_B);
16768 		intel_ddi_init(dev_priv, PORT_C);
16769 		intel_ddi_init(dev_priv, PORT_D);
16770 		intel_ddi_init(dev_priv, PORT_E);
16771 		/*
16772 		 * On some ICL SKUs port F is not present. No strap bits for
16773 		 * this, so rely on VBT.
16774 		 * Work around broken VBTs on SKUs known to have no port F.
16775 		 */
16776 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
16777 		    intel_bios_is_port_present(dev_priv, PORT_F))
16778 			intel_ddi_init(dev_priv, PORT_F);
16779 
16780 		icl_dsi_init(dev_priv);
16781 	} else if (IS_GEN9_LP(dev_priv)) {
16782 		/*
16783 		 * FIXME: Broxton doesn't support port detection via the
16784 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
16785 		 * detect the ports.
16786 		 */
16787 		intel_ddi_init(dev_priv, PORT_A);
16788 		intel_ddi_init(dev_priv, PORT_B);
16789 		intel_ddi_init(dev_priv, PORT_C);
16790 
16791 		vlv_dsi_init(dev_priv);
16792 	} else if (HAS_DDI(dev_priv)) {
16793 		int found;
16794 
16795 		if (intel_ddi_crt_present(dev_priv))
16796 			intel_crt_init(dev_priv);
16797 
16798 		/*
16799 		 * Haswell uses DDI functions to detect digital outputs.
16800 		 * On SKL pre-D0 the strap isn't connected, so we assume
16801 		 * it's there.
16802 		 */
16803 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
16804 		/* WaIgnoreDDIAStrap: skl */
16805 		if (found || IS_GEN9_BC(dev_priv))
16806 			intel_ddi_init(dev_priv, PORT_A);
16807 
16808 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
16809 		 * register */
16810 		found = intel_de_read(dev_priv, SFUSE_STRAP);
16811 
16812 		if (found & SFUSE_STRAP_DDIB_DETECTED)
16813 			intel_ddi_init(dev_priv, PORT_B);
16814 		if (found & SFUSE_STRAP_DDIC_DETECTED)
16815 			intel_ddi_init(dev_priv, PORT_C);
16816 		if (found & SFUSE_STRAP_DDID_DETECTED)
16817 			intel_ddi_init(dev_priv, PORT_D);
16818 		if (found & SFUSE_STRAP_DDIF_DETECTED)
16819 			intel_ddi_init(dev_priv, PORT_F);
16820 		/*
16821 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
16822 		 */
16823 		if (IS_GEN9_BC(dev_priv) &&
16824 		    intel_bios_is_port_present(dev_priv, PORT_E))
16825 			intel_ddi_init(dev_priv, PORT_E);
16826 
16827 	} else if (HAS_PCH_SPLIT(dev_priv)) {
16828 		int found;
16829 
16830 		/*
16831 		 * intel_edp_init_connector() depends on this completing first,
16832 		 * to prevent the registration of both eDP and LVDS and the
16833 		 * incorrect sharing of the PPS.
16834 		 */
16835 		intel_lvds_init(dev_priv);
16836 		intel_crt_init(dev_priv);
16837 
16838 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
16839 
16840 		if (ilk_has_edp_a(dev_priv))
16841 			intel_dp_init(dev_priv, DP_A, PORT_A);
16842 
16843 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
16844 			/* PCH SDVOB multiplex with HDMIB */
16845 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
16846 			if (!found)
16847 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
16848 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
16849 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
16850 		}
16851 
16852 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
16853 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
16854 
16855 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
16856 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
16857 
16858 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
16859 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
16860 
16861 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
16862 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
16863 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16864 		bool has_edp, has_port;
16865 
16866 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
16867 			intel_crt_init(dev_priv);
16868 
16869 		/*
16870 		 * The DP_DETECTED bit is the latched state of the DDC
16871 		 * SDA pin at boot. However since eDP doesn't require DDC
16872 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
16873 		 * eDP ports may have been muxed to an alternate function.
16874 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
16875 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
16876 		 * detect eDP ports.
16877 		 *
16878 		 * Sadly the straps seem to be missing sometimes even for HDMI
16879 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
16880 		 * and VBT for the presence of the port. Additionally we can't
16881 		 * trust the port type the VBT declares as we've seen at least
16882 		 * HDMI ports that the VBT claim are DP or eDP.
16883 		 */
16884 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
16885 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
16886 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
16887 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
16888 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
16889 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
16890 
16891 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
16892 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
16893 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
16894 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
16895 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
16896 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
16897 
16898 		if (IS_CHERRYVIEW(dev_priv)) {
16899 			/*
16900 			 * eDP not supported on port D,
16901 			 * so no need to worry about it
16902 			 */
16903 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
16904 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
16905 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
16906 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
16907 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
16908 		}
16909 
16910 		vlv_dsi_init(dev_priv);
16911 	} else if (IS_PINEVIEW(dev_priv)) {
16912 		intel_lvds_init(dev_priv);
16913 		intel_crt_init(dev_priv);
16914 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
16915 		bool found = false;
16916 
16917 		if (IS_MOBILE(dev_priv))
16918 			intel_lvds_init(dev_priv);
16919 
16920 		intel_crt_init(dev_priv);
16921 
16922 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
16923 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
16924 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
16925 			if (!found && IS_G4X(dev_priv)) {
16926 				drm_dbg_kms(&dev_priv->drm,
16927 					    "probing HDMI on SDVOB\n");
16928 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
16929 			}
16930 
16931 			if (!found && IS_G4X(dev_priv))
16932 				intel_dp_init(dev_priv, DP_B, PORT_B);
16933 		}
16934 
16935 		/* Before G4X SDVOC doesn't have its own detect register */
16936 
16937 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
16938 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
16939 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
16940 		}
16941 
16942 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
16943 
16944 			if (IS_G4X(dev_priv)) {
16945 				drm_dbg_kms(&dev_priv->drm,
16946 					    "probing HDMI on SDVOC\n");
16947 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
16948 			}
16949 			if (IS_G4X(dev_priv))
16950 				intel_dp_init(dev_priv, DP_C, PORT_C);
16951 		}
16952 
16953 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
16954 			intel_dp_init(dev_priv, DP_D, PORT_D);
16955 
16956 		if (SUPPORTS_TV(dev_priv))
16957 			intel_tv_init(dev_priv);
16958 	} else if (IS_GEN(dev_priv, 2)) {
16959 		if (IS_I85X(dev_priv))
16960 			intel_lvds_init(dev_priv);
16961 
16962 		intel_crt_init(dev_priv);
16963 		intel_dvo_init(dev_priv);
16964 	}
16965 
16966 	intel_psr_init(dev_priv);
16967 
16968 	for_each_intel_encoder(&dev_priv->drm, encoder) {
16969 		encoder->base.possible_crtcs =
16970 			intel_encoder_possible_crtcs(encoder);
16971 		encoder->base.possible_clones =
16972 			intel_encoder_possible_clones(encoder);
16973 	}
16974 
16975 	intel_init_pch_refclk(dev_priv);
16976 
16977 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
16978 }
16979 
16980 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
16981 {
16982 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
16983 
16984 	drm_framebuffer_cleanup(fb);
16985 	intel_frontbuffer_put(intel_fb->frontbuffer);
16986 
16987 	kfree(intel_fb);
16988 }
16989 
16990 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
16991 						struct drm_file *file,
16992 						unsigned int *handle)
16993 {
16994 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
16995 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
16996 
16997 	if (obj->userptr.mm) {
16998 		drm_dbg(&i915->drm,
16999 			"attempting to use a userptr for a framebuffer, denied\n");
17000 		return -EINVAL;
17001 	}
17002 
17003 	return drm_gem_handle_create(file, &obj->base, handle);
17004 }
17005 
17006 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17007 					struct drm_file *file,
17008 					unsigned flags, unsigned color,
17009 					struct drm_clip_rect *clips,
17010 					unsigned num_clips)
17011 {
17012 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17013 
17014 	i915_gem_object_flush_if_display(obj);
17015 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
17016 
17017 	return 0;
17018 }
17019 
17020 static const struct drm_framebuffer_funcs intel_fb_funcs = {
17021 	.destroy = intel_user_framebuffer_destroy,
17022 	.create_handle = intel_user_framebuffer_create_handle,
17023 	.dirty = intel_user_framebuffer_dirty,
17024 };
17025 
17026 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17027 				  struct drm_i915_gem_object *obj,
17028 				  struct drm_mode_fb_cmd2 *mode_cmd)
17029 {
17030 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
17031 	struct drm_framebuffer *fb = &intel_fb->base;
17032 	u32 max_stride;
17033 	unsigned int tiling, stride;
17034 	int ret = -EINVAL;
17035 	int i;
17036 
17037 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17038 	if (!intel_fb->frontbuffer)
17039 		return -ENOMEM;
17040 
17041 	i915_gem_object_lock(obj);
17042 	tiling = i915_gem_object_get_tiling(obj);
17043 	stride = i915_gem_object_get_stride(obj);
17044 	i915_gem_object_unlock(obj);
17045 
17046 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
17047 		/*
17048 		 * If there's a fence, enforce that
17049 		 * the fb modifier and tiling mode match.
17050 		 */
17051 		if (tiling != I915_TILING_NONE &&
17052 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17053 			drm_dbg_kms(&dev_priv->drm,
17054 				    "tiling_mode doesn't match fb modifier\n");
17055 			goto err;
17056 		}
17057 	} else {
17058 		if (tiling == I915_TILING_X) {
17059 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
17060 		} else if (tiling == I915_TILING_Y) {
17061 			drm_dbg_kms(&dev_priv->drm,
17062 				    "No Y tiling for legacy addfb\n");
17063 			goto err;
17064 		}
17065 	}
17066 
17067 	if (!drm_any_plane_has_format(&dev_priv->drm,
17068 				      mode_cmd->pixel_format,
17069 				      mode_cmd->modifier[0])) {
17070 		struct drm_format_name_buf format_name;
17071 
17072 		drm_dbg_kms(&dev_priv->drm,
17073 			    "unsupported pixel format %s / modifier 0x%llx\n",
17074 			    drm_get_format_name(mode_cmd->pixel_format,
17075 						&format_name),
17076 			    mode_cmd->modifier[0]);
17077 		goto err;
17078 	}
17079 
17080 	/*
17081 	 * gen2/3 display engine uses the fence if present,
17082 	 * so the tiling mode must match the fb modifier exactly.
17083 	 */
17084 	if (INTEL_GEN(dev_priv) < 4 &&
17085 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17086 		drm_dbg_kms(&dev_priv->drm,
17087 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
17088 		goto err;
17089 	}
17090 
17091 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17092 					 mode_cmd->modifier[0]);
17093 	if (mode_cmd->pitches[0] > max_stride) {
17094 		drm_dbg_kms(&dev_priv->drm,
17095 			    "%s pitch (%u) must be at most %d\n",
17096 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17097 			    "tiled" : "linear",
17098 			    mode_cmd->pitches[0], max_stride);
17099 		goto err;
17100 	}
17101 
17102 	/*
17103 	 * If there's a fence, enforce that
17104 	 * the fb pitch and fence stride match.
17105 	 */
17106 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
17107 		drm_dbg_kms(&dev_priv->drm,
17108 			    "pitch (%d) must match tiling stride (%d)\n",
17109 			    mode_cmd->pitches[0], stride);
17110 		goto err;
17111 	}
17112 
17113 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
17114 	if (mode_cmd->offsets[0] != 0) {
17115 		drm_dbg_kms(&dev_priv->drm,
17116 			    "plane 0 offset (0x%08x) must be 0\n",
17117 			    mode_cmd->offsets[0]);
17118 		goto err;
17119 	}
17120 
17121 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
17122 
17123 	for (i = 0; i < fb->format->num_planes; i++) {
17124 		u32 stride_alignment;
17125 
17126 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
17127 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17128 				    i);
17129 			goto err;
17130 		}
17131 
17132 		stride_alignment = intel_fb_stride_alignment(fb, i);
17133 		if (fb->pitches[i] & (stride_alignment - 1)) {
17134 			drm_dbg_kms(&dev_priv->drm,
17135 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
17136 				    i, fb->pitches[i], stride_alignment);
17137 			goto err;
17138 		}
17139 
17140 		if (is_gen12_ccs_plane(fb, i)) {
17141 			int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17142 
17143 			if (fb->pitches[i] != ccs_aux_stride) {
17144 				drm_dbg_kms(&dev_priv->drm,
17145 					    "ccs aux plane %d pitch (%d) must be %d\n",
17146 					    i,
17147 					    fb->pitches[i], ccs_aux_stride);
17148 				goto err;
17149 			}
17150 		}
17151 
17152 		fb->obj[i] = &obj->base;
17153 	}
17154 
17155 	ret = intel_fill_fb_info(dev_priv, fb);
17156 	if (ret)
17157 		goto err;
17158 
17159 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
17160 	if (ret) {
17161 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
17162 		goto err;
17163 	}
17164 
17165 	return 0;
17166 
17167 err:
17168 	intel_frontbuffer_put(intel_fb->frontbuffer);
17169 	return ret;
17170 }
17171 
17172 static struct drm_framebuffer *
17173 intel_user_framebuffer_create(struct drm_device *dev,
17174 			      struct drm_file *filp,
17175 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
17176 {
17177 	struct drm_framebuffer *fb;
17178 	struct drm_i915_gem_object *obj;
17179 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
17180 
17181 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
17182 	if (!obj)
17183 		return ERR_PTR(-ENOENT);
17184 
17185 	fb = intel_framebuffer_create(obj, &mode_cmd);
17186 	i915_gem_object_put(obj);
17187 
17188 	return fb;
17189 }
17190 
17191 static enum drm_mode_status
17192 intel_mode_valid(struct drm_device *dev,
17193 		 const struct drm_display_mode *mode)
17194 {
17195 	struct drm_i915_private *dev_priv = to_i915(dev);
17196 	int hdisplay_max, htotal_max;
17197 	int vdisplay_max, vtotal_max;
17198 
17199 	/*
17200 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
17201 	 * of DBLSCAN modes to the output's mode list when they detect
17202 	 * the scaling mode property on the connector. And they don't
17203 	 * ask the kernel to validate those modes in any way until
17204 	 * modeset time at which point the client gets a protocol error.
17205 	 * So in order to not upset those clients we silently ignore the
17206 	 * DBLSCAN flag on such connectors. For other connectors we will
17207 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
17208 	 * And we always reject DBLSCAN modes in connector->mode_valid()
17209 	 * as we never want such modes on the connector's mode list.
17210 	 */
17211 
17212 	if (mode->vscan > 1)
17213 		return MODE_NO_VSCAN;
17214 
17215 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
17216 		return MODE_H_ILLEGAL;
17217 
17218 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
17219 			   DRM_MODE_FLAG_NCSYNC |
17220 			   DRM_MODE_FLAG_PCSYNC))
17221 		return MODE_HSYNC;
17222 
17223 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
17224 			   DRM_MODE_FLAG_PIXMUX |
17225 			   DRM_MODE_FLAG_CLKDIV2))
17226 		return MODE_BAD;
17227 
17228 	/* Transcoder timing limits */
17229 	if (INTEL_GEN(dev_priv) >= 11) {
17230 		hdisplay_max = 16384;
17231 		vdisplay_max = 8192;
17232 		htotal_max = 16384;
17233 		vtotal_max = 8192;
17234 	} else if (INTEL_GEN(dev_priv) >= 9 ||
17235 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17236 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
17237 		vdisplay_max = 4096;
17238 		htotal_max = 8192;
17239 		vtotal_max = 8192;
17240 	} else if (INTEL_GEN(dev_priv) >= 3) {
17241 		hdisplay_max = 4096;
17242 		vdisplay_max = 4096;
17243 		htotal_max = 8192;
17244 		vtotal_max = 8192;
17245 	} else {
17246 		hdisplay_max = 2048;
17247 		vdisplay_max = 2048;
17248 		htotal_max = 4096;
17249 		vtotal_max = 4096;
17250 	}
17251 
17252 	if (mode->hdisplay > hdisplay_max ||
17253 	    mode->hsync_start > htotal_max ||
17254 	    mode->hsync_end > htotal_max ||
17255 	    mode->htotal > htotal_max)
17256 		return MODE_H_ILLEGAL;
17257 
17258 	if (mode->vdisplay > vdisplay_max ||
17259 	    mode->vsync_start > vtotal_max ||
17260 	    mode->vsync_end > vtotal_max ||
17261 	    mode->vtotal > vtotal_max)
17262 		return MODE_V_ILLEGAL;
17263 
17264 	if (INTEL_GEN(dev_priv) >= 5) {
17265 		if (mode->hdisplay < 64 ||
17266 		    mode->htotal - mode->hdisplay < 32)
17267 			return MODE_H_ILLEGAL;
17268 
17269 		if (mode->vtotal - mode->vdisplay < 5)
17270 			return MODE_V_ILLEGAL;
17271 	} else {
17272 		if (mode->htotal - mode->hdisplay < 32)
17273 			return MODE_H_ILLEGAL;
17274 
17275 		if (mode->vtotal - mode->vdisplay < 3)
17276 			return MODE_V_ILLEGAL;
17277 	}
17278 
17279 	return MODE_OK;
17280 }
17281 
17282 enum drm_mode_status
17283 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
17284 				const struct drm_display_mode *mode)
17285 {
17286 	int plane_width_max, plane_height_max;
17287 
17288 	/*
17289 	 * intel_mode_valid() should be
17290 	 * sufficient on older platforms.
17291 	 */
17292 	if (INTEL_GEN(dev_priv) < 9)
17293 		return MODE_OK;
17294 
17295 	/*
17296 	 * Most people will probably want a fullscreen
17297 	 * plane so let's not advertize modes that are
17298 	 * too big for that.
17299 	 */
17300 	if (INTEL_GEN(dev_priv) >= 11) {
17301 		plane_width_max = 5120;
17302 		plane_height_max = 4320;
17303 	} else {
17304 		plane_width_max = 5120;
17305 		plane_height_max = 4096;
17306 	}
17307 
17308 	if (mode->hdisplay > plane_width_max)
17309 		return MODE_H_ILLEGAL;
17310 
17311 	if (mode->vdisplay > plane_height_max)
17312 		return MODE_V_ILLEGAL;
17313 
17314 	return MODE_OK;
17315 }
17316 
17317 static const struct drm_mode_config_funcs intel_mode_funcs = {
17318 	.fb_create = intel_user_framebuffer_create,
17319 	.get_format_info = intel_get_format_info,
17320 	.output_poll_changed = intel_fbdev_output_poll_changed,
17321 	.mode_valid = intel_mode_valid,
17322 	.atomic_check = intel_atomic_check,
17323 	.atomic_commit = intel_atomic_commit,
17324 	.atomic_state_alloc = intel_atomic_state_alloc,
17325 	.atomic_state_clear = intel_atomic_state_clear,
17326 	.atomic_state_free = intel_atomic_state_free,
17327 };
17328 
17329 /**
17330  * intel_init_display_hooks - initialize the display modesetting hooks
17331  * @dev_priv: device private
17332  */
17333 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
17334 {
17335 	intel_init_cdclk_hooks(dev_priv);
17336 
17337 	if (INTEL_GEN(dev_priv) >= 9) {
17338 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17339 		dev_priv->display.get_initial_plane_config =
17340 			skl_get_initial_plane_config;
17341 		dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
17342 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17343 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17344 	} else if (HAS_DDI(dev_priv)) {
17345 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17346 		dev_priv->display.get_initial_plane_config =
17347 			i9xx_get_initial_plane_config;
17348 		dev_priv->display.crtc_compute_clock =
17349 			hsw_crtc_compute_clock;
17350 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17351 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17352 	} else if (HAS_PCH_SPLIT(dev_priv)) {
17353 		dev_priv->display.get_pipe_config = ilk_get_pipe_config;
17354 		dev_priv->display.get_initial_plane_config =
17355 			i9xx_get_initial_plane_config;
17356 		dev_priv->display.crtc_compute_clock =
17357 			ilk_crtc_compute_clock;
17358 		dev_priv->display.crtc_enable = ilk_crtc_enable;
17359 		dev_priv->display.crtc_disable = ilk_crtc_disable;
17360 	} else if (IS_CHERRYVIEW(dev_priv)) {
17361 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17362 		dev_priv->display.get_initial_plane_config =
17363 			i9xx_get_initial_plane_config;
17364 		dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
17365 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17366 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17367 	} else if (IS_VALLEYVIEW(dev_priv)) {
17368 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17369 		dev_priv->display.get_initial_plane_config =
17370 			i9xx_get_initial_plane_config;
17371 		dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
17372 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17373 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17374 	} else if (IS_G4X(dev_priv)) {
17375 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17376 		dev_priv->display.get_initial_plane_config =
17377 			i9xx_get_initial_plane_config;
17378 		dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
17379 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17380 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17381 	} else if (IS_PINEVIEW(dev_priv)) {
17382 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17383 		dev_priv->display.get_initial_plane_config =
17384 			i9xx_get_initial_plane_config;
17385 		dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
17386 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17387 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17388 	} else if (!IS_GEN(dev_priv, 2)) {
17389 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17390 		dev_priv->display.get_initial_plane_config =
17391 			i9xx_get_initial_plane_config;
17392 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
17393 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17394 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17395 	} else {
17396 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17397 		dev_priv->display.get_initial_plane_config =
17398 			i9xx_get_initial_plane_config;
17399 		dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
17400 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17401 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17402 	}
17403 
17404 	if (IS_GEN(dev_priv, 5)) {
17405 		dev_priv->display.fdi_link_train = ilk_fdi_link_train;
17406 	} else if (IS_GEN(dev_priv, 6)) {
17407 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
17408 	} else if (IS_IVYBRIDGE(dev_priv)) {
17409 		/* FIXME: detect B0+ stepping and use auto training */
17410 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
17411 	}
17412 
17413 	if (INTEL_GEN(dev_priv) >= 9)
17414 		dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
17415 	else
17416 		dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
17417 
17418 }
17419 
17420 void intel_modeset_init_hw(struct drm_i915_private *i915)
17421 {
17422 	struct intel_cdclk_state *cdclk_state =
17423 		to_intel_cdclk_state(i915->cdclk.obj.state);
17424 
17425 	intel_update_cdclk(i915);
17426 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
17427 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
17428 }
17429 
17430 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
17431 {
17432 	struct drm_plane *plane;
17433 	struct drm_crtc *crtc;
17434 
17435 	drm_for_each_crtc(crtc, state->dev) {
17436 		struct drm_crtc_state *crtc_state;
17437 
17438 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
17439 		if (IS_ERR(crtc_state))
17440 			return PTR_ERR(crtc_state);
17441 	}
17442 
17443 	drm_for_each_plane(plane, state->dev) {
17444 		struct drm_plane_state *plane_state;
17445 
17446 		plane_state = drm_atomic_get_plane_state(state, plane);
17447 		if (IS_ERR(plane_state))
17448 			return PTR_ERR(plane_state);
17449 	}
17450 
17451 	return 0;
17452 }
17453 
17454 /*
17455  * Calculate what we think the watermarks should be for the state we've read
17456  * out of the hardware and then immediately program those watermarks so that
17457  * we ensure the hardware settings match our internal state.
17458  *
17459  * We can calculate what we think WM's should be by creating a duplicate of the
17460  * current state (which was constructed during hardware readout) and running it
17461  * through the atomic check code to calculate new watermark values in the
17462  * state object.
17463  */
17464 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
17465 {
17466 	struct drm_atomic_state *state;
17467 	struct intel_atomic_state *intel_state;
17468 	struct intel_crtc *crtc;
17469 	struct intel_crtc_state *crtc_state;
17470 	struct drm_modeset_acquire_ctx ctx;
17471 	int ret;
17472 	int i;
17473 
17474 	/* Only supported on platforms that use atomic watermark design */
17475 	if (!dev_priv->display.optimize_watermarks)
17476 		return;
17477 
17478 	state = drm_atomic_state_alloc(&dev_priv->drm);
17479 	if (drm_WARN_ON(&dev_priv->drm, !state))
17480 		return;
17481 
17482 	intel_state = to_intel_atomic_state(state);
17483 
17484 	drm_modeset_acquire_init(&ctx, 0);
17485 
17486 retry:
17487 	state->acquire_ctx = &ctx;
17488 
17489 	/*
17490 	 * Hardware readout is the only time we don't want to calculate
17491 	 * intermediate watermarks (since we don't trust the current
17492 	 * watermarks).
17493 	 */
17494 	if (!HAS_GMCH(dev_priv))
17495 		intel_state->skip_intermediate_wm = true;
17496 
17497 	ret = sanitize_watermarks_add_affected(state);
17498 	if (ret)
17499 		goto fail;
17500 
17501 	ret = intel_atomic_check(&dev_priv->drm, state);
17502 	if (ret)
17503 		goto fail;
17504 
17505 	/* Write calculated watermark values back */
17506 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
17507 		crtc_state->wm.need_postvbl_update = true;
17508 		dev_priv->display.optimize_watermarks(intel_state, crtc);
17509 
17510 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
17511 	}
17512 
17513 fail:
17514 	if (ret == -EDEADLK) {
17515 		drm_atomic_state_clear(state);
17516 		drm_modeset_backoff(&ctx);
17517 		goto retry;
17518 	}
17519 
17520 	/*
17521 	 * If we fail here, it means that the hardware appears to be
17522 	 * programmed in a way that shouldn't be possible, given our
17523 	 * understanding of watermark requirements.  This might mean a
17524 	 * mistake in the hardware readout code or a mistake in the
17525 	 * watermark calculations for a given platform.  Raise a WARN
17526 	 * so that this is noticeable.
17527 	 *
17528 	 * If this actually happens, we'll have to just leave the
17529 	 * BIOS-programmed watermarks untouched and hope for the best.
17530 	 */
17531 	drm_WARN(&dev_priv->drm, ret,
17532 		 "Could not determine valid watermarks for inherited state\n");
17533 
17534 	drm_atomic_state_put(state);
17535 
17536 	drm_modeset_drop_locks(&ctx);
17537 	drm_modeset_acquire_fini(&ctx);
17538 }
17539 
17540 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
17541 {
17542 	if (IS_GEN(dev_priv, 5)) {
17543 		u32 fdi_pll_clk =
17544 			intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
17545 
17546 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
17547 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
17548 		dev_priv->fdi_pll_freq = 270000;
17549 	} else {
17550 		return;
17551 	}
17552 
17553 	drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
17554 }
17555 
17556 static int intel_initial_commit(struct drm_device *dev)
17557 {
17558 	struct drm_atomic_state *state = NULL;
17559 	struct drm_modeset_acquire_ctx ctx;
17560 	struct intel_crtc *crtc;
17561 	int ret = 0;
17562 
17563 	state = drm_atomic_state_alloc(dev);
17564 	if (!state)
17565 		return -ENOMEM;
17566 
17567 	drm_modeset_acquire_init(&ctx, 0);
17568 
17569 retry:
17570 	state->acquire_ctx = &ctx;
17571 
17572 	for_each_intel_crtc(dev, crtc) {
17573 		struct intel_crtc_state *crtc_state =
17574 			intel_atomic_get_crtc_state(state, crtc);
17575 
17576 		if (IS_ERR(crtc_state)) {
17577 			ret = PTR_ERR(crtc_state);
17578 			goto out;
17579 		}
17580 
17581 		if (crtc_state->hw.active) {
17582 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
17583 			if (ret)
17584 				goto out;
17585 
17586 			/*
17587 			 * FIXME hack to force a LUT update to avoid the
17588 			 * plane update forcing the pipe gamma on without
17589 			 * having a proper LUT loaded. Remove once we
17590 			 * have readout for pipe gamma enable.
17591 			 */
17592 			crtc_state->uapi.color_mgmt_changed = true;
17593 
17594 			/*
17595 			 * FIXME hack to force full modeset when DSC is being
17596 			 * used.
17597 			 *
17598 			 * As long as we do not have full state readout and
17599 			 * config comparison of crtc_state->dsc, we have no way
17600 			 * to ensure reliable fastset. Remove once we have
17601 			 * readout for DSC.
17602 			 */
17603 			if (crtc_state->dsc.compression_enable) {
17604 				ret = drm_atomic_add_affected_connectors(state,
17605 									 &crtc->base);
17606 				if (ret)
17607 					goto out;
17608 				crtc_state->uapi.mode_changed = true;
17609 				drm_dbg_kms(dev, "Force full modeset for DSC\n");
17610 			}
17611 		}
17612 	}
17613 
17614 	ret = drm_atomic_commit(state);
17615 
17616 out:
17617 	if (ret == -EDEADLK) {
17618 		drm_atomic_state_clear(state);
17619 		drm_modeset_backoff(&ctx);
17620 		goto retry;
17621 	}
17622 
17623 	drm_atomic_state_put(state);
17624 
17625 	drm_modeset_drop_locks(&ctx);
17626 	drm_modeset_acquire_fini(&ctx);
17627 
17628 	return ret;
17629 }
17630 
17631 static void intel_mode_config_init(struct drm_i915_private *i915)
17632 {
17633 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
17634 
17635 	drm_mode_config_init(&i915->drm);
17636 	INIT_LIST_HEAD(&i915->global_obj_list);
17637 
17638 	mode_config->min_width = 0;
17639 	mode_config->min_height = 0;
17640 
17641 	mode_config->preferred_depth = 24;
17642 	mode_config->prefer_shadow = 1;
17643 
17644 	mode_config->allow_fb_modifiers = true;
17645 
17646 	mode_config->funcs = &intel_mode_funcs;
17647 
17648 	/*
17649 	 * Maximum framebuffer dimensions, chosen to match
17650 	 * the maximum render engine surface size on gen4+.
17651 	 */
17652 	if (INTEL_GEN(i915) >= 7) {
17653 		mode_config->max_width = 16384;
17654 		mode_config->max_height = 16384;
17655 	} else if (INTEL_GEN(i915) >= 4) {
17656 		mode_config->max_width = 8192;
17657 		mode_config->max_height = 8192;
17658 	} else if (IS_GEN(i915, 3)) {
17659 		mode_config->max_width = 4096;
17660 		mode_config->max_height = 4096;
17661 	} else {
17662 		mode_config->max_width = 2048;
17663 		mode_config->max_height = 2048;
17664 	}
17665 
17666 	if (IS_I845G(i915) || IS_I865G(i915)) {
17667 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
17668 		mode_config->cursor_height = 1023;
17669 	} else if (IS_GEN(i915, 2)) {
17670 		mode_config->cursor_width = 64;
17671 		mode_config->cursor_height = 64;
17672 	} else {
17673 		mode_config->cursor_width = 256;
17674 		mode_config->cursor_height = 256;
17675 	}
17676 }
17677 
17678 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
17679 {
17680 	intel_atomic_global_obj_cleanup(i915);
17681 	drm_mode_config_cleanup(&i915->drm);
17682 }
17683 
17684 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
17685 {
17686 	if (plane_config->fb) {
17687 		struct drm_framebuffer *fb = &plane_config->fb->base;
17688 
17689 		/* We may only have the stub and not a full framebuffer */
17690 		if (drm_framebuffer_read_refcount(fb))
17691 			drm_framebuffer_put(fb);
17692 		else
17693 			kfree(fb);
17694 	}
17695 
17696 	if (plane_config->vma)
17697 		i915_vma_put(plane_config->vma);
17698 }
17699 
17700 /* part #1: call before irq install */
17701 int intel_modeset_init_noirq(struct drm_i915_private *i915)
17702 {
17703 	int ret;
17704 
17705 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
17706 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
17707 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
17708 
17709 	intel_mode_config_init(i915);
17710 
17711 	ret = intel_cdclk_init(i915);
17712 	if (ret)
17713 		return ret;
17714 
17715 	ret = intel_bw_init(i915);
17716 	if (ret)
17717 		return ret;
17718 
17719 	init_llist_head(&i915->atomic_helper.free_list);
17720 	INIT_WORK(&i915->atomic_helper.free_work,
17721 		  intel_atomic_helper_free_state_worker);
17722 
17723 	intel_init_quirks(i915);
17724 
17725 	intel_fbc_init(i915);
17726 
17727 	return 0;
17728 }
17729 
17730 /* part #2: call after irq install */
17731 int intel_modeset_init(struct drm_i915_private *i915)
17732 {
17733 	struct drm_device *dev = &i915->drm;
17734 	enum pipe pipe;
17735 	struct intel_crtc *crtc;
17736 	int ret;
17737 
17738 	intel_init_pm(i915);
17739 
17740 	intel_panel_sanitize_ssc(i915);
17741 
17742 	intel_gmbus_setup(i915);
17743 
17744 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
17745 		    INTEL_NUM_PIPES(i915),
17746 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
17747 
17748 	if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
17749 		for_each_pipe(i915, pipe) {
17750 			ret = intel_crtc_init(i915, pipe);
17751 			if (ret) {
17752 				intel_mode_config_cleanup(i915);
17753 				return ret;
17754 			}
17755 		}
17756 	}
17757 
17758 	intel_plane_possible_crtcs_init(i915);
17759 	intel_shared_dpll_init(dev);
17760 	intel_update_fdi_pll_freq(i915);
17761 
17762 	intel_update_czclk(i915);
17763 	intel_modeset_init_hw(i915);
17764 
17765 	intel_hdcp_component_init(i915);
17766 
17767 	if (i915->max_cdclk_freq == 0)
17768 		intel_update_max_cdclk(i915);
17769 
17770 	/* Just disable it once at startup */
17771 	intel_vga_disable(i915);
17772 	intel_setup_outputs(i915);
17773 
17774 	drm_modeset_lock_all(dev);
17775 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
17776 	drm_modeset_unlock_all(dev);
17777 
17778 	for_each_intel_crtc(dev, crtc) {
17779 		struct intel_initial_plane_config plane_config = {};
17780 
17781 		if (!crtc->active)
17782 			continue;
17783 
17784 		/*
17785 		 * Note that reserving the BIOS fb up front prevents us
17786 		 * from stuffing other stolen allocations like the ring
17787 		 * on top.  This prevents some ugliness at boot time, and
17788 		 * can even allow for smooth boot transitions if the BIOS
17789 		 * fb is large enough for the active pipe configuration.
17790 		 */
17791 		i915->display.get_initial_plane_config(crtc, &plane_config);
17792 
17793 		/*
17794 		 * If the fb is shared between multiple heads, we'll
17795 		 * just get the first one.
17796 		 */
17797 		intel_find_initial_plane_obj(crtc, &plane_config);
17798 
17799 		plane_config_fini(&plane_config);
17800 	}
17801 
17802 	/*
17803 	 * Make sure hardware watermarks really match the state we read out.
17804 	 * Note that we need to do this after reconstructing the BIOS fb's
17805 	 * since the watermark calculation done here will use pstate->fb.
17806 	 */
17807 	if (!HAS_GMCH(i915))
17808 		sanitize_watermarks(i915);
17809 
17810 	/*
17811 	 * Force all active planes to recompute their states. So that on
17812 	 * mode_setcrtc after probe, all the intel_plane_state variables
17813 	 * are already calculated and there is no assert_plane warnings
17814 	 * during bootup.
17815 	 */
17816 	ret = intel_initial_commit(dev);
17817 	if (ret)
17818 		drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
17819 
17820 	return 0;
17821 }
17822 
17823 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17824 {
17825 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17826 	/* 640x480@60Hz, ~25175 kHz */
17827 	struct dpll clock = {
17828 		.m1 = 18,
17829 		.m2 = 7,
17830 		.p1 = 13,
17831 		.p2 = 4,
17832 		.n = 2,
17833 	};
17834 	u32 dpll, fp;
17835 	int i;
17836 
17837 	drm_WARN_ON(&dev_priv->drm,
17838 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
17839 
17840 	drm_dbg_kms(&dev_priv->drm,
17841 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
17842 		    pipe_name(pipe), clock.vco, clock.dot);
17843 
17844 	fp = i9xx_dpll_compute_fp(&clock);
17845 	dpll = DPLL_DVO_2X_MODE |
17846 		DPLL_VGA_MODE_DIS |
17847 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
17848 		PLL_P2_DIVIDE_BY_4 |
17849 		PLL_REF_INPUT_DREFCLK |
17850 		DPLL_VCO_ENABLE;
17851 
17852 	intel_de_write(dev_priv, FP0(pipe), fp);
17853 	intel_de_write(dev_priv, FP1(pipe), fp);
17854 
17855 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
17856 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
17857 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
17858 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
17859 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
17860 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
17861 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
17862 
17863 	/*
17864 	 * Apparently we need to have VGA mode enabled prior to changing
17865 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
17866 	 * dividers, even though the register value does change.
17867 	 */
17868 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
17869 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17870 
17871 	/* Wait for the clocks to stabilize. */
17872 	intel_de_posting_read(dev_priv, DPLL(pipe));
17873 	udelay(150);
17874 
17875 	/* The pixel multiplier can only be updated once the
17876 	 * DPLL is enabled and the clocks are stable.
17877 	 *
17878 	 * So write it again.
17879 	 */
17880 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17881 
17882 	/* We do this three times for luck */
17883 	for (i = 0; i < 3 ; i++) {
17884 		intel_de_write(dev_priv, DPLL(pipe), dpll);
17885 		intel_de_posting_read(dev_priv, DPLL(pipe));
17886 		udelay(150); /* wait for warmup */
17887 	}
17888 
17889 	intel_de_write(dev_priv, PIPECONF(pipe),
17890 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
17891 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
17892 
17893 	intel_wait_for_pipe_scanline_moving(crtc);
17894 }
17895 
17896 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17897 {
17898 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17899 
17900 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
17901 		    pipe_name(pipe));
17902 
17903 	drm_WARN_ON(&dev_priv->drm,
17904 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
17905 		    DISPLAY_PLANE_ENABLE);
17906 	drm_WARN_ON(&dev_priv->drm,
17907 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
17908 		    DISPLAY_PLANE_ENABLE);
17909 	drm_WARN_ON(&dev_priv->drm,
17910 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
17911 		    DISPLAY_PLANE_ENABLE);
17912 	drm_WARN_ON(&dev_priv->drm,
17913 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
17914 	drm_WARN_ON(&dev_priv->drm,
17915 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
17916 
17917 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
17918 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
17919 
17920 	intel_wait_for_pipe_scanline_stopped(crtc);
17921 
17922 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
17923 	intel_de_posting_read(dev_priv, DPLL(pipe));
17924 }
17925 
17926 static void
17927 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
17928 {
17929 	struct intel_crtc *crtc;
17930 
17931 	if (INTEL_GEN(dev_priv) >= 4)
17932 		return;
17933 
17934 	for_each_intel_crtc(&dev_priv->drm, crtc) {
17935 		struct intel_plane *plane =
17936 			to_intel_plane(crtc->base.primary);
17937 		struct intel_crtc *plane_crtc;
17938 		enum pipe pipe;
17939 
17940 		if (!plane->get_hw_state(plane, &pipe))
17941 			continue;
17942 
17943 		if (pipe == crtc->pipe)
17944 			continue;
17945 
17946 		drm_dbg_kms(&dev_priv->drm,
17947 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
17948 			    plane->base.base.id, plane->base.name);
17949 
17950 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17951 		intel_plane_disable_noatomic(plane_crtc, plane);
17952 	}
17953 }
17954 
17955 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
17956 {
17957 	struct drm_device *dev = crtc->base.dev;
17958 	struct intel_encoder *encoder;
17959 
17960 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
17961 		return true;
17962 
17963 	return false;
17964 }
17965 
17966 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
17967 {
17968 	struct drm_device *dev = encoder->base.dev;
17969 	struct intel_connector *connector;
17970 
17971 	for_each_connector_on_encoder(dev, &encoder->base, connector)
17972 		return connector;
17973 
17974 	return NULL;
17975 }
17976 
17977 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
17978 			      enum pipe pch_transcoder)
17979 {
17980 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
17981 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
17982 }
17983 
17984 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
17985 {
17986 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
17987 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
17988 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
17989 
17990 	if (INTEL_GEN(dev_priv) >= 9 ||
17991 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17992 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
17993 		u32 val;
17994 
17995 		if (transcoder_is_dsi(cpu_transcoder))
17996 			return;
17997 
17998 		val = intel_de_read(dev_priv, reg);
17999 		val &= ~HSW_FRAME_START_DELAY_MASK;
18000 		val |= HSW_FRAME_START_DELAY(0);
18001 		intel_de_write(dev_priv, reg, val);
18002 	} else {
18003 		i915_reg_t reg = PIPECONF(cpu_transcoder);
18004 		u32 val;
18005 
18006 		val = intel_de_read(dev_priv, reg);
18007 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18008 		val |= PIPECONF_FRAME_START_DELAY(0);
18009 		intel_de_write(dev_priv, reg, val);
18010 	}
18011 
18012 	if (!crtc_state->has_pch_encoder)
18013 		return;
18014 
18015 	if (HAS_PCH_IBX(dev_priv)) {
18016 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18017 		u32 val;
18018 
18019 		val = intel_de_read(dev_priv, reg);
18020 		val &= ~TRANS_FRAME_START_DELAY_MASK;
18021 		val |= TRANS_FRAME_START_DELAY(0);
18022 		intel_de_write(dev_priv, reg, val);
18023 	} else {
18024 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18025 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
18026 		u32 val;
18027 
18028 		val = intel_de_read(dev_priv, reg);
18029 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18030 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
18031 		intel_de_write(dev_priv, reg, val);
18032 	}
18033 }
18034 
18035 static void intel_sanitize_crtc(struct intel_crtc *crtc,
18036 				struct drm_modeset_acquire_ctx *ctx)
18037 {
18038 	struct drm_device *dev = crtc->base.dev;
18039 	struct drm_i915_private *dev_priv = to_i915(dev);
18040 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
18041 
18042 	if (crtc_state->hw.active) {
18043 		struct intel_plane *plane;
18044 
18045 		/* Clear any frame start delays used for debugging left by the BIOS */
18046 		intel_sanitize_frame_start_delay(crtc_state);
18047 
18048 		/* Disable everything but the primary plane */
18049 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
18050 			const struct intel_plane_state *plane_state =
18051 				to_intel_plane_state(plane->base.state);
18052 
18053 			if (plane_state->uapi.visible &&
18054 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18055 				intel_plane_disable_noatomic(crtc, plane);
18056 		}
18057 
18058 		/*
18059 		 * Disable any background color set by the BIOS, but enable the
18060 		 * gamma and CSC to match how we program our planes.
18061 		 */
18062 		if (INTEL_GEN(dev_priv) >= 9)
18063 			intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
18064 				       SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
18065 	}
18066 
18067 	/* Adjust the state of the output pipe according to whether we
18068 	 * have active connectors/encoders. */
18069 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc))
18070 		intel_crtc_disable_noatomic(crtc, ctx);
18071 
18072 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
18073 		/*
18074 		 * We start out with underrun reporting disabled to avoid races.
18075 		 * For correct bookkeeping mark this on active crtcs.
18076 		 *
18077 		 * Also on gmch platforms we dont have any hardware bits to
18078 		 * disable the underrun reporting. Which means we need to start
18079 		 * out with underrun reporting disabled also on inactive pipes,
18080 		 * since otherwise we'll complain about the garbage we read when
18081 		 * e.g. coming up after runtime pm.
18082 		 *
18083 		 * No protection against concurrent access is required - at
18084 		 * worst a fifo underrun happens which also sets this to false.
18085 		 */
18086 		crtc->cpu_fifo_underrun_disabled = true;
18087 		/*
18088 		 * We track the PCH trancoder underrun reporting state
18089 		 * within the crtc. With crtc for pipe A housing the underrun
18090 		 * reporting state for PCH transcoder A, crtc for pipe B housing
18091 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
18092 		 * and marking underrun reporting as disabled for the non-existing
18093 		 * PCH transcoders B and C would prevent enabling the south
18094 		 * error interrupt (see cpt_can_enable_serr_int()).
18095 		 */
18096 		if (has_pch_trancoder(dev_priv, crtc->pipe))
18097 			crtc->pch_fifo_underrun_disabled = true;
18098 	}
18099 }
18100 
18101 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
18102 {
18103 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
18104 
18105 	/*
18106 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
18107 	 * the hardware when a high res displays plugged in. DPLL P
18108 	 * divider is zero, and the pipe timings are bonkers. We'll
18109 	 * try to disable everything in that case.
18110 	 *
18111 	 * FIXME would be nice to be able to sanitize this state
18112 	 * without several WARNs, but for now let's take the easy
18113 	 * road.
18114 	 */
18115 	return IS_GEN(dev_priv, 6) &&
18116 		crtc_state->hw.active &&
18117 		crtc_state->shared_dpll &&
18118 		crtc_state->port_clock == 0;
18119 }
18120 
18121 static void intel_sanitize_encoder(struct intel_encoder *encoder)
18122 {
18123 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
18124 	struct intel_connector *connector;
18125 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
18126 	struct intel_crtc_state *crtc_state = crtc ?
18127 		to_intel_crtc_state(crtc->base.state) : NULL;
18128 
18129 	/* We need to check both for a crtc link (meaning that the
18130 	 * encoder is active and trying to read from a pipe) and the
18131 	 * pipe itself being active. */
18132 	bool has_active_crtc = crtc_state &&
18133 		crtc_state->hw.active;
18134 
18135 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
18136 		drm_dbg_kms(&dev_priv->drm,
18137 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
18138 			    pipe_name(crtc->pipe));
18139 		has_active_crtc = false;
18140 	}
18141 
18142 	connector = intel_encoder_find_connector(encoder);
18143 	if (connector && !has_active_crtc) {
18144 		drm_dbg_kms(&dev_priv->drm,
18145 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
18146 			    encoder->base.base.id,
18147 			    encoder->base.name);
18148 
18149 		/* Connector is active, but has no active pipe. This is
18150 		 * fallout from our resume register restoring. Disable
18151 		 * the encoder manually again. */
18152 		if (crtc_state) {
18153 			struct drm_encoder *best_encoder;
18154 
18155 			drm_dbg_kms(&dev_priv->drm,
18156 				    "[ENCODER:%d:%s] manually disabled\n",
18157 				    encoder->base.base.id,
18158 				    encoder->base.name);
18159 
18160 			/* avoid oopsing in case the hooks consult best_encoder */
18161 			best_encoder = connector->base.state->best_encoder;
18162 			connector->base.state->best_encoder = &encoder->base;
18163 
18164 			/* FIXME NULL atomic state passed! */
18165 			if (encoder->disable)
18166 				encoder->disable(NULL, encoder, crtc_state,
18167 						 connector->base.state);
18168 			if (encoder->post_disable)
18169 				encoder->post_disable(NULL, encoder, crtc_state,
18170 						      connector->base.state);
18171 
18172 			connector->base.state->best_encoder = best_encoder;
18173 		}
18174 		encoder->base.crtc = NULL;
18175 
18176 		/* Inconsistent output/port/pipe state happens presumably due to
18177 		 * a bug in one of the get_hw_state functions. Or someplace else
18178 		 * in our code, like the register restore mess on resume. Clamp
18179 		 * things to off as a safer default. */
18180 
18181 		connector->base.dpms = DRM_MODE_DPMS_OFF;
18182 		connector->base.encoder = NULL;
18183 	}
18184 
18185 	/* notify opregion of the sanitized encoder state */
18186 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
18187 
18188 	if (INTEL_GEN(dev_priv) >= 11)
18189 		icl_sanitize_encoder_pll_mapping(encoder);
18190 }
18191 
18192 /* FIXME read out full plane state for all planes */
18193 static void readout_plane_state(struct drm_i915_private *dev_priv)
18194 {
18195 	struct intel_plane *plane;
18196 	struct intel_crtc *crtc;
18197 
18198 	for_each_intel_plane(&dev_priv->drm, plane) {
18199 		struct intel_plane_state *plane_state =
18200 			to_intel_plane_state(plane->base.state);
18201 		struct intel_crtc_state *crtc_state;
18202 		enum pipe pipe = PIPE_A;
18203 		bool visible;
18204 
18205 		visible = plane->get_hw_state(plane, &pipe);
18206 
18207 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18208 		crtc_state = to_intel_crtc_state(crtc->base.state);
18209 
18210 		intel_set_plane_visible(crtc_state, plane_state, visible);
18211 
18212 		drm_dbg_kms(&dev_priv->drm,
18213 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
18214 			    plane->base.base.id, plane->base.name,
18215 			    enableddisabled(visible), pipe_name(pipe));
18216 	}
18217 
18218 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18219 		struct intel_crtc_state *crtc_state =
18220 			to_intel_crtc_state(crtc->base.state);
18221 
18222 		fixup_active_planes(crtc_state);
18223 	}
18224 }
18225 
18226 static void intel_modeset_readout_hw_state(struct drm_device *dev)
18227 {
18228 	struct drm_i915_private *dev_priv = to_i915(dev);
18229 	struct intel_cdclk_state *cdclk_state =
18230 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
18231 	enum pipe pipe;
18232 	struct intel_crtc *crtc;
18233 	struct intel_encoder *encoder;
18234 	struct intel_connector *connector;
18235 	struct drm_connector_list_iter conn_iter;
18236 	u8 active_pipes = 0;
18237 
18238 	for_each_intel_crtc(dev, crtc) {
18239 		struct intel_crtc_state *crtc_state =
18240 			to_intel_crtc_state(crtc->base.state);
18241 
18242 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
18243 		intel_crtc_free_hw_state(crtc_state);
18244 		intel_crtc_state_reset(crtc_state, crtc);
18245 
18246 		crtc_state->hw.active = crtc_state->hw.enable =
18247 			dev_priv->display.get_pipe_config(crtc, crtc_state);
18248 
18249 		crtc->base.enabled = crtc_state->hw.enable;
18250 		crtc->active = crtc_state->hw.active;
18251 
18252 		if (crtc_state->hw.active)
18253 			active_pipes |= BIT(crtc->pipe);
18254 
18255 		drm_dbg_kms(&dev_priv->drm,
18256 			    "[CRTC:%d:%s] hw state readout: %s\n",
18257 			    crtc->base.base.id, crtc->base.name,
18258 			    enableddisabled(crtc_state->hw.active));
18259 	}
18260 
18261 	dev_priv->active_pipes = cdclk_state->active_pipes = active_pipes;
18262 
18263 	readout_plane_state(dev_priv);
18264 
18265 	intel_dpll_readout_hw_state(dev_priv);
18266 
18267 	for_each_intel_encoder(dev, encoder) {
18268 		pipe = 0;
18269 
18270 		if (encoder->get_hw_state(encoder, &pipe)) {
18271 			struct intel_crtc_state *crtc_state;
18272 
18273 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18274 			crtc_state = to_intel_crtc_state(crtc->base.state);
18275 
18276 			encoder->base.crtc = &crtc->base;
18277 			encoder->get_config(encoder, crtc_state);
18278 		} else {
18279 			encoder->base.crtc = NULL;
18280 		}
18281 
18282 		drm_dbg_kms(&dev_priv->drm,
18283 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
18284 			    encoder->base.base.id, encoder->base.name,
18285 			    enableddisabled(encoder->base.crtc),
18286 			    pipe_name(pipe));
18287 	}
18288 
18289 	drm_connector_list_iter_begin(dev, &conn_iter);
18290 	for_each_intel_connector_iter(connector, &conn_iter) {
18291 		if (connector->get_hw_state(connector)) {
18292 			struct intel_crtc_state *crtc_state;
18293 			struct intel_crtc *crtc;
18294 
18295 			connector->base.dpms = DRM_MODE_DPMS_ON;
18296 
18297 			encoder = intel_attached_encoder(connector);
18298 			connector->base.encoder = &encoder->base;
18299 
18300 			crtc = to_intel_crtc(encoder->base.crtc);
18301 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
18302 
18303 			if (crtc_state && crtc_state->hw.active) {
18304 				/*
18305 				 * This has to be done during hardware readout
18306 				 * because anything calling .crtc_disable may
18307 				 * rely on the connector_mask being accurate.
18308 				 */
18309 				crtc_state->uapi.connector_mask |=
18310 					drm_connector_mask(&connector->base);
18311 				crtc_state->uapi.encoder_mask |=
18312 					drm_encoder_mask(&encoder->base);
18313 			}
18314 		} else {
18315 			connector->base.dpms = DRM_MODE_DPMS_OFF;
18316 			connector->base.encoder = NULL;
18317 		}
18318 		drm_dbg_kms(&dev_priv->drm,
18319 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
18320 			    connector->base.base.id, connector->base.name,
18321 			    enableddisabled(connector->base.encoder));
18322 	}
18323 	drm_connector_list_iter_end(&conn_iter);
18324 
18325 	for_each_intel_crtc(dev, crtc) {
18326 		struct intel_bw_state *bw_state =
18327 			to_intel_bw_state(dev_priv->bw_obj.state);
18328 		struct intel_crtc_state *crtc_state =
18329 			to_intel_crtc_state(crtc->base.state);
18330 		struct intel_plane *plane;
18331 		int min_cdclk = 0;
18332 
18333 		if (crtc_state->hw.active) {
18334 			struct drm_display_mode *mode = &crtc_state->hw.mode;
18335 
18336 			intel_mode_from_pipe_config(&crtc_state->hw.adjusted_mode,
18337 						    crtc_state);
18338 
18339 			*mode = crtc_state->hw.adjusted_mode;
18340 			mode->hdisplay = crtc_state->pipe_src_w;
18341 			mode->vdisplay = crtc_state->pipe_src_h;
18342 
18343 			/*
18344 			 * The initial mode needs to be set in order to keep
18345 			 * the atomic core happy. It wants a valid mode if the
18346 			 * crtc's enabled, so we do the above call.
18347 			 *
18348 			 * But we don't set all the derived state fully, hence
18349 			 * set a flag to indicate that a full recalculation is
18350 			 * needed on the next commit.
18351 			 */
18352 			mode->private_flags = I915_MODE_FLAG_INHERITED;
18353 
18354 			intel_crtc_compute_pixel_rate(crtc_state);
18355 
18356 			intel_crtc_update_active_timings(crtc_state);
18357 
18358 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
18359 		}
18360 
18361 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
18362 			const struct intel_plane_state *plane_state =
18363 				to_intel_plane_state(plane->base.state);
18364 
18365 			/*
18366 			 * FIXME don't have the fb yet, so can't
18367 			 * use intel_plane_data_rate() :(
18368 			 */
18369 			if (plane_state->uapi.visible)
18370 				crtc_state->data_rate[plane->id] =
18371 					4 * crtc_state->pixel_rate;
18372 			/*
18373 			 * FIXME don't have the fb yet, so can't
18374 			 * use plane->min_cdclk() :(
18375 			 */
18376 			if (plane_state->uapi.visible && plane->min_cdclk) {
18377 				if (crtc_state->double_wide ||
18378 				    INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
18379 					crtc_state->min_cdclk[plane->id] =
18380 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
18381 				else
18382 					crtc_state->min_cdclk[plane->id] =
18383 						crtc_state->pixel_rate;
18384 			}
18385 			drm_dbg_kms(&dev_priv->drm,
18386 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
18387 				    plane->base.base.id, plane->base.name,
18388 				    crtc_state->min_cdclk[plane->id]);
18389 		}
18390 
18391 		if (crtc_state->hw.active) {
18392 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
18393 			if (drm_WARN_ON(dev, min_cdclk < 0))
18394 				min_cdclk = 0;
18395 		}
18396 
18397 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
18398 		cdclk_state->min_voltage_level[crtc->pipe] =
18399 			crtc_state->min_voltage_level;
18400 
18401 		intel_bw_crtc_update(bw_state, crtc_state);
18402 
18403 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
18404 	}
18405 }
18406 
18407 static void
18408 get_encoder_power_domains(struct drm_i915_private *dev_priv)
18409 {
18410 	struct intel_encoder *encoder;
18411 
18412 	for_each_intel_encoder(&dev_priv->drm, encoder) {
18413 		struct intel_crtc_state *crtc_state;
18414 
18415 		if (!encoder->get_power_domains)
18416 			continue;
18417 
18418 		/*
18419 		 * MST-primary and inactive encoders don't have a crtc state
18420 		 * and neither of these require any power domain references.
18421 		 */
18422 		if (!encoder->base.crtc)
18423 			continue;
18424 
18425 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
18426 		encoder->get_power_domains(encoder, crtc_state);
18427 	}
18428 }
18429 
18430 static void intel_early_display_was(struct drm_i915_private *dev_priv)
18431 {
18432 	/*
18433 	 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
18434 	 * Also known as Wa_14010480278.
18435 	 */
18436 	if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
18437 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
18438 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
18439 
18440 	if (IS_HASWELL(dev_priv)) {
18441 		/*
18442 		 * WaRsPkgCStateDisplayPMReq:hsw
18443 		 * System hang if this isn't done before disabling all planes!
18444 		 */
18445 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
18446 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
18447 	}
18448 }
18449 
18450 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
18451 				       enum port port, i915_reg_t hdmi_reg)
18452 {
18453 	u32 val = intel_de_read(dev_priv, hdmi_reg);
18454 
18455 	if (val & SDVO_ENABLE ||
18456 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
18457 		return;
18458 
18459 	drm_dbg_kms(&dev_priv->drm,
18460 		    "Sanitizing transcoder select for HDMI %c\n",
18461 		    port_name(port));
18462 
18463 	val &= ~SDVO_PIPE_SEL_MASK;
18464 	val |= SDVO_PIPE_SEL(PIPE_A);
18465 
18466 	intel_de_write(dev_priv, hdmi_reg, val);
18467 }
18468 
18469 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
18470 				     enum port port, i915_reg_t dp_reg)
18471 {
18472 	u32 val = intel_de_read(dev_priv, dp_reg);
18473 
18474 	if (val & DP_PORT_EN ||
18475 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
18476 		return;
18477 
18478 	drm_dbg_kms(&dev_priv->drm,
18479 		    "Sanitizing transcoder select for DP %c\n",
18480 		    port_name(port));
18481 
18482 	val &= ~DP_PIPE_SEL_MASK;
18483 	val |= DP_PIPE_SEL(PIPE_A);
18484 
18485 	intel_de_write(dev_priv, dp_reg, val);
18486 }
18487 
18488 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
18489 {
18490 	/*
18491 	 * The BIOS may select transcoder B on some of the PCH
18492 	 * ports even it doesn't enable the port. This would trip
18493 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
18494 	 * Sanitize the transcoder select bits to prevent that. We
18495 	 * assume that the BIOS never actually enabled the port,
18496 	 * because if it did we'd actually have to toggle the port
18497 	 * on and back off to make the transcoder A select stick
18498 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
18499 	 * intel_disable_sdvo()).
18500 	 */
18501 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
18502 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
18503 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
18504 
18505 	/* PCH SDVOB multiplex with HDMIB */
18506 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
18507 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
18508 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
18509 }
18510 
18511 /* Scan out the current hw modeset state,
18512  * and sanitizes it to the current state
18513  */
18514 static void
18515 intel_modeset_setup_hw_state(struct drm_device *dev,
18516 			     struct drm_modeset_acquire_ctx *ctx)
18517 {
18518 	struct drm_i915_private *dev_priv = to_i915(dev);
18519 	struct intel_encoder *encoder;
18520 	struct intel_crtc *crtc;
18521 	intel_wakeref_t wakeref;
18522 
18523 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
18524 
18525 	intel_early_display_was(dev_priv);
18526 	intel_modeset_readout_hw_state(dev);
18527 
18528 	/* HW state is read out, now we need to sanitize this mess. */
18529 
18530 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
18531 	for_each_intel_encoder(dev, encoder) {
18532 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
18533 
18534 		/* We need to sanitize only the MST primary port. */
18535 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
18536 		    intel_phy_is_tc(dev_priv, phy))
18537 			intel_tc_port_sanitize(enc_to_dig_port(encoder));
18538 	}
18539 
18540 	get_encoder_power_domains(dev_priv);
18541 
18542 	if (HAS_PCH_IBX(dev_priv))
18543 		ibx_sanitize_pch_ports(dev_priv);
18544 
18545 	/*
18546 	 * intel_sanitize_plane_mapping() may need to do vblank
18547 	 * waits, so we need vblank interrupts restored beforehand.
18548 	 */
18549 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18550 		struct intel_crtc_state *crtc_state =
18551 			to_intel_crtc_state(crtc->base.state);
18552 
18553 		drm_crtc_vblank_reset(&crtc->base);
18554 
18555 		if (crtc_state->hw.active)
18556 			intel_crtc_vblank_on(crtc_state);
18557 	}
18558 
18559 	intel_sanitize_plane_mapping(dev_priv);
18560 
18561 	for_each_intel_encoder(dev, encoder)
18562 		intel_sanitize_encoder(encoder);
18563 
18564 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18565 		struct intel_crtc_state *crtc_state =
18566 			to_intel_crtc_state(crtc->base.state);
18567 
18568 		intel_sanitize_crtc(crtc, ctx);
18569 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
18570 	}
18571 
18572 	intel_modeset_update_connector_atomic_state(dev);
18573 
18574 	intel_dpll_sanitize_state(dev_priv);
18575 
18576 	if (IS_G4X(dev_priv)) {
18577 		g4x_wm_get_hw_state(dev_priv);
18578 		g4x_wm_sanitize(dev_priv);
18579 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
18580 		vlv_wm_get_hw_state(dev_priv);
18581 		vlv_wm_sanitize(dev_priv);
18582 	} else if (INTEL_GEN(dev_priv) >= 9) {
18583 		skl_wm_get_hw_state(dev_priv);
18584 	} else if (HAS_PCH_SPLIT(dev_priv)) {
18585 		ilk_wm_get_hw_state(dev_priv);
18586 	}
18587 
18588 	for_each_intel_crtc(dev, crtc) {
18589 		struct intel_crtc_state *crtc_state =
18590 			to_intel_crtc_state(crtc->base.state);
18591 		u64 put_domains;
18592 
18593 		put_domains = modeset_get_crtc_power_domains(crtc_state);
18594 		if (drm_WARN_ON(dev, put_domains))
18595 			modeset_put_power_domains(dev_priv, put_domains);
18596 	}
18597 
18598 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
18599 }
18600 
18601 void intel_display_resume(struct drm_device *dev)
18602 {
18603 	struct drm_i915_private *dev_priv = to_i915(dev);
18604 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
18605 	struct drm_modeset_acquire_ctx ctx;
18606 	int ret;
18607 
18608 	dev_priv->modeset_restore_state = NULL;
18609 	if (state)
18610 		state->acquire_ctx = &ctx;
18611 
18612 	drm_modeset_acquire_init(&ctx, 0);
18613 
18614 	while (1) {
18615 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
18616 		if (ret != -EDEADLK)
18617 			break;
18618 
18619 		drm_modeset_backoff(&ctx);
18620 	}
18621 
18622 	if (!ret)
18623 		ret = __intel_display_resume(dev, state, &ctx);
18624 
18625 	intel_enable_ipc(dev_priv);
18626 	drm_modeset_drop_locks(&ctx);
18627 	drm_modeset_acquire_fini(&ctx);
18628 
18629 	if (ret)
18630 		drm_err(&dev_priv->drm,
18631 			"Restoring old state failed with %i\n", ret);
18632 	if (state)
18633 		drm_atomic_state_put(state);
18634 }
18635 
18636 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
18637 {
18638 	struct intel_connector *connector;
18639 	struct drm_connector_list_iter conn_iter;
18640 
18641 	/* Kill all the work that may have been queued by hpd. */
18642 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
18643 	for_each_intel_connector_iter(connector, &conn_iter) {
18644 		if (connector->modeset_retry_work.func)
18645 			cancel_work_sync(&connector->modeset_retry_work);
18646 		if (connector->hdcp.shim) {
18647 			cancel_delayed_work_sync(&connector->hdcp.check_work);
18648 			cancel_work_sync(&connector->hdcp.prop_work);
18649 		}
18650 	}
18651 	drm_connector_list_iter_end(&conn_iter);
18652 }
18653 
18654 /* part #1: call before irq uninstall */
18655 void intel_modeset_driver_remove(struct drm_i915_private *i915)
18656 {
18657 	flush_workqueue(i915->flip_wq);
18658 	flush_workqueue(i915->modeset_wq);
18659 
18660 	flush_work(&i915->atomic_helper.free_work);
18661 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
18662 }
18663 
18664 /* part #2: call after irq uninstall */
18665 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
18666 {
18667 	/*
18668 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
18669 	 * poll handlers. Hence disable polling after hpd handling is shut down.
18670 	 */
18671 	intel_hpd_poll_fini(i915);
18672 
18673 	/*
18674 	 * MST topology needs to be suspended so we don't have any calls to
18675 	 * fbdev after it's finalized. MST will be destroyed later as part of
18676 	 * drm_mode_config_cleanup()
18677 	 */
18678 	intel_dp_mst_suspend(i915);
18679 
18680 	/* poll work can call into fbdev, hence clean that up afterwards */
18681 	intel_fbdev_fini(i915);
18682 
18683 	intel_unregister_dsm_handler();
18684 
18685 	intel_fbc_global_disable(i915);
18686 
18687 	/* flush any delayed tasks or pending work */
18688 	flush_scheduled_work();
18689 
18690 	intel_hdcp_component_fini(i915);
18691 
18692 	intel_mode_config_cleanup(i915);
18693 
18694 	intel_overlay_cleanup(i915);
18695 
18696 	intel_gmbus_teardown(i915);
18697 
18698 	destroy_workqueue(i915->flip_wq);
18699 	destroy_workqueue(i915->modeset_wq);
18700 
18701 	intel_fbc_cleanup_cfb(i915);
18702 }
18703 
18704 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
18705 
18706 struct intel_display_error_state {
18707 
18708 	u32 power_well_driver;
18709 
18710 	struct intel_cursor_error_state {
18711 		u32 control;
18712 		u32 position;
18713 		u32 base;
18714 		u32 size;
18715 	} cursor[I915_MAX_PIPES];
18716 
18717 	struct intel_pipe_error_state {
18718 		bool power_domain_on;
18719 		u32 source;
18720 		u32 stat;
18721 	} pipe[I915_MAX_PIPES];
18722 
18723 	struct intel_plane_error_state {
18724 		u32 control;
18725 		u32 stride;
18726 		u32 size;
18727 		u32 pos;
18728 		u32 addr;
18729 		u32 surface;
18730 		u32 tile_offset;
18731 	} plane[I915_MAX_PIPES];
18732 
18733 	struct intel_transcoder_error_state {
18734 		bool available;
18735 		bool power_domain_on;
18736 		enum transcoder cpu_transcoder;
18737 
18738 		u32 conf;
18739 
18740 		u32 htotal;
18741 		u32 hblank;
18742 		u32 hsync;
18743 		u32 vtotal;
18744 		u32 vblank;
18745 		u32 vsync;
18746 	} transcoder[5];
18747 };
18748 
18749 struct intel_display_error_state *
18750 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
18751 {
18752 	struct intel_display_error_state *error;
18753 	int transcoders[] = {
18754 		TRANSCODER_A,
18755 		TRANSCODER_B,
18756 		TRANSCODER_C,
18757 		TRANSCODER_D,
18758 		TRANSCODER_EDP,
18759 	};
18760 	int i;
18761 
18762 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
18763 
18764 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
18765 		return NULL;
18766 
18767 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
18768 	if (error == NULL)
18769 		return NULL;
18770 
18771 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18772 		error->power_well_driver = intel_de_read(dev_priv,
18773 							 HSW_PWR_WELL_CTL2);
18774 
18775 	for_each_pipe(dev_priv, i) {
18776 		error->pipe[i].power_domain_on =
18777 			__intel_display_power_is_enabled(dev_priv,
18778 							 POWER_DOMAIN_PIPE(i));
18779 		if (!error->pipe[i].power_domain_on)
18780 			continue;
18781 
18782 		error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
18783 		error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
18784 		error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
18785 
18786 		error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
18787 		error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
18788 		if (INTEL_GEN(dev_priv) <= 3) {
18789 			error->plane[i].size = intel_de_read(dev_priv,
18790 							     DSPSIZE(i));
18791 			error->plane[i].pos = intel_de_read(dev_priv,
18792 							    DSPPOS(i));
18793 		}
18794 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18795 			error->plane[i].addr = intel_de_read(dev_priv,
18796 							     DSPADDR(i));
18797 		if (INTEL_GEN(dev_priv) >= 4) {
18798 			error->plane[i].surface = intel_de_read(dev_priv,
18799 								DSPSURF(i));
18800 			error->plane[i].tile_offset = intel_de_read(dev_priv,
18801 								    DSPTILEOFF(i));
18802 		}
18803 
18804 		error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
18805 
18806 		if (HAS_GMCH(dev_priv))
18807 			error->pipe[i].stat = intel_de_read(dev_priv,
18808 							    PIPESTAT(i));
18809 	}
18810 
18811 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
18812 		enum transcoder cpu_transcoder = transcoders[i];
18813 
18814 		if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
18815 			continue;
18816 
18817 		error->transcoder[i].available = true;
18818 		error->transcoder[i].power_domain_on =
18819 			__intel_display_power_is_enabled(dev_priv,
18820 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
18821 		if (!error->transcoder[i].power_domain_on)
18822 			continue;
18823 
18824 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
18825 
18826 		error->transcoder[i].conf = intel_de_read(dev_priv,
18827 							  PIPECONF(cpu_transcoder));
18828 		error->transcoder[i].htotal = intel_de_read(dev_priv,
18829 							    HTOTAL(cpu_transcoder));
18830 		error->transcoder[i].hblank = intel_de_read(dev_priv,
18831 							    HBLANK(cpu_transcoder));
18832 		error->transcoder[i].hsync = intel_de_read(dev_priv,
18833 							   HSYNC(cpu_transcoder));
18834 		error->transcoder[i].vtotal = intel_de_read(dev_priv,
18835 							    VTOTAL(cpu_transcoder));
18836 		error->transcoder[i].vblank = intel_de_read(dev_priv,
18837 							    VBLANK(cpu_transcoder));
18838 		error->transcoder[i].vsync = intel_de_read(dev_priv,
18839 							   VSYNC(cpu_transcoder));
18840 	}
18841 
18842 	return error;
18843 }
18844 
18845 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
18846 
18847 void
18848 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
18849 				struct intel_display_error_state *error)
18850 {
18851 	struct drm_i915_private *dev_priv = m->i915;
18852 	int i;
18853 
18854 	if (!error)
18855 		return;
18856 
18857 	err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
18858 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18859 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
18860 			   error->power_well_driver);
18861 	for_each_pipe(dev_priv, i) {
18862 		err_printf(m, "Pipe [%d]:\n", i);
18863 		err_printf(m, "  Power: %s\n",
18864 			   onoff(error->pipe[i].power_domain_on));
18865 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
18866 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
18867 
18868 		err_printf(m, "Plane [%d]:\n", i);
18869 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
18870 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
18871 		if (INTEL_GEN(dev_priv) <= 3) {
18872 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
18873 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
18874 		}
18875 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18876 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
18877 		if (INTEL_GEN(dev_priv) >= 4) {
18878 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
18879 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
18880 		}
18881 
18882 		err_printf(m, "Cursor [%d]:\n", i);
18883 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
18884 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
18885 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
18886 	}
18887 
18888 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
18889 		if (!error->transcoder[i].available)
18890 			continue;
18891 
18892 		err_printf(m, "CPU transcoder: %s\n",
18893 			   transcoder_name(error->transcoder[i].cpu_transcoder));
18894 		err_printf(m, "  Power: %s\n",
18895 			   onoff(error->transcoder[i].power_domain_on));
18896 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
18897 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
18898 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
18899 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
18900 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
18901 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
18902 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
18903 	}
18904 }
18905 
18906 #endif
18907