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_damage_helper.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_plane_helper.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/drm_rect.h>
45 
46 #include "display/intel_crt.h"
47 #include "display/intel_ddi.h"
48 #include "display/intel_dp.h"
49 #include "display/intel_dp_mst.h"
50 #include "display/intel_dpll_mgr.h"
51 #include "display/intel_dsi.h"
52 #include "display/intel_dvo.h"
53 #include "display/intel_gmbus.h"
54 #include "display/intel_hdmi.h"
55 #include "display/intel_lvds.h"
56 #include "display/intel_sdvo.h"
57 #include "display/intel_tv.h"
58 #include "display/intel_vdsc.h"
59 
60 #include "gt/intel_rps.h"
61 
62 #include "i915_drv.h"
63 #include "i915_trace.h"
64 #include "intel_acpi.h"
65 #include "intel_atomic.h"
66 #include "intel_atomic_plane.h"
67 #include "intel_bw.h"
68 #include "intel_cdclk.h"
69 #include "intel_color.h"
70 #include "intel_csr.h"
71 #include "intel_display_types.h"
72 #include "intel_dp_link_training.h"
73 #include "intel_fbc.h"
74 #include "intel_fbdev.h"
75 #include "intel_fifo_underrun.h"
76 #include "intel_frontbuffer.h"
77 #include "intel_hdcp.h"
78 #include "intel_hotplug.h"
79 #include "intel_overlay.h"
80 #include "intel_pipe_crc.h"
81 #include "intel_pm.h"
82 #include "intel_psr.h"
83 #include "intel_quirks.h"
84 #include "intel_sideband.h"
85 #include "intel_sprite.h"
86 #include "intel_tc.h"
87 #include "intel_vga.h"
88 
89 /* Primary plane formats for gen <= 3 */
90 static const u32 i8xx_primary_formats[] = {
91 	DRM_FORMAT_C8,
92 	DRM_FORMAT_XRGB1555,
93 	DRM_FORMAT_RGB565,
94 	DRM_FORMAT_XRGB8888,
95 };
96 
97 /* Primary plane formats for ivb (no fp16 due to hw issue) */
98 static const u32 ivb_primary_formats[] = {
99 	DRM_FORMAT_C8,
100 	DRM_FORMAT_RGB565,
101 	DRM_FORMAT_XRGB8888,
102 	DRM_FORMAT_XBGR8888,
103 	DRM_FORMAT_XRGB2101010,
104 	DRM_FORMAT_XBGR2101010,
105 };
106 
107 /* Primary plane formats for gen >= 4, except ivb */
108 static const u32 i965_primary_formats[] = {
109 	DRM_FORMAT_C8,
110 	DRM_FORMAT_RGB565,
111 	DRM_FORMAT_XRGB8888,
112 	DRM_FORMAT_XBGR8888,
113 	DRM_FORMAT_XRGB2101010,
114 	DRM_FORMAT_XBGR2101010,
115 	DRM_FORMAT_XBGR16161616F,
116 };
117 
118 /* Primary plane formats for vlv/chv */
119 static const u32 vlv_primary_formats[] = {
120 	DRM_FORMAT_C8,
121 	DRM_FORMAT_RGB565,
122 	DRM_FORMAT_XRGB8888,
123 	DRM_FORMAT_XBGR8888,
124 	DRM_FORMAT_ARGB8888,
125 	DRM_FORMAT_ABGR8888,
126 	DRM_FORMAT_XRGB2101010,
127 	DRM_FORMAT_XBGR2101010,
128 	DRM_FORMAT_ARGB2101010,
129 	DRM_FORMAT_ABGR2101010,
130 	DRM_FORMAT_XBGR16161616F,
131 };
132 
133 static const u64 i9xx_format_modifiers[] = {
134 	I915_FORMAT_MOD_X_TILED,
135 	DRM_FORMAT_MOD_LINEAR,
136 	DRM_FORMAT_MOD_INVALID
137 };
138 
139 /* Cursor formats */
140 static const u32 intel_cursor_formats[] = {
141 	DRM_FORMAT_ARGB8888,
142 };
143 
144 static const u64 cursor_format_modifiers[] = {
145 	DRM_FORMAT_MOD_LINEAR,
146 	DRM_FORMAT_MOD_INVALID
147 };
148 
149 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
150 				struct intel_crtc_state *pipe_config);
151 static void ilk_pch_clock_get(struct intel_crtc *crtc,
152 			      struct intel_crtc_state *pipe_config);
153 
154 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
155 				  struct drm_i915_gem_object *obj,
156 				  struct drm_mode_fb_cmd2 *mode_cmd);
157 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
158 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
159 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
160 					 const struct intel_link_m_n *m_n,
161 					 const struct intel_link_m_n *m2_n2);
162 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
163 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
164 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
165 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
166 static void vlv_prepare_pll(struct intel_crtc *crtc,
167 			    const struct intel_crtc_state *pipe_config);
168 static void chv_prepare_pll(struct intel_crtc *crtc,
169 			    const struct intel_crtc_state *pipe_config);
170 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
171 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
172 static void intel_modeset_setup_hw_state(struct drm_device *dev,
173 					 struct drm_modeset_acquire_ctx *ctx);
174 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
175 
176 struct intel_limit {
177 	struct {
178 		int min, max;
179 	} dot, vco, n, m, m1, m2, p, p1;
180 
181 	struct {
182 		int dot_limit;
183 		int p2_slow, p2_fast;
184 	} p2;
185 };
186 
187 /* returns HPLL frequency in kHz */
188 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
189 {
190 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
191 
192 	/* Obtain SKU information */
193 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
194 		CCK_FUSE_HPLL_FREQ_MASK;
195 
196 	return vco_freq[hpll_freq] * 1000;
197 }
198 
199 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
200 		      const char *name, u32 reg, int ref_freq)
201 {
202 	u32 val;
203 	int divider;
204 
205 	val = vlv_cck_read(dev_priv, reg);
206 	divider = val & CCK_FREQUENCY_VALUES;
207 
208 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
209 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
210 		 "%s change in progress\n", name);
211 
212 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
213 }
214 
215 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
216 			   const char *name, u32 reg)
217 {
218 	int hpll;
219 
220 	vlv_cck_get(dev_priv);
221 
222 	if (dev_priv->hpll_freq == 0)
223 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
224 
225 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
226 
227 	vlv_cck_put(dev_priv);
228 
229 	return hpll;
230 }
231 
232 static void intel_update_czclk(struct drm_i915_private *dev_priv)
233 {
234 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
235 		return;
236 
237 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
238 						      CCK_CZ_CLOCK_CONTROL);
239 
240 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
241 		dev_priv->czclk_freq);
242 }
243 
244 /* units of 100MHz */
245 static u32 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
246 			       const struct intel_crtc_state *pipe_config)
247 {
248 	if (HAS_DDI(dev_priv))
249 		return pipe_config->port_clock; /* SPLL */
250 	else
251 		return dev_priv->fdi_pll_freq;
252 }
253 
254 static const struct intel_limit intel_limits_i8xx_dac = {
255 	.dot = { .min = 25000, .max = 350000 },
256 	.vco = { .min = 908000, .max = 1512000 },
257 	.n = { .min = 2, .max = 16 },
258 	.m = { .min = 96, .max = 140 },
259 	.m1 = { .min = 18, .max = 26 },
260 	.m2 = { .min = 6, .max = 16 },
261 	.p = { .min = 4, .max = 128 },
262 	.p1 = { .min = 2, .max = 33 },
263 	.p2 = { .dot_limit = 165000,
264 		.p2_slow = 4, .p2_fast = 2 },
265 };
266 
267 static const struct intel_limit intel_limits_i8xx_dvo = {
268 	.dot = { .min = 25000, .max = 350000 },
269 	.vco = { .min = 908000, .max = 1512000 },
270 	.n = { .min = 2, .max = 16 },
271 	.m = { .min = 96, .max = 140 },
272 	.m1 = { .min = 18, .max = 26 },
273 	.m2 = { .min = 6, .max = 16 },
274 	.p = { .min = 4, .max = 128 },
275 	.p1 = { .min = 2, .max = 33 },
276 	.p2 = { .dot_limit = 165000,
277 		.p2_slow = 4, .p2_fast = 4 },
278 };
279 
280 static const struct intel_limit intel_limits_i8xx_lvds = {
281 	.dot = { .min = 25000, .max = 350000 },
282 	.vco = { .min = 908000, .max = 1512000 },
283 	.n = { .min = 2, .max = 16 },
284 	.m = { .min = 96, .max = 140 },
285 	.m1 = { .min = 18, .max = 26 },
286 	.m2 = { .min = 6, .max = 16 },
287 	.p = { .min = 4, .max = 128 },
288 	.p1 = { .min = 1, .max = 6 },
289 	.p2 = { .dot_limit = 165000,
290 		.p2_slow = 14, .p2_fast = 7 },
291 };
292 
293 static const struct intel_limit intel_limits_i9xx_sdvo = {
294 	.dot = { .min = 20000, .max = 400000 },
295 	.vco = { .min = 1400000, .max = 2800000 },
296 	.n = { .min = 1, .max = 6 },
297 	.m = { .min = 70, .max = 120 },
298 	.m1 = { .min = 8, .max = 18 },
299 	.m2 = { .min = 3, .max = 7 },
300 	.p = { .min = 5, .max = 80 },
301 	.p1 = { .min = 1, .max = 8 },
302 	.p2 = { .dot_limit = 200000,
303 		.p2_slow = 10, .p2_fast = 5 },
304 };
305 
306 static const struct intel_limit intel_limits_i9xx_lvds = {
307 	.dot = { .min = 20000, .max = 400000 },
308 	.vco = { .min = 1400000, .max = 2800000 },
309 	.n = { .min = 1, .max = 6 },
310 	.m = { .min = 70, .max = 120 },
311 	.m1 = { .min = 8, .max = 18 },
312 	.m2 = { .min = 3, .max = 7 },
313 	.p = { .min = 7, .max = 98 },
314 	.p1 = { .min = 1, .max = 8 },
315 	.p2 = { .dot_limit = 112000,
316 		.p2_slow = 14, .p2_fast = 7 },
317 };
318 
319 
320 static const struct intel_limit intel_limits_g4x_sdvo = {
321 	.dot = { .min = 25000, .max = 270000 },
322 	.vco = { .min = 1750000, .max = 3500000},
323 	.n = { .min = 1, .max = 4 },
324 	.m = { .min = 104, .max = 138 },
325 	.m1 = { .min = 17, .max = 23 },
326 	.m2 = { .min = 5, .max = 11 },
327 	.p = { .min = 10, .max = 30 },
328 	.p1 = { .min = 1, .max = 3},
329 	.p2 = { .dot_limit = 270000,
330 		.p2_slow = 10,
331 		.p2_fast = 10
332 	},
333 };
334 
335 static const struct intel_limit intel_limits_g4x_hdmi = {
336 	.dot = { .min = 22000, .max = 400000 },
337 	.vco = { .min = 1750000, .max = 3500000},
338 	.n = { .min = 1, .max = 4 },
339 	.m = { .min = 104, .max = 138 },
340 	.m1 = { .min = 16, .max = 23 },
341 	.m2 = { .min = 5, .max = 11 },
342 	.p = { .min = 5, .max = 80 },
343 	.p1 = { .min = 1, .max = 8},
344 	.p2 = { .dot_limit = 165000,
345 		.p2_slow = 10, .p2_fast = 5 },
346 };
347 
348 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
349 	.dot = { .min = 20000, .max = 115000 },
350 	.vco = { .min = 1750000, .max = 3500000 },
351 	.n = { .min = 1, .max = 3 },
352 	.m = { .min = 104, .max = 138 },
353 	.m1 = { .min = 17, .max = 23 },
354 	.m2 = { .min = 5, .max = 11 },
355 	.p = { .min = 28, .max = 112 },
356 	.p1 = { .min = 2, .max = 8 },
357 	.p2 = { .dot_limit = 0,
358 		.p2_slow = 14, .p2_fast = 14
359 	},
360 };
361 
362 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
363 	.dot = { .min = 80000, .max = 224000 },
364 	.vco = { .min = 1750000, .max = 3500000 },
365 	.n = { .min = 1, .max = 3 },
366 	.m = { .min = 104, .max = 138 },
367 	.m1 = { .min = 17, .max = 23 },
368 	.m2 = { .min = 5, .max = 11 },
369 	.p = { .min = 14, .max = 42 },
370 	.p1 = { .min = 2, .max = 6 },
371 	.p2 = { .dot_limit = 0,
372 		.p2_slow = 7, .p2_fast = 7
373 	},
374 };
375 
376 static const struct intel_limit pnv_limits_sdvo = {
377 	.dot = { .min = 20000, .max = 400000},
378 	.vco = { .min = 1700000, .max = 3500000 },
379 	/* Pineview's Ncounter is a ring counter */
380 	.n = { .min = 3, .max = 6 },
381 	.m = { .min = 2, .max = 256 },
382 	/* Pineview only has one combined m divider, which we treat as m2. */
383 	.m1 = { .min = 0, .max = 0 },
384 	.m2 = { .min = 0, .max = 254 },
385 	.p = { .min = 5, .max = 80 },
386 	.p1 = { .min = 1, .max = 8 },
387 	.p2 = { .dot_limit = 200000,
388 		.p2_slow = 10, .p2_fast = 5 },
389 };
390 
391 static const struct intel_limit pnv_limits_lvds = {
392 	.dot = { .min = 20000, .max = 400000 },
393 	.vco = { .min = 1700000, .max = 3500000 },
394 	.n = { .min = 3, .max = 6 },
395 	.m = { .min = 2, .max = 256 },
396 	.m1 = { .min = 0, .max = 0 },
397 	.m2 = { .min = 0, .max = 254 },
398 	.p = { .min = 7, .max = 112 },
399 	.p1 = { .min = 1, .max = 8 },
400 	.p2 = { .dot_limit = 112000,
401 		.p2_slow = 14, .p2_fast = 14 },
402 };
403 
404 /* Ironlake / Sandybridge
405  *
406  * We calculate clock using (register_value + 2) for N/M1/M2, so here
407  * the range value for them is (actual_value - 2).
408  */
409 static const struct intel_limit ilk_limits_dac = {
410 	.dot = { .min = 25000, .max = 350000 },
411 	.vco = { .min = 1760000, .max = 3510000 },
412 	.n = { .min = 1, .max = 5 },
413 	.m = { .min = 79, .max = 127 },
414 	.m1 = { .min = 12, .max = 22 },
415 	.m2 = { .min = 5, .max = 9 },
416 	.p = { .min = 5, .max = 80 },
417 	.p1 = { .min = 1, .max = 8 },
418 	.p2 = { .dot_limit = 225000,
419 		.p2_slow = 10, .p2_fast = 5 },
420 };
421 
422 static const struct intel_limit ilk_limits_single_lvds = {
423 	.dot = { .min = 25000, .max = 350000 },
424 	.vco = { .min = 1760000, .max = 3510000 },
425 	.n = { .min = 1, .max = 3 },
426 	.m = { .min = 79, .max = 118 },
427 	.m1 = { .min = 12, .max = 22 },
428 	.m2 = { .min = 5, .max = 9 },
429 	.p = { .min = 28, .max = 112 },
430 	.p1 = { .min = 2, .max = 8 },
431 	.p2 = { .dot_limit = 225000,
432 		.p2_slow = 14, .p2_fast = 14 },
433 };
434 
435 static const struct intel_limit ilk_limits_dual_lvds = {
436 	.dot = { .min = 25000, .max = 350000 },
437 	.vco = { .min = 1760000, .max = 3510000 },
438 	.n = { .min = 1, .max = 3 },
439 	.m = { .min = 79, .max = 127 },
440 	.m1 = { .min = 12, .max = 22 },
441 	.m2 = { .min = 5, .max = 9 },
442 	.p = { .min = 14, .max = 56 },
443 	.p1 = { .min = 2, .max = 8 },
444 	.p2 = { .dot_limit = 225000,
445 		.p2_slow = 7, .p2_fast = 7 },
446 };
447 
448 /* LVDS 100mhz refclk limits. */
449 static const struct intel_limit ilk_limits_single_lvds_100m = {
450 	.dot = { .min = 25000, .max = 350000 },
451 	.vco = { .min = 1760000, .max = 3510000 },
452 	.n = { .min = 1, .max = 2 },
453 	.m = { .min = 79, .max = 126 },
454 	.m1 = { .min = 12, .max = 22 },
455 	.m2 = { .min = 5, .max = 9 },
456 	.p = { .min = 28, .max = 112 },
457 	.p1 = { .min = 2, .max = 8 },
458 	.p2 = { .dot_limit = 225000,
459 		.p2_slow = 14, .p2_fast = 14 },
460 };
461 
462 static const struct intel_limit ilk_limits_dual_lvds_100m = {
463 	.dot = { .min = 25000, .max = 350000 },
464 	.vco = { .min = 1760000, .max = 3510000 },
465 	.n = { .min = 1, .max = 3 },
466 	.m = { .min = 79, .max = 126 },
467 	.m1 = { .min = 12, .max = 22 },
468 	.m2 = { .min = 5, .max = 9 },
469 	.p = { .min = 14, .max = 42 },
470 	.p1 = { .min = 2, .max = 6 },
471 	.p2 = { .dot_limit = 225000,
472 		.p2_slow = 7, .p2_fast = 7 },
473 };
474 
475 static const struct intel_limit intel_limits_vlv = {
476 	 /*
477 	  * These are the data rate limits (measured in fast clocks)
478 	  * since those are the strictest limits we have. The fast
479 	  * clock and actual rate limits are more relaxed, so checking
480 	  * them would make no difference.
481 	  */
482 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
483 	.vco = { .min = 4000000, .max = 6000000 },
484 	.n = { .min = 1, .max = 7 },
485 	.m1 = { .min = 2, .max = 3 },
486 	.m2 = { .min = 11, .max = 156 },
487 	.p1 = { .min = 2, .max = 3 },
488 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
489 };
490 
491 static const struct intel_limit intel_limits_chv = {
492 	/*
493 	 * These are the data rate limits (measured in fast clocks)
494 	 * since those are the strictest limits we have.  The fast
495 	 * clock and actual rate limits are more relaxed, so checking
496 	 * them would make no difference.
497 	 */
498 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
499 	.vco = { .min = 4800000, .max = 6480000 },
500 	.n = { .min = 1, .max = 1 },
501 	.m1 = { .min = 2, .max = 2 },
502 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
503 	.p1 = { .min = 2, .max = 4 },
504 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
505 };
506 
507 static const struct intel_limit intel_limits_bxt = {
508 	/* FIXME: find real dot limits */
509 	.dot = { .min = 0, .max = INT_MAX },
510 	.vco = { .min = 4800000, .max = 6700000 },
511 	.n = { .min = 1, .max = 1 },
512 	.m1 = { .min = 2, .max = 2 },
513 	/* FIXME: find real m2 limits */
514 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
515 	.p1 = { .min = 2, .max = 4 },
516 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
517 };
518 
519 /* WA Display #0827: Gen9:all */
520 static void
521 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
522 {
523 	if (enable)
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 	else
527 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
528 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
529 }
530 
531 /* Wa_2006604312:icl,ehl */
532 static void
533 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
534 		       bool enable)
535 {
536 	if (enable)
537 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
538 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
539 	else
540 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
541 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
542 }
543 
544 static bool
545 needs_modeset(const struct intel_crtc_state *state)
546 {
547 	return drm_atomic_crtc_needs_modeset(&state->uapi);
548 }
549 
550 static bool
551 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
552 {
553 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
554 }
555 
556 static bool
557 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
558 {
559 	return crtc_state->sync_mode_slaves_mask != 0;
560 }
561 
562 bool
563 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
564 {
565 	return is_trans_port_sync_master(crtc_state) ||
566 		is_trans_port_sync_slave(crtc_state);
567 }
568 
569 /*
570  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
571  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
572  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
573  * The helpers' return value is the rate of the clock that is fed to the
574  * display engine's pipe which can be the above fast dot clock rate or a
575  * divided-down version of it.
576  */
577 /* m1 is reserved as 0 in Pineview, n is a ring counter */
578 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
579 {
580 	clock->m = clock->m2 + 2;
581 	clock->p = clock->p1 * clock->p2;
582 	if (WARN_ON(clock->n == 0 || clock->p == 0))
583 		return 0;
584 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
585 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
586 
587 	return clock->dot;
588 }
589 
590 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
591 {
592 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
593 }
594 
595 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
596 {
597 	clock->m = i9xx_dpll_compute_m(clock);
598 	clock->p = clock->p1 * clock->p2;
599 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
600 		return 0;
601 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
602 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
603 
604 	return clock->dot;
605 }
606 
607 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
608 {
609 	clock->m = clock->m1 * clock->m2;
610 	clock->p = clock->p1 * clock->p2;
611 	if (WARN_ON(clock->n == 0 || clock->p == 0))
612 		return 0;
613 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
614 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
615 
616 	return clock->dot / 5;
617 }
618 
619 int chv_calc_dpll_params(int refclk, struct dpll *clock)
620 {
621 	clock->m = clock->m1 * clock->m2;
622 	clock->p = clock->p1 * clock->p2;
623 	if (WARN_ON(clock->n == 0 || clock->p == 0))
624 		return 0;
625 	clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
626 					   clock->n << 22);
627 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
628 
629 	return clock->dot / 5;
630 }
631 
632 /*
633  * Returns whether the given set of divisors are valid for a given refclk with
634  * the given connectors.
635  */
636 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
637 			       const struct intel_limit *limit,
638 			       const struct dpll *clock)
639 {
640 	if (clock->n < limit->n.min || limit->n.max < clock->n)
641 		return false;
642 	if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
643 		return false;
644 	if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
645 		return false;
646 	if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
647 		return false;
648 
649 	if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
650 	    !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
651 		if (clock->m1 <= clock->m2)
652 			return false;
653 
654 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
655 	    !IS_GEN9_LP(dev_priv)) {
656 		if (clock->p < limit->p.min || limit->p.max < clock->p)
657 			return false;
658 		if (clock->m < limit->m.min || limit->m.max < clock->m)
659 			return false;
660 	}
661 
662 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
663 		return false;
664 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
665 	 * connector, etc., rather than just a single range.
666 	 */
667 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
668 		return false;
669 
670 	return true;
671 }
672 
673 static int
674 i9xx_select_p2_div(const struct intel_limit *limit,
675 		   const struct intel_crtc_state *crtc_state,
676 		   int target)
677 {
678 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
679 
680 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
681 		/*
682 		 * For LVDS just rely on its current settings for dual-channel.
683 		 * We haven't figured out how to reliably set up different
684 		 * single/dual channel state, if we even can.
685 		 */
686 		if (intel_is_dual_link_lvds(dev_priv))
687 			return limit->p2.p2_fast;
688 		else
689 			return limit->p2.p2_slow;
690 	} else {
691 		if (target < limit->p2.dot_limit)
692 			return limit->p2.p2_slow;
693 		else
694 			return limit->p2.p2_fast;
695 	}
696 }
697 
698 /*
699  * Returns a set of divisors for the desired target clock with the given
700  * refclk, or FALSE.  The returned values represent the clock equation:
701  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
702  *
703  * Target and reference clocks are specified in kHz.
704  *
705  * If match_clock is provided, then best_clock P divider must match the P
706  * divider from @match_clock used for LVDS downclocking.
707  */
708 static bool
709 i9xx_find_best_dpll(const struct intel_limit *limit,
710 		    struct intel_crtc_state *crtc_state,
711 		    int target, int refclk, struct dpll *match_clock,
712 		    struct dpll *best_clock)
713 {
714 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
715 	struct dpll clock;
716 	int err = target;
717 
718 	memset(best_clock, 0, sizeof(*best_clock));
719 
720 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
721 
722 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
723 	     clock.m1++) {
724 		for (clock.m2 = limit->m2.min;
725 		     clock.m2 <= limit->m2.max; clock.m2++) {
726 			if (clock.m2 >= clock.m1)
727 				break;
728 			for (clock.n = limit->n.min;
729 			     clock.n <= limit->n.max; clock.n++) {
730 				for (clock.p1 = limit->p1.min;
731 					clock.p1 <= limit->p1.max; clock.p1++) {
732 					int this_err;
733 
734 					i9xx_calc_dpll_params(refclk, &clock);
735 					if (!intel_pll_is_valid(to_i915(dev),
736 								limit,
737 								&clock))
738 						continue;
739 					if (match_clock &&
740 					    clock.p != match_clock->p)
741 						continue;
742 
743 					this_err = abs(clock.dot - target);
744 					if (this_err < err) {
745 						*best_clock = clock;
746 						err = this_err;
747 					}
748 				}
749 			}
750 		}
751 	}
752 
753 	return (err != target);
754 }
755 
756 /*
757  * Returns a set of divisors for the desired target clock with the given
758  * refclk, or FALSE.  The returned values represent the clock equation:
759  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
760  *
761  * Target and reference clocks are specified in kHz.
762  *
763  * If match_clock is provided, then best_clock P divider must match the P
764  * divider from @match_clock used for LVDS downclocking.
765  */
766 static bool
767 pnv_find_best_dpll(const struct intel_limit *limit,
768 		   struct intel_crtc_state *crtc_state,
769 		   int target, int refclk, struct dpll *match_clock,
770 		   struct dpll *best_clock)
771 {
772 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
773 	struct dpll clock;
774 	int err = target;
775 
776 	memset(best_clock, 0, sizeof(*best_clock));
777 
778 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
779 
780 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
781 	     clock.m1++) {
782 		for (clock.m2 = limit->m2.min;
783 		     clock.m2 <= limit->m2.max; clock.m2++) {
784 			for (clock.n = limit->n.min;
785 			     clock.n <= limit->n.max; clock.n++) {
786 				for (clock.p1 = limit->p1.min;
787 					clock.p1 <= limit->p1.max; clock.p1++) {
788 					int this_err;
789 
790 					pnv_calc_dpll_params(refclk, &clock);
791 					if (!intel_pll_is_valid(to_i915(dev),
792 								limit,
793 								&clock))
794 						continue;
795 					if (match_clock &&
796 					    clock.p != match_clock->p)
797 						continue;
798 
799 					this_err = abs(clock.dot - target);
800 					if (this_err < err) {
801 						*best_clock = clock;
802 						err = this_err;
803 					}
804 				}
805 			}
806 		}
807 	}
808 
809 	return (err != target);
810 }
811 
812 /*
813  * Returns a set of divisors for the desired target clock with the given
814  * refclk, or FALSE.  The returned values represent the clock equation:
815  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
816  *
817  * Target and reference clocks are specified in kHz.
818  *
819  * If match_clock is provided, then best_clock P divider must match the P
820  * divider from @match_clock used for LVDS downclocking.
821  */
822 static bool
823 g4x_find_best_dpll(const struct intel_limit *limit,
824 		   struct intel_crtc_state *crtc_state,
825 		   int target, int refclk, struct dpll *match_clock,
826 		   struct dpll *best_clock)
827 {
828 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
829 	struct dpll clock;
830 	int max_n;
831 	bool found = false;
832 	/* approximately equals target * 0.00585 */
833 	int err_most = (target >> 8) + (target >> 9);
834 
835 	memset(best_clock, 0, sizeof(*best_clock));
836 
837 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
838 
839 	max_n = limit->n.max;
840 	/* based on hardware requirement, prefer smaller n to precision */
841 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
842 		/* based on hardware requirement, prefere larger m1,m2 */
843 		for (clock.m1 = limit->m1.max;
844 		     clock.m1 >= limit->m1.min; clock.m1--) {
845 			for (clock.m2 = limit->m2.max;
846 			     clock.m2 >= limit->m2.min; clock.m2--) {
847 				for (clock.p1 = limit->p1.max;
848 				     clock.p1 >= limit->p1.min; clock.p1--) {
849 					int this_err;
850 
851 					i9xx_calc_dpll_params(refclk, &clock);
852 					if (!intel_pll_is_valid(to_i915(dev),
853 								limit,
854 								&clock))
855 						continue;
856 
857 					this_err = abs(clock.dot - target);
858 					if (this_err < err_most) {
859 						*best_clock = clock;
860 						err_most = this_err;
861 						max_n = clock.n;
862 						found = true;
863 					}
864 				}
865 			}
866 		}
867 	}
868 	return found;
869 }
870 
871 /*
872  * Check if the calculated PLL configuration is more optimal compared to the
873  * best configuration and error found so far. Return the calculated error.
874  */
875 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
876 			       const struct dpll *calculated_clock,
877 			       const struct dpll *best_clock,
878 			       unsigned int best_error_ppm,
879 			       unsigned int *error_ppm)
880 {
881 	/*
882 	 * For CHV ignore the error and consider only the P value.
883 	 * Prefer a bigger P value based on HW requirements.
884 	 */
885 	if (IS_CHERRYVIEW(to_i915(dev))) {
886 		*error_ppm = 0;
887 
888 		return calculated_clock->p > best_clock->p;
889 	}
890 
891 	if (drm_WARN_ON_ONCE(dev, !target_freq))
892 		return false;
893 
894 	*error_ppm = div_u64(1000000ULL *
895 				abs(target_freq - calculated_clock->dot),
896 			     target_freq);
897 	/*
898 	 * Prefer a better P value over a better (smaller) error if the error
899 	 * is small. Ensure this preference for future configurations too by
900 	 * setting the error to 0.
901 	 */
902 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
903 		*error_ppm = 0;
904 
905 		return true;
906 	}
907 
908 	return *error_ppm + 10 < best_error_ppm;
909 }
910 
911 /*
912  * Returns a set of divisors for the desired target clock with the given
913  * refclk, or FALSE.  The returned values represent the clock equation:
914  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
915  */
916 static bool
917 vlv_find_best_dpll(const struct intel_limit *limit,
918 		   struct intel_crtc_state *crtc_state,
919 		   int target, int refclk, struct dpll *match_clock,
920 		   struct dpll *best_clock)
921 {
922 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
923 	struct drm_device *dev = crtc->base.dev;
924 	struct dpll clock;
925 	unsigned int bestppm = 1000000;
926 	/* min update 19.2 MHz */
927 	int max_n = min(limit->n.max, refclk / 19200);
928 	bool found = false;
929 
930 	target *= 5; /* fast clock */
931 
932 	memset(best_clock, 0, sizeof(*best_clock));
933 
934 	/* based on hardware requirement, prefer smaller n to precision */
935 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
936 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
937 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
938 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
939 				clock.p = clock.p1 * clock.p2;
940 				/* based on hardware requirement, prefer bigger m1,m2 values */
941 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
942 					unsigned int ppm;
943 
944 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
945 								     refclk * clock.m1);
946 
947 					vlv_calc_dpll_params(refclk, &clock);
948 
949 					if (!intel_pll_is_valid(to_i915(dev),
950 								limit,
951 								&clock))
952 						continue;
953 
954 					if (!vlv_PLL_is_optimal(dev, target,
955 								&clock,
956 								best_clock,
957 								bestppm, &ppm))
958 						continue;
959 
960 					*best_clock = clock;
961 					bestppm = ppm;
962 					found = true;
963 				}
964 			}
965 		}
966 	}
967 
968 	return found;
969 }
970 
971 /*
972  * Returns a set of divisors for the desired target clock with the given
973  * refclk, or FALSE.  The returned values represent the clock equation:
974  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
975  */
976 static bool
977 chv_find_best_dpll(const struct intel_limit *limit,
978 		   struct intel_crtc_state *crtc_state,
979 		   int target, int refclk, struct dpll *match_clock,
980 		   struct dpll *best_clock)
981 {
982 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
983 	struct drm_device *dev = crtc->base.dev;
984 	unsigned int best_error_ppm;
985 	struct dpll clock;
986 	u64 m2;
987 	int found = false;
988 
989 	memset(best_clock, 0, sizeof(*best_clock));
990 	best_error_ppm = 1000000;
991 
992 	/*
993 	 * Based on hardware doc, the n always set to 1, and m1 always
994 	 * set to 2.  If requires to support 200Mhz refclk, we need to
995 	 * revisit this because n may not 1 anymore.
996 	 */
997 	clock.n = 1, clock.m1 = 2;
998 	target *= 5;	/* fast clock */
999 
1000 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1001 		for (clock.p2 = limit->p2.p2_fast;
1002 				clock.p2 >= limit->p2.p2_slow;
1003 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
1004 			unsigned int error_ppm;
1005 
1006 			clock.p = clock.p1 * clock.p2;
1007 
1008 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1009 						   refclk * clock.m1);
1010 
1011 			if (m2 > INT_MAX/clock.m1)
1012 				continue;
1013 
1014 			clock.m2 = m2;
1015 
1016 			chv_calc_dpll_params(refclk, &clock);
1017 
1018 			if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
1019 				continue;
1020 
1021 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1022 						best_error_ppm, &error_ppm))
1023 				continue;
1024 
1025 			*best_clock = clock;
1026 			best_error_ppm = error_ppm;
1027 			found = true;
1028 		}
1029 	}
1030 
1031 	return found;
1032 }
1033 
1034 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
1035 			struct dpll *best_clock)
1036 {
1037 	int refclk = 100000;
1038 	const struct intel_limit *limit = &intel_limits_bxt;
1039 
1040 	return chv_find_best_dpll(limit, crtc_state,
1041 				  crtc_state->port_clock, refclk,
1042 				  NULL, best_clock);
1043 }
1044 
1045 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1046 				    enum pipe pipe)
1047 {
1048 	i915_reg_t reg = PIPEDSL(pipe);
1049 	u32 line1, line2;
1050 	u32 line_mask;
1051 
1052 	if (IS_GEN(dev_priv, 2))
1053 		line_mask = DSL_LINEMASK_GEN2;
1054 	else
1055 		line_mask = DSL_LINEMASK_GEN3;
1056 
1057 	line1 = intel_de_read(dev_priv, reg) & line_mask;
1058 	msleep(5);
1059 	line2 = intel_de_read(dev_priv, reg) & line_mask;
1060 
1061 	return line1 != line2;
1062 }
1063 
1064 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1065 {
1066 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1067 	enum pipe pipe = crtc->pipe;
1068 
1069 	/* Wait for the display line to settle/start moving */
1070 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1071 		drm_err(&dev_priv->drm,
1072 			"pipe %c scanline %s wait timed out\n",
1073 			pipe_name(pipe), onoff(state));
1074 }
1075 
1076 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1077 {
1078 	wait_for_pipe_scanline_moving(crtc, false);
1079 }
1080 
1081 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1082 {
1083 	wait_for_pipe_scanline_moving(crtc, true);
1084 }
1085 
1086 static void
1087 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1088 {
1089 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1090 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1091 
1092 	if (INTEL_GEN(dev_priv) >= 4) {
1093 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1094 		i915_reg_t reg = PIPECONF(cpu_transcoder);
1095 
1096 		/* Wait for the Pipe State to go off */
1097 		if (intel_de_wait_for_clear(dev_priv, reg,
1098 					    I965_PIPECONF_ACTIVE, 100))
1099 			drm_WARN(&dev_priv->drm, 1,
1100 				 "pipe_off wait timed out\n");
1101 	} else {
1102 		intel_wait_for_pipe_scanline_stopped(crtc);
1103 	}
1104 }
1105 
1106 /* Only for pre-ILK configs */
1107 void assert_pll(struct drm_i915_private *dev_priv,
1108 		enum pipe pipe, bool state)
1109 {
1110 	u32 val;
1111 	bool cur_state;
1112 
1113 	val = intel_de_read(dev_priv, DPLL(pipe));
1114 	cur_state = !!(val & DPLL_VCO_ENABLE);
1115 	I915_STATE_WARN(cur_state != state,
1116 	     "PLL state assertion failure (expected %s, current %s)\n",
1117 			onoff(state), onoff(cur_state));
1118 }
1119 
1120 /* XXX: the dsi pll is shared between MIPI DSI ports */
1121 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1122 {
1123 	u32 val;
1124 	bool cur_state;
1125 
1126 	vlv_cck_get(dev_priv);
1127 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1128 	vlv_cck_put(dev_priv);
1129 
1130 	cur_state = val & DSI_PLL_VCO_EN;
1131 	I915_STATE_WARN(cur_state != state,
1132 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1133 			onoff(state), onoff(cur_state));
1134 }
1135 
1136 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1137 			  enum pipe pipe, bool state)
1138 {
1139 	bool cur_state;
1140 
1141 	if (HAS_DDI(dev_priv)) {
1142 		/*
1143 		 * DDI does not have a specific FDI_TX register.
1144 		 *
1145 		 * FDI is never fed from EDP transcoder
1146 		 * so pipe->transcoder cast is fine here.
1147 		 */
1148 		enum transcoder cpu_transcoder = (enum transcoder)pipe;
1149 		u32 val = intel_de_read(dev_priv,
1150 					TRANS_DDI_FUNC_CTL(cpu_transcoder));
1151 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1152 	} else {
1153 		u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1154 		cur_state = !!(val & FDI_TX_ENABLE);
1155 	}
1156 	I915_STATE_WARN(cur_state != state,
1157 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1158 			onoff(state), onoff(cur_state));
1159 }
1160 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1161 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1162 
1163 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1164 			  enum pipe pipe, bool state)
1165 {
1166 	u32 val;
1167 	bool cur_state;
1168 
1169 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1170 	cur_state = !!(val & FDI_RX_ENABLE);
1171 	I915_STATE_WARN(cur_state != state,
1172 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1173 			onoff(state), onoff(cur_state));
1174 }
1175 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1176 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1177 
1178 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1179 				      enum pipe pipe)
1180 {
1181 	u32 val;
1182 
1183 	/* ILK FDI PLL is always enabled */
1184 	if (IS_GEN(dev_priv, 5))
1185 		return;
1186 
1187 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1188 	if (HAS_DDI(dev_priv))
1189 		return;
1190 
1191 	val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1192 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1193 }
1194 
1195 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1196 		       enum pipe pipe, bool state)
1197 {
1198 	u32 val;
1199 	bool cur_state;
1200 
1201 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1202 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1203 	I915_STATE_WARN(cur_state != state,
1204 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1205 			onoff(state), onoff(cur_state));
1206 }
1207 
1208 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1209 {
1210 	i915_reg_t pp_reg;
1211 	u32 val;
1212 	enum pipe panel_pipe = INVALID_PIPE;
1213 	bool locked = true;
1214 
1215 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1216 		return;
1217 
1218 	if (HAS_PCH_SPLIT(dev_priv)) {
1219 		u32 port_sel;
1220 
1221 		pp_reg = PP_CONTROL(0);
1222 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1223 
1224 		switch (port_sel) {
1225 		case PANEL_PORT_SELECT_LVDS:
1226 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1227 			break;
1228 		case PANEL_PORT_SELECT_DPA:
1229 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1230 			break;
1231 		case PANEL_PORT_SELECT_DPC:
1232 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1233 			break;
1234 		case PANEL_PORT_SELECT_DPD:
1235 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1236 			break;
1237 		default:
1238 			MISSING_CASE(port_sel);
1239 			break;
1240 		}
1241 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1242 		/* presumably write lock depends on pipe, not port select */
1243 		pp_reg = PP_CONTROL(pipe);
1244 		panel_pipe = pipe;
1245 	} else {
1246 		u32 port_sel;
1247 
1248 		pp_reg = PP_CONTROL(0);
1249 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1250 
1251 		drm_WARN_ON(&dev_priv->drm,
1252 			    port_sel != PANEL_PORT_SELECT_LVDS);
1253 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1254 	}
1255 
1256 	val = intel_de_read(dev_priv, pp_reg);
1257 	if (!(val & PANEL_POWER_ON) ||
1258 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1259 		locked = false;
1260 
1261 	I915_STATE_WARN(panel_pipe == pipe && locked,
1262 	     "panel assertion failure, pipe %c regs locked\n",
1263 	     pipe_name(pipe));
1264 }
1265 
1266 void assert_pipe(struct drm_i915_private *dev_priv,
1267 		 enum transcoder cpu_transcoder, bool state)
1268 {
1269 	bool cur_state;
1270 	enum intel_display_power_domain power_domain;
1271 	intel_wakeref_t wakeref;
1272 
1273 	/* we keep both pipes enabled on 830 */
1274 	if (IS_I830(dev_priv))
1275 		state = true;
1276 
1277 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1278 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1279 	if (wakeref) {
1280 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1281 		cur_state = !!(val & PIPECONF_ENABLE);
1282 
1283 		intel_display_power_put(dev_priv, power_domain, wakeref);
1284 	} else {
1285 		cur_state = false;
1286 	}
1287 
1288 	I915_STATE_WARN(cur_state != state,
1289 			"transcoder %s assertion failure (expected %s, current %s)\n",
1290 			transcoder_name(cpu_transcoder),
1291 			onoff(state), onoff(cur_state));
1292 }
1293 
1294 static void assert_plane(struct intel_plane *plane, bool state)
1295 {
1296 	enum pipe pipe;
1297 	bool cur_state;
1298 
1299 	cur_state = plane->get_hw_state(plane, &pipe);
1300 
1301 	I915_STATE_WARN(cur_state != state,
1302 			"%s assertion failure (expected %s, current %s)\n",
1303 			plane->base.name, onoff(state), onoff(cur_state));
1304 }
1305 
1306 #define assert_plane_enabled(p) assert_plane(p, true)
1307 #define assert_plane_disabled(p) assert_plane(p, false)
1308 
1309 static void assert_planes_disabled(struct intel_crtc *crtc)
1310 {
1311 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1312 	struct intel_plane *plane;
1313 
1314 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1315 		assert_plane_disabled(plane);
1316 }
1317 
1318 static void assert_vblank_disabled(struct drm_crtc *crtc)
1319 {
1320 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1321 		drm_crtc_vblank_put(crtc);
1322 }
1323 
1324 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1325 				    enum pipe pipe)
1326 {
1327 	u32 val;
1328 	bool enabled;
1329 
1330 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
1331 	enabled = !!(val & TRANS_ENABLE);
1332 	I915_STATE_WARN(enabled,
1333 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1334 	     pipe_name(pipe));
1335 }
1336 
1337 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1338 				   enum pipe pipe, enum port port,
1339 				   i915_reg_t dp_reg)
1340 {
1341 	enum pipe port_pipe;
1342 	bool state;
1343 
1344 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1345 
1346 	I915_STATE_WARN(state && port_pipe == pipe,
1347 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
1348 			port_name(port), pipe_name(pipe));
1349 
1350 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1351 			"IBX PCH DP %c still using transcoder B\n",
1352 			port_name(port));
1353 }
1354 
1355 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1356 				     enum pipe pipe, enum port port,
1357 				     i915_reg_t hdmi_reg)
1358 {
1359 	enum pipe port_pipe;
1360 	bool state;
1361 
1362 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1363 
1364 	I915_STATE_WARN(state && port_pipe == pipe,
1365 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1366 			port_name(port), pipe_name(pipe));
1367 
1368 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1369 			"IBX PCH HDMI %c still using transcoder B\n",
1370 			port_name(port));
1371 }
1372 
1373 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1374 				      enum pipe pipe)
1375 {
1376 	enum pipe port_pipe;
1377 
1378 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1379 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1380 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1381 
1382 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1383 			port_pipe == pipe,
1384 			"PCH VGA enabled on transcoder %c, should be disabled\n",
1385 			pipe_name(pipe));
1386 
1387 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1388 			port_pipe == pipe,
1389 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
1390 			pipe_name(pipe));
1391 
1392 	/* PCH SDVOB multiplex with HDMIB */
1393 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1394 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1395 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1396 }
1397 
1398 static void _vlv_enable_pll(struct intel_crtc *crtc,
1399 			    const struct intel_crtc_state *pipe_config)
1400 {
1401 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1402 	enum pipe pipe = crtc->pipe;
1403 
1404 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1405 	intel_de_posting_read(dev_priv, DPLL(pipe));
1406 	udelay(150);
1407 
1408 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1409 		drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1410 }
1411 
1412 static void vlv_enable_pll(struct intel_crtc *crtc,
1413 			   const struct intel_crtc_state *pipe_config)
1414 {
1415 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1416 	enum pipe pipe = crtc->pipe;
1417 
1418 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1419 
1420 	/* PLL is protected by panel, make sure we can write it */
1421 	assert_panel_unlocked(dev_priv, pipe);
1422 
1423 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1424 		_vlv_enable_pll(crtc, pipe_config);
1425 
1426 	intel_de_write(dev_priv, DPLL_MD(pipe),
1427 		       pipe_config->dpll_hw_state.dpll_md);
1428 	intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1429 }
1430 
1431 
1432 static void _chv_enable_pll(struct intel_crtc *crtc,
1433 			    const struct intel_crtc_state *pipe_config)
1434 {
1435 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1436 	enum pipe pipe = crtc->pipe;
1437 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1438 	u32 tmp;
1439 
1440 	vlv_dpio_get(dev_priv);
1441 
1442 	/* Enable back the 10bit clock to display controller */
1443 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1444 	tmp |= DPIO_DCLKP_EN;
1445 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1446 
1447 	vlv_dpio_put(dev_priv);
1448 
1449 	/*
1450 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1451 	 */
1452 	udelay(1);
1453 
1454 	/* Enable PLL */
1455 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1456 
1457 	/* Check PLL is locked */
1458 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1459 		drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1460 }
1461 
1462 static void chv_enable_pll(struct intel_crtc *crtc,
1463 			   const struct intel_crtc_state *pipe_config)
1464 {
1465 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1466 	enum pipe pipe = crtc->pipe;
1467 
1468 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1469 
1470 	/* PLL is protected by panel, make sure we can write it */
1471 	assert_panel_unlocked(dev_priv, pipe);
1472 
1473 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1474 		_chv_enable_pll(crtc, pipe_config);
1475 
1476 	if (pipe != PIPE_A) {
1477 		/*
1478 		 * WaPixelRepeatModeFixForC0:chv
1479 		 *
1480 		 * DPLLCMD is AWOL. Use chicken bits to propagate
1481 		 * the value from DPLLBMD to either pipe B or C.
1482 		 */
1483 		intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1484 		intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1485 			       pipe_config->dpll_hw_state.dpll_md);
1486 		intel_de_write(dev_priv, CBR4_VLV, 0);
1487 		dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1488 
1489 		/*
1490 		 * DPLLB VGA mode also seems to cause problems.
1491 		 * We should always have it disabled.
1492 		 */
1493 		drm_WARN_ON(&dev_priv->drm,
1494 			    (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1495 			     DPLL_VGA_MODE_DIS) == 0);
1496 	} else {
1497 		intel_de_write(dev_priv, DPLL_MD(pipe),
1498 			       pipe_config->dpll_hw_state.dpll_md);
1499 		intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1500 	}
1501 }
1502 
1503 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1504 {
1505 	if (IS_I830(dev_priv))
1506 		return false;
1507 
1508 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1509 }
1510 
1511 static void i9xx_enable_pll(struct intel_crtc *crtc,
1512 			    const struct intel_crtc_state *crtc_state)
1513 {
1514 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1515 	i915_reg_t reg = DPLL(crtc->pipe);
1516 	u32 dpll = crtc_state->dpll_hw_state.dpll;
1517 	int i;
1518 
1519 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1520 
1521 	/* PLL is protected by panel, make sure we can write it */
1522 	if (i9xx_has_pps(dev_priv))
1523 		assert_panel_unlocked(dev_priv, crtc->pipe);
1524 
1525 	/*
1526 	 * Apparently we need to have VGA mode enabled prior to changing
1527 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1528 	 * dividers, even though the register value does change.
1529 	 */
1530 	intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1531 	intel_de_write(dev_priv, reg, dpll);
1532 
1533 	/* Wait for the clocks to stabilize. */
1534 	intel_de_posting_read(dev_priv, reg);
1535 	udelay(150);
1536 
1537 	if (INTEL_GEN(dev_priv) >= 4) {
1538 		intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1539 			       crtc_state->dpll_hw_state.dpll_md);
1540 	} else {
1541 		/* The pixel multiplier can only be updated once the
1542 		 * DPLL is enabled and the clocks are stable.
1543 		 *
1544 		 * So write it again.
1545 		 */
1546 		intel_de_write(dev_priv, reg, dpll);
1547 	}
1548 
1549 	/* We do this three times for luck */
1550 	for (i = 0; i < 3; i++) {
1551 		intel_de_write(dev_priv, reg, dpll);
1552 		intel_de_posting_read(dev_priv, reg);
1553 		udelay(150); /* wait for warmup */
1554 	}
1555 }
1556 
1557 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1558 {
1559 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1560 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1561 	enum pipe pipe = crtc->pipe;
1562 
1563 	/* Don't disable pipe or pipe PLLs if needed */
1564 	if (IS_I830(dev_priv))
1565 		return;
1566 
1567 	/* Make sure the pipe isn't still relying on us */
1568 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1569 
1570 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1571 	intel_de_posting_read(dev_priv, DPLL(pipe));
1572 }
1573 
1574 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1575 {
1576 	u32 val;
1577 
1578 	/* Make sure the pipe isn't still relying on us */
1579 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1580 
1581 	val = DPLL_INTEGRATED_REF_CLK_VLV |
1582 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1583 	if (pipe != PIPE_A)
1584 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1585 
1586 	intel_de_write(dev_priv, DPLL(pipe), val);
1587 	intel_de_posting_read(dev_priv, DPLL(pipe));
1588 }
1589 
1590 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1591 {
1592 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1593 	u32 val;
1594 
1595 	/* Make sure the pipe isn't still relying on us */
1596 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1597 
1598 	val = DPLL_SSC_REF_CLK_CHV |
1599 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1600 	if (pipe != PIPE_A)
1601 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1602 
1603 	intel_de_write(dev_priv, DPLL(pipe), val);
1604 	intel_de_posting_read(dev_priv, DPLL(pipe));
1605 
1606 	vlv_dpio_get(dev_priv);
1607 
1608 	/* Disable 10bit clock to display controller */
1609 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1610 	val &= ~DPIO_DCLKP_EN;
1611 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1612 
1613 	vlv_dpio_put(dev_priv);
1614 }
1615 
1616 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1617 			 struct intel_digital_port *dig_port,
1618 			 unsigned int expected_mask)
1619 {
1620 	u32 port_mask;
1621 	i915_reg_t dpll_reg;
1622 
1623 	switch (dig_port->base.port) {
1624 	case PORT_B:
1625 		port_mask = DPLL_PORTB_READY_MASK;
1626 		dpll_reg = DPLL(0);
1627 		break;
1628 	case PORT_C:
1629 		port_mask = DPLL_PORTC_READY_MASK;
1630 		dpll_reg = DPLL(0);
1631 		expected_mask <<= 4;
1632 		break;
1633 	case PORT_D:
1634 		port_mask = DPLL_PORTD_READY_MASK;
1635 		dpll_reg = DPIO_PHY_STATUS;
1636 		break;
1637 	default:
1638 		BUG();
1639 	}
1640 
1641 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
1642 				       port_mask, expected_mask, 1000))
1643 		drm_WARN(&dev_priv->drm, 1,
1644 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
1645 			 dig_port->base.base.base.id, dig_port->base.base.name,
1646 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
1647 			 expected_mask);
1648 }
1649 
1650 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1651 {
1652 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1653 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1654 	enum pipe pipe = crtc->pipe;
1655 	i915_reg_t reg;
1656 	u32 val, pipeconf_val;
1657 
1658 	/* Make sure PCH DPLL is enabled */
1659 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1660 
1661 	/* FDI must be feeding us bits for PCH ports */
1662 	assert_fdi_tx_enabled(dev_priv, pipe);
1663 	assert_fdi_rx_enabled(dev_priv, pipe);
1664 
1665 	if (HAS_PCH_CPT(dev_priv)) {
1666 		reg = TRANS_CHICKEN2(pipe);
1667 		val = intel_de_read(dev_priv, reg);
1668 		/*
1669 		 * Workaround: Set the timing override bit
1670 		 * before enabling the pch transcoder.
1671 		 */
1672 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1673 		/* Configure frame start delay to match the CPU */
1674 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1675 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1676 		intel_de_write(dev_priv, reg, val);
1677 	}
1678 
1679 	reg = PCH_TRANSCONF(pipe);
1680 	val = intel_de_read(dev_priv, reg);
1681 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
1682 
1683 	if (HAS_PCH_IBX(dev_priv)) {
1684 		/* Configure frame start delay to match the CPU */
1685 		val &= ~TRANS_FRAME_START_DELAY_MASK;
1686 		val |= TRANS_FRAME_START_DELAY(0);
1687 
1688 		/*
1689 		 * Make the BPC in transcoder be consistent with
1690 		 * that in pipeconf reg. For HDMI we must use 8bpc
1691 		 * here for both 8bpc and 12bpc.
1692 		 */
1693 		val &= ~PIPECONF_BPC_MASK;
1694 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1695 			val |= PIPECONF_8BPC;
1696 		else
1697 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1698 	}
1699 
1700 	val &= ~TRANS_INTERLACE_MASK;
1701 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1702 		if (HAS_PCH_IBX(dev_priv) &&
1703 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1704 			val |= TRANS_LEGACY_INTERLACED_ILK;
1705 		else
1706 			val |= TRANS_INTERLACED;
1707 	} else {
1708 		val |= TRANS_PROGRESSIVE;
1709 	}
1710 
1711 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
1712 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1713 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1714 			pipe_name(pipe));
1715 }
1716 
1717 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1718 				      enum transcoder cpu_transcoder)
1719 {
1720 	u32 val, pipeconf_val;
1721 
1722 	/* FDI must be feeding us bits for PCH ports */
1723 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1724 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
1725 
1726 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1727 	/* Workaround: set timing override bit. */
1728 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1729 	/* Configure frame start delay to match the CPU */
1730 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1731 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1732 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1733 
1734 	val = TRANS_ENABLE;
1735 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1736 
1737 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1738 	    PIPECONF_INTERLACED_ILK)
1739 		val |= TRANS_INTERLACED;
1740 	else
1741 		val |= TRANS_PROGRESSIVE;
1742 
1743 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1744 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1745 				  TRANS_STATE_ENABLE, 100))
1746 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
1747 }
1748 
1749 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1750 				       enum pipe pipe)
1751 {
1752 	i915_reg_t reg;
1753 	u32 val;
1754 
1755 	/* FDI relies on the transcoder */
1756 	assert_fdi_tx_disabled(dev_priv, pipe);
1757 	assert_fdi_rx_disabled(dev_priv, pipe);
1758 
1759 	/* Ports must be off as well */
1760 	assert_pch_ports_disabled(dev_priv, pipe);
1761 
1762 	reg = PCH_TRANSCONF(pipe);
1763 	val = intel_de_read(dev_priv, reg);
1764 	val &= ~TRANS_ENABLE;
1765 	intel_de_write(dev_priv, reg, val);
1766 	/* wait for PCH transcoder off, transcoder state */
1767 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1768 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1769 			pipe_name(pipe));
1770 
1771 	if (HAS_PCH_CPT(dev_priv)) {
1772 		/* Workaround: Clear the timing override chicken bit again. */
1773 		reg = TRANS_CHICKEN2(pipe);
1774 		val = intel_de_read(dev_priv, reg);
1775 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1776 		intel_de_write(dev_priv, reg, val);
1777 	}
1778 }
1779 
1780 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1781 {
1782 	u32 val;
1783 
1784 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
1785 	val &= ~TRANS_ENABLE;
1786 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1787 	/* wait for PCH transcoder off, transcoder state */
1788 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1789 				    TRANS_STATE_ENABLE, 50))
1790 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
1791 
1792 	/* Workaround: clear timing override bit. */
1793 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1794 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1795 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1796 }
1797 
1798 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1799 {
1800 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1801 
1802 	if (HAS_PCH_LPT(dev_priv))
1803 		return PIPE_A;
1804 	else
1805 		return crtc->pipe;
1806 }
1807 
1808 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1809 {
1810 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1811 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1812 	u32 mode_flags = crtc->mode_flags;
1813 
1814 	/*
1815 	 * From Gen 11, In case of dsi cmd mode, frame counter wouldnt
1816 	 * have updated at the beginning of TE, if we want to use
1817 	 * the hw counter, then we would find it updated in only
1818 	 * the next TE, hence switching to sw counter.
1819 	 */
1820 	if (mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 | I915_MODE_FLAG_DSI_USE_TE1))
1821 		return 0;
1822 
1823 	/*
1824 	 * On i965gm the hardware frame counter reads
1825 	 * zero when the TV encoder is enabled :(
1826 	 */
1827 	if (IS_I965GM(dev_priv) &&
1828 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1829 		return 0;
1830 
1831 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1832 		return 0xffffffff; /* full 32 bit counter */
1833 	else if (INTEL_GEN(dev_priv) >= 3)
1834 		return 0xffffff; /* only 24 bits of frame count */
1835 	else
1836 		return 0; /* Gen2 doesn't have a hardware frame counter */
1837 }
1838 
1839 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1840 {
1841 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1842 
1843 	assert_vblank_disabled(&crtc->base);
1844 	drm_crtc_set_max_vblank_count(&crtc->base,
1845 				      intel_crtc_max_vblank_count(crtc_state));
1846 	drm_crtc_vblank_on(&crtc->base);
1847 }
1848 
1849 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
1850 {
1851 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1852 
1853 	drm_crtc_vblank_off(&crtc->base);
1854 	assert_vblank_disabled(&crtc->base);
1855 }
1856 
1857 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1858 {
1859 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1860 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1861 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1862 	enum pipe pipe = crtc->pipe;
1863 	i915_reg_t reg;
1864 	u32 val;
1865 
1866 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
1867 
1868 	assert_planes_disabled(crtc);
1869 
1870 	/*
1871 	 * A pipe without a PLL won't actually be able to drive bits from
1872 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1873 	 * need the check.
1874 	 */
1875 	if (HAS_GMCH(dev_priv)) {
1876 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1877 			assert_dsi_pll_enabled(dev_priv);
1878 		else
1879 			assert_pll_enabled(dev_priv, pipe);
1880 	} else {
1881 		if (new_crtc_state->has_pch_encoder) {
1882 			/* if driving the PCH, we need FDI enabled */
1883 			assert_fdi_rx_pll_enabled(dev_priv,
1884 						  intel_crtc_pch_transcoder(crtc));
1885 			assert_fdi_tx_pll_enabled(dev_priv,
1886 						  (enum pipe) cpu_transcoder);
1887 		}
1888 		/* FIXME: assert CPU port conditions for SNB+ */
1889 	}
1890 
1891 	trace_intel_pipe_enable(crtc);
1892 
1893 	reg = PIPECONF(cpu_transcoder);
1894 	val = intel_de_read(dev_priv, reg);
1895 	if (val & PIPECONF_ENABLE) {
1896 		/* we keep both pipes enabled on 830 */
1897 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
1898 		return;
1899 	}
1900 
1901 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1902 	intel_de_posting_read(dev_priv, reg);
1903 
1904 	/*
1905 	 * Until the pipe starts PIPEDSL reads will return a stale value,
1906 	 * which causes an apparent vblank timestamp jump when PIPEDSL
1907 	 * resets to its proper value. That also messes up the frame count
1908 	 * when it's derived from the timestamps. So let's wait for the
1909 	 * pipe to start properly before we call drm_crtc_vblank_on()
1910 	 */
1911 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1912 		intel_wait_for_pipe_scanline_moving(crtc);
1913 }
1914 
1915 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1916 {
1917 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1918 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1919 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1920 	enum pipe pipe = crtc->pipe;
1921 	i915_reg_t reg;
1922 	u32 val;
1923 
1924 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1925 
1926 	/*
1927 	 * Make sure planes won't keep trying to pump pixels to us,
1928 	 * or we might hang the display.
1929 	 */
1930 	assert_planes_disabled(crtc);
1931 
1932 	trace_intel_pipe_disable(crtc);
1933 
1934 	reg = PIPECONF(cpu_transcoder);
1935 	val = intel_de_read(dev_priv, reg);
1936 	if ((val & PIPECONF_ENABLE) == 0)
1937 		return;
1938 
1939 	/*
1940 	 * Double wide has implications for planes
1941 	 * so best keep it disabled when not needed.
1942 	 */
1943 	if (old_crtc_state->double_wide)
1944 		val &= ~PIPECONF_DOUBLE_WIDE;
1945 
1946 	/* Don't disable pipe or pipe PLLs if needed */
1947 	if (!IS_I830(dev_priv))
1948 		val &= ~PIPECONF_ENABLE;
1949 
1950 	intel_de_write(dev_priv, reg, val);
1951 	if ((val & PIPECONF_ENABLE) == 0)
1952 		intel_wait_for_pipe_off(old_crtc_state);
1953 }
1954 
1955 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1956 {
1957 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1958 }
1959 
1960 static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1961 {
1962 	if (!is_ccs_modifier(fb->modifier))
1963 		return false;
1964 
1965 	return plane >= fb->format->num_planes / 2;
1966 }
1967 
1968 static bool is_gen12_ccs_modifier(u64 modifier)
1969 {
1970 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1971 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1972 
1973 }
1974 
1975 static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1976 {
1977 	return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1978 }
1979 
1980 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1981 {
1982 	if (is_ccs_modifier(fb->modifier))
1983 		return is_ccs_plane(fb, plane);
1984 
1985 	return plane == 1;
1986 }
1987 
1988 static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1989 {
1990 	drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1991 		    (main_plane && main_plane >= fb->format->num_planes / 2));
1992 
1993 	return fb->format->num_planes / 2 + main_plane;
1994 }
1995 
1996 static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1997 {
1998 	drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1999 		    ccs_plane < fb->format->num_planes / 2);
2000 
2001 	return ccs_plane - fb->format->num_planes / 2;
2002 }
2003 
2004 int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
2005 {
2006 	struct drm_i915_private *i915 = to_i915(fb->dev);
2007 
2008 	if (is_ccs_modifier(fb->modifier))
2009 		return main_to_ccs_plane(fb, main_plane);
2010 	else if (INTEL_GEN(i915) < 11 &&
2011 		 intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
2012 		return 1;
2013 	else
2014 		return 0;
2015 }
2016 
2017 bool
2018 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
2019 				    uint64_t modifier)
2020 {
2021 	return info->is_yuv &&
2022 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2023 }
2024 
2025 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2026 				   int color_plane)
2027 {
2028 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2029 	       color_plane == 1;
2030 }
2031 
2032 static unsigned int
2033 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
2034 {
2035 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2036 	unsigned int cpp = fb->format->cpp[color_plane];
2037 
2038 	switch (fb->modifier) {
2039 	case DRM_FORMAT_MOD_LINEAR:
2040 		return intel_tile_size(dev_priv);
2041 	case I915_FORMAT_MOD_X_TILED:
2042 		if (IS_GEN(dev_priv, 2))
2043 			return 128;
2044 		else
2045 			return 512;
2046 	case I915_FORMAT_MOD_Y_TILED_CCS:
2047 		if (is_ccs_plane(fb, color_plane))
2048 			return 128;
2049 		fallthrough;
2050 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2051 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2052 		if (is_ccs_plane(fb, color_plane))
2053 			return 64;
2054 		fallthrough;
2055 	case I915_FORMAT_MOD_Y_TILED:
2056 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
2057 			return 128;
2058 		else
2059 			return 512;
2060 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2061 		if (is_ccs_plane(fb, color_plane))
2062 			return 128;
2063 		fallthrough;
2064 	case I915_FORMAT_MOD_Yf_TILED:
2065 		switch (cpp) {
2066 		case 1:
2067 			return 64;
2068 		case 2:
2069 		case 4:
2070 			return 128;
2071 		case 8:
2072 		case 16:
2073 			return 256;
2074 		default:
2075 			MISSING_CASE(cpp);
2076 			return cpp;
2077 		}
2078 		break;
2079 	default:
2080 		MISSING_CASE(fb->modifier);
2081 		return cpp;
2082 	}
2083 }
2084 
2085 static unsigned int
2086 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
2087 {
2088 	if (is_gen12_ccs_plane(fb, color_plane))
2089 		return 1;
2090 
2091 	return intel_tile_size(to_i915(fb->dev)) /
2092 		intel_tile_width_bytes(fb, color_plane);
2093 }
2094 
2095 /* Return the tile dimensions in pixel units */
2096 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
2097 			    unsigned int *tile_width,
2098 			    unsigned int *tile_height)
2099 {
2100 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2101 	unsigned int cpp = fb->format->cpp[color_plane];
2102 
2103 	*tile_width = tile_width_bytes / cpp;
2104 	*tile_height = intel_tile_height(fb, color_plane);
2105 }
2106 
2107 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2108 					int color_plane)
2109 {
2110 	unsigned int tile_width, tile_height;
2111 
2112 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2113 
2114 	return fb->pitches[color_plane] * tile_height;
2115 }
2116 
2117 unsigned int
2118 intel_fb_align_height(const struct drm_framebuffer *fb,
2119 		      int color_plane, unsigned int height)
2120 {
2121 	unsigned int tile_height = intel_tile_height(fb, color_plane);
2122 
2123 	return ALIGN(height, tile_height);
2124 }
2125 
2126 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127 {
2128 	unsigned int size = 0;
2129 	int i;
2130 
2131 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132 		size += rot_info->plane[i].width * rot_info->plane[i].height;
2133 
2134 	return size;
2135 }
2136 
2137 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2138 {
2139 	unsigned int size = 0;
2140 	int i;
2141 
2142 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2143 		size += rem_info->plane[i].width * rem_info->plane[i].height;
2144 
2145 	return size;
2146 }
2147 
2148 static void
2149 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2150 			const struct drm_framebuffer *fb,
2151 			unsigned int rotation)
2152 {
2153 	view->type = I915_GGTT_VIEW_NORMAL;
2154 	if (drm_rotation_90_or_270(rotation)) {
2155 		view->type = I915_GGTT_VIEW_ROTATED;
2156 		view->rotated = to_intel_framebuffer(fb)->rot_info;
2157 	}
2158 }
2159 
2160 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2161 {
2162 	if (IS_I830(dev_priv))
2163 		return 16 * 1024;
2164 	else if (IS_I85X(dev_priv))
2165 		return 256;
2166 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2167 		return 32;
2168 	else
2169 		return 4 * 1024;
2170 }
2171 
2172 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2173 {
2174 	if (INTEL_GEN(dev_priv) >= 9)
2175 		return 256 * 1024;
2176 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2177 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2178 		return 128 * 1024;
2179 	else if (INTEL_GEN(dev_priv) >= 4)
2180 		return 4 * 1024;
2181 	else
2182 		return 0;
2183 }
2184 
2185 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2186 					 int color_plane)
2187 {
2188 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2189 
2190 	/* AUX_DIST needs only 4K alignment */
2191 	if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2192 	    is_ccs_plane(fb, color_plane))
2193 		return 4096;
2194 
2195 	switch (fb->modifier) {
2196 	case DRM_FORMAT_MOD_LINEAR:
2197 		return intel_linear_alignment(dev_priv);
2198 	case I915_FORMAT_MOD_X_TILED:
2199 		if (INTEL_GEN(dev_priv) >= 9)
2200 			return 256 * 1024;
2201 		return 0;
2202 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2203 		if (is_semiplanar_uv_plane(fb, color_plane))
2204 			return intel_tile_row_size(fb, color_plane);
2205 		fallthrough;
2206 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2207 		return 16 * 1024;
2208 	case I915_FORMAT_MOD_Y_TILED_CCS:
2209 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2210 	case I915_FORMAT_MOD_Y_TILED:
2211 		if (INTEL_GEN(dev_priv) >= 12 &&
2212 		    is_semiplanar_uv_plane(fb, color_plane))
2213 			return intel_tile_row_size(fb, color_plane);
2214 		fallthrough;
2215 	case I915_FORMAT_MOD_Yf_TILED:
2216 		return 1 * 1024 * 1024;
2217 	default:
2218 		MISSING_CASE(fb->modifier);
2219 		return 0;
2220 	}
2221 }
2222 
2223 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2224 {
2225 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2226 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2227 
2228 	return INTEL_GEN(dev_priv) < 4 ||
2229 		(plane->has_fbc &&
2230 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2231 }
2232 
2233 struct i915_vma *
2234 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2235 			   const struct i915_ggtt_view *view,
2236 			   bool uses_fence,
2237 			   unsigned long *out_flags)
2238 {
2239 	struct drm_device *dev = fb->dev;
2240 	struct drm_i915_private *dev_priv = to_i915(dev);
2241 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2242 	intel_wakeref_t wakeref;
2243 	struct i915_vma *vma;
2244 	unsigned int pinctl;
2245 	u32 alignment;
2246 
2247 	if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
2248 		return ERR_PTR(-EINVAL);
2249 
2250 	alignment = intel_surf_alignment(fb, 0);
2251 	if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
2252 		return ERR_PTR(-EINVAL);
2253 
2254 	/* Note that the w/a also requires 64 PTE of padding following the
2255 	 * bo. We currently fill all unused PTE with the shadow page and so
2256 	 * we should always have valid PTE following the scanout preventing
2257 	 * the VT-d warning.
2258 	 */
2259 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2260 		alignment = 256 * 1024;
2261 
2262 	/*
2263 	 * Global gtt pte registers are special registers which actually forward
2264 	 * writes to a chunk of system memory. Which means that there is no risk
2265 	 * that the register values disappear as soon as we call
2266 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2267 	 * pin/unpin/fence and not more.
2268 	 */
2269 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2270 
2271 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2272 
2273 	/*
2274 	 * Valleyview is definitely limited to scanning out the first
2275 	 * 512MiB. Lets presume this behaviour was inherited from the
2276 	 * g4x display engine and that all earlier gen are similarly
2277 	 * limited. Testing suggests that it is a little more
2278 	 * complicated than this. For example, Cherryview appears quite
2279 	 * happy to scanout from anywhere within its global aperture.
2280 	 */
2281 	pinctl = 0;
2282 	if (HAS_GMCH(dev_priv))
2283 		pinctl |= PIN_MAPPABLE;
2284 
2285 	vma = i915_gem_object_pin_to_display_plane(obj,
2286 						   alignment, view, pinctl);
2287 	if (IS_ERR(vma))
2288 		goto err;
2289 
2290 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2291 		int ret;
2292 
2293 		/*
2294 		 * Install a fence for tiled scan-out. Pre-i965 always needs a
2295 		 * fence, whereas 965+ only requires a fence if using
2296 		 * framebuffer compression.  For simplicity, we always, when
2297 		 * possible, install a fence as the cost is not that onerous.
2298 		 *
2299 		 * If we fail to fence the tiled scanout, then either the
2300 		 * modeset will reject the change (which is highly unlikely as
2301 		 * the affected systems, all but one, do not have unmappable
2302 		 * space) or we will not be able to enable full powersaving
2303 		 * techniques (also likely not to apply due to various limits
2304 		 * FBC and the like impose on the size of the buffer, which
2305 		 * presumably we violated anyway with this unmappable buffer).
2306 		 * Anyway, it is presumably better to stumble onwards with
2307 		 * something and try to run the system in a "less than optimal"
2308 		 * mode that matches the user configuration.
2309 		 */
2310 		ret = i915_vma_pin_fence(vma);
2311 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2312 			i915_gem_object_unpin_from_display_plane(vma);
2313 			vma = ERR_PTR(ret);
2314 			goto err;
2315 		}
2316 
2317 		if (ret == 0 && vma->fence)
2318 			*out_flags |= PLANE_HAS_FENCE;
2319 	}
2320 
2321 	i915_vma_get(vma);
2322 err:
2323 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2324 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2325 	return vma;
2326 }
2327 
2328 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2329 {
2330 	i915_gem_object_lock(vma->obj, NULL);
2331 	if (flags & PLANE_HAS_FENCE)
2332 		i915_vma_unpin_fence(vma);
2333 	i915_gem_object_unpin_from_display_plane(vma);
2334 	i915_gem_object_unlock(vma->obj);
2335 
2336 	i915_vma_put(vma);
2337 }
2338 
2339 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2340 			  unsigned int rotation)
2341 {
2342 	if (drm_rotation_90_or_270(rotation))
2343 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2344 	else
2345 		return fb->pitches[color_plane];
2346 }
2347 
2348 /*
2349  * Convert the x/y offsets into a linear offset.
2350  * Only valid with 0/180 degree rotation, which is fine since linear
2351  * offset is only used with linear buffers on pre-hsw and tiled buffers
2352  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2353  */
2354 u32 intel_fb_xy_to_linear(int x, int y,
2355 			  const struct intel_plane_state *state,
2356 			  int color_plane)
2357 {
2358 	const struct drm_framebuffer *fb = state->hw.fb;
2359 	unsigned int cpp = fb->format->cpp[color_plane];
2360 	unsigned int pitch = state->color_plane[color_plane].stride;
2361 
2362 	return y * pitch + x * cpp;
2363 }
2364 
2365 /*
2366  * Add the x/y offsets derived from fb->offsets[] to the user
2367  * specified plane src x/y offsets. The resulting x/y offsets
2368  * specify the start of scanout from the beginning of the gtt mapping.
2369  */
2370 void intel_add_fb_offsets(int *x, int *y,
2371 			  const struct intel_plane_state *state,
2372 			  int color_plane)
2373 
2374 {
2375 	*x += state->color_plane[color_plane].x;
2376 	*y += state->color_plane[color_plane].y;
2377 }
2378 
2379 static u32 intel_adjust_tile_offset(int *x, int *y,
2380 				    unsigned int tile_width,
2381 				    unsigned int tile_height,
2382 				    unsigned int tile_size,
2383 				    unsigned int pitch_tiles,
2384 				    u32 old_offset,
2385 				    u32 new_offset)
2386 {
2387 	unsigned int pitch_pixels = pitch_tiles * tile_width;
2388 	unsigned int tiles;
2389 
2390 	WARN_ON(old_offset & (tile_size - 1));
2391 	WARN_ON(new_offset & (tile_size - 1));
2392 	WARN_ON(new_offset > old_offset);
2393 
2394 	tiles = (old_offset - new_offset) / tile_size;
2395 
2396 	*y += tiles / pitch_tiles * tile_height;
2397 	*x += tiles % pitch_tiles * tile_width;
2398 
2399 	/* minimize x in case it got needlessly big */
2400 	*y += *x / pitch_pixels * tile_height;
2401 	*x %= pitch_pixels;
2402 
2403 	return new_offset;
2404 }
2405 
2406 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2407 {
2408 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2409 	       is_gen12_ccs_plane(fb, color_plane);
2410 }
2411 
2412 static u32 intel_adjust_aligned_offset(int *x, int *y,
2413 				       const struct drm_framebuffer *fb,
2414 				       int color_plane,
2415 				       unsigned int rotation,
2416 				       unsigned int pitch,
2417 				       u32 old_offset, u32 new_offset)
2418 {
2419 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2420 	unsigned int cpp = fb->format->cpp[color_plane];
2421 
2422 	drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
2423 
2424 	if (!is_surface_linear(fb, color_plane)) {
2425 		unsigned int tile_size, tile_width, tile_height;
2426 		unsigned int pitch_tiles;
2427 
2428 		tile_size = intel_tile_size(dev_priv);
2429 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2430 
2431 		if (drm_rotation_90_or_270(rotation)) {
2432 			pitch_tiles = pitch / tile_height;
2433 			swap(tile_width, tile_height);
2434 		} else {
2435 			pitch_tiles = pitch / (tile_width * cpp);
2436 		}
2437 
2438 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2439 					 tile_size, pitch_tiles,
2440 					 old_offset, new_offset);
2441 	} else {
2442 		old_offset += *y * pitch + *x * cpp;
2443 
2444 		*y = (old_offset - new_offset) / pitch;
2445 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
2446 	}
2447 
2448 	return new_offset;
2449 }
2450 
2451 /*
2452  * Adjust the tile offset by moving the difference into
2453  * the x/y offsets.
2454  */
2455 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2456 					     const struct intel_plane_state *state,
2457 					     int color_plane,
2458 					     u32 old_offset, u32 new_offset)
2459 {
2460 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2461 					   state->hw.rotation,
2462 					   state->color_plane[color_plane].stride,
2463 					   old_offset, new_offset);
2464 }
2465 
2466 /*
2467  * Computes the aligned offset to the base tile and adjusts
2468  * x, y. bytes per pixel is assumed to be a power-of-two.
2469  *
2470  * In the 90/270 rotated case, x and y are assumed
2471  * to be already rotated to match the rotated GTT view, and
2472  * pitch is the tile_height aligned framebuffer height.
2473  *
2474  * This function is used when computing the derived information
2475  * under intel_framebuffer, so using any of that information
2476  * here is not allowed. Anything under drm_framebuffer can be
2477  * used. This is why the user has to pass in the pitch since it
2478  * is specified in the rotated orientation.
2479  */
2480 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2481 					int *x, int *y,
2482 					const struct drm_framebuffer *fb,
2483 					int color_plane,
2484 					unsigned int pitch,
2485 					unsigned int rotation,
2486 					u32 alignment)
2487 {
2488 	unsigned int cpp = fb->format->cpp[color_plane];
2489 	u32 offset, offset_aligned;
2490 
2491 	if (!is_surface_linear(fb, color_plane)) {
2492 		unsigned int tile_size, tile_width, tile_height;
2493 		unsigned int tile_rows, tiles, pitch_tiles;
2494 
2495 		tile_size = intel_tile_size(dev_priv);
2496 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2497 
2498 		if (drm_rotation_90_or_270(rotation)) {
2499 			pitch_tiles = pitch / tile_height;
2500 			swap(tile_width, tile_height);
2501 		} else {
2502 			pitch_tiles = pitch / (tile_width * cpp);
2503 		}
2504 
2505 		tile_rows = *y / tile_height;
2506 		*y %= tile_height;
2507 
2508 		tiles = *x / tile_width;
2509 		*x %= tile_width;
2510 
2511 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2512 
2513 		offset_aligned = offset;
2514 		if (alignment)
2515 			offset_aligned = rounddown(offset_aligned, alignment);
2516 
2517 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2518 					 tile_size, pitch_tiles,
2519 					 offset, offset_aligned);
2520 	} else {
2521 		offset = *y * pitch + *x * cpp;
2522 		offset_aligned = offset;
2523 		if (alignment) {
2524 			offset_aligned = rounddown(offset_aligned, alignment);
2525 			*y = (offset % alignment) / pitch;
2526 			*x = ((offset % alignment) - *y * pitch) / cpp;
2527 		} else {
2528 			*y = *x = 0;
2529 		}
2530 	}
2531 
2532 	return offset_aligned;
2533 }
2534 
2535 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2536 					      const struct intel_plane_state *state,
2537 					      int color_plane)
2538 {
2539 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
2540 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2541 	const struct drm_framebuffer *fb = state->hw.fb;
2542 	unsigned int rotation = state->hw.rotation;
2543 	int pitch = state->color_plane[color_plane].stride;
2544 	u32 alignment;
2545 
2546 	if (intel_plane->id == PLANE_CURSOR)
2547 		alignment = intel_cursor_alignment(dev_priv);
2548 	else
2549 		alignment = intel_surf_alignment(fb, color_plane);
2550 
2551 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2552 					    pitch, rotation, alignment);
2553 }
2554 
2555 /* Convert the fb->offset[] into x/y offsets */
2556 static int intel_fb_offset_to_xy(int *x, int *y,
2557 				 const struct drm_framebuffer *fb,
2558 				 int color_plane)
2559 {
2560 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2561 	unsigned int height;
2562 	u32 alignment;
2563 
2564 	if (INTEL_GEN(dev_priv) >= 12 &&
2565 	    is_semiplanar_uv_plane(fb, color_plane))
2566 		alignment = intel_tile_row_size(fb, color_plane);
2567 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2568 		alignment = intel_tile_size(dev_priv);
2569 	else
2570 		alignment = 0;
2571 
2572 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
2573 		drm_dbg_kms(&dev_priv->drm,
2574 			    "Misaligned offset 0x%08x for color plane %d\n",
2575 			    fb->offsets[color_plane], color_plane);
2576 		return -EINVAL;
2577 	}
2578 
2579 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2580 	height = ALIGN(height, intel_tile_height(fb, color_plane));
2581 
2582 	/* Catch potential overflows early */
2583 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2584 			    fb->offsets[color_plane])) {
2585 		drm_dbg_kms(&dev_priv->drm,
2586 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
2587 			    fb->offsets[color_plane], fb->pitches[color_plane],
2588 			    color_plane);
2589 		return -ERANGE;
2590 	}
2591 
2592 	*x = 0;
2593 	*y = 0;
2594 
2595 	intel_adjust_aligned_offset(x, y,
2596 				    fb, color_plane, DRM_MODE_ROTATE_0,
2597 				    fb->pitches[color_plane],
2598 				    fb->offsets[color_plane], 0);
2599 
2600 	return 0;
2601 }
2602 
2603 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2604 {
2605 	switch (fb_modifier) {
2606 	case I915_FORMAT_MOD_X_TILED:
2607 		return I915_TILING_X;
2608 	case I915_FORMAT_MOD_Y_TILED:
2609 	case I915_FORMAT_MOD_Y_TILED_CCS:
2610 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2611 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2612 		return I915_TILING_Y;
2613 	default:
2614 		return I915_TILING_NONE;
2615 	}
2616 }
2617 
2618 /*
2619  * From the Sky Lake PRM:
2620  * "The Color Control Surface (CCS) contains the compression status of
2621  *  the cache-line pairs. The compression state of the cache-line pair
2622  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2623  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2624  *  cache-line-pairs. CCS is always Y tiled."
2625  *
2626  * Since cache line pairs refers to horizontally adjacent cache lines,
2627  * each cache line in the CCS corresponds to an area of 32x16 cache
2628  * lines on the main surface. Since each pixel is 4 bytes, this gives
2629  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2630  * main surface.
2631  */
2632 static const struct drm_format_info skl_ccs_formats[] = {
2633 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2634 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2635 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2636 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2637 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2638 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2639 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2640 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2641 };
2642 
2643 /*
2644  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2645  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2646  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2647  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2648  * the main surface.
2649  */
2650 static const struct drm_format_info gen12_ccs_formats[] = {
2651 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2652 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2653 	  .hsub = 1, .vsub = 1, },
2654 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2655 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2656 	  .hsub = 1, .vsub = 1, },
2657 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2658 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2659 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2660 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2661 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2662 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2663 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
2664 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2665 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2666 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
2667 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2668 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2669 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
2670 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2671 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2672 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
2673 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2674 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2675 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
2676 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2677 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2678 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
2679 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2680 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2681 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
2682 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2683 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2684 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
2685 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2686 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2687 };
2688 
2689 static const struct drm_format_info *
2690 lookup_format_info(const struct drm_format_info formats[],
2691 		   int num_formats, u32 format)
2692 {
2693 	int i;
2694 
2695 	for (i = 0; i < num_formats; i++) {
2696 		if (formats[i].format == format)
2697 			return &formats[i];
2698 	}
2699 
2700 	return NULL;
2701 }
2702 
2703 static const struct drm_format_info *
2704 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2705 {
2706 	switch (cmd->modifier[0]) {
2707 	case I915_FORMAT_MOD_Y_TILED_CCS:
2708 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2709 		return lookup_format_info(skl_ccs_formats,
2710 					  ARRAY_SIZE(skl_ccs_formats),
2711 					  cmd->pixel_format);
2712 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2713 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2714 		return lookup_format_info(gen12_ccs_formats,
2715 					  ARRAY_SIZE(gen12_ccs_formats),
2716 					  cmd->pixel_format);
2717 	default:
2718 		return NULL;
2719 	}
2720 }
2721 
2722 bool is_ccs_modifier(u64 modifier)
2723 {
2724 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2725 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
2726 	       modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2727 	       modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2728 }
2729 
2730 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2731 {
2732 	return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2733 			    512) * 64;
2734 }
2735 
2736 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2737 			      u32 pixel_format, u64 modifier)
2738 {
2739 	struct intel_crtc *crtc;
2740 	struct intel_plane *plane;
2741 
2742 	/*
2743 	 * We assume the primary plane for pipe A has
2744 	 * the highest stride limits of them all,
2745 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
2746 	 */
2747 	crtc = intel_get_first_crtc(dev_priv);
2748 	if (!crtc)
2749 		return 0;
2750 
2751 	plane = to_intel_plane(crtc->base.primary);
2752 
2753 	return plane->max_stride(plane, pixel_format, modifier,
2754 				 DRM_MODE_ROTATE_0);
2755 }
2756 
2757 static
2758 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2759 			u32 pixel_format, u64 modifier)
2760 {
2761 	/*
2762 	 * Arbitrary limit for gen4+ chosen to match the
2763 	 * render engine max stride.
2764 	 *
2765 	 * The new CCS hash mode makes remapping impossible
2766 	 */
2767 	if (!is_ccs_modifier(modifier)) {
2768 		if (INTEL_GEN(dev_priv) >= 7)
2769 			return 256*1024;
2770 		else if (INTEL_GEN(dev_priv) >= 4)
2771 			return 128*1024;
2772 	}
2773 
2774 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2775 }
2776 
2777 static u32
2778 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2779 {
2780 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2781 	u32 tile_width;
2782 
2783 	if (is_surface_linear(fb, color_plane)) {
2784 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2785 							   fb->format->format,
2786 							   fb->modifier);
2787 
2788 		/*
2789 		 * To make remapping with linear generally feasible
2790 		 * we need the stride to be page aligned.
2791 		 */
2792 		if (fb->pitches[color_plane] > max_stride &&
2793 		    !is_ccs_modifier(fb->modifier))
2794 			return intel_tile_size(dev_priv);
2795 		else
2796 			return 64;
2797 	}
2798 
2799 	tile_width = intel_tile_width_bytes(fb, color_plane);
2800 	if (is_ccs_modifier(fb->modifier)) {
2801 		/*
2802 		 * Display WA #0531: skl,bxt,kbl,glk
2803 		 *
2804 		 * Render decompression and plane width > 3840
2805 		 * combined with horizontal panning requires the
2806 		 * plane stride to be a multiple of 4. We'll just
2807 		 * require the entire fb to accommodate that to avoid
2808 		 * potential runtime errors at plane configuration time.
2809 		 */
2810 		if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
2811 			tile_width *= 4;
2812 		/*
2813 		 * The main surface pitch must be padded to a multiple of four
2814 		 * tile widths.
2815 		 */
2816 		else if (INTEL_GEN(dev_priv) >= 12)
2817 			tile_width *= 4;
2818 	}
2819 	return tile_width;
2820 }
2821 
2822 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2823 {
2824 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2825 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2826 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2827 	int i;
2828 
2829 	/* We don't want to deal with remapping with cursors */
2830 	if (plane->id == PLANE_CURSOR)
2831 		return false;
2832 
2833 	/*
2834 	 * The display engine limits already match/exceed the
2835 	 * render engine limits, so not much point in remapping.
2836 	 * Would also need to deal with the fence POT alignment
2837 	 * and gen2 2KiB GTT tile size.
2838 	 */
2839 	if (INTEL_GEN(dev_priv) < 4)
2840 		return false;
2841 
2842 	/*
2843 	 * The new CCS hash mode isn't compatible with remapping as
2844 	 * the virtual address of the pages affects the compressed data.
2845 	 */
2846 	if (is_ccs_modifier(fb->modifier))
2847 		return false;
2848 
2849 	/* Linear needs a page aligned stride for remapping */
2850 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2851 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
2852 
2853 		for (i = 0; i < fb->format->num_planes; i++) {
2854 			if (fb->pitches[i] & alignment)
2855 				return false;
2856 		}
2857 	}
2858 
2859 	return true;
2860 }
2861 
2862 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2863 {
2864 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2865 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2866 	unsigned int rotation = plane_state->hw.rotation;
2867 	u32 stride, max_stride;
2868 
2869 	/*
2870 	 * No remapping for invisible planes since we don't have
2871 	 * an actual source viewport to remap.
2872 	 */
2873 	if (!plane_state->uapi.visible)
2874 		return false;
2875 
2876 	if (!intel_plane_can_remap(plane_state))
2877 		return false;
2878 
2879 	/*
2880 	 * FIXME: aux plane limits on gen9+ are
2881 	 * unclear in Bspec, for now no checking.
2882 	 */
2883 	stride = intel_fb_pitch(fb, 0, rotation);
2884 	max_stride = plane->max_stride(plane, fb->format->format,
2885 				       fb->modifier, rotation);
2886 
2887 	return stride > max_stride;
2888 }
2889 
2890 static void
2891 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2892 			       const struct drm_framebuffer *fb,
2893 			       int color_plane)
2894 {
2895 	int main_plane;
2896 
2897 	if (color_plane == 0) {
2898 		*hsub = 1;
2899 		*vsub = 1;
2900 
2901 		return;
2902 	}
2903 
2904 	/*
2905 	 * TODO: Deduct the subsampling from the char block for all CCS
2906 	 * formats and planes.
2907 	 */
2908 	if (!is_gen12_ccs_plane(fb, color_plane)) {
2909 		*hsub = fb->format->hsub;
2910 		*vsub = fb->format->vsub;
2911 
2912 		return;
2913 	}
2914 
2915 	main_plane = ccs_to_main_plane(fb, color_plane);
2916 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
2917 		drm_format_info_block_width(fb->format, main_plane);
2918 
2919 	/*
2920 	 * The min stride check in the core framebuffer_check() function
2921 	 * assumes that format->hsub applies to every plane except for the
2922 	 * first plane. That's incorrect for the CCS AUX plane of the first
2923 	 * plane, but for the above check to pass we must define the block
2924 	 * width with that subsampling applied to it. Adjust the width here
2925 	 * accordingly, so we can calculate the actual subsampling factor.
2926 	 */
2927 	if (main_plane == 0)
2928 		*hsub *= fb->format->hsub;
2929 
2930 	*vsub = 32;
2931 }
2932 static int
2933 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2934 {
2935 	struct drm_i915_private *i915 = to_i915(fb->dev);
2936 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2937 	int main_plane;
2938 	int hsub, vsub;
2939 	int tile_width, tile_height;
2940 	int ccs_x, ccs_y;
2941 	int main_x, main_y;
2942 
2943 	if (!is_ccs_plane(fb, ccs_plane))
2944 		return 0;
2945 
2946 	intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2947 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2948 
2949 	tile_width *= hsub;
2950 	tile_height *= vsub;
2951 
2952 	ccs_x = (x * hsub) % tile_width;
2953 	ccs_y = (y * vsub) % tile_height;
2954 
2955 	main_plane = ccs_to_main_plane(fb, ccs_plane);
2956 	main_x = intel_fb->normal[main_plane].x % tile_width;
2957 	main_y = intel_fb->normal[main_plane].y % tile_height;
2958 
2959 	/*
2960 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2961 	 * x/y offsets must match between CCS and the main surface.
2962 	 */
2963 	if (main_x != ccs_x || main_y != ccs_y) {
2964 		drm_dbg_kms(&i915->drm,
2965 			      "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2966 			      main_x, main_y,
2967 			      ccs_x, ccs_y,
2968 			      intel_fb->normal[main_plane].x,
2969 			      intel_fb->normal[main_plane].y,
2970 			      x, y);
2971 		return -EINVAL;
2972 	}
2973 
2974 	return 0;
2975 }
2976 
2977 static void
2978 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2979 {
2980 	int main_plane = is_ccs_plane(fb, color_plane) ?
2981 			 ccs_to_main_plane(fb, color_plane) : 0;
2982 	int main_hsub, main_vsub;
2983 	int hsub, vsub;
2984 
2985 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
2986 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
2987 	*w = fb->width / main_hsub / hsub;
2988 	*h = fb->height / main_vsub / vsub;
2989 }
2990 
2991 /*
2992  * Setup the rotated view for an FB plane and return the size the GTT mapping
2993  * requires for this view.
2994  */
2995 static u32
2996 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2997 		  u32 gtt_offset_rotated, int x, int y,
2998 		  unsigned int width, unsigned int height,
2999 		  unsigned int tile_size,
3000 		  unsigned int tile_width, unsigned int tile_height,
3001 		  struct drm_framebuffer *fb)
3002 {
3003 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3004 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
3005 	unsigned int pitch_tiles;
3006 	struct drm_rect r;
3007 
3008 	/* Y or Yf modifiers required for 90/270 rotation */
3009 	if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3010 	    fb->modifier != I915_FORMAT_MOD_Yf_TILED)
3011 		return 0;
3012 
3013 	if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
3014 		return 0;
3015 
3016 	rot_info->plane[plane] = *plane_info;
3017 
3018 	intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
3019 
3020 	/* rotate the x/y offsets to match the GTT view */
3021 	drm_rect_init(&r, x, y, width, height);
3022 	drm_rect_rotate(&r,
3023 			plane_info->width * tile_width,
3024 			plane_info->height * tile_height,
3025 			DRM_MODE_ROTATE_270);
3026 	x = r.x1;
3027 	y = r.y1;
3028 
3029 	/* rotate the tile dimensions to match the GTT view */
3030 	pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3031 	swap(tile_width, tile_height);
3032 
3033 	/*
3034 	 * We only keep the x/y offsets, so push all of the
3035 	 * gtt offset into the x/y offsets.
3036 	 */
3037 	intel_adjust_tile_offset(&x, &y,
3038 				 tile_width, tile_height,
3039 				 tile_size, pitch_tiles,
3040 				 gtt_offset_rotated * tile_size, 0);
3041 
3042 	/*
3043 	 * First pixel of the framebuffer from
3044 	 * the start of the rotated gtt mapping.
3045 	 */
3046 	intel_fb->rotated[plane].x = x;
3047 	intel_fb->rotated[plane].y = y;
3048 
3049 	return plane_info->width * plane_info->height;
3050 }
3051 
3052 static int
3053 intel_fill_fb_info(struct drm_i915_private *dev_priv,
3054 		   struct drm_framebuffer *fb)
3055 {
3056 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3057 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3058 	u32 gtt_offset_rotated = 0;
3059 	unsigned int max_size = 0;
3060 	int i, num_planes = fb->format->num_planes;
3061 	unsigned int tile_size = intel_tile_size(dev_priv);
3062 
3063 	for (i = 0; i < num_planes; i++) {
3064 		unsigned int width, height;
3065 		unsigned int cpp, size;
3066 		u32 offset;
3067 		int x, y;
3068 		int ret;
3069 
3070 		cpp = fb->format->cpp[i];
3071 		intel_fb_plane_dims(&width, &height, fb, i);
3072 
3073 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3074 		if (ret) {
3075 			drm_dbg_kms(&dev_priv->drm,
3076 				    "bad fb plane %d offset: 0x%x\n",
3077 				    i, fb->offsets[i]);
3078 			return ret;
3079 		}
3080 
3081 		ret = intel_fb_check_ccs_xy(fb, i, x, y);
3082 		if (ret)
3083 			return ret;
3084 
3085 		/*
3086 		 * The fence (if used) is aligned to the start of the object
3087 		 * so having the framebuffer wrap around across the edge of the
3088 		 * fenced region doesn't really work. We have no API to configure
3089 		 * the fence start offset within the object (nor could we probably
3090 		 * on gen2/3). So it's just easier if we just require that the
3091 		 * fb layout agrees with the fence layout. We already check that the
3092 		 * fb stride matches the fence stride elsewhere.
3093 		 */
3094 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
3095 		    (x + width) * cpp > fb->pitches[i]) {
3096 			drm_dbg_kms(&dev_priv->drm,
3097 				    "bad fb plane %d offset: 0x%x\n",
3098 				     i, fb->offsets[i]);
3099 			return -EINVAL;
3100 		}
3101 
3102 		/*
3103 		 * First pixel of the framebuffer from
3104 		 * the start of the normal gtt mapping.
3105 		 */
3106 		intel_fb->normal[i].x = x;
3107 		intel_fb->normal[i].y = y;
3108 
3109 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3110 						      fb->pitches[i],
3111 						      DRM_MODE_ROTATE_0,
3112 						      tile_size);
3113 		offset /= tile_size;
3114 
3115 		if (!is_surface_linear(fb, i)) {
3116 			struct intel_remapped_plane_info plane_info;
3117 			unsigned int tile_width, tile_height;
3118 
3119 			intel_tile_dims(fb, i, &tile_width, &tile_height);
3120 
3121 			plane_info.offset = offset;
3122 			plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3123 							 tile_width * cpp);
3124 			plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3125 			plane_info.height = DIV_ROUND_UP(y + height,
3126 							 tile_height);
3127 
3128 			/* how many tiles does this plane need */
3129 			size = plane_info.stride * plane_info.height;
3130 			/*
3131 			 * If the plane isn't horizontally tile aligned,
3132 			 * we need one more tile.
3133 			 */
3134 			if (x != 0)
3135 				size++;
3136 
3137 			gtt_offset_rotated +=
3138 				setup_fb_rotation(i, &plane_info,
3139 						  gtt_offset_rotated,
3140 						  x, y, width, height,
3141 						  tile_size,
3142 						  tile_width, tile_height,
3143 						  fb);
3144 		} else {
3145 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3146 					    x * cpp, tile_size);
3147 		}
3148 
3149 		/* how many tiles in total needed in the bo */
3150 		max_size = max(max_size, offset + size);
3151 	}
3152 
3153 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
3154 		drm_dbg_kms(&dev_priv->drm,
3155 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3156 			    mul_u32_u32(max_size, tile_size), obj->base.size);
3157 		return -EINVAL;
3158 	}
3159 
3160 	return 0;
3161 }
3162 
3163 static void
3164 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3165 {
3166 	struct drm_i915_private *dev_priv =
3167 		to_i915(plane_state->uapi.plane->dev);
3168 	struct drm_framebuffer *fb = plane_state->hw.fb;
3169 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3170 	struct intel_rotation_info *info = &plane_state->view.rotated;
3171 	unsigned int rotation = plane_state->hw.rotation;
3172 	int i, num_planes = fb->format->num_planes;
3173 	unsigned int tile_size = intel_tile_size(dev_priv);
3174 	unsigned int src_x, src_y;
3175 	unsigned int src_w, src_h;
3176 	u32 gtt_offset = 0;
3177 
3178 	memset(&plane_state->view, 0, sizeof(plane_state->view));
3179 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3180 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3181 
3182 	src_x = plane_state->uapi.src.x1 >> 16;
3183 	src_y = plane_state->uapi.src.y1 >> 16;
3184 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3185 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
3186 
3187 	drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
3188 
3189 	/* Make src coordinates relative to the viewport */
3190 	drm_rect_translate(&plane_state->uapi.src,
3191 			   -(src_x << 16), -(src_y << 16));
3192 
3193 	/* Rotate src coordinates to match rotated GTT view */
3194 	if (drm_rotation_90_or_270(rotation))
3195 		drm_rect_rotate(&plane_state->uapi.src,
3196 				src_w << 16, src_h << 16,
3197 				DRM_MODE_ROTATE_270);
3198 
3199 	for (i = 0; i < num_planes; i++) {
3200 		unsigned int hsub = i ? fb->format->hsub : 1;
3201 		unsigned int vsub = i ? fb->format->vsub : 1;
3202 		unsigned int cpp = fb->format->cpp[i];
3203 		unsigned int tile_width, tile_height;
3204 		unsigned int width, height;
3205 		unsigned int pitch_tiles;
3206 		unsigned int x, y;
3207 		u32 offset;
3208 
3209 		intel_tile_dims(fb, i, &tile_width, &tile_height);
3210 
3211 		x = src_x / hsub;
3212 		y = src_y / vsub;
3213 		width = src_w / hsub;
3214 		height = src_h / vsub;
3215 
3216 		/*
3217 		 * First pixel of the src viewport from the
3218 		 * start of the normal gtt mapping.
3219 		 */
3220 		x += intel_fb->normal[i].x;
3221 		y += intel_fb->normal[i].y;
3222 
3223 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3224 						      fb, i, fb->pitches[i],
3225 						      DRM_MODE_ROTATE_0, tile_size);
3226 		offset /= tile_size;
3227 
3228 		drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
3229 		info->plane[i].offset = offset;
3230 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3231 						     tile_width * cpp);
3232 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3233 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3234 
3235 		if (drm_rotation_90_or_270(rotation)) {
3236 			struct drm_rect r;
3237 
3238 			/* rotate the x/y offsets to match the GTT view */
3239 			drm_rect_init(&r, x, y, width, height);
3240 			drm_rect_rotate(&r,
3241 					info->plane[i].width * tile_width,
3242 					info->plane[i].height * tile_height,
3243 					DRM_MODE_ROTATE_270);
3244 			x = r.x1;
3245 			y = r.y1;
3246 
3247 			pitch_tiles = info->plane[i].height;
3248 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3249 
3250 			/* rotate the tile dimensions to match the GTT view */
3251 			swap(tile_width, tile_height);
3252 		} else {
3253 			pitch_tiles = info->plane[i].width;
3254 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3255 		}
3256 
3257 		/*
3258 		 * We only keep the x/y offsets, so push all of the
3259 		 * gtt offset into the x/y offsets.
3260 		 */
3261 		intel_adjust_tile_offset(&x, &y,
3262 					 tile_width, tile_height,
3263 					 tile_size, pitch_tiles,
3264 					 gtt_offset * tile_size, 0);
3265 
3266 		gtt_offset += info->plane[i].width * info->plane[i].height;
3267 
3268 		plane_state->color_plane[i].offset = 0;
3269 		plane_state->color_plane[i].x = x;
3270 		plane_state->color_plane[i].y = y;
3271 	}
3272 }
3273 
3274 static int
3275 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3276 {
3277 	const struct intel_framebuffer *fb =
3278 		to_intel_framebuffer(plane_state->hw.fb);
3279 	unsigned int rotation = plane_state->hw.rotation;
3280 	int i, num_planes;
3281 
3282 	if (!fb)
3283 		return 0;
3284 
3285 	num_planes = fb->base.format->num_planes;
3286 
3287 	if (intel_plane_needs_remap(plane_state)) {
3288 		intel_plane_remap_gtt(plane_state);
3289 
3290 		/*
3291 		 * Sometimes even remapping can't overcome
3292 		 * the stride limitations :( Can happen with
3293 		 * big plane sizes and suitably misaligned
3294 		 * offsets.
3295 		 */
3296 		return intel_plane_check_stride(plane_state);
3297 	}
3298 
3299 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3300 
3301 	for (i = 0; i < num_planes; i++) {
3302 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3303 		plane_state->color_plane[i].offset = 0;
3304 
3305 		if (drm_rotation_90_or_270(rotation)) {
3306 			plane_state->color_plane[i].x = fb->rotated[i].x;
3307 			plane_state->color_plane[i].y = fb->rotated[i].y;
3308 		} else {
3309 			plane_state->color_plane[i].x = fb->normal[i].x;
3310 			plane_state->color_plane[i].y = fb->normal[i].y;
3311 		}
3312 	}
3313 
3314 	/* Rotate src coordinates to match rotated GTT view */
3315 	if (drm_rotation_90_or_270(rotation))
3316 		drm_rect_rotate(&plane_state->uapi.src,
3317 				fb->base.width << 16, fb->base.height << 16,
3318 				DRM_MODE_ROTATE_270);
3319 
3320 	return intel_plane_check_stride(plane_state);
3321 }
3322 
3323 static int i9xx_format_to_fourcc(int format)
3324 {
3325 	switch (format) {
3326 	case DISPPLANE_8BPP:
3327 		return DRM_FORMAT_C8;
3328 	case DISPPLANE_BGRA555:
3329 		return DRM_FORMAT_ARGB1555;
3330 	case DISPPLANE_BGRX555:
3331 		return DRM_FORMAT_XRGB1555;
3332 	case DISPPLANE_BGRX565:
3333 		return DRM_FORMAT_RGB565;
3334 	default:
3335 	case DISPPLANE_BGRX888:
3336 		return DRM_FORMAT_XRGB8888;
3337 	case DISPPLANE_RGBX888:
3338 		return DRM_FORMAT_XBGR8888;
3339 	case DISPPLANE_BGRA888:
3340 		return DRM_FORMAT_ARGB8888;
3341 	case DISPPLANE_RGBA888:
3342 		return DRM_FORMAT_ABGR8888;
3343 	case DISPPLANE_BGRX101010:
3344 		return DRM_FORMAT_XRGB2101010;
3345 	case DISPPLANE_RGBX101010:
3346 		return DRM_FORMAT_XBGR2101010;
3347 	case DISPPLANE_BGRA101010:
3348 		return DRM_FORMAT_ARGB2101010;
3349 	case DISPPLANE_RGBA101010:
3350 		return DRM_FORMAT_ABGR2101010;
3351 	case DISPPLANE_RGBX161616:
3352 		return DRM_FORMAT_XBGR16161616F;
3353 	}
3354 }
3355 
3356 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
3357 {
3358 	switch (format) {
3359 	case PLANE_CTL_FORMAT_RGB_565:
3360 		return DRM_FORMAT_RGB565;
3361 	case PLANE_CTL_FORMAT_NV12:
3362 		return DRM_FORMAT_NV12;
3363 	case PLANE_CTL_FORMAT_XYUV:
3364 		return DRM_FORMAT_XYUV8888;
3365 	case PLANE_CTL_FORMAT_P010:
3366 		return DRM_FORMAT_P010;
3367 	case PLANE_CTL_FORMAT_P012:
3368 		return DRM_FORMAT_P012;
3369 	case PLANE_CTL_FORMAT_P016:
3370 		return DRM_FORMAT_P016;
3371 	case PLANE_CTL_FORMAT_Y210:
3372 		return DRM_FORMAT_Y210;
3373 	case PLANE_CTL_FORMAT_Y212:
3374 		return DRM_FORMAT_Y212;
3375 	case PLANE_CTL_FORMAT_Y216:
3376 		return DRM_FORMAT_Y216;
3377 	case PLANE_CTL_FORMAT_Y410:
3378 		return DRM_FORMAT_XVYU2101010;
3379 	case PLANE_CTL_FORMAT_Y412:
3380 		return DRM_FORMAT_XVYU12_16161616;
3381 	case PLANE_CTL_FORMAT_Y416:
3382 		return DRM_FORMAT_XVYU16161616;
3383 	default:
3384 	case PLANE_CTL_FORMAT_XRGB_8888:
3385 		if (rgb_order) {
3386 			if (alpha)
3387 				return DRM_FORMAT_ABGR8888;
3388 			else
3389 				return DRM_FORMAT_XBGR8888;
3390 		} else {
3391 			if (alpha)
3392 				return DRM_FORMAT_ARGB8888;
3393 			else
3394 				return DRM_FORMAT_XRGB8888;
3395 		}
3396 	case PLANE_CTL_FORMAT_XRGB_2101010:
3397 		if (rgb_order) {
3398 			if (alpha)
3399 				return DRM_FORMAT_ABGR2101010;
3400 			else
3401 				return DRM_FORMAT_XBGR2101010;
3402 		} else {
3403 			if (alpha)
3404 				return DRM_FORMAT_ARGB2101010;
3405 			else
3406 				return DRM_FORMAT_XRGB2101010;
3407 		}
3408 	case PLANE_CTL_FORMAT_XRGB_16161616F:
3409 		if (rgb_order) {
3410 			if (alpha)
3411 				return DRM_FORMAT_ABGR16161616F;
3412 			else
3413 				return DRM_FORMAT_XBGR16161616F;
3414 		} else {
3415 			if (alpha)
3416 				return DRM_FORMAT_ARGB16161616F;
3417 			else
3418 				return DRM_FORMAT_XRGB16161616F;
3419 		}
3420 	}
3421 }
3422 
3423 static struct i915_vma *
3424 initial_plane_vma(struct drm_i915_private *i915,
3425 		  struct intel_initial_plane_config *plane_config)
3426 {
3427 	struct drm_i915_gem_object *obj;
3428 	struct i915_vma *vma;
3429 	u32 base, size;
3430 
3431 	if (plane_config->size == 0)
3432 		return NULL;
3433 
3434 	base = round_down(plane_config->base,
3435 			  I915_GTT_MIN_ALIGNMENT);
3436 	size = round_up(plane_config->base + plane_config->size,
3437 			I915_GTT_MIN_ALIGNMENT);
3438 	size -= base;
3439 
3440 	/*
3441 	 * If the FB is too big, just don't use it since fbdev is not very
3442 	 * important and we should probably use that space with FBC or other
3443 	 * features.
3444 	 */
3445 	if (size * 2 > i915->stolen_usable_size)
3446 		return NULL;
3447 
3448 	obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3449 	if (IS_ERR(obj))
3450 		return NULL;
3451 
3452 	/*
3453 	 * Mark it WT ahead of time to avoid changing the
3454 	 * cache_level during fbdev initialization. The
3455 	 * unbind there would get stuck waiting for rcu.
3456 	 */
3457 	i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
3458 					    I915_CACHE_WT : I915_CACHE_NONE);
3459 
3460 	switch (plane_config->tiling) {
3461 	case I915_TILING_NONE:
3462 		break;
3463 	case I915_TILING_X:
3464 	case I915_TILING_Y:
3465 		obj->tiling_and_stride =
3466 			plane_config->fb->base.pitches[0] |
3467 			plane_config->tiling;
3468 		break;
3469 	default:
3470 		MISSING_CASE(plane_config->tiling);
3471 		goto err_obj;
3472 	}
3473 
3474 	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3475 	if (IS_ERR(vma))
3476 		goto err_obj;
3477 
3478 	if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
3479 		goto err_obj;
3480 
3481 	if (i915_gem_object_is_tiled(obj) &&
3482 	    !i915_vma_is_map_and_fenceable(vma))
3483 		goto err_obj;
3484 
3485 	return vma;
3486 
3487 err_obj:
3488 	i915_gem_object_put(obj);
3489 	return NULL;
3490 }
3491 
3492 static bool
3493 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3494 			      struct intel_initial_plane_config *plane_config)
3495 {
3496 	struct drm_device *dev = crtc->base.dev;
3497 	struct drm_i915_private *dev_priv = to_i915(dev);
3498 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3499 	struct drm_framebuffer *fb = &plane_config->fb->base;
3500 	struct i915_vma *vma;
3501 
3502 	switch (fb->modifier) {
3503 	case DRM_FORMAT_MOD_LINEAR:
3504 	case I915_FORMAT_MOD_X_TILED:
3505 	case I915_FORMAT_MOD_Y_TILED:
3506 		break;
3507 	default:
3508 		drm_dbg(&dev_priv->drm,
3509 			"Unsupported modifier for initial FB: 0x%llx\n",
3510 			fb->modifier);
3511 		return false;
3512 	}
3513 
3514 	vma = initial_plane_vma(dev_priv, plane_config);
3515 	if (!vma)
3516 		return false;
3517 
3518 	mode_cmd.pixel_format = fb->format->format;
3519 	mode_cmd.width = fb->width;
3520 	mode_cmd.height = fb->height;
3521 	mode_cmd.pitches[0] = fb->pitches[0];
3522 	mode_cmd.modifier[0] = fb->modifier;
3523 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3524 
3525 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
3526 				   vma->obj, &mode_cmd)) {
3527 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
3528 		goto err_vma;
3529 	}
3530 
3531 	plane_config->vma = vma;
3532 	return true;
3533 
3534 err_vma:
3535 	i915_vma_put(vma);
3536 	return false;
3537 }
3538 
3539 static void
3540 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3541 			struct intel_plane_state *plane_state,
3542 			bool visible)
3543 {
3544 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3545 
3546 	plane_state->uapi.visible = visible;
3547 
3548 	if (visible)
3549 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
3550 	else
3551 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
3552 }
3553 
3554 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3555 {
3556 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3557 	struct drm_plane *plane;
3558 
3559 	/*
3560 	 * Active_planes aliases if multiple "primary" or cursor planes
3561 	 * have been used on the same (or wrong) pipe. plane_mask uses
3562 	 * unique ids, hence we can use that to reconstruct active_planes.
3563 	 */
3564 	crtc_state->active_planes = 0;
3565 
3566 	drm_for_each_plane_mask(plane, &dev_priv->drm,
3567 				crtc_state->uapi.plane_mask)
3568 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3569 }
3570 
3571 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3572 					 struct intel_plane *plane)
3573 {
3574 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3575 	struct intel_crtc_state *crtc_state =
3576 		to_intel_crtc_state(crtc->base.state);
3577 	struct intel_plane_state *plane_state =
3578 		to_intel_plane_state(plane->base.state);
3579 
3580 	drm_dbg_kms(&dev_priv->drm,
3581 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3582 		    plane->base.base.id, plane->base.name,
3583 		    crtc->base.base.id, crtc->base.name);
3584 
3585 	intel_set_plane_visible(crtc_state, plane_state, false);
3586 	fixup_active_planes(crtc_state);
3587 	crtc_state->data_rate[plane->id] = 0;
3588 	crtc_state->min_cdclk[plane->id] = 0;
3589 
3590 	if (plane->id == PLANE_PRIMARY)
3591 		hsw_disable_ips(crtc_state);
3592 
3593 	/*
3594 	 * Vblank time updates from the shadow to live plane control register
3595 	 * are blocked if the memory self-refresh mode is active at that
3596 	 * moment. So to make sure the plane gets truly disabled, disable
3597 	 * first the self-refresh mode. The self-refresh enable bit in turn
3598 	 * will be checked/applied by the HW only at the next frame start
3599 	 * event which is after the vblank start event, so we need to have a
3600 	 * wait-for-vblank between disabling the plane and the pipe.
3601 	 */
3602 	if (HAS_GMCH(dev_priv) &&
3603 	    intel_set_memory_cxsr(dev_priv, false))
3604 		intel_wait_for_vblank(dev_priv, crtc->pipe);
3605 
3606 	/*
3607 	 * Gen2 reports pipe underruns whenever all planes are disabled.
3608 	 * So disable underrun reporting before all the planes get disabled.
3609 	 */
3610 	if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3611 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3612 
3613 	intel_disable_plane(plane, crtc_state);
3614 }
3615 
3616 static struct intel_frontbuffer *
3617 to_intel_frontbuffer(struct drm_framebuffer *fb)
3618 {
3619 	return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3620 }
3621 
3622 static void
3623 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3624 			     struct intel_initial_plane_config *plane_config)
3625 {
3626 	struct drm_device *dev = intel_crtc->base.dev;
3627 	struct drm_i915_private *dev_priv = to_i915(dev);
3628 	struct drm_crtc *c;
3629 	struct drm_plane *primary = intel_crtc->base.primary;
3630 	struct drm_plane_state *plane_state = primary->state;
3631 	struct intel_plane *intel_plane = to_intel_plane(primary);
3632 	struct intel_plane_state *intel_state =
3633 		to_intel_plane_state(plane_state);
3634 	struct drm_framebuffer *fb;
3635 	struct i915_vma *vma;
3636 
3637 	if (!plane_config->fb)
3638 		return;
3639 
3640 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3641 		fb = &plane_config->fb->base;
3642 		vma = plane_config->vma;
3643 		goto valid_fb;
3644 	}
3645 
3646 	/*
3647 	 * Failed to alloc the obj, check to see if we should share
3648 	 * an fb with another CRTC instead
3649 	 */
3650 	for_each_crtc(dev, c) {
3651 		struct intel_plane_state *state;
3652 
3653 		if (c == &intel_crtc->base)
3654 			continue;
3655 
3656 		if (!to_intel_crtc(c)->active)
3657 			continue;
3658 
3659 		state = to_intel_plane_state(c->primary->state);
3660 		if (!state->vma)
3661 			continue;
3662 
3663 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
3664 			fb = state->hw.fb;
3665 			vma = state->vma;
3666 			goto valid_fb;
3667 		}
3668 	}
3669 
3670 	/*
3671 	 * We've failed to reconstruct the BIOS FB.  Current display state
3672 	 * indicates that the primary plane is visible, but has a NULL FB,
3673 	 * which will lead to problems later if we don't fix it up.  The
3674 	 * simplest solution is to just disable the primary plane now and
3675 	 * pretend the BIOS never had it enabled.
3676 	 */
3677 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
3678 
3679 	return;
3680 
3681 valid_fb:
3682 	intel_state->hw.rotation = plane_config->rotation;
3683 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
3684 				intel_state->hw.rotation);
3685 	intel_state->color_plane[0].stride =
3686 		intel_fb_pitch(fb, 0, intel_state->hw.rotation);
3687 
3688 	__i915_vma_pin(vma);
3689 	intel_state->vma = i915_vma_get(vma);
3690 	if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3691 		if (vma->fence)
3692 			intel_state->flags |= PLANE_HAS_FENCE;
3693 
3694 	plane_state->src_x = 0;
3695 	plane_state->src_y = 0;
3696 	plane_state->src_w = fb->width << 16;
3697 	plane_state->src_h = fb->height << 16;
3698 
3699 	plane_state->crtc_x = 0;
3700 	plane_state->crtc_y = 0;
3701 	plane_state->crtc_w = fb->width;
3702 	plane_state->crtc_h = fb->height;
3703 
3704 	intel_state->uapi.src = drm_plane_state_src(plane_state);
3705 	intel_state->uapi.dst = drm_plane_state_dest(plane_state);
3706 
3707 	if (plane_config->tiling)
3708 		dev_priv->preserve_bios_swizzle = true;
3709 
3710 	plane_state->fb = fb;
3711 	drm_framebuffer_get(fb);
3712 
3713 	plane_state->crtc = &intel_crtc->base;
3714 	intel_plane_copy_uapi_to_hw_state(intel_state, intel_state);
3715 
3716 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3717 
3718 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3719 		  &to_intel_frontbuffer(fb)->bits);
3720 }
3721 
3722 static int skl_max_plane_width(const struct drm_framebuffer *fb,
3723 			       int color_plane,
3724 			       unsigned int rotation)
3725 {
3726 	int cpp = fb->format->cpp[color_plane];
3727 
3728 	switch (fb->modifier) {
3729 	case DRM_FORMAT_MOD_LINEAR:
3730 	case I915_FORMAT_MOD_X_TILED:
3731 		/*
3732 		 * Validated limit is 4k, but has 5k should
3733 		 * work apart from the following features:
3734 		 * - Ytile (already limited to 4k)
3735 		 * - FP16 (already limited to 4k)
3736 		 * - render compression (already limited to 4k)
3737 		 * - KVMR sprite and cursor (don't care)
3738 		 * - horizontal panning (TODO verify this)
3739 		 * - pipe and plane scaling (TODO verify this)
3740 		 */
3741 		if (cpp == 8)
3742 			return 4096;
3743 		else
3744 			return 5120;
3745 	case I915_FORMAT_MOD_Y_TILED_CCS:
3746 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3747 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
3748 		/* FIXME AUX plane? */
3749 	case I915_FORMAT_MOD_Y_TILED:
3750 	case I915_FORMAT_MOD_Yf_TILED:
3751 		if (cpp == 8)
3752 			return 2048;
3753 		else
3754 			return 4096;
3755 	default:
3756 		MISSING_CASE(fb->modifier);
3757 		return 2048;
3758 	}
3759 }
3760 
3761 static int glk_max_plane_width(const struct drm_framebuffer *fb,
3762 			       int color_plane,
3763 			       unsigned int rotation)
3764 {
3765 	int cpp = fb->format->cpp[color_plane];
3766 
3767 	switch (fb->modifier) {
3768 	case DRM_FORMAT_MOD_LINEAR:
3769 	case I915_FORMAT_MOD_X_TILED:
3770 		if (cpp == 8)
3771 			return 4096;
3772 		else
3773 			return 5120;
3774 	case I915_FORMAT_MOD_Y_TILED_CCS:
3775 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3776 		/* FIXME AUX plane? */
3777 	case I915_FORMAT_MOD_Y_TILED:
3778 	case I915_FORMAT_MOD_Yf_TILED:
3779 		if (cpp == 8)
3780 			return 2048;
3781 		else
3782 			return 5120;
3783 	default:
3784 		MISSING_CASE(fb->modifier);
3785 		return 2048;
3786 	}
3787 }
3788 
3789 static int icl_min_plane_width(const struct drm_framebuffer *fb)
3790 {
3791 	/* Wa_14011264657, Wa_14011050563: gen11+ */
3792 	switch (fb->format->format) {
3793 	case DRM_FORMAT_C8:
3794 		return 18;
3795 	case DRM_FORMAT_RGB565:
3796 		return 10;
3797 	case DRM_FORMAT_XRGB8888:
3798 	case DRM_FORMAT_XBGR8888:
3799 	case DRM_FORMAT_ARGB8888:
3800 	case DRM_FORMAT_ABGR8888:
3801 	case DRM_FORMAT_XRGB2101010:
3802 	case DRM_FORMAT_XBGR2101010:
3803 	case DRM_FORMAT_ARGB2101010:
3804 	case DRM_FORMAT_ABGR2101010:
3805 	case DRM_FORMAT_XVYU2101010:
3806 	case DRM_FORMAT_Y212:
3807 	case DRM_FORMAT_Y216:
3808 		return 6;
3809 	case DRM_FORMAT_NV12:
3810 		return 20;
3811 	case DRM_FORMAT_P010:
3812 	case DRM_FORMAT_P012:
3813 	case DRM_FORMAT_P016:
3814 		return 12;
3815 	case DRM_FORMAT_XRGB16161616F:
3816 	case DRM_FORMAT_XBGR16161616F:
3817 	case DRM_FORMAT_ARGB16161616F:
3818 	case DRM_FORMAT_ABGR16161616F:
3819 	case DRM_FORMAT_XVYU12_16161616:
3820 	case DRM_FORMAT_XVYU16161616:
3821 		return 4;
3822 	default:
3823 		return 1;
3824 	}
3825 }
3826 
3827 static int icl_max_plane_width(const struct drm_framebuffer *fb,
3828 			       int color_plane,
3829 			       unsigned int rotation)
3830 {
3831 	return 5120;
3832 }
3833 
3834 static int skl_max_plane_height(void)
3835 {
3836 	return 4096;
3837 }
3838 
3839 static int icl_max_plane_height(void)
3840 {
3841 	return 4320;
3842 }
3843 
3844 static bool
3845 skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3846 			       int main_x, int main_y, u32 main_offset,
3847 			       int ccs_plane)
3848 {
3849 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3850 	int aux_x = plane_state->color_plane[ccs_plane].x;
3851 	int aux_y = plane_state->color_plane[ccs_plane].y;
3852 	u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3853 	u32 alignment = intel_surf_alignment(fb, ccs_plane);
3854 	int hsub;
3855 	int vsub;
3856 
3857 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3858 	while (aux_offset >= main_offset && aux_y <= main_y) {
3859 		int x, y;
3860 
3861 		if (aux_x == main_x && aux_y == main_y)
3862 			break;
3863 
3864 		if (aux_offset == 0)
3865 			break;
3866 
3867 		x = aux_x / hsub;
3868 		y = aux_y / vsub;
3869 		aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3870 							       plane_state,
3871 							       ccs_plane,
3872 							       aux_offset,
3873 							       aux_offset -
3874 								alignment);
3875 		aux_x = x * hsub + aux_x % hsub;
3876 		aux_y = y * vsub + aux_y % vsub;
3877 	}
3878 
3879 	if (aux_x != main_x || aux_y != main_y)
3880 		return false;
3881 
3882 	plane_state->color_plane[ccs_plane].offset = aux_offset;
3883 	plane_state->color_plane[ccs_plane].x = aux_x;
3884 	plane_state->color_plane[ccs_plane].y = aux_y;
3885 
3886 	return true;
3887 }
3888 
3889 unsigned int
3890 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
3891 {
3892 	int x = 0, y = 0;
3893 
3894 	intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3895 					  plane_state->color_plane[0].offset, 0);
3896 
3897 	return y;
3898 }
3899 
3900 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3901 {
3902 	struct drm_i915_private *dev_priv = to_i915(plane_state->uapi.plane->dev);
3903 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3904 	unsigned int rotation = plane_state->hw.rotation;
3905 	int x = plane_state->uapi.src.x1 >> 16;
3906 	int y = plane_state->uapi.src.y1 >> 16;
3907 	int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3908 	int h = drm_rect_height(&plane_state->uapi.src) >> 16;
3909 	int max_width, min_width, max_height;
3910 	u32 alignment, offset;
3911 	int aux_plane = intel_main_to_aux_plane(fb, 0);
3912 	u32 aux_offset = plane_state->color_plane[aux_plane].offset;
3913 
3914 	if (INTEL_GEN(dev_priv) >= 11) {
3915 		max_width = icl_max_plane_width(fb, 0, rotation);
3916 		min_width = icl_min_plane_width(fb);
3917 	} else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
3918 		max_width = glk_max_plane_width(fb, 0, rotation);
3919 		min_width = 1;
3920 	} else {
3921 		max_width = skl_max_plane_width(fb, 0, rotation);
3922 		min_width = 1;
3923 	}
3924 
3925 	if (INTEL_GEN(dev_priv) >= 11)
3926 		max_height = icl_max_plane_height();
3927 	else
3928 		max_height = skl_max_plane_height();
3929 
3930 	if (w > max_width || w < min_width || h > max_height) {
3931 		drm_dbg_kms(&dev_priv->drm,
3932 			    "requested Y/RGB source size %dx%d outside limits (min: %dx1 max: %dx%d)\n",
3933 			    w, h, min_width, max_width, max_height);
3934 		return -EINVAL;
3935 	}
3936 
3937 	intel_add_fb_offsets(&x, &y, plane_state, 0);
3938 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3939 	alignment = intel_surf_alignment(fb, 0);
3940 	if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
3941 		return -EINVAL;
3942 
3943 	/*
3944 	 * AUX surface offset is specified as the distance from the
3945 	 * main surface offset, and it must be non-negative. Make
3946 	 * sure that is what we will get.
3947 	 */
3948 	if (aux_plane && offset > aux_offset)
3949 		offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3950 							   offset, aux_offset & ~(alignment - 1));
3951 
3952 	/*
3953 	 * When using an X-tiled surface, the plane blows up
3954 	 * if the x offset + width exceed the stride.
3955 	 *
3956 	 * TODO: linear and Y-tiled seem fine, Yf untested,
3957 	 */
3958 	if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3959 		int cpp = fb->format->cpp[0];
3960 
3961 		while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3962 			if (offset == 0) {
3963 				drm_dbg_kms(&dev_priv->drm,
3964 					    "Unable to find suitable display surface offset due to X-tiling\n");
3965 				return -EINVAL;
3966 			}
3967 
3968 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3969 								   offset, offset - alignment);
3970 		}
3971 	}
3972 
3973 	/*
3974 	 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3975 	 * they match with the main surface x/y offsets.
3976 	 */
3977 	if (is_ccs_modifier(fb->modifier)) {
3978 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3979 						       offset, aux_plane)) {
3980 			if (offset == 0)
3981 				break;
3982 
3983 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3984 								   offset, offset - alignment);
3985 		}
3986 
3987 		if (x != plane_state->color_plane[aux_plane].x ||
3988 		    y != plane_state->color_plane[aux_plane].y) {
3989 			drm_dbg_kms(&dev_priv->drm,
3990 				    "Unable to find suitable display surface offset due to CCS\n");
3991 			return -EINVAL;
3992 		}
3993 	}
3994 
3995 	plane_state->color_plane[0].offset = offset;
3996 	plane_state->color_plane[0].x = x;
3997 	plane_state->color_plane[0].y = y;
3998 
3999 	/*
4000 	 * Put the final coordinates back so that the src
4001 	 * coordinate checks will see the right values.
4002 	 */
4003 	drm_rect_translate_to(&plane_state->uapi.src,
4004 			      x << 16, y << 16);
4005 
4006 	return 0;
4007 }
4008 
4009 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
4010 {
4011 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
4012 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4013 	unsigned int rotation = plane_state->hw.rotation;
4014 	int uv_plane = 1;
4015 	int max_width = skl_max_plane_width(fb, uv_plane, rotation);
4016 	int max_height = 4096;
4017 	int x = plane_state->uapi.src.x1 >> 17;
4018 	int y = plane_state->uapi.src.y1 >> 17;
4019 	int w = drm_rect_width(&plane_state->uapi.src) >> 17;
4020 	int h = drm_rect_height(&plane_state->uapi.src) >> 17;
4021 	u32 offset;
4022 
4023 	intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
4024 	offset = intel_plane_compute_aligned_offset(&x, &y,
4025 						    plane_state, uv_plane);
4026 
4027 	/* FIXME not quite sure how/if these apply to the chroma plane */
4028 	if (w > max_width || h > max_height) {
4029 		drm_dbg_kms(&i915->drm,
4030 			    "CbCr source size %dx%d too big (limit %dx%d)\n",
4031 			    w, h, max_width, max_height);
4032 		return -EINVAL;
4033 	}
4034 
4035 	if (is_ccs_modifier(fb->modifier)) {
4036 		int ccs_plane = main_to_ccs_plane(fb, uv_plane);
4037 		u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
4038 		u32 alignment = intel_surf_alignment(fb, uv_plane);
4039 
4040 		if (offset > aux_offset)
4041 			offset = intel_plane_adjust_aligned_offset(&x, &y,
4042 								   plane_state,
4043 								   uv_plane,
4044 								   offset,
4045 								   aux_offset & ~(alignment - 1));
4046 
4047 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
4048 						       offset, ccs_plane)) {
4049 			if (offset == 0)
4050 				break;
4051 
4052 			offset = intel_plane_adjust_aligned_offset(&x, &y,
4053 								   plane_state,
4054 								   uv_plane,
4055 								   offset, offset - alignment);
4056 		}
4057 
4058 		if (x != plane_state->color_plane[ccs_plane].x ||
4059 		    y != plane_state->color_plane[ccs_plane].y) {
4060 			drm_dbg_kms(&i915->drm,
4061 				    "Unable to find suitable display surface offset due to CCS\n");
4062 			return -EINVAL;
4063 		}
4064 	}
4065 
4066 	plane_state->color_plane[uv_plane].offset = offset;
4067 	plane_state->color_plane[uv_plane].x = x;
4068 	plane_state->color_plane[uv_plane].y = y;
4069 
4070 	return 0;
4071 }
4072 
4073 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
4074 {
4075 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4076 	int src_x = plane_state->uapi.src.x1 >> 16;
4077 	int src_y = plane_state->uapi.src.y1 >> 16;
4078 	u32 offset;
4079 	int ccs_plane;
4080 
4081 	for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
4082 		int main_hsub, main_vsub;
4083 		int hsub, vsub;
4084 		int x, y;
4085 
4086 		if (!is_ccs_plane(fb, ccs_plane))
4087 			continue;
4088 
4089 		intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
4090 					       ccs_to_main_plane(fb, ccs_plane));
4091 		intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
4092 
4093 		hsub *= main_hsub;
4094 		vsub *= main_vsub;
4095 		x = src_x / hsub;
4096 		y = src_y / vsub;
4097 
4098 		intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
4099 
4100 		offset = intel_plane_compute_aligned_offset(&x, &y,
4101 							    plane_state,
4102 							    ccs_plane);
4103 
4104 		plane_state->color_plane[ccs_plane].offset = offset;
4105 		plane_state->color_plane[ccs_plane].x = (x * hsub +
4106 							 src_x % hsub) /
4107 							main_hsub;
4108 		plane_state->color_plane[ccs_plane].y = (y * vsub +
4109 							 src_y % vsub) /
4110 							main_vsub;
4111 	}
4112 
4113 	return 0;
4114 }
4115 
4116 int skl_check_plane_surface(struct intel_plane_state *plane_state)
4117 {
4118 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4119 	int ret, i;
4120 
4121 	ret = intel_plane_compute_gtt(plane_state);
4122 	if (ret)
4123 		return ret;
4124 
4125 	if (!plane_state->uapi.visible)
4126 		return 0;
4127 
4128 	/*
4129 	 * Handle the AUX surface first since the main surface setup depends on
4130 	 * it.
4131 	 */
4132 	if (is_ccs_modifier(fb->modifier)) {
4133 		ret = skl_check_ccs_aux_surface(plane_state);
4134 		if (ret)
4135 			return ret;
4136 	}
4137 
4138 	if (intel_format_info_is_yuv_semiplanar(fb->format,
4139 						fb->modifier)) {
4140 		ret = skl_check_nv12_aux_surface(plane_state);
4141 		if (ret)
4142 			return ret;
4143 	}
4144 
4145 	for (i = fb->format->num_planes; i < ARRAY_SIZE(plane_state->color_plane); i++) {
4146 		plane_state->color_plane[i].offset = 0;
4147 		plane_state->color_plane[i].x = 0;
4148 		plane_state->color_plane[i].y = 0;
4149 	}
4150 
4151 	ret = skl_check_main_surface(plane_state);
4152 	if (ret)
4153 		return ret;
4154 
4155 	return 0;
4156 }
4157 
4158 static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4159 			     const struct intel_plane_state *plane_state,
4160 			     unsigned int *num, unsigned int *den)
4161 {
4162 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4163 	unsigned int cpp = fb->format->cpp[0];
4164 
4165 	/*
4166 	 * g4x bspec says 64bpp pixel rate can't exceed 80%
4167 	 * of cdclk when the sprite plane is enabled on the
4168 	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
4169 	 * never allowed to exceed 80% of cdclk. Let's just go
4170 	 * with the ilk/snb limit always.
4171 	 */
4172 	if (cpp == 8) {
4173 		*num = 10;
4174 		*den = 8;
4175 	} else {
4176 		*num = 1;
4177 		*den = 1;
4178 	}
4179 }
4180 
4181 static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4182 				const struct intel_plane_state *plane_state)
4183 {
4184 	unsigned int pixel_rate;
4185 	unsigned int num, den;
4186 
4187 	/*
4188 	 * Note that crtc_state->pixel_rate accounts for both
4189 	 * horizontal and vertical panel fitter downscaling factors.
4190 	 * Pre-HSW bspec tells us to only consider the horizontal
4191 	 * downscaling factor here. We ignore that and just consider
4192 	 * both for simplicity.
4193 	 */
4194 	pixel_rate = crtc_state->pixel_rate;
4195 
4196 	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4197 
4198 	/* two pixels per clock with double wide pipe */
4199 	if (crtc_state->double_wide)
4200 		den *= 2;
4201 
4202 	return DIV_ROUND_UP(pixel_rate * num, den);
4203 }
4204 
4205 unsigned int
4206 i9xx_plane_max_stride(struct intel_plane *plane,
4207 		      u32 pixel_format, u64 modifier,
4208 		      unsigned int rotation)
4209 {
4210 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4211 
4212 	if (!HAS_GMCH(dev_priv)) {
4213 		return 32*1024;
4214 	} else if (INTEL_GEN(dev_priv) >= 4) {
4215 		if (modifier == I915_FORMAT_MOD_X_TILED)
4216 			return 16*1024;
4217 		else
4218 			return 32*1024;
4219 	} else if (INTEL_GEN(dev_priv) >= 3) {
4220 		if (modifier == I915_FORMAT_MOD_X_TILED)
4221 			return 8*1024;
4222 		else
4223 			return 16*1024;
4224 	} else {
4225 		if (plane->i9xx_plane == PLANE_C)
4226 			return 4*1024;
4227 		else
4228 			return 8*1024;
4229 	}
4230 }
4231 
4232 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4233 {
4234 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4235 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4236 	u32 dspcntr = 0;
4237 
4238 	if (crtc_state->gamma_enable)
4239 		dspcntr |= DISPPLANE_GAMMA_ENABLE;
4240 
4241 	if (crtc_state->csc_enable)
4242 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4243 
4244 	if (INTEL_GEN(dev_priv) < 5)
4245 		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4246 
4247 	return dspcntr;
4248 }
4249 
4250 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4251 			  const struct intel_plane_state *plane_state)
4252 {
4253 	struct drm_i915_private *dev_priv =
4254 		to_i915(plane_state->uapi.plane->dev);
4255 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4256 	unsigned int rotation = plane_state->hw.rotation;
4257 	u32 dspcntr;
4258 
4259 	dspcntr = DISPLAY_PLANE_ENABLE;
4260 
4261 	if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4262 	    IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
4263 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
4264 
4265 	switch (fb->format->format) {
4266 	case DRM_FORMAT_C8:
4267 		dspcntr |= DISPPLANE_8BPP;
4268 		break;
4269 	case DRM_FORMAT_XRGB1555:
4270 		dspcntr |= DISPPLANE_BGRX555;
4271 		break;
4272 	case DRM_FORMAT_ARGB1555:
4273 		dspcntr |= DISPPLANE_BGRA555;
4274 		break;
4275 	case DRM_FORMAT_RGB565:
4276 		dspcntr |= DISPPLANE_BGRX565;
4277 		break;
4278 	case DRM_FORMAT_XRGB8888:
4279 		dspcntr |= DISPPLANE_BGRX888;
4280 		break;
4281 	case DRM_FORMAT_XBGR8888:
4282 		dspcntr |= DISPPLANE_RGBX888;
4283 		break;
4284 	case DRM_FORMAT_ARGB8888:
4285 		dspcntr |= DISPPLANE_BGRA888;
4286 		break;
4287 	case DRM_FORMAT_ABGR8888:
4288 		dspcntr |= DISPPLANE_RGBA888;
4289 		break;
4290 	case DRM_FORMAT_XRGB2101010:
4291 		dspcntr |= DISPPLANE_BGRX101010;
4292 		break;
4293 	case DRM_FORMAT_XBGR2101010:
4294 		dspcntr |= DISPPLANE_RGBX101010;
4295 		break;
4296 	case DRM_FORMAT_ARGB2101010:
4297 		dspcntr |= DISPPLANE_BGRA101010;
4298 		break;
4299 	case DRM_FORMAT_ABGR2101010:
4300 		dspcntr |= DISPPLANE_RGBA101010;
4301 		break;
4302 	case DRM_FORMAT_XBGR16161616F:
4303 		dspcntr |= DISPPLANE_RGBX161616;
4304 		break;
4305 	default:
4306 		MISSING_CASE(fb->format->format);
4307 		return 0;
4308 	}
4309 
4310 	if (INTEL_GEN(dev_priv) >= 4 &&
4311 	    fb->modifier == I915_FORMAT_MOD_X_TILED)
4312 		dspcntr |= DISPPLANE_TILED;
4313 
4314 	if (rotation & DRM_MODE_ROTATE_180)
4315 		dspcntr |= DISPPLANE_ROTATE_180;
4316 
4317 	if (rotation & DRM_MODE_REFLECT_X)
4318 		dspcntr |= DISPPLANE_MIRROR;
4319 
4320 	return dspcntr;
4321 }
4322 
4323 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
4324 {
4325 	struct drm_i915_private *dev_priv =
4326 		to_i915(plane_state->uapi.plane->dev);
4327 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4328 	int src_x, src_y, src_w;
4329 	u32 offset;
4330 	int ret;
4331 
4332 	ret = intel_plane_compute_gtt(plane_state);
4333 	if (ret)
4334 		return ret;
4335 
4336 	if (!plane_state->uapi.visible)
4337 		return 0;
4338 
4339 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4340 	src_x = plane_state->uapi.src.x1 >> 16;
4341 	src_y = plane_state->uapi.src.y1 >> 16;
4342 
4343 	/* Undocumented hardware limit on i965/g4x/vlv/chv */
4344 	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4345 		return -EINVAL;
4346 
4347 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
4348 
4349 	if (INTEL_GEN(dev_priv) >= 4)
4350 		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4351 							    plane_state, 0);
4352 	else
4353 		offset = 0;
4354 
4355 	/*
4356 	 * Put the final coordinates back so that the src
4357 	 * coordinate checks will see the right values.
4358 	 */
4359 	drm_rect_translate_to(&plane_state->uapi.src,
4360 			      src_x << 16, src_y << 16);
4361 
4362 	/* HSW/BDW do this automagically in hardware */
4363 	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
4364 		unsigned int rotation = plane_state->hw.rotation;
4365 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4366 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4367 
4368 		if (rotation & DRM_MODE_ROTATE_180) {
4369 			src_x += src_w - 1;
4370 			src_y += src_h - 1;
4371 		} else if (rotation & DRM_MODE_REFLECT_X) {
4372 			src_x += src_w - 1;
4373 		}
4374 	}
4375 
4376 	plane_state->color_plane[0].offset = offset;
4377 	plane_state->color_plane[0].x = src_x;
4378 	plane_state->color_plane[0].y = src_y;
4379 
4380 	return 0;
4381 }
4382 
4383 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4384 {
4385 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4386 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4387 
4388 	if (IS_CHERRYVIEW(dev_priv))
4389 		return i9xx_plane == PLANE_B;
4390 	else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4391 		return false;
4392 	else if (IS_GEN(dev_priv, 4))
4393 		return i9xx_plane == PLANE_C;
4394 	else
4395 		return i9xx_plane == PLANE_B ||
4396 			i9xx_plane == PLANE_C;
4397 }
4398 
4399 static int
4400 i9xx_plane_check(struct intel_crtc_state *crtc_state,
4401 		 struct intel_plane_state *plane_state)
4402 {
4403 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4404 	int ret;
4405 
4406 	ret = chv_plane_check_rotation(plane_state);
4407 	if (ret)
4408 		return ret;
4409 
4410 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
4411 						  &crtc_state->uapi,
4412 						  DRM_PLANE_HELPER_NO_SCALING,
4413 						  DRM_PLANE_HELPER_NO_SCALING,
4414 						  i9xx_plane_has_windowing(plane),
4415 						  true);
4416 	if (ret)
4417 		return ret;
4418 
4419 	ret = i9xx_check_plane_surface(plane_state);
4420 	if (ret)
4421 		return ret;
4422 
4423 	if (!plane_state->uapi.visible)
4424 		return 0;
4425 
4426 	ret = intel_plane_check_src_coordinates(plane_state);
4427 	if (ret)
4428 		return ret;
4429 
4430 	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4431 
4432 	return 0;
4433 }
4434 
4435 static void i9xx_update_plane(struct intel_plane *plane,
4436 			      const struct intel_crtc_state *crtc_state,
4437 			      const struct intel_plane_state *plane_state)
4438 {
4439 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4440 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4441 	u32 linear_offset;
4442 	int x = plane_state->color_plane[0].x;
4443 	int y = plane_state->color_plane[0].y;
4444 	int crtc_x = plane_state->uapi.dst.x1;
4445 	int crtc_y = plane_state->uapi.dst.y1;
4446 	int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4447 	int crtc_h = drm_rect_height(&plane_state->uapi.dst);
4448 	unsigned long irqflags;
4449 	u32 dspaddr_offset;
4450 	u32 dspcntr;
4451 
4452 	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
4453 
4454 	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
4455 
4456 	if (INTEL_GEN(dev_priv) >= 4)
4457 		dspaddr_offset = plane_state->color_plane[0].offset;
4458 	else
4459 		dspaddr_offset = linear_offset;
4460 
4461 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4462 
4463 	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4464 			  plane_state->color_plane[0].stride);
4465 
4466 	if (INTEL_GEN(dev_priv) < 4) {
4467 		/*
4468 		 * PLANE_A doesn't actually have a full window
4469 		 * generator but let's assume we still need to
4470 		 * program whatever is there.
4471 		 */
4472 		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4473 				  (crtc_y << 16) | crtc_x);
4474 		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4475 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4476 	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
4477 		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4478 				  (crtc_y << 16) | crtc_x);
4479 		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4480 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4481 		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
4482 	}
4483 
4484 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
4485 		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4486 				  (y << 16) | x);
4487 	} else if (INTEL_GEN(dev_priv) >= 4) {
4488 		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4489 				  linear_offset);
4490 		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4491 				  (y << 16) | x);
4492 	}
4493 
4494 	/*
4495 	 * The control register self-arms if the plane was previously
4496 	 * disabled. Try to make the plane enable atomic by writing
4497 	 * the control register just before the surface register.
4498 	 */
4499 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4500 	if (INTEL_GEN(dev_priv) >= 4)
4501 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4502 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4503 	else
4504 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4505 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4506 
4507 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4508 }
4509 
4510 static void i9xx_disable_plane(struct intel_plane *plane,
4511 			       const struct intel_crtc_state *crtc_state)
4512 {
4513 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4514 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4515 	unsigned long irqflags;
4516 	u32 dspcntr;
4517 
4518 	/*
4519 	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4520 	 * enable on ilk+ affect the pipe bottom color as
4521 	 * well, so we must configure them even if the plane
4522 	 * is disabled.
4523 	 *
4524 	 * On pre-g4x there is no way to gamma correct the
4525 	 * pipe bottom color but we'll keep on doing this
4526 	 * anyway so that the crtc state readout works correctly.
4527 	 */
4528 	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
4529 
4530 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4531 
4532 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4533 	if (INTEL_GEN(dev_priv) >= 4)
4534 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
4535 	else
4536 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
4537 
4538 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4539 }
4540 
4541 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4542 				    enum pipe *pipe)
4543 {
4544 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4545 	enum intel_display_power_domain power_domain;
4546 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4547 	intel_wakeref_t wakeref;
4548 	bool ret;
4549 	u32 val;
4550 
4551 	/*
4552 	 * Not 100% correct for planes that can move between pipes,
4553 	 * but that's only the case for gen2-4 which don't have any
4554 	 * display power wells.
4555 	 */
4556 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
4557 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4558 	if (!wakeref)
4559 		return false;
4560 
4561 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4562 
4563 	ret = val & DISPLAY_PLANE_ENABLE;
4564 
4565 	if (INTEL_GEN(dev_priv) >= 5)
4566 		*pipe = plane->pipe;
4567 	else
4568 		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4569 			DISPPLANE_SEL_PIPE_SHIFT;
4570 
4571 	intel_display_power_put(dev_priv, power_domain, wakeref);
4572 
4573 	return ret;
4574 }
4575 
4576 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4577 {
4578 	struct drm_device *dev = intel_crtc->base.dev;
4579 	struct drm_i915_private *dev_priv = to_i915(dev);
4580 	unsigned long irqflags;
4581 
4582 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4583 
4584 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4585 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4586 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
4587 
4588 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4589 }
4590 
4591 /*
4592  * This function detaches (aka. unbinds) unused scalers in hardware
4593  */
4594 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
4595 {
4596 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4597 	const struct intel_crtc_scaler_state *scaler_state =
4598 		&crtc_state->scaler_state;
4599 	int i;
4600 
4601 	/* loop through and disable scalers that aren't in use */
4602 	for (i = 0; i < intel_crtc->num_scalers; i++) {
4603 		if (!scaler_state->scalers[i].in_use)
4604 			skl_detach_scaler(intel_crtc, i);
4605 	}
4606 }
4607 
4608 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4609 					  int color_plane, unsigned int rotation)
4610 {
4611 	/*
4612 	 * The stride is either expressed as a multiple of 64 bytes chunks for
4613 	 * linear buffers or in number of tiles for tiled buffers.
4614 	 */
4615 	if (is_surface_linear(fb, color_plane))
4616 		return 64;
4617 	else if (drm_rotation_90_or_270(rotation))
4618 		return intel_tile_height(fb, color_plane);
4619 	else
4620 		return intel_tile_width_bytes(fb, color_plane);
4621 }
4622 
4623 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
4624 		     int color_plane)
4625 {
4626 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4627 	unsigned int rotation = plane_state->hw.rotation;
4628 	u32 stride = plane_state->color_plane[color_plane].stride;
4629 
4630 	if (color_plane >= fb->format->num_planes)
4631 		return 0;
4632 
4633 	return stride / skl_plane_stride_mult(fb, color_plane, rotation);
4634 }
4635 
4636 static u32 skl_plane_ctl_format(u32 pixel_format)
4637 {
4638 	switch (pixel_format) {
4639 	case DRM_FORMAT_C8:
4640 		return PLANE_CTL_FORMAT_INDEXED;
4641 	case DRM_FORMAT_RGB565:
4642 		return PLANE_CTL_FORMAT_RGB_565;
4643 	case DRM_FORMAT_XBGR8888:
4644 	case DRM_FORMAT_ABGR8888:
4645 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
4646 	case DRM_FORMAT_XRGB8888:
4647 	case DRM_FORMAT_ARGB8888:
4648 		return PLANE_CTL_FORMAT_XRGB_8888;
4649 	case DRM_FORMAT_XBGR2101010:
4650 	case DRM_FORMAT_ABGR2101010:
4651 		return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
4652 	case DRM_FORMAT_XRGB2101010:
4653 	case DRM_FORMAT_ARGB2101010:
4654 		return PLANE_CTL_FORMAT_XRGB_2101010;
4655 	case DRM_FORMAT_XBGR16161616F:
4656 	case DRM_FORMAT_ABGR16161616F:
4657 		return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4658 	case DRM_FORMAT_XRGB16161616F:
4659 	case DRM_FORMAT_ARGB16161616F:
4660 		return PLANE_CTL_FORMAT_XRGB_16161616F;
4661 	case DRM_FORMAT_XYUV8888:
4662 		return PLANE_CTL_FORMAT_XYUV;
4663 	case DRM_FORMAT_YUYV:
4664 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
4665 	case DRM_FORMAT_YVYU:
4666 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
4667 	case DRM_FORMAT_UYVY:
4668 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
4669 	case DRM_FORMAT_VYUY:
4670 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
4671 	case DRM_FORMAT_NV12:
4672 		return PLANE_CTL_FORMAT_NV12;
4673 	case DRM_FORMAT_P010:
4674 		return PLANE_CTL_FORMAT_P010;
4675 	case DRM_FORMAT_P012:
4676 		return PLANE_CTL_FORMAT_P012;
4677 	case DRM_FORMAT_P016:
4678 		return PLANE_CTL_FORMAT_P016;
4679 	case DRM_FORMAT_Y210:
4680 		return PLANE_CTL_FORMAT_Y210;
4681 	case DRM_FORMAT_Y212:
4682 		return PLANE_CTL_FORMAT_Y212;
4683 	case DRM_FORMAT_Y216:
4684 		return PLANE_CTL_FORMAT_Y216;
4685 	case DRM_FORMAT_XVYU2101010:
4686 		return PLANE_CTL_FORMAT_Y410;
4687 	case DRM_FORMAT_XVYU12_16161616:
4688 		return PLANE_CTL_FORMAT_Y412;
4689 	case DRM_FORMAT_XVYU16161616:
4690 		return PLANE_CTL_FORMAT_Y416;
4691 	default:
4692 		MISSING_CASE(pixel_format);
4693 	}
4694 
4695 	return 0;
4696 }
4697 
4698 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4699 {
4700 	if (!plane_state->hw.fb->format->has_alpha)
4701 		return PLANE_CTL_ALPHA_DISABLE;
4702 
4703 	switch (plane_state->hw.pixel_blend_mode) {
4704 	case DRM_MODE_BLEND_PIXEL_NONE:
4705 		return PLANE_CTL_ALPHA_DISABLE;
4706 	case DRM_MODE_BLEND_PREMULTI:
4707 		return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4708 	case DRM_MODE_BLEND_COVERAGE:
4709 		return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4710 	default:
4711 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4712 		return PLANE_CTL_ALPHA_DISABLE;
4713 	}
4714 }
4715 
4716 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4717 {
4718 	if (!plane_state->hw.fb->format->has_alpha)
4719 		return PLANE_COLOR_ALPHA_DISABLE;
4720 
4721 	switch (plane_state->hw.pixel_blend_mode) {
4722 	case DRM_MODE_BLEND_PIXEL_NONE:
4723 		return PLANE_COLOR_ALPHA_DISABLE;
4724 	case DRM_MODE_BLEND_PREMULTI:
4725 		return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4726 	case DRM_MODE_BLEND_COVERAGE:
4727 		return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4728 	default:
4729 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4730 		return PLANE_COLOR_ALPHA_DISABLE;
4731 	}
4732 }
4733 
4734 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4735 {
4736 	switch (fb_modifier) {
4737 	case DRM_FORMAT_MOD_LINEAR:
4738 		break;
4739 	case I915_FORMAT_MOD_X_TILED:
4740 		return PLANE_CTL_TILED_X;
4741 	case I915_FORMAT_MOD_Y_TILED:
4742 		return PLANE_CTL_TILED_Y;
4743 	case I915_FORMAT_MOD_Y_TILED_CCS:
4744 		return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4745 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4746 		return PLANE_CTL_TILED_Y |
4747 		       PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4748 		       PLANE_CTL_CLEAR_COLOR_DISABLE;
4749 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4750 		return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
4751 	case I915_FORMAT_MOD_Yf_TILED:
4752 		return PLANE_CTL_TILED_YF;
4753 	case I915_FORMAT_MOD_Yf_TILED_CCS:
4754 		return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4755 	default:
4756 		MISSING_CASE(fb_modifier);
4757 	}
4758 
4759 	return 0;
4760 }
4761 
4762 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4763 {
4764 	switch (rotate) {
4765 	case DRM_MODE_ROTATE_0:
4766 		break;
4767 	/*
4768 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4769 	 * while i915 HW rotation is clockwise, thats why this swapping.
4770 	 */
4771 	case DRM_MODE_ROTATE_90:
4772 		return PLANE_CTL_ROTATE_270;
4773 	case DRM_MODE_ROTATE_180:
4774 		return PLANE_CTL_ROTATE_180;
4775 	case DRM_MODE_ROTATE_270:
4776 		return PLANE_CTL_ROTATE_90;
4777 	default:
4778 		MISSING_CASE(rotate);
4779 	}
4780 
4781 	return 0;
4782 }
4783 
4784 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4785 {
4786 	switch (reflect) {
4787 	case 0:
4788 		break;
4789 	case DRM_MODE_REFLECT_X:
4790 		return PLANE_CTL_FLIP_HORIZONTAL;
4791 	case DRM_MODE_REFLECT_Y:
4792 	default:
4793 		MISSING_CASE(reflect);
4794 	}
4795 
4796 	return 0;
4797 }
4798 
4799 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4800 {
4801 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4802 	u32 plane_ctl = 0;
4803 
4804 	if (crtc_state->uapi.async_flip)
4805 		plane_ctl |= PLANE_CTL_ASYNC_FLIP;
4806 
4807 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4808 		return plane_ctl;
4809 
4810 	if (crtc_state->gamma_enable)
4811 		plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4812 
4813 	if (crtc_state->csc_enable)
4814 		plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4815 
4816 	return plane_ctl;
4817 }
4818 
4819 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4820 		  const struct intel_plane_state *plane_state)
4821 {
4822 	struct drm_i915_private *dev_priv =
4823 		to_i915(plane_state->uapi.plane->dev);
4824 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4825 	unsigned int rotation = plane_state->hw.rotation;
4826 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4827 	u32 plane_ctl;
4828 
4829 	plane_ctl = PLANE_CTL_ENABLE;
4830 
4831 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4832 		plane_ctl |= skl_plane_ctl_alpha(plane_state);
4833 		plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4834 
4835 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4836 			plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4837 
4838 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4839 			plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4840 	}
4841 
4842 	plane_ctl |= skl_plane_ctl_format(fb->format->format);
4843 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4844 	plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4845 
4846 	if (INTEL_GEN(dev_priv) >= 10)
4847 		plane_ctl |= cnl_plane_ctl_flip(rotation &
4848 						DRM_MODE_REFLECT_MASK);
4849 
4850 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
4851 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4852 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
4853 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4854 
4855 	return plane_ctl;
4856 }
4857 
4858 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4859 {
4860 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4861 	u32 plane_color_ctl = 0;
4862 
4863 	if (INTEL_GEN(dev_priv) >= 11)
4864 		return plane_color_ctl;
4865 
4866 	if (crtc_state->gamma_enable)
4867 		plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4868 
4869 	if (crtc_state->csc_enable)
4870 		plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4871 
4872 	return plane_color_ctl;
4873 }
4874 
4875 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4876 			const struct intel_plane_state *plane_state)
4877 {
4878 	struct drm_i915_private *dev_priv =
4879 		to_i915(plane_state->uapi.plane->dev);
4880 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4881 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4882 	u32 plane_color_ctl = 0;
4883 
4884 	plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4885 	plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4886 
4887 	if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4888 		switch (plane_state->hw.color_encoding) {
4889 		case DRM_COLOR_YCBCR_BT709:
4890 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4891 			break;
4892 		case DRM_COLOR_YCBCR_BT2020:
4893 			plane_color_ctl |=
4894 				PLANE_COLOR_CSC_MODE_YUV2020_TO_RGB2020;
4895 			break;
4896 		default:
4897 			plane_color_ctl |=
4898 				PLANE_COLOR_CSC_MODE_YUV601_TO_RGB601;
4899 		}
4900 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4901 			plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4902 	} else if (fb->format->is_yuv) {
4903 		plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4904 	}
4905 
4906 	return plane_color_ctl;
4907 }
4908 
4909 static int
4910 __intel_display_resume(struct drm_device *dev,
4911 		       struct drm_atomic_state *state,
4912 		       struct drm_modeset_acquire_ctx *ctx)
4913 {
4914 	struct drm_crtc_state *crtc_state;
4915 	struct drm_crtc *crtc;
4916 	int i, ret;
4917 
4918 	intel_modeset_setup_hw_state(dev, ctx);
4919 	intel_vga_redisable(to_i915(dev));
4920 
4921 	if (!state)
4922 		return 0;
4923 
4924 	/*
4925 	 * We've duplicated the state, pointers to the old state are invalid.
4926 	 *
4927 	 * Don't attempt to use the old state until we commit the duplicated state.
4928 	 */
4929 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4930 		/*
4931 		 * Force recalculation even if we restore
4932 		 * current state. With fast modeset this may not result
4933 		 * in a modeset when the state is compatible.
4934 		 */
4935 		crtc_state->mode_changed = true;
4936 	}
4937 
4938 	/* ignore any reset values/BIOS leftovers in the WM registers */
4939 	if (!HAS_GMCH(to_i915(dev)))
4940 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
4941 
4942 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4943 
4944 	drm_WARN_ON(dev, ret == -EDEADLK);
4945 	return ret;
4946 }
4947 
4948 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4949 {
4950 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4951 		intel_has_gpu_reset(&dev_priv->gt));
4952 }
4953 
4954 void intel_prepare_reset(struct drm_i915_private *dev_priv)
4955 {
4956 	struct drm_device *dev = &dev_priv->drm;
4957 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4958 	struct drm_atomic_state *state;
4959 	int ret;
4960 
4961 	/* reset doesn't touch the display */
4962 	if (!dev_priv->params.force_reset_modeset_test &&
4963 	    !gpu_reset_clobbers_display(dev_priv))
4964 		return;
4965 
4966 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
4967 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4968 	smp_mb__after_atomic();
4969 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4970 
4971 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4972 		drm_dbg_kms(&dev_priv->drm,
4973 			    "Modeset potentially stuck, unbreaking through wedging\n");
4974 		intel_gt_set_wedged(&dev_priv->gt);
4975 	}
4976 
4977 	/*
4978 	 * Need mode_config.mutex so that we don't
4979 	 * trample ongoing ->detect() and whatnot.
4980 	 */
4981 	mutex_lock(&dev->mode_config.mutex);
4982 	drm_modeset_acquire_init(ctx, 0);
4983 	while (1) {
4984 		ret = drm_modeset_lock_all_ctx(dev, ctx);
4985 		if (ret != -EDEADLK)
4986 			break;
4987 
4988 		drm_modeset_backoff(ctx);
4989 	}
4990 	/*
4991 	 * Disabling the crtcs gracefully seems nicer. Also the
4992 	 * g33 docs say we should at least disable all the planes.
4993 	 */
4994 	state = drm_atomic_helper_duplicate_state(dev, ctx);
4995 	if (IS_ERR(state)) {
4996 		ret = PTR_ERR(state);
4997 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4998 			ret);
4999 		return;
5000 	}
5001 
5002 	ret = drm_atomic_helper_disable_all(dev, ctx);
5003 	if (ret) {
5004 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
5005 			ret);
5006 		drm_atomic_state_put(state);
5007 		return;
5008 	}
5009 
5010 	dev_priv->modeset_restore_state = state;
5011 	state->acquire_ctx = ctx;
5012 }
5013 
5014 void intel_finish_reset(struct drm_i915_private *dev_priv)
5015 {
5016 	struct drm_device *dev = &dev_priv->drm;
5017 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
5018 	struct drm_atomic_state *state;
5019 	int ret;
5020 
5021 	/* reset doesn't touch the display */
5022 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
5023 		return;
5024 
5025 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
5026 	if (!state)
5027 		goto unlock;
5028 
5029 	/* reset doesn't touch the display */
5030 	if (!gpu_reset_clobbers_display(dev_priv)) {
5031 		/* for testing only restore the display */
5032 		ret = __intel_display_resume(dev, state, ctx);
5033 		if (ret)
5034 			drm_err(&dev_priv->drm,
5035 				"Restoring old state failed with %i\n", ret);
5036 	} else {
5037 		/*
5038 		 * The display has been reset as well,
5039 		 * so need a full re-initialization.
5040 		 */
5041 		intel_pps_unlock_regs_wa(dev_priv);
5042 		intel_modeset_init_hw(dev_priv);
5043 		intel_init_clock_gating(dev_priv);
5044 		intel_hpd_init(dev_priv);
5045 
5046 		ret = __intel_display_resume(dev, state, ctx);
5047 		if (ret)
5048 			drm_err(&dev_priv->drm,
5049 				"Restoring old state failed with %i\n", ret);
5050 
5051 		intel_hpd_poll_disable(dev_priv);
5052 	}
5053 
5054 	drm_atomic_state_put(state);
5055 unlock:
5056 	drm_modeset_drop_locks(ctx);
5057 	drm_modeset_acquire_fini(ctx);
5058 	mutex_unlock(&dev->mode_config.mutex);
5059 
5060 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
5061 }
5062 
5063 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
5064 {
5065 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5066 	enum pipe pipe = crtc->pipe;
5067 	u32 tmp;
5068 
5069 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
5070 
5071 	/*
5072 	 * Display WA #1153: icl
5073 	 * enable hardware to bypass the alpha math
5074 	 * and rounding for per-pixel values 00 and 0xff
5075 	 */
5076 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
5077 	/*
5078 	 * Display WA # 1605353570: icl
5079 	 * Set the pixel rounding bit to 1 for allowing
5080 	 * passthrough of Frame buffer pixels unmodified
5081 	 * across pipe
5082 	 */
5083 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
5084 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
5085 }
5086 
5087 static void intel_fdi_normal_train(struct intel_crtc *crtc)
5088 {
5089 	struct drm_device *dev = crtc->base.dev;
5090 	struct drm_i915_private *dev_priv = to_i915(dev);
5091 	enum pipe pipe = crtc->pipe;
5092 	i915_reg_t reg;
5093 	u32 temp;
5094 
5095 	/* enable normal train */
5096 	reg = FDI_TX_CTL(pipe);
5097 	temp = intel_de_read(dev_priv, reg);
5098 	if (IS_IVYBRIDGE(dev_priv)) {
5099 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5100 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
5101 	} else {
5102 		temp &= ~FDI_LINK_TRAIN_NONE;
5103 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
5104 	}
5105 	intel_de_write(dev_priv, reg, temp);
5106 
5107 	reg = FDI_RX_CTL(pipe);
5108 	temp = intel_de_read(dev_priv, reg);
5109 	if (HAS_PCH_CPT(dev_priv)) {
5110 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5111 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5112 	} else {
5113 		temp &= ~FDI_LINK_TRAIN_NONE;
5114 		temp |= FDI_LINK_TRAIN_NONE;
5115 	}
5116 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5117 
5118 	/* wait one idle pattern time */
5119 	intel_de_posting_read(dev_priv, reg);
5120 	udelay(1000);
5121 
5122 	/* IVB wants error correction enabled */
5123 	if (IS_IVYBRIDGE(dev_priv))
5124 		intel_de_write(dev_priv, reg,
5125 		               intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5126 }
5127 
5128 /* The FDI link training functions for ILK/Ibexpeak. */
5129 static void ilk_fdi_link_train(struct intel_crtc *crtc,
5130 			       const struct intel_crtc_state *crtc_state)
5131 {
5132 	struct drm_device *dev = crtc->base.dev;
5133 	struct drm_i915_private *dev_priv = to_i915(dev);
5134 	enum pipe pipe = crtc->pipe;
5135 	i915_reg_t reg;
5136 	u32 temp, tries;
5137 
5138 	/* FDI needs bits from pipe first */
5139 	assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
5140 
5141 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5142 	   for train result */
5143 	reg = FDI_RX_IMR(pipe);
5144 	temp = intel_de_read(dev_priv, reg);
5145 	temp &= ~FDI_RX_SYMBOL_LOCK;
5146 	temp &= ~FDI_RX_BIT_LOCK;
5147 	intel_de_write(dev_priv, reg, temp);
5148 	intel_de_read(dev_priv, reg);
5149 	udelay(150);
5150 
5151 	/* enable CPU FDI TX and PCH FDI RX */
5152 	reg = FDI_TX_CTL(pipe);
5153 	temp = intel_de_read(dev_priv, reg);
5154 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5155 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5156 	temp &= ~FDI_LINK_TRAIN_NONE;
5157 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5158 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5159 
5160 	reg = FDI_RX_CTL(pipe);
5161 	temp = intel_de_read(dev_priv, reg);
5162 	temp &= ~FDI_LINK_TRAIN_NONE;
5163 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5164 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5165 
5166 	intel_de_posting_read(dev_priv, reg);
5167 	udelay(150);
5168 
5169 	/* Ironlake workaround, enable clock pointer after FDI enable*/
5170 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5171 		       FDI_RX_PHASE_SYNC_POINTER_OVR);
5172 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5173 		       FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5174 
5175 	reg = FDI_RX_IIR(pipe);
5176 	for (tries = 0; tries < 5; tries++) {
5177 		temp = intel_de_read(dev_priv, reg);
5178 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5179 
5180 		if ((temp & FDI_RX_BIT_LOCK)) {
5181 			drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
5182 			intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
5183 			break;
5184 		}
5185 	}
5186 	if (tries == 5)
5187 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5188 
5189 	/* Train 2 */
5190 	reg = FDI_TX_CTL(pipe);
5191 	temp = intel_de_read(dev_priv, reg);
5192 	temp &= ~FDI_LINK_TRAIN_NONE;
5193 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5194 	intel_de_write(dev_priv, reg, temp);
5195 
5196 	reg = FDI_RX_CTL(pipe);
5197 	temp = intel_de_read(dev_priv, reg);
5198 	temp &= ~FDI_LINK_TRAIN_NONE;
5199 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5200 	intel_de_write(dev_priv, reg, temp);
5201 
5202 	intel_de_posting_read(dev_priv, reg);
5203 	udelay(150);
5204 
5205 	reg = FDI_RX_IIR(pipe);
5206 	for (tries = 0; tries < 5; tries++) {
5207 		temp = intel_de_read(dev_priv, reg);
5208 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5209 
5210 		if (temp & FDI_RX_SYMBOL_LOCK) {
5211 			intel_de_write(dev_priv, reg,
5212 				       temp | FDI_RX_SYMBOL_LOCK);
5213 			drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
5214 			break;
5215 		}
5216 	}
5217 	if (tries == 5)
5218 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5219 
5220 	drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5221 
5222 }
5223 
5224 static const int snb_b_fdi_train_param[] = {
5225 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5226 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5227 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5228 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5229 };
5230 
5231 /* The FDI link training functions for SNB/Cougarpoint. */
5232 static void gen6_fdi_link_train(struct intel_crtc *crtc,
5233 				const struct intel_crtc_state *crtc_state)
5234 {
5235 	struct drm_device *dev = crtc->base.dev;
5236 	struct drm_i915_private *dev_priv = to_i915(dev);
5237 	enum pipe pipe = crtc->pipe;
5238 	i915_reg_t reg;
5239 	u32 temp, i, retry;
5240 
5241 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5242 	   for train result */
5243 	reg = FDI_RX_IMR(pipe);
5244 	temp = intel_de_read(dev_priv, reg);
5245 	temp &= ~FDI_RX_SYMBOL_LOCK;
5246 	temp &= ~FDI_RX_BIT_LOCK;
5247 	intel_de_write(dev_priv, reg, temp);
5248 
5249 	intel_de_posting_read(dev_priv, reg);
5250 	udelay(150);
5251 
5252 	/* enable CPU FDI TX and PCH FDI RX */
5253 	reg = FDI_TX_CTL(pipe);
5254 	temp = intel_de_read(dev_priv, reg);
5255 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5256 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5257 	temp &= ~FDI_LINK_TRAIN_NONE;
5258 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5259 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5260 	/* SNB-B */
5261 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5262 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5263 
5264 	intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5265 		       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5266 
5267 	reg = FDI_RX_CTL(pipe);
5268 	temp = intel_de_read(dev_priv, reg);
5269 	if (HAS_PCH_CPT(dev_priv)) {
5270 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5271 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5272 	} else {
5273 		temp &= ~FDI_LINK_TRAIN_NONE;
5274 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5275 	}
5276 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5277 
5278 	intel_de_posting_read(dev_priv, reg);
5279 	udelay(150);
5280 
5281 	for (i = 0; i < 4; i++) {
5282 		reg = FDI_TX_CTL(pipe);
5283 		temp = intel_de_read(dev_priv, reg);
5284 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5285 		temp |= snb_b_fdi_train_param[i];
5286 		intel_de_write(dev_priv, reg, temp);
5287 
5288 		intel_de_posting_read(dev_priv, reg);
5289 		udelay(500);
5290 
5291 		for (retry = 0; retry < 5; retry++) {
5292 			reg = FDI_RX_IIR(pipe);
5293 			temp = intel_de_read(dev_priv, reg);
5294 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5295 			if (temp & FDI_RX_BIT_LOCK) {
5296 				intel_de_write(dev_priv, reg,
5297 					       temp | FDI_RX_BIT_LOCK);
5298 				drm_dbg_kms(&dev_priv->drm,
5299 					    "FDI train 1 done.\n");
5300 				break;
5301 			}
5302 			udelay(50);
5303 		}
5304 		if (retry < 5)
5305 			break;
5306 	}
5307 	if (i == 4)
5308 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5309 
5310 	/* Train 2 */
5311 	reg = FDI_TX_CTL(pipe);
5312 	temp = intel_de_read(dev_priv, reg);
5313 	temp &= ~FDI_LINK_TRAIN_NONE;
5314 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5315 	if (IS_GEN(dev_priv, 6)) {
5316 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5317 		/* SNB-B */
5318 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5319 	}
5320 	intel_de_write(dev_priv, reg, temp);
5321 
5322 	reg = FDI_RX_CTL(pipe);
5323 	temp = intel_de_read(dev_priv, reg);
5324 	if (HAS_PCH_CPT(dev_priv)) {
5325 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5326 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5327 	} else {
5328 		temp &= ~FDI_LINK_TRAIN_NONE;
5329 		temp |= FDI_LINK_TRAIN_PATTERN_2;
5330 	}
5331 	intel_de_write(dev_priv, reg, temp);
5332 
5333 	intel_de_posting_read(dev_priv, reg);
5334 	udelay(150);
5335 
5336 	for (i = 0; i < 4; i++) {
5337 		reg = FDI_TX_CTL(pipe);
5338 		temp = intel_de_read(dev_priv, reg);
5339 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5340 		temp |= snb_b_fdi_train_param[i];
5341 		intel_de_write(dev_priv, reg, temp);
5342 
5343 		intel_de_posting_read(dev_priv, reg);
5344 		udelay(500);
5345 
5346 		for (retry = 0; retry < 5; retry++) {
5347 			reg = FDI_RX_IIR(pipe);
5348 			temp = intel_de_read(dev_priv, reg);
5349 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5350 			if (temp & FDI_RX_SYMBOL_LOCK) {
5351 				intel_de_write(dev_priv, reg,
5352 					       temp | FDI_RX_SYMBOL_LOCK);
5353 				drm_dbg_kms(&dev_priv->drm,
5354 					    "FDI train 2 done.\n");
5355 				break;
5356 			}
5357 			udelay(50);
5358 		}
5359 		if (retry < 5)
5360 			break;
5361 	}
5362 	if (i == 4)
5363 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5364 
5365 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5366 }
5367 
5368 /* Manual link training for Ivy Bridge A0 parts */
5369 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5370 				      const struct intel_crtc_state *crtc_state)
5371 {
5372 	struct drm_device *dev = crtc->base.dev;
5373 	struct drm_i915_private *dev_priv = to_i915(dev);
5374 	enum pipe pipe = crtc->pipe;
5375 	i915_reg_t reg;
5376 	u32 temp, i, j;
5377 
5378 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5379 	   for train result */
5380 	reg = FDI_RX_IMR(pipe);
5381 	temp = intel_de_read(dev_priv, reg);
5382 	temp &= ~FDI_RX_SYMBOL_LOCK;
5383 	temp &= ~FDI_RX_BIT_LOCK;
5384 	intel_de_write(dev_priv, reg, temp);
5385 
5386 	intel_de_posting_read(dev_priv, reg);
5387 	udelay(150);
5388 
5389 	drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
5390 		    intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
5391 
5392 	/* Try each vswing and preemphasis setting twice before moving on */
5393 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5394 		/* disable first in case we need to retry */
5395 		reg = FDI_TX_CTL(pipe);
5396 		temp = intel_de_read(dev_priv, reg);
5397 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5398 		temp &= ~FDI_TX_ENABLE;
5399 		intel_de_write(dev_priv, reg, temp);
5400 
5401 		reg = FDI_RX_CTL(pipe);
5402 		temp = intel_de_read(dev_priv, reg);
5403 		temp &= ~FDI_LINK_TRAIN_AUTO;
5404 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5405 		temp &= ~FDI_RX_ENABLE;
5406 		intel_de_write(dev_priv, reg, temp);
5407 
5408 		/* enable CPU FDI TX and PCH FDI RX */
5409 		reg = FDI_TX_CTL(pipe);
5410 		temp = intel_de_read(dev_priv, reg);
5411 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
5412 		temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5413 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
5414 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5415 		temp |= snb_b_fdi_train_param[j/2];
5416 		temp |= FDI_COMPOSITE_SYNC;
5417 		intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5418 
5419 		intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5420 			       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5421 
5422 		reg = FDI_RX_CTL(pipe);
5423 		temp = intel_de_read(dev_priv, reg);
5424 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5425 		temp |= FDI_COMPOSITE_SYNC;
5426 		intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5427 
5428 		intel_de_posting_read(dev_priv, reg);
5429 		udelay(1); /* should be 0.5us */
5430 
5431 		for (i = 0; i < 4; i++) {
5432 			reg = FDI_RX_IIR(pipe);
5433 			temp = intel_de_read(dev_priv, reg);
5434 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5435 
5436 			if (temp & FDI_RX_BIT_LOCK ||
5437 			    (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5438 				intel_de_write(dev_priv, reg,
5439 					       temp | FDI_RX_BIT_LOCK);
5440 				drm_dbg_kms(&dev_priv->drm,
5441 					    "FDI train 1 done, level %i.\n",
5442 					    i);
5443 				break;
5444 			}
5445 			udelay(1); /* should be 0.5us */
5446 		}
5447 		if (i == 4) {
5448 			drm_dbg_kms(&dev_priv->drm,
5449 				    "FDI train 1 fail on vswing %d\n", j / 2);
5450 			continue;
5451 		}
5452 
5453 		/* Train 2 */
5454 		reg = FDI_TX_CTL(pipe);
5455 		temp = intel_de_read(dev_priv, reg);
5456 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5457 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
5458 		intel_de_write(dev_priv, reg, temp);
5459 
5460 		reg = FDI_RX_CTL(pipe);
5461 		temp = intel_de_read(dev_priv, reg);
5462 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5463 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5464 		intel_de_write(dev_priv, reg, temp);
5465 
5466 		intel_de_posting_read(dev_priv, reg);
5467 		udelay(2); /* should be 1.5us */
5468 
5469 		for (i = 0; i < 4; i++) {
5470 			reg = FDI_RX_IIR(pipe);
5471 			temp = intel_de_read(dev_priv, reg);
5472 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5473 
5474 			if (temp & FDI_RX_SYMBOL_LOCK ||
5475 			    (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5476 				intel_de_write(dev_priv, reg,
5477 					       temp | FDI_RX_SYMBOL_LOCK);
5478 				drm_dbg_kms(&dev_priv->drm,
5479 					    "FDI train 2 done, level %i.\n",
5480 					    i);
5481 				goto train_done;
5482 			}
5483 			udelay(2); /* should be 1.5us */
5484 		}
5485 		if (i == 4)
5486 			drm_dbg_kms(&dev_priv->drm,
5487 				    "FDI train 2 fail on vswing %d\n", j / 2);
5488 	}
5489 
5490 train_done:
5491 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5492 }
5493 
5494 static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
5495 {
5496 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
5497 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5498 	enum pipe pipe = intel_crtc->pipe;
5499 	i915_reg_t reg;
5500 	u32 temp;
5501 
5502 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5503 	reg = FDI_RX_CTL(pipe);
5504 	temp = intel_de_read(dev_priv, reg);
5505 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
5506 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5507 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5508 	intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5509 
5510 	intel_de_posting_read(dev_priv, reg);
5511 	udelay(200);
5512 
5513 	/* Switch from Rawclk to PCDclk */
5514 	temp = intel_de_read(dev_priv, reg);
5515 	intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5516 
5517 	intel_de_posting_read(dev_priv, reg);
5518 	udelay(200);
5519 
5520 	/* Enable CPU FDI TX PLL, always on for Ironlake */
5521 	reg = FDI_TX_CTL(pipe);
5522 	temp = intel_de_read(dev_priv, reg);
5523 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
5524 		intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5525 
5526 		intel_de_posting_read(dev_priv, reg);
5527 		udelay(100);
5528 	}
5529 }
5530 
5531 static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
5532 {
5533 	struct drm_device *dev = intel_crtc->base.dev;
5534 	struct drm_i915_private *dev_priv = to_i915(dev);
5535 	enum pipe pipe = intel_crtc->pipe;
5536 	i915_reg_t reg;
5537 	u32 temp;
5538 
5539 	/* Switch from PCDclk to Rawclk */
5540 	reg = FDI_RX_CTL(pipe);
5541 	temp = intel_de_read(dev_priv, reg);
5542 	intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
5543 
5544 	/* Disable CPU FDI TX PLL */
5545 	reg = FDI_TX_CTL(pipe);
5546 	temp = intel_de_read(dev_priv, reg);
5547 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
5548 
5549 	intel_de_posting_read(dev_priv, reg);
5550 	udelay(100);
5551 
5552 	reg = FDI_RX_CTL(pipe);
5553 	temp = intel_de_read(dev_priv, reg);
5554 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
5555 
5556 	/* Wait for the clocks to turn off. */
5557 	intel_de_posting_read(dev_priv, reg);
5558 	udelay(100);
5559 }
5560 
5561 static void ilk_fdi_disable(struct intel_crtc *crtc)
5562 {
5563 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5564 	enum pipe pipe = crtc->pipe;
5565 	i915_reg_t reg;
5566 	u32 temp;
5567 
5568 	/* disable CPU FDI tx and PCH FDI rx */
5569 	reg = FDI_TX_CTL(pipe);
5570 	temp = intel_de_read(dev_priv, reg);
5571 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5572 	intel_de_posting_read(dev_priv, reg);
5573 
5574 	reg = FDI_RX_CTL(pipe);
5575 	temp = intel_de_read(dev_priv, reg);
5576 	temp &= ~(0x7 << 16);
5577 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5578 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
5579 
5580 	intel_de_posting_read(dev_priv, reg);
5581 	udelay(100);
5582 
5583 	/* Ironlake workaround, disable clock pointer after downing FDI */
5584 	if (HAS_PCH_IBX(dev_priv))
5585 		intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5586 			       FDI_RX_PHASE_SYNC_POINTER_OVR);
5587 
5588 	/* still set train pattern 1 */
5589 	reg = FDI_TX_CTL(pipe);
5590 	temp = intel_de_read(dev_priv, reg);
5591 	temp &= ~FDI_LINK_TRAIN_NONE;
5592 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5593 	intel_de_write(dev_priv, reg, temp);
5594 
5595 	reg = FDI_RX_CTL(pipe);
5596 	temp = intel_de_read(dev_priv, reg);
5597 	if (HAS_PCH_CPT(dev_priv)) {
5598 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5599 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5600 	} else {
5601 		temp &= ~FDI_LINK_TRAIN_NONE;
5602 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5603 	}
5604 	/* BPC in FDI rx is consistent with that in PIPECONF */
5605 	temp &= ~(0x07 << 16);
5606 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5607 	intel_de_write(dev_priv, reg, temp);
5608 
5609 	intel_de_posting_read(dev_priv, reg);
5610 	udelay(100);
5611 }
5612 
5613 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5614 {
5615 	struct drm_crtc *crtc;
5616 	bool cleanup_done;
5617 
5618 	drm_for_each_crtc(crtc, &dev_priv->drm) {
5619 		struct drm_crtc_commit *commit;
5620 		spin_lock(&crtc->commit_lock);
5621 		commit = list_first_entry_or_null(&crtc->commit_list,
5622 						  struct drm_crtc_commit, commit_entry);
5623 		cleanup_done = commit ?
5624 			try_wait_for_completion(&commit->cleanup_done) : true;
5625 		spin_unlock(&crtc->commit_lock);
5626 
5627 		if (cleanup_done)
5628 			continue;
5629 
5630 		drm_crtc_wait_one_vblank(crtc);
5631 
5632 		return true;
5633 	}
5634 
5635 	return false;
5636 }
5637 
5638 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
5639 {
5640 	u32 temp;
5641 
5642 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
5643 
5644 	mutex_lock(&dev_priv->sb_lock);
5645 
5646 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5647 	temp |= SBI_SSCCTL_DISABLE;
5648 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5649 
5650 	mutex_unlock(&dev_priv->sb_lock);
5651 }
5652 
5653 /* Program iCLKIP clock to the desired frequency */
5654 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
5655 {
5656 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5657 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5658 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
5659 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
5660 	u32 temp;
5661 
5662 	lpt_disable_iclkip(dev_priv);
5663 
5664 	/* The iCLK virtual clock root frequency is in MHz,
5665 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
5666 	 * divisors, it is necessary to divide one by another, so we
5667 	 * convert the virtual clock precision to KHz here for higher
5668 	 * precision.
5669 	 */
5670 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
5671 		u32 iclk_virtual_root_freq = 172800 * 1000;
5672 		u32 iclk_pi_range = 64;
5673 		u32 desired_divisor;
5674 
5675 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5676 						    clock << auxdiv);
5677 		divsel = (desired_divisor / iclk_pi_range) - 2;
5678 		phaseinc = desired_divisor % iclk_pi_range;
5679 
5680 		/*
5681 		 * Near 20MHz is a corner case which is
5682 		 * out of range for the 7-bit divisor
5683 		 */
5684 		if (divsel <= 0x7f)
5685 			break;
5686 	}
5687 
5688 	/* This should not happen with any sane values */
5689 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5690 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5691 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5692 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5693 
5694 	drm_dbg_kms(&dev_priv->drm,
5695 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5696 		    clock, auxdiv, divsel, phasedir, phaseinc);
5697 
5698 	mutex_lock(&dev_priv->sb_lock);
5699 
5700 	/* Program SSCDIVINTPHASE6 */
5701 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5702 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5703 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5704 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5705 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5706 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5707 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5708 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5709 
5710 	/* Program SSCAUXDIV */
5711 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5712 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5713 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5714 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5715 
5716 	/* Enable modulator and associated divider */
5717 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5718 	temp &= ~SBI_SSCCTL_DISABLE;
5719 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5720 
5721 	mutex_unlock(&dev_priv->sb_lock);
5722 
5723 	/* Wait for initialization time */
5724 	udelay(24);
5725 
5726 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5727 }
5728 
5729 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5730 {
5731 	u32 divsel, phaseinc, auxdiv;
5732 	u32 iclk_virtual_root_freq = 172800 * 1000;
5733 	u32 iclk_pi_range = 64;
5734 	u32 desired_divisor;
5735 	u32 temp;
5736 
5737 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5738 		return 0;
5739 
5740 	mutex_lock(&dev_priv->sb_lock);
5741 
5742 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5743 	if (temp & SBI_SSCCTL_DISABLE) {
5744 		mutex_unlock(&dev_priv->sb_lock);
5745 		return 0;
5746 	}
5747 
5748 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5749 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5750 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5751 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5752 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5753 
5754 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5755 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5756 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5757 
5758 	mutex_unlock(&dev_priv->sb_lock);
5759 
5760 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5761 
5762 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5763 				 desired_divisor << auxdiv);
5764 }
5765 
5766 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5767 					   enum pipe pch_transcoder)
5768 {
5769 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5770 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5771 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5772 
5773 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5774 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5775 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5776 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5777 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5778 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
5779 
5780 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5781 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5782 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5783 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5784 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5785 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5786 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5787 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
5788 }
5789 
5790 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5791 {
5792 	u32 temp;
5793 
5794 	temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
5795 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5796 		return;
5797 
5798 	drm_WARN_ON(&dev_priv->drm,
5799 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5800 		    FDI_RX_ENABLE);
5801 	drm_WARN_ON(&dev_priv->drm,
5802 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5803 		    FDI_RX_ENABLE);
5804 
5805 	temp &= ~FDI_BC_BIFURCATION_SELECT;
5806 	if (enable)
5807 		temp |= FDI_BC_BIFURCATION_SELECT;
5808 
5809 	drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5810 		    enable ? "en" : "dis");
5811 	intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5812 	intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
5813 }
5814 
5815 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5816 {
5817 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5818 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5819 
5820 	switch (crtc->pipe) {
5821 	case PIPE_A:
5822 		break;
5823 	case PIPE_B:
5824 		if (crtc_state->fdi_lanes > 2)
5825 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
5826 		else
5827 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
5828 
5829 		break;
5830 	case PIPE_C:
5831 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
5832 
5833 		break;
5834 	default:
5835 		BUG();
5836 	}
5837 }
5838 
5839 /*
5840  * Finds the encoder associated with the given CRTC. This can only be
5841  * used when we know that the CRTC isn't feeding multiple encoders!
5842  */
5843 static struct intel_encoder *
5844 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5845 			   const struct intel_crtc_state *crtc_state)
5846 {
5847 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5848 	const struct drm_connector_state *connector_state;
5849 	const struct drm_connector *connector;
5850 	struct intel_encoder *encoder = NULL;
5851 	int num_encoders = 0;
5852 	int i;
5853 
5854 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5855 		if (connector_state->crtc != &crtc->base)
5856 			continue;
5857 
5858 		encoder = to_intel_encoder(connector_state->best_encoder);
5859 		num_encoders++;
5860 	}
5861 
5862 	drm_WARN(encoder->base.dev, num_encoders != 1,
5863 		 "%d encoders for pipe %c\n",
5864 		 num_encoders, pipe_name(crtc->pipe));
5865 
5866 	return encoder;
5867 }
5868 
5869 /*
5870  * Enable PCH resources required for PCH ports:
5871  *   - PCH PLLs
5872  *   - FDI training & RX/TX
5873  *   - update transcoder timings
5874  *   - DP transcoding bits
5875  *   - transcoder
5876  */
5877 static void ilk_pch_enable(const struct intel_atomic_state *state,
5878 			   const struct intel_crtc_state *crtc_state)
5879 {
5880 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5881 	struct drm_device *dev = crtc->base.dev;
5882 	struct drm_i915_private *dev_priv = to_i915(dev);
5883 	enum pipe pipe = crtc->pipe;
5884 	u32 temp;
5885 
5886 	assert_pch_transcoder_disabled(dev_priv, pipe);
5887 
5888 	if (IS_IVYBRIDGE(dev_priv))
5889 		ivb_update_fdi_bc_bifurcation(crtc_state);
5890 
5891 	/* Write the TU size bits before fdi link training, so that error
5892 	 * detection works. */
5893 	intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5894 		       intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5895 
5896 	/* For PCH output, training FDI link */
5897 	dev_priv->display.fdi_link_train(crtc, crtc_state);
5898 
5899 	/* We need to program the right clock selection before writing the pixel
5900 	 * mutliplier into the DPLL. */
5901 	if (HAS_PCH_CPT(dev_priv)) {
5902 		u32 sel;
5903 
5904 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5905 		temp |= TRANS_DPLL_ENABLE(pipe);
5906 		sel = TRANS_DPLLB_SEL(pipe);
5907 		if (crtc_state->shared_dpll ==
5908 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5909 			temp |= sel;
5910 		else
5911 			temp &= ~sel;
5912 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
5913 	}
5914 
5915 	/* XXX: pch pll's can be enabled any time before we enable the PCH
5916 	 * transcoder, and we actually should do this to not upset any PCH
5917 	 * transcoder that already use the clock when we share it.
5918 	 *
5919 	 * Note that enable_shared_dpll tries to do the right thing, but
5920 	 * get_shared_dpll unconditionally resets the pll - we need that to have
5921 	 * the right LVDS enable sequence. */
5922 	intel_enable_shared_dpll(crtc_state);
5923 
5924 	/* set transcoder timing, panel must allow it */
5925 	assert_panel_unlocked(dev_priv, pipe);
5926 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
5927 
5928 	intel_fdi_normal_train(crtc);
5929 
5930 	/* For PCH DP, enable TRANS_DP_CTL */
5931 	if (HAS_PCH_CPT(dev_priv) &&
5932 	    intel_crtc_has_dp_encoder(crtc_state)) {
5933 		const struct drm_display_mode *adjusted_mode =
5934 			&crtc_state->hw.adjusted_mode;
5935 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5936 		i915_reg_t reg = TRANS_DP_CTL(pipe);
5937 		enum port port;
5938 
5939 		temp = intel_de_read(dev_priv, reg);
5940 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
5941 			  TRANS_DP_SYNC_MASK |
5942 			  TRANS_DP_BPC_MASK);
5943 		temp |= TRANS_DP_OUTPUT_ENABLE;
5944 		temp |= bpc << 9; /* same format but at 11:9 */
5945 
5946 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5947 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5948 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5949 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5950 
5951 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5952 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
5953 		temp |= TRANS_DP_PORT_SEL(port);
5954 
5955 		intel_de_write(dev_priv, reg, temp);
5956 	}
5957 
5958 	ilk_enable_pch_transcoder(crtc_state);
5959 }
5960 
5961 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
5962 {
5963 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5964 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5965 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5966 
5967 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5968 
5969 	lpt_program_iclkip(crtc_state);
5970 
5971 	/* Set transcoder timing. */
5972 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
5973 
5974 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5975 }
5976 
5977 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5978 			       enum pipe pipe)
5979 {
5980 	i915_reg_t dslreg = PIPEDSL(pipe);
5981 	u32 temp;
5982 
5983 	temp = intel_de_read(dev_priv, dslreg);
5984 	udelay(500);
5985 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5986 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
5987 			drm_err(&dev_priv->drm,
5988 				"mode set failed: pipe %c stuck\n",
5989 				pipe_name(pipe));
5990 	}
5991 }
5992 
5993 /*
5994  * The hardware phase 0.0 refers to the center of the pixel.
5995  * We want to start from the top/left edge which is phase
5996  * -0.5. That matches how the hardware calculates the scaling
5997  * factors (from top-left of the first pixel to bottom-right
5998  * of the last pixel, as opposed to the pixel centers).
5999  *
6000  * For 4:2:0 subsampled chroma planes we obviously have to
6001  * adjust that so that the chroma sample position lands in
6002  * the right spot.
6003  *
6004  * Note that for packed YCbCr 4:2:2 formats there is no way to
6005  * control chroma siting. The hardware simply replicates the
6006  * chroma samples for both of the luma samples, and thus we don't
6007  * actually get the expected MPEG2 chroma siting convention :(
6008  * The same behaviour is observed on pre-SKL platforms as well.
6009  *
6010  * Theory behind the formula (note that we ignore sub-pixel
6011  * source coordinates):
6012  * s = source sample position
6013  * d = destination sample position
6014  *
6015  * Downscaling 4:1:
6016  * -0.5
6017  * | 0.0
6018  * | |     1.5 (initial phase)
6019  * | |     |
6020  * v v     v
6021  * | s | s | s | s |
6022  * |       d       |
6023  *
6024  * Upscaling 1:4:
6025  * -0.5
6026  * | -0.375 (initial phase)
6027  * | |     0.0
6028  * | |     |
6029  * v v     v
6030  * |       s       |
6031  * | d | d | d | d |
6032  */
6033 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
6034 {
6035 	int phase = -0x8000;
6036 	u16 trip = 0;
6037 
6038 	if (chroma_cosited)
6039 		phase += (sub - 1) * 0x8000 / sub;
6040 
6041 	phase += scale / (2 * sub);
6042 
6043 	/*
6044 	 * Hardware initial phase limited to [-0.5:1.5].
6045 	 * Since the max hardware scale factor is 3.0, we
6046 	 * should never actually excdeed 1.0 here.
6047 	 */
6048 	WARN_ON(phase < -0x8000 || phase > 0x18000);
6049 
6050 	if (phase < 0)
6051 		phase = 0x10000 + phase;
6052 	else
6053 		trip = PS_PHASE_TRIP;
6054 
6055 	return ((phase >> 2) & PS_PHASE_MASK) | trip;
6056 }
6057 
6058 #define SKL_MIN_SRC_W 8
6059 #define SKL_MAX_SRC_W 4096
6060 #define SKL_MIN_SRC_H 8
6061 #define SKL_MAX_SRC_H 4096
6062 #define SKL_MIN_DST_W 8
6063 #define SKL_MAX_DST_W 4096
6064 #define SKL_MIN_DST_H 8
6065 #define SKL_MAX_DST_H 4096
6066 #define ICL_MAX_SRC_W 5120
6067 #define ICL_MAX_SRC_H 4096
6068 #define ICL_MAX_DST_W 5120
6069 #define ICL_MAX_DST_H 4096
6070 #define SKL_MIN_YUV_420_SRC_W 16
6071 #define SKL_MIN_YUV_420_SRC_H 16
6072 
6073 static int
6074 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
6075 		  unsigned int scaler_user, int *scaler_id,
6076 		  int src_w, int src_h, int dst_w, int dst_h,
6077 		  const struct drm_format_info *format,
6078 		  u64 modifier, bool need_scaler)
6079 {
6080 	struct intel_crtc_scaler_state *scaler_state =
6081 		&crtc_state->scaler_state;
6082 	struct intel_crtc *intel_crtc =
6083 		to_intel_crtc(crtc_state->uapi.crtc);
6084 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
6085 	const struct drm_display_mode *adjusted_mode =
6086 		&crtc_state->hw.adjusted_mode;
6087 
6088 	/*
6089 	 * Src coordinates are already rotated by 270 degrees for
6090 	 * the 90/270 degree plane rotation cases (to match the
6091 	 * GTT mapping), hence no need to account for rotation here.
6092 	 */
6093 	if (src_w != dst_w || src_h != dst_h)
6094 		need_scaler = true;
6095 
6096 	/*
6097 	 * Scaling/fitting not supported in IF-ID mode in GEN9+
6098 	 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6099 	 * Once NV12 is enabled, handle it here while allocating scaler
6100 	 * for NV12.
6101 	 */
6102 	if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
6103 	    need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6104 		drm_dbg_kms(&dev_priv->drm,
6105 			    "Pipe/Plane scaling not supported with IF-ID mode\n");
6106 		return -EINVAL;
6107 	}
6108 
6109 	/*
6110 	 * if plane is being disabled or scaler is no more required or force detach
6111 	 *  - free scaler binded to this plane/crtc
6112 	 *  - in order to do this, update crtc->scaler_usage
6113 	 *
6114 	 * Here scaler state in crtc_state is set free so that
6115 	 * scaler can be assigned to other user. Actual register
6116 	 * update to free the scaler is done in plane/panel-fit programming.
6117 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6118 	 */
6119 	if (force_detach || !need_scaler) {
6120 		if (*scaler_id >= 0) {
6121 			scaler_state->scaler_users &= ~(1 << scaler_user);
6122 			scaler_state->scalers[*scaler_id].in_use = 0;
6123 
6124 			drm_dbg_kms(&dev_priv->drm,
6125 				    "scaler_user index %u.%u: "
6126 				    "Staged freeing scaler id %d scaler_users = 0x%x\n",
6127 				    intel_crtc->pipe, scaler_user, *scaler_id,
6128 				    scaler_state->scaler_users);
6129 			*scaler_id = -1;
6130 		}
6131 		return 0;
6132 	}
6133 
6134 	if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
6135 	    (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
6136 		drm_dbg_kms(&dev_priv->drm,
6137 			    "Planar YUV: src dimensions not met\n");
6138 		return -EINVAL;
6139 	}
6140 
6141 	/* range checks */
6142 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
6143 	    dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
6144 	    (INTEL_GEN(dev_priv) >= 11 &&
6145 	     (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6146 	      dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
6147 	    (INTEL_GEN(dev_priv) < 11 &&
6148 	     (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6149 	      dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H)))	{
6150 		drm_dbg_kms(&dev_priv->drm,
6151 			    "scaler_user index %u.%u: src %ux%u dst %ux%u "
6152 			    "size is out of scaler range\n",
6153 			    intel_crtc->pipe, scaler_user, src_w, src_h,
6154 			    dst_w, dst_h);
6155 		return -EINVAL;
6156 	}
6157 
6158 	/* mark this plane as a scaler user in crtc_state */
6159 	scaler_state->scaler_users |= (1 << scaler_user);
6160 	drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6161 		    "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6162 		    intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6163 		    scaler_state->scaler_users);
6164 
6165 	return 0;
6166 }
6167 
6168 static int skl_update_scaler_crtc(struct intel_crtc_state *crtc_state)
6169 {
6170 	const struct drm_display_mode *adjusted_mode =
6171 		&crtc_state->hw.adjusted_mode;
6172 	int width, height;
6173 
6174 	if (crtc_state->pch_pfit.enabled) {
6175 		width = drm_rect_width(&crtc_state->pch_pfit.dst);
6176 		height = drm_rect_height(&crtc_state->pch_pfit.dst);
6177 	} else {
6178 		width = adjusted_mode->crtc_hdisplay;
6179 		height = adjusted_mode->crtc_vdisplay;
6180 	}
6181 
6182 	return skl_update_scaler(crtc_state, !crtc_state->hw.active,
6183 				 SKL_CRTC_INDEX,
6184 				 &crtc_state->scaler_state.scaler_id,
6185 				 crtc_state->pipe_src_w, crtc_state->pipe_src_h,
6186 				 width, height, NULL, 0,
6187 				 crtc_state->pch_pfit.enabled);
6188 }
6189 
6190 /**
6191  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
6192  * @crtc_state: crtc's scaler state
6193  * @plane_state: atomic plane state to update
6194  *
6195  * Return
6196  *     0 - scaler_usage updated successfully
6197  *    error - requested scaling cannot be supported or other error condition
6198  */
6199 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6200 				   struct intel_plane_state *plane_state)
6201 {
6202 	struct intel_plane *intel_plane =
6203 		to_intel_plane(plane_state->uapi.plane);
6204 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
6205 	struct drm_framebuffer *fb = plane_state->hw.fb;
6206 	int ret;
6207 	bool force_detach = !fb || !plane_state->uapi.visible;
6208 	bool need_scaler = false;
6209 
6210 	/* Pre-gen11 and SDR planes always need a scaler for planar formats. */
6211 	if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
6212 	    fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
6213 		need_scaler = true;
6214 
6215 	ret = skl_update_scaler(crtc_state, force_detach,
6216 				drm_plane_index(&intel_plane->base),
6217 				&plane_state->scaler_id,
6218 				drm_rect_width(&plane_state->uapi.src) >> 16,
6219 				drm_rect_height(&plane_state->uapi.src) >> 16,
6220 				drm_rect_width(&plane_state->uapi.dst),
6221 				drm_rect_height(&plane_state->uapi.dst),
6222 				fb ? fb->format : NULL,
6223 				fb ? fb->modifier : 0,
6224 				need_scaler);
6225 
6226 	if (ret || plane_state->scaler_id < 0)
6227 		return ret;
6228 
6229 	/* check colorkey */
6230 	if (plane_state->ckey.flags) {
6231 		drm_dbg_kms(&dev_priv->drm,
6232 			    "[PLANE:%d:%s] scaling with color key not allowed",
6233 			    intel_plane->base.base.id,
6234 			    intel_plane->base.name);
6235 		return -EINVAL;
6236 	}
6237 
6238 	/* Check src format */
6239 	switch (fb->format->format) {
6240 	case DRM_FORMAT_RGB565:
6241 	case DRM_FORMAT_XBGR8888:
6242 	case DRM_FORMAT_XRGB8888:
6243 	case DRM_FORMAT_ABGR8888:
6244 	case DRM_FORMAT_ARGB8888:
6245 	case DRM_FORMAT_XRGB2101010:
6246 	case DRM_FORMAT_XBGR2101010:
6247 	case DRM_FORMAT_ARGB2101010:
6248 	case DRM_FORMAT_ABGR2101010:
6249 	case DRM_FORMAT_YUYV:
6250 	case DRM_FORMAT_YVYU:
6251 	case DRM_FORMAT_UYVY:
6252 	case DRM_FORMAT_VYUY:
6253 	case DRM_FORMAT_NV12:
6254 	case DRM_FORMAT_XYUV8888:
6255 	case DRM_FORMAT_P010:
6256 	case DRM_FORMAT_P012:
6257 	case DRM_FORMAT_P016:
6258 	case DRM_FORMAT_Y210:
6259 	case DRM_FORMAT_Y212:
6260 	case DRM_FORMAT_Y216:
6261 	case DRM_FORMAT_XVYU2101010:
6262 	case DRM_FORMAT_XVYU12_16161616:
6263 	case DRM_FORMAT_XVYU16161616:
6264 		break;
6265 	case DRM_FORMAT_XBGR16161616F:
6266 	case DRM_FORMAT_ABGR16161616F:
6267 	case DRM_FORMAT_XRGB16161616F:
6268 	case DRM_FORMAT_ARGB16161616F:
6269 		if (INTEL_GEN(dev_priv) >= 11)
6270 			break;
6271 		fallthrough;
6272 	default:
6273 		drm_dbg_kms(&dev_priv->drm,
6274 			    "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6275 			    intel_plane->base.base.id, intel_plane->base.name,
6276 			    fb->base.id, fb->format->format);
6277 		return -EINVAL;
6278 	}
6279 
6280 	return 0;
6281 }
6282 
6283 void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
6284 {
6285 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6286 	int i;
6287 
6288 	for (i = 0; i < crtc->num_scalers; i++)
6289 		skl_detach_scaler(crtc, i);
6290 }
6291 
6292 static int cnl_coef_tap(int i)
6293 {
6294 	return i % 7;
6295 }
6296 
6297 static u16 cnl_nearest_filter_coef(int t)
6298 {
6299 	return t == 3 ? 0x0800 : 0x3000;
6300 }
6301 
6302 /*
6303  *  Theory behind setting nearest-neighbor integer scaling:
6304  *
6305  *  17 phase of 7 taps requires 119 coefficients in 60 dwords per set.
6306  *  The letter represents the filter tap (D is the center tap) and the number
6307  *  represents the coefficient set for a phase (0-16).
6308  *
6309  *         +------------+------------------------+------------------------+
6310  *         |Index value | Data value coeffient 1 | Data value coeffient 2 |
6311  *         +------------+------------------------+------------------------+
6312  *         |   00h      |          B0            |          A0            |
6313  *         +------------+------------------------+------------------------+
6314  *         |   01h      |          D0            |          C0            |
6315  *         +------------+------------------------+------------------------+
6316  *         |   02h      |          F0            |          E0            |
6317  *         +------------+------------------------+------------------------+
6318  *         |   03h      |          A1            |          G0            |
6319  *         +------------+------------------------+------------------------+
6320  *         |   04h      |          C1            |          B1            |
6321  *         +------------+------------------------+------------------------+
6322  *         |   ...      |          ...           |          ...           |
6323  *         +------------+------------------------+------------------------+
6324  *         |   38h      |          B16           |          A16           |
6325  *         +------------+------------------------+------------------------+
6326  *         |   39h      |          D16           |          C16           |
6327  *         +------------+------------------------+------------------------+
6328  *         |   3Ah      |          F16           |          C16           |
6329  *         +------------+------------------------+------------------------+
6330  *         |   3Bh      |        Reserved        |          G16           |
6331  *         +------------+------------------------+------------------------+
6332  *
6333  *  To enable nearest-neighbor scaling:  program scaler coefficents with
6334  *  the center tap (Dxx) values set to 1 and all other values set to 0 as per
6335  *  SCALER_COEFFICIENT_FORMAT
6336  *
6337  */
6338 
6339 static void cnl_program_nearest_filter_coefs(struct drm_i915_private *dev_priv,
6340 					     enum pipe pipe, int id, int set)
6341 {
6342 	int i;
6343 
6344 	intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set),
6345 			  PS_COEE_INDEX_AUTO_INC);
6346 
6347 	for (i = 0; i < 17 * 7; i += 2) {
6348 		u32 tmp;
6349 		int t;
6350 
6351 		t = cnl_coef_tap(i);
6352 		tmp = cnl_nearest_filter_coef(t);
6353 
6354 		t = cnl_coef_tap(i + 1);
6355 		tmp |= cnl_nearest_filter_coef(t) << 16;
6356 
6357 		intel_de_write_fw(dev_priv, CNL_PS_COEF_DATA_SET(pipe, id, set),
6358 				  tmp);
6359 	}
6360 
6361 	intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set), 0);
6362 }
6363 
6364 inline u32 skl_scaler_get_filter_select(enum drm_scaling_filter filter, int set)
6365 {
6366 	if (filter == DRM_SCALING_FILTER_NEAREST_NEIGHBOR) {
6367 		return (PS_FILTER_PROGRAMMED |
6368 			PS_Y_VERT_FILTER_SELECT(set) |
6369 			PS_Y_HORZ_FILTER_SELECT(set) |
6370 			PS_UV_VERT_FILTER_SELECT(set) |
6371 			PS_UV_HORZ_FILTER_SELECT(set));
6372 	}
6373 
6374 	return PS_FILTER_MEDIUM;
6375 }
6376 
6377 void skl_scaler_setup_filter(struct drm_i915_private *dev_priv, enum pipe pipe,
6378 			     int id, int set, enum drm_scaling_filter filter)
6379 {
6380 	switch (filter) {
6381 	case DRM_SCALING_FILTER_DEFAULT:
6382 		break;
6383 	case DRM_SCALING_FILTER_NEAREST_NEIGHBOR:
6384 		cnl_program_nearest_filter_coefs(dev_priv, pipe, id, set);
6385 		break;
6386 	default:
6387 		MISSING_CASE(filter);
6388 	}
6389 }
6390 
6391 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
6392 {
6393 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6394 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6395 	const struct intel_crtc_scaler_state *scaler_state =
6396 		&crtc_state->scaler_state;
6397 	struct drm_rect src = {
6398 		.x2 = crtc_state->pipe_src_w << 16,
6399 		.y2 = crtc_state->pipe_src_h << 16,
6400 	};
6401 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6402 	u16 uv_rgb_hphase, uv_rgb_vphase;
6403 	enum pipe pipe = crtc->pipe;
6404 	int width = drm_rect_width(dst);
6405 	int height = drm_rect_height(dst);
6406 	int x = dst->x1;
6407 	int y = dst->y1;
6408 	int hscale, vscale;
6409 	unsigned long irqflags;
6410 	int id;
6411 	u32 ps_ctrl;
6412 
6413 	if (!crtc_state->pch_pfit.enabled)
6414 		return;
6415 
6416 	if (drm_WARN_ON(&dev_priv->drm,
6417 			crtc_state->scaler_state.scaler_id < 0))
6418 		return;
6419 
6420 	hscale = drm_rect_calc_hscale(&src, dst, 0, INT_MAX);
6421 	vscale = drm_rect_calc_vscale(&src, dst, 0, INT_MAX);
6422 
6423 	uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6424 	uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
6425 
6426 	id = scaler_state->scaler_id;
6427 
6428 	ps_ctrl = skl_scaler_get_filter_select(crtc_state->hw.scaling_filter, 0);
6429 	ps_ctrl |=  PS_SCALER_EN | scaler_state->scalers[id].mode;
6430 
6431 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6432 
6433 	skl_scaler_setup_filter(dev_priv, pipe, id, 0,
6434 				crtc_state->hw.scaling_filter);
6435 
6436 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), ps_ctrl);
6437 
6438 	intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6439 			  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6440 	intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6441 			  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6442 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
6443 			  x << 16 | y);
6444 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
6445 			  width << 16 | height);
6446 
6447 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6448 }
6449 
6450 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
6451 {
6452 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6453 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6454 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6455 	enum pipe pipe = crtc->pipe;
6456 	int width = drm_rect_width(dst);
6457 	int height = drm_rect_height(dst);
6458 	int x = dst->x1;
6459 	int y = dst->y1;
6460 
6461 	if (!crtc_state->pch_pfit.enabled)
6462 		return;
6463 
6464 	/* Force use of hard-coded filter coefficients
6465 	 * as some pre-programmed values are broken,
6466 	 * e.g. x201.
6467 	 */
6468 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6469 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6470 			       PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6471 	else
6472 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6473 			       PF_FILTER_MED_3x3);
6474 	intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
6475 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
6476 }
6477 
6478 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
6479 {
6480 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6481 	struct drm_device *dev = crtc->base.dev;
6482 	struct drm_i915_private *dev_priv = to_i915(dev);
6483 
6484 	if (!crtc_state->ips_enabled)
6485 		return;
6486 
6487 	/*
6488 	 * We can only enable IPS after we enable a plane and wait for a vblank
6489 	 * This function is called from post_plane_update, which is run after
6490 	 * a vblank wait.
6491 	 */
6492 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
6493 
6494 	if (IS_BROADWELL(dev_priv)) {
6495 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6496 							 IPS_ENABLE | IPS_PCODE_CONTROL));
6497 		/* Quoting Art Runyan: "its not safe to expect any particular
6498 		 * value in IPS_CTL bit 31 after enabling IPS through the
6499 		 * mailbox." Moreover, the mailbox may return a bogus state,
6500 		 * so we need to just enable it and continue on.
6501 		 */
6502 	} else {
6503 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
6504 		/* The bit only becomes 1 in the next vblank, so this wait here
6505 		 * is essentially intel_wait_for_vblank. If we don't have this
6506 		 * and don't wait for vblanks until the end of crtc_enable, then
6507 		 * the HW state readout code will complain that the expected
6508 		 * IPS_CTL value is not the one we read. */
6509 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
6510 			drm_err(&dev_priv->drm,
6511 				"Timed out waiting for IPS enable\n");
6512 	}
6513 }
6514 
6515 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
6516 {
6517 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6518 	struct drm_device *dev = crtc->base.dev;
6519 	struct drm_i915_private *dev_priv = to_i915(dev);
6520 
6521 	if (!crtc_state->ips_enabled)
6522 		return;
6523 
6524 	if (IS_BROADWELL(dev_priv)) {
6525 		drm_WARN_ON(dev,
6526 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
6527 		/*
6528 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
6529 		 * 42ms timeout value leads to occasional timeouts so use 100ms
6530 		 * instead.
6531 		 */
6532 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
6533 			drm_err(&dev_priv->drm,
6534 				"Timed out waiting for IPS disable\n");
6535 	} else {
6536 		intel_de_write(dev_priv, IPS_CTL, 0);
6537 		intel_de_posting_read(dev_priv, IPS_CTL);
6538 	}
6539 
6540 	/* We need to wait for a vblank before we can disable the plane. */
6541 	intel_wait_for_vblank(dev_priv, crtc->pipe);
6542 }
6543 
6544 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
6545 {
6546 	if (intel_crtc->overlay)
6547 		(void) intel_overlay_switch_off(intel_crtc->overlay);
6548 
6549 	/* Let userspace switch the overlay on again. In most cases userspace
6550 	 * has to recompute where to put it anyway.
6551 	 */
6552 }
6553 
6554 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6555 				       const struct intel_crtc_state *new_crtc_state)
6556 {
6557 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6558 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6559 
6560 	if (!old_crtc_state->ips_enabled)
6561 		return false;
6562 
6563 	if (needs_modeset(new_crtc_state))
6564 		return true;
6565 
6566 	/*
6567 	 * Workaround : Do not read or write the pipe palette/gamma data while
6568 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6569 	 *
6570 	 * Disable IPS before we program the LUT.
6571 	 */
6572 	if (IS_HASWELL(dev_priv) &&
6573 	    (new_crtc_state->uapi.color_mgmt_changed ||
6574 	     new_crtc_state->update_pipe) &&
6575 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6576 		return true;
6577 
6578 	return !new_crtc_state->ips_enabled;
6579 }
6580 
6581 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6582 				       const struct intel_crtc_state *new_crtc_state)
6583 {
6584 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6585 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6586 
6587 	if (!new_crtc_state->ips_enabled)
6588 		return false;
6589 
6590 	if (needs_modeset(new_crtc_state))
6591 		return true;
6592 
6593 	/*
6594 	 * Workaround : Do not read or write the pipe palette/gamma data while
6595 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6596 	 *
6597 	 * Re-enable IPS after the LUT has been programmed.
6598 	 */
6599 	if (IS_HASWELL(dev_priv) &&
6600 	    (new_crtc_state->uapi.color_mgmt_changed ||
6601 	     new_crtc_state->update_pipe) &&
6602 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6603 		return true;
6604 
6605 	/*
6606 	 * We can't read out IPS on broadwell, assume the worst and
6607 	 * forcibly enable IPS on the first fastset.
6608 	 */
6609 	if (new_crtc_state->update_pipe && old_crtc_state->inherited)
6610 		return true;
6611 
6612 	return !old_crtc_state->ips_enabled;
6613 }
6614 
6615 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
6616 {
6617 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6618 
6619 	if (!crtc_state->nv12_planes)
6620 		return false;
6621 
6622 	/* WA Display #0827: Gen9:all */
6623 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
6624 		return true;
6625 
6626 	return false;
6627 }
6628 
6629 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
6630 {
6631 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6632 
6633 	/* Wa_2006604312:icl,ehl */
6634 	if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
6635 		return true;
6636 
6637 	return false;
6638 }
6639 
6640 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6641 			    const struct intel_crtc_state *new_crtc_state)
6642 {
6643 	return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6644 		new_crtc_state->active_planes;
6645 }
6646 
6647 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6648 			     const struct intel_crtc_state *new_crtc_state)
6649 {
6650 	return old_crtc_state->active_planes &&
6651 		(!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6652 }
6653 
6654 static void intel_post_plane_update(struct intel_atomic_state *state,
6655 				    struct intel_crtc *crtc)
6656 {
6657 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6658 	const struct intel_crtc_state *old_crtc_state =
6659 		intel_atomic_get_old_crtc_state(state, crtc);
6660 	const struct intel_crtc_state *new_crtc_state =
6661 		intel_atomic_get_new_crtc_state(state, crtc);
6662 	enum pipe pipe = crtc->pipe;
6663 
6664 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
6665 
6666 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
6667 		intel_update_watermarks(crtc);
6668 
6669 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6670 		hsw_enable_ips(new_crtc_state);
6671 
6672 	intel_fbc_post_update(state, crtc);
6673 
6674 	if (needs_nv12_wa(old_crtc_state) &&
6675 	    !needs_nv12_wa(new_crtc_state))
6676 		skl_wa_827(dev_priv, pipe, false);
6677 
6678 	if (needs_scalerclk_wa(old_crtc_state) &&
6679 	    !needs_scalerclk_wa(new_crtc_state))
6680 		icl_wa_scalerclkgating(dev_priv, pipe, false);
6681 }
6682 
6683 static void skl_disable_async_flip_wa(struct intel_atomic_state *state,
6684 				      struct intel_crtc *crtc,
6685 				      const struct intel_crtc_state *new_crtc_state)
6686 {
6687 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6688 	struct intel_plane *plane;
6689 	struct intel_plane_state *new_plane_state;
6690 	int i;
6691 
6692 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
6693 		u32 update_mask = new_crtc_state->update_planes;
6694 		u32 plane_ctl, surf_addr;
6695 		enum plane_id plane_id;
6696 		unsigned long irqflags;
6697 		enum pipe pipe;
6698 
6699 		if (crtc->pipe != plane->pipe ||
6700 		    !(update_mask & BIT(plane->id)))
6701 			continue;
6702 
6703 		plane_id = plane->id;
6704 		pipe = plane->pipe;
6705 
6706 		spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6707 		plane_ctl = intel_de_read_fw(dev_priv, PLANE_CTL(pipe, plane_id));
6708 		surf_addr = intel_de_read_fw(dev_priv, PLANE_SURF(pipe, plane_id));
6709 
6710 		plane_ctl &= ~PLANE_CTL_ASYNC_FLIP;
6711 
6712 		intel_de_write_fw(dev_priv, PLANE_CTL(pipe, plane_id), plane_ctl);
6713 		intel_de_write_fw(dev_priv, PLANE_SURF(pipe, plane_id), surf_addr);
6714 		spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6715 	}
6716 
6717 	intel_wait_for_vblank(dev_priv, crtc->pipe);
6718 }
6719 
6720 static void intel_pre_plane_update(struct intel_atomic_state *state,
6721 				   struct intel_crtc *crtc)
6722 {
6723 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6724 	const struct intel_crtc_state *old_crtc_state =
6725 		intel_atomic_get_old_crtc_state(state, crtc);
6726 	const struct intel_crtc_state *new_crtc_state =
6727 		intel_atomic_get_new_crtc_state(state, crtc);
6728 	enum pipe pipe = crtc->pipe;
6729 
6730 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
6731 		hsw_disable_ips(old_crtc_state);
6732 
6733 	if (intel_fbc_pre_update(state, crtc))
6734 		intel_wait_for_vblank(dev_priv, pipe);
6735 
6736 	/* Display WA 827 */
6737 	if (!needs_nv12_wa(old_crtc_state) &&
6738 	    needs_nv12_wa(new_crtc_state))
6739 		skl_wa_827(dev_priv, pipe, true);
6740 
6741 	/* Wa_2006604312:icl,ehl */
6742 	if (!needs_scalerclk_wa(old_crtc_state) &&
6743 	    needs_scalerclk_wa(new_crtc_state))
6744 		icl_wa_scalerclkgating(dev_priv, pipe, true);
6745 
6746 	/*
6747 	 * Vblank time updates from the shadow to live plane control register
6748 	 * are blocked if the memory self-refresh mode is active at that
6749 	 * moment. So to make sure the plane gets truly disabled, disable
6750 	 * first the self-refresh mode. The self-refresh enable bit in turn
6751 	 * will be checked/applied by the HW only at the next frame start
6752 	 * event which is after the vblank start event, so we need to have a
6753 	 * wait-for-vblank between disabling the plane and the pipe.
6754 	 */
6755 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
6756 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
6757 		intel_wait_for_vblank(dev_priv, pipe);
6758 
6759 	/*
6760 	 * IVB workaround: must disable low power watermarks for at least
6761 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
6762 	 * when scaling is disabled.
6763 	 *
6764 	 * WaCxSRDisabledForSpriteScaling:ivb
6765 	 */
6766 	if (old_crtc_state->hw.active &&
6767 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
6768 		intel_wait_for_vblank(dev_priv, pipe);
6769 
6770 	/*
6771 	 * If we're doing a modeset we don't need to do any
6772 	 * pre-vblank watermark programming here.
6773 	 */
6774 	if (!needs_modeset(new_crtc_state)) {
6775 		/*
6776 		 * For platforms that support atomic watermarks, program the
6777 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6778 		 * will be the intermediate values that are safe for both pre- and
6779 		 * post- vblank; when vblank happens, the 'active' values will be set
6780 		 * to the final 'target' values and we'll do this again to get the
6781 		 * optimal watermarks.  For gen9+ platforms, the values we program here
6782 		 * will be the final target values which will get automatically latched
6783 		 * at vblank time; no further programming will be necessary.
6784 		 *
6785 		 * If a platform hasn't been transitioned to atomic watermarks yet,
6786 		 * we'll continue to update watermarks the old way, if flags tell
6787 		 * us to.
6788 		 */
6789 		if (dev_priv->display.initial_watermarks)
6790 			dev_priv->display.initial_watermarks(state, crtc);
6791 		else if (new_crtc_state->update_wm_pre)
6792 			intel_update_watermarks(crtc);
6793 	}
6794 
6795 	/*
6796 	 * Gen2 reports pipe underruns whenever all planes are disabled.
6797 	 * So disable underrun reporting before all the planes get disabled.
6798 	 *
6799 	 * We do this after .initial_watermarks() so that we have a
6800 	 * chance of catching underruns with the intermediate watermarks
6801 	 * vs. the old plane configuration.
6802 	 */
6803 	if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6804 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6805 
6806 	/*
6807 	 * WA for platforms where async address update enable bit
6808 	 * is double buffered and only latched at start of vblank.
6809 	 */
6810 	if (old_crtc_state->uapi.async_flip &&
6811 	    !new_crtc_state->uapi.async_flip &&
6812 	    IS_GEN_RANGE(dev_priv, 9, 10))
6813 		skl_disable_async_flip_wa(state, crtc, new_crtc_state);
6814 }
6815 
6816 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6817 				      struct intel_crtc *crtc)
6818 {
6819 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6820 	const struct intel_crtc_state *new_crtc_state =
6821 		intel_atomic_get_new_crtc_state(state, crtc);
6822 	unsigned int update_mask = new_crtc_state->update_planes;
6823 	const struct intel_plane_state *old_plane_state;
6824 	struct intel_plane *plane;
6825 	unsigned fb_bits = 0;
6826 	int i;
6827 
6828 	intel_crtc_dpms_overlay_disable(crtc);
6829 
6830 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6831 		if (crtc->pipe != plane->pipe ||
6832 		    !(update_mask & BIT(plane->id)))
6833 			continue;
6834 
6835 		intel_disable_plane(plane, new_crtc_state);
6836 
6837 		if (old_plane_state->uapi.visible)
6838 			fb_bits |= plane->frontbuffer_bit;
6839 	}
6840 
6841 	intel_frontbuffer_flip(dev_priv, fb_bits);
6842 }
6843 
6844 /*
6845  * intel_connector_primary_encoder - get the primary encoder for a connector
6846  * @connector: connector for which to return the encoder
6847  *
6848  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6849  * all connectors to their encoder, except for DP-MST connectors which have
6850  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6851  * pointed to by as many DP-MST connectors as there are pipes.
6852  */
6853 static struct intel_encoder *
6854 intel_connector_primary_encoder(struct intel_connector *connector)
6855 {
6856 	struct intel_encoder *encoder;
6857 
6858 	if (connector->mst_port)
6859 		return &dp_to_dig_port(connector->mst_port)->base;
6860 
6861 	encoder = intel_attached_encoder(connector);
6862 	drm_WARN_ON(connector->base.dev, !encoder);
6863 
6864 	return encoder;
6865 }
6866 
6867 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6868 {
6869 	struct drm_connector_state *new_conn_state;
6870 	struct drm_connector *connector;
6871 	int i;
6872 
6873 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6874 					i) {
6875 		struct intel_connector *intel_connector;
6876 		struct intel_encoder *encoder;
6877 		struct intel_crtc *crtc;
6878 
6879 		if (!intel_connector_needs_modeset(state, connector))
6880 			continue;
6881 
6882 		intel_connector = to_intel_connector(connector);
6883 		encoder = intel_connector_primary_encoder(intel_connector);
6884 		if (!encoder->update_prepare)
6885 			continue;
6886 
6887 		crtc = new_conn_state->crtc ?
6888 			to_intel_crtc(new_conn_state->crtc) : NULL;
6889 		encoder->update_prepare(state, encoder, crtc);
6890 	}
6891 }
6892 
6893 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6894 {
6895 	struct drm_connector_state *new_conn_state;
6896 	struct drm_connector *connector;
6897 	int i;
6898 
6899 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6900 					i) {
6901 		struct intel_connector *intel_connector;
6902 		struct intel_encoder *encoder;
6903 		struct intel_crtc *crtc;
6904 
6905 		if (!intel_connector_needs_modeset(state, connector))
6906 			continue;
6907 
6908 		intel_connector = to_intel_connector(connector);
6909 		encoder = intel_connector_primary_encoder(intel_connector);
6910 		if (!encoder->update_complete)
6911 			continue;
6912 
6913 		crtc = new_conn_state->crtc ?
6914 			to_intel_crtc(new_conn_state->crtc) : NULL;
6915 		encoder->update_complete(state, encoder, crtc);
6916 	}
6917 }
6918 
6919 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6920 					  struct intel_crtc *crtc)
6921 {
6922 	const struct intel_crtc_state *crtc_state =
6923 		intel_atomic_get_new_crtc_state(state, crtc);
6924 	const struct drm_connector_state *conn_state;
6925 	struct drm_connector *conn;
6926 	int i;
6927 
6928 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6929 		struct intel_encoder *encoder =
6930 			to_intel_encoder(conn_state->best_encoder);
6931 
6932 		if (conn_state->crtc != &crtc->base)
6933 			continue;
6934 
6935 		if (encoder->pre_pll_enable)
6936 			encoder->pre_pll_enable(state, encoder,
6937 						crtc_state, conn_state);
6938 	}
6939 }
6940 
6941 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6942 				      struct intel_crtc *crtc)
6943 {
6944 	const struct intel_crtc_state *crtc_state =
6945 		intel_atomic_get_new_crtc_state(state, crtc);
6946 	const struct drm_connector_state *conn_state;
6947 	struct drm_connector *conn;
6948 	int i;
6949 
6950 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6951 		struct intel_encoder *encoder =
6952 			to_intel_encoder(conn_state->best_encoder);
6953 
6954 		if (conn_state->crtc != &crtc->base)
6955 			continue;
6956 
6957 		if (encoder->pre_enable)
6958 			encoder->pre_enable(state, encoder,
6959 					    crtc_state, conn_state);
6960 	}
6961 }
6962 
6963 static void intel_encoders_enable(struct intel_atomic_state *state,
6964 				  struct intel_crtc *crtc)
6965 {
6966 	const struct intel_crtc_state *crtc_state =
6967 		intel_atomic_get_new_crtc_state(state, crtc);
6968 	const struct drm_connector_state *conn_state;
6969 	struct drm_connector *conn;
6970 	int i;
6971 
6972 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6973 		struct intel_encoder *encoder =
6974 			to_intel_encoder(conn_state->best_encoder);
6975 
6976 		if (conn_state->crtc != &crtc->base)
6977 			continue;
6978 
6979 		if (encoder->enable)
6980 			encoder->enable(state, encoder,
6981 					crtc_state, conn_state);
6982 		intel_opregion_notify_encoder(encoder, true);
6983 	}
6984 }
6985 
6986 static void intel_encoders_disable(struct intel_atomic_state *state,
6987 				   struct intel_crtc *crtc)
6988 {
6989 	const struct intel_crtc_state *old_crtc_state =
6990 		intel_atomic_get_old_crtc_state(state, crtc);
6991 	const struct drm_connector_state *old_conn_state;
6992 	struct drm_connector *conn;
6993 	int i;
6994 
6995 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6996 		struct intel_encoder *encoder =
6997 			to_intel_encoder(old_conn_state->best_encoder);
6998 
6999 		if (old_conn_state->crtc != &crtc->base)
7000 			continue;
7001 
7002 		intel_opregion_notify_encoder(encoder, false);
7003 		if (encoder->disable)
7004 			encoder->disable(state, encoder,
7005 					 old_crtc_state, old_conn_state);
7006 	}
7007 }
7008 
7009 static void intel_encoders_post_disable(struct intel_atomic_state *state,
7010 					struct intel_crtc *crtc)
7011 {
7012 	const struct intel_crtc_state *old_crtc_state =
7013 		intel_atomic_get_old_crtc_state(state, crtc);
7014 	const struct drm_connector_state *old_conn_state;
7015 	struct drm_connector *conn;
7016 	int i;
7017 
7018 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
7019 		struct intel_encoder *encoder =
7020 			to_intel_encoder(old_conn_state->best_encoder);
7021 
7022 		if (old_conn_state->crtc != &crtc->base)
7023 			continue;
7024 
7025 		if (encoder->post_disable)
7026 			encoder->post_disable(state, encoder,
7027 					      old_crtc_state, old_conn_state);
7028 	}
7029 }
7030 
7031 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
7032 					    struct intel_crtc *crtc)
7033 {
7034 	const struct intel_crtc_state *old_crtc_state =
7035 		intel_atomic_get_old_crtc_state(state, crtc);
7036 	const struct drm_connector_state *old_conn_state;
7037 	struct drm_connector *conn;
7038 	int i;
7039 
7040 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
7041 		struct intel_encoder *encoder =
7042 			to_intel_encoder(old_conn_state->best_encoder);
7043 
7044 		if (old_conn_state->crtc != &crtc->base)
7045 			continue;
7046 
7047 		if (encoder->post_pll_disable)
7048 			encoder->post_pll_disable(state, encoder,
7049 						  old_crtc_state, old_conn_state);
7050 	}
7051 }
7052 
7053 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
7054 				       struct intel_crtc *crtc)
7055 {
7056 	const struct intel_crtc_state *crtc_state =
7057 		intel_atomic_get_new_crtc_state(state, crtc);
7058 	const struct drm_connector_state *conn_state;
7059 	struct drm_connector *conn;
7060 	int i;
7061 
7062 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
7063 		struct intel_encoder *encoder =
7064 			to_intel_encoder(conn_state->best_encoder);
7065 
7066 		if (conn_state->crtc != &crtc->base)
7067 			continue;
7068 
7069 		if (encoder->update_pipe)
7070 			encoder->update_pipe(state, encoder,
7071 					     crtc_state, conn_state);
7072 	}
7073 }
7074 
7075 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
7076 {
7077 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7078 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
7079 
7080 	plane->disable_plane(plane, crtc_state);
7081 }
7082 
7083 static void ilk_crtc_enable(struct intel_atomic_state *state,
7084 			    struct intel_crtc *crtc)
7085 {
7086 	const struct intel_crtc_state *new_crtc_state =
7087 		intel_atomic_get_new_crtc_state(state, crtc);
7088 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7089 	enum pipe pipe = crtc->pipe;
7090 
7091 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7092 		return;
7093 
7094 	/*
7095 	 * Sometimes spurious CPU pipe underruns happen during FDI
7096 	 * training, at least with VGA+HDMI cloning. Suppress them.
7097 	 *
7098 	 * On ILK we get an occasional spurious CPU pipe underruns
7099 	 * between eDP port A enable and vdd enable. Also PCH port
7100 	 * enable seems to result in the occasional CPU pipe underrun.
7101 	 *
7102 	 * Spurious PCH underruns also occur during PCH enabling.
7103 	 */
7104 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7105 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7106 
7107 	if (new_crtc_state->has_pch_encoder)
7108 		intel_prepare_shared_dpll(new_crtc_state);
7109 
7110 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7111 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7112 
7113 	intel_set_transcoder_timings(new_crtc_state);
7114 	intel_set_pipe_src_size(new_crtc_state);
7115 
7116 	if (new_crtc_state->has_pch_encoder)
7117 		intel_cpu_transcoder_set_m_n(new_crtc_state,
7118 					     &new_crtc_state->fdi_m_n, NULL);
7119 
7120 	ilk_set_pipeconf(new_crtc_state);
7121 
7122 	crtc->active = true;
7123 
7124 	intel_encoders_pre_enable(state, crtc);
7125 
7126 	if (new_crtc_state->has_pch_encoder) {
7127 		/* Note: FDI PLL enabling _must_ be done before we enable the
7128 		 * cpu pipes, hence this is separate from all the other fdi/pch
7129 		 * enabling. */
7130 		ilk_fdi_pll_enable(new_crtc_state);
7131 	} else {
7132 		assert_fdi_tx_disabled(dev_priv, pipe);
7133 		assert_fdi_rx_disabled(dev_priv, pipe);
7134 	}
7135 
7136 	ilk_pfit_enable(new_crtc_state);
7137 
7138 	/*
7139 	 * On ILK+ LUT must be loaded before the pipe is running but with
7140 	 * clocks enabled
7141 	 */
7142 	intel_color_load_luts(new_crtc_state);
7143 	intel_color_commit(new_crtc_state);
7144 	/* update DSPCNTR to configure gamma for pipe bottom color */
7145 	intel_disable_primary_plane(new_crtc_state);
7146 
7147 	if (dev_priv->display.initial_watermarks)
7148 		dev_priv->display.initial_watermarks(state, crtc);
7149 	intel_enable_pipe(new_crtc_state);
7150 
7151 	if (new_crtc_state->has_pch_encoder)
7152 		ilk_pch_enable(state, new_crtc_state);
7153 
7154 	intel_crtc_vblank_on(new_crtc_state);
7155 
7156 	intel_encoders_enable(state, crtc);
7157 
7158 	if (HAS_PCH_CPT(dev_priv))
7159 		cpt_verify_modeset(dev_priv, pipe);
7160 
7161 	/*
7162 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
7163 	 * And a second vblank wait is needed at least on ILK with
7164 	 * some interlaced HDMI modes. Let's do the double wait always
7165 	 * in case there are more corner cases we don't know about.
7166 	 */
7167 	if (new_crtc_state->has_pch_encoder) {
7168 		intel_wait_for_vblank(dev_priv, pipe);
7169 		intel_wait_for_vblank(dev_priv, pipe);
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 /* IPS only exists on ULT machines and is tied to pipe A. */
7176 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
7177 {
7178 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
7179 }
7180 
7181 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
7182 					    enum pipe pipe, bool apply)
7183 {
7184 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
7185 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
7186 
7187 	if (apply)
7188 		val |= mask;
7189 	else
7190 		val &= ~mask;
7191 
7192 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
7193 }
7194 
7195 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
7196 {
7197 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7198 	enum pipe pipe = crtc->pipe;
7199 	u32 val;
7200 
7201 	val = MBUS_DBOX_A_CREDIT(2);
7202 
7203 	if (INTEL_GEN(dev_priv) >= 12) {
7204 		val |= MBUS_DBOX_BW_CREDIT(2);
7205 		val |= MBUS_DBOX_B_CREDIT(12);
7206 	} else {
7207 		val |= MBUS_DBOX_BW_CREDIT(1);
7208 		val |= MBUS_DBOX_B_CREDIT(8);
7209 	}
7210 
7211 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
7212 }
7213 
7214 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
7215 {
7216 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7217 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7218 
7219 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7220 		       HSW_LINETIME(crtc_state->linetime) |
7221 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
7222 }
7223 
7224 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7225 {
7226 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7227 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7228 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7229 	u32 val;
7230 
7231 	val = intel_de_read(dev_priv, reg);
7232 	val &= ~HSW_FRAME_START_DELAY_MASK;
7233 	val |= HSW_FRAME_START_DELAY(0);
7234 	intel_de_write(dev_priv, reg, val);
7235 }
7236 
7237 static void hsw_crtc_enable(struct intel_atomic_state *state,
7238 			    struct intel_crtc *crtc)
7239 {
7240 	const struct intel_crtc_state *new_crtc_state =
7241 		intel_atomic_get_new_crtc_state(state, crtc);
7242 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7243 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
7244 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
7245 	bool psl_clkgate_wa;
7246 
7247 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7248 		return;
7249 
7250 	intel_encoders_pre_pll_enable(state, crtc);
7251 
7252 	if (new_crtc_state->shared_dpll)
7253 		intel_enable_shared_dpll(new_crtc_state);
7254 
7255 	intel_encoders_pre_enable(state, crtc);
7256 
7257 	if (!transcoder_is_dsi(cpu_transcoder))
7258 		intel_set_transcoder_timings(new_crtc_state);
7259 
7260 	intel_set_pipe_src_size(new_crtc_state);
7261 
7262 	if (cpu_transcoder != TRANSCODER_EDP &&
7263 	    !transcoder_is_dsi(cpu_transcoder))
7264 		intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7265 			       new_crtc_state->pixel_multiplier - 1);
7266 
7267 	if (new_crtc_state->has_pch_encoder)
7268 		intel_cpu_transcoder_set_m_n(new_crtc_state,
7269 					     &new_crtc_state->fdi_m_n, NULL);
7270 
7271 	if (!transcoder_is_dsi(cpu_transcoder)) {
7272 		hsw_set_frame_start_delay(new_crtc_state);
7273 		hsw_set_pipeconf(new_crtc_state);
7274 	}
7275 
7276 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7277 		bdw_set_pipemisc(new_crtc_state);
7278 
7279 	crtc->active = true;
7280 
7281 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7282 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
7283 		new_crtc_state->pch_pfit.enabled;
7284 	if (psl_clkgate_wa)
7285 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7286 
7287 	if (INTEL_GEN(dev_priv) >= 9)
7288 		skl_pfit_enable(new_crtc_state);
7289 	else
7290 		ilk_pfit_enable(new_crtc_state);
7291 
7292 	/*
7293 	 * On ILK+ LUT must be loaded before the pipe is running but with
7294 	 * clocks enabled
7295 	 */
7296 	intel_color_load_luts(new_crtc_state);
7297 	intel_color_commit(new_crtc_state);
7298 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
7299 	if (INTEL_GEN(dev_priv) < 9)
7300 		intel_disable_primary_plane(new_crtc_state);
7301 
7302 	hsw_set_linetime_wm(new_crtc_state);
7303 
7304 	if (INTEL_GEN(dev_priv) >= 11)
7305 		icl_set_pipe_chicken(crtc);
7306 
7307 	if (dev_priv->display.initial_watermarks)
7308 		dev_priv->display.initial_watermarks(state, crtc);
7309 
7310 	if (INTEL_GEN(dev_priv) >= 11)
7311 		icl_pipe_mbus_enable(crtc);
7312 
7313 	intel_encoders_enable(state, crtc);
7314 
7315 	if (psl_clkgate_wa) {
7316 		intel_wait_for_vblank(dev_priv, pipe);
7317 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7318 	}
7319 
7320 	/* If we change the relative order between pipe/planes enabling, we need
7321 	 * to change the workaround. */
7322 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
7323 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
7324 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7325 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7326 	}
7327 }
7328 
7329 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7330 {
7331 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7332 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7333 	enum pipe pipe = crtc->pipe;
7334 
7335 	/* To avoid upsetting the power well on haswell only disable the pfit if
7336 	 * it's in use. The hw state code will make sure we get this right. */
7337 	if (!old_crtc_state->pch_pfit.enabled)
7338 		return;
7339 
7340 	intel_de_write(dev_priv, PF_CTL(pipe), 0);
7341 	intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7342 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
7343 }
7344 
7345 static void ilk_crtc_disable(struct intel_atomic_state *state,
7346 			     struct intel_crtc *crtc)
7347 {
7348 	const struct intel_crtc_state *old_crtc_state =
7349 		intel_atomic_get_old_crtc_state(state, crtc);
7350 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7351 	enum pipe pipe = crtc->pipe;
7352 
7353 	/*
7354 	 * Sometimes spurious CPU pipe underruns happen when the
7355 	 * pipe is already disabled, but FDI RX/TX is still enabled.
7356 	 * Happens at least with VGA+HDMI cloning. Suppress them.
7357 	 */
7358 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7359 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7360 
7361 	intel_encoders_disable(state, crtc);
7362 
7363 	intel_crtc_vblank_off(old_crtc_state);
7364 
7365 	intel_disable_pipe(old_crtc_state);
7366 
7367 	ilk_pfit_disable(old_crtc_state);
7368 
7369 	if (old_crtc_state->has_pch_encoder)
7370 		ilk_fdi_disable(crtc);
7371 
7372 	intel_encoders_post_disable(state, crtc);
7373 
7374 	if (old_crtc_state->has_pch_encoder) {
7375 		ilk_disable_pch_transcoder(dev_priv, pipe);
7376 
7377 		if (HAS_PCH_CPT(dev_priv)) {
7378 			i915_reg_t reg;
7379 			u32 temp;
7380 
7381 			/* disable TRANS_DP_CTL */
7382 			reg = TRANS_DP_CTL(pipe);
7383 			temp = intel_de_read(dev_priv, reg);
7384 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7385 				  TRANS_DP_PORT_SEL_MASK);
7386 			temp |= TRANS_DP_PORT_SEL_NONE;
7387 			intel_de_write(dev_priv, reg, temp);
7388 
7389 			/* disable DPLL_SEL */
7390 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
7391 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
7392 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
7393 		}
7394 
7395 		ilk_fdi_pll_disable(crtc);
7396 	}
7397 
7398 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7399 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7400 }
7401 
7402 static void hsw_crtc_disable(struct intel_atomic_state *state,
7403 			     struct intel_crtc *crtc)
7404 {
7405 	/*
7406 	 * FIXME collapse everything to one hook.
7407 	 * Need care with mst->ddi interactions.
7408 	 */
7409 	intel_encoders_disable(state, crtc);
7410 	intel_encoders_post_disable(state, crtc);
7411 }
7412 
7413 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
7414 {
7415 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7416 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7417 
7418 	if (!crtc_state->gmch_pfit.control)
7419 		return;
7420 
7421 	/*
7422 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
7423 	 * according to register description and PRM.
7424 	 */
7425 	drm_WARN_ON(&dev_priv->drm,
7426 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
7427 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
7428 
7429 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7430 		       crtc_state->gmch_pfit.pgm_ratios);
7431 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
7432 
7433 	/* Border color in case we don't scale up to the full screen. Black by
7434 	 * default, change to something else for debugging. */
7435 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
7436 }
7437 
7438 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7439 {
7440 	if (phy == PHY_NONE)
7441 		return false;
7442 	else if (IS_ROCKETLAKE(dev_priv))
7443 		return phy <= PHY_D;
7444 	else if (IS_JSL_EHL(dev_priv))
7445 		return phy <= PHY_C;
7446 	else if (INTEL_GEN(dev_priv) >= 11)
7447 		return phy <= PHY_B;
7448 	else
7449 		return false;
7450 }
7451 
7452 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7453 {
7454 	if (IS_ROCKETLAKE(dev_priv))
7455 		return false;
7456 	else if (INTEL_GEN(dev_priv) >= 12)
7457 		return phy >= PHY_D && phy <= PHY_I;
7458 	else if (INTEL_GEN(dev_priv) >= 11 && !IS_JSL_EHL(dev_priv))
7459 		return phy >= PHY_C && phy <= PHY_F;
7460 	else
7461 		return false;
7462 }
7463 
7464 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7465 {
7466 	if (IS_ROCKETLAKE(i915) && port >= PORT_TC1)
7467 		return PHY_C + port - PORT_TC1;
7468 	else if (IS_JSL_EHL(i915) && port == PORT_D)
7469 		return PHY_A;
7470 
7471 	return PHY_A + port - PORT_A;
7472 }
7473 
7474 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7475 {
7476 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
7477 		return TC_PORT_NONE;
7478 
7479 	if (INTEL_GEN(dev_priv) >= 12)
7480 		return TC_PORT_1 + port - PORT_TC1;
7481 	else
7482 		return TC_PORT_1 + port - PORT_C;
7483 }
7484 
7485 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
7486 {
7487 	switch (port) {
7488 	case PORT_A:
7489 		return POWER_DOMAIN_PORT_DDI_A_LANES;
7490 	case PORT_B:
7491 		return POWER_DOMAIN_PORT_DDI_B_LANES;
7492 	case PORT_C:
7493 		return POWER_DOMAIN_PORT_DDI_C_LANES;
7494 	case PORT_D:
7495 		return POWER_DOMAIN_PORT_DDI_D_LANES;
7496 	case PORT_E:
7497 		return POWER_DOMAIN_PORT_DDI_E_LANES;
7498 	case PORT_F:
7499 		return POWER_DOMAIN_PORT_DDI_F_LANES;
7500 	case PORT_G:
7501 		return POWER_DOMAIN_PORT_DDI_G_LANES;
7502 	case PORT_H:
7503 		return POWER_DOMAIN_PORT_DDI_H_LANES;
7504 	case PORT_I:
7505 		return POWER_DOMAIN_PORT_DDI_I_LANES;
7506 	default:
7507 		MISSING_CASE(port);
7508 		return POWER_DOMAIN_PORT_OTHER;
7509 	}
7510 }
7511 
7512 enum intel_display_power_domain
7513 intel_aux_power_domain(struct intel_digital_port *dig_port)
7514 {
7515 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
7516 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
7517 
7518 	if (intel_phy_is_tc(dev_priv, phy) &&
7519 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
7520 		switch (dig_port->aux_ch) {
7521 		case AUX_CH_C:
7522 			return POWER_DOMAIN_AUX_C_TBT;
7523 		case AUX_CH_D:
7524 			return POWER_DOMAIN_AUX_D_TBT;
7525 		case AUX_CH_E:
7526 			return POWER_DOMAIN_AUX_E_TBT;
7527 		case AUX_CH_F:
7528 			return POWER_DOMAIN_AUX_F_TBT;
7529 		case AUX_CH_G:
7530 			return POWER_DOMAIN_AUX_G_TBT;
7531 		case AUX_CH_H:
7532 			return POWER_DOMAIN_AUX_H_TBT;
7533 		case AUX_CH_I:
7534 			return POWER_DOMAIN_AUX_I_TBT;
7535 		default:
7536 			MISSING_CASE(dig_port->aux_ch);
7537 			return POWER_DOMAIN_AUX_C_TBT;
7538 		}
7539 	}
7540 
7541 	return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
7542 }
7543 
7544 /*
7545  * Converts aux_ch to power_domain without caring about TBT ports for that use
7546  * intel_aux_power_domain()
7547  */
7548 enum intel_display_power_domain
7549 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
7550 {
7551 	switch (aux_ch) {
7552 	case AUX_CH_A:
7553 		return POWER_DOMAIN_AUX_A;
7554 	case AUX_CH_B:
7555 		return POWER_DOMAIN_AUX_B;
7556 	case AUX_CH_C:
7557 		return POWER_DOMAIN_AUX_C;
7558 	case AUX_CH_D:
7559 		return POWER_DOMAIN_AUX_D;
7560 	case AUX_CH_E:
7561 		return POWER_DOMAIN_AUX_E;
7562 	case AUX_CH_F:
7563 		return POWER_DOMAIN_AUX_F;
7564 	case AUX_CH_G:
7565 		return POWER_DOMAIN_AUX_G;
7566 	case AUX_CH_H:
7567 		return POWER_DOMAIN_AUX_H;
7568 	case AUX_CH_I:
7569 		return POWER_DOMAIN_AUX_I;
7570 	default:
7571 		MISSING_CASE(aux_ch);
7572 		return POWER_DOMAIN_AUX_A;
7573 	}
7574 }
7575 
7576 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7577 {
7578 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7579 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7580 	struct drm_encoder *encoder;
7581 	enum pipe pipe = crtc->pipe;
7582 	u64 mask;
7583 	enum transcoder transcoder = crtc_state->cpu_transcoder;
7584 
7585 	if (!crtc_state->hw.active)
7586 		return 0;
7587 
7588 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7589 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
7590 	if (crtc_state->pch_pfit.enabled ||
7591 	    crtc_state->pch_pfit.force_thru)
7592 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
7593 
7594 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
7595 				  crtc_state->uapi.encoder_mask) {
7596 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7597 
7598 		mask |= BIT_ULL(intel_encoder->power_domain);
7599 	}
7600 
7601 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
7602 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
7603 
7604 	if (crtc_state->shared_dpll)
7605 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
7606 
7607 	return mask;
7608 }
7609 
7610 static u64
7611 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7612 {
7613 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7614 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7615 	enum intel_display_power_domain domain;
7616 	u64 domains, new_domains, old_domains;
7617 
7618 	old_domains = crtc->enabled_power_domains;
7619 	crtc->enabled_power_domains = new_domains =
7620 		get_crtc_power_domains(crtc_state);
7621 
7622 	domains = new_domains & ~old_domains;
7623 
7624 	for_each_power_domain(domain, domains)
7625 		intel_display_power_get(dev_priv, domain);
7626 
7627 	return old_domains & ~new_domains;
7628 }
7629 
7630 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
7631 				      u64 domains)
7632 {
7633 	enum intel_display_power_domain domain;
7634 
7635 	for_each_power_domain(domain, domains)
7636 		intel_display_power_put_unchecked(dev_priv, domain);
7637 }
7638 
7639 static void valleyview_crtc_enable(struct intel_atomic_state *state,
7640 				   struct intel_crtc *crtc)
7641 {
7642 	const struct intel_crtc_state *new_crtc_state =
7643 		intel_atomic_get_new_crtc_state(state, crtc);
7644 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7645 	enum pipe pipe = crtc->pipe;
7646 
7647 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7648 		return;
7649 
7650 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7651 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7652 
7653 	intel_set_transcoder_timings(new_crtc_state);
7654 	intel_set_pipe_src_size(new_crtc_state);
7655 
7656 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
7657 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7658 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
7659 	}
7660 
7661 	i9xx_set_pipeconf(new_crtc_state);
7662 
7663 	crtc->active = true;
7664 
7665 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7666 
7667 	intel_encoders_pre_pll_enable(state, crtc);
7668 
7669 	if (IS_CHERRYVIEW(dev_priv)) {
7670 		chv_prepare_pll(crtc, new_crtc_state);
7671 		chv_enable_pll(crtc, new_crtc_state);
7672 	} else {
7673 		vlv_prepare_pll(crtc, new_crtc_state);
7674 		vlv_enable_pll(crtc, new_crtc_state);
7675 	}
7676 
7677 	intel_encoders_pre_enable(state, crtc);
7678 
7679 	i9xx_pfit_enable(new_crtc_state);
7680 
7681 	intel_color_load_luts(new_crtc_state);
7682 	intel_color_commit(new_crtc_state);
7683 	/* update DSPCNTR to configure gamma for pipe bottom color */
7684 	intel_disable_primary_plane(new_crtc_state);
7685 
7686 	dev_priv->display.initial_watermarks(state, crtc);
7687 	intel_enable_pipe(new_crtc_state);
7688 
7689 	intel_crtc_vblank_on(new_crtc_state);
7690 
7691 	intel_encoders_enable(state, crtc);
7692 }
7693 
7694 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
7695 {
7696 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7697 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7698 
7699 	intel_de_write(dev_priv, FP0(crtc->pipe),
7700 		       crtc_state->dpll_hw_state.fp0);
7701 	intel_de_write(dev_priv, FP1(crtc->pipe),
7702 		       crtc_state->dpll_hw_state.fp1);
7703 }
7704 
7705 static void i9xx_crtc_enable(struct intel_atomic_state *state,
7706 			     struct intel_crtc *crtc)
7707 {
7708 	const struct intel_crtc_state *new_crtc_state =
7709 		intel_atomic_get_new_crtc_state(state, crtc);
7710 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7711 	enum pipe pipe = crtc->pipe;
7712 
7713 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7714 		return;
7715 
7716 	i9xx_set_pll_dividers(new_crtc_state);
7717 
7718 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7719 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7720 
7721 	intel_set_transcoder_timings(new_crtc_state);
7722 	intel_set_pipe_src_size(new_crtc_state);
7723 
7724 	i9xx_set_pipeconf(new_crtc_state);
7725 
7726 	crtc->active = true;
7727 
7728 	if (!IS_GEN(dev_priv, 2))
7729 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7730 
7731 	intel_encoders_pre_enable(state, crtc);
7732 
7733 	i9xx_enable_pll(crtc, new_crtc_state);
7734 
7735 	i9xx_pfit_enable(new_crtc_state);
7736 
7737 	intel_color_load_luts(new_crtc_state);
7738 	intel_color_commit(new_crtc_state);
7739 	/* update DSPCNTR to configure gamma for pipe bottom color */
7740 	intel_disable_primary_plane(new_crtc_state);
7741 
7742 	if (dev_priv->display.initial_watermarks)
7743 		dev_priv->display.initial_watermarks(state, crtc);
7744 	else
7745 		intel_update_watermarks(crtc);
7746 	intel_enable_pipe(new_crtc_state);
7747 
7748 	intel_crtc_vblank_on(new_crtc_state);
7749 
7750 	intel_encoders_enable(state, crtc);
7751 
7752 	/* prevents spurious underruns */
7753 	if (IS_GEN(dev_priv, 2))
7754 		intel_wait_for_vblank(dev_priv, pipe);
7755 }
7756 
7757 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7758 {
7759 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7760 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7761 
7762 	if (!old_crtc_state->gmch_pfit.control)
7763 		return;
7764 
7765 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7766 
7767 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
7768 		    intel_de_read(dev_priv, PFIT_CONTROL));
7769 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
7770 }
7771 
7772 static void i9xx_crtc_disable(struct intel_atomic_state *state,
7773 			      struct intel_crtc *crtc)
7774 {
7775 	struct intel_crtc_state *old_crtc_state =
7776 		intel_atomic_get_old_crtc_state(state, crtc);
7777 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7778 	enum pipe pipe = crtc->pipe;
7779 
7780 	/*
7781 	 * On gen2 planes are double buffered but the pipe isn't, so we must
7782 	 * wait for planes to fully turn off before disabling the pipe.
7783 	 */
7784 	if (IS_GEN(dev_priv, 2))
7785 		intel_wait_for_vblank(dev_priv, pipe);
7786 
7787 	intel_encoders_disable(state, crtc);
7788 
7789 	intel_crtc_vblank_off(old_crtc_state);
7790 
7791 	intel_disable_pipe(old_crtc_state);
7792 
7793 	i9xx_pfit_disable(old_crtc_state);
7794 
7795 	intel_encoders_post_disable(state, crtc);
7796 
7797 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7798 		if (IS_CHERRYVIEW(dev_priv))
7799 			chv_disable_pll(dev_priv, pipe);
7800 		else if (IS_VALLEYVIEW(dev_priv))
7801 			vlv_disable_pll(dev_priv, pipe);
7802 		else
7803 			i9xx_disable_pll(old_crtc_state);
7804 	}
7805 
7806 	intel_encoders_post_pll_disable(state, crtc);
7807 
7808 	if (!IS_GEN(dev_priv, 2))
7809 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7810 
7811 	if (!dev_priv->display.initial_watermarks)
7812 		intel_update_watermarks(crtc);
7813 
7814 	/* clock the pipe down to 640x480@60 to potentially save power */
7815 	if (IS_I830(dev_priv))
7816 		i830_enable_pipe(dev_priv, pipe);
7817 }
7818 
7819 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
7820 					struct drm_modeset_acquire_ctx *ctx)
7821 {
7822 	struct intel_encoder *encoder;
7823 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7824 	struct intel_bw_state *bw_state =
7825 		to_intel_bw_state(dev_priv->bw_obj.state);
7826 	struct intel_cdclk_state *cdclk_state =
7827 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
7828 	struct intel_dbuf_state *dbuf_state =
7829 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
7830 	struct intel_crtc_state *crtc_state =
7831 		to_intel_crtc_state(crtc->base.state);
7832 	enum intel_display_power_domain domain;
7833 	struct intel_plane *plane;
7834 	struct drm_atomic_state *state;
7835 	struct intel_crtc_state *temp_crtc_state;
7836 	enum pipe pipe = crtc->pipe;
7837 	u64 domains;
7838 	int ret;
7839 
7840 	if (!crtc_state->hw.active)
7841 		return;
7842 
7843 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7844 		const struct intel_plane_state *plane_state =
7845 			to_intel_plane_state(plane->base.state);
7846 
7847 		if (plane_state->uapi.visible)
7848 			intel_plane_disable_noatomic(crtc, plane);
7849 	}
7850 
7851 	state = drm_atomic_state_alloc(&dev_priv->drm);
7852 	if (!state) {
7853 		drm_dbg_kms(&dev_priv->drm,
7854 			    "failed to disable [CRTC:%d:%s], out of memory",
7855 			    crtc->base.base.id, crtc->base.name);
7856 		return;
7857 	}
7858 
7859 	state->acquire_ctx = ctx;
7860 
7861 	/* Everything's already locked, -EDEADLK can't happen. */
7862 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7863 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
7864 
7865 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
7866 
7867 	dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
7868 
7869 	drm_atomic_state_put(state);
7870 
7871 	drm_dbg_kms(&dev_priv->drm,
7872 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7873 		    crtc->base.base.id, crtc->base.name);
7874 
7875 	crtc->active = false;
7876 	crtc->base.enabled = false;
7877 
7878 	drm_WARN_ON(&dev_priv->drm,
7879 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
7880 	crtc_state->uapi.active = false;
7881 	crtc_state->uapi.connector_mask = 0;
7882 	crtc_state->uapi.encoder_mask = 0;
7883 	intel_crtc_free_hw_state(crtc_state);
7884 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
7885 
7886 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
7887 		encoder->base.crtc = NULL;
7888 
7889 	intel_fbc_disable(crtc);
7890 	intel_update_watermarks(crtc);
7891 	intel_disable_shared_dpll(crtc_state);
7892 
7893 	domains = crtc->enabled_power_domains;
7894 	for_each_power_domain(domain, domains)
7895 		intel_display_power_put_unchecked(dev_priv, domain);
7896 	crtc->enabled_power_domains = 0;
7897 
7898 	dev_priv->active_pipes &= ~BIT(pipe);
7899 	cdclk_state->min_cdclk[pipe] = 0;
7900 	cdclk_state->min_voltage_level[pipe] = 0;
7901 	cdclk_state->active_pipes &= ~BIT(pipe);
7902 
7903 	dbuf_state->active_pipes &= ~BIT(pipe);
7904 
7905 	bw_state->data_rate[pipe] = 0;
7906 	bw_state->num_active_planes[pipe] = 0;
7907 }
7908 
7909 /*
7910  * turn all crtc's off, but do not adjust state
7911  * This has to be paired with a call to intel_modeset_setup_hw_state.
7912  */
7913 int intel_display_suspend(struct drm_device *dev)
7914 {
7915 	struct drm_i915_private *dev_priv = to_i915(dev);
7916 	struct drm_atomic_state *state;
7917 	int ret;
7918 
7919 	state = drm_atomic_helper_suspend(dev);
7920 	ret = PTR_ERR_OR_ZERO(state);
7921 	if (ret)
7922 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7923 			ret);
7924 	else
7925 		dev_priv->modeset_restore_state = state;
7926 	return ret;
7927 }
7928 
7929 void intel_encoder_destroy(struct drm_encoder *encoder)
7930 {
7931 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7932 
7933 	drm_encoder_cleanup(encoder);
7934 	kfree(intel_encoder);
7935 }
7936 
7937 /* Cross check the actual hw state with our own modeset state tracking (and it's
7938  * internal consistency). */
7939 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7940 					 struct drm_connector_state *conn_state)
7941 {
7942 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7943 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
7944 
7945 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7946 		    connector->base.base.id, connector->base.name);
7947 
7948 	if (connector->get_hw_state(connector)) {
7949 		struct intel_encoder *encoder = intel_attached_encoder(connector);
7950 
7951 		I915_STATE_WARN(!crtc_state,
7952 			 "connector enabled without attached crtc\n");
7953 
7954 		if (!crtc_state)
7955 			return;
7956 
7957 		I915_STATE_WARN(!crtc_state->hw.active,
7958 				"connector is active, but attached crtc isn't\n");
7959 
7960 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7961 			return;
7962 
7963 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7964 			"atomic encoder doesn't match attached encoder\n");
7965 
7966 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7967 			"attached encoder crtc differs from connector crtc\n");
7968 	} else {
7969 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7970 				"attached crtc is active, but connector isn't\n");
7971 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7972 			"best encoder set without crtc!\n");
7973 	}
7974 }
7975 
7976 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7977 {
7978 	if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
7979 		return crtc_state->fdi_lanes;
7980 
7981 	return 0;
7982 }
7983 
7984 static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7985 			       struct intel_crtc_state *pipe_config)
7986 {
7987 	struct drm_i915_private *dev_priv = to_i915(dev);
7988 	struct drm_atomic_state *state = pipe_config->uapi.state;
7989 	struct intel_crtc *other_crtc;
7990 	struct intel_crtc_state *other_crtc_state;
7991 
7992 	drm_dbg_kms(&dev_priv->drm,
7993 		    "checking fdi config on pipe %c, lanes %i\n",
7994 		    pipe_name(pipe), pipe_config->fdi_lanes);
7995 	if (pipe_config->fdi_lanes > 4) {
7996 		drm_dbg_kms(&dev_priv->drm,
7997 			    "invalid fdi lane config on pipe %c: %i lanes\n",
7998 			    pipe_name(pipe), pipe_config->fdi_lanes);
7999 		return -EINVAL;
8000 	}
8001 
8002 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
8003 		if (pipe_config->fdi_lanes > 2) {
8004 			drm_dbg_kms(&dev_priv->drm,
8005 				    "only 2 lanes on haswell, required: %i lanes\n",
8006 				    pipe_config->fdi_lanes);
8007 			return -EINVAL;
8008 		} else {
8009 			return 0;
8010 		}
8011 	}
8012 
8013 	if (INTEL_NUM_PIPES(dev_priv) == 2)
8014 		return 0;
8015 
8016 	/* Ivybridge 3 pipe is really complicated */
8017 	switch (pipe) {
8018 	case PIPE_A:
8019 		return 0;
8020 	case PIPE_B:
8021 		if (pipe_config->fdi_lanes <= 2)
8022 			return 0;
8023 
8024 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
8025 		other_crtc_state =
8026 			intel_atomic_get_crtc_state(state, other_crtc);
8027 		if (IS_ERR(other_crtc_state))
8028 			return PTR_ERR(other_crtc_state);
8029 
8030 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
8031 			drm_dbg_kms(&dev_priv->drm,
8032 				    "invalid shared fdi lane config on pipe %c: %i lanes\n",
8033 				    pipe_name(pipe), pipe_config->fdi_lanes);
8034 			return -EINVAL;
8035 		}
8036 		return 0;
8037 	case PIPE_C:
8038 		if (pipe_config->fdi_lanes > 2) {
8039 			drm_dbg_kms(&dev_priv->drm,
8040 				    "only 2 lanes on pipe %c: required %i lanes\n",
8041 				    pipe_name(pipe), pipe_config->fdi_lanes);
8042 			return -EINVAL;
8043 		}
8044 
8045 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
8046 		other_crtc_state =
8047 			intel_atomic_get_crtc_state(state, other_crtc);
8048 		if (IS_ERR(other_crtc_state))
8049 			return PTR_ERR(other_crtc_state);
8050 
8051 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
8052 			drm_dbg_kms(&dev_priv->drm,
8053 				    "fdi link B uses too many lanes to enable link C\n");
8054 			return -EINVAL;
8055 		}
8056 		return 0;
8057 	default:
8058 		BUG();
8059 	}
8060 }
8061 
8062 #define RETRY 1
8063 static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
8064 				  struct intel_crtc_state *pipe_config)
8065 {
8066 	struct drm_device *dev = intel_crtc->base.dev;
8067 	struct drm_i915_private *i915 = to_i915(dev);
8068 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
8069 	int lane, link_bw, fdi_dotclock, ret;
8070 	bool needs_recompute = false;
8071 
8072 retry:
8073 	/* FDI is a binary signal running at ~2.7GHz, encoding
8074 	 * each output octet as 10 bits. The actual frequency
8075 	 * is stored as a divider into a 100MHz clock, and the
8076 	 * mode pixel clock is stored in units of 1KHz.
8077 	 * Hence the bw of each lane in terms of the mode signal
8078 	 * is:
8079 	 */
8080 	link_bw = intel_fdi_link_freq(i915, pipe_config);
8081 
8082 	fdi_dotclock = adjusted_mode->crtc_clock;
8083 
8084 	lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
8085 				      pipe_config->pipe_bpp);
8086 
8087 	pipe_config->fdi_lanes = lane;
8088 
8089 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
8090 			       link_bw, &pipe_config->fdi_m_n, false, false);
8091 
8092 	ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
8093 	if (ret == -EDEADLK)
8094 		return ret;
8095 
8096 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
8097 		pipe_config->pipe_bpp -= 2*3;
8098 		drm_dbg_kms(&i915->drm,
8099 			    "fdi link bw constraint, reducing pipe bpp to %i\n",
8100 			    pipe_config->pipe_bpp);
8101 		needs_recompute = true;
8102 		pipe_config->bw_constrained = true;
8103 
8104 		goto retry;
8105 	}
8106 
8107 	if (needs_recompute)
8108 		return RETRY;
8109 
8110 	return ret;
8111 }
8112 
8113 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
8114 {
8115 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8116 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8117 
8118 	/* IPS only exists on ULT machines and is tied to pipe A. */
8119 	if (!hsw_crtc_supports_ips(crtc))
8120 		return false;
8121 
8122 	if (!dev_priv->params.enable_ips)
8123 		return false;
8124 
8125 	if (crtc_state->pipe_bpp > 24)
8126 		return false;
8127 
8128 	/*
8129 	 * We compare against max which means we must take
8130 	 * the increased cdclk requirement into account when
8131 	 * calculating the new cdclk.
8132 	 *
8133 	 * Should measure whether using a lower cdclk w/o IPS
8134 	 */
8135 	if (IS_BROADWELL(dev_priv) &&
8136 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
8137 		return false;
8138 
8139 	return true;
8140 }
8141 
8142 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
8143 {
8144 	struct drm_i915_private *dev_priv =
8145 		to_i915(crtc_state->uapi.crtc->dev);
8146 	struct intel_atomic_state *state =
8147 		to_intel_atomic_state(crtc_state->uapi.state);
8148 
8149 	crtc_state->ips_enabled = false;
8150 
8151 	if (!hsw_crtc_state_ips_capable(crtc_state))
8152 		return 0;
8153 
8154 	/*
8155 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
8156 	 * enabled and disabled dynamically based on package C states,
8157 	 * user space can't make reliable use of the CRCs, so let's just
8158 	 * completely disable it.
8159 	 */
8160 	if (crtc_state->crc_enabled)
8161 		return 0;
8162 
8163 	/* IPS should be fine as long as at least one plane is enabled. */
8164 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
8165 		return 0;
8166 
8167 	if (IS_BROADWELL(dev_priv)) {
8168 		const struct intel_cdclk_state *cdclk_state;
8169 
8170 		cdclk_state = intel_atomic_get_cdclk_state(state);
8171 		if (IS_ERR(cdclk_state))
8172 			return PTR_ERR(cdclk_state);
8173 
8174 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
8175 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
8176 			return 0;
8177 	}
8178 
8179 	crtc_state->ips_enabled = true;
8180 
8181 	return 0;
8182 }
8183 
8184 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
8185 {
8186 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8187 
8188 	/* GDG double wide on either pipe, otherwise pipe A only */
8189 	return INTEL_GEN(dev_priv) < 4 &&
8190 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
8191 }
8192 
8193 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
8194 {
8195 	u32 pixel_rate = crtc_state->hw.adjusted_mode.crtc_clock;
8196 	unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
8197 
8198 	/*
8199 	 * We only use IF-ID interlacing. If we ever use
8200 	 * PF-ID we'll need to adjust the pixel_rate here.
8201 	 */
8202 
8203 	if (!crtc_state->pch_pfit.enabled)
8204 		return pixel_rate;
8205 
8206 	pipe_w = crtc_state->pipe_src_w;
8207 	pipe_h = crtc_state->pipe_src_h;
8208 
8209 	pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
8210 	pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
8211 
8212 	if (pipe_w < pfit_w)
8213 		pipe_w = pfit_w;
8214 	if (pipe_h < pfit_h)
8215 		pipe_h = pfit_h;
8216 
8217 	if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
8218 			!pfit_w || !pfit_h))
8219 		return pixel_rate;
8220 
8221 	return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
8222 		       pfit_w * pfit_h);
8223 }
8224 
8225 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
8226 {
8227 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8228 
8229 	if (HAS_GMCH(dev_priv))
8230 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
8231 		crtc_state->pixel_rate =
8232 			crtc_state->hw.adjusted_mode.crtc_clock;
8233 	else
8234 		crtc_state->pixel_rate =
8235 			ilk_pipe_pixel_rate(crtc_state);
8236 }
8237 
8238 static int intel_crtc_compute_config(struct intel_crtc *crtc,
8239 				     struct intel_crtc_state *pipe_config)
8240 {
8241 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8242 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
8243 	int clock_limit = dev_priv->max_dotclk_freq;
8244 
8245 	if (INTEL_GEN(dev_priv) < 4) {
8246 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
8247 
8248 		/*
8249 		 * Enable double wide mode when the dot clock
8250 		 * is > 90% of the (display) core speed.
8251 		 */
8252 		if (intel_crtc_supports_double_wide(crtc) &&
8253 		    adjusted_mode->crtc_clock > clock_limit) {
8254 			clock_limit = dev_priv->max_dotclk_freq;
8255 			pipe_config->double_wide = true;
8256 		}
8257 	}
8258 
8259 	if (adjusted_mode->crtc_clock > clock_limit) {
8260 		drm_dbg_kms(&dev_priv->drm,
8261 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
8262 			    adjusted_mode->crtc_clock, clock_limit,
8263 			    yesno(pipe_config->double_wide));
8264 		return -EINVAL;
8265 	}
8266 
8267 	if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8268 	     pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
8269 	     pipe_config->hw.ctm) {
8270 		/*
8271 		 * There is only one pipe CSC unit per pipe, and we need that
8272 		 * for output conversion from RGB->YCBCR. So if CTM is already
8273 		 * applied we can't support YCBCR420 output.
8274 		 */
8275 		drm_dbg_kms(&dev_priv->drm,
8276 			    "YCBCR420 and CTM together are not possible\n");
8277 		return -EINVAL;
8278 	}
8279 
8280 	/*
8281 	 * Pipe horizontal size must be even in:
8282 	 * - DVO ganged mode
8283 	 * - LVDS dual channel mode
8284 	 * - Double wide pipe
8285 	 */
8286 	if (pipe_config->pipe_src_w & 1) {
8287 		if (pipe_config->double_wide) {
8288 			drm_dbg_kms(&dev_priv->drm,
8289 				    "Odd pipe source width not supported with double wide pipe\n");
8290 			return -EINVAL;
8291 		}
8292 
8293 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
8294 		    intel_is_dual_link_lvds(dev_priv)) {
8295 			drm_dbg_kms(&dev_priv->drm,
8296 				    "Odd pipe source width not supported with dual link LVDS\n");
8297 			return -EINVAL;
8298 		}
8299 	}
8300 
8301 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
8302 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8303 	 */
8304 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8305 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
8306 		return -EINVAL;
8307 
8308 	intel_crtc_compute_pixel_rate(pipe_config);
8309 
8310 	if (pipe_config->has_pch_encoder)
8311 		return ilk_fdi_compute_config(crtc, pipe_config);
8312 
8313 	return 0;
8314 }
8315 
8316 static void
8317 intel_reduce_m_n_ratio(u32 *num, u32 *den)
8318 {
8319 	while (*num > DATA_LINK_M_N_MASK ||
8320 	       *den > DATA_LINK_M_N_MASK) {
8321 		*num >>= 1;
8322 		*den >>= 1;
8323 	}
8324 }
8325 
8326 static void compute_m_n(unsigned int m, unsigned int n,
8327 			u32 *ret_m, u32 *ret_n,
8328 			bool constant_n)
8329 {
8330 	/*
8331 	 * Several DP dongles in particular seem to be fussy about
8332 	 * too large link M/N values. Give N value as 0x8000 that
8333 	 * should be acceptable by specific devices. 0x8000 is the
8334 	 * specified fixed N value for asynchronous clock mode,
8335 	 * which the devices expect also in synchronous clock mode.
8336 	 */
8337 	if (constant_n)
8338 		*ret_n = DP_LINK_CONSTANT_N_VALUE;
8339 	else
8340 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
8341 
8342 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
8343 	intel_reduce_m_n_ratio(ret_m, ret_n);
8344 }
8345 
8346 void
8347 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
8348 		       int pixel_clock, int link_clock,
8349 		       struct intel_link_m_n *m_n,
8350 		       bool constant_n, bool fec_enable)
8351 {
8352 	u32 data_clock = bits_per_pixel * pixel_clock;
8353 
8354 	if (fec_enable)
8355 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
8356 
8357 	m_n->tu = 64;
8358 	compute_m_n(data_clock,
8359 		    link_clock * nlanes * 8,
8360 		    &m_n->gmch_m, &m_n->gmch_n,
8361 		    constant_n);
8362 
8363 	compute_m_n(pixel_clock, link_clock,
8364 		    &m_n->link_m, &m_n->link_n,
8365 		    constant_n);
8366 }
8367 
8368 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8369 {
8370 	/*
8371 	 * There may be no VBT; and if the BIOS enabled SSC we can
8372 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
8373 	 * BIOS isn't using it, don't assume it will work even if the VBT
8374 	 * indicates as much.
8375 	 */
8376 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
8377 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8378 						       PCH_DREF_CONTROL) &
8379 			DREF_SSC1_ENABLE;
8380 
8381 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
8382 			drm_dbg_kms(&dev_priv->drm,
8383 				    "SSC %s by BIOS, overriding VBT which says %s\n",
8384 				    enableddisabled(bios_lvds_use_ssc),
8385 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
8386 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8387 		}
8388 	}
8389 }
8390 
8391 static bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
8392 {
8393 	if (dev_priv->params.panel_use_ssc >= 0)
8394 		return dev_priv->params.panel_use_ssc != 0;
8395 	return dev_priv->vbt.lvds_use_ssc
8396 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
8397 }
8398 
8399 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
8400 {
8401 	return (1 << dpll->n) << 16 | dpll->m2;
8402 }
8403 
8404 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
8405 {
8406 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
8407 }
8408 
8409 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
8410 				     struct intel_crtc_state *crtc_state,
8411 				     struct dpll *reduced_clock)
8412 {
8413 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8414 	u32 fp, fp2 = 0;
8415 
8416 	if (IS_PINEVIEW(dev_priv)) {
8417 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
8418 		if (reduced_clock)
8419 			fp2 = pnv_dpll_compute_fp(reduced_clock);
8420 	} else {
8421 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8422 		if (reduced_clock)
8423 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
8424 	}
8425 
8426 	crtc_state->dpll_hw_state.fp0 = fp;
8427 
8428 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8429 	    reduced_clock) {
8430 		crtc_state->dpll_hw_state.fp1 = fp2;
8431 	} else {
8432 		crtc_state->dpll_hw_state.fp1 = fp;
8433 	}
8434 }
8435 
8436 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8437 		pipe)
8438 {
8439 	u32 reg_val;
8440 
8441 	/*
8442 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8443 	 * and set it to a reasonable value instead.
8444 	 */
8445 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8446 	reg_val &= 0xffffff00;
8447 	reg_val |= 0x00000030;
8448 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8449 
8450 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8451 	reg_val &= 0x00ffffff;
8452 	reg_val |= 0x8c000000;
8453 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8454 
8455 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8456 	reg_val &= 0xffffff00;
8457 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8458 
8459 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8460 	reg_val &= 0x00ffffff;
8461 	reg_val |= 0xb0000000;
8462 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8463 }
8464 
8465 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8466 					 const struct intel_link_m_n *m_n)
8467 {
8468 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8469 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8470 	enum pipe pipe = crtc->pipe;
8471 
8472 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8473 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
8474 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8475 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8476 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
8477 }
8478 
8479 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8480 				 enum transcoder transcoder)
8481 {
8482 	if (IS_HASWELL(dev_priv))
8483 		return transcoder == TRANSCODER_EDP;
8484 
8485 	/*
8486 	 * Strictly speaking some registers are available before
8487 	 * gen7, but we only support DRRS on gen7+
8488 	 */
8489 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
8490 }
8491 
8492 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8493 					 const struct intel_link_m_n *m_n,
8494 					 const struct intel_link_m_n *m2_n2)
8495 {
8496 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8497 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8498 	enum pipe pipe = crtc->pipe;
8499 	enum transcoder transcoder = crtc_state->cpu_transcoder;
8500 
8501 	if (INTEL_GEN(dev_priv) >= 5) {
8502 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8503 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8504 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8505 			       m_n->gmch_n);
8506 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8507 			       m_n->link_m);
8508 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8509 			       m_n->link_n);
8510 		/*
8511 		 *  M2_N2 registers are set only if DRRS is supported
8512 		 * (to make sure the registers are not unnecessarily accessed).
8513 		 */
8514 		if (m2_n2 && crtc_state->has_drrs &&
8515 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
8516 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8517 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8518 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8519 				       m2_n2->gmch_n);
8520 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8521 				       m2_n2->link_m);
8522 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8523 				       m2_n2->link_n);
8524 		}
8525 	} else {
8526 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8527 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8528 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8529 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8530 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
8531 	}
8532 }
8533 
8534 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
8535 {
8536 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
8537 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
8538 
8539 	if (m_n == M1_N1) {
8540 		dp_m_n = &crtc_state->dp_m_n;
8541 		dp_m2_n2 = &crtc_state->dp_m2_n2;
8542 	} else if (m_n == M2_N2) {
8543 
8544 		/*
8545 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
8546 		 * needs to be programmed into M1_N1.
8547 		 */
8548 		dp_m_n = &crtc_state->dp_m2_n2;
8549 	} else {
8550 		drm_err(&i915->drm, "Unsupported divider value\n");
8551 		return;
8552 	}
8553 
8554 	if (crtc_state->has_pch_encoder)
8555 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
8556 	else
8557 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
8558 }
8559 
8560 static void vlv_compute_dpll(struct intel_crtc *crtc,
8561 			     struct intel_crtc_state *pipe_config)
8562 {
8563 	pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
8564 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8565 	if (crtc->pipe != PIPE_A)
8566 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8567 
8568 	/* DPLL not used with DSI, but still need the rest set up */
8569 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8570 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8571 			DPLL_EXT_BUFFER_ENABLE_VLV;
8572 
8573 	pipe_config->dpll_hw_state.dpll_md =
8574 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8575 }
8576 
8577 static void chv_compute_dpll(struct intel_crtc *crtc,
8578 			     struct intel_crtc_state *pipe_config)
8579 {
8580 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
8581 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8582 	if (crtc->pipe != PIPE_A)
8583 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8584 
8585 	/* DPLL not used with DSI, but still need the rest set up */
8586 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8587 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8588 
8589 	pipe_config->dpll_hw_state.dpll_md =
8590 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8591 }
8592 
8593 static void vlv_prepare_pll(struct intel_crtc *crtc,
8594 			    const struct intel_crtc_state *pipe_config)
8595 {
8596 	struct drm_device *dev = crtc->base.dev;
8597 	struct drm_i915_private *dev_priv = to_i915(dev);
8598 	enum pipe pipe = crtc->pipe;
8599 	u32 mdiv;
8600 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
8601 	u32 coreclk, reg_val;
8602 
8603 	/* Enable Refclk */
8604 	intel_de_write(dev_priv, DPLL(pipe),
8605 		       pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
8606 
8607 	/* No need to actually set up the DPLL with DSI */
8608 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8609 		return;
8610 
8611 	vlv_dpio_get(dev_priv);
8612 
8613 	bestn = pipe_config->dpll.n;
8614 	bestm1 = pipe_config->dpll.m1;
8615 	bestm2 = pipe_config->dpll.m2;
8616 	bestp1 = pipe_config->dpll.p1;
8617 	bestp2 = pipe_config->dpll.p2;
8618 
8619 	/* See eDP HDMI DPIO driver vbios notes doc */
8620 
8621 	/* PLL B needs special handling */
8622 	if (pipe == PIPE_B)
8623 		vlv_pllb_recal_opamp(dev_priv, pipe);
8624 
8625 	/* Set up Tx target for periodic Rcomp update */
8626 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
8627 
8628 	/* Disable target IRef on PLL */
8629 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
8630 	reg_val &= 0x00ffffff;
8631 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
8632 
8633 	/* Disable fast lock */
8634 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
8635 
8636 	/* Set idtafcrecal before PLL is enabled */
8637 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8638 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8639 	mdiv |= ((bestn << DPIO_N_SHIFT));
8640 	mdiv |= (1 << DPIO_K_SHIFT);
8641 
8642 	/*
8643 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8644 	 * but we don't support that).
8645 	 * Note: don't use the DAC post divider as it seems unstable.
8646 	 */
8647 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
8648 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8649 
8650 	mdiv |= DPIO_ENABLE_CALIBRATION;
8651 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8652 
8653 	/* Set HBR and RBR LPF coefficients */
8654 	if (pipe_config->port_clock == 162000 ||
8655 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8656 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
8657 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8658 				 0x009f0003);
8659 	else
8660 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8661 				 0x00d0000f);
8662 
8663 	if (intel_crtc_has_dp_encoder(pipe_config)) {
8664 		/* Use SSC source */
8665 		if (pipe == PIPE_A)
8666 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8667 					 0x0df40000);
8668 		else
8669 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8670 					 0x0df70000);
8671 	} else { /* HDMI or VGA */
8672 		/* Use bend source */
8673 		if (pipe == PIPE_A)
8674 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8675 					 0x0df70000);
8676 		else
8677 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8678 					 0x0df40000);
8679 	}
8680 
8681 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
8682 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
8683 	if (intel_crtc_has_dp_encoder(pipe_config))
8684 		coreclk |= 0x01000000;
8685 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
8686 
8687 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
8688 
8689 	vlv_dpio_put(dev_priv);
8690 }
8691 
8692 static void chv_prepare_pll(struct intel_crtc *crtc,
8693 			    const struct intel_crtc_state *pipe_config)
8694 {
8695 	struct drm_device *dev = crtc->base.dev;
8696 	struct drm_i915_private *dev_priv = to_i915(dev);
8697 	enum pipe pipe = crtc->pipe;
8698 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8699 	u32 loopfilter, tribuf_calcntr;
8700 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
8701 	u32 dpio_val;
8702 	int vco;
8703 
8704 	/* Enable Refclk and SSC */
8705 	intel_de_write(dev_priv, DPLL(pipe),
8706 		       pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8707 
8708 	/* No need to actually set up the DPLL with DSI */
8709 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8710 		return;
8711 
8712 	bestn = pipe_config->dpll.n;
8713 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8714 	bestm1 = pipe_config->dpll.m1;
8715 	bestm2 = pipe_config->dpll.m2 >> 22;
8716 	bestp1 = pipe_config->dpll.p1;
8717 	bestp2 = pipe_config->dpll.p2;
8718 	vco = pipe_config->dpll.vco;
8719 	dpio_val = 0;
8720 	loopfilter = 0;
8721 
8722 	vlv_dpio_get(dev_priv);
8723 
8724 	/* p1 and p2 divider */
8725 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8726 			5 << DPIO_CHV_S1_DIV_SHIFT |
8727 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8728 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8729 			1 << DPIO_CHV_K_DIV_SHIFT);
8730 
8731 	/* Feedback post-divider - m2 */
8732 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8733 
8734 	/* Feedback refclk divider - n and m1 */
8735 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8736 			DPIO_CHV_M1_DIV_BY_2 |
8737 			1 << DPIO_CHV_N_DIV_SHIFT);
8738 
8739 	/* M2 fraction division */
8740 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
8741 
8742 	/* M2 fraction division enable */
8743 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8744 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8745 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8746 	if (bestm2_frac)
8747 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8748 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
8749 
8750 	/* Program digital lock detect threshold */
8751 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8752 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8753 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8754 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8755 	if (!bestm2_frac)
8756 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8757 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8758 
8759 	/* Loop filter */
8760 	if (vco == 5400000) {
8761 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8762 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8763 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8764 		tribuf_calcntr = 0x9;
8765 	} else if (vco <= 6200000) {
8766 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8767 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8768 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8769 		tribuf_calcntr = 0x9;
8770 	} else if (vco <= 6480000) {
8771 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8772 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8773 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8774 		tribuf_calcntr = 0x8;
8775 	} else {
8776 		/* Not supported. Apply the same limits as in the max case */
8777 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8778 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8779 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8780 		tribuf_calcntr = 0;
8781 	}
8782 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8783 
8784 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
8785 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8786 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8787 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8788 
8789 	/* AFC Recal */
8790 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8791 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8792 			DPIO_AFC_RECAL);
8793 
8794 	vlv_dpio_put(dev_priv);
8795 }
8796 
8797 /**
8798  * vlv_force_pll_on - forcibly enable just the PLL
8799  * @dev_priv: i915 private structure
8800  * @pipe: pipe PLL to enable
8801  * @dpll: PLL configuration
8802  *
8803  * Enable the PLL for @pipe using the supplied @dpll config. To be used
8804  * in cases where we need the PLL enabled even when @pipe is not going to
8805  * be enabled.
8806  */
8807 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
8808 		     const struct dpll *dpll)
8809 {
8810 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
8811 	struct intel_crtc_state *pipe_config;
8812 
8813 	pipe_config = intel_crtc_state_alloc(crtc);
8814 	if (!pipe_config)
8815 		return -ENOMEM;
8816 
8817 	pipe_config->cpu_transcoder = (enum transcoder)pipe;
8818 	pipe_config->pixel_multiplier = 1;
8819 	pipe_config->dpll = *dpll;
8820 
8821 	if (IS_CHERRYVIEW(dev_priv)) {
8822 		chv_compute_dpll(crtc, pipe_config);
8823 		chv_prepare_pll(crtc, pipe_config);
8824 		chv_enable_pll(crtc, pipe_config);
8825 	} else {
8826 		vlv_compute_dpll(crtc, pipe_config);
8827 		vlv_prepare_pll(crtc, pipe_config);
8828 		vlv_enable_pll(crtc, pipe_config);
8829 	}
8830 
8831 	kfree(pipe_config);
8832 
8833 	return 0;
8834 }
8835 
8836 /**
8837  * vlv_force_pll_off - forcibly disable just the PLL
8838  * @dev_priv: i915 private structure
8839  * @pipe: pipe PLL to disable
8840  *
8841  * Disable the PLL for @pipe. To be used in cases where we need
8842  * the PLL enabled even when @pipe is not going to be enabled.
8843  */
8844 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
8845 {
8846 	if (IS_CHERRYVIEW(dev_priv))
8847 		chv_disable_pll(dev_priv, pipe);
8848 	else
8849 		vlv_disable_pll(dev_priv, pipe);
8850 }
8851 
8852 static void i9xx_compute_dpll(struct intel_crtc *crtc,
8853 			      struct intel_crtc_state *crtc_state,
8854 			      struct dpll *reduced_clock)
8855 {
8856 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8857 	u32 dpll;
8858 	struct dpll *clock = &crtc_state->dpll;
8859 
8860 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8861 
8862 	dpll = DPLL_VGA_MODE_DIS;
8863 
8864 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
8865 		dpll |= DPLLB_MODE_LVDS;
8866 	else
8867 		dpll |= DPLLB_MODE_DAC_SERIAL;
8868 
8869 	if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8870 	    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8871 		dpll |= (crtc_state->pixel_multiplier - 1)
8872 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
8873 	}
8874 
8875 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8876 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8877 		dpll |= DPLL_SDVO_HIGH_SPEED;
8878 
8879 	if (intel_crtc_has_dp_encoder(crtc_state))
8880 		dpll |= DPLL_SDVO_HIGH_SPEED;
8881 
8882 	/* compute bitmask from p1 value */
8883 	if (IS_PINEVIEW(dev_priv))
8884 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8885 	else {
8886 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8887 		if (IS_G4X(dev_priv) && reduced_clock)
8888 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8889 	}
8890 	switch (clock->p2) {
8891 	case 5:
8892 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8893 		break;
8894 	case 7:
8895 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8896 		break;
8897 	case 10:
8898 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8899 		break;
8900 	case 14:
8901 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8902 		break;
8903 	}
8904 	if (INTEL_GEN(dev_priv) >= 4)
8905 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8906 
8907 	if (crtc_state->sdvo_tv_clock)
8908 		dpll |= PLL_REF_INPUT_TVCLKINBC;
8909 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8910 		 intel_panel_use_ssc(dev_priv))
8911 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8912 	else
8913 		dpll |= PLL_REF_INPUT_DREFCLK;
8914 
8915 	dpll |= DPLL_VCO_ENABLE;
8916 	crtc_state->dpll_hw_state.dpll = dpll;
8917 
8918 	if (INTEL_GEN(dev_priv) >= 4) {
8919 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8920 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
8921 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
8922 	}
8923 }
8924 
8925 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8926 			      struct intel_crtc_state *crtc_state,
8927 			      struct dpll *reduced_clock)
8928 {
8929 	struct drm_device *dev = crtc->base.dev;
8930 	struct drm_i915_private *dev_priv = to_i915(dev);
8931 	u32 dpll;
8932 	struct dpll *clock = &crtc_state->dpll;
8933 
8934 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8935 
8936 	dpll = DPLL_VGA_MODE_DIS;
8937 
8938 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8939 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8940 	} else {
8941 		if (clock->p1 == 2)
8942 			dpll |= PLL_P1_DIVIDE_BY_TWO;
8943 		else
8944 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8945 		if (clock->p2 == 4)
8946 			dpll |= PLL_P2_DIVIDE_BY_4;
8947 	}
8948 
8949 	/*
8950 	 * Bspec:
8951 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8952 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8953 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8954 	 *  Enable) must be set to “1” in both the DPLL A Control Register
8955 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8956 	 *
8957 	 * For simplicity We simply keep both bits always enabled in
8958 	 * both DPLLS. The spec says we should disable the DVO 2X clock
8959 	 * when not needed, but this seems to work fine in practice.
8960 	 */
8961 	if (IS_I830(dev_priv) ||
8962 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8963 		dpll |= DPLL_DVO_2X_MODE;
8964 
8965 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8966 	    intel_panel_use_ssc(dev_priv))
8967 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8968 	else
8969 		dpll |= PLL_REF_INPUT_DREFCLK;
8970 
8971 	dpll |= DPLL_VCO_ENABLE;
8972 	crtc_state->dpll_hw_state.dpll = dpll;
8973 }
8974 
8975 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
8976 {
8977 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8978 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8979 	enum pipe pipe = crtc->pipe;
8980 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8981 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8982 	u32 crtc_vtotal, crtc_vblank_end;
8983 	int vsyncshift = 0;
8984 
8985 	/* We need to be careful not to changed the adjusted mode, for otherwise
8986 	 * the hw state checker will get angry at the mismatch. */
8987 	crtc_vtotal = adjusted_mode->crtc_vtotal;
8988 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8989 
8990 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8991 		/* the chip adds 2 halflines automatically */
8992 		crtc_vtotal -= 1;
8993 		crtc_vblank_end -= 1;
8994 
8995 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8996 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8997 		else
8998 			vsyncshift = adjusted_mode->crtc_hsync_start -
8999 				adjusted_mode->crtc_htotal / 2;
9000 		if (vsyncshift < 0)
9001 			vsyncshift += adjusted_mode->crtc_htotal;
9002 	}
9003 
9004 	if (INTEL_GEN(dev_priv) > 3)
9005 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
9006 		               vsyncshift);
9007 
9008 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
9009 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
9010 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
9011 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
9012 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
9013 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
9014 
9015 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
9016 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
9017 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
9018 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
9019 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
9020 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
9021 
9022 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
9023 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
9024 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
9025 	 * bits. */
9026 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
9027 	    (pipe == PIPE_B || pipe == PIPE_C))
9028 		intel_de_write(dev_priv, VTOTAL(pipe),
9029 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
9030 
9031 }
9032 
9033 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
9034 {
9035 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9036 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9037 	enum pipe pipe = crtc->pipe;
9038 
9039 	/* pipesrc controls the size that is scaled from, which should
9040 	 * always be the user's requested size.
9041 	 */
9042 	intel_de_write(dev_priv, PIPESRC(pipe),
9043 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
9044 }
9045 
9046 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
9047 {
9048 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
9049 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
9050 
9051 	if (IS_GEN(dev_priv, 2))
9052 		return false;
9053 
9054 	if (INTEL_GEN(dev_priv) >= 9 ||
9055 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
9056 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
9057 	else
9058 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
9059 }
9060 
9061 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
9062 					 struct intel_crtc_state *pipe_config)
9063 {
9064 	struct drm_device *dev = crtc->base.dev;
9065 	struct drm_i915_private *dev_priv = to_i915(dev);
9066 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
9067 	u32 tmp;
9068 
9069 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
9070 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
9071 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
9072 
9073 	if (!transcoder_is_dsi(cpu_transcoder)) {
9074 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
9075 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
9076 							(tmp & 0xffff) + 1;
9077 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
9078 						((tmp >> 16) & 0xffff) + 1;
9079 	}
9080 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
9081 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
9082 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
9083 
9084 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
9085 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
9086 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
9087 
9088 	if (!transcoder_is_dsi(cpu_transcoder)) {
9089 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
9090 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
9091 							(tmp & 0xffff) + 1;
9092 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
9093 						((tmp >> 16) & 0xffff) + 1;
9094 	}
9095 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
9096 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
9097 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
9098 
9099 	if (intel_pipe_is_interlaced(pipe_config)) {
9100 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
9101 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
9102 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
9103 	}
9104 }
9105 
9106 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
9107 				    struct intel_crtc_state *pipe_config)
9108 {
9109 	struct drm_device *dev = crtc->base.dev;
9110 	struct drm_i915_private *dev_priv = to_i915(dev);
9111 	u32 tmp;
9112 
9113 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
9114 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
9115 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
9116 
9117 	pipe_config->hw.mode.vdisplay = pipe_config->pipe_src_h;
9118 	pipe_config->hw.mode.hdisplay = pipe_config->pipe_src_w;
9119 }
9120 
9121 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
9122 				 struct intel_crtc_state *pipe_config)
9123 {
9124 	mode->hdisplay = pipe_config->hw.adjusted_mode.crtc_hdisplay;
9125 	mode->htotal = pipe_config->hw.adjusted_mode.crtc_htotal;
9126 	mode->hsync_start = pipe_config->hw.adjusted_mode.crtc_hsync_start;
9127 	mode->hsync_end = pipe_config->hw.adjusted_mode.crtc_hsync_end;
9128 
9129 	mode->vdisplay = pipe_config->hw.adjusted_mode.crtc_vdisplay;
9130 	mode->vtotal = pipe_config->hw.adjusted_mode.crtc_vtotal;
9131 	mode->vsync_start = pipe_config->hw.adjusted_mode.crtc_vsync_start;
9132 	mode->vsync_end = pipe_config->hw.adjusted_mode.crtc_vsync_end;
9133 
9134 	mode->flags = pipe_config->hw.adjusted_mode.flags;
9135 	mode->type = DRM_MODE_TYPE_DRIVER;
9136 
9137 	mode->clock = pipe_config->hw.adjusted_mode.crtc_clock;
9138 
9139 	drm_mode_set_name(mode);
9140 }
9141 
9142 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
9143 {
9144 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9145 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9146 	u32 pipeconf;
9147 
9148 	pipeconf = 0;
9149 
9150 	/* we keep both pipes enabled on 830 */
9151 	if (IS_I830(dev_priv))
9152 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
9153 
9154 	if (crtc_state->double_wide)
9155 		pipeconf |= PIPECONF_DOUBLE_WIDE;
9156 
9157 	/* only g4x and later have fancy bpc/dither controls */
9158 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9159 	    IS_CHERRYVIEW(dev_priv)) {
9160 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
9161 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
9162 			pipeconf |= PIPECONF_DITHER_EN |
9163 				    PIPECONF_DITHER_TYPE_SP;
9164 
9165 		switch (crtc_state->pipe_bpp) {
9166 		case 18:
9167 			pipeconf |= PIPECONF_6BPC;
9168 			break;
9169 		case 24:
9170 			pipeconf |= PIPECONF_8BPC;
9171 			break;
9172 		case 30:
9173 			pipeconf |= PIPECONF_10BPC;
9174 			break;
9175 		default:
9176 			/* Case prevented by intel_choose_pipe_bpp_dither. */
9177 			BUG();
9178 		}
9179 	}
9180 
9181 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
9182 		if (INTEL_GEN(dev_priv) < 4 ||
9183 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
9184 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
9185 		else
9186 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
9187 	} else {
9188 		pipeconf |= PIPECONF_PROGRESSIVE;
9189 	}
9190 
9191 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9192 	     crtc_state->limited_color_range)
9193 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9194 
9195 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
9196 
9197 	pipeconf |= PIPECONF_FRAME_START_DELAY(0);
9198 
9199 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
9200 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
9201 }
9202 
9203 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
9204 				   struct intel_crtc_state *crtc_state)
9205 {
9206 	struct drm_device *dev = crtc->base.dev;
9207 	struct drm_i915_private *dev_priv = to_i915(dev);
9208 	const struct intel_limit *limit;
9209 	int refclk = 48000;
9210 
9211 	memset(&crtc_state->dpll_hw_state, 0,
9212 	       sizeof(crtc_state->dpll_hw_state));
9213 
9214 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9215 		if (intel_panel_use_ssc(dev_priv)) {
9216 			refclk = dev_priv->vbt.lvds_ssc_freq;
9217 			drm_dbg_kms(&dev_priv->drm,
9218 				    "using SSC reference clock of %d kHz\n",
9219 				    refclk);
9220 		}
9221 
9222 		limit = &intel_limits_i8xx_lvds;
9223 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
9224 		limit = &intel_limits_i8xx_dvo;
9225 	} else {
9226 		limit = &intel_limits_i8xx_dac;
9227 	}
9228 
9229 	if (!crtc_state->clock_set &&
9230 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9231 				 refclk, NULL, &crtc_state->dpll)) {
9232 		drm_err(&dev_priv->drm,
9233 			"Couldn't find PLL settings for mode!\n");
9234 		return -EINVAL;
9235 	}
9236 
9237 	i8xx_compute_dpll(crtc, crtc_state, NULL);
9238 
9239 	return 0;
9240 }
9241 
9242 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
9243 				  struct intel_crtc_state *crtc_state)
9244 {
9245 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9246 	const struct intel_limit *limit;
9247 	int refclk = 96000;
9248 
9249 	memset(&crtc_state->dpll_hw_state, 0,
9250 	       sizeof(crtc_state->dpll_hw_state));
9251 
9252 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9253 		if (intel_panel_use_ssc(dev_priv)) {
9254 			refclk = dev_priv->vbt.lvds_ssc_freq;
9255 			drm_dbg_kms(&dev_priv->drm,
9256 				    "using SSC reference clock of %d kHz\n",
9257 				    refclk);
9258 		}
9259 
9260 		if (intel_is_dual_link_lvds(dev_priv))
9261 			limit = &intel_limits_g4x_dual_channel_lvds;
9262 		else
9263 			limit = &intel_limits_g4x_single_channel_lvds;
9264 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9265 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
9266 		limit = &intel_limits_g4x_hdmi;
9267 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
9268 		limit = &intel_limits_g4x_sdvo;
9269 	} else {
9270 		/* The option is for other outputs */
9271 		limit = &intel_limits_i9xx_sdvo;
9272 	}
9273 
9274 	if (!crtc_state->clock_set &&
9275 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9276 				refclk, NULL, &crtc_state->dpll)) {
9277 		drm_err(&dev_priv->drm,
9278 			"Couldn't find PLL settings for mode!\n");
9279 		return -EINVAL;
9280 	}
9281 
9282 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9283 
9284 	return 0;
9285 }
9286 
9287 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9288 				  struct intel_crtc_state *crtc_state)
9289 {
9290 	struct drm_device *dev = crtc->base.dev;
9291 	struct drm_i915_private *dev_priv = to_i915(dev);
9292 	const struct intel_limit *limit;
9293 	int refclk = 96000;
9294 
9295 	memset(&crtc_state->dpll_hw_state, 0,
9296 	       sizeof(crtc_state->dpll_hw_state));
9297 
9298 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9299 		if (intel_panel_use_ssc(dev_priv)) {
9300 			refclk = dev_priv->vbt.lvds_ssc_freq;
9301 			drm_dbg_kms(&dev_priv->drm,
9302 				    "using SSC reference clock of %d kHz\n",
9303 				    refclk);
9304 		}
9305 
9306 		limit = &pnv_limits_lvds;
9307 	} else {
9308 		limit = &pnv_limits_sdvo;
9309 	}
9310 
9311 	if (!crtc_state->clock_set &&
9312 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9313 				refclk, NULL, &crtc_state->dpll)) {
9314 		drm_err(&dev_priv->drm,
9315 			"Couldn't find PLL settings for mode!\n");
9316 		return -EINVAL;
9317 	}
9318 
9319 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9320 
9321 	return 0;
9322 }
9323 
9324 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9325 				   struct intel_crtc_state *crtc_state)
9326 {
9327 	struct drm_device *dev = crtc->base.dev;
9328 	struct drm_i915_private *dev_priv = to_i915(dev);
9329 	const struct intel_limit *limit;
9330 	int refclk = 96000;
9331 
9332 	memset(&crtc_state->dpll_hw_state, 0,
9333 	       sizeof(crtc_state->dpll_hw_state));
9334 
9335 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9336 		if (intel_panel_use_ssc(dev_priv)) {
9337 			refclk = dev_priv->vbt.lvds_ssc_freq;
9338 			drm_dbg_kms(&dev_priv->drm,
9339 				    "using SSC reference clock of %d kHz\n",
9340 				    refclk);
9341 		}
9342 
9343 		limit = &intel_limits_i9xx_lvds;
9344 	} else {
9345 		limit = &intel_limits_i9xx_sdvo;
9346 	}
9347 
9348 	if (!crtc_state->clock_set &&
9349 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9350 				 refclk, NULL, &crtc_state->dpll)) {
9351 		drm_err(&dev_priv->drm,
9352 			"Couldn't find PLL settings for mode!\n");
9353 		return -EINVAL;
9354 	}
9355 
9356 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9357 
9358 	return 0;
9359 }
9360 
9361 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9362 				  struct intel_crtc_state *crtc_state)
9363 {
9364 	int refclk = 100000;
9365 	const struct intel_limit *limit = &intel_limits_chv;
9366 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9367 
9368 	memset(&crtc_state->dpll_hw_state, 0,
9369 	       sizeof(crtc_state->dpll_hw_state));
9370 
9371 	if (!crtc_state->clock_set &&
9372 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9373 				refclk, NULL, &crtc_state->dpll)) {
9374 		drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
9375 		return -EINVAL;
9376 	}
9377 
9378 	chv_compute_dpll(crtc, crtc_state);
9379 
9380 	return 0;
9381 }
9382 
9383 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9384 				  struct intel_crtc_state *crtc_state)
9385 {
9386 	int refclk = 100000;
9387 	const struct intel_limit *limit = &intel_limits_vlv;
9388 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9389 
9390 	memset(&crtc_state->dpll_hw_state, 0,
9391 	       sizeof(crtc_state->dpll_hw_state));
9392 
9393 	if (!crtc_state->clock_set &&
9394 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9395 				refclk, NULL, &crtc_state->dpll)) {
9396 		drm_err(&i915->drm,  "Couldn't find PLL settings for mode!\n");
9397 		return -EINVAL;
9398 	}
9399 
9400 	vlv_compute_dpll(crtc, crtc_state);
9401 
9402 	return 0;
9403 }
9404 
9405 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9406 {
9407 	if (IS_I830(dev_priv))
9408 		return false;
9409 
9410 	return INTEL_GEN(dev_priv) >= 4 ||
9411 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9412 }
9413 
9414 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
9415 {
9416 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9417 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9418 	u32 tmp;
9419 
9420 	if (!i9xx_has_pfit(dev_priv))
9421 		return;
9422 
9423 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
9424 	if (!(tmp & PFIT_ENABLE))
9425 		return;
9426 
9427 	/* Check whether the pfit is attached to our pipe. */
9428 	if (INTEL_GEN(dev_priv) < 4) {
9429 		if (crtc->pipe != PIPE_B)
9430 			return;
9431 	} else {
9432 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9433 			return;
9434 	}
9435 
9436 	crtc_state->gmch_pfit.control = tmp;
9437 	crtc_state->gmch_pfit.pgm_ratios =
9438 		intel_de_read(dev_priv, PFIT_PGM_RATIOS);
9439 }
9440 
9441 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
9442 			       struct intel_crtc_state *pipe_config)
9443 {
9444 	struct drm_device *dev = crtc->base.dev;
9445 	struct drm_i915_private *dev_priv = to_i915(dev);
9446 	enum pipe pipe = crtc->pipe;
9447 	struct dpll clock;
9448 	u32 mdiv;
9449 	int refclk = 100000;
9450 
9451 	/* In case of DSI, DPLL will not be used */
9452 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9453 		return;
9454 
9455 	vlv_dpio_get(dev_priv);
9456 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
9457 	vlv_dpio_put(dev_priv);
9458 
9459 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9460 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
9461 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9462 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9463 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9464 
9465 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
9466 }
9467 
9468 static void
9469 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9470 			      struct intel_initial_plane_config *plane_config)
9471 {
9472 	struct drm_device *dev = crtc->base.dev;
9473 	struct drm_i915_private *dev_priv = to_i915(dev);
9474 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9475 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9476 	enum pipe pipe;
9477 	u32 val, base, offset;
9478 	int fourcc, pixel_format;
9479 	unsigned int aligned_height;
9480 	struct drm_framebuffer *fb;
9481 	struct intel_framebuffer *intel_fb;
9482 
9483 	if (!plane->get_hw_state(plane, &pipe))
9484 		return;
9485 
9486 	drm_WARN_ON(dev, pipe != crtc->pipe);
9487 
9488 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9489 	if (!intel_fb) {
9490 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
9491 		return;
9492 	}
9493 
9494 	fb = &intel_fb->base;
9495 
9496 	fb->dev = dev;
9497 
9498 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9499 
9500 	if (INTEL_GEN(dev_priv) >= 4) {
9501 		if (val & DISPPLANE_TILED) {
9502 			plane_config->tiling = I915_TILING_X;
9503 			fb->modifier = I915_FORMAT_MOD_X_TILED;
9504 		}
9505 
9506 		if (val & DISPPLANE_ROTATE_180)
9507 			plane_config->rotation = DRM_MODE_ROTATE_180;
9508 	}
9509 
9510 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9511 	    val & DISPPLANE_MIRROR)
9512 		plane_config->rotation |= DRM_MODE_REFLECT_X;
9513 
9514 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9515 	fourcc = i9xx_format_to_fourcc(pixel_format);
9516 	fb->format = drm_format_info(fourcc);
9517 
9518 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
9519 		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9520 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9521 	} else if (INTEL_GEN(dev_priv) >= 4) {
9522 		if (plane_config->tiling)
9523 			offset = intel_de_read(dev_priv,
9524 					       DSPTILEOFF(i9xx_plane));
9525 		else
9526 			offset = intel_de_read(dev_priv,
9527 					       DSPLINOFF(i9xx_plane));
9528 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9529 	} else {
9530 		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
9531 	}
9532 	plane_config->base = base;
9533 
9534 	val = intel_de_read(dev_priv, PIPESRC(pipe));
9535 	fb->width = ((val >> 16) & 0xfff) + 1;
9536 	fb->height = ((val >> 0) & 0xfff) + 1;
9537 
9538 	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
9539 	fb->pitches[0] = val & 0xffffffc0;
9540 
9541 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
9542 
9543 	plane_config->size = fb->pitches[0] * aligned_height;
9544 
9545 	drm_dbg_kms(&dev_priv->drm,
9546 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9547 		    crtc->base.name, plane->base.name, fb->width, fb->height,
9548 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
9549 		    plane_config->size);
9550 
9551 	plane_config->fb = intel_fb;
9552 }
9553 
9554 static void chv_crtc_clock_get(struct intel_crtc *crtc,
9555 			       struct intel_crtc_state *pipe_config)
9556 {
9557 	struct drm_device *dev = crtc->base.dev;
9558 	struct drm_i915_private *dev_priv = to_i915(dev);
9559 	enum pipe pipe = crtc->pipe;
9560 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
9561 	struct dpll clock;
9562 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
9563 	int refclk = 100000;
9564 
9565 	/* In case of DSI, DPLL will not be used */
9566 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9567 		return;
9568 
9569 	vlv_dpio_get(dev_priv);
9570 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9571 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9572 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9573 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
9574 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
9575 	vlv_dpio_put(dev_priv);
9576 
9577 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
9578 	clock.m2 = (pll_dw0 & 0xff) << 22;
9579 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9580 		clock.m2 |= pll_dw2 & 0x3fffff;
9581 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9582 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9583 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9584 
9585 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
9586 }
9587 
9588 static enum intel_output_format
9589 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
9590 {
9591 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9592 	u32 tmp;
9593 
9594 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
9595 
9596 	if (tmp & PIPEMISC_YUV420_ENABLE) {
9597 		/* We support 4:2:0 in full blend mode only */
9598 		drm_WARN_ON(&dev_priv->drm,
9599 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
9600 
9601 		return INTEL_OUTPUT_FORMAT_YCBCR420;
9602 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9603 		return INTEL_OUTPUT_FORMAT_YCBCR444;
9604 	} else {
9605 		return INTEL_OUTPUT_FORMAT_RGB;
9606 	}
9607 }
9608 
9609 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9610 {
9611 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9612 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9613 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9614 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9615 	u32 tmp;
9616 
9617 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9618 
9619 	if (tmp & DISPPLANE_GAMMA_ENABLE)
9620 		crtc_state->gamma_enable = true;
9621 
9622 	if (!HAS_GMCH(dev_priv) &&
9623 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
9624 		crtc_state->csc_enable = true;
9625 }
9626 
9627 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
9628 				 struct intel_crtc_state *pipe_config)
9629 {
9630 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9631 	enum intel_display_power_domain power_domain;
9632 	intel_wakeref_t wakeref;
9633 	u32 tmp;
9634 	bool ret;
9635 
9636 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9637 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9638 	if (!wakeref)
9639 		return false;
9640 
9641 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9642 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9643 	pipe_config->shared_dpll = NULL;
9644 
9645 	ret = false;
9646 
9647 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
9648 	if (!(tmp & PIPECONF_ENABLE))
9649 		goto out;
9650 
9651 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9652 	    IS_CHERRYVIEW(dev_priv)) {
9653 		switch (tmp & PIPECONF_BPC_MASK) {
9654 		case PIPECONF_6BPC:
9655 			pipe_config->pipe_bpp = 18;
9656 			break;
9657 		case PIPECONF_8BPC:
9658 			pipe_config->pipe_bpp = 24;
9659 			break;
9660 		case PIPECONF_10BPC:
9661 			pipe_config->pipe_bpp = 30;
9662 			break;
9663 		default:
9664 			break;
9665 		}
9666 	}
9667 
9668 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9669 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
9670 		pipe_config->limited_color_range = true;
9671 
9672 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9673 		PIPECONF_GAMMA_MODE_SHIFT;
9674 
9675 	if (IS_CHERRYVIEW(dev_priv))
9676 		pipe_config->cgm_mode = intel_de_read(dev_priv,
9677 						      CGM_PIPE_MODE(crtc->pipe));
9678 
9679 	i9xx_get_pipe_color_config(pipe_config);
9680 	intel_color_get_config(pipe_config);
9681 
9682 	if (INTEL_GEN(dev_priv) < 4)
9683 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9684 
9685 	intel_get_transcoder_timings(crtc, pipe_config);
9686 	intel_get_pipe_src_size(crtc, pipe_config);
9687 
9688 	i9xx_get_pfit_config(pipe_config);
9689 
9690 	if (INTEL_GEN(dev_priv) >= 4) {
9691 		/* No way to read it out on pipes B and C */
9692 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
9693 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
9694 		else
9695 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
9696 		pipe_config->pixel_multiplier =
9697 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9698 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
9699 		pipe_config->dpll_hw_state.dpll_md = tmp;
9700 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
9701 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
9702 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
9703 		pipe_config->pixel_multiplier =
9704 			((tmp & SDVO_MULTIPLIER_MASK)
9705 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9706 	} else {
9707 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
9708 		 * port and will be fixed up in the encoder->get_config
9709 		 * function. */
9710 		pipe_config->pixel_multiplier = 1;
9711 	}
9712 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9713 							DPLL(crtc->pipe));
9714 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
9715 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9716 							       FP0(crtc->pipe));
9717 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9718 							       FP1(crtc->pipe));
9719 	} else {
9720 		/* Mask out read-only status bits. */
9721 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9722 						     DPLL_PORTC_READY_MASK |
9723 						     DPLL_PORTB_READY_MASK);
9724 	}
9725 
9726 	if (IS_CHERRYVIEW(dev_priv))
9727 		chv_crtc_clock_get(crtc, pipe_config);
9728 	else if (IS_VALLEYVIEW(dev_priv))
9729 		vlv_crtc_clock_get(crtc, pipe_config);
9730 	else
9731 		i9xx_crtc_clock_get(crtc, pipe_config);
9732 
9733 	/*
9734 	 * Normally the dotclock is filled in by the encoder .get_config()
9735 	 * but in case the pipe is enabled w/o any ports we need a sane
9736 	 * default.
9737 	 */
9738 	pipe_config->hw.adjusted_mode.crtc_clock =
9739 		pipe_config->port_clock / pipe_config->pixel_multiplier;
9740 
9741 	ret = true;
9742 
9743 out:
9744 	intel_display_power_put(dev_priv, power_domain, wakeref);
9745 
9746 	return ret;
9747 }
9748 
9749 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
9750 {
9751 	struct intel_encoder *encoder;
9752 	int i;
9753 	u32 val, final;
9754 	bool has_lvds = false;
9755 	bool has_cpu_edp = false;
9756 	bool has_panel = false;
9757 	bool has_ck505 = false;
9758 	bool can_ssc = false;
9759 	bool using_ssc_source = false;
9760 
9761 	/* We need to take the global config into account */
9762 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9763 		switch (encoder->type) {
9764 		case INTEL_OUTPUT_LVDS:
9765 			has_panel = true;
9766 			has_lvds = true;
9767 			break;
9768 		case INTEL_OUTPUT_EDP:
9769 			has_panel = true;
9770 			if (encoder->port == PORT_A)
9771 				has_cpu_edp = true;
9772 			break;
9773 		default:
9774 			break;
9775 		}
9776 	}
9777 
9778 	if (HAS_PCH_IBX(dev_priv)) {
9779 		has_ck505 = dev_priv->vbt.display_clock_mode;
9780 		can_ssc = has_ck505;
9781 	} else {
9782 		has_ck505 = false;
9783 		can_ssc = true;
9784 	}
9785 
9786 	/* Check if any DPLLs are using the SSC source */
9787 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
9788 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
9789 
9790 		if (!(temp & DPLL_VCO_ENABLE))
9791 			continue;
9792 
9793 		if ((temp & PLL_REF_INPUT_MASK) ==
9794 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9795 			using_ssc_source = true;
9796 			break;
9797 		}
9798 	}
9799 
9800 	drm_dbg_kms(&dev_priv->drm,
9801 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9802 		    has_panel, has_lvds, has_ck505, using_ssc_source);
9803 
9804 	/* Ironlake: try to setup display ref clock before DPLL
9805 	 * enabling. This is only under driver's control after
9806 	 * PCH B stepping, previous chipset stepping should be
9807 	 * ignoring this setting.
9808 	 */
9809 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
9810 
9811 	/* As we must carefully and slowly disable/enable each source in turn,
9812 	 * compute the final state we want first and check if we need to
9813 	 * make any changes at all.
9814 	 */
9815 	final = val;
9816 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
9817 	if (has_ck505)
9818 		final |= DREF_NONSPREAD_CK505_ENABLE;
9819 	else
9820 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
9821 
9822 	final &= ~DREF_SSC_SOURCE_MASK;
9823 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9824 	final &= ~DREF_SSC1_ENABLE;
9825 
9826 	if (has_panel) {
9827 		final |= DREF_SSC_SOURCE_ENABLE;
9828 
9829 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
9830 			final |= DREF_SSC1_ENABLE;
9831 
9832 		if (has_cpu_edp) {
9833 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
9834 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9835 			else
9836 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9837 		} else
9838 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9839 	} else if (using_ssc_source) {
9840 		final |= DREF_SSC_SOURCE_ENABLE;
9841 		final |= DREF_SSC1_ENABLE;
9842 	}
9843 
9844 	if (final == val)
9845 		return;
9846 
9847 	/* Always enable nonspread source */
9848 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
9849 
9850 	if (has_ck505)
9851 		val |= DREF_NONSPREAD_CK505_ENABLE;
9852 	else
9853 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
9854 
9855 	if (has_panel) {
9856 		val &= ~DREF_SSC_SOURCE_MASK;
9857 		val |= DREF_SSC_SOURCE_ENABLE;
9858 
9859 		/* SSC must be turned on before enabling the CPU output  */
9860 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9861 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
9862 			val |= DREF_SSC1_ENABLE;
9863 		} else
9864 			val &= ~DREF_SSC1_ENABLE;
9865 
9866 		/* Get SSC going before enabling the outputs */
9867 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9868 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9869 		udelay(200);
9870 
9871 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9872 
9873 		/* Enable CPU source on CPU attached eDP */
9874 		if (has_cpu_edp) {
9875 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9876 				drm_dbg_kms(&dev_priv->drm,
9877 					    "Using SSC on eDP\n");
9878 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9879 			} else
9880 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9881 		} else
9882 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9883 
9884 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9885 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9886 		udelay(200);
9887 	} else {
9888 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
9889 
9890 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9891 
9892 		/* Turn off CPU output */
9893 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9894 
9895 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9896 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9897 		udelay(200);
9898 
9899 		if (!using_ssc_source) {
9900 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
9901 
9902 			/* Turn off the SSC source */
9903 			val &= ~DREF_SSC_SOURCE_MASK;
9904 			val |= DREF_SSC_SOURCE_DISABLE;
9905 
9906 			/* Turn off SSC1 */
9907 			val &= ~DREF_SSC1_ENABLE;
9908 
9909 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9910 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9911 			udelay(200);
9912 		}
9913 	}
9914 
9915 	BUG_ON(val != final);
9916 }
9917 
9918 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9919 {
9920 	u32 tmp;
9921 
9922 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9923 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9924 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9925 
9926 	if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9927 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9928 		drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
9929 
9930 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9931 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9932 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9933 
9934 	if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9935 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9936 		drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
9937 }
9938 
9939 /* WaMPhyProgramming:hsw */
9940 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9941 {
9942 	u32 tmp;
9943 
9944 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9945 	tmp &= ~(0xFF << 24);
9946 	tmp |= (0x12 << 24);
9947 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9948 
9949 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9950 	tmp |= (1 << 11);
9951 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9952 
9953 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9954 	tmp |= (1 << 11);
9955 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9956 
9957 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9958 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9959 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9960 
9961 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9962 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9963 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9964 
9965 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9966 	tmp &= ~(7 << 13);
9967 	tmp |= (5 << 13);
9968 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9969 
9970 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9971 	tmp &= ~(7 << 13);
9972 	tmp |= (5 << 13);
9973 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9974 
9975 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9976 	tmp &= ~0xFF;
9977 	tmp |= 0x1C;
9978 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9979 
9980 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9981 	tmp &= ~0xFF;
9982 	tmp |= 0x1C;
9983 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9984 
9985 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9986 	tmp &= ~(0xFF << 16);
9987 	tmp |= (0x1C << 16);
9988 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9989 
9990 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9991 	tmp &= ~(0xFF << 16);
9992 	tmp |= (0x1C << 16);
9993 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9994 
9995 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9996 	tmp |= (1 << 27);
9997 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9998 
9999 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
10000 	tmp |= (1 << 27);
10001 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
10002 
10003 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
10004 	tmp &= ~(0xF << 28);
10005 	tmp |= (4 << 28);
10006 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
10007 
10008 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
10009 	tmp &= ~(0xF << 28);
10010 	tmp |= (4 << 28);
10011 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
10012 }
10013 
10014 /* Implements 3 different sequences from BSpec chapter "Display iCLK
10015  * Programming" based on the parameters passed:
10016  * - Sequence to enable CLKOUT_DP
10017  * - Sequence to enable CLKOUT_DP without spread
10018  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
10019  */
10020 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
10021 				 bool with_spread, bool with_fdi)
10022 {
10023 	u32 reg, tmp;
10024 
10025 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
10026 		     "FDI requires downspread\n"))
10027 		with_spread = true;
10028 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
10029 		     with_fdi, "LP PCH doesn't have FDI\n"))
10030 		with_fdi = false;
10031 
10032 	mutex_lock(&dev_priv->sb_lock);
10033 
10034 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10035 	tmp &= ~SBI_SSCCTL_DISABLE;
10036 	tmp |= SBI_SSCCTL_PATHALT;
10037 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10038 
10039 	udelay(24);
10040 
10041 	if (with_spread) {
10042 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10043 		tmp &= ~SBI_SSCCTL_PATHALT;
10044 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10045 
10046 		if (with_fdi) {
10047 			lpt_reset_fdi_mphy(dev_priv);
10048 			lpt_program_fdi_mphy(dev_priv);
10049 		}
10050 	}
10051 
10052 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
10053 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10054 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10055 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
10056 
10057 	mutex_unlock(&dev_priv->sb_lock);
10058 }
10059 
10060 /* Sequence to disable CLKOUT_DP */
10061 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
10062 {
10063 	u32 reg, tmp;
10064 
10065 	mutex_lock(&dev_priv->sb_lock);
10066 
10067 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
10068 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
10069 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
10070 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
10071 
10072 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
10073 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
10074 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
10075 			tmp |= SBI_SSCCTL_PATHALT;
10076 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10077 			udelay(32);
10078 		}
10079 		tmp |= SBI_SSCCTL_DISABLE;
10080 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
10081 	}
10082 
10083 	mutex_unlock(&dev_priv->sb_lock);
10084 }
10085 
10086 #define BEND_IDX(steps) ((50 + (steps)) / 5)
10087 
10088 static const u16 sscdivintphase[] = {
10089 	[BEND_IDX( 50)] = 0x3B23,
10090 	[BEND_IDX( 45)] = 0x3B23,
10091 	[BEND_IDX( 40)] = 0x3C23,
10092 	[BEND_IDX( 35)] = 0x3C23,
10093 	[BEND_IDX( 30)] = 0x3D23,
10094 	[BEND_IDX( 25)] = 0x3D23,
10095 	[BEND_IDX( 20)] = 0x3E23,
10096 	[BEND_IDX( 15)] = 0x3E23,
10097 	[BEND_IDX( 10)] = 0x3F23,
10098 	[BEND_IDX(  5)] = 0x3F23,
10099 	[BEND_IDX(  0)] = 0x0025,
10100 	[BEND_IDX( -5)] = 0x0025,
10101 	[BEND_IDX(-10)] = 0x0125,
10102 	[BEND_IDX(-15)] = 0x0125,
10103 	[BEND_IDX(-20)] = 0x0225,
10104 	[BEND_IDX(-25)] = 0x0225,
10105 	[BEND_IDX(-30)] = 0x0325,
10106 	[BEND_IDX(-35)] = 0x0325,
10107 	[BEND_IDX(-40)] = 0x0425,
10108 	[BEND_IDX(-45)] = 0x0425,
10109 	[BEND_IDX(-50)] = 0x0525,
10110 };
10111 
10112 /*
10113  * Bend CLKOUT_DP
10114  * steps -50 to 50 inclusive, in steps of 5
10115  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
10116  * change in clock period = -(steps / 10) * 5.787 ps
10117  */
10118 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
10119 {
10120 	u32 tmp;
10121 	int idx = BEND_IDX(steps);
10122 
10123 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
10124 		return;
10125 
10126 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
10127 		return;
10128 
10129 	mutex_lock(&dev_priv->sb_lock);
10130 
10131 	if (steps % 10 != 0)
10132 		tmp = 0xAAAAAAAB;
10133 	else
10134 		tmp = 0x00000000;
10135 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
10136 
10137 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
10138 	tmp &= 0xffff0000;
10139 	tmp |= sscdivintphase[idx];
10140 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
10141 
10142 	mutex_unlock(&dev_priv->sb_lock);
10143 }
10144 
10145 #undef BEND_IDX
10146 
10147 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
10148 {
10149 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10150 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
10151 
10152 	if ((ctl & SPLL_PLL_ENABLE) == 0)
10153 		return false;
10154 
10155 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
10156 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10157 		return true;
10158 
10159 	if (IS_BROADWELL(dev_priv) &&
10160 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
10161 		return true;
10162 
10163 	return false;
10164 }
10165 
10166 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
10167 			       enum intel_dpll_id id)
10168 {
10169 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10170 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
10171 
10172 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
10173 		return false;
10174 
10175 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
10176 		return true;
10177 
10178 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
10179 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
10180 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10181 		return true;
10182 
10183 	return false;
10184 }
10185 
10186 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
10187 {
10188 	struct intel_encoder *encoder;
10189 	bool has_fdi = false;
10190 
10191 	for_each_intel_encoder(&dev_priv->drm, encoder) {
10192 		switch (encoder->type) {
10193 		case INTEL_OUTPUT_ANALOG:
10194 			has_fdi = true;
10195 			break;
10196 		default:
10197 			break;
10198 		}
10199 	}
10200 
10201 	/*
10202 	 * The BIOS may have decided to use the PCH SSC
10203 	 * reference so we must not disable it until the
10204 	 * relevant PLLs have stopped relying on it. We'll
10205 	 * just leave the PCH SSC reference enabled in case
10206 	 * any active PLL is using it. It will get disabled
10207 	 * after runtime suspend if we don't have FDI.
10208 	 *
10209 	 * TODO: Move the whole reference clock handling
10210 	 * to the modeset sequence proper so that we can
10211 	 * actually enable/disable/reconfigure these things
10212 	 * safely. To do that we need to introduce a real
10213 	 * clock hierarchy. That would also allow us to do
10214 	 * clock bending finally.
10215 	 */
10216 	dev_priv->pch_ssc_use = 0;
10217 
10218 	if (spll_uses_pch_ssc(dev_priv)) {
10219 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
10220 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
10221 	}
10222 
10223 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
10224 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
10225 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
10226 	}
10227 
10228 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
10229 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
10230 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
10231 	}
10232 
10233 	if (dev_priv->pch_ssc_use)
10234 		return;
10235 
10236 	if (has_fdi) {
10237 		lpt_bend_clkout_dp(dev_priv, 0);
10238 		lpt_enable_clkout_dp(dev_priv, true, true);
10239 	} else {
10240 		lpt_disable_clkout_dp(dev_priv);
10241 	}
10242 }
10243 
10244 /*
10245  * Initialize reference clocks when the driver loads
10246  */
10247 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
10248 {
10249 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
10250 		ilk_init_pch_refclk(dev_priv);
10251 	else if (HAS_PCH_LPT(dev_priv))
10252 		lpt_init_pch_refclk(dev_priv);
10253 }
10254 
10255 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
10256 {
10257 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10258 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10259 	enum pipe pipe = crtc->pipe;
10260 	u32 val;
10261 
10262 	val = 0;
10263 
10264 	switch (crtc_state->pipe_bpp) {
10265 	case 18:
10266 		val |= PIPECONF_6BPC;
10267 		break;
10268 	case 24:
10269 		val |= PIPECONF_8BPC;
10270 		break;
10271 	case 30:
10272 		val |= PIPECONF_10BPC;
10273 		break;
10274 	case 36:
10275 		val |= PIPECONF_12BPC;
10276 		break;
10277 	default:
10278 		/* Case prevented by intel_choose_pipe_bpp_dither. */
10279 		BUG();
10280 	}
10281 
10282 	if (crtc_state->dither)
10283 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10284 
10285 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10286 		val |= PIPECONF_INTERLACED_ILK;
10287 	else
10288 		val |= PIPECONF_PROGRESSIVE;
10289 
10290 	/*
10291 	 * This would end up with an odd purple hue over
10292 	 * the entire display. Make sure we don't do it.
10293 	 */
10294 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10295 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
10296 
10297 	if (crtc_state->limited_color_range &&
10298 	    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
10299 		val |= PIPECONF_COLOR_RANGE_SELECT;
10300 
10301 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10302 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10303 
10304 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10305 
10306 	val |= PIPECONF_FRAME_START_DELAY(0);
10307 
10308 	intel_de_write(dev_priv, PIPECONF(pipe), val);
10309 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
10310 }
10311 
10312 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
10313 {
10314 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10315 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10316 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
10317 	u32 val = 0;
10318 
10319 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
10320 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10321 
10322 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10323 		val |= PIPECONF_INTERLACED_ILK;
10324 	else
10325 		val |= PIPECONF_PROGRESSIVE;
10326 
10327 	if (IS_HASWELL(dev_priv) &&
10328 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10329 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10330 
10331 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10332 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
10333 }
10334 
10335 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
10336 {
10337 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10338 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10339 	u32 val = 0;
10340 
10341 	switch (crtc_state->pipe_bpp) {
10342 	case 18:
10343 		val |= PIPEMISC_DITHER_6_BPC;
10344 		break;
10345 	case 24:
10346 		val |= PIPEMISC_DITHER_8_BPC;
10347 		break;
10348 	case 30:
10349 		val |= PIPEMISC_DITHER_10_BPC;
10350 		break;
10351 	case 36:
10352 		val |= PIPEMISC_DITHER_12_BPC;
10353 		break;
10354 	default:
10355 		MISSING_CASE(crtc_state->pipe_bpp);
10356 		break;
10357 	}
10358 
10359 	if (crtc_state->dither)
10360 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
10361 
10362 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10363 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10364 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
10365 
10366 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10367 		val |= PIPEMISC_YUV420_ENABLE |
10368 			PIPEMISC_YUV420_MODE_FULL_BLEND;
10369 
10370 	if (INTEL_GEN(dev_priv) >= 11 &&
10371 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10372 					   BIT(PLANE_CURSOR))) == 0)
10373 		val |= PIPEMISC_HDR_MODE_PRECISION;
10374 
10375 	if (INTEL_GEN(dev_priv) >= 12)
10376 		val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10377 
10378 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
10379 }
10380 
10381 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10382 {
10383 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10384 	u32 tmp;
10385 
10386 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
10387 
10388 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10389 	case PIPEMISC_DITHER_6_BPC:
10390 		return 18;
10391 	case PIPEMISC_DITHER_8_BPC:
10392 		return 24;
10393 	case PIPEMISC_DITHER_10_BPC:
10394 		return 30;
10395 	case PIPEMISC_DITHER_12_BPC:
10396 		return 36;
10397 	default:
10398 		MISSING_CASE(tmp);
10399 		return 0;
10400 	}
10401 }
10402 
10403 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
10404 {
10405 	/*
10406 	 * Account for spread spectrum to avoid
10407 	 * oversubscribing the link. Max center spread
10408 	 * is 2.5%; use 5% for safety's sake.
10409 	 */
10410 	u32 bps = target_clock * bpp * 21 / 20;
10411 	return DIV_ROUND_UP(bps, link_bw * 8);
10412 }
10413 
10414 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
10415 {
10416 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
10417 }
10418 
10419 static void ilk_compute_dpll(struct intel_crtc *crtc,
10420 			     struct intel_crtc_state *crtc_state,
10421 			     struct dpll *reduced_clock)
10422 {
10423 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10424 	u32 dpll, fp, fp2;
10425 	int factor;
10426 
10427 	/* Enable autotuning of the PLL clock (if permissible) */
10428 	factor = 21;
10429 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10430 		if ((intel_panel_use_ssc(dev_priv) &&
10431 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
10432 		    (HAS_PCH_IBX(dev_priv) &&
10433 		     intel_is_dual_link_lvds(dev_priv)))
10434 			factor = 25;
10435 	} else if (crtc_state->sdvo_tv_clock) {
10436 		factor = 20;
10437 	}
10438 
10439 	fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10440 
10441 	if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
10442 		fp |= FP_CB_TUNE;
10443 
10444 	if (reduced_clock) {
10445 		fp2 = i9xx_dpll_compute_fp(reduced_clock);
10446 
10447 		if (reduced_clock->m < factor * reduced_clock->n)
10448 			fp2 |= FP_CB_TUNE;
10449 	} else {
10450 		fp2 = fp;
10451 	}
10452 
10453 	dpll = 0;
10454 
10455 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
10456 		dpll |= DPLLB_MODE_LVDS;
10457 	else
10458 		dpll |= DPLLB_MODE_DAC_SERIAL;
10459 
10460 	dpll |= (crtc_state->pixel_multiplier - 1)
10461 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
10462 
10463 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10464 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
10465 		dpll |= DPLL_SDVO_HIGH_SPEED;
10466 
10467 	if (intel_crtc_has_dp_encoder(crtc_state))
10468 		dpll |= DPLL_SDVO_HIGH_SPEED;
10469 
10470 	/*
10471 	 * The high speed IO clock is only really required for
10472 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10473 	 * possible to share the DPLL between CRT and HDMI. Enabling
10474 	 * the clock needlessly does no real harm, except use up a
10475 	 * bit of power potentially.
10476 	 *
10477 	 * We'll limit this to IVB with 3 pipes, since it has only two
10478 	 * DPLLs and so DPLL sharing is the only way to get three pipes
10479 	 * driving PCH ports at the same time. On SNB we could do this,
10480 	 * and potentially avoid enabling the second DPLL, but it's not
10481 	 * clear if it''s a win or loss power wise. No point in doing
10482 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10483 	 */
10484 	if (INTEL_NUM_PIPES(dev_priv) == 3 &&
10485 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10486 		dpll |= DPLL_SDVO_HIGH_SPEED;
10487 
10488 	/* compute bitmask from p1 value */
10489 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
10490 	/* also FPA1 */
10491 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
10492 
10493 	switch (crtc_state->dpll.p2) {
10494 	case 5:
10495 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10496 		break;
10497 	case 7:
10498 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10499 		break;
10500 	case 10:
10501 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10502 		break;
10503 	case 14:
10504 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10505 		break;
10506 	}
10507 
10508 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10509 	    intel_panel_use_ssc(dev_priv))
10510 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
10511 	else
10512 		dpll |= PLL_REF_INPUT_DREFCLK;
10513 
10514 	dpll |= DPLL_VCO_ENABLE;
10515 
10516 	crtc_state->dpll_hw_state.dpll = dpll;
10517 	crtc_state->dpll_hw_state.fp0 = fp;
10518 	crtc_state->dpll_hw_state.fp1 = fp2;
10519 }
10520 
10521 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10522 				  struct intel_crtc_state *crtc_state)
10523 {
10524 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10525 	struct intel_atomic_state *state =
10526 		to_intel_atomic_state(crtc_state->uapi.state);
10527 	const struct intel_limit *limit;
10528 	int refclk = 120000;
10529 
10530 	memset(&crtc_state->dpll_hw_state, 0,
10531 	       sizeof(crtc_state->dpll_hw_state));
10532 
10533 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10534 	if (!crtc_state->has_pch_encoder)
10535 		return 0;
10536 
10537 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10538 		if (intel_panel_use_ssc(dev_priv)) {
10539 			drm_dbg_kms(&dev_priv->drm,
10540 				    "using SSC reference clock of %d kHz\n",
10541 				    dev_priv->vbt.lvds_ssc_freq);
10542 			refclk = dev_priv->vbt.lvds_ssc_freq;
10543 		}
10544 
10545 		if (intel_is_dual_link_lvds(dev_priv)) {
10546 			if (refclk == 100000)
10547 				limit = &ilk_limits_dual_lvds_100m;
10548 			else
10549 				limit = &ilk_limits_dual_lvds;
10550 		} else {
10551 			if (refclk == 100000)
10552 				limit = &ilk_limits_single_lvds_100m;
10553 			else
10554 				limit = &ilk_limits_single_lvds;
10555 		}
10556 	} else {
10557 		limit = &ilk_limits_dac;
10558 	}
10559 
10560 	if (!crtc_state->clock_set &&
10561 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10562 				refclk, NULL, &crtc_state->dpll)) {
10563 		drm_err(&dev_priv->drm,
10564 			"Couldn't find PLL settings for mode!\n");
10565 		return -EINVAL;
10566 	}
10567 
10568 	ilk_compute_dpll(crtc, crtc_state, NULL);
10569 
10570 	if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
10571 		drm_dbg_kms(&dev_priv->drm,
10572 			    "failed to find PLL for pipe %c\n",
10573 			    pipe_name(crtc->pipe));
10574 		return -EINVAL;
10575 	}
10576 
10577 	return 0;
10578 }
10579 
10580 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10581 					 struct intel_link_m_n *m_n)
10582 {
10583 	struct drm_device *dev = crtc->base.dev;
10584 	struct drm_i915_private *dev_priv = to_i915(dev);
10585 	enum pipe pipe = crtc->pipe;
10586 
10587 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10588 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10589 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10590 		& ~TU_SIZE_MASK;
10591 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10592 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10593 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10594 }
10595 
10596 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10597 					 enum transcoder transcoder,
10598 					 struct intel_link_m_n *m_n,
10599 					 struct intel_link_m_n *m2_n2)
10600 {
10601 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10602 	enum pipe pipe = crtc->pipe;
10603 
10604 	if (INTEL_GEN(dev_priv) >= 5) {
10605 		m_n->link_m = intel_de_read(dev_priv,
10606 					    PIPE_LINK_M1(transcoder));
10607 		m_n->link_n = intel_de_read(dev_priv,
10608 					    PIPE_LINK_N1(transcoder));
10609 		m_n->gmch_m = intel_de_read(dev_priv,
10610 					    PIPE_DATA_M1(transcoder))
10611 			& ~TU_SIZE_MASK;
10612 		m_n->gmch_n = intel_de_read(dev_priv,
10613 					    PIPE_DATA_N1(transcoder));
10614 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
10615 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10616 
10617 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
10618 			m2_n2->link_m = intel_de_read(dev_priv,
10619 						      PIPE_LINK_M2(transcoder));
10620 			m2_n2->link_n =	intel_de_read(dev_priv,
10621 							     PIPE_LINK_N2(transcoder));
10622 			m2_n2->gmch_m =	intel_de_read(dev_priv,
10623 							     PIPE_DATA_M2(transcoder))
10624 					& ~TU_SIZE_MASK;
10625 			m2_n2->gmch_n =	intel_de_read(dev_priv,
10626 							     PIPE_DATA_N2(transcoder));
10627 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
10628 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10629 		}
10630 	} else {
10631 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10632 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10633 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10634 			& ~TU_SIZE_MASK;
10635 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10636 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10637 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10638 	}
10639 }
10640 
10641 void intel_dp_get_m_n(struct intel_crtc *crtc,
10642 		      struct intel_crtc_state *pipe_config)
10643 {
10644 	if (pipe_config->has_pch_encoder)
10645 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10646 	else
10647 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10648 					     &pipe_config->dp_m_n,
10649 					     &pipe_config->dp_m2_n2);
10650 }
10651 
10652 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10653 				   struct intel_crtc_state *pipe_config)
10654 {
10655 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10656 				     &pipe_config->fdi_m_n, NULL);
10657 }
10658 
10659 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
10660 				  u32 pos, u32 size)
10661 {
10662 	drm_rect_init(&crtc_state->pch_pfit.dst,
10663 		      pos >> 16, pos & 0xffff,
10664 		      size >> 16, size & 0xffff);
10665 }
10666 
10667 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
10668 {
10669 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10670 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10671 	struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
10672 	int id = -1;
10673 	int i;
10674 
10675 	/* find scaler attached to this pipe */
10676 	for (i = 0; i < crtc->num_scalers; i++) {
10677 		u32 ctl, pos, size;
10678 
10679 		ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10680 		if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
10681 			continue;
10682 
10683 		id = i;
10684 		crtc_state->pch_pfit.enabled = true;
10685 
10686 		pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
10687 		size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
10688 
10689 		ilk_get_pfit_pos_size(crtc_state, pos, size);
10690 
10691 		scaler_state->scalers[i].in_use = true;
10692 		break;
10693 	}
10694 
10695 	scaler_state->scaler_id = id;
10696 	if (id >= 0)
10697 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
10698 	else
10699 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
10700 }
10701 
10702 static void
10703 skl_get_initial_plane_config(struct intel_crtc *crtc,
10704 			     struct intel_initial_plane_config *plane_config)
10705 {
10706 	struct drm_device *dev = crtc->base.dev;
10707 	struct drm_i915_private *dev_priv = to_i915(dev);
10708 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10709 	enum plane_id plane_id = plane->id;
10710 	enum pipe pipe;
10711 	u32 val, base, offset, stride_mult, tiling, alpha;
10712 	int fourcc, pixel_format;
10713 	unsigned int aligned_height;
10714 	struct drm_framebuffer *fb;
10715 	struct intel_framebuffer *intel_fb;
10716 
10717 	if (!plane->get_hw_state(plane, &pipe))
10718 		return;
10719 
10720 	drm_WARN_ON(dev, pipe != crtc->pipe);
10721 
10722 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10723 	if (!intel_fb) {
10724 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
10725 		return;
10726 	}
10727 
10728 	fb = &intel_fb->base;
10729 
10730 	fb->dev = dev;
10731 
10732 	val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
10733 
10734 	if (INTEL_GEN(dev_priv) >= 11)
10735 		pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10736 	else
10737 		pixel_format = val & PLANE_CTL_FORMAT_MASK;
10738 
10739 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
10740 		alpha = intel_de_read(dev_priv,
10741 				      PLANE_COLOR_CTL(pipe, plane_id));
10742 		alpha &= PLANE_COLOR_ALPHA_MASK;
10743 	} else {
10744 		alpha = val & PLANE_CTL_ALPHA_MASK;
10745 	}
10746 
10747 	fourcc = skl_format_to_fourcc(pixel_format,
10748 				      val & PLANE_CTL_ORDER_RGBX, alpha);
10749 	fb->format = drm_format_info(fourcc);
10750 
10751 	tiling = val & PLANE_CTL_TILED_MASK;
10752 	switch (tiling) {
10753 	case PLANE_CTL_TILED_LINEAR:
10754 		fb->modifier = DRM_FORMAT_MOD_LINEAR;
10755 		break;
10756 	case PLANE_CTL_TILED_X:
10757 		plane_config->tiling = I915_TILING_X;
10758 		fb->modifier = I915_FORMAT_MOD_X_TILED;
10759 		break;
10760 	case PLANE_CTL_TILED_Y:
10761 		plane_config->tiling = I915_TILING_Y;
10762 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10763 			fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10764 				I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10765 				I915_FORMAT_MOD_Y_TILED_CCS;
10766 		else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10767 			fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
10768 		else
10769 			fb->modifier = I915_FORMAT_MOD_Y_TILED;
10770 		break;
10771 	case PLANE_CTL_TILED_YF:
10772 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10773 			fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10774 		else
10775 			fb->modifier = I915_FORMAT_MOD_Yf_TILED;
10776 		break;
10777 	default:
10778 		MISSING_CASE(tiling);
10779 		goto error;
10780 	}
10781 
10782 	/*
10783 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10784 	 * while i915 HW rotation is clockwise, thats why this swapping.
10785 	 */
10786 	switch (val & PLANE_CTL_ROTATE_MASK) {
10787 	case PLANE_CTL_ROTATE_0:
10788 		plane_config->rotation = DRM_MODE_ROTATE_0;
10789 		break;
10790 	case PLANE_CTL_ROTATE_90:
10791 		plane_config->rotation = DRM_MODE_ROTATE_270;
10792 		break;
10793 	case PLANE_CTL_ROTATE_180:
10794 		plane_config->rotation = DRM_MODE_ROTATE_180;
10795 		break;
10796 	case PLANE_CTL_ROTATE_270:
10797 		plane_config->rotation = DRM_MODE_ROTATE_90;
10798 		break;
10799 	}
10800 
10801 	if (INTEL_GEN(dev_priv) >= 10 &&
10802 	    val & PLANE_CTL_FLIP_HORIZONTAL)
10803 		plane_config->rotation |= DRM_MODE_REFLECT_X;
10804 
10805 	/* 90/270 degree rotation would require extra work */
10806 	if (drm_rotation_90_or_270(plane_config->rotation))
10807 		goto error;
10808 
10809 	base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
10810 	plane_config->base = base;
10811 
10812 	offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
10813 
10814 	val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
10815 	fb->height = ((val >> 16) & 0xffff) + 1;
10816 	fb->width = ((val >> 0) & 0xffff) + 1;
10817 
10818 	val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
10819 	stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
10820 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
10821 
10822 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
10823 
10824 	plane_config->size = fb->pitches[0] * aligned_height;
10825 
10826 	drm_dbg_kms(&dev_priv->drm,
10827 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10828 		    crtc->base.name, plane->base.name, fb->width, fb->height,
10829 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
10830 		    plane_config->size);
10831 
10832 	plane_config->fb = intel_fb;
10833 	return;
10834 
10835 error:
10836 	kfree(intel_fb);
10837 }
10838 
10839 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
10840 {
10841 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10842 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10843 	u32 ctl, pos, size;
10844 
10845 	ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10846 	if ((ctl & PF_ENABLE) == 0)
10847 		return;
10848 
10849 	crtc_state->pch_pfit.enabled = true;
10850 
10851 	pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
10852 	size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
10853 
10854 	ilk_get_pfit_pos_size(crtc_state, pos, size);
10855 
10856 	/*
10857 	 * We currently do not free assignements of panel fitters on
10858 	 * ivb/hsw (since we don't use the higher upscaling modes which
10859 	 * differentiates them) so just WARN about this case for now.
10860 	 */
10861 	drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
10862 		    (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
10863 }
10864 
10865 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10866 				struct intel_crtc_state *pipe_config)
10867 {
10868 	struct drm_device *dev = crtc->base.dev;
10869 	struct drm_i915_private *dev_priv = to_i915(dev);
10870 	enum intel_display_power_domain power_domain;
10871 	intel_wakeref_t wakeref;
10872 	u32 tmp;
10873 	bool ret;
10874 
10875 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10876 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10877 	if (!wakeref)
10878 		return false;
10879 
10880 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10881 	pipe_config->shared_dpll = NULL;
10882 
10883 	ret = false;
10884 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
10885 	if (!(tmp & PIPECONF_ENABLE))
10886 		goto out;
10887 
10888 	switch (tmp & PIPECONF_BPC_MASK) {
10889 	case PIPECONF_6BPC:
10890 		pipe_config->pipe_bpp = 18;
10891 		break;
10892 	case PIPECONF_8BPC:
10893 		pipe_config->pipe_bpp = 24;
10894 		break;
10895 	case PIPECONF_10BPC:
10896 		pipe_config->pipe_bpp = 30;
10897 		break;
10898 	case PIPECONF_12BPC:
10899 		pipe_config->pipe_bpp = 36;
10900 		break;
10901 	default:
10902 		break;
10903 	}
10904 
10905 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10906 		pipe_config->limited_color_range = true;
10907 
10908 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10909 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10910 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10911 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10912 		break;
10913 	default:
10914 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10915 		break;
10916 	}
10917 
10918 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10919 		PIPECONF_GAMMA_MODE_SHIFT;
10920 
10921 	pipe_config->csc_mode = intel_de_read(dev_priv,
10922 					      PIPE_CSC_MODE(crtc->pipe));
10923 
10924 	i9xx_get_pipe_color_config(pipe_config);
10925 	intel_color_get_config(pipe_config);
10926 
10927 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
10928 		struct intel_shared_dpll *pll;
10929 		enum intel_dpll_id pll_id;
10930 
10931 		pipe_config->has_pch_encoder = true;
10932 
10933 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
10934 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10935 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
10936 
10937 		ilk_get_fdi_m_n_config(crtc, pipe_config);
10938 
10939 		if (HAS_PCH_IBX(dev_priv)) {
10940 			/*
10941 			 * The pipe->pch transcoder and pch transcoder->pll
10942 			 * mapping is fixed.
10943 			 */
10944 			pll_id = (enum intel_dpll_id) crtc->pipe;
10945 		} else {
10946 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
10947 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
10948 				pll_id = DPLL_ID_PCH_PLL_B;
10949 			else
10950 				pll_id= DPLL_ID_PCH_PLL_A;
10951 		}
10952 
10953 		pipe_config->shared_dpll =
10954 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
10955 		pll = pipe_config->shared_dpll;
10956 
10957 		drm_WARN_ON(dev, !pll->info->funcs->get_hw_state(dev_priv, pll,
10958 						 &pipe_config->dpll_hw_state));
10959 
10960 		tmp = pipe_config->dpll_hw_state.dpll;
10961 		pipe_config->pixel_multiplier =
10962 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10963 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10964 
10965 		ilk_pch_clock_get(crtc, pipe_config);
10966 	} else {
10967 		pipe_config->pixel_multiplier = 1;
10968 	}
10969 
10970 	intel_get_transcoder_timings(crtc, pipe_config);
10971 	intel_get_pipe_src_size(crtc, pipe_config);
10972 
10973 	ilk_get_pfit_config(pipe_config);
10974 
10975 	ret = true;
10976 
10977 out:
10978 	intel_display_power_put(dev_priv, power_domain, wakeref);
10979 
10980 	return ret;
10981 }
10982 
10983 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
10984 				  struct intel_crtc_state *crtc_state)
10985 {
10986 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10987 	struct intel_atomic_state *state =
10988 		to_intel_atomic_state(crtc_state->uapi.state);
10989 
10990 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10991 	    INTEL_GEN(dev_priv) >= 11) {
10992 		struct intel_encoder *encoder =
10993 			intel_get_crtc_new_encoder(state, crtc_state);
10994 
10995 		if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10996 			drm_dbg_kms(&dev_priv->drm,
10997 				    "failed to find PLL for pipe %c\n",
10998 				    pipe_name(crtc->pipe));
10999 			return -EINVAL;
11000 		}
11001 	}
11002 
11003 	return 0;
11004 }
11005 
11006 static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11007 			    struct intel_crtc_state *pipe_config)
11008 {
11009 	enum intel_dpll_id id;
11010 	u32 temp;
11011 
11012 	temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
11013 	id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
11014 
11015 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
11016 		return;
11017 
11018 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11019 }
11020 
11021 static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11022 			    struct intel_crtc_state *pipe_config)
11023 {
11024 	enum phy phy = intel_port_to_phy(dev_priv, port);
11025 	enum icl_port_dpll_id port_dpll_id;
11026 	enum intel_dpll_id id;
11027 	u32 temp;
11028 
11029 	if (intel_phy_is_combo(dev_priv, phy)) {
11030 		u32 mask, shift;
11031 
11032 		if (IS_ROCKETLAKE(dev_priv)) {
11033 			mask = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11034 			shift = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11035 		} else {
11036 			mask = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
11037 			shift = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
11038 		}
11039 
11040 		temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) & mask;
11041 		id = temp >> shift;
11042 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
11043 	} else if (intel_phy_is_tc(dev_priv, phy)) {
11044 		u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
11045 
11046 		if (clk_sel == DDI_CLK_SEL_MG) {
11047 			id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
11048 								    port));
11049 			port_dpll_id = ICL_PORT_DPLL_MG_PHY;
11050 		} else {
11051 			drm_WARN_ON(&dev_priv->drm,
11052 				    clk_sel < DDI_CLK_SEL_TBT_162);
11053 			id = DPLL_ID_ICL_TBTPLL;
11054 			port_dpll_id = ICL_PORT_DPLL_DEFAULT;
11055 		}
11056 	} else {
11057 		drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
11058 		return;
11059 	}
11060 
11061 	pipe_config->icl_port_dplls[port_dpll_id].pll =
11062 		intel_get_shared_dpll_by_id(dev_priv, id);
11063 
11064 	icl_set_active_port_dpll(pipe_config, port_dpll_id);
11065 }
11066 
11067 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
11068 				enum port port,
11069 				struct intel_crtc_state *pipe_config)
11070 {
11071 	enum intel_dpll_id id;
11072 
11073 	switch (port) {
11074 	case PORT_A:
11075 		id = DPLL_ID_SKL_DPLL0;
11076 		break;
11077 	case PORT_B:
11078 		id = DPLL_ID_SKL_DPLL1;
11079 		break;
11080 	case PORT_C:
11081 		id = DPLL_ID_SKL_DPLL2;
11082 		break;
11083 	default:
11084 		drm_err(&dev_priv->drm, "Incorrect port type\n");
11085 		return;
11086 	}
11087 
11088 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11089 }
11090 
11091 static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11092 			    struct intel_crtc_state *pipe_config)
11093 {
11094 	enum intel_dpll_id id;
11095 	u32 temp;
11096 
11097 	temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
11098 	id = temp >> (port * 3 + 1);
11099 
11100 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
11101 		return;
11102 
11103 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11104 }
11105 
11106 static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11107 			    struct intel_crtc_state *pipe_config)
11108 {
11109 	enum intel_dpll_id id;
11110 	u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
11111 
11112 	switch (ddi_pll_sel) {
11113 	case PORT_CLK_SEL_WRPLL1:
11114 		id = DPLL_ID_WRPLL1;
11115 		break;
11116 	case PORT_CLK_SEL_WRPLL2:
11117 		id = DPLL_ID_WRPLL2;
11118 		break;
11119 	case PORT_CLK_SEL_SPLL:
11120 		id = DPLL_ID_SPLL;
11121 		break;
11122 	case PORT_CLK_SEL_LCPLL_810:
11123 		id = DPLL_ID_LCPLL_810;
11124 		break;
11125 	case PORT_CLK_SEL_LCPLL_1350:
11126 		id = DPLL_ID_LCPLL_1350;
11127 		break;
11128 	case PORT_CLK_SEL_LCPLL_2700:
11129 		id = DPLL_ID_LCPLL_2700;
11130 		break;
11131 	default:
11132 		MISSING_CASE(ddi_pll_sel);
11133 		fallthrough;
11134 	case PORT_CLK_SEL_NONE:
11135 		return;
11136 	}
11137 
11138 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11139 }
11140 
11141 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
11142 				     struct intel_crtc_state *pipe_config,
11143 				     u64 *power_domain_mask,
11144 				     intel_wakeref_t *wakerefs)
11145 {
11146 	struct drm_device *dev = crtc->base.dev;
11147 	struct drm_i915_private *dev_priv = to_i915(dev);
11148 	enum intel_display_power_domain power_domain;
11149 	unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
11150 	unsigned long enabled_panel_transcoders = 0;
11151 	enum transcoder panel_transcoder;
11152 	intel_wakeref_t wf;
11153 	u32 tmp;
11154 
11155 	if (INTEL_GEN(dev_priv) >= 11)
11156 		panel_transcoder_mask |=
11157 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
11158 
11159 	/*
11160 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
11161 	 * and DSI transcoders handled below.
11162 	 */
11163 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
11164 
11165 	/*
11166 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
11167 	 * consistency and less surprising code; it's in always on power).
11168 	 */
11169 	for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
11170 				       panel_transcoder_mask) {
11171 		bool force_thru = false;
11172 		enum pipe trans_pipe;
11173 
11174 		tmp = intel_de_read(dev_priv,
11175 				    TRANS_DDI_FUNC_CTL(panel_transcoder));
11176 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11177 			continue;
11178 
11179 		/*
11180 		 * Log all enabled ones, only use the first one.
11181 		 *
11182 		 * FIXME: This won't work for two separate DSI displays.
11183 		 */
11184 		enabled_panel_transcoders |= BIT(panel_transcoder);
11185 		if (enabled_panel_transcoders != BIT(panel_transcoder))
11186 			continue;
11187 
11188 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
11189 		default:
11190 			drm_WARN(dev, 1,
11191 				 "unknown pipe linked to transcoder %s\n",
11192 				 transcoder_name(panel_transcoder));
11193 			fallthrough;
11194 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
11195 			force_thru = true;
11196 			fallthrough;
11197 		case TRANS_DDI_EDP_INPUT_A_ON:
11198 			trans_pipe = PIPE_A;
11199 			break;
11200 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
11201 			trans_pipe = PIPE_B;
11202 			break;
11203 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
11204 			trans_pipe = PIPE_C;
11205 			break;
11206 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
11207 			trans_pipe = PIPE_D;
11208 			break;
11209 		}
11210 
11211 		if (trans_pipe == crtc->pipe) {
11212 			pipe_config->cpu_transcoder = panel_transcoder;
11213 			pipe_config->pch_pfit.force_thru = force_thru;
11214 		}
11215 	}
11216 
11217 	/*
11218 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
11219 	 */
11220 	drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
11221 		    enabled_panel_transcoders != BIT(TRANSCODER_EDP));
11222 
11223 	power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
11224 	drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
11225 
11226 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11227 	if (!wf)
11228 		return false;
11229 
11230 	wakerefs[power_domain] = wf;
11231 	*power_domain_mask |= BIT_ULL(power_domain);
11232 
11233 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
11234 
11235 	return tmp & PIPECONF_ENABLE;
11236 }
11237 
11238 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
11239 					 struct intel_crtc_state *pipe_config,
11240 					 u64 *power_domain_mask,
11241 					 intel_wakeref_t *wakerefs)
11242 {
11243 	struct drm_device *dev = crtc->base.dev;
11244 	struct drm_i915_private *dev_priv = to_i915(dev);
11245 	enum intel_display_power_domain power_domain;
11246 	enum transcoder cpu_transcoder;
11247 	intel_wakeref_t wf;
11248 	enum port port;
11249 	u32 tmp;
11250 
11251 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
11252 		if (port == PORT_A)
11253 			cpu_transcoder = TRANSCODER_DSI_A;
11254 		else
11255 			cpu_transcoder = TRANSCODER_DSI_C;
11256 
11257 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
11258 		drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
11259 
11260 		wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11261 		if (!wf)
11262 			continue;
11263 
11264 		wakerefs[power_domain] = wf;
11265 		*power_domain_mask |= BIT_ULL(power_domain);
11266 
11267 		/*
11268 		 * The PLL needs to be enabled with a valid divider
11269 		 * configuration, otherwise accessing DSI registers will hang
11270 		 * the machine. See BSpec North Display Engine
11271 		 * registers/MIPI[BXT]. We can break out here early, since we
11272 		 * need the same DSI PLL to be enabled for both DSI ports.
11273 		 */
11274 		if (!bxt_dsi_pll_is_enabled(dev_priv))
11275 			break;
11276 
11277 		/* XXX: this works for video mode only */
11278 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
11279 		if (!(tmp & DPI_ENABLE))
11280 			continue;
11281 
11282 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
11283 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11284 			continue;
11285 
11286 		pipe_config->cpu_transcoder = cpu_transcoder;
11287 		break;
11288 	}
11289 
11290 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
11291 }
11292 
11293 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11294 				   struct intel_crtc_state *pipe_config)
11295 {
11296 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11297 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
11298 	struct intel_shared_dpll *pll;
11299 	enum port port;
11300 	u32 tmp;
11301 
11302 	if (transcoder_is_dsi(cpu_transcoder)) {
11303 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11304 						PORT_A : PORT_B;
11305 	} else {
11306 		tmp = intel_de_read(dev_priv,
11307 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
11308 		if (INTEL_GEN(dev_priv) >= 12)
11309 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11310 		else
11311 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11312 	}
11313 
11314 	if (INTEL_GEN(dev_priv) >= 11)
11315 		icl_get_ddi_pll(dev_priv, port, pipe_config);
11316 	else if (IS_CANNONLAKE(dev_priv))
11317 		cnl_get_ddi_pll(dev_priv, port, pipe_config);
11318 	else if (IS_GEN9_BC(dev_priv))
11319 		skl_get_ddi_pll(dev_priv, port, pipe_config);
11320 	else if (IS_GEN9_LP(dev_priv))
11321 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
11322 	else
11323 		hsw_get_ddi_pll(dev_priv, port, pipe_config);
11324 
11325 	pll = pipe_config->shared_dpll;
11326 	if (pll) {
11327 		drm_WARN_ON(&dev_priv->drm,
11328 			    !pll->info->funcs->get_hw_state(dev_priv, pll,
11329 						&pipe_config->dpll_hw_state));
11330 	}
11331 
11332 	/*
11333 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11334 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
11335 	 * the PCH transcoder is on.
11336 	 */
11337 	if (INTEL_GEN(dev_priv) < 9 &&
11338 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
11339 		pipe_config->has_pch_encoder = true;
11340 
11341 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
11342 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11343 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
11344 
11345 		ilk_get_fdi_m_n_config(crtc, pipe_config);
11346 	}
11347 }
11348 
11349 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11350 				struct intel_crtc_state *pipe_config)
11351 {
11352 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11353 	intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
11354 	enum intel_display_power_domain power_domain;
11355 	u64 power_domain_mask;
11356 	bool active;
11357 	u32 tmp;
11358 
11359 	pipe_config->master_transcoder = INVALID_TRANSCODER;
11360 
11361 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
11362 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11363 	if (!wf)
11364 		return false;
11365 
11366 	wakerefs[power_domain] = wf;
11367 	power_domain_mask = BIT_ULL(power_domain);
11368 
11369 	pipe_config->shared_dpll = NULL;
11370 
11371 	active = hsw_get_transcoder_state(crtc, pipe_config,
11372 					  &power_domain_mask, wakerefs);
11373 
11374 	if (IS_GEN9_LP(dev_priv) &&
11375 	    bxt_get_dsi_transcoder_state(crtc, pipe_config,
11376 					 &power_domain_mask, wakerefs)) {
11377 		drm_WARN_ON(&dev_priv->drm, active);
11378 		active = true;
11379 	}
11380 
11381 	if (!active)
11382 		goto out;
11383 
11384 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
11385 	    INTEL_GEN(dev_priv) >= 11) {
11386 		hsw_get_ddi_port_state(crtc, pipe_config);
11387 		intel_get_transcoder_timings(crtc, pipe_config);
11388 	}
11389 
11390 	intel_get_pipe_src_size(crtc, pipe_config);
11391 
11392 	if (IS_HASWELL(dev_priv)) {
11393 		u32 tmp = intel_de_read(dev_priv,
11394 					PIPECONF(pipe_config->cpu_transcoder));
11395 
11396 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11397 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11398 		else
11399 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11400 	} else {
11401 		pipe_config->output_format =
11402 			bdw_get_pipemisc_output_format(crtc);
11403 	}
11404 
11405 	pipe_config->gamma_mode = intel_de_read(dev_priv,
11406 						GAMMA_MODE(crtc->pipe));
11407 
11408 	pipe_config->csc_mode = intel_de_read(dev_priv,
11409 					      PIPE_CSC_MODE(crtc->pipe));
11410 
11411 	if (INTEL_GEN(dev_priv) >= 9) {
11412 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
11413 
11414 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11415 			pipe_config->gamma_enable = true;
11416 
11417 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11418 			pipe_config->csc_enable = true;
11419 	} else {
11420 		i9xx_get_pipe_color_config(pipe_config);
11421 	}
11422 
11423 	intel_color_get_config(pipe_config);
11424 
11425 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11426 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11427 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11428 		pipe_config->ips_linetime =
11429 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11430 
11431 	power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
11432 	drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
11433 
11434 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11435 	if (wf) {
11436 		wakerefs[power_domain] = wf;
11437 		power_domain_mask |= BIT_ULL(power_domain);
11438 
11439 		if (INTEL_GEN(dev_priv) >= 9)
11440 			skl_get_pfit_config(pipe_config);
11441 		else
11442 			ilk_get_pfit_config(pipe_config);
11443 	}
11444 
11445 	if (hsw_crtc_supports_ips(crtc)) {
11446 		if (IS_HASWELL(dev_priv))
11447 			pipe_config->ips_enabled = intel_de_read(dev_priv,
11448 								 IPS_CTL) & IPS_ENABLE;
11449 		else {
11450 			/*
11451 			 * We cannot readout IPS state on broadwell, set to
11452 			 * true so we can set it to a defined state on first
11453 			 * commit.
11454 			 */
11455 			pipe_config->ips_enabled = true;
11456 		}
11457 	}
11458 
11459 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
11460 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
11461 		pipe_config->pixel_multiplier =
11462 			intel_de_read(dev_priv,
11463 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
11464 	} else {
11465 		pipe_config->pixel_multiplier = 1;
11466 	}
11467 
11468 out:
11469 	for_each_power_domain(power_domain, power_domain_mask)
11470 		intel_display_power_put(dev_priv,
11471 					power_domain, wakerefs[power_domain]);
11472 
11473 	return active;
11474 }
11475 
11476 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
11477 {
11478 	struct drm_i915_private *dev_priv =
11479 		to_i915(plane_state->uapi.plane->dev);
11480 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11481 	const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11482 	u32 base;
11483 
11484 	if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
11485 		base = sg_dma_address(obj->mm.pages->sgl);
11486 	else
11487 		base = intel_plane_ggtt_offset(plane_state);
11488 
11489 	return base + plane_state->color_plane[0].offset;
11490 }
11491 
11492 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11493 {
11494 	int x = plane_state->uapi.dst.x1;
11495 	int y = plane_state->uapi.dst.y1;
11496 	u32 pos = 0;
11497 
11498 	if (x < 0) {
11499 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11500 		x = -x;
11501 	}
11502 	pos |= x << CURSOR_X_SHIFT;
11503 
11504 	if (y < 0) {
11505 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11506 		y = -y;
11507 	}
11508 	pos |= y << CURSOR_Y_SHIFT;
11509 
11510 	return pos;
11511 }
11512 
11513 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11514 {
11515 	const struct drm_mode_config *config =
11516 		&plane_state->uapi.plane->dev->mode_config;
11517 	int width = drm_rect_width(&plane_state->uapi.dst);
11518 	int height = drm_rect_height(&plane_state->uapi.dst);
11519 
11520 	return width > 0 && width <= config->cursor_width &&
11521 		height > 0 && height <= config->cursor_height;
11522 }
11523 
11524 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
11525 {
11526 	struct drm_i915_private *dev_priv =
11527 		to_i915(plane_state->uapi.plane->dev);
11528 	unsigned int rotation = plane_state->hw.rotation;
11529 	int src_x, src_y;
11530 	u32 offset;
11531 	int ret;
11532 
11533 	ret = intel_plane_compute_gtt(plane_state);
11534 	if (ret)
11535 		return ret;
11536 
11537 	if (!plane_state->uapi.visible)
11538 		return 0;
11539 
11540 	src_x = plane_state->uapi.src.x1 >> 16;
11541 	src_y = plane_state->uapi.src.y1 >> 16;
11542 
11543 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11544 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11545 						    plane_state, 0);
11546 
11547 	if (src_x != 0 || src_y != 0) {
11548 		drm_dbg_kms(&dev_priv->drm,
11549 			    "Arbitrary cursor panning not supported\n");
11550 		return -EINVAL;
11551 	}
11552 
11553 	/*
11554 	 * Put the final coordinates back so that the src
11555 	 * coordinate checks will see the right values.
11556 	 */
11557 	drm_rect_translate_to(&plane_state->uapi.src,
11558 			      src_x << 16, src_y << 16);
11559 
11560 	/* ILK+ do this automagically in hardware */
11561 	if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
11562 		const struct drm_framebuffer *fb = plane_state->hw.fb;
11563 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11564 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
11565 
11566 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
11567 	}
11568 
11569 	plane_state->color_plane[0].offset = offset;
11570 	plane_state->color_plane[0].x = src_x;
11571 	plane_state->color_plane[0].y = src_y;
11572 
11573 	return 0;
11574 }
11575 
11576 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11577 			      struct intel_plane_state *plane_state)
11578 {
11579 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11580 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11581 	int ret;
11582 
11583 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
11584 		drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
11585 		return -EINVAL;
11586 	}
11587 
11588 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
11589 						  &crtc_state->uapi,
11590 						  DRM_PLANE_HELPER_NO_SCALING,
11591 						  DRM_PLANE_HELPER_NO_SCALING,
11592 						  true, true);
11593 	if (ret)
11594 		return ret;
11595 
11596 	/* Use the unclipped src/dst rectangles, which we program to hw */
11597 	plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11598 	plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
11599 
11600 	ret = intel_cursor_check_surface(plane_state);
11601 	if (ret)
11602 		return ret;
11603 
11604 	if (!plane_state->uapi.visible)
11605 		return 0;
11606 
11607 	ret = intel_plane_check_src_coordinates(plane_state);
11608 	if (ret)
11609 		return ret;
11610 
11611 	return 0;
11612 }
11613 
11614 static unsigned int
11615 i845_cursor_max_stride(struct intel_plane *plane,
11616 		       u32 pixel_format, u64 modifier,
11617 		       unsigned int rotation)
11618 {
11619 	return 2048;
11620 }
11621 
11622 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11623 {
11624 	u32 cntl = 0;
11625 
11626 	if (crtc_state->gamma_enable)
11627 		cntl |= CURSOR_GAMMA_ENABLE;
11628 
11629 	return cntl;
11630 }
11631 
11632 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11633 			   const struct intel_plane_state *plane_state)
11634 {
11635 	return CURSOR_ENABLE |
11636 		CURSOR_FORMAT_ARGB |
11637 		CURSOR_STRIDE(plane_state->color_plane[0].stride);
11638 }
11639 
11640 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11641 {
11642 	int width = drm_rect_width(&plane_state->uapi.dst);
11643 
11644 	/*
11645 	 * 845g/865g are only limited by the width of their cursors,
11646 	 * the height is arbitrary up to the precision of the register.
11647 	 */
11648 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
11649 }
11650 
11651 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
11652 			     struct intel_plane_state *plane_state)
11653 {
11654 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11655 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11656 	int ret;
11657 
11658 	ret = intel_check_cursor(crtc_state, plane_state);
11659 	if (ret)
11660 		return ret;
11661 
11662 	/* if we want to turn off the cursor ignore width and height */
11663 	if (!fb)
11664 		return 0;
11665 
11666 	/* Check for which cursor types we support */
11667 	if (!i845_cursor_size_ok(plane_state)) {
11668 		drm_dbg_kms(&i915->drm,
11669 			    "Cursor dimension %dx%d not supported\n",
11670 			    drm_rect_width(&plane_state->uapi.dst),
11671 			    drm_rect_height(&plane_state->uapi.dst));
11672 		return -EINVAL;
11673 	}
11674 
11675 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11676 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11677 
11678 	switch (fb->pitches[0]) {
11679 	case 256:
11680 	case 512:
11681 	case 1024:
11682 	case 2048:
11683 		break;
11684 	default:
11685 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11686 			     fb->pitches[0]);
11687 		return -EINVAL;
11688 	}
11689 
11690 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11691 
11692 	return 0;
11693 }
11694 
11695 static void i845_update_cursor(struct intel_plane *plane,
11696 			       const struct intel_crtc_state *crtc_state,
11697 			       const struct intel_plane_state *plane_state)
11698 {
11699 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11700 	u32 cntl = 0, base = 0, pos = 0, size = 0;
11701 	unsigned long irqflags;
11702 
11703 	if (plane_state && plane_state->uapi.visible) {
11704 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11705 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
11706 
11707 		cntl = plane_state->ctl |
11708 			i845_cursor_ctl_crtc(crtc_state);
11709 
11710 		size = (height << 12) | width;
11711 
11712 		base = intel_cursor_base(plane_state);
11713 		pos = intel_cursor_position(plane_state);
11714 	}
11715 
11716 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11717 
11718 	/* On these chipsets we can only modify the base/size/stride
11719 	 * whilst the cursor is disabled.
11720 	 */
11721 	if (plane->cursor.base != base ||
11722 	    plane->cursor.size != size ||
11723 	    plane->cursor.cntl != cntl) {
11724 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11725 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11726 		intel_de_write_fw(dev_priv, CURSIZE, size);
11727 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11728 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
11729 
11730 		plane->cursor.base = base;
11731 		plane->cursor.size = size;
11732 		plane->cursor.cntl = cntl;
11733 	} else {
11734 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11735 	}
11736 
11737 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11738 }
11739 
11740 static void i845_disable_cursor(struct intel_plane *plane,
11741 				const struct intel_crtc_state *crtc_state)
11742 {
11743 	i845_update_cursor(plane, crtc_state, NULL);
11744 }
11745 
11746 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11747 				     enum pipe *pipe)
11748 {
11749 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11750 	enum intel_display_power_domain power_domain;
11751 	intel_wakeref_t wakeref;
11752 	bool ret;
11753 
11754 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
11755 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11756 	if (!wakeref)
11757 		return false;
11758 
11759 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
11760 
11761 	*pipe = PIPE_A;
11762 
11763 	intel_display_power_put(dev_priv, power_domain, wakeref);
11764 
11765 	return ret;
11766 }
11767 
11768 static unsigned int
11769 i9xx_cursor_max_stride(struct intel_plane *plane,
11770 		       u32 pixel_format, u64 modifier,
11771 		       unsigned int rotation)
11772 {
11773 	return plane->base.dev->mode_config.cursor_width * 4;
11774 }
11775 
11776 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11777 {
11778 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11779 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11780 	u32 cntl = 0;
11781 
11782 	if (INTEL_GEN(dev_priv) >= 11)
11783 		return cntl;
11784 
11785 	if (crtc_state->gamma_enable)
11786 		cntl = MCURSOR_GAMMA_ENABLE;
11787 
11788 	if (crtc_state->csc_enable)
11789 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
11790 
11791 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11792 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
11793 
11794 	return cntl;
11795 }
11796 
11797 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11798 			   const struct intel_plane_state *plane_state)
11799 {
11800 	struct drm_i915_private *dev_priv =
11801 		to_i915(plane_state->uapi.plane->dev);
11802 	u32 cntl = 0;
11803 
11804 	if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11805 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11806 
11807 	switch (drm_rect_width(&plane_state->uapi.dst)) {
11808 	case 64:
11809 		cntl |= MCURSOR_MODE_64_ARGB_AX;
11810 		break;
11811 	case 128:
11812 		cntl |= MCURSOR_MODE_128_ARGB_AX;
11813 		break;
11814 	case 256:
11815 		cntl |= MCURSOR_MODE_256_ARGB_AX;
11816 		break;
11817 	default:
11818 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
11819 		return 0;
11820 	}
11821 
11822 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
11823 		cntl |= MCURSOR_ROTATE_180;
11824 
11825 	return cntl;
11826 }
11827 
11828 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
11829 {
11830 	struct drm_i915_private *dev_priv =
11831 		to_i915(plane_state->uapi.plane->dev);
11832 	int width = drm_rect_width(&plane_state->uapi.dst);
11833 	int height = drm_rect_height(&plane_state->uapi.dst);
11834 
11835 	if (!intel_cursor_size_ok(plane_state))
11836 		return false;
11837 
11838 	/* Cursor width is limited to a few power-of-two sizes */
11839 	switch (width) {
11840 	case 256:
11841 	case 128:
11842 	case 64:
11843 		break;
11844 	default:
11845 		return false;
11846 	}
11847 
11848 	/*
11849 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11850 	 * height from 8 lines up to the cursor width, when the
11851 	 * cursor is not rotated. Everything else requires square
11852 	 * cursors.
11853 	 */
11854 	if (HAS_CUR_FBC(dev_priv) &&
11855 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
11856 		if (height < 8 || height > width)
11857 			return false;
11858 	} else {
11859 		if (height != width)
11860 			return false;
11861 	}
11862 
11863 	return true;
11864 }
11865 
11866 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
11867 			     struct intel_plane_state *plane_state)
11868 {
11869 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11870 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11871 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11872 	enum pipe pipe = plane->pipe;
11873 	int ret;
11874 
11875 	ret = intel_check_cursor(crtc_state, plane_state);
11876 	if (ret)
11877 		return ret;
11878 
11879 	/* if we want to turn off the cursor ignore width and height */
11880 	if (!fb)
11881 		return 0;
11882 
11883 	/* Check for which cursor types we support */
11884 	if (!i9xx_cursor_size_ok(plane_state)) {
11885 		drm_dbg(&dev_priv->drm,
11886 			"Cursor dimension %dx%d not supported\n",
11887 			drm_rect_width(&plane_state->uapi.dst),
11888 			drm_rect_height(&plane_state->uapi.dst));
11889 		return -EINVAL;
11890 	}
11891 
11892 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
11893 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11894 
11895 	if (fb->pitches[0] !=
11896 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
11897 		drm_dbg_kms(&dev_priv->drm,
11898 			    "Invalid cursor stride (%u) (cursor width %d)\n",
11899 			    fb->pitches[0],
11900 			    drm_rect_width(&plane_state->uapi.dst));
11901 		return -EINVAL;
11902 	}
11903 
11904 	/*
11905 	 * There's something wrong with the cursor on CHV pipe C.
11906 	 * If it straddles the left edge of the screen then
11907 	 * moving it away from the edge or disabling it often
11908 	 * results in a pipe underrun, and often that can lead to
11909 	 * dead pipe (constant underrun reported, and it scans
11910 	 * out just a solid color). To recover from that, the
11911 	 * display power well must be turned off and on again.
11912 	 * Refuse the put the cursor into that compromised position.
11913 	 */
11914 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
11915 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
11916 		drm_dbg_kms(&dev_priv->drm,
11917 			    "CHV cursor C not allowed to straddle the left screen edge\n");
11918 		return -EINVAL;
11919 	}
11920 
11921 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
11922 
11923 	return 0;
11924 }
11925 
11926 static void i9xx_update_cursor(struct intel_plane *plane,
11927 			       const struct intel_crtc_state *crtc_state,
11928 			       const struct intel_plane_state *plane_state)
11929 {
11930 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11931 	enum pipe pipe = plane->pipe;
11932 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
11933 	unsigned long irqflags;
11934 
11935 	if (plane_state && plane_state->uapi.visible) {
11936 		unsigned width = drm_rect_width(&plane_state->uapi.dst);
11937 		unsigned height = drm_rect_height(&plane_state->uapi.dst);
11938 
11939 		cntl = plane_state->ctl |
11940 			i9xx_cursor_ctl_crtc(crtc_state);
11941 
11942 		if (width != height)
11943 			fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
11944 
11945 		base = intel_cursor_base(plane_state);
11946 		pos = intel_cursor_position(plane_state);
11947 	}
11948 
11949 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11950 
11951 	/*
11952 	 * On some platforms writing CURCNTR first will also
11953 	 * cause CURPOS to be armed by the CURBASE write.
11954 	 * Without the CURCNTR write the CURPOS write would
11955 	 * arm itself. Thus we always update CURCNTR before
11956 	 * CURPOS.
11957 	 *
11958 	 * On other platforms CURPOS always requires the
11959 	 * CURBASE write to arm the update. Additonally
11960 	 * a write to any of the cursor register will cancel
11961 	 * an already armed cursor update. Thus leaving out
11962 	 * the CURBASE write after CURPOS could lead to a
11963 	 * cursor that doesn't appear to move, or even change
11964 	 * shape. Thus we always write CURBASE.
11965 	 *
11966 	 * The other registers are armed by by the CURBASE write
11967 	 * except when the plane is getting enabled at which time
11968 	 * the CURCNTR write arms the update.
11969 	 */
11970 
11971 	if (INTEL_GEN(dev_priv) >= 9)
11972 		skl_write_cursor_wm(plane, crtc_state);
11973 
11974 	if (!needs_modeset(crtc_state))
11975 		intel_psr2_program_plane_sel_fetch(plane, crtc_state, plane_state, 0);
11976 
11977 	if (plane->cursor.base != base ||
11978 	    plane->cursor.size != fbc_ctl ||
11979 	    plane->cursor.cntl != cntl) {
11980 		if (HAS_CUR_FBC(dev_priv))
11981 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
11982 					  fbc_ctl);
11983 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
11984 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11985 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11986 
11987 		plane->cursor.base = base;
11988 		plane->cursor.size = fbc_ctl;
11989 		plane->cursor.cntl = cntl;
11990 	} else {
11991 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11992 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11993 	}
11994 
11995 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11996 }
11997 
11998 static void i9xx_disable_cursor(struct intel_plane *plane,
11999 				const struct intel_crtc_state *crtc_state)
12000 {
12001 	i9xx_update_cursor(plane, crtc_state, NULL);
12002 }
12003 
12004 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
12005 				     enum pipe *pipe)
12006 {
12007 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
12008 	enum intel_display_power_domain power_domain;
12009 	intel_wakeref_t wakeref;
12010 	bool ret;
12011 	u32 val;
12012 
12013 	/*
12014 	 * Not 100% correct for planes that can move between pipes,
12015 	 * but that's only the case for gen2-3 which don't have any
12016 	 * display power wells.
12017 	 */
12018 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
12019 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
12020 	if (!wakeref)
12021 		return false;
12022 
12023 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
12024 
12025 	ret = val & MCURSOR_MODE;
12026 
12027 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
12028 		*pipe = plane->pipe;
12029 	else
12030 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
12031 			MCURSOR_PIPE_SELECT_SHIFT;
12032 
12033 	intel_display_power_put(dev_priv, power_domain, wakeref);
12034 
12035 	return ret;
12036 }
12037 
12038 /* VESA 640x480x72Hz mode to set on the pipe */
12039 static const struct drm_display_mode load_detect_mode = {
12040 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
12041 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
12042 };
12043 
12044 struct drm_framebuffer *
12045 intel_framebuffer_create(struct drm_i915_gem_object *obj,
12046 			 struct drm_mode_fb_cmd2 *mode_cmd)
12047 {
12048 	struct intel_framebuffer *intel_fb;
12049 	int ret;
12050 
12051 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
12052 	if (!intel_fb)
12053 		return ERR_PTR(-ENOMEM);
12054 
12055 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
12056 	if (ret)
12057 		goto err;
12058 
12059 	return &intel_fb->base;
12060 
12061 err:
12062 	kfree(intel_fb);
12063 	return ERR_PTR(ret);
12064 }
12065 
12066 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
12067 					struct drm_crtc *crtc)
12068 {
12069 	struct drm_plane *plane;
12070 	struct drm_plane_state *plane_state;
12071 	int ret, i;
12072 
12073 	ret = drm_atomic_add_affected_planes(state, crtc);
12074 	if (ret)
12075 		return ret;
12076 
12077 	for_each_new_plane_in_state(state, plane, plane_state, i) {
12078 		if (plane_state->crtc != crtc)
12079 			continue;
12080 
12081 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
12082 		if (ret)
12083 			return ret;
12084 
12085 		drm_atomic_set_fb_for_plane(plane_state, NULL);
12086 	}
12087 
12088 	return 0;
12089 }
12090 
12091 int intel_get_load_detect_pipe(struct drm_connector *connector,
12092 			       struct intel_load_detect_pipe *old,
12093 			       struct drm_modeset_acquire_ctx *ctx)
12094 {
12095 	struct intel_crtc *intel_crtc;
12096 	struct intel_encoder *intel_encoder =
12097 		intel_attached_encoder(to_intel_connector(connector));
12098 	struct drm_crtc *possible_crtc;
12099 	struct drm_encoder *encoder = &intel_encoder->base;
12100 	struct drm_crtc *crtc = NULL;
12101 	struct drm_device *dev = encoder->dev;
12102 	struct drm_i915_private *dev_priv = to_i915(dev);
12103 	struct drm_mode_config *config = &dev->mode_config;
12104 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
12105 	struct drm_connector_state *connector_state;
12106 	struct intel_crtc_state *crtc_state;
12107 	int ret, i = -1;
12108 
12109 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12110 		    connector->base.id, connector->name,
12111 		    encoder->base.id, encoder->name);
12112 
12113 	old->restore_state = NULL;
12114 
12115 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
12116 
12117 	/*
12118 	 * Algorithm gets a little messy:
12119 	 *
12120 	 *   - if the connector already has an assigned crtc, use it (but make
12121 	 *     sure it's on first)
12122 	 *
12123 	 *   - try to find the first unused crtc that can drive this connector,
12124 	 *     and use that if we find one
12125 	 */
12126 
12127 	/* See if we already have a CRTC for this connector */
12128 	if (connector->state->crtc) {
12129 		crtc = connector->state->crtc;
12130 
12131 		ret = drm_modeset_lock(&crtc->mutex, ctx);
12132 		if (ret)
12133 			goto fail;
12134 
12135 		/* Make sure the crtc and connector are running */
12136 		goto found;
12137 	}
12138 
12139 	/* Find an unused one (if possible) */
12140 	for_each_crtc(dev, possible_crtc) {
12141 		i++;
12142 		if (!(encoder->possible_crtcs & (1 << i)))
12143 			continue;
12144 
12145 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
12146 		if (ret)
12147 			goto fail;
12148 
12149 		if (possible_crtc->state->enable) {
12150 			drm_modeset_unlock(&possible_crtc->mutex);
12151 			continue;
12152 		}
12153 
12154 		crtc = possible_crtc;
12155 		break;
12156 	}
12157 
12158 	/*
12159 	 * If we didn't find an unused CRTC, don't use any.
12160 	 */
12161 	if (!crtc) {
12162 		drm_dbg_kms(&dev_priv->drm,
12163 			    "no pipe available for load-detect\n");
12164 		ret = -ENODEV;
12165 		goto fail;
12166 	}
12167 
12168 found:
12169 	intel_crtc = to_intel_crtc(crtc);
12170 
12171 	state = drm_atomic_state_alloc(dev);
12172 	restore_state = drm_atomic_state_alloc(dev);
12173 	if (!state || !restore_state) {
12174 		ret = -ENOMEM;
12175 		goto fail;
12176 	}
12177 
12178 	state->acquire_ctx = ctx;
12179 	restore_state->acquire_ctx = ctx;
12180 
12181 	connector_state = drm_atomic_get_connector_state(state, connector);
12182 	if (IS_ERR(connector_state)) {
12183 		ret = PTR_ERR(connector_state);
12184 		goto fail;
12185 	}
12186 
12187 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
12188 	if (ret)
12189 		goto fail;
12190 
12191 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12192 	if (IS_ERR(crtc_state)) {
12193 		ret = PTR_ERR(crtc_state);
12194 		goto fail;
12195 	}
12196 
12197 	crtc_state->uapi.active = true;
12198 
12199 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
12200 					   &load_detect_mode);
12201 	if (ret)
12202 		goto fail;
12203 
12204 	ret = intel_modeset_disable_planes(state, crtc);
12205 	if (ret)
12206 		goto fail;
12207 
12208 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12209 	if (!ret)
12210 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
12211 	if (!ret)
12212 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
12213 	if (ret) {
12214 		drm_dbg_kms(&dev_priv->drm,
12215 			    "Failed to create a copy of old state to restore: %i\n",
12216 			    ret);
12217 		goto fail;
12218 	}
12219 
12220 	ret = drm_atomic_commit(state);
12221 	if (ret) {
12222 		drm_dbg_kms(&dev_priv->drm,
12223 			    "failed to set mode on load-detect pipe\n");
12224 		goto fail;
12225 	}
12226 
12227 	old->restore_state = restore_state;
12228 	drm_atomic_state_put(state);
12229 
12230 	/* let the connector get through one full cycle before testing */
12231 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
12232 	return true;
12233 
12234 fail:
12235 	if (state) {
12236 		drm_atomic_state_put(state);
12237 		state = NULL;
12238 	}
12239 	if (restore_state) {
12240 		drm_atomic_state_put(restore_state);
12241 		restore_state = NULL;
12242 	}
12243 
12244 	if (ret == -EDEADLK)
12245 		return ret;
12246 
12247 	return false;
12248 }
12249 
12250 void intel_release_load_detect_pipe(struct drm_connector *connector,
12251 				    struct intel_load_detect_pipe *old,
12252 				    struct drm_modeset_acquire_ctx *ctx)
12253 {
12254 	struct intel_encoder *intel_encoder =
12255 		intel_attached_encoder(to_intel_connector(connector));
12256 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
12257 	struct drm_encoder *encoder = &intel_encoder->base;
12258 	struct drm_atomic_state *state = old->restore_state;
12259 	int ret;
12260 
12261 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12262 		    connector->base.id, connector->name,
12263 		    encoder->base.id, encoder->name);
12264 
12265 	if (!state)
12266 		return;
12267 
12268 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
12269 	if (ret)
12270 		drm_dbg_kms(&i915->drm,
12271 			    "Couldn't release load detect pipe: %i\n", ret);
12272 	drm_atomic_state_put(state);
12273 }
12274 
12275 static int i9xx_pll_refclk(struct drm_device *dev,
12276 			   const struct intel_crtc_state *pipe_config)
12277 {
12278 	struct drm_i915_private *dev_priv = to_i915(dev);
12279 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12280 
12281 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
12282 		return dev_priv->vbt.lvds_ssc_freq;
12283 	else if (HAS_PCH_SPLIT(dev_priv))
12284 		return 120000;
12285 	else if (!IS_GEN(dev_priv, 2))
12286 		return 96000;
12287 	else
12288 		return 48000;
12289 }
12290 
12291 /* Returns the clock of the currently programmed mode of the given pipe. */
12292 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
12293 				struct intel_crtc_state *pipe_config)
12294 {
12295 	struct drm_device *dev = crtc->base.dev;
12296 	struct drm_i915_private *dev_priv = to_i915(dev);
12297 	enum pipe pipe = crtc->pipe;
12298 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12299 	u32 fp;
12300 	struct dpll clock;
12301 	int port_clock;
12302 	int refclk = i9xx_pll_refclk(dev, pipe_config);
12303 
12304 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
12305 		fp = pipe_config->dpll_hw_state.fp0;
12306 	else
12307 		fp = pipe_config->dpll_hw_state.fp1;
12308 
12309 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
12310 	if (IS_PINEVIEW(dev_priv)) {
12311 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12312 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
12313 	} else {
12314 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12315 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12316 	}
12317 
12318 	if (!IS_GEN(dev_priv, 2)) {
12319 		if (IS_PINEVIEW(dev_priv))
12320 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12321 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
12322 		else
12323 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
12324 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
12325 
12326 		switch (dpll & DPLL_MODE_MASK) {
12327 		case DPLLB_MODE_DAC_SERIAL:
12328 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12329 				5 : 10;
12330 			break;
12331 		case DPLLB_MODE_LVDS:
12332 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12333 				7 : 14;
12334 			break;
12335 		default:
12336 			drm_dbg_kms(&dev_priv->drm,
12337 				    "Unknown DPLL mode %08x in programmed "
12338 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
12339 			return;
12340 		}
12341 
12342 		if (IS_PINEVIEW(dev_priv))
12343 			port_clock = pnv_calc_dpll_params(refclk, &clock);
12344 		else
12345 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
12346 	} else {
12347 		u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12348 								 LVDS);
12349 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
12350 
12351 		if (is_lvds) {
12352 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12353 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
12354 
12355 			if (lvds & LVDS_CLKB_POWER_UP)
12356 				clock.p2 = 7;
12357 			else
12358 				clock.p2 = 14;
12359 		} else {
12360 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
12361 				clock.p1 = 2;
12362 			else {
12363 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12364 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12365 			}
12366 			if (dpll & PLL_P2_DIVIDE_BY_4)
12367 				clock.p2 = 4;
12368 			else
12369 				clock.p2 = 2;
12370 		}
12371 
12372 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
12373 	}
12374 
12375 	/*
12376 	 * This value includes pixel_multiplier. We will use
12377 	 * port_clock to compute adjusted_mode.crtc_clock in the
12378 	 * encoder's get_config() function.
12379 	 */
12380 	pipe_config->port_clock = port_clock;
12381 }
12382 
12383 int intel_dotclock_calculate(int link_freq,
12384 			     const struct intel_link_m_n *m_n)
12385 {
12386 	/*
12387 	 * The calculation for the data clock is:
12388 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
12389 	 * But we want to avoid losing precison if possible, so:
12390 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
12391 	 *
12392 	 * and the link clock is simpler:
12393 	 * link_clock = (m * link_clock) / n
12394 	 */
12395 
12396 	if (!m_n->link_n)
12397 		return 0;
12398 
12399 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
12400 }
12401 
12402 static void ilk_pch_clock_get(struct intel_crtc *crtc,
12403 			      struct intel_crtc_state *pipe_config)
12404 {
12405 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12406 
12407 	/* read out port_clock from the DPLL */
12408 	i9xx_crtc_clock_get(crtc, pipe_config);
12409 
12410 	/*
12411 	 * In case there is an active pipe without active ports,
12412 	 * we may need some idea for the dotclock anyway.
12413 	 * Calculate one based on the FDI configuration.
12414 	 */
12415 	pipe_config->hw.adjusted_mode.crtc_clock =
12416 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12417 					 &pipe_config->fdi_m_n);
12418 }
12419 
12420 static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12421 				   struct intel_crtc *crtc)
12422 {
12423 	memset(crtc_state, 0, sizeof(*crtc_state));
12424 
12425 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12426 
12427 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12428 	crtc_state->master_transcoder = INVALID_TRANSCODER;
12429 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12430 	crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12431 	crtc_state->scaler_state.scaler_id = -1;
12432 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
12433 }
12434 
12435 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12436 {
12437 	struct intel_crtc_state *crtc_state;
12438 
12439 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12440 
12441 	if (crtc_state)
12442 		intel_crtc_state_reset(crtc_state, crtc);
12443 
12444 	return crtc_state;
12445 }
12446 
12447 /* Returns the currently programmed mode of the given encoder. */
12448 struct drm_display_mode *
12449 intel_encoder_current_mode(struct intel_encoder *encoder)
12450 {
12451 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12452 	struct intel_crtc_state *crtc_state;
12453 	struct drm_display_mode *mode;
12454 	struct intel_crtc *crtc;
12455 	enum pipe pipe;
12456 
12457 	if (!encoder->get_hw_state(encoder, &pipe))
12458 		return NULL;
12459 
12460 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12461 
12462 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12463 	if (!mode)
12464 		return NULL;
12465 
12466 	crtc_state = intel_crtc_state_alloc(crtc);
12467 	if (!crtc_state) {
12468 		kfree(mode);
12469 		return NULL;
12470 	}
12471 
12472 	if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
12473 		kfree(crtc_state);
12474 		kfree(mode);
12475 		return NULL;
12476 	}
12477 
12478 	encoder->get_config(encoder, crtc_state);
12479 
12480 	intel_mode_from_pipe_config(mode, crtc_state);
12481 
12482 	kfree(crtc_state);
12483 
12484 	return mode;
12485 }
12486 
12487 static void intel_crtc_destroy(struct drm_crtc *crtc)
12488 {
12489 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12490 
12491 	drm_crtc_cleanup(crtc);
12492 	kfree(intel_crtc);
12493 }
12494 
12495 /**
12496  * intel_wm_need_update - Check whether watermarks need updating
12497  * @cur: current plane state
12498  * @new: new plane state
12499  *
12500  * Check current plane state versus the new one to determine whether
12501  * watermarks need to be recalculated.
12502  *
12503  * Returns true or false.
12504  */
12505 static bool intel_wm_need_update(const struct intel_plane_state *cur,
12506 				 struct intel_plane_state *new)
12507 {
12508 	/* Update watermarks on tiling or size changes. */
12509 	if (new->uapi.visible != cur->uapi.visible)
12510 		return true;
12511 
12512 	if (!cur->hw.fb || !new->hw.fb)
12513 		return false;
12514 
12515 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12516 	    cur->hw.rotation != new->hw.rotation ||
12517 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12518 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12519 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12520 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
12521 		return true;
12522 
12523 	return false;
12524 }
12525 
12526 static bool needs_scaling(const struct intel_plane_state *state)
12527 {
12528 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
12529 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
12530 	int dst_w = drm_rect_width(&state->uapi.dst);
12531 	int dst_h = drm_rect_height(&state->uapi.dst);
12532 
12533 	return (src_w != dst_w || src_h != dst_h);
12534 }
12535 
12536 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
12537 				    struct intel_crtc_state *crtc_state,
12538 				    const struct intel_plane_state *old_plane_state,
12539 				    struct intel_plane_state *plane_state)
12540 {
12541 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12542 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12543 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12544 	bool mode_changed = needs_modeset(crtc_state);
12545 	bool was_crtc_enabled = old_crtc_state->hw.active;
12546 	bool is_crtc_enabled = crtc_state->hw.active;
12547 	bool turn_off, turn_on, visible, was_visible;
12548 	int ret;
12549 
12550 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
12551 		ret = skl_update_scaler_plane(crtc_state, plane_state);
12552 		if (ret)
12553 			return ret;
12554 	}
12555 
12556 	was_visible = old_plane_state->uapi.visible;
12557 	visible = plane_state->uapi.visible;
12558 
12559 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
12560 		was_visible = false;
12561 
12562 	/*
12563 	 * Visibility is calculated as if the crtc was on, but
12564 	 * after scaler setup everything depends on it being off
12565 	 * when the crtc isn't active.
12566 	 *
12567 	 * FIXME this is wrong for watermarks. Watermarks should also
12568 	 * be computed as if the pipe would be active. Perhaps move
12569 	 * per-plane wm computation to the .check_plane() hook, and
12570 	 * only combine the results from all planes in the current place?
12571 	 */
12572 	if (!is_crtc_enabled) {
12573 		intel_plane_set_invisible(crtc_state, plane_state);
12574 		visible = false;
12575 	}
12576 
12577 	if (!was_visible && !visible)
12578 		return 0;
12579 
12580 	turn_off = was_visible && (!visible || mode_changed);
12581 	turn_on = visible && (!was_visible || mode_changed);
12582 
12583 	drm_dbg_atomic(&dev_priv->drm,
12584 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12585 		       crtc->base.base.id, crtc->base.name,
12586 		       plane->base.base.id, plane->base.name,
12587 		       was_visible, visible,
12588 		       turn_off, turn_on, mode_changed);
12589 
12590 	if (turn_on) {
12591 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12592 			crtc_state->update_wm_pre = true;
12593 
12594 		/* must disable cxsr around plane enable/disable */
12595 		if (plane->id != PLANE_CURSOR)
12596 			crtc_state->disable_cxsr = true;
12597 	} else if (turn_off) {
12598 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12599 			crtc_state->update_wm_post = true;
12600 
12601 		/* must disable cxsr around plane enable/disable */
12602 		if (plane->id != PLANE_CURSOR)
12603 			crtc_state->disable_cxsr = true;
12604 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
12605 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
12606 			/* FIXME bollocks */
12607 			crtc_state->update_wm_pre = true;
12608 			crtc_state->update_wm_post = true;
12609 		}
12610 	}
12611 
12612 	if (visible || was_visible)
12613 		crtc_state->fb_bits |= plane->frontbuffer_bit;
12614 
12615 	/*
12616 	 * ILK/SNB DVSACNTR/Sprite Enable
12617 	 * IVB SPR_CTL/Sprite Enable
12618 	 * "When in Self Refresh Big FIFO mode, a write to enable the
12619 	 *  plane will be internally buffered and delayed while Big FIFO
12620 	 *  mode is exiting."
12621 	 *
12622 	 * Which means that enabling the sprite can take an extra frame
12623 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
12624 	 * down to LP0 and wait for vblank in order to make sure the
12625 	 * sprite gets enabled on the next vblank after the register write.
12626 	 * Doing otherwise would risk enabling the sprite one frame after
12627 	 * we've already signalled flip completion. We can resume LP1+
12628 	 * once the sprite has been enabled.
12629 	 *
12630 	 *
12631 	 * WaCxSRDisabledForSpriteScaling:ivb
12632 	 * IVB SPR_SCALE/Scaling Enable
12633 	 * "Low Power watermarks must be disabled for at least one
12634 	 *  frame before enabling sprite scaling, and kept disabled
12635 	 *  until sprite scaling is disabled."
12636 	 *
12637 	 * ILK/SNB DVSASCALE/Scaling Enable
12638 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
12639 	 *  masked off while Big FIFO mode is exiting."
12640 	 *
12641 	 * Despite the w/a only being listed for IVB we assume that
12642 	 * the ILK/SNB note has similar ramifications, hence we apply
12643 	 * the w/a on all three platforms.
12644 	 *
12645 	 * With experimental results seems this is needed also for primary
12646 	 * plane, not only sprite plane.
12647 	 */
12648 	if (plane->id != PLANE_CURSOR &&
12649 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
12650 	     IS_IVYBRIDGE(dev_priv)) &&
12651 	    (turn_on || (!needs_scaling(old_plane_state) &&
12652 			 needs_scaling(plane_state))))
12653 		crtc_state->disable_lp_wm = true;
12654 
12655 	return 0;
12656 }
12657 
12658 static bool encoders_cloneable(const struct intel_encoder *a,
12659 			       const struct intel_encoder *b)
12660 {
12661 	/* masks could be asymmetric, so check both ways */
12662 	return a == b || (a->cloneable & (1 << b->type) &&
12663 			  b->cloneable & (1 << a->type));
12664 }
12665 
12666 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12667 					 struct intel_crtc *crtc,
12668 					 struct intel_encoder *encoder)
12669 {
12670 	struct intel_encoder *source_encoder;
12671 	struct drm_connector *connector;
12672 	struct drm_connector_state *connector_state;
12673 	int i;
12674 
12675 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12676 		if (connector_state->crtc != &crtc->base)
12677 			continue;
12678 
12679 		source_encoder =
12680 			to_intel_encoder(connector_state->best_encoder);
12681 		if (!encoders_cloneable(encoder, source_encoder))
12682 			return false;
12683 	}
12684 
12685 	return true;
12686 }
12687 
12688 static int icl_add_linked_planes(struct intel_atomic_state *state)
12689 {
12690 	struct intel_plane *plane, *linked;
12691 	struct intel_plane_state *plane_state, *linked_plane_state;
12692 	int i;
12693 
12694 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12695 		linked = plane_state->planar_linked_plane;
12696 
12697 		if (!linked)
12698 			continue;
12699 
12700 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
12701 		if (IS_ERR(linked_plane_state))
12702 			return PTR_ERR(linked_plane_state);
12703 
12704 		drm_WARN_ON(state->base.dev,
12705 			    linked_plane_state->planar_linked_plane != plane);
12706 		drm_WARN_ON(state->base.dev,
12707 			    linked_plane_state->planar_slave == plane_state->planar_slave);
12708 	}
12709 
12710 	return 0;
12711 }
12712 
12713 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12714 {
12715 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12716 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12717 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
12718 	struct intel_plane *plane, *linked;
12719 	struct intel_plane_state *plane_state;
12720 	int i;
12721 
12722 	if (INTEL_GEN(dev_priv) < 11)
12723 		return 0;
12724 
12725 	/*
12726 	 * Destroy all old plane links and make the slave plane invisible
12727 	 * in the crtc_state->active_planes mask.
12728 	 */
12729 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12730 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
12731 			continue;
12732 
12733 		plane_state->planar_linked_plane = NULL;
12734 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
12735 			crtc_state->active_planes &= ~BIT(plane->id);
12736 			crtc_state->update_planes |= BIT(plane->id);
12737 		}
12738 
12739 		plane_state->planar_slave = false;
12740 	}
12741 
12742 	if (!crtc_state->nv12_planes)
12743 		return 0;
12744 
12745 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12746 		struct intel_plane_state *linked_state = NULL;
12747 
12748 		if (plane->pipe != crtc->pipe ||
12749 		    !(crtc_state->nv12_planes & BIT(plane->id)))
12750 			continue;
12751 
12752 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
12753 			if (!icl_is_nv12_y_plane(dev_priv, linked->id))
12754 				continue;
12755 
12756 			if (crtc_state->active_planes & BIT(linked->id))
12757 				continue;
12758 
12759 			linked_state = intel_atomic_get_plane_state(state, linked);
12760 			if (IS_ERR(linked_state))
12761 				return PTR_ERR(linked_state);
12762 
12763 			break;
12764 		}
12765 
12766 		if (!linked_state) {
12767 			drm_dbg_kms(&dev_priv->drm,
12768 				    "Need %d free Y planes for planar YUV\n",
12769 				    hweight8(crtc_state->nv12_planes));
12770 
12771 			return -EINVAL;
12772 		}
12773 
12774 		plane_state->planar_linked_plane = linked;
12775 
12776 		linked_state->planar_slave = true;
12777 		linked_state->planar_linked_plane = plane;
12778 		crtc_state->active_planes |= BIT(linked->id);
12779 		crtc_state->update_planes |= BIT(linked->id);
12780 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12781 			    linked->base.name, plane->base.name);
12782 
12783 		/* Copy parameters to slave plane */
12784 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12785 		linked_state->color_ctl = plane_state->color_ctl;
12786 		linked_state->view = plane_state->view;
12787 		memcpy(linked_state->color_plane, plane_state->color_plane,
12788 		       sizeof(linked_state->color_plane));
12789 
12790 		intel_plane_copy_uapi_to_hw_state(linked_state, plane_state);
12791 		linked_state->uapi.src = plane_state->uapi.src;
12792 		linked_state->uapi.dst = plane_state->uapi.dst;
12793 
12794 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
12795 			if (linked->id == PLANE_SPRITE5)
12796 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12797 			else if (linked->id == PLANE_SPRITE4)
12798 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
12799 			else if (linked->id == PLANE_SPRITE3)
12800 				plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
12801 			else if (linked->id == PLANE_SPRITE2)
12802 				plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
12803 			else
12804 				MISSING_CASE(linked->id);
12805 		}
12806 	}
12807 
12808 	return 0;
12809 }
12810 
12811 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12812 {
12813 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
12814 	struct intel_atomic_state *state =
12815 		to_intel_atomic_state(new_crtc_state->uapi.state);
12816 	const struct intel_crtc_state *old_crtc_state =
12817 		intel_atomic_get_old_crtc_state(state, crtc);
12818 
12819 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12820 }
12821 
12822 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12823 {
12824 	const struct drm_display_mode *adjusted_mode =
12825 		&crtc_state->hw.adjusted_mode;
12826 	int linetime_wm;
12827 
12828 	if (!crtc_state->hw.enable)
12829 		return 0;
12830 
12831 	linetime_wm = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12832 					adjusted_mode->crtc_clock);
12833 
12834 	return min(linetime_wm, 0x1ff);
12835 }
12836 
12837 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12838 			       const struct intel_cdclk_state *cdclk_state)
12839 {
12840 	const struct drm_display_mode *adjusted_mode =
12841 		&crtc_state->hw.adjusted_mode;
12842 	int linetime_wm;
12843 
12844 	if (!crtc_state->hw.enable)
12845 		return 0;
12846 
12847 	linetime_wm = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12848 					cdclk_state->logical.cdclk);
12849 
12850 	return min(linetime_wm, 0x1ff);
12851 }
12852 
12853 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12854 {
12855 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12856 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12857 	const struct drm_display_mode *adjusted_mode =
12858 		&crtc_state->hw.adjusted_mode;
12859 	int linetime_wm;
12860 
12861 	if (!crtc_state->hw.enable)
12862 		return 0;
12863 
12864 	linetime_wm = DIV_ROUND_UP(adjusted_mode->crtc_htotal * 1000 * 8,
12865 				   crtc_state->pixel_rate);
12866 
12867 	/* Display WA #1135: BXT:ALL GLK:ALL */
12868 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12869 		linetime_wm /= 2;
12870 
12871 	return min(linetime_wm, 0x1ff);
12872 }
12873 
12874 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12875 				   struct intel_crtc *crtc)
12876 {
12877 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12878 	struct intel_crtc_state *crtc_state =
12879 		intel_atomic_get_new_crtc_state(state, crtc);
12880 	const struct intel_cdclk_state *cdclk_state;
12881 
12882 	if (INTEL_GEN(dev_priv) >= 9)
12883 		crtc_state->linetime = skl_linetime_wm(crtc_state);
12884 	else
12885 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
12886 
12887 	if (!hsw_crtc_supports_ips(crtc))
12888 		return 0;
12889 
12890 	cdclk_state = intel_atomic_get_cdclk_state(state);
12891 	if (IS_ERR(cdclk_state))
12892 		return PTR_ERR(cdclk_state);
12893 
12894 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
12895 						       cdclk_state);
12896 
12897 	return 0;
12898 }
12899 
12900 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
12901 				   struct intel_crtc *crtc)
12902 {
12903 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12904 	struct intel_crtc_state *crtc_state =
12905 		intel_atomic_get_new_crtc_state(state, crtc);
12906 	bool mode_changed = needs_modeset(crtc_state);
12907 	int ret;
12908 
12909 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
12910 	    mode_changed && !crtc_state->hw.active)
12911 		crtc_state->update_wm_post = true;
12912 
12913 	if (mode_changed && crtc_state->hw.enable &&
12914 	    dev_priv->display.crtc_compute_clock &&
12915 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
12916 		ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
12917 		if (ret)
12918 			return ret;
12919 	}
12920 
12921 	/*
12922 	 * May need to update pipe gamma enable bits
12923 	 * when C8 planes are getting enabled/disabled.
12924 	 */
12925 	if (c8_planes_changed(crtc_state))
12926 		crtc_state->uapi.color_mgmt_changed = true;
12927 
12928 	if (mode_changed || crtc_state->update_pipe ||
12929 	    crtc_state->uapi.color_mgmt_changed) {
12930 		ret = intel_color_check(crtc_state);
12931 		if (ret)
12932 			return ret;
12933 	}
12934 
12935 	if (dev_priv->display.compute_pipe_wm) {
12936 		ret = dev_priv->display.compute_pipe_wm(crtc_state);
12937 		if (ret) {
12938 			drm_dbg_kms(&dev_priv->drm,
12939 				    "Target pipe watermarks are invalid\n");
12940 			return ret;
12941 		}
12942 	}
12943 
12944 	if (dev_priv->display.compute_intermediate_wm) {
12945 		if (drm_WARN_ON(&dev_priv->drm,
12946 				!dev_priv->display.compute_pipe_wm))
12947 			return 0;
12948 
12949 		/*
12950 		 * Calculate 'intermediate' watermarks that satisfy both the
12951 		 * old state and the new state.  We can program these
12952 		 * immediately.
12953 		 */
12954 		ret = dev_priv->display.compute_intermediate_wm(crtc_state);
12955 		if (ret) {
12956 			drm_dbg_kms(&dev_priv->drm,
12957 				    "No valid intermediate pipe watermarks are possible\n");
12958 			return ret;
12959 		}
12960 	}
12961 
12962 	if (INTEL_GEN(dev_priv) >= 9) {
12963 		if (mode_changed || crtc_state->update_pipe) {
12964 			ret = skl_update_scaler_crtc(crtc_state);
12965 			if (ret)
12966 				return ret;
12967 		}
12968 
12969 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
12970 		if (ret)
12971 			return ret;
12972 	}
12973 
12974 	if (HAS_IPS(dev_priv)) {
12975 		ret = hsw_compute_ips_config(crtc_state);
12976 		if (ret)
12977 			return ret;
12978 	}
12979 
12980 	if (INTEL_GEN(dev_priv) >= 9 ||
12981 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12982 		ret = hsw_compute_linetime_wm(state, crtc);
12983 		if (ret)
12984 			return ret;
12985 
12986 	}
12987 
12988 	if (!mode_changed) {
12989 		ret = intel_psr2_sel_fetch_update(state, crtc);
12990 		if (ret)
12991 			return ret;
12992 	}
12993 
12994 	return 0;
12995 }
12996 
12997 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12998 {
12999 	struct intel_connector *connector;
13000 	struct drm_connector_list_iter conn_iter;
13001 
13002 	drm_connector_list_iter_begin(dev, &conn_iter);
13003 	for_each_intel_connector_iter(connector, &conn_iter) {
13004 		if (connector->base.state->crtc)
13005 			drm_connector_put(&connector->base);
13006 
13007 		if (connector->base.encoder) {
13008 			connector->base.state->best_encoder =
13009 				connector->base.encoder;
13010 			connector->base.state->crtc =
13011 				connector->base.encoder->crtc;
13012 
13013 			drm_connector_get(&connector->base);
13014 		} else {
13015 			connector->base.state->best_encoder = NULL;
13016 			connector->base.state->crtc = NULL;
13017 		}
13018 	}
13019 	drm_connector_list_iter_end(&conn_iter);
13020 }
13021 
13022 static int
13023 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
13024 		      struct intel_crtc_state *pipe_config)
13025 {
13026 	struct drm_connector *connector = conn_state->connector;
13027 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13028 	const struct drm_display_info *info = &connector->display_info;
13029 	int bpp;
13030 
13031 	switch (conn_state->max_bpc) {
13032 	case 6 ... 7:
13033 		bpp = 6 * 3;
13034 		break;
13035 	case 8 ... 9:
13036 		bpp = 8 * 3;
13037 		break;
13038 	case 10 ... 11:
13039 		bpp = 10 * 3;
13040 		break;
13041 	case 12:
13042 		bpp = 12 * 3;
13043 		break;
13044 	default:
13045 		return -EINVAL;
13046 	}
13047 
13048 	if (bpp < pipe_config->pipe_bpp) {
13049 		drm_dbg_kms(&i915->drm,
13050 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
13051 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
13052 			    connector->base.id, connector->name,
13053 			    bpp, 3 * info->bpc,
13054 			    3 * conn_state->max_requested_bpc,
13055 			    pipe_config->pipe_bpp);
13056 
13057 		pipe_config->pipe_bpp = bpp;
13058 	}
13059 
13060 	return 0;
13061 }
13062 
13063 static int
13064 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
13065 			  struct intel_crtc_state *pipe_config)
13066 {
13067 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13068 	struct drm_atomic_state *state = pipe_config->uapi.state;
13069 	struct drm_connector *connector;
13070 	struct drm_connector_state *connector_state;
13071 	int bpp, i;
13072 
13073 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
13074 	    IS_CHERRYVIEW(dev_priv)))
13075 		bpp = 10*3;
13076 	else if (INTEL_GEN(dev_priv) >= 5)
13077 		bpp = 12*3;
13078 	else
13079 		bpp = 8*3;
13080 
13081 	pipe_config->pipe_bpp = bpp;
13082 
13083 	/* Clamp display bpp to connector max bpp */
13084 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13085 		int ret;
13086 
13087 		if (connector_state->crtc != &crtc->base)
13088 			continue;
13089 
13090 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
13091 		if (ret)
13092 			return ret;
13093 	}
13094 
13095 	return 0;
13096 }
13097 
13098 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
13099 				    const struct drm_display_mode *mode)
13100 {
13101 	drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
13102 		    "type: 0x%x flags: 0x%x\n",
13103 		    mode->crtc_clock,
13104 		    mode->crtc_hdisplay, mode->crtc_hsync_start,
13105 		    mode->crtc_hsync_end, mode->crtc_htotal,
13106 		    mode->crtc_vdisplay, mode->crtc_vsync_start,
13107 		    mode->crtc_vsync_end, mode->crtc_vtotal,
13108 		    mode->type, mode->flags);
13109 }
13110 
13111 static void
13112 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
13113 		      const char *id, unsigned int lane_count,
13114 		      const struct intel_link_m_n *m_n)
13115 {
13116 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13117 
13118 	drm_dbg_kms(&i915->drm,
13119 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
13120 		    id, lane_count,
13121 		    m_n->gmch_m, m_n->gmch_n,
13122 		    m_n->link_m, m_n->link_n, m_n->tu);
13123 }
13124 
13125 static void
13126 intel_dump_infoframe(struct drm_i915_private *dev_priv,
13127 		     const union hdmi_infoframe *frame)
13128 {
13129 	if (!drm_debug_enabled(DRM_UT_KMS))
13130 		return;
13131 
13132 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
13133 }
13134 
13135 static void
13136 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
13137 		      const struct drm_dp_vsc_sdp *vsc)
13138 {
13139 	if (!drm_debug_enabled(DRM_UT_KMS))
13140 		return;
13141 
13142 	drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
13143 }
13144 
13145 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
13146 
13147 static const char * const output_type_str[] = {
13148 	OUTPUT_TYPE(UNUSED),
13149 	OUTPUT_TYPE(ANALOG),
13150 	OUTPUT_TYPE(DVO),
13151 	OUTPUT_TYPE(SDVO),
13152 	OUTPUT_TYPE(LVDS),
13153 	OUTPUT_TYPE(TVOUT),
13154 	OUTPUT_TYPE(HDMI),
13155 	OUTPUT_TYPE(DP),
13156 	OUTPUT_TYPE(EDP),
13157 	OUTPUT_TYPE(DSI),
13158 	OUTPUT_TYPE(DDI),
13159 	OUTPUT_TYPE(DP_MST),
13160 };
13161 
13162 #undef OUTPUT_TYPE
13163 
13164 static void snprintf_output_types(char *buf, size_t len,
13165 				  unsigned int output_types)
13166 {
13167 	char *str = buf;
13168 	int i;
13169 
13170 	str[0] = '\0';
13171 
13172 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
13173 		int r;
13174 
13175 		if ((output_types & BIT(i)) == 0)
13176 			continue;
13177 
13178 		r = snprintf(str, len, "%s%s",
13179 			     str != buf ? "," : "", output_type_str[i]);
13180 		if (r >= len)
13181 			break;
13182 		str += r;
13183 		len -= r;
13184 
13185 		output_types &= ~BIT(i);
13186 	}
13187 
13188 	WARN_ON_ONCE(output_types != 0);
13189 }
13190 
13191 static const char * const output_format_str[] = {
13192 	[INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
13193 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
13194 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
13195 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
13196 };
13197 
13198 static const char *output_formats(enum intel_output_format format)
13199 {
13200 	if (format >= ARRAY_SIZE(output_format_str))
13201 		format = INTEL_OUTPUT_FORMAT_INVALID;
13202 	return output_format_str[format];
13203 }
13204 
13205 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
13206 {
13207 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
13208 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
13209 	const struct drm_framebuffer *fb = plane_state->hw.fb;
13210 	struct drm_format_name_buf format_name;
13211 
13212 	if (!fb) {
13213 		drm_dbg_kms(&i915->drm,
13214 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
13215 			    plane->base.base.id, plane->base.name,
13216 			    yesno(plane_state->uapi.visible));
13217 		return;
13218 	}
13219 
13220 	drm_dbg_kms(&i915->drm,
13221 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s, visible: %s\n",
13222 		    plane->base.base.id, plane->base.name,
13223 		    fb->base.id, fb->width, fb->height,
13224 		    drm_get_format_name(fb->format->format, &format_name),
13225 		    yesno(plane_state->uapi.visible));
13226 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13227 		    plane_state->hw.rotation, plane_state->scaler_id);
13228 	if (plane_state->uapi.visible)
13229 		drm_dbg_kms(&i915->drm,
13230 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13231 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
13232 			    DRM_RECT_ARG(&plane_state->uapi.dst));
13233 }
13234 
13235 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
13236 				   struct intel_atomic_state *state,
13237 				   const char *context)
13238 {
13239 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13240 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13241 	const struct intel_plane_state *plane_state;
13242 	struct intel_plane *plane;
13243 	char buf[64];
13244 	int i;
13245 
13246 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13247 		    crtc->base.base.id, crtc->base.name,
13248 		    yesno(pipe_config->hw.enable), context);
13249 
13250 	if (!pipe_config->hw.enable)
13251 		goto dump_planes;
13252 
13253 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
13254 	drm_dbg_kms(&dev_priv->drm,
13255 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
13256 		    yesno(pipe_config->hw.active),
13257 		    buf, pipe_config->output_types,
13258 		    output_formats(pipe_config->output_format));
13259 
13260 	drm_dbg_kms(&dev_priv->drm,
13261 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13262 		    transcoder_name(pipe_config->cpu_transcoder),
13263 		    pipe_config->pipe_bpp, pipe_config->dither);
13264 
13265 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13266 		    transcoder_name(pipe_config->mst_master_transcoder));
13267 
13268 	drm_dbg_kms(&dev_priv->drm,
13269 		    "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
13270 		    transcoder_name(pipe_config->master_transcoder),
13271 		    pipe_config->sync_mode_slaves_mask);
13272 
13273 	if (pipe_config->has_pch_encoder)
13274 		intel_dump_m_n_config(pipe_config, "fdi",
13275 				      pipe_config->fdi_lanes,
13276 				      &pipe_config->fdi_m_n);
13277 
13278 	if (intel_crtc_has_dp_encoder(pipe_config)) {
13279 		intel_dump_m_n_config(pipe_config, "dp m_n",
13280 				pipe_config->lane_count, &pipe_config->dp_m_n);
13281 		if (pipe_config->has_drrs)
13282 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
13283 					      pipe_config->lane_count,
13284 					      &pipe_config->dp_m2_n2);
13285 	}
13286 
13287 	drm_dbg_kms(&dev_priv->drm,
13288 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13289 		    pipe_config->has_audio, pipe_config->has_infoframe,
13290 		    pipe_config->infoframes.enable);
13291 
13292 	if (pipe_config->infoframes.enable &
13293 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
13294 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13295 			    pipe_config->infoframes.gcp);
13296 	if (pipe_config->infoframes.enable &
13297 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13298 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13299 	if (pipe_config->infoframes.enable &
13300 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13301 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13302 	if (pipe_config->infoframes.enable &
13303 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13304 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
13305 	if (pipe_config->infoframes.enable &
13306 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
13307 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13308 	if (pipe_config->infoframes.enable &
13309 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
13310 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13311 	if (pipe_config->infoframes.enable &
13312 	    intel_hdmi_infoframe_enable(DP_SDP_VSC))
13313 		intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
13314 
13315 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
13316 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
13317 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
13318 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
13319 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
13320 	drm_dbg_kms(&dev_priv->drm,
13321 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13322 		    pipe_config->port_clock,
13323 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13324 		    pipe_config->pixel_rate);
13325 
13326 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13327 		    pipe_config->linetime, pipe_config->ips_linetime);
13328 
13329 	if (INTEL_GEN(dev_priv) >= 9)
13330 		drm_dbg_kms(&dev_priv->drm,
13331 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13332 			    crtc->num_scalers,
13333 			    pipe_config->scaler_state.scaler_users,
13334 			    pipe_config->scaler_state.scaler_id);
13335 
13336 	if (HAS_GMCH(dev_priv))
13337 		drm_dbg_kms(&dev_priv->drm,
13338 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13339 			    pipe_config->gmch_pfit.control,
13340 			    pipe_config->gmch_pfit.pgm_ratios,
13341 			    pipe_config->gmch_pfit.lvds_border_bits);
13342 	else
13343 		drm_dbg_kms(&dev_priv->drm,
13344 			    "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
13345 			    DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
13346 			    enableddisabled(pipe_config->pch_pfit.enabled),
13347 			    yesno(pipe_config->pch_pfit.force_thru));
13348 
13349 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13350 		    pipe_config->ips_enabled, pipe_config->double_wide);
13351 
13352 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
13353 
13354 	if (IS_CHERRYVIEW(dev_priv))
13355 		drm_dbg_kms(&dev_priv->drm,
13356 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13357 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
13358 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13359 	else
13360 		drm_dbg_kms(&dev_priv->drm,
13361 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13362 			    pipe_config->csc_mode, pipe_config->gamma_mode,
13363 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13364 
13365 	drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
13366 		    pipe_config->hw.degamma_lut ?
13367 		    drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
13368 		    pipe_config->hw.gamma_lut ?
13369 		    drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
13370 
13371 dump_planes:
13372 	if (!state)
13373 		return;
13374 
13375 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13376 		if (plane->pipe == crtc->pipe)
13377 			intel_dump_plane_state(plane_state);
13378 	}
13379 }
13380 
13381 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
13382 {
13383 	struct drm_device *dev = state->base.dev;
13384 	struct drm_connector *connector;
13385 	struct drm_connector_list_iter conn_iter;
13386 	unsigned int used_ports = 0;
13387 	unsigned int used_mst_ports = 0;
13388 	bool ret = true;
13389 
13390 	/*
13391 	 * We're going to peek into connector->state,
13392 	 * hence connection_mutex must be held.
13393 	 */
13394 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13395 
13396 	/*
13397 	 * Walk the connector list instead of the encoder
13398 	 * list to detect the problem on ddi platforms
13399 	 * where there's just one encoder per digital port.
13400 	 */
13401 	drm_connector_list_iter_begin(dev, &conn_iter);
13402 	drm_for_each_connector_iter(connector, &conn_iter) {
13403 		struct drm_connector_state *connector_state;
13404 		struct intel_encoder *encoder;
13405 
13406 		connector_state =
13407 			drm_atomic_get_new_connector_state(&state->base,
13408 							   connector);
13409 		if (!connector_state)
13410 			connector_state = connector->state;
13411 
13412 		if (!connector_state->best_encoder)
13413 			continue;
13414 
13415 		encoder = to_intel_encoder(connector_state->best_encoder);
13416 
13417 		drm_WARN_ON(dev, !connector_state->crtc);
13418 
13419 		switch (encoder->type) {
13420 		case INTEL_OUTPUT_DDI:
13421 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
13422 				break;
13423 			fallthrough;
13424 		case INTEL_OUTPUT_DP:
13425 		case INTEL_OUTPUT_HDMI:
13426 		case INTEL_OUTPUT_EDP:
13427 			/* the same port mustn't appear more than once */
13428 			if (used_ports & BIT(encoder->port))
13429 				ret = false;
13430 
13431 			used_ports |= BIT(encoder->port);
13432 			break;
13433 		case INTEL_OUTPUT_DP_MST:
13434 			used_mst_ports |=
13435 				1 << encoder->port;
13436 			break;
13437 		default:
13438 			break;
13439 		}
13440 	}
13441 	drm_connector_list_iter_end(&conn_iter);
13442 
13443 	/* can't mix MST and SST/HDMI on the same port */
13444 	if (used_ports & used_mst_ports)
13445 		return false;
13446 
13447 	return ret;
13448 }
13449 
13450 static void
13451 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_crtc_state *crtc_state)
13452 {
13453 	intel_crtc_copy_color_blobs(crtc_state);
13454 }
13455 
13456 static void
13457 intel_crtc_copy_uapi_to_hw_state(struct intel_crtc_state *crtc_state)
13458 {
13459 	crtc_state->hw.enable = crtc_state->uapi.enable;
13460 	crtc_state->hw.active = crtc_state->uapi.active;
13461 	crtc_state->hw.mode = crtc_state->uapi.mode;
13462 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
13463 	crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
13464 	intel_crtc_copy_uapi_to_hw_state_nomodeset(crtc_state);
13465 }
13466 
13467 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13468 {
13469 	crtc_state->uapi.enable = crtc_state->hw.enable;
13470 	crtc_state->uapi.active = crtc_state->hw.active;
13471 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
13472 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
13473 
13474 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
13475 	crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
13476 
13477 	/* copy color blobs to uapi */
13478 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13479 				  crtc_state->hw.degamma_lut);
13480 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13481 				  crtc_state->hw.gamma_lut);
13482 	drm_property_replace_blob(&crtc_state->uapi.ctm,
13483 				  crtc_state->hw.ctm);
13484 }
13485 
13486 static int
13487 intel_crtc_prepare_cleared_state(struct intel_crtc_state *crtc_state)
13488 {
13489 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13490 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13491 	struct intel_crtc_state *saved_state;
13492 
13493 	saved_state = intel_crtc_state_alloc(crtc);
13494 	if (!saved_state)
13495 		return -ENOMEM;
13496 
13497 	/* free the old crtc_state->hw members */
13498 	intel_crtc_free_hw_state(crtc_state);
13499 
13500 	/* FIXME: before the switch to atomic started, a new pipe_config was
13501 	 * kzalloc'd. Code that depends on any field being zero should be
13502 	 * fixed, so that the crtc_state can be safely duplicated. For now,
13503 	 * only fields that are know to not cause problems are preserved. */
13504 
13505 	saved_state->uapi = crtc_state->uapi;
13506 	saved_state->scaler_state = crtc_state->scaler_state;
13507 	saved_state->shared_dpll = crtc_state->shared_dpll;
13508 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13509 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13510 	       sizeof(saved_state->icl_port_dplls));
13511 	saved_state->crc_enabled = crtc_state->crc_enabled;
13512 	if (IS_G4X(dev_priv) ||
13513 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13514 		saved_state->wm = crtc_state->wm;
13515 
13516 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13517 	kfree(saved_state);
13518 
13519 	intel_crtc_copy_uapi_to_hw_state(crtc_state);
13520 
13521 	return 0;
13522 }
13523 
13524 static int
13525 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
13526 {
13527 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
13528 	struct drm_atomic_state *state = pipe_config->uapi.state;
13529 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13530 	struct drm_connector *connector;
13531 	struct drm_connector_state *connector_state;
13532 	int base_bpp, ret, i;
13533 	bool retry = true;
13534 
13535 	pipe_config->cpu_transcoder =
13536 		(enum transcoder) to_intel_crtc(crtc)->pipe;
13537 
13538 	/*
13539 	 * Sanitize sync polarity flags based on requested ones. If neither
13540 	 * positive or negative polarity is requested, treat this as meaning
13541 	 * negative polarity.
13542 	 */
13543 	if (!(pipe_config->hw.adjusted_mode.flags &
13544 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
13545 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
13546 
13547 	if (!(pipe_config->hw.adjusted_mode.flags &
13548 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
13549 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
13550 
13551 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13552 					pipe_config);
13553 	if (ret)
13554 		return ret;
13555 
13556 	base_bpp = pipe_config->pipe_bpp;
13557 
13558 	/*
13559 	 * Determine the real pipe dimensions. Note that stereo modes can
13560 	 * increase the actual pipe size due to the frame doubling and
13561 	 * insertion of additional space for blanks between the frame. This
13562 	 * is stored in the crtc timings. We use the requested mode to do this
13563 	 * computation to clearly distinguish it from the adjusted mode, which
13564 	 * can be changed by the connectors in the below retry loop.
13565 	 */
13566 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
13567 			       &pipe_config->pipe_src_w,
13568 			       &pipe_config->pipe_src_h);
13569 
13570 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13571 		struct intel_encoder *encoder =
13572 			to_intel_encoder(connector_state->best_encoder);
13573 
13574 		if (connector_state->crtc != crtc)
13575 			continue;
13576 
13577 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13578 			drm_dbg_kms(&i915->drm,
13579 				    "rejecting invalid cloning configuration\n");
13580 			return -EINVAL;
13581 		}
13582 
13583 		/*
13584 		 * Determine output_types before calling the .compute_config()
13585 		 * hooks so that the hooks can use this information safely.
13586 		 */
13587 		if (encoder->compute_output_type)
13588 			pipe_config->output_types |=
13589 				BIT(encoder->compute_output_type(encoder, pipe_config,
13590 								 connector_state));
13591 		else
13592 			pipe_config->output_types |= BIT(encoder->type);
13593 	}
13594 
13595 encoder_retry:
13596 	/* Ensure the port clock defaults are reset when retrying. */
13597 	pipe_config->port_clock = 0;
13598 	pipe_config->pixel_multiplier = 1;
13599 
13600 	/* Fill in default crtc timings, allow encoders to overwrite them. */
13601 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
13602 			      CRTC_STEREO_DOUBLE);
13603 
13604 	/* Pass our mode to the connectors and the CRTC to give them a chance to
13605 	 * adjust it according to limitations or connector properties, and also
13606 	 * a chance to reject the mode entirely.
13607 	 */
13608 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13609 		struct intel_encoder *encoder =
13610 			to_intel_encoder(connector_state->best_encoder);
13611 
13612 		if (connector_state->crtc != crtc)
13613 			continue;
13614 
13615 		ret = encoder->compute_config(encoder, pipe_config,
13616 					      connector_state);
13617 		if (ret < 0) {
13618 			if (ret != -EDEADLK)
13619 				drm_dbg_kms(&i915->drm,
13620 					    "Encoder config failure: %d\n",
13621 					    ret);
13622 			return ret;
13623 		}
13624 	}
13625 
13626 	/* Set default port clock if not overwritten by the encoder. Needs to be
13627 	 * done afterwards in case the encoder adjusts the mode. */
13628 	if (!pipe_config->port_clock)
13629 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
13630 			* pipe_config->pixel_multiplier;
13631 
13632 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
13633 	if (ret == -EDEADLK)
13634 		return ret;
13635 	if (ret < 0) {
13636 		drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
13637 		return ret;
13638 	}
13639 
13640 	if (ret == RETRY) {
13641 		if (drm_WARN(&i915->drm, !retry,
13642 			     "loop in pipe configuration computation\n"))
13643 			return -EINVAL;
13644 
13645 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
13646 		retry = false;
13647 		goto encoder_retry;
13648 	}
13649 
13650 	/* Dithering seems to not pass-through bits correctly when it should, so
13651 	 * only enable it on 6bpc panels and when its not a compliance
13652 	 * test requesting 6bpc video pattern.
13653 	 */
13654 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13655 		!pipe_config->dither_force_disable;
13656 	drm_dbg_kms(&i915->drm,
13657 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13658 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
13659 
13660 	return 0;
13661 }
13662 
13663 static int
13664 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13665 {
13666 	struct intel_atomic_state *state =
13667 		to_intel_atomic_state(crtc_state->uapi.state);
13668 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13669 	struct drm_connector_state *conn_state;
13670 	struct drm_connector *connector;
13671 	int i;
13672 
13673 	for_each_new_connector_in_state(&state->base, connector,
13674 					conn_state, i) {
13675 		struct intel_encoder *encoder =
13676 			to_intel_encoder(conn_state->best_encoder);
13677 		int ret;
13678 
13679 		if (conn_state->crtc != &crtc->base ||
13680 		    !encoder->compute_config_late)
13681 			continue;
13682 
13683 		ret = encoder->compute_config_late(encoder, crtc_state,
13684 						   conn_state);
13685 		if (ret)
13686 			return ret;
13687 	}
13688 
13689 	return 0;
13690 }
13691 
13692 bool intel_fuzzy_clock_check(int clock1, int clock2)
13693 {
13694 	int diff;
13695 
13696 	if (clock1 == clock2)
13697 		return true;
13698 
13699 	if (!clock1 || !clock2)
13700 		return false;
13701 
13702 	diff = abs(clock1 - clock2);
13703 
13704 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13705 		return true;
13706 
13707 	return false;
13708 }
13709 
13710 static bool
13711 intel_compare_m_n(unsigned int m, unsigned int n,
13712 		  unsigned int m2, unsigned int n2,
13713 		  bool exact)
13714 {
13715 	if (m == m2 && n == n2)
13716 		return true;
13717 
13718 	if (exact || !m || !n || !m2 || !n2)
13719 		return false;
13720 
13721 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13722 
13723 	if (n > n2) {
13724 		while (n > n2) {
13725 			m2 <<= 1;
13726 			n2 <<= 1;
13727 		}
13728 	} else if (n < n2) {
13729 		while (n < n2) {
13730 			m <<= 1;
13731 			n <<= 1;
13732 		}
13733 	}
13734 
13735 	if (n != n2)
13736 		return false;
13737 
13738 	return intel_fuzzy_clock_check(m, m2);
13739 }
13740 
13741 static bool
13742 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13743 		       const struct intel_link_m_n *m2_n2,
13744 		       bool exact)
13745 {
13746 	return m_n->tu == m2_n2->tu &&
13747 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13748 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13749 		intel_compare_m_n(m_n->link_m, m_n->link_n,
13750 				  m2_n2->link_m, m2_n2->link_n, exact);
13751 }
13752 
13753 static bool
13754 intel_compare_infoframe(const union hdmi_infoframe *a,
13755 			const union hdmi_infoframe *b)
13756 {
13757 	return memcmp(a, b, sizeof(*a)) == 0;
13758 }
13759 
13760 static bool
13761 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
13762 			 const struct drm_dp_vsc_sdp *b)
13763 {
13764 	return memcmp(a, b, sizeof(*a)) == 0;
13765 }
13766 
13767 static void
13768 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13769 			       bool fastset, const char *name,
13770 			       const union hdmi_infoframe *a,
13771 			       const union hdmi_infoframe *b)
13772 {
13773 	if (fastset) {
13774 		if (!drm_debug_enabled(DRM_UT_KMS))
13775 			return;
13776 
13777 		drm_dbg_kms(&dev_priv->drm,
13778 			    "fastset mismatch in %s infoframe\n", name);
13779 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13780 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
13781 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13782 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13783 	} else {
13784 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13785 		drm_err(&dev_priv->drm, "expected:\n");
13786 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
13787 		drm_err(&dev_priv->drm, "found:\n");
13788 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13789 	}
13790 }
13791 
13792 static void
13793 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
13794 				bool fastset, const char *name,
13795 				const struct drm_dp_vsc_sdp *a,
13796 				const struct drm_dp_vsc_sdp *b)
13797 {
13798 	if (fastset) {
13799 		if (!drm_debug_enabled(DRM_UT_KMS))
13800 			return;
13801 
13802 		drm_dbg_kms(&dev_priv->drm,
13803 			    "fastset mismatch in %s dp sdp\n", name);
13804 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13805 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
13806 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13807 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
13808 	} else {
13809 		drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
13810 		drm_err(&dev_priv->drm, "expected:\n");
13811 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
13812 		drm_err(&dev_priv->drm, "found:\n");
13813 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
13814 	}
13815 }
13816 
13817 static void __printf(4, 5)
13818 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13819 		     const char *name, const char *format, ...)
13820 {
13821 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
13822 	struct va_format vaf;
13823 	va_list args;
13824 
13825 	va_start(args, format);
13826 	vaf.fmt = format;
13827 	vaf.va = &args;
13828 
13829 	if (fastset)
13830 		drm_dbg_kms(&i915->drm,
13831 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
13832 			    crtc->base.base.id, crtc->base.name, name, &vaf);
13833 	else
13834 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
13835 			crtc->base.base.id, crtc->base.name, name, &vaf);
13836 
13837 	va_end(args);
13838 }
13839 
13840 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
13841 {
13842 	if (dev_priv->params.fastboot != -1)
13843 		return dev_priv->params.fastboot;
13844 
13845 	/* Enable fastboot by default on Skylake and newer */
13846 	if (INTEL_GEN(dev_priv) >= 9)
13847 		return true;
13848 
13849 	/* Enable fastboot by default on VLV and CHV */
13850 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13851 		return true;
13852 
13853 	/* Disabled by default on all others */
13854 	return false;
13855 }
13856 
13857 static bool
13858 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
13859 			  const struct intel_crtc_state *pipe_config,
13860 			  bool fastset)
13861 {
13862 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
13863 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13864 	bool ret = true;
13865 	u32 bp_gamma = 0;
13866 	bool fixup_inherited = fastset &&
13867 		current_config->inherited && !pipe_config->inherited;
13868 
13869 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
13870 		drm_dbg_kms(&dev_priv->drm,
13871 			    "initial modeset and fastboot not set\n");
13872 		ret = false;
13873 	}
13874 
13875 #define PIPE_CONF_CHECK_X(name) do { \
13876 	if (current_config->name != pipe_config->name) { \
13877 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13878 				     "(expected 0x%08x, found 0x%08x)", \
13879 				     current_config->name, \
13880 				     pipe_config->name); \
13881 		ret = false; \
13882 	} \
13883 } while (0)
13884 
13885 #define PIPE_CONF_CHECK_I(name) do { \
13886 	if (current_config->name != pipe_config->name) { \
13887 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13888 				     "(expected %i, found %i)", \
13889 				     current_config->name, \
13890 				     pipe_config->name); \
13891 		ret = false; \
13892 	} \
13893 } while (0)
13894 
13895 #define PIPE_CONF_CHECK_BOOL(name) do { \
13896 	if (current_config->name != pipe_config->name) { \
13897 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
13898 				     "(expected %s, found %s)", \
13899 				     yesno(current_config->name), \
13900 				     yesno(pipe_config->name)); \
13901 		ret = false; \
13902 	} \
13903 } while (0)
13904 
13905 /*
13906  * Checks state where we only read out the enabling, but not the entire
13907  * state itself (like full infoframes or ELD for audio). These states
13908  * require a full modeset on bootup to fix up.
13909  */
13910 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
13911 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
13912 		PIPE_CONF_CHECK_BOOL(name); \
13913 	} else { \
13914 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13915 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
13916 				     yesno(current_config->name), \
13917 				     yesno(pipe_config->name)); \
13918 		ret = false; \
13919 	} \
13920 } while (0)
13921 
13922 #define PIPE_CONF_CHECK_P(name) do { \
13923 	if (current_config->name != pipe_config->name) { \
13924 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13925 				     "(expected %p, found %p)", \
13926 				     current_config->name, \
13927 				     pipe_config->name); \
13928 		ret = false; \
13929 	} \
13930 } while (0)
13931 
13932 #define PIPE_CONF_CHECK_M_N(name) do { \
13933 	if (!intel_compare_link_m_n(&current_config->name, \
13934 				    &pipe_config->name,\
13935 				    !fastset)) { \
13936 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13937 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13938 				     "found tu %i, gmch %i/%i link %i/%i)", \
13939 				     current_config->name.tu, \
13940 				     current_config->name.gmch_m, \
13941 				     current_config->name.gmch_n, \
13942 				     current_config->name.link_m, \
13943 				     current_config->name.link_n, \
13944 				     pipe_config->name.tu, \
13945 				     pipe_config->name.gmch_m, \
13946 				     pipe_config->name.gmch_n, \
13947 				     pipe_config->name.link_m, \
13948 				     pipe_config->name.link_n); \
13949 		ret = false; \
13950 	} \
13951 } while (0)
13952 
13953 /* This is required for BDW+ where there is only one set of registers for
13954  * switching between high and low RR.
13955  * This macro can be used whenever a comparison has to be made between one
13956  * hw state and multiple sw state variables.
13957  */
13958 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
13959 	if (!intel_compare_link_m_n(&current_config->name, \
13960 				    &pipe_config->name, !fastset) && \
13961 	    !intel_compare_link_m_n(&current_config->alt_name, \
13962 				    &pipe_config->name, !fastset)) { \
13963 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13964 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13965 				     "or tu %i gmch %i/%i link %i/%i, " \
13966 				     "found tu %i, gmch %i/%i link %i/%i)", \
13967 				     current_config->name.tu, \
13968 				     current_config->name.gmch_m, \
13969 				     current_config->name.gmch_n, \
13970 				     current_config->name.link_m, \
13971 				     current_config->name.link_n, \
13972 				     current_config->alt_name.tu, \
13973 				     current_config->alt_name.gmch_m, \
13974 				     current_config->alt_name.gmch_n, \
13975 				     current_config->alt_name.link_m, \
13976 				     current_config->alt_name.link_n, \
13977 				     pipe_config->name.tu, \
13978 				     pipe_config->name.gmch_m, \
13979 				     pipe_config->name.gmch_n, \
13980 				     pipe_config->name.link_m, \
13981 				     pipe_config->name.link_n); \
13982 		ret = false; \
13983 	} \
13984 } while (0)
13985 
13986 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
13987 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
13988 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13989 				     "(%x) (expected %i, found %i)", \
13990 				     (mask), \
13991 				     current_config->name & (mask), \
13992 				     pipe_config->name & (mask)); \
13993 		ret = false; \
13994 	} \
13995 } while (0)
13996 
13997 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
13998 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
13999 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
14000 				     "(expected %i, found %i)", \
14001 				     current_config->name, \
14002 				     pipe_config->name); \
14003 		ret = false; \
14004 	} \
14005 } while (0)
14006 
14007 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
14008 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
14009 				     &pipe_config->infoframes.name)) { \
14010 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
14011 					       &current_config->infoframes.name, \
14012 					       &pipe_config->infoframes.name); \
14013 		ret = false; \
14014 	} \
14015 } while (0)
14016 
14017 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
14018 	if (!current_config->has_psr && !pipe_config->has_psr && \
14019 	    !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
14020 				      &pipe_config->infoframes.name)) { \
14021 		pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
14022 						&current_config->infoframes.name, \
14023 						&pipe_config->infoframes.name); \
14024 		ret = false; \
14025 	} \
14026 } while (0)
14027 
14028 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
14029 	if (current_config->name1 != pipe_config->name1) { \
14030 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
14031 				"(expected %i, found %i, won't compare lut values)", \
14032 				current_config->name1, \
14033 				pipe_config->name1); \
14034 		ret = false;\
14035 	} else { \
14036 		if (!intel_color_lut_equal(current_config->name2, \
14037 					pipe_config->name2, pipe_config->name1, \
14038 					bit_precision)) { \
14039 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
14040 					"hw_state doesn't match sw_state"); \
14041 			ret = false; \
14042 		} \
14043 	} \
14044 } while (0)
14045 
14046 #define PIPE_CONF_QUIRK(quirk) \
14047 	((current_config->quirks | pipe_config->quirks) & (quirk))
14048 
14049 	PIPE_CONF_CHECK_I(cpu_transcoder);
14050 
14051 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
14052 	PIPE_CONF_CHECK_I(fdi_lanes);
14053 	PIPE_CONF_CHECK_M_N(fdi_m_n);
14054 
14055 	PIPE_CONF_CHECK_I(lane_count);
14056 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
14057 
14058 	if (INTEL_GEN(dev_priv) < 8) {
14059 		PIPE_CONF_CHECK_M_N(dp_m_n);
14060 
14061 		if (current_config->has_drrs)
14062 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
14063 	} else
14064 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
14065 
14066 	PIPE_CONF_CHECK_X(output_types);
14067 
14068 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
14069 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
14070 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
14071 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
14072 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
14073 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
14074 
14075 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
14076 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
14077 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
14078 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
14079 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
14080 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
14081 
14082 	PIPE_CONF_CHECK_I(pixel_multiplier);
14083 	PIPE_CONF_CHECK_I(output_format);
14084 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
14085 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
14086 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14087 		PIPE_CONF_CHECK_BOOL(limited_color_range);
14088 
14089 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
14090 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
14091 	PIPE_CONF_CHECK_BOOL(has_infoframe);
14092 	PIPE_CONF_CHECK_BOOL(fec_enable);
14093 
14094 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
14095 
14096 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14097 			      DRM_MODE_FLAG_INTERLACE);
14098 
14099 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
14100 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14101 				      DRM_MODE_FLAG_PHSYNC);
14102 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14103 				      DRM_MODE_FLAG_NHSYNC);
14104 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14105 				      DRM_MODE_FLAG_PVSYNC);
14106 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14107 				      DRM_MODE_FLAG_NVSYNC);
14108 	}
14109 
14110 	PIPE_CONF_CHECK_X(gmch_pfit.control);
14111 	/* pfit ratios are autocomputed by the hw on gen4+ */
14112 	if (INTEL_GEN(dev_priv) < 4)
14113 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
14114 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
14115 
14116 	/*
14117 	 * Changing the EDP transcoder input mux
14118 	 * (A_ONOFF vs. A_ON) requires a full modeset.
14119 	 */
14120 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
14121 
14122 	if (!fastset) {
14123 		PIPE_CONF_CHECK_I(pipe_src_w);
14124 		PIPE_CONF_CHECK_I(pipe_src_h);
14125 
14126 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
14127 		if (current_config->pch_pfit.enabled) {
14128 			PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
14129 			PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
14130 			PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
14131 			PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
14132 		}
14133 
14134 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
14135 		PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
14136 
14137 		PIPE_CONF_CHECK_X(gamma_mode);
14138 		if (IS_CHERRYVIEW(dev_priv))
14139 			PIPE_CONF_CHECK_X(cgm_mode);
14140 		else
14141 			PIPE_CONF_CHECK_X(csc_mode);
14142 		PIPE_CONF_CHECK_BOOL(gamma_enable);
14143 		PIPE_CONF_CHECK_BOOL(csc_enable);
14144 
14145 		PIPE_CONF_CHECK_I(linetime);
14146 		PIPE_CONF_CHECK_I(ips_linetime);
14147 
14148 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
14149 		if (bp_gamma)
14150 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
14151 	}
14152 
14153 	PIPE_CONF_CHECK_BOOL(double_wide);
14154 
14155 	PIPE_CONF_CHECK_P(shared_dpll);
14156 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
14157 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
14158 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
14159 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
14160 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
14161 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
14162 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
14163 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
14164 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
14165 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
14166 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
14167 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
14168 	PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
14169 	PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
14170 	PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
14171 	PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
14172 	PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
14173 	PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
14174 	PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
14175 	PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
14176 	PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
14177 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
14178 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
14179 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
14180 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
14181 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
14182 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
14183 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
14184 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
14185 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
14186 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
14187 
14188 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
14189 	PIPE_CONF_CHECK_X(dsi_pll.div);
14190 
14191 	if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
14192 		PIPE_CONF_CHECK_I(pipe_bpp);
14193 
14194 	PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
14195 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
14196 
14197 	PIPE_CONF_CHECK_I(min_voltage_level);
14198 
14199 	PIPE_CONF_CHECK_X(infoframes.enable);
14200 	PIPE_CONF_CHECK_X(infoframes.gcp);
14201 	PIPE_CONF_CHECK_INFOFRAME(avi);
14202 	PIPE_CONF_CHECK_INFOFRAME(spd);
14203 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
14204 	PIPE_CONF_CHECK_INFOFRAME(drm);
14205 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
14206 
14207 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
14208 	PIPE_CONF_CHECK_I(master_transcoder);
14209 
14210 	PIPE_CONF_CHECK_I(dsc.compression_enable);
14211 	PIPE_CONF_CHECK_I(dsc.dsc_split);
14212 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
14213 
14214 	PIPE_CONF_CHECK_I(mst_master_transcoder);
14215 
14216 #undef PIPE_CONF_CHECK_X
14217 #undef PIPE_CONF_CHECK_I
14218 #undef PIPE_CONF_CHECK_BOOL
14219 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
14220 #undef PIPE_CONF_CHECK_P
14221 #undef PIPE_CONF_CHECK_FLAGS
14222 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
14223 #undef PIPE_CONF_CHECK_COLOR_LUT
14224 #undef PIPE_CONF_QUIRK
14225 
14226 	return ret;
14227 }
14228 
14229 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
14230 					   const struct intel_crtc_state *pipe_config)
14231 {
14232 	if (pipe_config->has_pch_encoder) {
14233 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
14234 							    &pipe_config->fdi_m_n);
14235 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
14236 
14237 		/*
14238 		 * FDI already provided one idea for the dotclock.
14239 		 * Yell if the encoder disagrees.
14240 		 */
14241 		drm_WARN(&dev_priv->drm,
14242 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
14243 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
14244 			 fdi_dotclock, dotclock);
14245 	}
14246 }
14247 
14248 static void verify_wm_state(struct intel_crtc *crtc,
14249 			    struct intel_crtc_state *new_crtc_state)
14250 {
14251 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14252 	struct skl_hw_state {
14253 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
14254 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
14255 		struct skl_pipe_wm wm;
14256 	} *hw;
14257 	struct skl_pipe_wm *sw_wm;
14258 	struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
14259 	u8 hw_enabled_slices;
14260 	const enum pipe pipe = crtc->pipe;
14261 	int plane, level, max_level = ilk_wm_max_level(dev_priv);
14262 
14263 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
14264 		return;
14265 
14266 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
14267 	if (!hw)
14268 		return;
14269 
14270 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
14271 	sw_wm = &new_crtc_state->wm.skl.optimal;
14272 
14273 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
14274 
14275 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
14276 
14277 	if (INTEL_GEN(dev_priv) >= 11 &&
14278 	    hw_enabled_slices != dev_priv->dbuf.enabled_slices)
14279 		drm_err(&dev_priv->drm,
14280 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
14281 			dev_priv->dbuf.enabled_slices,
14282 			hw_enabled_slices);
14283 
14284 	/* planes */
14285 	for_each_universal_plane(dev_priv, pipe, plane) {
14286 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14287 
14288 		hw_plane_wm = &hw->wm.planes[plane];
14289 		sw_plane_wm = &sw_wm->planes[plane];
14290 
14291 		/* Watermarks */
14292 		for (level = 0; level <= max_level; level++) {
14293 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14294 						&sw_plane_wm->wm[level]) ||
14295 			    (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14296 							       &sw_plane_wm->sagv_wm0)))
14297 				continue;
14298 
14299 			drm_err(&dev_priv->drm,
14300 				"mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14301 				pipe_name(pipe), plane + 1, level,
14302 				sw_plane_wm->wm[level].plane_en,
14303 				sw_plane_wm->wm[level].plane_res_b,
14304 				sw_plane_wm->wm[level].plane_res_l,
14305 				hw_plane_wm->wm[level].plane_en,
14306 				hw_plane_wm->wm[level].plane_res_b,
14307 				hw_plane_wm->wm[level].plane_res_l);
14308 		}
14309 
14310 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14311 					 &sw_plane_wm->trans_wm)) {
14312 			drm_err(&dev_priv->drm,
14313 				"mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14314 				pipe_name(pipe), plane + 1,
14315 				sw_plane_wm->trans_wm.plane_en,
14316 				sw_plane_wm->trans_wm.plane_res_b,
14317 				sw_plane_wm->trans_wm.plane_res_l,
14318 				hw_plane_wm->trans_wm.plane_en,
14319 				hw_plane_wm->trans_wm.plane_res_b,
14320 				hw_plane_wm->trans_wm.plane_res_l);
14321 		}
14322 
14323 		/* DDB */
14324 		hw_ddb_entry = &hw->ddb_y[plane];
14325 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
14326 
14327 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14328 			drm_err(&dev_priv->drm,
14329 				"mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14330 				pipe_name(pipe), plane + 1,
14331 				sw_ddb_entry->start, sw_ddb_entry->end,
14332 				hw_ddb_entry->start, hw_ddb_entry->end);
14333 		}
14334 	}
14335 
14336 	/*
14337 	 * cursor
14338 	 * If the cursor plane isn't active, we may not have updated it's ddb
14339 	 * allocation. In that case since the ddb allocation will be updated
14340 	 * once the plane becomes visible, we can skip this check
14341 	 */
14342 	if (1) {
14343 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14344 
14345 		hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
14346 		sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14347 
14348 		/* Watermarks */
14349 		for (level = 0; level <= max_level; level++) {
14350 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14351 						&sw_plane_wm->wm[level]) ||
14352 			    (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14353 							       &sw_plane_wm->sagv_wm0)))
14354 				continue;
14355 
14356 			drm_err(&dev_priv->drm,
14357 				"mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14358 				pipe_name(pipe), level,
14359 				sw_plane_wm->wm[level].plane_en,
14360 				sw_plane_wm->wm[level].plane_res_b,
14361 				sw_plane_wm->wm[level].plane_res_l,
14362 				hw_plane_wm->wm[level].plane_en,
14363 				hw_plane_wm->wm[level].plane_res_b,
14364 				hw_plane_wm->wm[level].plane_res_l);
14365 		}
14366 
14367 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14368 					 &sw_plane_wm->trans_wm)) {
14369 			drm_err(&dev_priv->drm,
14370 				"mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14371 				pipe_name(pipe),
14372 				sw_plane_wm->trans_wm.plane_en,
14373 				sw_plane_wm->trans_wm.plane_res_b,
14374 				sw_plane_wm->trans_wm.plane_res_l,
14375 				hw_plane_wm->trans_wm.plane_en,
14376 				hw_plane_wm->trans_wm.plane_res_b,
14377 				hw_plane_wm->trans_wm.plane_res_l);
14378 		}
14379 
14380 		/* DDB */
14381 		hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
14382 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
14383 
14384 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14385 			drm_err(&dev_priv->drm,
14386 				"mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14387 				pipe_name(pipe),
14388 				sw_ddb_entry->start, sw_ddb_entry->end,
14389 				hw_ddb_entry->start, hw_ddb_entry->end);
14390 		}
14391 	}
14392 
14393 	kfree(hw);
14394 }
14395 
14396 static void
14397 verify_connector_state(struct intel_atomic_state *state,
14398 		       struct intel_crtc *crtc)
14399 {
14400 	struct drm_connector *connector;
14401 	struct drm_connector_state *new_conn_state;
14402 	int i;
14403 
14404 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
14405 		struct drm_encoder *encoder = connector->encoder;
14406 		struct intel_crtc_state *crtc_state = NULL;
14407 
14408 		if (new_conn_state->crtc != &crtc->base)
14409 			continue;
14410 
14411 		if (crtc)
14412 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
14413 
14414 		intel_connector_verify_state(crtc_state, new_conn_state);
14415 
14416 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
14417 		     "connector's atomic encoder doesn't match legacy encoder\n");
14418 	}
14419 }
14420 
14421 static void
14422 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
14423 {
14424 	struct intel_encoder *encoder;
14425 	struct drm_connector *connector;
14426 	struct drm_connector_state *old_conn_state, *new_conn_state;
14427 	int i;
14428 
14429 	for_each_intel_encoder(&dev_priv->drm, encoder) {
14430 		bool enabled = false, found = false;
14431 		enum pipe pipe;
14432 
14433 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14434 			    encoder->base.base.id,
14435 			    encoder->base.name);
14436 
14437 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
14438 						   new_conn_state, i) {
14439 			if (old_conn_state->best_encoder == &encoder->base)
14440 				found = true;
14441 
14442 			if (new_conn_state->best_encoder != &encoder->base)
14443 				continue;
14444 			found = enabled = true;
14445 
14446 			I915_STATE_WARN(new_conn_state->crtc !=
14447 					encoder->base.crtc,
14448 			     "connector's crtc doesn't match encoder crtc\n");
14449 		}
14450 
14451 		if (!found)
14452 			continue;
14453 
14454 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
14455 		     "encoder's enabled state mismatch "
14456 		     "(expected %i, found %i)\n",
14457 		     !!encoder->base.crtc, enabled);
14458 
14459 		if (!encoder->base.crtc) {
14460 			bool active;
14461 
14462 			active = encoder->get_hw_state(encoder, &pipe);
14463 			I915_STATE_WARN(active,
14464 			     "encoder detached but still enabled on pipe %c.\n",
14465 			     pipe_name(pipe));
14466 		}
14467 	}
14468 }
14469 
14470 static void
14471 verify_crtc_state(struct intel_crtc *crtc,
14472 		  struct intel_crtc_state *old_crtc_state,
14473 		  struct intel_crtc_state *new_crtc_state)
14474 {
14475 	struct drm_device *dev = crtc->base.dev;
14476 	struct drm_i915_private *dev_priv = to_i915(dev);
14477 	struct intel_encoder *encoder;
14478 	struct intel_crtc_state *pipe_config = old_crtc_state;
14479 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
14480 
14481 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
14482 	intel_crtc_free_hw_state(old_crtc_state);
14483 	intel_crtc_state_reset(old_crtc_state, crtc);
14484 	old_crtc_state->uapi.state = state;
14485 
14486 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14487 		    crtc->base.name);
14488 
14489 	pipe_config->hw.enable = new_crtc_state->hw.enable;
14490 
14491 	pipe_config->hw.active =
14492 		dev_priv->display.get_pipe_config(crtc, pipe_config);
14493 
14494 	/* we keep both pipes enabled on 830 */
14495 	if (IS_I830(dev_priv) && pipe_config->hw.active)
14496 		pipe_config->hw.active = new_crtc_state->hw.active;
14497 
14498 	I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
14499 			"crtc active state doesn't match with hw state "
14500 			"(expected %i, found %i)\n",
14501 			new_crtc_state->hw.active, pipe_config->hw.active);
14502 
14503 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14504 			"transitional active state does not match atomic hw state "
14505 			"(expected %i, found %i)\n",
14506 			new_crtc_state->hw.active, crtc->active);
14507 
14508 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14509 		enum pipe pipe;
14510 		bool active;
14511 
14512 		active = encoder->get_hw_state(encoder, &pipe);
14513 		I915_STATE_WARN(active != new_crtc_state->hw.active,
14514 				"[ENCODER:%i] active %i with crtc active %i\n",
14515 				encoder->base.base.id, active,
14516 				new_crtc_state->hw.active);
14517 
14518 		I915_STATE_WARN(active && crtc->pipe != pipe,
14519 				"Encoder connected to wrong pipe %c\n",
14520 				pipe_name(pipe));
14521 
14522 		if (active)
14523 			encoder->get_config(encoder, pipe_config);
14524 	}
14525 
14526 	intel_crtc_compute_pixel_rate(pipe_config);
14527 
14528 	if (!new_crtc_state->hw.active)
14529 		return;
14530 
14531 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
14532 
14533 	if (!intel_pipe_config_compare(new_crtc_state,
14534 				       pipe_config, false)) {
14535 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
14536 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
14537 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
14538 	}
14539 }
14540 
14541 static void
14542 intel_verify_planes(struct intel_atomic_state *state)
14543 {
14544 	struct intel_plane *plane;
14545 	const struct intel_plane_state *plane_state;
14546 	int i;
14547 
14548 	for_each_new_intel_plane_in_state(state, plane,
14549 					  plane_state, i)
14550 		assert_plane(plane, plane_state->planar_slave ||
14551 			     plane_state->uapi.visible);
14552 }
14553 
14554 static void
14555 verify_single_dpll_state(struct drm_i915_private *dev_priv,
14556 			 struct intel_shared_dpll *pll,
14557 			 struct intel_crtc *crtc,
14558 			 struct intel_crtc_state *new_crtc_state)
14559 {
14560 	struct intel_dpll_hw_state dpll_hw_state;
14561 	unsigned int crtc_mask;
14562 	bool active;
14563 
14564 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
14565 
14566 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
14567 
14568 	active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
14569 
14570 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
14571 		I915_STATE_WARN(!pll->on && pll->active_mask,
14572 		     "pll in active use but not on in sw tracking\n");
14573 		I915_STATE_WARN(pll->on && !pll->active_mask,
14574 		     "pll is on but not used by any active crtc\n");
14575 		I915_STATE_WARN(pll->on != active,
14576 		     "pll on state mismatch (expected %i, found %i)\n",
14577 		     pll->on, active);
14578 	}
14579 
14580 	if (!crtc) {
14581 		I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
14582 				"more active pll users than references: %x vs %x\n",
14583 				pll->active_mask, pll->state.crtc_mask);
14584 
14585 		return;
14586 	}
14587 
14588 	crtc_mask = drm_crtc_mask(&crtc->base);
14589 
14590 	if (new_crtc_state->hw.active)
14591 		I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14592 				"pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
14593 				pipe_name(crtc->pipe), pll->active_mask);
14594 	else
14595 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14596 				"pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
14597 				pipe_name(crtc->pipe), pll->active_mask);
14598 
14599 	I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
14600 			"pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
14601 			crtc_mask, pll->state.crtc_mask);
14602 
14603 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
14604 					  &dpll_hw_state,
14605 					  sizeof(dpll_hw_state)),
14606 			"pll hw state mismatch\n");
14607 }
14608 
14609 static void
14610 verify_shared_dpll_state(struct intel_crtc *crtc,
14611 			 struct intel_crtc_state *old_crtc_state,
14612 			 struct intel_crtc_state *new_crtc_state)
14613 {
14614 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14615 
14616 	if (new_crtc_state->shared_dpll)
14617 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
14618 
14619 	if (old_crtc_state->shared_dpll &&
14620 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14621 		unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14622 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
14623 
14624 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14625 				"pll active mismatch (didn't expect pipe %c in active mask)\n",
14626 				pipe_name(crtc->pipe));
14627 		I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
14628 				"pll enabled crtcs mismatch (found %x in enabled mask)\n",
14629 				pipe_name(crtc->pipe));
14630 	}
14631 }
14632 
14633 static void
14634 intel_modeset_verify_crtc(struct intel_crtc *crtc,
14635 			  struct intel_atomic_state *state,
14636 			  struct intel_crtc_state *old_crtc_state,
14637 			  struct intel_crtc_state *new_crtc_state)
14638 {
14639 	if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
14640 		return;
14641 
14642 	verify_wm_state(crtc, new_crtc_state);
14643 	verify_connector_state(state, crtc);
14644 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14645 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
14646 }
14647 
14648 static void
14649 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
14650 {
14651 	int i;
14652 
14653 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14654 		verify_single_dpll_state(dev_priv,
14655 					 &dev_priv->dpll.shared_dplls[i],
14656 					 NULL, NULL);
14657 }
14658 
14659 static void
14660 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
14661 			      struct intel_atomic_state *state)
14662 {
14663 	verify_encoder_state(dev_priv, state);
14664 	verify_connector_state(state, NULL);
14665 	verify_disabled_dpll_state(dev_priv);
14666 }
14667 
14668 static void
14669 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
14670 {
14671 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
14672 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14673 	const struct drm_display_mode *adjusted_mode =
14674 		&crtc_state->hw.adjusted_mode;
14675 
14676 	drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
14677 
14678 	crtc->mode_flags = crtc_state->mode_flags;
14679 
14680 	/*
14681 	 * The scanline counter increments at the leading edge of hsync.
14682 	 *
14683 	 * On most platforms it starts counting from vtotal-1 on the
14684 	 * first active line. That means the scanline counter value is
14685 	 * always one less than what we would expect. Ie. just after
14686 	 * start of vblank, which also occurs at start of hsync (on the
14687 	 * last active line), the scanline counter will read vblank_start-1.
14688 	 *
14689 	 * On gen2 the scanline counter starts counting from 1 instead
14690 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14691 	 * to keep the value positive), instead of adding one.
14692 	 *
14693 	 * On HSW+ the behaviour of the scanline counter depends on the output
14694 	 * type. For DP ports it behaves like most other platforms, but on HDMI
14695 	 * there's an extra 1 line difference. So we need to add two instead of
14696 	 * one to the value.
14697 	 *
14698 	 * On VLV/CHV DSI the scanline counter would appear to increment
14699 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14700 	 * that means we can't tell whether we're in vblank or not while
14701 	 * we're on that particular line. We must still set scanline_offset
14702 	 * to 1 so that the vblank timestamps come out correct when we query
14703 	 * the scanline counter from within the vblank interrupt handler.
14704 	 * However if queried just before the start of vblank we'll get an
14705 	 * answer that's slightly in the future.
14706 	 */
14707 	if (IS_GEN(dev_priv, 2)) {
14708 		int vtotal;
14709 
14710 		vtotal = adjusted_mode->crtc_vtotal;
14711 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
14712 			vtotal /= 2;
14713 
14714 		crtc->scanline_offset = vtotal - 1;
14715 	} else if (HAS_DDI(dev_priv) &&
14716 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
14717 		crtc->scanline_offset = 2;
14718 	} else {
14719 		crtc->scanline_offset = 1;
14720 	}
14721 }
14722 
14723 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
14724 {
14725 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14726 	struct intel_crtc_state *new_crtc_state;
14727 	struct intel_crtc *crtc;
14728 	int i;
14729 
14730 	if (!dev_priv->display.crtc_compute_clock)
14731 		return;
14732 
14733 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14734 		if (!needs_modeset(new_crtc_state))
14735 			continue;
14736 
14737 		intel_release_shared_dplls(state, crtc);
14738 	}
14739 }
14740 
14741 /*
14742  * This implements the workaround described in the "notes" section of the mode
14743  * set sequence documentation. When going from no pipes or single pipe to
14744  * multiple pipes, and planes are enabled after the pipe, we need to wait at
14745  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14746  */
14747 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
14748 {
14749 	struct intel_crtc_state *crtc_state;
14750 	struct intel_crtc *crtc;
14751 	struct intel_crtc_state *first_crtc_state = NULL;
14752 	struct intel_crtc_state *other_crtc_state = NULL;
14753 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14754 	int i;
14755 
14756 	/* look at all crtc's that are going to be enabled in during modeset */
14757 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14758 		if (!crtc_state->hw.active ||
14759 		    !needs_modeset(crtc_state))
14760 			continue;
14761 
14762 		if (first_crtc_state) {
14763 			other_crtc_state = crtc_state;
14764 			break;
14765 		} else {
14766 			first_crtc_state = crtc_state;
14767 			first_pipe = crtc->pipe;
14768 		}
14769 	}
14770 
14771 	/* No workaround needed? */
14772 	if (!first_crtc_state)
14773 		return 0;
14774 
14775 	/* w/a possibly needed, check how many crtc's are already enabled. */
14776 	for_each_intel_crtc(state->base.dev, crtc) {
14777 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14778 		if (IS_ERR(crtc_state))
14779 			return PTR_ERR(crtc_state);
14780 
14781 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
14782 
14783 		if (!crtc_state->hw.active ||
14784 		    needs_modeset(crtc_state))
14785 			continue;
14786 
14787 		/* 2 or more enabled crtcs means no need for w/a */
14788 		if (enabled_pipe != INVALID_PIPE)
14789 			return 0;
14790 
14791 		enabled_pipe = crtc->pipe;
14792 	}
14793 
14794 	if (enabled_pipe != INVALID_PIPE)
14795 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
14796 	else if (other_crtc_state)
14797 		other_crtc_state->hsw_workaround_pipe = first_pipe;
14798 
14799 	return 0;
14800 }
14801 
14802 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
14803 			   u8 active_pipes)
14804 {
14805 	const struct intel_crtc_state *crtc_state;
14806 	struct intel_crtc *crtc;
14807 	int i;
14808 
14809 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14810 		if (crtc_state->hw.active)
14811 			active_pipes |= BIT(crtc->pipe);
14812 		else
14813 			active_pipes &= ~BIT(crtc->pipe);
14814 	}
14815 
14816 	return active_pipes;
14817 }
14818 
14819 static int intel_modeset_checks(struct intel_atomic_state *state)
14820 {
14821 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14822 
14823 	state->modeset = true;
14824 
14825 	if (IS_HASWELL(dev_priv))
14826 		return hsw_mode_set_planes_workaround(state);
14827 
14828 	return 0;
14829 }
14830 
14831 /*
14832  * Handle calculation of various watermark data at the end of the atomic check
14833  * phase.  The code here should be run after the per-crtc and per-plane 'check'
14834  * handlers to ensure that all derived state has been updated.
14835  */
14836 static int calc_watermark_data(struct intel_atomic_state *state)
14837 {
14838 	struct drm_device *dev = state->base.dev;
14839 	struct drm_i915_private *dev_priv = to_i915(dev);
14840 
14841 	/* Is there platform-specific watermark information to calculate? */
14842 	if (dev_priv->display.compute_global_watermarks)
14843 		return dev_priv->display.compute_global_watermarks(state);
14844 
14845 	return 0;
14846 }
14847 
14848 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
14849 				     struct intel_crtc_state *new_crtc_state)
14850 {
14851 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
14852 		return;
14853 
14854 	new_crtc_state->uapi.mode_changed = false;
14855 	new_crtc_state->update_pipe = true;
14856 }
14857 
14858 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
14859 				    struct intel_crtc_state *new_crtc_state)
14860 {
14861 	/*
14862 	 * If we're not doing the full modeset we want to
14863 	 * keep the current M/N values as they may be
14864 	 * sufficiently different to the computed values
14865 	 * to cause problems.
14866 	 *
14867 	 * FIXME: should really copy more fuzzy state here
14868 	 */
14869 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
14870 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
14871 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
14872 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
14873 }
14874 
14875 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
14876 					  struct intel_crtc *crtc,
14877 					  u8 plane_ids_mask)
14878 {
14879 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14880 	struct intel_plane *plane;
14881 
14882 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14883 		struct intel_plane_state *plane_state;
14884 
14885 		if ((plane_ids_mask & BIT(plane->id)) == 0)
14886 			continue;
14887 
14888 		plane_state = intel_atomic_get_plane_state(state, plane);
14889 		if (IS_ERR(plane_state))
14890 			return PTR_ERR(plane_state);
14891 	}
14892 
14893 	return 0;
14894 }
14895 
14896 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
14897 {
14898 	/* See {hsw,vlv,ivb}_plane_ratio() */
14899 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
14900 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
14901 		IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
14902 }
14903 
14904 static int intel_atomic_check_planes(struct intel_atomic_state *state)
14905 {
14906 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14907 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14908 	struct intel_plane_state *plane_state;
14909 	struct intel_plane *plane;
14910 	struct intel_crtc *crtc;
14911 	int i, ret;
14912 
14913 	ret = icl_add_linked_planes(state);
14914 	if (ret)
14915 		return ret;
14916 
14917 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14918 		ret = intel_plane_atomic_check(state, plane);
14919 		if (ret) {
14920 			drm_dbg_atomic(&dev_priv->drm,
14921 				       "[PLANE:%d:%s] atomic driver check failed\n",
14922 				       plane->base.base.id, plane->base.name);
14923 			return ret;
14924 		}
14925 	}
14926 
14927 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14928 					    new_crtc_state, i) {
14929 		u8 old_active_planes, new_active_planes;
14930 
14931 		ret = icl_check_nv12_planes(new_crtc_state);
14932 		if (ret)
14933 			return ret;
14934 
14935 		/*
14936 		 * On some platforms the number of active planes affects
14937 		 * the planes' minimum cdclk calculation. Add such planes
14938 		 * to the state before we compute the minimum cdclk.
14939 		 */
14940 		if (!active_planes_affects_min_cdclk(dev_priv))
14941 			continue;
14942 
14943 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14944 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14945 
14946 		/*
14947 		 * Not only the number of planes, but if the plane configuration had
14948 		 * changed might already mean we need to recompute min CDCLK,
14949 		 * because different planes might consume different amount of Dbuf bandwidth
14950 		 * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
14951 		 */
14952 		if (old_active_planes == new_active_planes)
14953 			continue;
14954 
14955 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
14956 		if (ret)
14957 			return ret;
14958 	}
14959 
14960 	return 0;
14961 }
14962 
14963 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
14964 				    bool *need_cdclk_calc)
14965 {
14966 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14967 	const struct intel_cdclk_state *old_cdclk_state;
14968 	const struct intel_cdclk_state *new_cdclk_state;
14969 	struct intel_plane_state *plane_state;
14970 	struct intel_bw_state *new_bw_state;
14971 	struct intel_plane *plane;
14972 	int min_cdclk = 0;
14973 	enum pipe pipe;
14974 	int ret;
14975 	int i;
14976 	/*
14977 	 * active_planes bitmask has been updated, and potentially
14978 	 * affected planes are part of the state. We can now
14979 	 * compute the minimum cdclk for each plane.
14980 	 */
14981 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14982 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
14983 		if (ret)
14984 			return ret;
14985 	}
14986 
14987 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
14988 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
14989 
14990 	if (new_cdclk_state &&
14991 	    old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
14992 		*need_cdclk_calc = true;
14993 
14994 	ret = dev_priv->display.bw_calc_min_cdclk(state);
14995 	if (ret)
14996 		return ret;
14997 
14998 	new_bw_state = intel_atomic_get_new_bw_state(state);
14999 
15000 	if (!new_cdclk_state || !new_bw_state)
15001 		return 0;
15002 
15003 	for_each_pipe(dev_priv, pipe) {
15004 		min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
15005 
15006 		/*
15007 		 * Currently do this change only if we need to increase
15008 		 */
15009 		if (new_bw_state->min_cdclk > min_cdclk)
15010 			*need_cdclk_calc = true;
15011 	}
15012 
15013 	return 0;
15014 }
15015 
15016 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
15017 {
15018 	struct intel_crtc_state *crtc_state;
15019 	struct intel_crtc *crtc;
15020 	int i;
15021 
15022 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15023 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
15024 		int ret;
15025 
15026 		ret = intel_crtc_atomic_check(state, crtc);
15027 		if (ret) {
15028 			drm_dbg_atomic(&i915->drm,
15029 				       "[CRTC:%d:%s] atomic driver check failed\n",
15030 				       crtc->base.base.id, crtc->base.name);
15031 			return ret;
15032 		}
15033 	}
15034 
15035 	return 0;
15036 }
15037 
15038 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
15039 					       u8 transcoders)
15040 {
15041 	const struct intel_crtc_state *new_crtc_state;
15042 	struct intel_crtc *crtc;
15043 	int i;
15044 
15045 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15046 		if (new_crtc_state->hw.enable &&
15047 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
15048 		    needs_modeset(new_crtc_state))
15049 			return true;
15050 	}
15051 
15052 	return false;
15053 }
15054 
15055 /**
15056  * DOC: asynchronous flip implementation
15057  *
15058  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
15059  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
15060  * Correspondingly, support is currently added for primary plane only.
15061  *
15062  * Async flip can only change the plane surface address, so anything else
15063  * changing is rejected from the intel_atomic_check_async() function.
15064  * Once this check is cleared, flip done interrupt is enabled using
15065  * the skl_enable_flip_done() function.
15066  *
15067  * As soon as the surface address register is written, flip done interrupt is
15068  * generated and the requested events are sent to the usersapce in the interrupt
15069  * handler itself. The timestamp and sequence sent during the flip done event
15070  * correspond to the last vblank and have no relation to the actual time when
15071  * the flip done event was sent.
15072  */
15073 static int intel_atomic_check_async(struct intel_atomic_state *state)
15074 {
15075 	struct drm_i915_private *i915 = to_i915(state->base.dev);
15076 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15077 	const struct intel_plane_state *new_plane_state, *old_plane_state;
15078 	struct intel_crtc *crtc;
15079 	struct intel_plane *plane;
15080 	int i;
15081 
15082 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15083 					    new_crtc_state, i) {
15084 		if (needs_modeset(new_crtc_state)) {
15085 			drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
15086 			return -EINVAL;
15087 		}
15088 
15089 		if (!new_crtc_state->hw.active) {
15090 			drm_dbg_kms(&i915->drm, "CRTC inactive\n");
15091 			return -EINVAL;
15092 		}
15093 		if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
15094 			drm_dbg_kms(&i915->drm,
15095 				    "Active planes cannot be changed during async flip\n");
15096 			return -EINVAL;
15097 		}
15098 	}
15099 
15100 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15101 					     new_plane_state, i) {
15102 		/*
15103 		 * TODO: Async flip is only supported through the page flip IOCTL
15104 		 * as of now. So support currently added for primary plane only.
15105 		 * Support for other planes on platforms on which supports
15106 		 * this(vlv/chv and icl+) should be added when async flip is
15107 		 * enabled in the atomic IOCTL path.
15108 		 */
15109 		if (plane->id != PLANE_PRIMARY)
15110 			return -EINVAL;
15111 
15112 		/*
15113 		 * FIXME: This check is kept generic for all platforms.
15114 		 * Need to verify this for all gen9 and gen10 platforms to enable
15115 		 * this selectively if required.
15116 		 */
15117 		switch (new_plane_state->hw.fb->modifier) {
15118 		case I915_FORMAT_MOD_X_TILED:
15119 		case I915_FORMAT_MOD_Y_TILED:
15120 		case I915_FORMAT_MOD_Yf_TILED:
15121 			break;
15122 		default:
15123 			drm_dbg_kms(&i915->drm,
15124 				    "Linear memory/CCS does not support async flips\n");
15125 			return -EINVAL;
15126 		}
15127 
15128 		if (old_plane_state->color_plane[0].stride !=
15129 		    new_plane_state->color_plane[0].stride) {
15130 			drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
15131 			return -EINVAL;
15132 		}
15133 
15134 		if (old_plane_state->hw.fb->modifier !=
15135 		    new_plane_state->hw.fb->modifier) {
15136 			drm_dbg_kms(&i915->drm,
15137 				    "Framebuffer modifiers cannot be changed in async flip\n");
15138 			return -EINVAL;
15139 		}
15140 
15141 		if (old_plane_state->hw.fb->format !=
15142 		    new_plane_state->hw.fb->format) {
15143 			drm_dbg_kms(&i915->drm,
15144 				    "Framebuffer format cannot be changed in async flip\n");
15145 			return -EINVAL;
15146 		}
15147 
15148 		if (old_plane_state->hw.rotation !=
15149 		    new_plane_state->hw.rotation) {
15150 			drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
15151 			return -EINVAL;
15152 		}
15153 
15154 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
15155 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
15156 			drm_dbg_kms(&i915->drm,
15157 				    "Plane size/co-ordinates cannot be changed in async flip\n");
15158 			return -EINVAL;
15159 		}
15160 
15161 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
15162 			drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
15163 			return -EINVAL;
15164 		}
15165 
15166 		if (old_plane_state->hw.pixel_blend_mode !=
15167 		    new_plane_state->hw.pixel_blend_mode) {
15168 			drm_dbg_kms(&i915->drm,
15169 				    "Pixel blend mode cannot be changed in async flip\n");
15170 			return -EINVAL;
15171 		}
15172 
15173 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
15174 			drm_dbg_kms(&i915->drm,
15175 				    "Color encoding cannot be changed in async flip\n");
15176 			return -EINVAL;
15177 		}
15178 
15179 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
15180 			drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
15181 			return -EINVAL;
15182 		}
15183 	}
15184 
15185 	return 0;
15186 }
15187 
15188 /**
15189  * intel_atomic_check - validate state object
15190  * @dev: drm device
15191  * @_state: state to validate
15192  */
15193 static int intel_atomic_check(struct drm_device *dev,
15194 			      struct drm_atomic_state *_state)
15195 {
15196 	struct drm_i915_private *dev_priv = to_i915(dev);
15197 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
15198 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15199 	struct intel_crtc *crtc;
15200 	int ret, i;
15201 	bool any_ms = false;
15202 
15203 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15204 					    new_crtc_state, i) {
15205 		if (new_crtc_state->inherited != old_crtc_state->inherited)
15206 			new_crtc_state->uapi.mode_changed = true;
15207 	}
15208 
15209 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
15210 	if (ret)
15211 		goto fail;
15212 
15213 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15214 					    new_crtc_state, i) {
15215 		if (!needs_modeset(new_crtc_state)) {
15216 			/* Light copy */
15217 			intel_crtc_copy_uapi_to_hw_state_nomodeset(new_crtc_state);
15218 
15219 			continue;
15220 		}
15221 
15222 		ret = intel_crtc_prepare_cleared_state(new_crtc_state);
15223 		if (ret)
15224 			goto fail;
15225 
15226 		if (!new_crtc_state->hw.enable)
15227 			continue;
15228 
15229 		ret = intel_modeset_pipe_config(new_crtc_state);
15230 		if (ret)
15231 			goto fail;
15232 	}
15233 
15234 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15235 					    new_crtc_state, i) {
15236 		if (!needs_modeset(new_crtc_state))
15237 			continue;
15238 
15239 		ret = intel_modeset_pipe_config_late(new_crtc_state);
15240 		if (ret)
15241 			goto fail;
15242 
15243 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
15244 	}
15245 
15246 	/**
15247 	 * Check if fastset is allowed by external dependencies like other
15248 	 * pipes and transcoders.
15249 	 *
15250 	 * Right now it only forces a fullmodeset when the MST master
15251 	 * transcoder did not changed but the pipe of the master transcoder
15252 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
15253 	 * in case of port synced crtcs, if one of the synced crtcs
15254 	 * needs a full modeset, all other synced crtcs should be
15255 	 * forced a full modeset.
15256 	 */
15257 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15258 		if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
15259 			continue;
15260 
15261 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
15262 			enum transcoder master = new_crtc_state->mst_master_transcoder;
15263 
15264 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
15265 				new_crtc_state->uapi.mode_changed = true;
15266 				new_crtc_state->update_pipe = false;
15267 			}
15268 		}
15269 
15270 		if (is_trans_port_sync_mode(new_crtc_state)) {
15271 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
15272 
15273 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
15274 				trans |= BIT(new_crtc_state->master_transcoder);
15275 
15276 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
15277 				new_crtc_state->uapi.mode_changed = true;
15278 				new_crtc_state->update_pipe = false;
15279 			}
15280 		}
15281 	}
15282 
15283 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15284 					    new_crtc_state, i) {
15285 		if (needs_modeset(new_crtc_state)) {
15286 			any_ms = true;
15287 			continue;
15288 		}
15289 
15290 		if (!new_crtc_state->update_pipe)
15291 			continue;
15292 
15293 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
15294 	}
15295 
15296 	if (any_ms && !check_digital_port_conflicts(state)) {
15297 		drm_dbg_kms(&dev_priv->drm,
15298 			    "rejecting conflicting digital port configuration\n");
15299 		ret = -EINVAL;
15300 		goto fail;
15301 	}
15302 
15303 	ret = drm_dp_mst_atomic_check(&state->base);
15304 	if (ret)
15305 		goto fail;
15306 
15307 	ret = intel_atomic_check_planes(state);
15308 	if (ret)
15309 		goto fail;
15310 
15311 	/*
15312 	 * distrust_bios_wm will force a full dbuf recomputation
15313 	 * but the hardware state will only get updated accordingly
15314 	 * if state->modeset==true. Hence distrust_bios_wm==true &&
15315 	 * state->modeset==false is an invalid combination which
15316 	 * would cause the hardware and software dbuf state to get
15317 	 * out of sync. We must prevent that.
15318 	 *
15319 	 * FIXME clean up this mess and introduce better
15320 	 * state tracking for dbuf.
15321 	 */
15322 	if (dev_priv->wm.distrust_bios_wm)
15323 		any_ms = true;
15324 
15325 	intel_fbc_choose_crtc(dev_priv, state);
15326 	ret = calc_watermark_data(state);
15327 	if (ret)
15328 		goto fail;
15329 
15330 	ret = intel_bw_atomic_check(state);
15331 	if (ret)
15332 		goto fail;
15333 
15334 	ret = intel_atomic_check_cdclk(state, &any_ms);
15335 	if (ret)
15336 		goto fail;
15337 
15338 	if (any_ms) {
15339 		ret = intel_modeset_checks(state);
15340 		if (ret)
15341 			goto fail;
15342 
15343 		ret = intel_modeset_calc_cdclk(state);
15344 		if (ret)
15345 			return ret;
15346 
15347 		intel_modeset_clear_plls(state);
15348 	}
15349 
15350 	ret = intel_atomic_check_crtcs(state);
15351 	if (ret)
15352 		goto fail;
15353 
15354 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15355 					    new_crtc_state, i) {
15356 		if (new_crtc_state->uapi.async_flip) {
15357 			ret = intel_atomic_check_async(state);
15358 			if (ret)
15359 				goto fail;
15360 		}
15361 
15362 		if (!needs_modeset(new_crtc_state) &&
15363 		    !new_crtc_state->update_pipe)
15364 			continue;
15365 
15366 		intel_dump_pipe_config(new_crtc_state, state,
15367 				       needs_modeset(new_crtc_state) ?
15368 				       "[modeset]" : "[fastset]");
15369 	}
15370 
15371 	return 0;
15372 
15373  fail:
15374 	if (ret == -EDEADLK)
15375 		return ret;
15376 
15377 	/*
15378 	 * FIXME would probably be nice to know which crtc specifically
15379 	 * caused the failure, in cases where we can pinpoint it.
15380 	 */
15381 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15382 					    new_crtc_state, i)
15383 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
15384 
15385 	return ret;
15386 }
15387 
15388 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
15389 {
15390 	struct intel_crtc_state *crtc_state;
15391 	struct intel_crtc *crtc;
15392 	int i, ret;
15393 
15394 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
15395 	if (ret < 0)
15396 		return ret;
15397 
15398 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15399 		bool mode_changed = needs_modeset(crtc_state);
15400 
15401 		if (mode_changed || crtc_state->update_pipe ||
15402 		    crtc_state->uapi.color_mgmt_changed) {
15403 			intel_dsb_prepare(crtc_state);
15404 		}
15405 	}
15406 
15407 	return 0;
15408 }
15409 
15410 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
15411 {
15412 	struct drm_device *dev = crtc->base.dev;
15413 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
15414 
15415 	if (!vblank->max_vblank_count)
15416 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
15417 
15418 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
15419 }
15420 
15421 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
15422 				  struct intel_crtc_state *crtc_state)
15423 {
15424 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15425 
15426 	if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
15427 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15428 
15429 	if (crtc_state->has_pch_encoder) {
15430 		enum pipe pch_transcoder =
15431 			intel_crtc_pch_transcoder(crtc);
15432 
15433 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
15434 	}
15435 }
15436 
15437 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
15438 			       const struct intel_crtc_state *new_crtc_state)
15439 {
15440 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
15441 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15442 
15443 	/*
15444 	 * Update pipe size and adjust fitter if needed: the reason for this is
15445 	 * that in compute_mode_changes we check the native mode (not the pfit
15446 	 * mode) to see if we can flip rather than do a full mode set. In the
15447 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
15448 	 * pfit state, we'll end up with a big fb scanned out into the wrong
15449 	 * sized surface.
15450 	 */
15451 	intel_set_pipe_src_size(new_crtc_state);
15452 
15453 	/* on skylake this is done by detaching scalers */
15454 	if (INTEL_GEN(dev_priv) >= 9) {
15455 		skl_detach_scalers(new_crtc_state);
15456 
15457 		if (new_crtc_state->pch_pfit.enabled)
15458 			skl_pfit_enable(new_crtc_state);
15459 	} else if (HAS_PCH_SPLIT(dev_priv)) {
15460 		if (new_crtc_state->pch_pfit.enabled)
15461 			ilk_pfit_enable(new_crtc_state);
15462 		else if (old_crtc_state->pch_pfit.enabled)
15463 			ilk_pfit_disable(old_crtc_state);
15464 	}
15465 
15466 	/*
15467 	 * The register is supposedly single buffered so perhaps
15468 	 * not 100% correct to do this here. But SKL+ calculate
15469 	 * this based on the adjust pixel rate so pfit changes do
15470 	 * affect it and so it must be updated for fastsets.
15471 	 * HSW/BDW only really need this here for fastboot, after
15472 	 * that the value should not change without a full modeset.
15473 	 */
15474 	if (INTEL_GEN(dev_priv) >= 9 ||
15475 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
15476 		hsw_set_linetime_wm(new_crtc_state);
15477 
15478 	if (INTEL_GEN(dev_priv) >= 11)
15479 		icl_set_pipe_chicken(crtc);
15480 }
15481 
15482 static void commit_pipe_config(struct intel_atomic_state *state,
15483 			       struct intel_crtc *crtc)
15484 {
15485 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15486 	const struct intel_crtc_state *old_crtc_state =
15487 		intel_atomic_get_old_crtc_state(state, crtc);
15488 	const struct intel_crtc_state *new_crtc_state =
15489 		intel_atomic_get_new_crtc_state(state, crtc);
15490 	bool modeset = needs_modeset(new_crtc_state);
15491 
15492 	/*
15493 	 * During modesets pipe configuration was programmed as the
15494 	 * CRTC was enabled.
15495 	 */
15496 	if (!modeset) {
15497 		if (new_crtc_state->uapi.color_mgmt_changed ||
15498 		    new_crtc_state->update_pipe)
15499 			intel_color_commit(new_crtc_state);
15500 
15501 		if (INTEL_GEN(dev_priv) >= 9)
15502 			skl_detach_scalers(new_crtc_state);
15503 
15504 		if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15505 			bdw_set_pipemisc(new_crtc_state);
15506 
15507 		if (new_crtc_state->update_pipe)
15508 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
15509 
15510 		intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
15511 	}
15512 
15513 	if (dev_priv->display.atomic_update_watermarks)
15514 		dev_priv->display.atomic_update_watermarks(state, crtc);
15515 }
15516 
15517 static void intel_enable_crtc(struct intel_atomic_state *state,
15518 			      struct intel_crtc *crtc)
15519 {
15520 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15521 	const struct intel_crtc_state *new_crtc_state =
15522 		intel_atomic_get_new_crtc_state(state, crtc);
15523 
15524 	if (!needs_modeset(new_crtc_state))
15525 		return;
15526 
15527 	intel_crtc_update_active_timings(new_crtc_state);
15528 
15529 	dev_priv->display.crtc_enable(state, crtc);
15530 
15531 	/* vblanks work again, re-enable pipe CRC. */
15532 	intel_crtc_enable_pipe_crc(crtc);
15533 }
15534 
15535 static void intel_update_crtc(struct intel_atomic_state *state,
15536 			      struct intel_crtc *crtc)
15537 {
15538 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15539 	const struct intel_crtc_state *old_crtc_state =
15540 		intel_atomic_get_old_crtc_state(state, crtc);
15541 	struct intel_crtc_state *new_crtc_state =
15542 		intel_atomic_get_new_crtc_state(state, crtc);
15543 	bool modeset = needs_modeset(new_crtc_state);
15544 
15545 	if (!modeset) {
15546 		if (new_crtc_state->preload_luts &&
15547 		    (new_crtc_state->uapi.color_mgmt_changed ||
15548 		     new_crtc_state->update_pipe))
15549 			intel_color_load_luts(new_crtc_state);
15550 
15551 		intel_pre_plane_update(state, crtc);
15552 
15553 		if (new_crtc_state->update_pipe)
15554 			intel_encoders_update_pipe(state, crtc);
15555 	}
15556 
15557 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15558 		intel_fbc_disable(crtc);
15559 	else
15560 		intel_fbc_enable(state, crtc);
15561 
15562 	/* Perform vblank evasion around commit operation */
15563 	intel_pipe_update_start(new_crtc_state);
15564 
15565 	commit_pipe_config(state, crtc);
15566 
15567 	if (INTEL_GEN(dev_priv) >= 9)
15568 		skl_update_planes_on_crtc(state, crtc);
15569 	else
15570 		i9xx_update_planes_on_crtc(state, crtc);
15571 
15572 	intel_pipe_update_end(new_crtc_state);
15573 
15574 	/*
15575 	 * We usually enable FIFO underrun interrupts as part of the
15576 	 * CRTC enable sequence during modesets.  But when we inherit a
15577 	 * valid pipe configuration from the BIOS we need to take care
15578 	 * of enabling them on the CRTC's first fastset.
15579 	 */
15580 	if (new_crtc_state->update_pipe && !modeset &&
15581 	    old_crtc_state->inherited)
15582 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15583 }
15584 
15585 
15586 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15587 					  struct intel_crtc_state *old_crtc_state,
15588 					  struct intel_crtc_state *new_crtc_state,
15589 					  struct intel_crtc *crtc)
15590 {
15591 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15592 
15593 	intel_crtc_disable_planes(state, crtc);
15594 
15595 	/*
15596 	 * We need to disable pipe CRC before disabling the pipe,
15597 	 * or we race against vblank off.
15598 	 */
15599 	intel_crtc_disable_pipe_crc(crtc);
15600 
15601 	dev_priv->display.crtc_disable(state, crtc);
15602 	crtc->active = false;
15603 	intel_fbc_disable(crtc);
15604 	intel_disable_shared_dpll(old_crtc_state);
15605 
15606 	/* FIXME unify this for all platforms */
15607 	if (!new_crtc_state->hw.active &&
15608 	    !HAS_GMCH(dev_priv) &&
15609 	    dev_priv->display.initial_watermarks)
15610 		dev_priv->display.initial_watermarks(state, crtc);
15611 }
15612 
15613 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15614 {
15615 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15616 	struct intel_crtc *crtc;
15617 	u32 handled = 0;
15618 	int i;
15619 
15620 	/* Only disable port sync and MST slaves */
15621 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15622 					    new_crtc_state, i) {
15623 		if (!needs_modeset(new_crtc_state))
15624 			continue;
15625 
15626 		if (!old_crtc_state->hw.active)
15627 			continue;
15628 
15629 		/* In case of Transcoder port Sync master slave CRTCs can be
15630 		 * assigned in any order and we need to make sure that
15631 		 * slave CRTCs are disabled first and then master CRTC since
15632 		 * Slave vblanks are masked till Master Vblanks.
15633 		 */
15634 		if (!is_trans_port_sync_slave(old_crtc_state) &&
15635 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
15636 			continue;
15637 
15638 		intel_pre_plane_update(state, crtc);
15639 		intel_old_crtc_state_disables(state, old_crtc_state,
15640 					      new_crtc_state, crtc);
15641 		handled |= BIT(crtc->pipe);
15642 	}
15643 
15644 	/* Disable everything else left on */
15645 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15646 					    new_crtc_state, i) {
15647 		if (!needs_modeset(new_crtc_state) ||
15648 		    (handled & BIT(crtc->pipe)))
15649 			continue;
15650 
15651 		intel_pre_plane_update(state, crtc);
15652 		if (old_crtc_state->hw.active)
15653 			intel_old_crtc_state_disables(state, old_crtc_state,
15654 						      new_crtc_state, crtc);
15655 	}
15656 }
15657 
15658 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
15659 {
15660 	struct intel_crtc_state *new_crtc_state;
15661 	struct intel_crtc *crtc;
15662 	int i;
15663 
15664 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15665 		if (!new_crtc_state->hw.active)
15666 			continue;
15667 
15668 		intel_enable_crtc(state, crtc);
15669 		intel_update_crtc(state, crtc);
15670 	}
15671 }
15672 
15673 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
15674 {
15675 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15676 	struct intel_crtc *crtc;
15677 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15678 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
15679 	u8 update_pipes = 0, modeset_pipes = 0;
15680 	int i;
15681 
15682 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15683 		enum pipe pipe = crtc->pipe;
15684 
15685 		if (!new_crtc_state->hw.active)
15686 			continue;
15687 
15688 		/* ignore allocations for crtc's that have been turned off. */
15689 		if (!needs_modeset(new_crtc_state)) {
15690 			entries[pipe] = old_crtc_state->wm.skl.ddb;
15691 			update_pipes |= BIT(pipe);
15692 		} else {
15693 			modeset_pipes |= BIT(pipe);
15694 		}
15695 	}
15696 
15697 	/*
15698 	 * Whenever the number of active pipes changes, we need to make sure we
15699 	 * update the pipes in the right order so that their ddb allocations
15700 	 * never overlap with each other between CRTC updates. Otherwise we'll
15701 	 * cause pipe underruns and other bad stuff.
15702 	 *
15703 	 * So first lets enable all pipes that do not need a fullmodeset as
15704 	 * those don't have any external dependency.
15705 	 */
15706 	while (update_pipes) {
15707 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15708 						    new_crtc_state, i) {
15709 			enum pipe pipe = crtc->pipe;
15710 
15711 			if ((update_pipes & BIT(pipe)) == 0)
15712 				continue;
15713 
15714 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15715 							entries, I915_MAX_PIPES, pipe))
15716 				continue;
15717 
15718 			entries[pipe] = new_crtc_state->wm.skl.ddb;
15719 			update_pipes &= ~BIT(pipe);
15720 
15721 			intel_update_crtc(state, crtc);
15722 
15723 			/*
15724 			 * If this is an already active pipe, it's DDB changed,
15725 			 * and this isn't the last pipe that needs updating
15726 			 * then we need to wait for a vblank to pass for the
15727 			 * new ddb allocation to take effect.
15728 			 */
15729 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
15730 						 &old_crtc_state->wm.skl.ddb) &&
15731 			    (update_pipes | modeset_pipes))
15732 				intel_wait_for_vblank(dev_priv, pipe);
15733 		}
15734 	}
15735 
15736 	update_pipes = modeset_pipes;
15737 
15738 	/*
15739 	 * Enable all pipes that needs a modeset and do not depends on other
15740 	 * pipes
15741 	 */
15742 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15743 		enum pipe pipe = crtc->pipe;
15744 
15745 		if ((modeset_pipes & BIT(pipe)) == 0)
15746 			continue;
15747 
15748 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
15749 		    is_trans_port_sync_master(new_crtc_state))
15750 			continue;
15751 
15752 		modeset_pipes &= ~BIT(pipe);
15753 
15754 		intel_enable_crtc(state, crtc);
15755 	}
15756 
15757 	/*
15758 	 * Then we enable all remaining pipes that depend on other
15759 	 * pipes: MST slaves and port sync masters.
15760 	 */
15761 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15762 		enum pipe pipe = crtc->pipe;
15763 
15764 		if ((modeset_pipes & BIT(pipe)) == 0)
15765 			continue;
15766 
15767 		modeset_pipes &= ~BIT(pipe);
15768 
15769 		intel_enable_crtc(state, crtc);
15770 	}
15771 
15772 	/*
15773 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
15774 	 */
15775 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15776 		enum pipe pipe = crtc->pipe;
15777 
15778 		if ((update_pipes & BIT(pipe)) == 0)
15779 			continue;
15780 
15781 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15782 									entries, I915_MAX_PIPES, pipe));
15783 
15784 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15785 		update_pipes &= ~BIT(pipe);
15786 
15787 		intel_update_crtc(state, crtc);
15788 	}
15789 
15790 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
15791 	drm_WARN_ON(&dev_priv->drm, update_pipes);
15792 }
15793 
15794 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
15795 {
15796 	struct intel_atomic_state *state, *next;
15797 	struct llist_node *freed;
15798 
15799 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
15800 	llist_for_each_entry_safe(state, next, freed, freed)
15801 		drm_atomic_state_put(&state->base);
15802 }
15803 
15804 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
15805 {
15806 	struct drm_i915_private *dev_priv =
15807 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
15808 
15809 	intel_atomic_helper_free_state(dev_priv);
15810 }
15811 
15812 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
15813 {
15814 	struct wait_queue_entry wait_fence, wait_reset;
15815 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
15816 
15817 	init_wait_entry(&wait_fence, 0);
15818 	init_wait_entry(&wait_reset, 0);
15819 	for (;;) {
15820 		prepare_to_wait(&intel_state->commit_ready.wait,
15821 				&wait_fence, TASK_UNINTERRUPTIBLE);
15822 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15823 					      I915_RESET_MODESET),
15824 				&wait_reset, TASK_UNINTERRUPTIBLE);
15825 
15826 
15827 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
15828 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
15829 			break;
15830 
15831 		schedule();
15832 	}
15833 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
15834 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15835 				  I915_RESET_MODESET),
15836 		    &wait_reset);
15837 }
15838 
15839 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
15840 {
15841 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15842 	struct intel_crtc *crtc;
15843 	int i;
15844 
15845 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15846 					    new_crtc_state, i)
15847 		intel_dsb_cleanup(old_crtc_state);
15848 }
15849 
15850 static void intel_atomic_cleanup_work(struct work_struct *work)
15851 {
15852 	struct intel_atomic_state *state =
15853 		container_of(work, struct intel_atomic_state, base.commit_work);
15854 	struct drm_i915_private *i915 = to_i915(state->base.dev);
15855 
15856 	intel_cleanup_dsbs(state);
15857 	drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
15858 	drm_atomic_helper_commit_cleanup_done(&state->base);
15859 	drm_atomic_state_put(&state->base);
15860 
15861 	intel_atomic_helper_free_state(i915);
15862 }
15863 
15864 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
15865 {
15866 	struct drm_device *dev = state->base.dev;
15867 	struct drm_i915_private *dev_priv = to_i915(dev);
15868 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15869 	struct intel_crtc *crtc;
15870 	u64 put_domains[I915_MAX_PIPES] = {};
15871 	intel_wakeref_t wakeref = 0;
15872 	int i;
15873 
15874 	intel_atomic_commit_fence_wait(state);
15875 
15876 	drm_atomic_helper_wait_for_dependencies(&state->base);
15877 
15878 	if (state->modeset)
15879 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
15880 
15881 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15882 					    new_crtc_state, i) {
15883 		if (needs_modeset(new_crtc_state) ||
15884 		    new_crtc_state->update_pipe) {
15885 
15886 			put_domains[crtc->pipe] =
15887 				modeset_get_crtc_power_domains(new_crtc_state);
15888 		}
15889 	}
15890 
15891 	intel_commit_modeset_disables(state);
15892 
15893 	/* FIXME: Eventually get rid of our crtc->config pointer */
15894 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15895 		crtc->config = new_crtc_state;
15896 
15897 	if (state->modeset) {
15898 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
15899 
15900 		intel_set_cdclk_pre_plane_update(state);
15901 
15902 		intel_modeset_verify_disabled(dev_priv, state);
15903 	}
15904 
15905 	intel_sagv_pre_plane_update(state);
15906 
15907 	/* Complete the events for pipes that have now been disabled */
15908 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15909 		bool modeset = needs_modeset(new_crtc_state);
15910 
15911 		/* Complete events for now disable pipes here. */
15912 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
15913 			spin_lock_irq(&dev->event_lock);
15914 			drm_crtc_send_vblank_event(&crtc->base,
15915 						   new_crtc_state->uapi.event);
15916 			spin_unlock_irq(&dev->event_lock);
15917 
15918 			new_crtc_state->uapi.event = NULL;
15919 		}
15920 	}
15921 
15922 	if (state->modeset)
15923 		intel_encoders_update_prepare(state);
15924 
15925 	intel_dbuf_pre_plane_update(state);
15926 
15927 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15928 		if (new_crtc_state->uapi.async_flip)
15929 			skl_enable_flip_done(crtc);
15930 	}
15931 
15932 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
15933 	dev_priv->display.commit_modeset_enables(state);
15934 
15935 	if (state->modeset) {
15936 		intel_encoders_update_complete(state);
15937 
15938 		intel_set_cdclk_post_plane_update(state);
15939 	}
15940 
15941 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
15942 	 * already, but still need the state for the delayed optimization. To
15943 	 * fix this:
15944 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
15945 	 * - schedule that vblank worker _before_ calling hw_done
15946 	 * - at the start of commit_tail, cancel it _synchrously
15947 	 * - switch over to the vblank wait helper in the core after that since
15948 	 *   we don't need out special handling any more.
15949 	 */
15950 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
15951 
15952 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15953 		if (new_crtc_state->uapi.async_flip)
15954 			skl_disable_flip_done(crtc);
15955 
15956 		if (new_crtc_state->hw.active &&
15957 		    !needs_modeset(new_crtc_state) &&
15958 		    !new_crtc_state->preload_luts &&
15959 		    (new_crtc_state->uapi.color_mgmt_changed ||
15960 		     new_crtc_state->update_pipe))
15961 			intel_color_load_luts(new_crtc_state);
15962 	}
15963 
15964 	/*
15965 	 * Now that the vblank has passed, we can go ahead and program the
15966 	 * optimal watermarks on platforms that need two-step watermark
15967 	 * programming.
15968 	 *
15969 	 * TODO: Move this (and other cleanup) to an async worker eventually.
15970 	 */
15971 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15972 					    new_crtc_state, i) {
15973 		/*
15974 		 * Gen2 reports pipe underruns whenever all planes are disabled.
15975 		 * So re-enable underrun reporting after some planes get enabled.
15976 		 *
15977 		 * We do this before .optimize_watermarks() so that we have a
15978 		 * chance of catching underruns with the intermediate watermarks
15979 		 * vs. the new plane configuration.
15980 		 */
15981 		if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
15982 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15983 
15984 		if (dev_priv->display.optimize_watermarks)
15985 			dev_priv->display.optimize_watermarks(state, crtc);
15986 	}
15987 
15988 	intel_dbuf_post_plane_update(state);
15989 
15990 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15991 		intel_post_plane_update(state, crtc);
15992 
15993 		if (put_domains[i])
15994 			modeset_put_power_domains(dev_priv, put_domains[i]);
15995 
15996 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
15997 
15998 		/*
15999 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
16000 		 * cleanup. So copy and reset the dsb structure to sync with
16001 		 * commit_done and later do dsb cleanup in cleanup_work.
16002 		 */
16003 		old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
16004 	}
16005 
16006 	/* Underruns don't always raise interrupts, so check manually */
16007 	intel_check_cpu_fifo_underruns(dev_priv);
16008 	intel_check_pch_fifo_underruns(dev_priv);
16009 
16010 	if (state->modeset)
16011 		intel_verify_planes(state);
16012 
16013 	intel_sagv_post_plane_update(state);
16014 
16015 	drm_atomic_helper_commit_hw_done(&state->base);
16016 
16017 	if (state->modeset) {
16018 		/* As one of the primary mmio accessors, KMS has a high
16019 		 * likelihood of triggering bugs in unclaimed access. After we
16020 		 * finish modesetting, see if an error has been flagged, and if
16021 		 * so enable debugging for the next modeset - and hope we catch
16022 		 * the culprit.
16023 		 */
16024 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
16025 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
16026 	}
16027 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
16028 
16029 	/*
16030 	 * Defer the cleanup of the old state to a separate worker to not
16031 	 * impede the current task (userspace for blocking modesets) that
16032 	 * are executed inline. For out-of-line asynchronous modesets/flips,
16033 	 * deferring to a new worker seems overkill, but we would place a
16034 	 * schedule point (cond_resched()) here anyway to keep latencies
16035 	 * down.
16036 	 */
16037 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
16038 	queue_work(system_highpri_wq, &state->base.commit_work);
16039 }
16040 
16041 static void intel_atomic_commit_work(struct work_struct *work)
16042 {
16043 	struct intel_atomic_state *state =
16044 		container_of(work, struct intel_atomic_state, base.commit_work);
16045 
16046 	intel_atomic_commit_tail(state);
16047 }
16048 
16049 static int __i915_sw_fence_call
16050 intel_atomic_commit_ready(struct i915_sw_fence *fence,
16051 			  enum i915_sw_fence_notify notify)
16052 {
16053 	struct intel_atomic_state *state =
16054 		container_of(fence, struct intel_atomic_state, commit_ready);
16055 
16056 	switch (notify) {
16057 	case FENCE_COMPLETE:
16058 		/* we do blocking waits in the worker, nothing to do here */
16059 		break;
16060 	case FENCE_FREE:
16061 		{
16062 			struct intel_atomic_helper *helper =
16063 				&to_i915(state->base.dev)->atomic_helper;
16064 
16065 			if (llist_add(&state->freed, &helper->free_list))
16066 				schedule_work(&helper->free_work);
16067 			break;
16068 		}
16069 	}
16070 
16071 	return NOTIFY_DONE;
16072 }
16073 
16074 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
16075 {
16076 	struct intel_plane_state *old_plane_state, *new_plane_state;
16077 	struct intel_plane *plane;
16078 	int i;
16079 
16080 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
16081 					     new_plane_state, i)
16082 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16083 					to_intel_frontbuffer(new_plane_state->hw.fb),
16084 					plane->frontbuffer_bit);
16085 }
16086 
16087 static int intel_atomic_commit(struct drm_device *dev,
16088 			       struct drm_atomic_state *_state,
16089 			       bool nonblock)
16090 {
16091 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
16092 	struct drm_i915_private *dev_priv = to_i915(dev);
16093 	int ret = 0;
16094 
16095 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
16096 
16097 	drm_atomic_state_get(&state->base);
16098 	i915_sw_fence_init(&state->commit_ready,
16099 			   intel_atomic_commit_ready);
16100 
16101 	/*
16102 	 * The intel_legacy_cursor_update() fast path takes care
16103 	 * of avoiding the vblank waits for simple cursor
16104 	 * movement and flips. For cursor on/off and size changes,
16105 	 * we want to perform the vblank waits so that watermark
16106 	 * updates happen during the correct frames. Gen9+ have
16107 	 * double buffered watermarks and so shouldn't need this.
16108 	 *
16109 	 * Unset state->legacy_cursor_update before the call to
16110 	 * drm_atomic_helper_setup_commit() because otherwise
16111 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
16112 	 * we get FIFO underruns because we didn't wait
16113 	 * for vblank.
16114 	 *
16115 	 * FIXME doing watermarks and fb cleanup from a vblank worker
16116 	 * (assuming we had any) would solve these problems.
16117 	 */
16118 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
16119 		struct intel_crtc_state *new_crtc_state;
16120 		struct intel_crtc *crtc;
16121 		int i;
16122 
16123 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16124 			if (new_crtc_state->wm.need_postvbl_update ||
16125 			    new_crtc_state->update_wm_post)
16126 				state->base.legacy_cursor_update = false;
16127 	}
16128 
16129 	ret = intel_atomic_prepare_commit(state);
16130 	if (ret) {
16131 		drm_dbg_atomic(&dev_priv->drm,
16132 			       "Preparing state failed with %i\n", ret);
16133 		i915_sw_fence_commit(&state->commit_ready);
16134 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
16135 		return ret;
16136 	}
16137 
16138 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
16139 	if (!ret)
16140 		ret = drm_atomic_helper_swap_state(&state->base, true);
16141 	if (!ret)
16142 		intel_atomic_swap_global_state(state);
16143 
16144 	if (ret) {
16145 		struct intel_crtc_state *new_crtc_state;
16146 		struct intel_crtc *crtc;
16147 		int i;
16148 
16149 		i915_sw_fence_commit(&state->commit_ready);
16150 
16151 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16152 			intel_dsb_cleanup(new_crtc_state);
16153 
16154 		drm_atomic_helper_cleanup_planes(dev, &state->base);
16155 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
16156 		return ret;
16157 	}
16158 	dev_priv->wm.distrust_bios_wm = false;
16159 	intel_shared_dpll_swap_state(state);
16160 	intel_atomic_track_fbs(state);
16161 
16162 	drm_atomic_state_get(&state->base);
16163 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
16164 
16165 	i915_sw_fence_commit(&state->commit_ready);
16166 	if (nonblock && state->modeset) {
16167 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
16168 	} else if (nonblock) {
16169 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
16170 	} else {
16171 		if (state->modeset)
16172 			flush_workqueue(dev_priv->modeset_wq);
16173 		intel_atomic_commit_tail(state);
16174 	}
16175 
16176 	return 0;
16177 }
16178 
16179 struct wait_rps_boost {
16180 	struct wait_queue_entry wait;
16181 
16182 	struct drm_crtc *crtc;
16183 	struct i915_request *request;
16184 };
16185 
16186 static int do_rps_boost(struct wait_queue_entry *_wait,
16187 			unsigned mode, int sync, void *key)
16188 {
16189 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
16190 	struct i915_request *rq = wait->request;
16191 
16192 	/*
16193 	 * If we missed the vblank, but the request is already running it
16194 	 * is reasonable to assume that it will complete before the next
16195 	 * vblank without our intervention, so leave RPS alone.
16196 	 */
16197 	if (!i915_request_started(rq))
16198 		intel_rps_boost(rq);
16199 	i915_request_put(rq);
16200 
16201 	drm_crtc_vblank_put(wait->crtc);
16202 
16203 	list_del(&wait->wait.entry);
16204 	kfree(wait);
16205 	return 1;
16206 }
16207 
16208 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
16209 				       struct dma_fence *fence)
16210 {
16211 	struct wait_rps_boost *wait;
16212 
16213 	if (!dma_fence_is_i915(fence))
16214 		return;
16215 
16216 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
16217 		return;
16218 
16219 	if (drm_crtc_vblank_get(crtc))
16220 		return;
16221 
16222 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
16223 	if (!wait) {
16224 		drm_crtc_vblank_put(crtc);
16225 		return;
16226 	}
16227 
16228 	wait->request = to_request(dma_fence_get(fence));
16229 	wait->crtc = crtc;
16230 
16231 	wait->wait.func = do_rps_boost;
16232 	wait->wait.flags = 0;
16233 
16234 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
16235 }
16236 
16237 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
16238 {
16239 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
16240 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16241 	struct drm_framebuffer *fb = plane_state->hw.fb;
16242 	struct i915_vma *vma;
16243 
16244 	if (plane->id == PLANE_CURSOR &&
16245 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
16246 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
16247 		const int align = intel_cursor_alignment(dev_priv);
16248 		int err;
16249 
16250 		err = i915_gem_object_attach_phys(obj, align);
16251 		if (err)
16252 			return err;
16253 	}
16254 
16255 	vma = intel_pin_and_fence_fb_obj(fb,
16256 					 &plane_state->view,
16257 					 intel_plane_uses_fence(plane_state),
16258 					 &plane_state->flags);
16259 	if (IS_ERR(vma))
16260 		return PTR_ERR(vma);
16261 
16262 	plane_state->vma = vma;
16263 
16264 	return 0;
16265 }
16266 
16267 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
16268 {
16269 	struct i915_vma *vma;
16270 
16271 	vma = fetch_and_zero(&old_plane_state->vma);
16272 	if (vma)
16273 		intel_unpin_fb_vma(vma, old_plane_state->flags);
16274 }
16275 
16276 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
16277 {
16278 	struct i915_sched_attr attr = {
16279 		.priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
16280 	};
16281 
16282 	i915_gem_object_wait_priority(obj, 0, &attr);
16283 }
16284 
16285 /**
16286  * intel_prepare_plane_fb - Prepare fb for usage on plane
16287  * @_plane: drm plane to prepare for
16288  * @_new_plane_state: the plane state being prepared
16289  *
16290  * Prepares a framebuffer for usage on a display plane.  Generally this
16291  * involves pinning the underlying object and updating the frontbuffer tracking
16292  * bits.  Some older platforms need special physical address handling for
16293  * cursor planes.
16294  *
16295  * Returns 0 on success, negative error code on failure.
16296  */
16297 int
16298 intel_prepare_plane_fb(struct drm_plane *_plane,
16299 		       struct drm_plane_state *_new_plane_state)
16300 {
16301 	struct intel_plane *plane = to_intel_plane(_plane);
16302 	struct intel_plane_state *new_plane_state =
16303 		to_intel_plane_state(_new_plane_state);
16304 	struct intel_atomic_state *state =
16305 		to_intel_atomic_state(new_plane_state->uapi.state);
16306 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16307 	const struct intel_plane_state *old_plane_state =
16308 		intel_atomic_get_old_plane_state(state, plane);
16309 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
16310 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
16311 	int ret;
16312 
16313 	if (old_obj) {
16314 		const struct intel_crtc_state *crtc_state =
16315 			intel_atomic_get_new_crtc_state(state,
16316 							to_intel_crtc(old_plane_state->hw.crtc));
16317 
16318 		/* Big Hammer, we also need to ensure that any pending
16319 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
16320 		 * current scanout is retired before unpinning the old
16321 		 * framebuffer. Note that we rely on userspace rendering
16322 		 * into the buffer attached to the pipe they are waiting
16323 		 * on. If not, userspace generates a GPU hang with IPEHR
16324 		 * point to the MI_WAIT_FOR_EVENT.
16325 		 *
16326 		 * This should only fail upon a hung GPU, in which case we
16327 		 * can safely continue.
16328 		 */
16329 		if (needs_modeset(crtc_state)) {
16330 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
16331 							      old_obj->base.resv, NULL,
16332 							      false, 0,
16333 							      GFP_KERNEL);
16334 			if (ret < 0)
16335 				return ret;
16336 		}
16337 	}
16338 
16339 	if (new_plane_state->uapi.fence) { /* explicit fencing */
16340 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
16341 						    new_plane_state->uapi.fence,
16342 						    i915_fence_timeout(dev_priv),
16343 						    GFP_KERNEL);
16344 		if (ret < 0)
16345 			return ret;
16346 	}
16347 
16348 	if (!obj)
16349 		return 0;
16350 
16351 	ret = i915_gem_object_pin_pages(obj);
16352 	if (ret)
16353 		return ret;
16354 
16355 	ret = intel_plane_pin_fb(new_plane_state);
16356 
16357 	i915_gem_object_unpin_pages(obj);
16358 	if (ret)
16359 		return ret;
16360 
16361 	fb_obj_bump_render_priority(obj);
16362 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
16363 
16364 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
16365 		struct dma_fence *fence;
16366 
16367 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
16368 						      obj->base.resv, NULL,
16369 						      false,
16370 						      i915_fence_timeout(dev_priv),
16371 						      GFP_KERNEL);
16372 		if (ret < 0)
16373 			goto unpin_fb;
16374 
16375 		fence = dma_resv_get_excl_rcu(obj->base.resv);
16376 		if (fence) {
16377 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16378 						   fence);
16379 			dma_fence_put(fence);
16380 		}
16381 	} else {
16382 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16383 					   new_plane_state->uapi.fence);
16384 	}
16385 
16386 	/*
16387 	 * We declare pageflips to be interactive and so merit a small bias
16388 	 * towards upclocking to deliver the frame on time. By only changing
16389 	 * the RPS thresholds to sample more regularly and aim for higher
16390 	 * clocks we can hopefully deliver low power workloads (like kodi)
16391 	 * that are not quite steady state without resorting to forcing
16392 	 * maximum clocks following a vblank miss (see do_rps_boost()).
16393 	 */
16394 	if (!state->rps_interactive) {
16395 		intel_rps_mark_interactive(&dev_priv->gt.rps, true);
16396 		state->rps_interactive = true;
16397 	}
16398 
16399 	return 0;
16400 
16401 unpin_fb:
16402 	intel_plane_unpin_fb(new_plane_state);
16403 
16404 	return ret;
16405 }
16406 
16407 /**
16408  * intel_cleanup_plane_fb - Cleans up an fb after plane use
16409  * @plane: drm plane to clean up for
16410  * @_old_plane_state: the state from the previous modeset
16411  *
16412  * Cleans up a framebuffer that has just been removed from a plane.
16413  */
16414 void
16415 intel_cleanup_plane_fb(struct drm_plane *plane,
16416 		       struct drm_plane_state *_old_plane_state)
16417 {
16418 	struct intel_plane_state *old_plane_state =
16419 		to_intel_plane_state(_old_plane_state);
16420 	struct intel_atomic_state *state =
16421 		to_intel_atomic_state(old_plane_state->uapi.state);
16422 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
16423 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16424 
16425 	if (!obj)
16426 		return;
16427 
16428 	if (state->rps_interactive) {
16429 		intel_rps_mark_interactive(&dev_priv->gt.rps, false);
16430 		state->rps_interactive = false;
16431 	}
16432 
16433 	/* Should only be called after a successful intel_prepare_plane_fb()! */
16434 	intel_plane_unpin_fb(old_plane_state);
16435 }
16436 
16437 /**
16438  * intel_plane_destroy - destroy a plane
16439  * @plane: plane to destroy
16440  *
16441  * Common destruction function for all types of planes (primary, cursor,
16442  * sprite).
16443  */
16444 void intel_plane_destroy(struct drm_plane *plane)
16445 {
16446 	drm_plane_cleanup(plane);
16447 	kfree(to_intel_plane(plane));
16448 }
16449 
16450 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16451 					    u32 format, u64 modifier)
16452 {
16453 	switch (modifier) {
16454 	case DRM_FORMAT_MOD_LINEAR:
16455 	case I915_FORMAT_MOD_X_TILED:
16456 		break;
16457 	default:
16458 		return false;
16459 	}
16460 
16461 	switch (format) {
16462 	case DRM_FORMAT_C8:
16463 	case DRM_FORMAT_RGB565:
16464 	case DRM_FORMAT_XRGB1555:
16465 	case DRM_FORMAT_XRGB8888:
16466 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16467 			modifier == I915_FORMAT_MOD_X_TILED;
16468 	default:
16469 		return false;
16470 	}
16471 }
16472 
16473 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16474 					    u32 format, u64 modifier)
16475 {
16476 	switch (modifier) {
16477 	case DRM_FORMAT_MOD_LINEAR:
16478 	case I915_FORMAT_MOD_X_TILED:
16479 		break;
16480 	default:
16481 		return false;
16482 	}
16483 
16484 	switch (format) {
16485 	case DRM_FORMAT_C8:
16486 	case DRM_FORMAT_RGB565:
16487 	case DRM_FORMAT_XRGB8888:
16488 	case DRM_FORMAT_XBGR8888:
16489 	case DRM_FORMAT_ARGB8888:
16490 	case DRM_FORMAT_ABGR8888:
16491 	case DRM_FORMAT_XRGB2101010:
16492 	case DRM_FORMAT_XBGR2101010:
16493 	case DRM_FORMAT_ARGB2101010:
16494 	case DRM_FORMAT_ABGR2101010:
16495 	case DRM_FORMAT_XBGR16161616F:
16496 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16497 			modifier == I915_FORMAT_MOD_X_TILED;
16498 	default:
16499 		return false;
16500 	}
16501 }
16502 
16503 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16504 					      u32 format, u64 modifier)
16505 {
16506 	return modifier == DRM_FORMAT_MOD_LINEAR &&
16507 		format == DRM_FORMAT_ARGB8888;
16508 }
16509 
16510 static const struct drm_plane_funcs i965_plane_funcs = {
16511 	.update_plane = drm_atomic_helper_update_plane,
16512 	.disable_plane = drm_atomic_helper_disable_plane,
16513 	.destroy = intel_plane_destroy,
16514 	.atomic_duplicate_state = intel_plane_duplicate_state,
16515 	.atomic_destroy_state = intel_plane_destroy_state,
16516 	.format_mod_supported = i965_plane_format_mod_supported,
16517 };
16518 
16519 static const struct drm_plane_funcs i8xx_plane_funcs = {
16520 	.update_plane = drm_atomic_helper_update_plane,
16521 	.disable_plane = drm_atomic_helper_disable_plane,
16522 	.destroy = intel_plane_destroy,
16523 	.atomic_duplicate_state = intel_plane_duplicate_state,
16524 	.atomic_destroy_state = intel_plane_destroy_state,
16525 	.format_mod_supported = i8xx_plane_format_mod_supported,
16526 };
16527 
16528 static int
16529 intel_legacy_cursor_update(struct drm_plane *_plane,
16530 			   struct drm_crtc *_crtc,
16531 			   struct drm_framebuffer *fb,
16532 			   int crtc_x, int crtc_y,
16533 			   unsigned int crtc_w, unsigned int crtc_h,
16534 			   u32 src_x, u32 src_y,
16535 			   u32 src_w, u32 src_h,
16536 			   struct drm_modeset_acquire_ctx *ctx)
16537 {
16538 	struct intel_plane *plane = to_intel_plane(_plane);
16539 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
16540 	struct intel_plane_state *old_plane_state =
16541 		to_intel_plane_state(plane->base.state);
16542 	struct intel_plane_state *new_plane_state;
16543 	struct intel_crtc_state *crtc_state =
16544 		to_intel_crtc_state(crtc->base.state);
16545 	struct intel_crtc_state *new_crtc_state;
16546 	int ret;
16547 
16548 	/*
16549 	 * When crtc is inactive or there is a modeset pending,
16550 	 * wait for it to complete in the slowpath
16551 	 */
16552 	if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
16553 	    crtc_state->update_pipe)
16554 		goto slow;
16555 
16556 	/*
16557 	 * Don't do an async update if there is an outstanding commit modifying
16558 	 * the plane.  This prevents our async update's changes from getting
16559 	 * overridden by a previous synchronous update's state.
16560 	 */
16561 	if (old_plane_state->uapi.commit &&
16562 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
16563 		goto slow;
16564 
16565 	/*
16566 	 * If any parameters change that may affect watermarks,
16567 	 * take the slowpath. Only changing fb or position should be
16568 	 * in the fastpath.
16569 	 */
16570 	if (old_plane_state->uapi.crtc != &crtc->base ||
16571 	    old_plane_state->uapi.src_w != src_w ||
16572 	    old_plane_state->uapi.src_h != src_h ||
16573 	    old_plane_state->uapi.crtc_w != crtc_w ||
16574 	    old_plane_state->uapi.crtc_h != crtc_h ||
16575 	    !old_plane_state->uapi.fb != !fb)
16576 		goto slow;
16577 
16578 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
16579 	if (!new_plane_state)
16580 		return -ENOMEM;
16581 
16582 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
16583 	if (!new_crtc_state) {
16584 		ret = -ENOMEM;
16585 		goto out_free;
16586 	}
16587 
16588 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
16589 
16590 	new_plane_state->uapi.src_x = src_x;
16591 	new_plane_state->uapi.src_y = src_y;
16592 	new_plane_state->uapi.src_w = src_w;
16593 	new_plane_state->uapi.src_h = src_h;
16594 	new_plane_state->uapi.crtc_x = crtc_x;
16595 	new_plane_state->uapi.crtc_y = crtc_y;
16596 	new_plane_state->uapi.crtc_w = crtc_w;
16597 	new_plane_state->uapi.crtc_h = crtc_h;
16598 
16599 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state);
16600 
16601 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
16602 						  old_plane_state, new_plane_state);
16603 	if (ret)
16604 		goto out_free;
16605 
16606 	ret = intel_plane_pin_fb(new_plane_state);
16607 	if (ret)
16608 		goto out_free;
16609 
16610 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16611 				ORIGIN_FLIP);
16612 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16613 				to_intel_frontbuffer(new_plane_state->hw.fb),
16614 				plane->frontbuffer_bit);
16615 
16616 	/* Swap plane state */
16617 	plane->base.state = &new_plane_state->uapi;
16618 
16619 	/*
16620 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
16621 	 * page flip that's running simultaneously. If we swap crtc_state and
16622 	 * destroy the old state, we will cause a use-after-free there.
16623 	 *
16624 	 * Only update active_planes, which is needed for our internal
16625 	 * bookkeeping. Either value will do the right thing when updating
16626 	 * planes atomically. If the cursor was part of the atomic update then
16627 	 * we would have taken the slowpath.
16628 	 */
16629 	crtc_state->active_planes = new_crtc_state->active_planes;
16630 
16631 	if (new_plane_state->uapi.visible)
16632 		intel_update_plane(plane, crtc_state, new_plane_state);
16633 	else
16634 		intel_disable_plane(plane, crtc_state);
16635 
16636 	intel_plane_unpin_fb(old_plane_state);
16637 
16638 out_free:
16639 	if (new_crtc_state)
16640 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
16641 	if (ret)
16642 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
16643 	else
16644 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
16645 	return ret;
16646 
16647 slow:
16648 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
16649 					      crtc_x, crtc_y, crtc_w, crtc_h,
16650 					      src_x, src_y, src_w, src_h, ctx);
16651 }
16652 
16653 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
16654 	.update_plane = intel_legacy_cursor_update,
16655 	.disable_plane = drm_atomic_helper_disable_plane,
16656 	.destroy = intel_plane_destroy,
16657 	.atomic_duplicate_state = intel_plane_duplicate_state,
16658 	.atomic_destroy_state = intel_plane_destroy_state,
16659 	.format_mod_supported = intel_cursor_format_mod_supported,
16660 };
16661 
16662 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
16663 			       enum i9xx_plane_id i9xx_plane)
16664 {
16665 	if (!HAS_FBC(dev_priv))
16666 		return false;
16667 
16668 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16669 		return i9xx_plane == PLANE_A; /* tied to pipe A */
16670 	else if (IS_IVYBRIDGE(dev_priv))
16671 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
16672 			i9xx_plane == PLANE_C;
16673 	else if (INTEL_GEN(dev_priv) >= 4)
16674 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
16675 	else
16676 		return i9xx_plane == PLANE_A;
16677 }
16678 
16679 static struct intel_plane *
16680 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
16681 {
16682 	struct intel_plane *plane;
16683 	const struct drm_plane_funcs *plane_funcs;
16684 	unsigned int supported_rotations;
16685 	const u32 *formats;
16686 	int num_formats;
16687 	int ret, zpos;
16688 
16689 	if (INTEL_GEN(dev_priv) >= 9)
16690 		return skl_universal_plane_create(dev_priv, pipe,
16691 						  PLANE_PRIMARY);
16692 
16693 	plane = intel_plane_alloc();
16694 	if (IS_ERR(plane))
16695 		return plane;
16696 
16697 	plane->pipe = pipe;
16698 	/*
16699 	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
16700 	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
16701 	 */
16702 	if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4 &&
16703 	    INTEL_NUM_PIPES(dev_priv) == 2)
16704 		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
16705 	else
16706 		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
16707 	plane->id = PLANE_PRIMARY;
16708 	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
16709 
16710 	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
16711 	if (plane->has_fbc) {
16712 		struct intel_fbc *fbc = &dev_priv->fbc;
16713 
16714 		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
16715 	}
16716 
16717 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16718 		formats = vlv_primary_formats;
16719 		num_formats = ARRAY_SIZE(vlv_primary_formats);
16720 	} else if (INTEL_GEN(dev_priv) >= 4) {
16721 		/*
16722 		 * WaFP16GammaEnabling:ivb
16723 		 * "Workaround : When using the 64-bit format, the plane
16724 		 *  output on each color channel has one quarter amplitude.
16725 		 *  It can be brought up to full amplitude by using pipe
16726 		 *  gamma correction or pipe color space conversion to
16727 		 *  multiply the plane output by four."
16728 		 *
16729 		 * There is no dedicated plane gamma for the primary plane,
16730 		 * and using the pipe gamma/csc could conflict with other
16731 		 * planes, so we choose not to expose fp16 on IVB primary
16732 		 * planes. HSW primary planes no longer have this problem.
16733 		 */
16734 		if (IS_IVYBRIDGE(dev_priv)) {
16735 			formats = ivb_primary_formats;
16736 			num_formats = ARRAY_SIZE(ivb_primary_formats);
16737 		} else {
16738 			formats = i965_primary_formats;
16739 			num_formats = ARRAY_SIZE(i965_primary_formats);
16740 		}
16741 	} else {
16742 		formats = i8xx_primary_formats;
16743 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
16744 	}
16745 
16746 	if (INTEL_GEN(dev_priv) >= 4)
16747 		plane_funcs = &i965_plane_funcs;
16748 	else
16749 		plane_funcs = &i8xx_plane_funcs;
16750 
16751 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16752 		plane->min_cdclk = vlv_plane_min_cdclk;
16753 	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16754 		plane->min_cdclk = hsw_plane_min_cdclk;
16755 	else if (IS_IVYBRIDGE(dev_priv))
16756 		plane->min_cdclk = ivb_plane_min_cdclk;
16757 	else
16758 		plane->min_cdclk = i9xx_plane_min_cdclk;
16759 
16760 	plane->max_stride = i9xx_plane_max_stride;
16761 	plane->update_plane = i9xx_update_plane;
16762 	plane->disable_plane = i9xx_disable_plane;
16763 	plane->get_hw_state = i9xx_plane_get_hw_state;
16764 	plane->check_plane = i9xx_plane_check;
16765 
16766 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16767 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16768 					       0, plane_funcs,
16769 					       formats, num_formats,
16770 					       i9xx_format_modifiers,
16771 					       DRM_PLANE_TYPE_PRIMARY,
16772 					       "primary %c", pipe_name(pipe));
16773 	else
16774 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16775 					       0, plane_funcs,
16776 					       formats, num_formats,
16777 					       i9xx_format_modifiers,
16778 					       DRM_PLANE_TYPE_PRIMARY,
16779 					       "plane %c",
16780 					       plane_name(plane->i9xx_plane));
16781 	if (ret)
16782 		goto fail;
16783 
16784 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
16785 		supported_rotations =
16786 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
16787 			DRM_MODE_REFLECT_X;
16788 	} else if (INTEL_GEN(dev_priv) >= 4) {
16789 		supported_rotations =
16790 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
16791 	} else {
16792 		supported_rotations = DRM_MODE_ROTATE_0;
16793 	}
16794 
16795 	if (INTEL_GEN(dev_priv) >= 4)
16796 		drm_plane_create_rotation_property(&plane->base,
16797 						   DRM_MODE_ROTATE_0,
16798 						   supported_rotations);
16799 
16800 	zpos = 0;
16801 	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
16802 
16803 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
16804 
16805 	return plane;
16806 
16807 fail:
16808 	intel_plane_free(plane);
16809 
16810 	return ERR_PTR(ret);
16811 }
16812 
16813 static struct intel_plane *
16814 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
16815 			  enum pipe pipe)
16816 {
16817 	struct intel_plane *cursor;
16818 	int ret, zpos;
16819 
16820 	cursor = intel_plane_alloc();
16821 	if (IS_ERR(cursor))
16822 		return cursor;
16823 
16824 	cursor->pipe = pipe;
16825 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
16826 	cursor->id = PLANE_CURSOR;
16827 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
16828 
16829 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16830 		cursor->max_stride = i845_cursor_max_stride;
16831 		cursor->update_plane = i845_update_cursor;
16832 		cursor->disable_plane = i845_disable_cursor;
16833 		cursor->get_hw_state = i845_cursor_get_hw_state;
16834 		cursor->check_plane = i845_check_cursor;
16835 	} else {
16836 		cursor->max_stride = i9xx_cursor_max_stride;
16837 		cursor->update_plane = i9xx_update_cursor;
16838 		cursor->disable_plane = i9xx_disable_cursor;
16839 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
16840 		cursor->check_plane = i9xx_check_cursor;
16841 	}
16842 
16843 	cursor->cursor.base = ~0;
16844 	cursor->cursor.cntl = ~0;
16845 
16846 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
16847 		cursor->cursor.size = ~0;
16848 
16849 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
16850 				       0, &intel_cursor_plane_funcs,
16851 				       intel_cursor_formats,
16852 				       ARRAY_SIZE(intel_cursor_formats),
16853 				       cursor_format_modifiers,
16854 				       DRM_PLANE_TYPE_CURSOR,
16855 				       "cursor %c", pipe_name(pipe));
16856 	if (ret)
16857 		goto fail;
16858 
16859 	if (INTEL_GEN(dev_priv) >= 4)
16860 		drm_plane_create_rotation_property(&cursor->base,
16861 						   DRM_MODE_ROTATE_0,
16862 						   DRM_MODE_ROTATE_0 |
16863 						   DRM_MODE_ROTATE_180);
16864 
16865 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
16866 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
16867 
16868 	if (INTEL_GEN(dev_priv) >= 12)
16869 		drm_plane_enable_fb_damage_clips(&cursor->base);
16870 
16871 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
16872 
16873 	return cursor;
16874 
16875 fail:
16876 	intel_plane_free(cursor);
16877 
16878 	return ERR_PTR(ret);
16879 }
16880 
16881 #define INTEL_CRTC_FUNCS \
16882 	.gamma_set = drm_atomic_helper_legacy_gamma_set, \
16883 	.set_config = drm_atomic_helper_set_config, \
16884 	.destroy = intel_crtc_destroy, \
16885 	.page_flip = drm_atomic_helper_page_flip, \
16886 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
16887 	.atomic_destroy_state = intel_crtc_destroy_state, \
16888 	.set_crc_source = intel_crtc_set_crc_source, \
16889 	.verify_crc_source = intel_crtc_verify_crc_source, \
16890 	.get_crc_sources = intel_crtc_get_crc_sources
16891 
16892 static const struct drm_crtc_funcs bdw_crtc_funcs = {
16893 	INTEL_CRTC_FUNCS,
16894 
16895 	.get_vblank_counter = g4x_get_vblank_counter,
16896 	.enable_vblank = bdw_enable_vblank,
16897 	.disable_vblank = bdw_disable_vblank,
16898 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16899 };
16900 
16901 static const struct drm_crtc_funcs ilk_crtc_funcs = {
16902 	INTEL_CRTC_FUNCS,
16903 
16904 	.get_vblank_counter = g4x_get_vblank_counter,
16905 	.enable_vblank = ilk_enable_vblank,
16906 	.disable_vblank = ilk_disable_vblank,
16907 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16908 };
16909 
16910 static const struct drm_crtc_funcs g4x_crtc_funcs = {
16911 	INTEL_CRTC_FUNCS,
16912 
16913 	.get_vblank_counter = g4x_get_vblank_counter,
16914 	.enable_vblank = i965_enable_vblank,
16915 	.disable_vblank = i965_disable_vblank,
16916 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16917 };
16918 
16919 static const struct drm_crtc_funcs i965_crtc_funcs = {
16920 	INTEL_CRTC_FUNCS,
16921 
16922 	.get_vblank_counter = i915_get_vblank_counter,
16923 	.enable_vblank = i965_enable_vblank,
16924 	.disable_vblank = i965_disable_vblank,
16925 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16926 };
16927 
16928 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
16929 	INTEL_CRTC_FUNCS,
16930 
16931 	.get_vblank_counter = i915_get_vblank_counter,
16932 	.enable_vblank = i915gm_enable_vblank,
16933 	.disable_vblank = i915gm_disable_vblank,
16934 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16935 };
16936 
16937 static const struct drm_crtc_funcs i915_crtc_funcs = {
16938 	INTEL_CRTC_FUNCS,
16939 
16940 	.get_vblank_counter = i915_get_vblank_counter,
16941 	.enable_vblank = i8xx_enable_vblank,
16942 	.disable_vblank = i8xx_disable_vblank,
16943 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16944 };
16945 
16946 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
16947 	INTEL_CRTC_FUNCS,
16948 
16949 	/* no hw vblank counter */
16950 	.enable_vblank = i8xx_enable_vblank,
16951 	.disable_vblank = i8xx_disable_vblank,
16952 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16953 };
16954 
16955 static struct intel_crtc *intel_crtc_alloc(void)
16956 {
16957 	struct intel_crtc_state *crtc_state;
16958 	struct intel_crtc *crtc;
16959 
16960 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
16961 	if (!crtc)
16962 		return ERR_PTR(-ENOMEM);
16963 
16964 	crtc_state = intel_crtc_state_alloc(crtc);
16965 	if (!crtc_state) {
16966 		kfree(crtc);
16967 		return ERR_PTR(-ENOMEM);
16968 	}
16969 
16970 	crtc->base.state = &crtc_state->uapi;
16971 	crtc->config = crtc_state;
16972 
16973 	return crtc;
16974 }
16975 
16976 static void intel_crtc_free(struct intel_crtc *crtc)
16977 {
16978 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
16979 	kfree(crtc);
16980 }
16981 
16982 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
16983 {
16984 	struct intel_plane *plane;
16985 
16986 	for_each_intel_plane(&dev_priv->drm, plane) {
16987 		struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
16988 								  plane->pipe);
16989 
16990 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
16991 	}
16992 }
16993 
16994 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
16995 {
16996 	struct intel_plane *primary, *cursor;
16997 	const struct drm_crtc_funcs *funcs;
16998 	struct intel_crtc *crtc;
16999 	int sprite, ret;
17000 
17001 	crtc = intel_crtc_alloc();
17002 	if (IS_ERR(crtc))
17003 		return PTR_ERR(crtc);
17004 
17005 	crtc->pipe = pipe;
17006 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
17007 
17008 	primary = intel_primary_plane_create(dev_priv, pipe);
17009 	if (IS_ERR(primary)) {
17010 		ret = PTR_ERR(primary);
17011 		goto fail;
17012 	}
17013 	crtc->plane_ids_mask |= BIT(primary->id);
17014 
17015 	for_each_sprite(dev_priv, pipe, sprite) {
17016 		struct intel_plane *plane;
17017 
17018 		plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
17019 		if (IS_ERR(plane)) {
17020 			ret = PTR_ERR(plane);
17021 			goto fail;
17022 		}
17023 		crtc->plane_ids_mask |= BIT(plane->id);
17024 	}
17025 
17026 	cursor = intel_cursor_plane_create(dev_priv, pipe);
17027 	if (IS_ERR(cursor)) {
17028 		ret = PTR_ERR(cursor);
17029 		goto fail;
17030 	}
17031 	crtc->plane_ids_mask |= BIT(cursor->id);
17032 
17033 	if (HAS_GMCH(dev_priv)) {
17034 		if (IS_CHERRYVIEW(dev_priv) ||
17035 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
17036 			funcs = &g4x_crtc_funcs;
17037 		else if (IS_GEN(dev_priv, 4))
17038 			funcs = &i965_crtc_funcs;
17039 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
17040 			funcs = &i915gm_crtc_funcs;
17041 		else if (IS_GEN(dev_priv, 3))
17042 			funcs = &i915_crtc_funcs;
17043 		else
17044 			funcs = &i8xx_crtc_funcs;
17045 	} else {
17046 		if (INTEL_GEN(dev_priv) >= 8)
17047 			funcs = &bdw_crtc_funcs;
17048 		else
17049 			funcs = &ilk_crtc_funcs;
17050 	}
17051 
17052 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
17053 					&primary->base, &cursor->base,
17054 					funcs, "pipe %c", pipe_name(pipe));
17055 	if (ret)
17056 		goto fail;
17057 
17058 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
17059 	       dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
17060 	dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
17061 
17062 	if (INTEL_GEN(dev_priv) < 9) {
17063 		enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
17064 
17065 		BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
17066 		       dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
17067 		dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
17068 	}
17069 
17070 	if (INTEL_GEN(dev_priv) >= 10)
17071 		drm_crtc_create_scaling_filter_property(&crtc->base,
17072 						BIT(DRM_SCALING_FILTER_DEFAULT) |
17073 						BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
17074 
17075 	intel_color_init(crtc);
17076 
17077 	intel_crtc_crc_init(crtc);
17078 
17079 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
17080 
17081 	return 0;
17082 
17083 fail:
17084 	intel_crtc_free(crtc);
17085 
17086 	return ret;
17087 }
17088 
17089 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
17090 				      struct drm_file *file)
17091 {
17092 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
17093 	struct drm_crtc *drmmode_crtc;
17094 	struct intel_crtc *crtc;
17095 
17096 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
17097 	if (!drmmode_crtc)
17098 		return -ENOENT;
17099 
17100 	crtc = to_intel_crtc(drmmode_crtc);
17101 	pipe_from_crtc_id->pipe = crtc->pipe;
17102 
17103 	return 0;
17104 }
17105 
17106 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
17107 {
17108 	struct drm_device *dev = encoder->base.dev;
17109 	struct intel_encoder *source_encoder;
17110 	u32 possible_clones = 0;
17111 
17112 	for_each_intel_encoder(dev, source_encoder) {
17113 		if (encoders_cloneable(encoder, source_encoder))
17114 			possible_clones |= drm_encoder_mask(&source_encoder->base);
17115 	}
17116 
17117 	return possible_clones;
17118 }
17119 
17120 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
17121 {
17122 	struct drm_device *dev = encoder->base.dev;
17123 	struct intel_crtc *crtc;
17124 	u32 possible_crtcs = 0;
17125 
17126 	for_each_intel_crtc(dev, crtc) {
17127 		if (encoder->pipe_mask & BIT(crtc->pipe))
17128 			possible_crtcs |= drm_crtc_mask(&crtc->base);
17129 	}
17130 
17131 	return possible_crtcs;
17132 }
17133 
17134 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
17135 {
17136 	if (!IS_MOBILE(dev_priv))
17137 		return false;
17138 
17139 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
17140 		return false;
17141 
17142 	if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
17143 		return false;
17144 
17145 	return true;
17146 }
17147 
17148 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
17149 {
17150 	if (INTEL_GEN(dev_priv) >= 9)
17151 		return false;
17152 
17153 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
17154 		return false;
17155 
17156 	if (HAS_PCH_LPT_H(dev_priv) &&
17157 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
17158 		return false;
17159 
17160 	/* DDI E can't be used if DDI A requires 4 lanes */
17161 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
17162 		return false;
17163 
17164 	if (!dev_priv->vbt.int_crt_support)
17165 		return false;
17166 
17167 	return true;
17168 }
17169 
17170 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
17171 {
17172 	int pps_num;
17173 	int pps_idx;
17174 
17175 	if (HAS_DDI(dev_priv))
17176 		return;
17177 	/*
17178 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
17179 	 * everywhere where registers can be write protected.
17180 	 */
17181 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17182 		pps_num = 2;
17183 	else
17184 		pps_num = 1;
17185 
17186 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
17187 		u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
17188 
17189 		val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
17190 		intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
17191 	}
17192 }
17193 
17194 static void intel_pps_init(struct drm_i915_private *dev_priv)
17195 {
17196 	if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
17197 		dev_priv->pps_mmio_base = PCH_PPS_BASE;
17198 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17199 		dev_priv->pps_mmio_base = VLV_PPS_BASE;
17200 	else
17201 		dev_priv->pps_mmio_base = PPS_BASE;
17202 
17203 	intel_pps_unlock_regs_wa(dev_priv);
17204 }
17205 
17206 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
17207 {
17208 	struct intel_encoder *encoder;
17209 	bool dpd_is_edp = false;
17210 
17211 	intel_pps_init(dev_priv);
17212 
17213 	if (!HAS_DISPLAY(dev_priv))
17214 		return;
17215 
17216 	if (IS_ROCKETLAKE(dev_priv)) {
17217 		intel_ddi_init(dev_priv, PORT_A);
17218 		intel_ddi_init(dev_priv, PORT_B);
17219 		intel_ddi_init(dev_priv, PORT_TC1);
17220 		intel_ddi_init(dev_priv, PORT_TC2);
17221 	} else if (INTEL_GEN(dev_priv) >= 12) {
17222 		intel_ddi_init(dev_priv, PORT_A);
17223 		intel_ddi_init(dev_priv, PORT_B);
17224 		intel_ddi_init(dev_priv, PORT_TC1);
17225 		intel_ddi_init(dev_priv, PORT_TC2);
17226 		intel_ddi_init(dev_priv, PORT_TC2);
17227 		intel_ddi_init(dev_priv, PORT_TC4);
17228 		intel_ddi_init(dev_priv, PORT_TC5);
17229 		intel_ddi_init(dev_priv, PORT_TC6);
17230 		icl_dsi_init(dev_priv);
17231 	} else if (IS_JSL_EHL(dev_priv)) {
17232 		intel_ddi_init(dev_priv, PORT_A);
17233 		intel_ddi_init(dev_priv, PORT_B);
17234 		intel_ddi_init(dev_priv, PORT_C);
17235 		intel_ddi_init(dev_priv, PORT_D);
17236 		icl_dsi_init(dev_priv);
17237 	} else if (IS_GEN(dev_priv, 11)) {
17238 		intel_ddi_init(dev_priv, PORT_A);
17239 		intel_ddi_init(dev_priv, PORT_B);
17240 		intel_ddi_init(dev_priv, PORT_C);
17241 		intel_ddi_init(dev_priv, PORT_D);
17242 		intel_ddi_init(dev_priv, PORT_E);
17243 		/*
17244 		 * On some ICL SKUs port F is not present. No strap bits for
17245 		 * this, so rely on VBT.
17246 		 * Work around broken VBTs on SKUs known to have no port F.
17247 		 */
17248 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
17249 		    intel_bios_is_port_present(dev_priv, PORT_F))
17250 			intel_ddi_init(dev_priv, PORT_F);
17251 
17252 		icl_dsi_init(dev_priv);
17253 	} else if (IS_GEN9_LP(dev_priv)) {
17254 		/*
17255 		 * FIXME: Broxton doesn't support port detection via the
17256 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
17257 		 * detect the ports.
17258 		 */
17259 		intel_ddi_init(dev_priv, PORT_A);
17260 		intel_ddi_init(dev_priv, PORT_B);
17261 		intel_ddi_init(dev_priv, PORT_C);
17262 
17263 		vlv_dsi_init(dev_priv);
17264 	} else if (HAS_DDI(dev_priv)) {
17265 		int found;
17266 
17267 		if (intel_ddi_crt_present(dev_priv))
17268 			intel_crt_init(dev_priv);
17269 
17270 		/*
17271 		 * Haswell uses DDI functions to detect digital outputs.
17272 		 * On SKL pre-D0 the strap isn't connected, so we assume
17273 		 * it's there.
17274 		 */
17275 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
17276 		/* WaIgnoreDDIAStrap: skl */
17277 		if (found || IS_GEN9_BC(dev_priv))
17278 			intel_ddi_init(dev_priv, PORT_A);
17279 
17280 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
17281 		 * register */
17282 		found = intel_de_read(dev_priv, SFUSE_STRAP);
17283 
17284 		if (found & SFUSE_STRAP_DDIB_DETECTED)
17285 			intel_ddi_init(dev_priv, PORT_B);
17286 		if (found & SFUSE_STRAP_DDIC_DETECTED)
17287 			intel_ddi_init(dev_priv, PORT_C);
17288 		if (found & SFUSE_STRAP_DDID_DETECTED)
17289 			intel_ddi_init(dev_priv, PORT_D);
17290 		if (found & SFUSE_STRAP_DDIF_DETECTED)
17291 			intel_ddi_init(dev_priv, PORT_F);
17292 		/*
17293 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
17294 		 */
17295 		if (IS_GEN9_BC(dev_priv) &&
17296 		    intel_bios_is_port_present(dev_priv, PORT_E))
17297 			intel_ddi_init(dev_priv, PORT_E);
17298 
17299 	} else if (HAS_PCH_SPLIT(dev_priv)) {
17300 		int found;
17301 
17302 		/*
17303 		 * intel_edp_init_connector() depends on this completing first,
17304 		 * to prevent the registration of both eDP and LVDS and the
17305 		 * incorrect sharing of the PPS.
17306 		 */
17307 		intel_lvds_init(dev_priv);
17308 		intel_crt_init(dev_priv);
17309 
17310 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
17311 
17312 		if (ilk_has_edp_a(dev_priv))
17313 			intel_dp_init(dev_priv, DP_A, PORT_A);
17314 
17315 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
17316 			/* PCH SDVOB multiplex with HDMIB */
17317 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
17318 			if (!found)
17319 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
17320 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
17321 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
17322 		}
17323 
17324 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
17325 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
17326 
17327 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
17328 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
17329 
17330 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
17331 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
17332 
17333 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
17334 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
17335 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
17336 		bool has_edp, has_port;
17337 
17338 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
17339 			intel_crt_init(dev_priv);
17340 
17341 		/*
17342 		 * The DP_DETECTED bit is the latched state of the DDC
17343 		 * SDA pin at boot. However since eDP doesn't require DDC
17344 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
17345 		 * eDP ports may have been muxed to an alternate function.
17346 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
17347 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
17348 		 * detect eDP ports.
17349 		 *
17350 		 * Sadly the straps seem to be missing sometimes even for HDMI
17351 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
17352 		 * and VBT for the presence of the port. Additionally we can't
17353 		 * trust the port type the VBT declares as we've seen at least
17354 		 * HDMI ports that the VBT claim are DP or eDP.
17355 		 */
17356 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
17357 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
17358 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
17359 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
17360 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
17361 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
17362 
17363 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
17364 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
17365 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
17366 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
17367 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
17368 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
17369 
17370 		if (IS_CHERRYVIEW(dev_priv)) {
17371 			/*
17372 			 * eDP not supported on port D,
17373 			 * so no need to worry about it
17374 			 */
17375 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
17376 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
17377 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
17378 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
17379 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
17380 		}
17381 
17382 		vlv_dsi_init(dev_priv);
17383 	} else if (IS_PINEVIEW(dev_priv)) {
17384 		intel_lvds_init(dev_priv);
17385 		intel_crt_init(dev_priv);
17386 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
17387 		bool found = false;
17388 
17389 		if (IS_MOBILE(dev_priv))
17390 			intel_lvds_init(dev_priv);
17391 
17392 		intel_crt_init(dev_priv);
17393 
17394 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17395 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
17396 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
17397 			if (!found && IS_G4X(dev_priv)) {
17398 				drm_dbg_kms(&dev_priv->drm,
17399 					    "probing HDMI on SDVOB\n");
17400 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
17401 			}
17402 
17403 			if (!found && IS_G4X(dev_priv))
17404 				intel_dp_init(dev_priv, DP_B, PORT_B);
17405 		}
17406 
17407 		/* Before G4X SDVOC doesn't have its own detect register */
17408 
17409 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17410 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
17411 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
17412 		}
17413 
17414 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
17415 
17416 			if (IS_G4X(dev_priv)) {
17417 				drm_dbg_kms(&dev_priv->drm,
17418 					    "probing HDMI on SDVOC\n");
17419 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
17420 			}
17421 			if (IS_G4X(dev_priv))
17422 				intel_dp_init(dev_priv, DP_C, PORT_C);
17423 		}
17424 
17425 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
17426 			intel_dp_init(dev_priv, DP_D, PORT_D);
17427 
17428 		if (SUPPORTS_TV(dev_priv))
17429 			intel_tv_init(dev_priv);
17430 	} else if (IS_GEN(dev_priv, 2)) {
17431 		if (IS_I85X(dev_priv))
17432 			intel_lvds_init(dev_priv);
17433 
17434 		intel_crt_init(dev_priv);
17435 		intel_dvo_init(dev_priv);
17436 	}
17437 
17438 	intel_psr_init(dev_priv);
17439 
17440 	for_each_intel_encoder(&dev_priv->drm, encoder) {
17441 		encoder->base.possible_crtcs =
17442 			intel_encoder_possible_crtcs(encoder);
17443 		encoder->base.possible_clones =
17444 			intel_encoder_possible_clones(encoder);
17445 	}
17446 
17447 	intel_init_pch_refclk(dev_priv);
17448 
17449 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
17450 }
17451 
17452 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17453 {
17454 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
17455 
17456 	drm_framebuffer_cleanup(fb);
17457 	intel_frontbuffer_put(intel_fb->frontbuffer);
17458 
17459 	kfree(intel_fb);
17460 }
17461 
17462 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
17463 						struct drm_file *file,
17464 						unsigned int *handle)
17465 {
17466 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17467 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
17468 
17469 	if (obj->userptr.mm) {
17470 		drm_dbg(&i915->drm,
17471 			"attempting to use a userptr for a framebuffer, denied\n");
17472 		return -EINVAL;
17473 	}
17474 
17475 	return drm_gem_handle_create(file, &obj->base, handle);
17476 }
17477 
17478 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17479 					struct drm_file *file,
17480 					unsigned flags, unsigned color,
17481 					struct drm_clip_rect *clips,
17482 					unsigned num_clips)
17483 {
17484 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17485 
17486 	i915_gem_object_flush_if_display(obj);
17487 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
17488 
17489 	return 0;
17490 }
17491 
17492 static const struct drm_framebuffer_funcs intel_fb_funcs = {
17493 	.destroy = intel_user_framebuffer_destroy,
17494 	.create_handle = intel_user_framebuffer_create_handle,
17495 	.dirty = intel_user_framebuffer_dirty,
17496 };
17497 
17498 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17499 				  struct drm_i915_gem_object *obj,
17500 				  struct drm_mode_fb_cmd2 *mode_cmd)
17501 {
17502 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
17503 	struct drm_framebuffer *fb = &intel_fb->base;
17504 	u32 max_stride;
17505 	unsigned int tiling, stride;
17506 	int ret = -EINVAL;
17507 	int i;
17508 
17509 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17510 	if (!intel_fb->frontbuffer)
17511 		return -ENOMEM;
17512 
17513 	i915_gem_object_lock(obj, NULL);
17514 	tiling = i915_gem_object_get_tiling(obj);
17515 	stride = i915_gem_object_get_stride(obj);
17516 	i915_gem_object_unlock(obj);
17517 
17518 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
17519 		/*
17520 		 * If there's a fence, enforce that
17521 		 * the fb modifier and tiling mode match.
17522 		 */
17523 		if (tiling != I915_TILING_NONE &&
17524 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17525 			drm_dbg_kms(&dev_priv->drm,
17526 				    "tiling_mode doesn't match fb modifier\n");
17527 			goto err;
17528 		}
17529 	} else {
17530 		if (tiling == I915_TILING_X) {
17531 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
17532 		} else if (tiling == I915_TILING_Y) {
17533 			drm_dbg_kms(&dev_priv->drm,
17534 				    "No Y tiling for legacy addfb\n");
17535 			goto err;
17536 		}
17537 	}
17538 
17539 	if (!drm_any_plane_has_format(&dev_priv->drm,
17540 				      mode_cmd->pixel_format,
17541 				      mode_cmd->modifier[0])) {
17542 		struct drm_format_name_buf format_name;
17543 
17544 		drm_dbg_kms(&dev_priv->drm,
17545 			    "unsupported pixel format %s / modifier 0x%llx\n",
17546 			    drm_get_format_name(mode_cmd->pixel_format,
17547 						&format_name),
17548 			    mode_cmd->modifier[0]);
17549 		goto err;
17550 	}
17551 
17552 	/*
17553 	 * gen2/3 display engine uses the fence if present,
17554 	 * so the tiling mode must match the fb modifier exactly.
17555 	 */
17556 	if (INTEL_GEN(dev_priv) < 4 &&
17557 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17558 		drm_dbg_kms(&dev_priv->drm,
17559 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
17560 		goto err;
17561 	}
17562 
17563 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17564 					 mode_cmd->modifier[0]);
17565 	if (mode_cmd->pitches[0] > max_stride) {
17566 		drm_dbg_kms(&dev_priv->drm,
17567 			    "%s pitch (%u) must be at most %d\n",
17568 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17569 			    "tiled" : "linear",
17570 			    mode_cmd->pitches[0], max_stride);
17571 		goto err;
17572 	}
17573 
17574 	/*
17575 	 * If there's a fence, enforce that
17576 	 * the fb pitch and fence stride match.
17577 	 */
17578 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
17579 		drm_dbg_kms(&dev_priv->drm,
17580 			    "pitch (%d) must match tiling stride (%d)\n",
17581 			    mode_cmd->pitches[0], stride);
17582 		goto err;
17583 	}
17584 
17585 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
17586 	if (mode_cmd->offsets[0] != 0) {
17587 		drm_dbg_kms(&dev_priv->drm,
17588 			    "plane 0 offset (0x%08x) must be 0\n",
17589 			    mode_cmd->offsets[0]);
17590 		goto err;
17591 	}
17592 
17593 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
17594 
17595 	for (i = 0; i < fb->format->num_planes; i++) {
17596 		u32 stride_alignment;
17597 
17598 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
17599 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17600 				    i);
17601 			goto err;
17602 		}
17603 
17604 		stride_alignment = intel_fb_stride_alignment(fb, i);
17605 		if (fb->pitches[i] & (stride_alignment - 1)) {
17606 			drm_dbg_kms(&dev_priv->drm,
17607 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
17608 				    i, fb->pitches[i], stride_alignment);
17609 			goto err;
17610 		}
17611 
17612 		if (is_gen12_ccs_plane(fb, i)) {
17613 			int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17614 
17615 			if (fb->pitches[i] != ccs_aux_stride) {
17616 				drm_dbg_kms(&dev_priv->drm,
17617 					    "ccs aux plane %d pitch (%d) must be %d\n",
17618 					    i,
17619 					    fb->pitches[i], ccs_aux_stride);
17620 				goto err;
17621 			}
17622 		}
17623 
17624 		fb->obj[i] = &obj->base;
17625 	}
17626 
17627 	ret = intel_fill_fb_info(dev_priv, fb);
17628 	if (ret)
17629 		goto err;
17630 
17631 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
17632 	if (ret) {
17633 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
17634 		goto err;
17635 	}
17636 
17637 	return 0;
17638 
17639 err:
17640 	intel_frontbuffer_put(intel_fb->frontbuffer);
17641 	return ret;
17642 }
17643 
17644 static struct drm_framebuffer *
17645 intel_user_framebuffer_create(struct drm_device *dev,
17646 			      struct drm_file *filp,
17647 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
17648 {
17649 	struct drm_framebuffer *fb;
17650 	struct drm_i915_gem_object *obj;
17651 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
17652 
17653 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
17654 	if (!obj)
17655 		return ERR_PTR(-ENOENT);
17656 
17657 	fb = intel_framebuffer_create(obj, &mode_cmd);
17658 	i915_gem_object_put(obj);
17659 
17660 	return fb;
17661 }
17662 
17663 static enum drm_mode_status
17664 intel_mode_valid(struct drm_device *dev,
17665 		 const struct drm_display_mode *mode)
17666 {
17667 	struct drm_i915_private *dev_priv = to_i915(dev);
17668 	int hdisplay_max, htotal_max;
17669 	int vdisplay_max, vtotal_max;
17670 
17671 	/*
17672 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
17673 	 * of DBLSCAN modes to the output's mode list when they detect
17674 	 * the scaling mode property on the connector. And they don't
17675 	 * ask the kernel to validate those modes in any way until
17676 	 * modeset time at which point the client gets a protocol error.
17677 	 * So in order to not upset those clients we silently ignore the
17678 	 * DBLSCAN flag on such connectors. For other connectors we will
17679 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
17680 	 * And we always reject DBLSCAN modes in connector->mode_valid()
17681 	 * as we never want such modes on the connector's mode list.
17682 	 */
17683 
17684 	if (mode->vscan > 1)
17685 		return MODE_NO_VSCAN;
17686 
17687 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
17688 		return MODE_H_ILLEGAL;
17689 
17690 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
17691 			   DRM_MODE_FLAG_NCSYNC |
17692 			   DRM_MODE_FLAG_PCSYNC))
17693 		return MODE_HSYNC;
17694 
17695 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
17696 			   DRM_MODE_FLAG_PIXMUX |
17697 			   DRM_MODE_FLAG_CLKDIV2))
17698 		return MODE_BAD;
17699 
17700 	/* Transcoder timing limits */
17701 	if (INTEL_GEN(dev_priv) >= 11) {
17702 		hdisplay_max = 16384;
17703 		vdisplay_max = 8192;
17704 		htotal_max = 16384;
17705 		vtotal_max = 8192;
17706 	} else if (INTEL_GEN(dev_priv) >= 9 ||
17707 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17708 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
17709 		vdisplay_max = 4096;
17710 		htotal_max = 8192;
17711 		vtotal_max = 8192;
17712 	} else if (INTEL_GEN(dev_priv) >= 3) {
17713 		hdisplay_max = 4096;
17714 		vdisplay_max = 4096;
17715 		htotal_max = 8192;
17716 		vtotal_max = 8192;
17717 	} else {
17718 		hdisplay_max = 2048;
17719 		vdisplay_max = 2048;
17720 		htotal_max = 4096;
17721 		vtotal_max = 4096;
17722 	}
17723 
17724 	if (mode->hdisplay > hdisplay_max ||
17725 	    mode->hsync_start > htotal_max ||
17726 	    mode->hsync_end > htotal_max ||
17727 	    mode->htotal > htotal_max)
17728 		return MODE_H_ILLEGAL;
17729 
17730 	if (mode->vdisplay > vdisplay_max ||
17731 	    mode->vsync_start > vtotal_max ||
17732 	    mode->vsync_end > vtotal_max ||
17733 	    mode->vtotal > vtotal_max)
17734 		return MODE_V_ILLEGAL;
17735 
17736 	if (INTEL_GEN(dev_priv) >= 5) {
17737 		if (mode->hdisplay < 64 ||
17738 		    mode->htotal - mode->hdisplay < 32)
17739 			return MODE_H_ILLEGAL;
17740 
17741 		if (mode->vtotal - mode->vdisplay < 5)
17742 			return MODE_V_ILLEGAL;
17743 	} else {
17744 		if (mode->htotal - mode->hdisplay < 32)
17745 			return MODE_H_ILLEGAL;
17746 
17747 		if (mode->vtotal - mode->vdisplay < 3)
17748 			return MODE_V_ILLEGAL;
17749 	}
17750 
17751 	return MODE_OK;
17752 }
17753 
17754 enum drm_mode_status
17755 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
17756 				const struct drm_display_mode *mode)
17757 {
17758 	int plane_width_max, plane_height_max;
17759 
17760 	/*
17761 	 * intel_mode_valid() should be
17762 	 * sufficient on older platforms.
17763 	 */
17764 	if (INTEL_GEN(dev_priv) < 9)
17765 		return MODE_OK;
17766 
17767 	/*
17768 	 * Most people will probably want a fullscreen
17769 	 * plane so let's not advertize modes that are
17770 	 * too big for that.
17771 	 */
17772 	if (INTEL_GEN(dev_priv) >= 11) {
17773 		plane_width_max = 5120;
17774 		plane_height_max = 4320;
17775 	} else {
17776 		plane_width_max = 5120;
17777 		plane_height_max = 4096;
17778 	}
17779 
17780 	if (mode->hdisplay > plane_width_max)
17781 		return MODE_H_ILLEGAL;
17782 
17783 	if (mode->vdisplay > plane_height_max)
17784 		return MODE_V_ILLEGAL;
17785 
17786 	return MODE_OK;
17787 }
17788 
17789 static const struct drm_mode_config_funcs intel_mode_funcs = {
17790 	.fb_create = intel_user_framebuffer_create,
17791 	.get_format_info = intel_get_format_info,
17792 	.output_poll_changed = intel_fbdev_output_poll_changed,
17793 	.mode_valid = intel_mode_valid,
17794 	.atomic_check = intel_atomic_check,
17795 	.atomic_commit = intel_atomic_commit,
17796 	.atomic_state_alloc = intel_atomic_state_alloc,
17797 	.atomic_state_clear = intel_atomic_state_clear,
17798 	.atomic_state_free = intel_atomic_state_free,
17799 };
17800 
17801 /**
17802  * intel_init_display_hooks - initialize the display modesetting hooks
17803  * @dev_priv: device private
17804  */
17805 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
17806 {
17807 	intel_init_cdclk_hooks(dev_priv);
17808 
17809 	if (INTEL_GEN(dev_priv) >= 9) {
17810 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17811 		dev_priv->display.get_initial_plane_config =
17812 			skl_get_initial_plane_config;
17813 		dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
17814 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17815 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17816 	} else if (HAS_DDI(dev_priv)) {
17817 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17818 		dev_priv->display.get_initial_plane_config =
17819 			i9xx_get_initial_plane_config;
17820 		dev_priv->display.crtc_compute_clock =
17821 			hsw_crtc_compute_clock;
17822 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17823 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17824 	} else if (HAS_PCH_SPLIT(dev_priv)) {
17825 		dev_priv->display.get_pipe_config = ilk_get_pipe_config;
17826 		dev_priv->display.get_initial_plane_config =
17827 			i9xx_get_initial_plane_config;
17828 		dev_priv->display.crtc_compute_clock =
17829 			ilk_crtc_compute_clock;
17830 		dev_priv->display.crtc_enable = ilk_crtc_enable;
17831 		dev_priv->display.crtc_disable = ilk_crtc_disable;
17832 	} else if (IS_CHERRYVIEW(dev_priv)) {
17833 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17834 		dev_priv->display.get_initial_plane_config =
17835 			i9xx_get_initial_plane_config;
17836 		dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
17837 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17838 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17839 	} else if (IS_VALLEYVIEW(dev_priv)) {
17840 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17841 		dev_priv->display.get_initial_plane_config =
17842 			i9xx_get_initial_plane_config;
17843 		dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
17844 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17845 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17846 	} else if (IS_G4X(dev_priv)) {
17847 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17848 		dev_priv->display.get_initial_plane_config =
17849 			i9xx_get_initial_plane_config;
17850 		dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
17851 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17852 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17853 	} else if (IS_PINEVIEW(dev_priv)) {
17854 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17855 		dev_priv->display.get_initial_plane_config =
17856 			i9xx_get_initial_plane_config;
17857 		dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
17858 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17859 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17860 	} else if (!IS_GEN(dev_priv, 2)) {
17861 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17862 		dev_priv->display.get_initial_plane_config =
17863 			i9xx_get_initial_plane_config;
17864 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
17865 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17866 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17867 	} else {
17868 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17869 		dev_priv->display.get_initial_plane_config =
17870 			i9xx_get_initial_plane_config;
17871 		dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
17872 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17873 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17874 	}
17875 
17876 	if (IS_GEN(dev_priv, 5)) {
17877 		dev_priv->display.fdi_link_train = ilk_fdi_link_train;
17878 	} else if (IS_GEN(dev_priv, 6)) {
17879 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
17880 	} else if (IS_IVYBRIDGE(dev_priv)) {
17881 		/* FIXME: detect B0+ stepping and use auto training */
17882 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
17883 	}
17884 
17885 	if (INTEL_GEN(dev_priv) >= 9)
17886 		dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
17887 	else
17888 		dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
17889 
17890 }
17891 
17892 void intel_modeset_init_hw(struct drm_i915_private *i915)
17893 {
17894 	struct intel_cdclk_state *cdclk_state =
17895 		to_intel_cdclk_state(i915->cdclk.obj.state);
17896 	struct intel_dbuf_state *dbuf_state =
17897 		to_intel_dbuf_state(i915->dbuf.obj.state);
17898 
17899 	intel_update_cdclk(i915);
17900 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
17901 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
17902 
17903 	dbuf_state->enabled_slices = i915->dbuf.enabled_slices;
17904 }
17905 
17906 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
17907 {
17908 	struct drm_plane *plane;
17909 	struct intel_crtc *crtc;
17910 
17911 	for_each_intel_crtc(state->dev, crtc) {
17912 		struct intel_crtc_state *crtc_state;
17913 
17914 		crtc_state = intel_atomic_get_crtc_state(state, crtc);
17915 		if (IS_ERR(crtc_state))
17916 			return PTR_ERR(crtc_state);
17917 
17918 		if (crtc_state->hw.active) {
17919 			/*
17920 			 * Preserve the inherited flag to avoid
17921 			 * taking the full modeset path.
17922 			 */
17923 			crtc_state->inherited = true;
17924 		}
17925 	}
17926 
17927 	drm_for_each_plane(plane, state->dev) {
17928 		struct drm_plane_state *plane_state;
17929 
17930 		plane_state = drm_atomic_get_plane_state(state, plane);
17931 		if (IS_ERR(plane_state))
17932 			return PTR_ERR(plane_state);
17933 	}
17934 
17935 	return 0;
17936 }
17937 
17938 /*
17939  * Calculate what we think the watermarks should be for the state we've read
17940  * out of the hardware and then immediately program those watermarks so that
17941  * we ensure the hardware settings match our internal state.
17942  *
17943  * We can calculate what we think WM's should be by creating a duplicate of the
17944  * current state (which was constructed during hardware readout) and running it
17945  * through the atomic check code to calculate new watermark values in the
17946  * state object.
17947  */
17948 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
17949 {
17950 	struct drm_atomic_state *state;
17951 	struct intel_atomic_state *intel_state;
17952 	struct intel_crtc *crtc;
17953 	struct intel_crtc_state *crtc_state;
17954 	struct drm_modeset_acquire_ctx ctx;
17955 	int ret;
17956 	int i;
17957 
17958 	/* Only supported on platforms that use atomic watermark design */
17959 	if (!dev_priv->display.optimize_watermarks)
17960 		return;
17961 
17962 	state = drm_atomic_state_alloc(&dev_priv->drm);
17963 	if (drm_WARN_ON(&dev_priv->drm, !state))
17964 		return;
17965 
17966 	intel_state = to_intel_atomic_state(state);
17967 
17968 	drm_modeset_acquire_init(&ctx, 0);
17969 
17970 retry:
17971 	state->acquire_ctx = &ctx;
17972 
17973 	/*
17974 	 * Hardware readout is the only time we don't want to calculate
17975 	 * intermediate watermarks (since we don't trust the current
17976 	 * watermarks).
17977 	 */
17978 	if (!HAS_GMCH(dev_priv))
17979 		intel_state->skip_intermediate_wm = true;
17980 
17981 	ret = sanitize_watermarks_add_affected(state);
17982 	if (ret)
17983 		goto fail;
17984 
17985 	ret = intel_atomic_check(&dev_priv->drm, state);
17986 	if (ret)
17987 		goto fail;
17988 
17989 	/* Write calculated watermark values back */
17990 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
17991 		crtc_state->wm.need_postvbl_update = true;
17992 		dev_priv->display.optimize_watermarks(intel_state, crtc);
17993 
17994 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
17995 	}
17996 
17997 fail:
17998 	if (ret == -EDEADLK) {
17999 		drm_atomic_state_clear(state);
18000 		drm_modeset_backoff(&ctx);
18001 		goto retry;
18002 	}
18003 
18004 	/*
18005 	 * If we fail here, it means that the hardware appears to be
18006 	 * programmed in a way that shouldn't be possible, given our
18007 	 * understanding of watermark requirements.  This might mean a
18008 	 * mistake in the hardware readout code or a mistake in the
18009 	 * watermark calculations for a given platform.  Raise a WARN
18010 	 * so that this is noticeable.
18011 	 *
18012 	 * If this actually happens, we'll have to just leave the
18013 	 * BIOS-programmed watermarks untouched and hope for the best.
18014 	 */
18015 	drm_WARN(&dev_priv->drm, ret,
18016 		 "Could not determine valid watermarks for inherited state\n");
18017 
18018 	drm_atomic_state_put(state);
18019 
18020 	drm_modeset_drop_locks(&ctx);
18021 	drm_modeset_acquire_fini(&ctx);
18022 }
18023 
18024 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
18025 {
18026 	if (IS_GEN(dev_priv, 5)) {
18027 		u32 fdi_pll_clk =
18028 			intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
18029 
18030 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
18031 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
18032 		dev_priv->fdi_pll_freq = 270000;
18033 	} else {
18034 		return;
18035 	}
18036 
18037 	drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
18038 }
18039 
18040 static int intel_initial_commit(struct drm_device *dev)
18041 {
18042 	struct drm_atomic_state *state = NULL;
18043 	struct drm_modeset_acquire_ctx ctx;
18044 	struct intel_crtc *crtc;
18045 	int ret = 0;
18046 
18047 	state = drm_atomic_state_alloc(dev);
18048 	if (!state)
18049 		return -ENOMEM;
18050 
18051 	drm_modeset_acquire_init(&ctx, 0);
18052 
18053 retry:
18054 	state->acquire_ctx = &ctx;
18055 
18056 	for_each_intel_crtc(dev, crtc) {
18057 		struct intel_crtc_state *crtc_state =
18058 			intel_atomic_get_crtc_state(state, crtc);
18059 
18060 		if (IS_ERR(crtc_state)) {
18061 			ret = PTR_ERR(crtc_state);
18062 			goto out;
18063 		}
18064 
18065 		if (crtc_state->hw.active) {
18066 			struct intel_encoder *encoder;
18067 
18068 			/*
18069 			 * We've not yet detected sink capabilities
18070 			 * (audio,infoframes,etc.) and thus we don't want to
18071 			 * force a full state recomputation yet. We want that to
18072 			 * happen only for the first real commit from userspace.
18073 			 * So preserve the inherited flag for the time being.
18074 			 */
18075 			crtc_state->inherited = true;
18076 
18077 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
18078 			if (ret)
18079 				goto out;
18080 
18081 			/*
18082 			 * FIXME hack to force a LUT update to avoid the
18083 			 * plane update forcing the pipe gamma on without
18084 			 * having a proper LUT loaded. Remove once we
18085 			 * have readout for pipe gamma enable.
18086 			 */
18087 			crtc_state->uapi.color_mgmt_changed = true;
18088 
18089 			for_each_intel_encoder_mask(dev, encoder,
18090 						    crtc_state->uapi.encoder_mask) {
18091 				if (encoder->initial_fastset_check &&
18092 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
18093 					ret = drm_atomic_add_affected_connectors(state,
18094 										 &crtc->base);
18095 					if (ret)
18096 						goto out;
18097 				}
18098 			}
18099 		}
18100 	}
18101 
18102 	ret = drm_atomic_commit(state);
18103 
18104 out:
18105 	if (ret == -EDEADLK) {
18106 		drm_atomic_state_clear(state);
18107 		drm_modeset_backoff(&ctx);
18108 		goto retry;
18109 	}
18110 
18111 	drm_atomic_state_put(state);
18112 
18113 	drm_modeset_drop_locks(&ctx);
18114 	drm_modeset_acquire_fini(&ctx);
18115 
18116 	return ret;
18117 }
18118 
18119 static void intel_mode_config_init(struct drm_i915_private *i915)
18120 {
18121 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
18122 
18123 	drm_mode_config_init(&i915->drm);
18124 	INIT_LIST_HEAD(&i915->global_obj_list);
18125 
18126 	mode_config->min_width = 0;
18127 	mode_config->min_height = 0;
18128 
18129 	mode_config->preferred_depth = 24;
18130 	mode_config->prefer_shadow = 1;
18131 
18132 	mode_config->allow_fb_modifiers = true;
18133 
18134 	mode_config->funcs = &intel_mode_funcs;
18135 
18136 	if (INTEL_GEN(i915) >= 9)
18137 		mode_config->async_page_flip = true;
18138 
18139 	/*
18140 	 * Maximum framebuffer dimensions, chosen to match
18141 	 * the maximum render engine surface size on gen4+.
18142 	 */
18143 	if (INTEL_GEN(i915) >= 7) {
18144 		mode_config->max_width = 16384;
18145 		mode_config->max_height = 16384;
18146 	} else if (INTEL_GEN(i915) >= 4) {
18147 		mode_config->max_width = 8192;
18148 		mode_config->max_height = 8192;
18149 	} else if (IS_GEN(i915, 3)) {
18150 		mode_config->max_width = 4096;
18151 		mode_config->max_height = 4096;
18152 	} else {
18153 		mode_config->max_width = 2048;
18154 		mode_config->max_height = 2048;
18155 	}
18156 
18157 	if (IS_I845G(i915) || IS_I865G(i915)) {
18158 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
18159 		mode_config->cursor_height = 1023;
18160 	} else if (IS_I830(i915) || IS_I85X(i915) ||
18161 		   IS_I915G(i915) || IS_I915GM(i915)) {
18162 		mode_config->cursor_width = 64;
18163 		mode_config->cursor_height = 64;
18164 	} else {
18165 		mode_config->cursor_width = 256;
18166 		mode_config->cursor_height = 256;
18167 	}
18168 }
18169 
18170 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
18171 {
18172 	intel_atomic_global_obj_cleanup(i915);
18173 	drm_mode_config_cleanup(&i915->drm);
18174 }
18175 
18176 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
18177 {
18178 	if (plane_config->fb) {
18179 		struct drm_framebuffer *fb = &plane_config->fb->base;
18180 
18181 		/* We may only have the stub and not a full framebuffer */
18182 		if (drm_framebuffer_read_refcount(fb))
18183 			drm_framebuffer_put(fb);
18184 		else
18185 			kfree(fb);
18186 	}
18187 
18188 	if (plane_config->vma)
18189 		i915_vma_put(plane_config->vma);
18190 }
18191 
18192 /* part #1: call before irq install */
18193 int intel_modeset_init_noirq(struct drm_i915_private *i915)
18194 {
18195 	int ret;
18196 
18197 	if (i915_inject_probe_failure(i915))
18198 		return -ENODEV;
18199 
18200 	if (HAS_DISPLAY(i915)) {
18201 		ret = drm_vblank_init(&i915->drm,
18202 				      INTEL_NUM_PIPES(i915));
18203 		if (ret)
18204 			return ret;
18205 	}
18206 
18207 	intel_bios_init(i915);
18208 
18209 	ret = intel_vga_register(i915);
18210 	if (ret)
18211 		goto cleanup_bios;
18212 
18213 	/* FIXME: completely on the wrong abstraction layer */
18214 	intel_power_domains_init_hw(i915, false);
18215 
18216 	intel_csr_ucode_init(i915);
18217 
18218 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
18219 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
18220 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
18221 
18222 	intel_mode_config_init(i915);
18223 
18224 	ret = intel_cdclk_init(i915);
18225 	if (ret)
18226 		goto cleanup_vga_client_pw_domain_csr;
18227 
18228 	ret = intel_dbuf_init(i915);
18229 	if (ret)
18230 		goto cleanup_vga_client_pw_domain_csr;
18231 
18232 	ret = intel_bw_init(i915);
18233 	if (ret)
18234 		goto cleanup_vga_client_pw_domain_csr;
18235 
18236 	init_llist_head(&i915->atomic_helper.free_list);
18237 	INIT_WORK(&i915->atomic_helper.free_work,
18238 		  intel_atomic_helper_free_state_worker);
18239 
18240 	intel_init_quirks(i915);
18241 
18242 	intel_fbc_init(i915);
18243 
18244 	return 0;
18245 
18246 cleanup_vga_client_pw_domain_csr:
18247 	intel_csr_ucode_fini(i915);
18248 	intel_power_domains_driver_remove(i915);
18249 	intel_vga_unregister(i915);
18250 cleanup_bios:
18251 	intel_bios_driver_remove(i915);
18252 
18253 	return ret;
18254 }
18255 
18256 /* part #2: call after irq install, but before gem init */
18257 int intel_modeset_init_nogem(struct drm_i915_private *i915)
18258 {
18259 	struct drm_device *dev = &i915->drm;
18260 	enum pipe pipe;
18261 	struct intel_crtc *crtc;
18262 	int ret;
18263 
18264 	intel_init_pm(i915);
18265 
18266 	intel_panel_sanitize_ssc(i915);
18267 
18268 	intel_gmbus_setup(i915);
18269 
18270 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
18271 		    INTEL_NUM_PIPES(i915),
18272 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
18273 
18274 	if (HAS_DISPLAY(i915)) {
18275 		for_each_pipe(i915, pipe) {
18276 			ret = intel_crtc_init(i915, pipe);
18277 			if (ret) {
18278 				intel_mode_config_cleanup(i915);
18279 				return ret;
18280 			}
18281 		}
18282 	}
18283 
18284 	intel_plane_possible_crtcs_init(i915);
18285 	intel_shared_dpll_init(dev);
18286 	intel_update_fdi_pll_freq(i915);
18287 
18288 	intel_update_czclk(i915);
18289 	intel_modeset_init_hw(i915);
18290 
18291 	intel_hdcp_component_init(i915);
18292 
18293 	if (i915->max_cdclk_freq == 0)
18294 		intel_update_max_cdclk(i915);
18295 
18296 	/*
18297 	 * If the platform has HTI, we need to find out whether it has reserved
18298 	 * any display resources before we create our display outputs.
18299 	 */
18300 	if (INTEL_INFO(i915)->display.has_hti)
18301 		i915->hti_state = intel_de_read(i915, HDPORT_STATE);
18302 
18303 	/* Just disable it once at startup */
18304 	intel_vga_disable(i915);
18305 	intel_setup_outputs(i915);
18306 
18307 	drm_modeset_lock_all(dev);
18308 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
18309 	drm_modeset_unlock_all(dev);
18310 
18311 	for_each_intel_crtc(dev, crtc) {
18312 		struct intel_initial_plane_config plane_config = {};
18313 
18314 		if (!crtc->active)
18315 			continue;
18316 
18317 		/*
18318 		 * Note that reserving the BIOS fb up front prevents us
18319 		 * from stuffing other stolen allocations like the ring
18320 		 * on top.  This prevents some ugliness at boot time, and
18321 		 * can even allow for smooth boot transitions if the BIOS
18322 		 * fb is large enough for the active pipe configuration.
18323 		 */
18324 		i915->display.get_initial_plane_config(crtc, &plane_config);
18325 
18326 		/*
18327 		 * If the fb is shared between multiple heads, we'll
18328 		 * just get the first one.
18329 		 */
18330 		intel_find_initial_plane_obj(crtc, &plane_config);
18331 
18332 		plane_config_fini(&plane_config);
18333 	}
18334 
18335 	/*
18336 	 * Make sure hardware watermarks really match the state we read out.
18337 	 * Note that we need to do this after reconstructing the BIOS fb's
18338 	 * since the watermark calculation done here will use pstate->fb.
18339 	 */
18340 	if (!HAS_GMCH(i915))
18341 		sanitize_watermarks(i915);
18342 
18343 	/*
18344 	 * Force all active planes to recompute their states. So that on
18345 	 * mode_setcrtc after probe, all the intel_plane_state variables
18346 	 * are already calculated and there is no assert_plane warnings
18347 	 * during bootup.
18348 	 */
18349 	ret = intel_initial_commit(dev);
18350 	if (ret)
18351 		drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
18352 
18353 	return 0;
18354 }
18355 
18356 /* part #3: call after gem init */
18357 int intel_modeset_init(struct drm_i915_private *i915)
18358 {
18359 	int ret;
18360 
18361 	intel_overlay_setup(i915);
18362 
18363 	if (!HAS_DISPLAY(i915))
18364 		return 0;
18365 
18366 	ret = intel_fbdev_init(&i915->drm);
18367 	if (ret)
18368 		return ret;
18369 
18370 	/* Only enable hotplug handling once the fbdev is fully set up. */
18371 	intel_hpd_init(i915);
18372 	intel_hpd_poll_disable(i915);
18373 
18374 	intel_init_ipc(i915);
18375 
18376 	intel_psr_set_force_mode_changed(i915->psr.dp);
18377 
18378 	return 0;
18379 }
18380 
18381 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18382 {
18383 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18384 	/* 640x480@60Hz, ~25175 kHz */
18385 	struct dpll clock = {
18386 		.m1 = 18,
18387 		.m2 = 7,
18388 		.p1 = 13,
18389 		.p2 = 4,
18390 		.n = 2,
18391 	};
18392 	u32 dpll, fp;
18393 	int i;
18394 
18395 	drm_WARN_ON(&dev_priv->drm,
18396 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
18397 
18398 	drm_dbg_kms(&dev_priv->drm,
18399 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
18400 		    pipe_name(pipe), clock.vco, clock.dot);
18401 
18402 	fp = i9xx_dpll_compute_fp(&clock);
18403 	dpll = DPLL_DVO_2X_MODE |
18404 		DPLL_VGA_MODE_DIS |
18405 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
18406 		PLL_P2_DIVIDE_BY_4 |
18407 		PLL_REF_INPUT_DREFCLK |
18408 		DPLL_VCO_ENABLE;
18409 
18410 	intel_de_write(dev_priv, FP0(pipe), fp);
18411 	intel_de_write(dev_priv, FP1(pipe), fp);
18412 
18413 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
18414 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
18415 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
18416 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
18417 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
18418 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
18419 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
18420 
18421 	/*
18422 	 * Apparently we need to have VGA mode enabled prior to changing
18423 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
18424 	 * dividers, even though the register value does change.
18425 	 */
18426 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
18427 	intel_de_write(dev_priv, DPLL(pipe), dpll);
18428 
18429 	/* Wait for the clocks to stabilize. */
18430 	intel_de_posting_read(dev_priv, DPLL(pipe));
18431 	udelay(150);
18432 
18433 	/* The pixel multiplier can only be updated once the
18434 	 * DPLL is enabled and the clocks are stable.
18435 	 *
18436 	 * So write it again.
18437 	 */
18438 	intel_de_write(dev_priv, DPLL(pipe), dpll);
18439 
18440 	/* We do this three times for luck */
18441 	for (i = 0; i < 3 ; i++) {
18442 		intel_de_write(dev_priv, DPLL(pipe), dpll);
18443 		intel_de_posting_read(dev_priv, DPLL(pipe));
18444 		udelay(150); /* wait for warmup */
18445 	}
18446 
18447 	intel_de_write(dev_priv, PIPECONF(pipe),
18448 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
18449 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
18450 
18451 	intel_wait_for_pipe_scanline_moving(crtc);
18452 }
18453 
18454 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18455 {
18456 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18457 
18458 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
18459 		    pipe_name(pipe));
18460 
18461 	drm_WARN_ON(&dev_priv->drm,
18462 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
18463 		    DISPLAY_PLANE_ENABLE);
18464 	drm_WARN_ON(&dev_priv->drm,
18465 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
18466 		    DISPLAY_PLANE_ENABLE);
18467 	drm_WARN_ON(&dev_priv->drm,
18468 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
18469 		    DISPLAY_PLANE_ENABLE);
18470 	drm_WARN_ON(&dev_priv->drm,
18471 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
18472 	drm_WARN_ON(&dev_priv->drm,
18473 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
18474 
18475 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
18476 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
18477 
18478 	intel_wait_for_pipe_scanline_stopped(crtc);
18479 
18480 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18481 	intel_de_posting_read(dev_priv, DPLL(pipe));
18482 }
18483 
18484 static void
18485 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18486 {
18487 	struct intel_crtc *crtc;
18488 
18489 	if (INTEL_GEN(dev_priv) >= 4)
18490 		return;
18491 
18492 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18493 		struct intel_plane *plane =
18494 			to_intel_plane(crtc->base.primary);
18495 		struct intel_crtc *plane_crtc;
18496 		enum pipe pipe;
18497 
18498 		if (!plane->get_hw_state(plane, &pipe))
18499 			continue;
18500 
18501 		if (pipe == crtc->pipe)
18502 			continue;
18503 
18504 		drm_dbg_kms(&dev_priv->drm,
18505 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18506 			    plane->base.base.id, plane->base.name);
18507 
18508 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18509 		intel_plane_disable_noatomic(plane_crtc, plane);
18510 	}
18511 }
18512 
18513 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18514 {
18515 	struct drm_device *dev = crtc->base.dev;
18516 	struct intel_encoder *encoder;
18517 
18518 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18519 		return true;
18520 
18521 	return false;
18522 }
18523 
18524 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18525 {
18526 	struct drm_device *dev = encoder->base.dev;
18527 	struct intel_connector *connector;
18528 
18529 	for_each_connector_on_encoder(dev, &encoder->base, connector)
18530 		return connector;
18531 
18532 	return NULL;
18533 }
18534 
18535 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
18536 			      enum pipe pch_transcoder)
18537 {
18538 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
18539 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
18540 }
18541 
18542 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
18543 {
18544 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18545 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
18546 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
18547 
18548 	if (INTEL_GEN(dev_priv) >= 9 ||
18549 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18550 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18551 		u32 val;
18552 
18553 		if (transcoder_is_dsi(cpu_transcoder))
18554 			return;
18555 
18556 		val = intel_de_read(dev_priv, reg);
18557 		val &= ~HSW_FRAME_START_DELAY_MASK;
18558 		val |= HSW_FRAME_START_DELAY(0);
18559 		intel_de_write(dev_priv, reg, val);
18560 	} else {
18561 		i915_reg_t reg = PIPECONF(cpu_transcoder);
18562 		u32 val;
18563 
18564 		val = intel_de_read(dev_priv, reg);
18565 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18566 		val |= PIPECONF_FRAME_START_DELAY(0);
18567 		intel_de_write(dev_priv, reg, val);
18568 	}
18569 
18570 	if (!crtc_state->has_pch_encoder)
18571 		return;
18572 
18573 	if (HAS_PCH_IBX(dev_priv)) {
18574 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18575 		u32 val;
18576 
18577 		val = intel_de_read(dev_priv, reg);
18578 		val &= ~TRANS_FRAME_START_DELAY_MASK;
18579 		val |= TRANS_FRAME_START_DELAY(0);
18580 		intel_de_write(dev_priv, reg, val);
18581 	} else {
18582 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18583 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
18584 		u32 val;
18585 
18586 		val = intel_de_read(dev_priv, reg);
18587 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18588 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
18589 		intel_de_write(dev_priv, reg, val);
18590 	}
18591 }
18592 
18593 static void intel_sanitize_crtc(struct intel_crtc *crtc,
18594 				struct drm_modeset_acquire_ctx *ctx)
18595 {
18596 	struct drm_device *dev = crtc->base.dev;
18597 	struct drm_i915_private *dev_priv = to_i915(dev);
18598 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
18599 
18600 	if (crtc_state->hw.active) {
18601 		struct intel_plane *plane;
18602 
18603 		/* Clear any frame start delays used for debugging left by the BIOS */
18604 		intel_sanitize_frame_start_delay(crtc_state);
18605 
18606 		/* Disable everything but the primary plane */
18607 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
18608 			const struct intel_plane_state *plane_state =
18609 				to_intel_plane_state(plane->base.state);
18610 
18611 			if (plane_state->uapi.visible &&
18612 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18613 				intel_plane_disable_noatomic(crtc, plane);
18614 		}
18615 
18616 		/*
18617 		 * Disable any background color set by the BIOS, but enable the
18618 		 * gamma and CSC to match how we program our planes.
18619 		 */
18620 		if (INTEL_GEN(dev_priv) >= 9)
18621 			intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
18622 				       SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
18623 	}
18624 
18625 	/* Adjust the state of the output pipe according to whether we
18626 	 * have active connectors/encoders. */
18627 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc))
18628 		intel_crtc_disable_noatomic(crtc, ctx);
18629 
18630 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
18631 		/*
18632 		 * We start out with underrun reporting disabled to avoid races.
18633 		 * For correct bookkeeping mark this on active crtcs.
18634 		 *
18635 		 * Also on gmch platforms we dont have any hardware bits to
18636 		 * disable the underrun reporting. Which means we need to start
18637 		 * out with underrun reporting disabled also on inactive pipes,
18638 		 * since otherwise we'll complain about the garbage we read when
18639 		 * e.g. coming up after runtime pm.
18640 		 *
18641 		 * No protection against concurrent access is required - at
18642 		 * worst a fifo underrun happens which also sets this to false.
18643 		 */
18644 		crtc->cpu_fifo_underrun_disabled = true;
18645 		/*
18646 		 * We track the PCH trancoder underrun reporting state
18647 		 * within the crtc. With crtc for pipe A housing the underrun
18648 		 * reporting state for PCH transcoder A, crtc for pipe B housing
18649 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
18650 		 * and marking underrun reporting as disabled for the non-existing
18651 		 * PCH transcoders B and C would prevent enabling the south
18652 		 * error interrupt (see cpt_can_enable_serr_int()).
18653 		 */
18654 		if (has_pch_trancoder(dev_priv, crtc->pipe))
18655 			crtc->pch_fifo_underrun_disabled = true;
18656 	}
18657 }
18658 
18659 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
18660 {
18661 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
18662 
18663 	/*
18664 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
18665 	 * the hardware when a high res displays plugged in. DPLL P
18666 	 * divider is zero, and the pipe timings are bonkers. We'll
18667 	 * try to disable everything in that case.
18668 	 *
18669 	 * FIXME would be nice to be able to sanitize this state
18670 	 * without several WARNs, but for now let's take the easy
18671 	 * road.
18672 	 */
18673 	return IS_GEN(dev_priv, 6) &&
18674 		crtc_state->hw.active &&
18675 		crtc_state->shared_dpll &&
18676 		crtc_state->port_clock == 0;
18677 }
18678 
18679 static void intel_sanitize_encoder(struct intel_encoder *encoder)
18680 {
18681 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
18682 	struct intel_connector *connector;
18683 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
18684 	struct intel_crtc_state *crtc_state = crtc ?
18685 		to_intel_crtc_state(crtc->base.state) : NULL;
18686 
18687 	/* We need to check both for a crtc link (meaning that the
18688 	 * encoder is active and trying to read from a pipe) and the
18689 	 * pipe itself being active. */
18690 	bool has_active_crtc = crtc_state &&
18691 		crtc_state->hw.active;
18692 
18693 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
18694 		drm_dbg_kms(&dev_priv->drm,
18695 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
18696 			    pipe_name(crtc->pipe));
18697 		has_active_crtc = false;
18698 	}
18699 
18700 	connector = intel_encoder_find_connector(encoder);
18701 	if (connector && !has_active_crtc) {
18702 		drm_dbg_kms(&dev_priv->drm,
18703 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
18704 			    encoder->base.base.id,
18705 			    encoder->base.name);
18706 
18707 		/* Connector is active, but has no active pipe. This is
18708 		 * fallout from our resume register restoring. Disable
18709 		 * the encoder manually again. */
18710 		if (crtc_state) {
18711 			struct drm_encoder *best_encoder;
18712 
18713 			drm_dbg_kms(&dev_priv->drm,
18714 				    "[ENCODER:%d:%s] manually disabled\n",
18715 				    encoder->base.base.id,
18716 				    encoder->base.name);
18717 
18718 			/* avoid oopsing in case the hooks consult best_encoder */
18719 			best_encoder = connector->base.state->best_encoder;
18720 			connector->base.state->best_encoder = &encoder->base;
18721 
18722 			/* FIXME NULL atomic state passed! */
18723 			if (encoder->disable)
18724 				encoder->disable(NULL, encoder, crtc_state,
18725 						 connector->base.state);
18726 			if (encoder->post_disable)
18727 				encoder->post_disable(NULL, encoder, crtc_state,
18728 						      connector->base.state);
18729 
18730 			connector->base.state->best_encoder = best_encoder;
18731 		}
18732 		encoder->base.crtc = NULL;
18733 
18734 		/* Inconsistent output/port/pipe state happens presumably due to
18735 		 * a bug in one of the get_hw_state functions. Or someplace else
18736 		 * in our code, like the register restore mess on resume. Clamp
18737 		 * things to off as a safer default. */
18738 
18739 		connector->base.dpms = DRM_MODE_DPMS_OFF;
18740 		connector->base.encoder = NULL;
18741 	}
18742 
18743 	/* notify opregion of the sanitized encoder state */
18744 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
18745 
18746 	if (INTEL_GEN(dev_priv) >= 11)
18747 		icl_sanitize_encoder_pll_mapping(encoder);
18748 }
18749 
18750 /* FIXME read out full plane state for all planes */
18751 static void readout_plane_state(struct drm_i915_private *dev_priv)
18752 {
18753 	struct intel_plane *plane;
18754 	struct intel_crtc *crtc;
18755 
18756 	for_each_intel_plane(&dev_priv->drm, plane) {
18757 		struct intel_plane_state *plane_state =
18758 			to_intel_plane_state(plane->base.state);
18759 		struct intel_crtc_state *crtc_state;
18760 		enum pipe pipe = PIPE_A;
18761 		bool visible;
18762 
18763 		visible = plane->get_hw_state(plane, &pipe);
18764 
18765 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18766 		crtc_state = to_intel_crtc_state(crtc->base.state);
18767 
18768 		intel_set_plane_visible(crtc_state, plane_state, visible);
18769 
18770 		drm_dbg_kms(&dev_priv->drm,
18771 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
18772 			    plane->base.base.id, plane->base.name,
18773 			    enableddisabled(visible), pipe_name(pipe));
18774 	}
18775 
18776 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18777 		struct intel_crtc_state *crtc_state =
18778 			to_intel_crtc_state(crtc->base.state);
18779 
18780 		fixup_active_planes(crtc_state);
18781 	}
18782 }
18783 
18784 static void intel_modeset_readout_hw_state(struct drm_device *dev)
18785 {
18786 	struct drm_i915_private *dev_priv = to_i915(dev);
18787 	struct intel_cdclk_state *cdclk_state =
18788 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
18789 	struct intel_dbuf_state *dbuf_state =
18790 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
18791 	enum pipe pipe;
18792 	struct intel_crtc *crtc;
18793 	struct intel_encoder *encoder;
18794 	struct intel_connector *connector;
18795 	struct drm_connector_list_iter conn_iter;
18796 	u8 active_pipes = 0;
18797 
18798 	for_each_intel_crtc(dev, crtc) {
18799 		struct intel_crtc_state *crtc_state =
18800 			to_intel_crtc_state(crtc->base.state);
18801 
18802 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
18803 		intel_crtc_free_hw_state(crtc_state);
18804 		intel_crtc_state_reset(crtc_state, crtc);
18805 
18806 		crtc_state->hw.active = crtc_state->hw.enable =
18807 			dev_priv->display.get_pipe_config(crtc, crtc_state);
18808 
18809 		crtc->base.enabled = crtc_state->hw.enable;
18810 		crtc->active = crtc_state->hw.active;
18811 
18812 		if (crtc_state->hw.active)
18813 			active_pipes |= BIT(crtc->pipe);
18814 
18815 		drm_dbg_kms(&dev_priv->drm,
18816 			    "[CRTC:%d:%s] hw state readout: %s\n",
18817 			    crtc->base.base.id, crtc->base.name,
18818 			    enableddisabled(crtc_state->hw.active));
18819 	}
18820 
18821 	dev_priv->active_pipes = cdclk_state->active_pipes =
18822 		dbuf_state->active_pipes = active_pipes;
18823 
18824 	readout_plane_state(dev_priv);
18825 
18826 	intel_dpll_readout_hw_state(dev_priv);
18827 
18828 	for_each_intel_encoder(dev, encoder) {
18829 		pipe = 0;
18830 
18831 		if (encoder->get_hw_state(encoder, &pipe)) {
18832 			struct intel_crtc_state *crtc_state;
18833 
18834 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18835 			crtc_state = to_intel_crtc_state(crtc->base.state);
18836 
18837 			encoder->base.crtc = &crtc->base;
18838 			encoder->get_config(encoder, crtc_state);
18839 			if (encoder->sync_state)
18840 				encoder->sync_state(encoder, crtc_state);
18841 		} else {
18842 			encoder->base.crtc = NULL;
18843 		}
18844 
18845 		drm_dbg_kms(&dev_priv->drm,
18846 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
18847 			    encoder->base.base.id, encoder->base.name,
18848 			    enableddisabled(encoder->base.crtc),
18849 			    pipe_name(pipe));
18850 	}
18851 
18852 	drm_connector_list_iter_begin(dev, &conn_iter);
18853 	for_each_intel_connector_iter(connector, &conn_iter) {
18854 		if (connector->get_hw_state(connector)) {
18855 			struct intel_crtc_state *crtc_state;
18856 			struct intel_crtc *crtc;
18857 
18858 			connector->base.dpms = DRM_MODE_DPMS_ON;
18859 
18860 			encoder = intel_attached_encoder(connector);
18861 			connector->base.encoder = &encoder->base;
18862 
18863 			crtc = to_intel_crtc(encoder->base.crtc);
18864 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
18865 
18866 			if (crtc_state && crtc_state->hw.active) {
18867 				/*
18868 				 * This has to be done during hardware readout
18869 				 * because anything calling .crtc_disable may
18870 				 * rely on the connector_mask being accurate.
18871 				 */
18872 				crtc_state->uapi.connector_mask |=
18873 					drm_connector_mask(&connector->base);
18874 				crtc_state->uapi.encoder_mask |=
18875 					drm_encoder_mask(&encoder->base);
18876 			}
18877 		} else {
18878 			connector->base.dpms = DRM_MODE_DPMS_OFF;
18879 			connector->base.encoder = NULL;
18880 		}
18881 		drm_dbg_kms(&dev_priv->drm,
18882 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
18883 			    connector->base.base.id, connector->base.name,
18884 			    enableddisabled(connector->base.encoder));
18885 	}
18886 	drm_connector_list_iter_end(&conn_iter);
18887 
18888 	for_each_intel_crtc(dev, crtc) {
18889 		struct intel_bw_state *bw_state =
18890 			to_intel_bw_state(dev_priv->bw_obj.state);
18891 		struct intel_crtc_state *crtc_state =
18892 			to_intel_crtc_state(crtc->base.state);
18893 		struct intel_plane *plane;
18894 		int min_cdclk = 0;
18895 
18896 		if (crtc_state->hw.active) {
18897 			struct drm_display_mode *mode = &crtc_state->hw.mode;
18898 
18899 			intel_mode_from_pipe_config(&crtc_state->hw.adjusted_mode,
18900 						    crtc_state);
18901 
18902 			*mode = crtc_state->hw.adjusted_mode;
18903 			mode->hdisplay = crtc_state->pipe_src_w;
18904 			mode->vdisplay = crtc_state->pipe_src_h;
18905 
18906 			/*
18907 			 * The initial mode needs to be set in order to keep
18908 			 * the atomic core happy. It wants a valid mode if the
18909 			 * crtc's enabled, so we do the above call.
18910 			 *
18911 			 * But we don't set all the derived state fully, hence
18912 			 * set a flag to indicate that a full recalculation is
18913 			 * needed on the next commit.
18914 			 */
18915 			crtc_state->inherited = true;
18916 
18917 			intel_crtc_compute_pixel_rate(crtc_state);
18918 
18919 			intel_crtc_update_active_timings(crtc_state);
18920 
18921 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
18922 		}
18923 
18924 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
18925 			const struct intel_plane_state *plane_state =
18926 				to_intel_plane_state(plane->base.state);
18927 
18928 			/*
18929 			 * FIXME don't have the fb yet, so can't
18930 			 * use intel_plane_data_rate() :(
18931 			 */
18932 			if (plane_state->uapi.visible)
18933 				crtc_state->data_rate[plane->id] =
18934 					4 * crtc_state->pixel_rate;
18935 			/*
18936 			 * FIXME don't have the fb yet, so can't
18937 			 * use plane->min_cdclk() :(
18938 			 */
18939 			if (plane_state->uapi.visible && plane->min_cdclk) {
18940 				if (crtc_state->double_wide ||
18941 				    INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
18942 					crtc_state->min_cdclk[plane->id] =
18943 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
18944 				else
18945 					crtc_state->min_cdclk[plane->id] =
18946 						crtc_state->pixel_rate;
18947 			}
18948 			drm_dbg_kms(&dev_priv->drm,
18949 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
18950 				    plane->base.base.id, plane->base.name,
18951 				    crtc_state->min_cdclk[plane->id]);
18952 		}
18953 
18954 		if (crtc_state->hw.active) {
18955 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
18956 			if (drm_WARN_ON(dev, min_cdclk < 0))
18957 				min_cdclk = 0;
18958 		}
18959 
18960 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
18961 		cdclk_state->min_voltage_level[crtc->pipe] =
18962 			crtc_state->min_voltage_level;
18963 
18964 		intel_bw_crtc_update(bw_state, crtc_state);
18965 
18966 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
18967 	}
18968 }
18969 
18970 static void
18971 get_encoder_power_domains(struct drm_i915_private *dev_priv)
18972 {
18973 	struct intel_encoder *encoder;
18974 
18975 	for_each_intel_encoder(&dev_priv->drm, encoder) {
18976 		struct intel_crtc_state *crtc_state;
18977 
18978 		if (!encoder->get_power_domains)
18979 			continue;
18980 
18981 		/*
18982 		 * MST-primary and inactive encoders don't have a crtc state
18983 		 * and neither of these require any power domain references.
18984 		 */
18985 		if (!encoder->base.crtc)
18986 			continue;
18987 
18988 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
18989 		encoder->get_power_domains(encoder, crtc_state);
18990 	}
18991 }
18992 
18993 static void intel_early_display_was(struct drm_i915_private *dev_priv)
18994 {
18995 	/*
18996 	 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
18997 	 * Also known as Wa_14010480278.
18998 	 */
18999 	if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
19000 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
19001 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
19002 
19003 	if (IS_HASWELL(dev_priv)) {
19004 		/*
19005 		 * WaRsPkgCStateDisplayPMReq:hsw
19006 		 * System hang if this isn't done before disabling all planes!
19007 		 */
19008 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
19009 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
19010 	}
19011 
19012 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
19013 		/* Display WA #1142:kbl,cfl,cml */
19014 		intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
19015 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
19016 		intel_de_rmw(dev_priv, CHICKEN_MISC_2,
19017 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
19018 			     KBL_ARB_FILL_SPARE_14);
19019 	}
19020 }
19021 
19022 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
19023 				       enum port port, i915_reg_t hdmi_reg)
19024 {
19025 	u32 val = intel_de_read(dev_priv, hdmi_reg);
19026 
19027 	if (val & SDVO_ENABLE ||
19028 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
19029 		return;
19030 
19031 	drm_dbg_kms(&dev_priv->drm,
19032 		    "Sanitizing transcoder select for HDMI %c\n",
19033 		    port_name(port));
19034 
19035 	val &= ~SDVO_PIPE_SEL_MASK;
19036 	val |= SDVO_PIPE_SEL(PIPE_A);
19037 
19038 	intel_de_write(dev_priv, hdmi_reg, val);
19039 }
19040 
19041 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
19042 				     enum port port, i915_reg_t dp_reg)
19043 {
19044 	u32 val = intel_de_read(dev_priv, dp_reg);
19045 
19046 	if (val & DP_PORT_EN ||
19047 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
19048 		return;
19049 
19050 	drm_dbg_kms(&dev_priv->drm,
19051 		    "Sanitizing transcoder select for DP %c\n",
19052 		    port_name(port));
19053 
19054 	val &= ~DP_PIPE_SEL_MASK;
19055 	val |= DP_PIPE_SEL(PIPE_A);
19056 
19057 	intel_de_write(dev_priv, dp_reg, val);
19058 }
19059 
19060 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
19061 {
19062 	/*
19063 	 * The BIOS may select transcoder B on some of the PCH
19064 	 * ports even it doesn't enable the port. This would trip
19065 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
19066 	 * Sanitize the transcoder select bits to prevent that. We
19067 	 * assume that the BIOS never actually enabled the port,
19068 	 * because if it did we'd actually have to toggle the port
19069 	 * on and back off to make the transcoder A select stick
19070 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
19071 	 * intel_disable_sdvo()).
19072 	 */
19073 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
19074 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
19075 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
19076 
19077 	/* PCH SDVOB multiplex with HDMIB */
19078 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
19079 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
19080 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
19081 }
19082 
19083 /* Scan out the current hw modeset state,
19084  * and sanitizes it to the current state
19085  */
19086 static void
19087 intel_modeset_setup_hw_state(struct drm_device *dev,
19088 			     struct drm_modeset_acquire_ctx *ctx)
19089 {
19090 	struct drm_i915_private *dev_priv = to_i915(dev);
19091 	struct intel_encoder *encoder;
19092 	struct intel_crtc *crtc;
19093 	intel_wakeref_t wakeref;
19094 
19095 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
19096 
19097 	intel_early_display_was(dev_priv);
19098 	intel_modeset_readout_hw_state(dev);
19099 
19100 	/* HW state is read out, now we need to sanitize this mess. */
19101 
19102 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
19103 	for_each_intel_encoder(dev, encoder) {
19104 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
19105 
19106 		/* We need to sanitize only the MST primary port. */
19107 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
19108 		    intel_phy_is_tc(dev_priv, phy))
19109 			intel_tc_port_sanitize(enc_to_dig_port(encoder));
19110 	}
19111 
19112 	get_encoder_power_domains(dev_priv);
19113 
19114 	if (HAS_PCH_IBX(dev_priv))
19115 		ibx_sanitize_pch_ports(dev_priv);
19116 
19117 	/*
19118 	 * intel_sanitize_plane_mapping() may need to do vblank
19119 	 * waits, so we need vblank interrupts restored beforehand.
19120 	 */
19121 	for_each_intel_crtc(&dev_priv->drm, crtc) {
19122 		struct intel_crtc_state *crtc_state =
19123 			to_intel_crtc_state(crtc->base.state);
19124 
19125 		drm_crtc_vblank_reset(&crtc->base);
19126 
19127 		if (crtc_state->hw.active)
19128 			intel_crtc_vblank_on(crtc_state);
19129 	}
19130 
19131 	intel_sanitize_plane_mapping(dev_priv);
19132 
19133 	for_each_intel_encoder(dev, encoder)
19134 		intel_sanitize_encoder(encoder);
19135 
19136 	for_each_intel_crtc(&dev_priv->drm, crtc) {
19137 		struct intel_crtc_state *crtc_state =
19138 			to_intel_crtc_state(crtc->base.state);
19139 
19140 		intel_sanitize_crtc(crtc, ctx);
19141 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
19142 	}
19143 
19144 	intel_modeset_update_connector_atomic_state(dev);
19145 
19146 	intel_dpll_sanitize_state(dev_priv);
19147 
19148 	if (IS_G4X(dev_priv)) {
19149 		g4x_wm_get_hw_state(dev_priv);
19150 		g4x_wm_sanitize(dev_priv);
19151 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
19152 		vlv_wm_get_hw_state(dev_priv);
19153 		vlv_wm_sanitize(dev_priv);
19154 	} else if (INTEL_GEN(dev_priv) >= 9) {
19155 		skl_wm_get_hw_state(dev_priv);
19156 	} else if (HAS_PCH_SPLIT(dev_priv)) {
19157 		ilk_wm_get_hw_state(dev_priv);
19158 	}
19159 
19160 	for_each_intel_crtc(dev, crtc) {
19161 		struct intel_crtc_state *crtc_state =
19162 			to_intel_crtc_state(crtc->base.state);
19163 		u64 put_domains;
19164 
19165 		put_domains = modeset_get_crtc_power_domains(crtc_state);
19166 		if (drm_WARN_ON(dev, put_domains))
19167 			modeset_put_power_domains(dev_priv, put_domains);
19168 	}
19169 
19170 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
19171 }
19172 
19173 void intel_display_resume(struct drm_device *dev)
19174 {
19175 	struct drm_i915_private *dev_priv = to_i915(dev);
19176 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
19177 	struct drm_modeset_acquire_ctx ctx;
19178 	int ret;
19179 
19180 	dev_priv->modeset_restore_state = NULL;
19181 	if (state)
19182 		state->acquire_ctx = &ctx;
19183 
19184 	drm_modeset_acquire_init(&ctx, 0);
19185 
19186 	while (1) {
19187 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
19188 		if (ret != -EDEADLK)
19189 			break;
19190 
19191 		drm_modeset_backoff(&ctx);
19192 	}
19193 
19194 	if (!ret)
19195 		ret = __intel_display_resume(dev, state, &ctx);
19196 
19197 	intel_enable_ipc(dev_priv);
19198 	drm_modeset_drop_locks(&ctx);
19199 	drm_modeset_acquire_fini(&ctx);
19200 
19201 	if (ret)
19202 		drm_err(&dev_priv->drm,
19203 			"Restoring old state failed with %i\n", ret);
19204 	if (state)
19205 		drm_atomic_state_put(state);
19206 }
19207 
19208 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
19209 {
19210 	struct intel_connector *connector;
19211 	struct drm_connector_list_iter conn_iter;
19212 
19213 	/* Kill all the work that may have been queued by hpd. */
19214 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
19215 	for_each_intel_connector_iter(connector, &conn_iter) {
19216 		if (connector->modeset_retry_work.func)
19217 			cancel_work_sync(&connector->modeset_retry_work);
19218 		if (connector->hdcp.shim) {
19219 			cancel_delayed_work_sync(&connector->hdcp.check_work);
19220 			cancel_work_sync(&connector->hdcp.prop_work);
19221 		}
19222 	}
19223 	drm_connector_list_iter_end(&conn_iter);
19224 }
19225 
19226 /* part #1: call before irq uninstall */
19227 void intel_modeset_driver_remove(struct drm_i915_private *i915)
19228 {
19229 	flush_workqueue(i915->flip_wq);
19230 	flush_workqueue(i915->modeset_wq);
19231 
19232 	flush_work(&i915->atomic_helper.free_work);
19233 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
19234 }
19235 
19236 /* part #2: call after irq uninstall */
19237 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
19238 {
19239 	/*
19240 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
19241 	 * poll handlers. Hence disable polling after hpd handling is shut down.
19242 	 */
19243 	intel_hpd_poll_fini(i915);
19244 
19245 	/*
19246 	 * MST topology needs to be suspended so we don't have any calls to
19247 	 * fbdev after it's finalized. MST will be destroyed later as part of
19248 	 * drm_mode_config_cleanup()
19249 	 */
19250 	intel_dp_mst_suspend(i915);
19251 
19252 	/* poll work can call into fbdev, hence clean that up afterwards */
19253 	intel_fbdev_fini(i915);
19254 
19255 	intel_unregister_dsm_handler();
19256 
19257 	intel_fbc_global_disable(i915);
19258 
19259 	/* flush any delayed tasks or pending work */
19260 	flush_scheduled_work();
19261 
19262 	intel_hdcp_component_fini(i915);
19263 
19264 	intel_mode_config_cleanup(i915);
19265 
19266 	intel_overlay_cleanup(i915);
19267 
19268 	intel_gmbus_teardown(i915);
19269 
19270 	destroy_workqueue(i915->flip_wq);
19271 	destroy_workqueue(i915->modeset_wq);
19272 
19273 	intel_fbc_cleanup_cfb(i915);
19274 }
19275 
19276 /* part #3: call after gem init */
19277 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
19278 {
19279 	intel_csr_ucode_fini(i915);
19280 
19281 	intel_power_domains_driver_remove(i915);
19282 
19283 	intel_vga_unregister(i915);
19284 
19285 	intel_bios_driver_remove(i915);
19286 }
19287 
19288 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
19289 
19290 struct intel_display_error_state {
19291 
19292 	u32 power_well_driver;
19293 
19294 	struct intel_cursor_error_state {
19295 		u32 control;
19296 		u32 position;
19297 		u32 base;
19298 		u32 size;
19299 	} cursor[I915_MAX_PIPES];
19300 
19301 	struct intel_pipe_error_state {
19302 		bool power_domain_on;
19303 		u32 source;
19304 		u32 stat;
19305 	} pipe[I915_MAX_PIPES];
19306 
19307 	struct intel_plane_error_state {
19308 		u32 control;
19309 		u32 stride;
19310 		u32 size;
19311 		u32 pos;
19312 		u32 addr;
19313 		u32 surface;
19314 		u32 tile_offset;
19315 	} plane[I915_MAX_PIPES];
19316 
19317 	struct intel_transcoder_error_state {
19318 		bool available;
19319 		bool power_domain_on;
19320 		enum transcoder cpu_transcoder;
19321 
19322 		u32 conf;
19323 
19324 		u32 htotal;
19325 		u32 hblank;
19326 		u32 hsync;
19327 		u32 vtotal;
19328 		u32 vblank;
19329 		u32 vsync;
19330 	} transcoder[5];
19331 };
19332 
19333 struct intel_display_error_state *
19334 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
19335 {
19336 	struct intel_display_error_state *error;
19337 	int transcoders[] = {
19338 		TRANSCODER_A,
19339 		TRANSCODER_B,
19340 		TRANSCODER_C,
19341 		TRANSCODER_D,
19342 		TRANSCODER_EDP,
19343 	};
19344 	int i;
19345 
19346 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
19347 
19348 	if (!HAS_DISPLAY(dev_priv))
19349 		return NULL;
19350 
19351 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
19352 	if (error == NULL)
19353 		return NULL;
19354 
19355 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
19356 		error->power_well_driver = intel_de_read(dev_priv,
19357 							 HSW_PWR_WELL_CTL2);
19358 
19359 	for_each_pipe(dev_priv, i) {
19360 		error->pipe[i].power_domain_on =
19361 			__intel_display_power_is_enabled(dev_priv,
19362 							 POWER_DOMAIN_PIPE(i));
19363 		if (!error->pipe[i].power_domain_on)
19364 			continue;
19365 
19366 		error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
19367 		error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
19368 		error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
19369 
19370 		error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
19371 		error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
19372 		if (INTEL_GEN(dev_priv) <= 3) {
19373 			error->plane[i].size = intel_de_read(dev_priv,
19374 							     DSPSIZE(i));
19375 			error->plane[i].pos = intel_de_read(dev_priv,
19376 							    DSPPOS(i));
19377 		}
19378 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
19379 			error->plane[i].addr = intel_de_read(dev_priv,
19380 							     DSPADDR(i));
19381 		if (INTEL_GEN(dev_priv) >= 4) {
19382 			error->plane[i].surface = intel_de_read(dev_priv,
19383 								DSPSURF(i));
19384 			error->plane[i].tile_offset = intel_de_read(dev_priv,
19385 								    DSPTILEOFF(i));
19386 		}
19387 
19388 		error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
19389 
19390 		if (HAS_GMCH(dev_priv))
19391 			error->pipe[i].stat = intel_de_read(dev_priv,
19392 							    PIPESTAT(i));
19393 	}
19394 
19395 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19396 		enum transcoder cpu_transcoder = transcoders[i];
19397 
19398 		if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
19399 			continue;
19400 
19401 		error->transcoder[i].available = true;
19402 		error->transcoder[i].power_domain_on =
19403 			__intel_display_power_is_enabled(dev_priv,
19404 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
19405 		if (!error->transcoder[i].power_domain_on)
19406 			continue;
19407 
19408 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
19409 
19410 		error->transcoder[i].conf = intel_de_read(dev_priv,
19411 							  PIPECONF(cpu_transcoder));
19412 		error->transcoder[i].htotal = intel_de_read(dev_priv,
19413 							    HTOTAL(cpu_transcoder));
19414 		error->transcoder[i].hblank = intel_de_read(dev_priv,
19415 							    HBLANK(cpu_transcoder));
19416 		error->transcoder[i].hsync = intel_de_read(dev_priv,
19417 							   HSYNC(cpu_transcoder));
19418 		error->transcoder[i].vtotal = intel_de_read(dev_priv,
19419 							    VTOTAL(cpu_transcoder));
19420 		error->transcoder[i].vblank = intel_de_read(dev_priv,
19421 							    VBLANK(cpu_transcoder));
19422 		error->transcoder[i].vsync = intel_de_read(dev_priv,
19423 							   VSYNC(cpu_transcoder));
19424 	}
19425 
19426 	return error;
19427 }
19428 
19429 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
19430 
19431 void
19432 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
19433 				struct intel_display_error_state *error)
19434 {
19435 	struct drm_i915_private *dev_priv = m->i915;
19436 	int i;
19437 
19438 	if (!error)
19439 		return;
19440 
19441 	err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
19442 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
19443 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
19444 			   error->power_well_driver);
19445 	for_each_pipe(dev_priv, i) {
19446 		err_printf(m, "Pipe [%d]:\n", i);
19447 		err_printf(m, "  Power: %s\n",
19448 			   onoff(error->pipe[i].power_domain_on));
19449 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
19450 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
19451 
19452 		err_printf(m, "Plane [%d]:\n", i);
19453 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
19454 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
19455 		if (INTEL_GEN(dev_priv) <= 3) {
19456 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
19457 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
19458 		}
19459 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
19460 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
19461 		if (INTEL_GEN(dev_priv) >= 4) {
19462 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
19463 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
19464 		}
19465 
19466 		err_printf(m, "Cursor [%d]:\n", i);
19467 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
19468 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
19469 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
19470 	}
19471 
19472 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19473 		if (!error->transcoder[i].available)
19474 			continue;
19475 
19476 		err_printf(m, "CPU transcoder: %s\n",
19477 			   transcoder_name(error->transcoder[i].cpu_transcoder));
19478 		err_printf(m, "  Power: %s\n",
19479 			   onoff(error->transcoder[i].power_domain_on));
19480 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
19481 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
19482 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
19483 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
19484 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
19485 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
19486 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
19487 	}
19488 }
19489 
19490 #endif
19491