1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/i2c.h>
28 #include <linux/input.h>
29 #include <linux/intel-iommu.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dma-resv.h>
33 #include <linux/slab.h>
34 
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_atomic_uapi.h>
38 #include <drm/drm_dp_helper.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_rect.h>
44 
45 #include "display/intel_crt.h"
46 #include "display/intel_ddi.h"
47 #include "display/intel_dp.h"
48 #include "display/intel_dp_mst.h"
49 #include "display/intel_dsi.h"
50 #include "display/intel_dvo.h"
51 #include "display/intel_gmbus.h"
52 #include "display/intel_hdmi.h"
53 #include "display/intel_lvds.h"
54 #include "display/intel_sdvo.h"
55 #include "display/intel_tv.h"
56 #include "display/intel_vdsc.h"
57 
58 #include "gt/intel_rps.h"
59 
60 #include "i915_drv.h"
61 #include "i915_trace.h"
62 #include "intel_acpi.h"
63 #include "intel_atomic.h"
64 #include "intel_atomic_plane.h"
65 #include "intel_bw.h"
66 #include "intel_cdclk.h"
67 #include "intel_color.h"
68 #include "intel_display_types.h"
69 #include "intel_dp_link_training.h"
70 #include "intel_fbc.h"
71 #include "intel_fbdev.h"
72 #include "intel_fifo_underrun.h"
73 #include "intel_frontbuffer.h"
74 #include "intel_hdcp.h"
75 #include "intel_hotplug.h"
76 #include "intel_overlay.h"
77 #include "intel_pipe_crc.h"
78 #include "intel_pm.h"
79 #include "intel_psr.h"
80 #include "intel_quirks.h"
81 #include "intel_sideband.h"
82 #include "intel_sprite.h"
83 #include "intel_tc.h"
84 #include "intel_vga.h"
85 
86 /* Primary plane formats for gen <= 3 */
87 static const u32 i8xx_primary_formats[] = {
88 	DRM_FORMAT_C8,
89 	DRM_FORMAT_XRGB1555,
90 	DRM_FORMAT_RGB565,
91 	DRM_FORMAT_XRGB8888,
92 };
93 
94 /* Primary plane formats for ivb (no fp16 due to hw issue) */
95 static const u32 ivb_primary_formats[] = {
96 	DRM_FORMAT_C8,
97 	DRM_FORMAT_RGB565,
98 	DRM_FORMAT_XRGB8888,
99 	DRM_FORMAT_XBGR8888,
100 	DRM_FORMAT_XRGB2101010,
101 	DRM_FORMAT_XBGR2101010,
102 };
103 
104 /* Primary plane formats for gen >= 4, except ivb */
105 static const u32 i965_primary_formats[] = {
106 	DRM_FORMAT_C8,
107 	DRM_FORMAT_RGB565,
108 	DRM_FORMAT_XRGB8888,
109 	DRM_FORMAT_XBGR8888,
110 	DRM_FORMAT_XRGB2101010,
111 	DRM_FORMAT_XBGR2101010,
112 	DRM_FORMAT_XBGR16161616F,
113 };
114 
115 /* Primary plane formats for vlv/chv */
116 static const u32 vlv_primary_formats[] = {
117 	DRM_FORMAT_C8,
118 	DRM_FORMAT_RGB565,
119 	DRM_FORMAT_XRGB8888,
120 	DRM_FORMAT_XBGR8888,
121 	DRM_FORMAT_ARGB8888,
122 	DRM_FORMAT_ABGR8888,
123 	DRM_FORMAT_XRGB2101010,
124 	DRM_FORMAT_XBGR2101010,
125 	DRM_FORMAT_ARGB2101010,
126 	DRM_FORMAT_ABGR2101010,
127 	DRM_FORMAT_XBGR16161616F,
128 };
129 
130 static const u64 i9xx_format_modifiers[] = {
131 	I915_FORMAT_MOD_X_TILED,
132 	DRM_FORMAT_MOD_LINEAR,
133 	DRM_FORMAT_MOD_INVALID
134 };
135 
136 /* Cursor formats */
137 static const u32 intel_cursor_formats[] = {
138 	DRM_FORMAT_ARGB8888,
139 };
140 
141 static const u64 cursor_format_modifiers[] = {
142 	DRM_FORMAT_MOD_LINEAR,
143 	DRM_FORMAT_MOD_INVALID
144 };
145 
146 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
147 				struct intel_crtc_state *pipe_config);
148 static void ilk_pch_clock_get(struct intel_crtc *crtc,
149 			      struct intel_crtc_state *pipe_config);
150 
151 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
152 				  struct drm_i915_gem_object *obj,
153 				  struct drm_mode_fb_cmd2 *mode_cmd);
154 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
155 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
156 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
157 					 const struct intel_link_m_n *m_n,
158 					 const struct intel_link_m_n *m2_n2);
159 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
160 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
161 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
162 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
163 static void vlv_prepare_pll(struct intel_crtc *crtc,
164 			    const struct intel_crtc_state *pipe_config);
165 static void chv_prepare_pll(struct intel_crtc *crtc,
166 			    const struct intel_crtc_state *pipe_config);
167 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
168 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
169 static void intel_modeset_setup_hw_state(struct drm_device *dev,
170 					 struct drm_modeset_acquire_ctx *ctx);
171 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
172 
173 struct intel_limit {
174 	struct {
175 		int min, max;
176 	} dot, vco, n, m, m1, m2, p, p1;
177 
178 	struct {
179 		int dot_limit;
180 		int p2_slow, p2_fast;
181 	} p2;
182 };
183 
184 /* returns HPLL frequency in kHz */
185 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
186 {
187 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
188 
189 	/* Obtain SKU information */
190 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
191 		CCK_FUSE_HPLL_FREQ_MASK;
192 
193 	return vco_freq[hpll_freq] * 1000;
194 }
195 
196 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
197 		      const char *name, u32 reg, int ref_freq)
198 {
199 	u32 val;
200 	int divider;
201 
202 	val = vlv_cck_read(dev_priv, reg);
203 	divider = val & CCK_FREQUENCY_VALUES;
204 
205 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
206 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
207 		 "%s change in progress\n", name);
208 
209 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
210 }
211 
212 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
213 			   const char *name, u32 reg)
214 {
215 	int hpll;
216 
217 	vlv_cck_get(dev_priv);
218 
219 	if (dev_priv->hpll_freq == 0)
220 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
221 
222 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
223 
224 	vlv_cck_put(dev_priv);
225 
226 	return hpll;
227 }
228 
229 static void intel_update_czclk(struct drm_i915_private *dev_priv)
230 {
231 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
232 		return;
233 
234 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
235 						      CCK_CZ_CLOCK_CONTROL);
236 
237 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
238 		dev_priv->czclk_freq);
239 }
240 
241 static inline u32 /* units of 100MHz */
242 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
243 		    const struct intel_crtc_state *pipe_config)
244 {
245 	if (HAS_DDI(dev_priv))
246 		return pipe_config->port_clock; /* SPLL */
247 	else
248 		return dev_priv->fdi_pll_freq;
249 }
250 
251 static const struct intel_limit intel_limits_i8xx_dac = {
252 	.dot = { .min = 25000, .max = 350000 },
253 	.vco = { .min = 908000, .max = 1512000 },
254 	.n = { .min = 2, .max = 16 },
255 	.m = { .min = 96, .max = 140 },
256 	.m1 = { .min = 18, .max = 26 },
257 	.m2 = { .min = 6, .max = 16 },
258 	.p = { .min = 4, .max = 128 },
259 	.p1 = { .min = 2, .max = 33 },
260 	.p2 = { .dot_limit = 165000,
261 		.p2_slow = 4, .p2_fast = 2 },
262 };
263 
264 static const struct intel_limit intel_limits_i8xx_dvo = {
265 	.dot = { .min = 25000, .max = 350000 },
266 	.vco = { .min = 908000, .max = 1512000 },
267 	.n = { .min = 2, .max = 16 },
268 	.m = { .min = 96, .max = 140 },
269 	.m1 = { .min = 18, .max = 26 },
270 	.m2 = { .min = 6, .max = 16 },
271 	.p = { .min = 4, .max = 128 },
272 	.p1 = { .min = 2, .max = 33 },
273 	.p2 = { .dot_limit = 165000,
274 		.p2_slow = 4, .p2_fast = 4 },
275 };
276 
277 static const struct intel_limit intel_limits_i8xx_lvds = {
278 	.dot = { .min = 25000, .max = 350000 },
279 	.vco = { .min = 908000, .max = 1512000 },
280 	.n = { .min = 2, .max = 16 },
281 	.m = { .min = 96, .max = 140 },
282 	.m1 = { .min = 18, .max = 26 },
283 	.m2 = { .min = 6, .max = 16 },
284 	.p = { .min = 4, .max = 128 },
285 	.p1 = { .min = 1, .max = 6 },
286 	.p2 = { .dot_limit = 165000,
287 		.p2_slow = 14, .p2_fast = 7 },
288 };
289 
290 static const struct intel_limit intel_limits_i9xx_sdvo = {
291 	.dot = { .min = 20000, .max = 400000 },
292 	.vco = { .min = 1400000, .max = 2800000 },
293 	.n = { .min = 1, .max = 6 },
294 	.m = { .min = 70, .max = 120 },
295 	.m1 = { .min = 8, .max = 18 },
296 	.m2 = { .min = 3, .max = 7 },
297 	.p = { .min = 5, .max = 80 },
298 	.p1 = { .min = 1, .max = 8 },
299 	.p2 = { .dot_limit = 200000,
300 		.p2_slow = 10, .p2_fast = 5 },
301 };
302 
303 static const struct intel_limit intel_limits_i9xx_lvds = {
304 	.dot = { .min = 20000, .max = 400000 },
305 	.vco = { .min = 1400000, .max = 2800000 },
306 	.n = { .min = 1, .max = 6 },
307 	.m = { .min = 70, .max = 120 },
308 	.m1 = { .min = 8, .max = 18 },
309 	.m2 = { .min = 3, .max = 7 },
310 	.p = { .min = 7, .max = 98 },
311 	.p1 = { .min = 1, .max = 8 },
312 	.p2 = { .dot_limit = 112000,
313 		.p2_slow = 14, .p2_fast = 7 },
314 };
315 
316 
317 static const struct intel_limit intel_limits_g4x_sdvo = {
318 	.dot = { .min = 25000, .max = 270000 },
319 	.vco = { .min = 1750000, .max = 3500000},
320 	.n = { .min = 1, .max = 4 },
321 	.m = { .min = 104, .max = 138 },
322 	.m1 = { .min = 17, .max = 23 },
323 	.m2 = { .min = 5, .max = 11 },
324 	.p = { .min = 10, .max = 30 },
325 	.p1 = { .min = 1, .max = 3},
326 	.p2 = { .dot_limit = 270000,
327 		.p2_slow = 10,
328 		.p2_fast = 10
329 	},
330 };
331 
332 static const struct intel_limit intel_limits_g4x_hdmi = {
333 	.dot = { .min = 22000, .max = 400000 },
334 	.vco = { .min = 1750000, .max = 3500000},
335 	.n = { .min = 1, .max = 4 },
336 	.m = { .min = 104, .max = 138 },
337 	.m1 = { .min = 16, .max = 23 },
338 	.m2 = { .min = 5, .max = 11 },
339 	.p = { .min = 5, .max = 80 },
340 	.p1 = { .min = 1, .max = 8},
341 	.p2 = { .dot_limit = 165000,
342 		.p2_slow = 10, .p2_fast = 5 },
343 };
344 
345 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
346 	.dot = { .min = 20000, .max = 115000 },
347 	.vco = { .min = 1750000, .max = 3500000 },
348 	.n = { .min = 1, .max = 3 },
349 	.m = { .min = 104, .max = 138 },
350 	.m1 = { .min = 17, .max = 23 },
351 	.m2 = { .min = 5, .max = 11 },
352 	.p = { .min = 28, .max = 112 },
353 	.p1 = { .min = 2, .max = 8 },
354 	.p2 = { .dot_limit = 0,
355 		.p2_slow = 14, .p2_fast = 14
356 	},
357 };
358 
359 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
360 	.dot = { .min = 80000, .max = 224000 },
361 	.vco = { .min = 1750000, .max = 3500000 },
362 	.n = { .min = 1, .max = 3 },
363 	.m = { .min = 104, .max = 138 },
364 	.m1 = { .min = 17, .max = 23 },
365 	.m2 = { .min = 5, .max = 11 },
366 	.p = { .min = 14, .max = 42 },
367 	.p1 = { .min = 2, .max = 6 },
368 	.p2 = { .dot_limit = 0,
369 		.p2_slow = 7, .p2_fast = 7
370 	},
371 };
372 
373 static const struct intel_limit pnv_limits_sdvo = {
374 	.dot = { .min = 20000, .max = 400000},
375 	.vco = { .min = 1700000, .max = 3500000 },
376 	/* Pineview's Ncounter is a ring counter */
377 	.n = { .min = 3, .max = 6 },
378 	.m = { .min = 2, .max = 256 },
379 	/* Pineview only has one combined m divider, which we treat as m2. */
380 	.m1 = { .min = 0, .max = 0 },
381 	.m2 = { .min = 0, .max = 254 },
382 	.p = { .min = 5, .max = 80 },
383 	.p1 = { .min = 1, .max = 8 },
384 	.p2 = { .dot_limit = 200000,
385 		.p2_slow = 10, .p2_fast = 5 },
386 };
387 
388 static const struct intel_limit pnv_limits_lvds = {
389 	.dot = { .min = 20000, .max = 400000 },
390 	.vco = { .min = 1700000, .max = 3500000 },
391 	.n = { .min = 3, .max = 6 },
392 	.m = { .min = 2, .max = 256 },
393 	.m1 = { .min = 0, .max = 0 },
394 	.m2 = { .min = 0, .max = 254 },
395 	.p = { .min = 7, .max = 112 },
396 	.p1 = { .min = 1, .max = 8 },
397 	.p2 = { .dot_limit = 112000,
398 		.p2_slow = 14, .p2_fast = 14 },
399 };
400 
401 /* Ironlake / Sandybridge
402  *
403  * We calculate clock using (register_value + 2) for N/M1/M2, so here
404  * the range value for them is (actual_value - 2).
405  */
406 static const struct intel_limit ilk_limits_dac = {
407 	.dot = { .min = 25000, .max = 350000 },
408 	.vco = { .min = 1760000, .max = 3510000 },
409 	.n = { .min = 1, .max = 5 },
410 	.m = { .min = 79, .max = 127 },
411 	.m1 = { .min = 12, .max = 22 },
412 	.m2 = { .min = 5, .max = 9 },
413 	.p = { .min = 5, .max = 80 },
414 	.p1 = { .min = 1, .max = 8 },
415 	.p2 = { .dot_limit = 225000,
416 		.p2_slow = 10, .p2_fast = 5 },
417 };
418 
419 static const struct intel_limit ilk_limits_single_lvds = {
420 	.dot = { .min = 25000, .max = 350000 },
421 	.vco = { .min = 1760000, .max = 3510000 },
422 	.n = { .min = 1, .max = 3 },
423 	.m = { .min = 79, .max = 118 },
424 	.m1 = { .min = 12, .max = 22 },
425 	.m2 = { .min = 5, .max = 9 },
426 	.p = { .min = 28, .max = 112 },
427 	.p1 = { .min = 2, .max = 8 },
428 	.p2 = { .dot_limit = 225000,
429 		.p2_slow = 14, .p2_fast = 14 },
430 };
431 
432 static const struct intel_limit ilk_limits_dual_lvds = {
433 	.dot = { .min = 25000, .max = 350000 },
434 	.vco = { .min = 1760000, .max = 3510000 },
435 	.n = { .min = 1, .max = 3 },
436 	.m = { .min = 79, .max = 127 },
437 	.m1 = { .min = 12, .max = 22 },
438 	.m2 = { .min = 5, .max = 9 },
439 	.p = { .min = 14, .max = 56 },
440 	.p1 = { .min = 2, .max = 8 },
441 	.p2 = { .dot_limit = 225000,
442 		.p2_slow = 7, .p2_fast = 7 },
443 };
444 
445 /* LVDS 100mhz refclk limits. */
446 static const struct intel_limit ilk_limits_single_lvds_100m = {
447 	.dot = { .min = 25000, .max = 350000 },
448 	.vco = { .min = 1760000, .max = 3510000 },
449 	.n = { .min = 1, .max = 2 },
450 	.m = { .min = 79, .max = 126 },
451 	.m1 = { .min = 12, .max = 22 },
452 	.m2 = { .min = 5, .max = 9 },
453 	.p = { .min = 28, .max = 112 },
454 	.p1 = { .min = 2, .max = 8 },
455 	.p2 = { .dot_limit = 225000,
456 		.p2_slow = 14, .p2_fast = 14 },
457 };
458 
459 static const struct intel_limit ilk_limits_dual_lvds_100m = {
460 	.dot = { .min = 25000, .max = 350000 },
461 	.vco = { .min = 1760000, .max = 3510000 },
462 	.n = { .min = 1, .max = 3 },
463 	.m = { .min = 79, .max = 126 },
464 	.m1 = { .min = 12, .max = 22 },
465 	.m2 = { .min = 5, .max = 9 },
466 	.p = { .min = 14, .max = 42 },
467 	.p1 = { .min = 2, .max = 6 },
468 	.p2 = { .dot_limit = 225000,
469 		.p2_slow = 7, .p2_fast = 7 },
470 };
471 
472 static const struct intel_limit intel_limits_vlv = {
473 	 /*
474 	  * These are the data rate limits (measured in fast clocks)
475 	  * since those are the strictest limits we have. The fast
476 	  * clock and actual rate limits are more relaxed, so checking
477 	  * them would make no difference.
478 	  */
479 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
480 	.vco = { .min = 4000000, .max = 6000000 },
481 	.n = { .min = 1, .max = 7 },
482 	.m1 = { .min = 2, .max = 3 },
483 	.m2 = { .min = 11, .max = 156 },
484 	.p1 = { .min = 2, .max = 3 },
485 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
486 };
487 
488 static const struct intel_limit intel_limits_chv = {
489 	/*
490 	 * These are the data rate limits (measured in fast clocks)
491 	 * since those are the strictest limits we have.  The fast
492 	 * clock and actual rate limits are more relaxed, so checking
493 	 * them would make no difference.
494 	 */
495 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
496 	.vco = { .min = 4800000, .max = 6480000 },
497 	.n = { .min = 1, .max = 1 },
498 	.m1 = { .min = 2, .max = 2 },
499 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
500 	.p1 = { .min = 2, .max = 4 },
501 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
502 };
503 
504 static const struct intel_limit intel_limits_bxt = {
505 	/* FIXME: find real dot limits */
506 	.dot = { .min = 0, .max = INT_MAX },
507 	.vco = { .min = 4800000, .max = 6700000 },
508 	.n = { .min = 1, .max = 1 },
509 	.m1 = { .min = 2, .max = 2 },
510 	/* FIXME: find real m2 limits */
511 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
512 	.p1 = { .min = 2, .max = 4 },
513 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
514 };
515 
516 /* WA Display #0827: Gen9:all */
517 static void
518 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
519 {
520 	if (enable)
521 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
522 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
523 	else
524 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
525 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
526 }
527 
528 /* Wa_2006604312:icl */
529 static void
530 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
531 		       bool enable)
532 {
533 	if (enable)
534 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
535 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
536 	else
537 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
538 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
539 }
540 
541 static bool
542 needs_modeset(const struct intel_crtc_state *state)
543 {
544 	return drm_atomic_crtc_needs_modeset(&state->uapi);
545 }
546 
547 bool
548 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
549 {
550 	return (crtc_state->master_transcoder != INVALID_TRANSCODER ||
551 		crtc_state->sync_mode_slaves_mask);
552 }
553 
554 static bool
555 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
556 {
557 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
558 }
559 
560 /*
561  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
562  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
563  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
564  * The helpers' return value is the rate of the clock that is fed to the
565  * display engine's pipe which can be the above fast dot clock rate or a
566  * divided-down version of it.
567  */
568 /* m1 is reserved as 0 in Pineview, n is a ring counter */
569 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
570 {
571 	clock->m = clock->m2 + 2;
572 	clock->p = clock->p1 * clock->p2;
573 	if (WARN_ON(clock->n == 0 || clock->p == 0))
574 		return 0;
575 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
576 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
577 
578 	return clock->dot;
579 }
580 
581 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
582 {
583 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
584 }
585 
586 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
587 {
588 	clock->m = i9xx_dpll_compute_m(clock);
589 	clock->p = clock->p1 * clock->p2;
590 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
591 		return 0;
592 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
593 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
594 
595 	return clock->dot;
596 }
597 
598 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
599 {
600 	clock->m = clock->m1 * clock->m2;
601 	clock->p = clock->p1 * clock->p2;
602 	if (WARN_ON(clock->n == 0 || clock->p == 0))
603 		return 0;
604 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
605 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
606 
607 	return clock->dot / 5;
608 }
609 
610 int chv_calc_dpll_params(int refclk, struct dpll *clock)
611 {
612 	clock->m = clock->m1 * clock->m2;
613 	clock->p = clock->p1 * clock->p2;
614 	if (WARN_ON(clock->n == 0 || clock->p == 0))
615 		return 0;
616 	clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
617 					   clock->n << 22);
618 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
619 
620 	return clock->dot / 5;
621 }
622 
623 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
624 
625 /*
626  * Returns whether the given set of divisors are valid for a given refclk with
627  * the given connectors.
628  */
629 static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
630 			       const struct intel_limit *limit,
631 			       const struct dpll *clock)
632 {
633 	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
634 		INTELPllInvalid("n out of range\n");
635 	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
636 		INTELPllInvalid("p1 out of range\n");
637 	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
638 		INTELPllInvalid("m2 out of range\n");
639 	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
640 		INTELPllInvalid("m1 out of range\n");
641 
642 	if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
643 	    !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
644 		if (clock->m1 <= clock->m2)
645 			INTELPllInvalid("m1 <= m2\n");
646 
647 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
648 	    !IS_GEN9_LP(dev_priv)) {
649 		if (clock->p < limit->p.min || limit->p.max < clock->p)
650 			INTELPllInvalid("p out of range\n");
651 		if (clock->m < limit->m.min || limit->m.max < clock->m)
652 			INTELPllInvalid("m out of range\n");
653 	}
654 
655 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
656 		INTELPllInvalid("vco out of range\n");
657 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
658 	 * connector, etc., rather than just a single range.
659 	 */
660 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
661 		INTELPllInvalid("dot out of range\n");
662 
663 	return true;
664 }
665 
666 static int
667 i9xx_select_p2_div(const struct intel_limit *limit,
668 		   const struct intel_crtc_state *crtc_state,
669 		   int target)
670 {
671 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
672 
673 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
674 		/*
675 		 * For LVDS just rely on its current settings for dual-channel.
676 		 * We haven't figured out how to reliably set up different
677 		 * single/dual channel state, if we even can.
678 		 */
679 		if (intel_is_dual_link_lvds(dev_priv))
680 			return limit->p2.p2_fast;
681 		else
682 			return limit->p2.p2_slow;
683 	} else {
684 		if (target < limit->p2.dot_limit)
685 			return limit->p2.p2_slow;
686 		else
687 			return limit->p2.p2_fast;
688 	}
689 }
690 
691 /*
692  * Returns a set of divisors for the desired target clock with the given
693  * refclk, or FALSE.  The returned values represent the clock equation:
694  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
695  *
696  * Target and reference clocks are specified in kHz.
697  *
698  * If match_clock is provided, then best_clock P divider must match the P
699  * divider from @match_clock used for LVDS downclocking.
700  */
701 static bool
702 i9xx_find_best_dpll(const struct intel_limit *limit,
703 		    struct intel_crtc_state *crtc_state,
704 		    int target, int refclk, struct dpll *match_clock,
705 		    struct dpll *best_clock)
706 {
707 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
708 	struct dpll clock;
709 	int err = target;
710 
711 	memset(best_clock, 0, sizeof(*best_clock));
712 
713 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
714 
715 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
716 	     clock.m1++) {
717 		for (clock.m2 = limit->m2.min;
718 		     clock.m2 <= limit->m2.max; clock.m2++) {
719 			if (clock.m2 >= clock.m1)
720 				break;
721 			for (clock.n = limit->n.min;
722 			     clock.n <= limit->n.max; clock.n++) {
723 				for (clock.p1 = limit->p1.min;
724 					clock.p1 <= limit->p1.max; clock.p1++) {
725 					int this_err;
726 
727 					i9xx_calc_dpll_params(refclk, &clock);
728 					if (!intel_PLL_is_valid(to_i915(dev),
729 								limit,
730 								&clock))
731 						continue;
732 					if (match_clock &&
733 					    clock.p != match_clock->p)
734 						continue;
735 
736 					this_err = abs(clock.dot - target);
737 					if (this_err < err) {
738 						*best_clock = clock;
739 						err = this_err;
740 					}
741 				}
742 			}
743 		}
744 	}
745 
746 	return (err != target);
747 }
748 
749 /*
750  * Returns a set of divisors for the desired target clock with the given
751  * refclk, or FALSE.  The returned values represent the clock equation:
752  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
753  *
754  * Target and reference clocks are specified in kHz.
755  *
756  * If match_clock is provided, then best_clock P divider must match the P
757  * divider from @match_clock used for LVDS downclocking.
758  */
759 static bool
760 pnv_find_best_dpll(const struct intel_limit *limit,
761 		   struct intel_crtc_state *crtc_state,
762 		   int target, int refclk, struct dpll *match_clock,
763 		   struct dpll *best_clock)
764 {
765 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
766 	struct dpll clock;
767 	int err = target;
768 
769 	memset(best_clock, 0, sizeof(*best_clock));
770 
771 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
772 
773 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
774 	     clock.m1++) {
775 		for (clock.m2 = limit->m2.min;
776 		     clock.m2 <= limit->m2.max; clock.m2++) {
777 			for (clock.n = limit->n.min;
778 			     clock.n <= limit->n.max; clock.n++) {
779 				for (clock.p1 = limit->p1.min;
780 					clock.p1 <= limit->p1.max; clock.p1++) {
781 					int this_err;
782 
783 					pnv_calc_dpll_params(refclk, &clock);
784 					if (!intel_PLL_is_valid(to_i915(dev),
785 								limit,
786 								&clock))
787 						continue;
788 					if (match_clock &&
789 					    clock.p != match_clock->p)
790 						continue;
791 
792 					this_err = abs(clock.dot - target);
793 					if (this_err < err) {
794 						*best_clock = clock;
795 						err = this_err;
796 					}
797 				}
798 			}
799 		}
800 	}
801 
802 	return (err != target);
803 }
804 
805 /*
806  * Returns a set of divisors for the desired target clock with the given
807  * refclk, or FALSE.  The returned values represent the clock equation:
808  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
809  *
810  * Target and reference clocks are specified in kHz.
811  *
812  * If match_clock is provided, then best_clock P divider must match the P
813  * divider from @match_clock used for LVDS downclocking.
814  */
815 static bool
816 g4x_find_best_dpll(const struct intel_limit *limit,
817 		   struct intel_crtc_state *crtc_state,
818 		   int target, int refclk, struct dpll *match_clock,
819 		   struct dpll *best_clock)
820 {
821 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
822 	struct dpll clock;
823 	int max_n;
824 	bool found = false;
825 	/* approximately equals target * 0.00585 */
826 	int err_most = (target >> 8) + (target >> 9);
827 
828 	memset(best_clock, 0, sizeof(*best_clock));
829 
830 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
831 
832 	max_n = limit->n.max;
833 	/* based on hardware requirement, prefer smaller n to precision */
834 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
835 		/* based on hardware requirement, prefere larger m1,m2 */
836 		for (clock.m1 = limit->m1.max;
837 		     clock.m1 >= limit->m1.min; clock.m1--) {
838 			for (clock.m2 = limit->m2.max;
839 			     clock.m2 >= limit->m2.min; clock.m2--) {
840 				for (clock.p1 = limit->p1.max;
841 				     clock.p1 >= limit->p1.min; clock.p1--) {
842 					int this_err;
843 
844 					i9xx_calc_dpll_params(refclk, &clock);
845 					if (!intel_PLL_is_valid(to_i915(dev),
846 								limit,
847 								&clock))
848 						continue;
849 
850 					this_err = abs(clock.dot - target);
851 					if (this_err < err_most) {
852 						*best_clock = clock;
853 						err_most = this_err;
854 						max_n = clock.n;
855 						found = true;
856 					}
857 				}
858 			}
859 		}
860 	}
861 	return found;
862 }
863 
864 /*
865  * Check if the calculated PLL configuration is more optimal compared to the
866  * best configuration and error found so far. Return the calculated error.
867  */
868 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
869 			       const struct dpll *calculated_clock,
870 			       const struct dpll *best_clock,
871 			       unsigned int best_error_ppm,
872 			       unsigned int *error_ppm)
873 {
874 	/*
875 	 * For CHV ignore the error and consider only the P value.
876 	 * Prefer a bigger P value based on HW requirements.
877 	 */
878 	if (IS_CHERRYVIEW(to_i915(dev))) {
879 		*error_ppm = 0;
880 
881 		return calculated_clock->p > best_clock->p;
882 	}
883 
884 	if (drm_WARN_ON_ONCE(dev, !target_freq))
885 		return false;
886 
887 	*error_ppm = div_u64(1000000ULL *
888 				abs(target_freq - calculated_clock->dot),
889 			     target_freq);
890 	/*
891 	 * Prefer a better P value over a better (smaller) error if the error
892 	 * is small. Ensure this preference for future configurations too by
893 	 * setting the error to 0.
894 	 */
895 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
896 		*error_ppm = 0;
897 
898 		return true;
899 	}
900 
901 	return *error_ppm + 10 < best_error_ppm;
902 }
903 
904 /*
905  * Returns a set of divisors for the desired target clock with the given
906  * refclk, or FALSE.  The returned values represent the clock equation:
907  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
908  */
909 static bool
910 vlv_find_best_dpll(const struct intel_limit *limit,
911 		   struct intel_crtc_state *crtc_state,
912 		   int target, int refclk, struct dpll *match_clock,
913 		   struct dpll *best_clock)
914 {
915 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
916 	struct drm_device *dev = crtc->base.dev;
917 	struct dpll clock;
918 	unsigned int bestppm = 1000000;
919 	/* min update 19.2 MHz */
920 	int max_n = min(limit->n.max, refclk / 19200);
921 	bool found = false;
922 
923 	target *= 5; /* fast clock */
924 
925 	memset(best_clock, 0, sizeof(*best_clock));
926 
927 	/* based on hardware requirement, prefer smaller n to precision */
928 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
929 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
930 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
931 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
932 				clock.p = clock.p1 * clock.p2;
933 				/* based on hardware requirement, prefer bigger m1,m2 values */
934 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
935 					unsigned int ppm;
936 
937 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
938 								     refclk * clock.m1);
939 
940 					vlv_calc_dpll_params(refclk, &clock);
941 
942 					if (!intel_PLL_is_valid(to_i915(dev),
943 								limit,
944 								&clock))
945 						continue;
946 
947 					if (!vlv_PLL_is_optimal(dev, target,
948 								&clock,
949 								best_clock,
950 								bestppm, &ppm))
951 						continue;
952 
953 					*best_clock = clock;
954 					bestppm = ppm;
955 					found = true;
956 				}
957 			}
958 		}
959 	}
960 
961 	return found;
962 }
963 
964 /*
965  * Returns a set of divisors for the desired target clock with the given
966  * refclk, or FALSE.  The returned values represent the clock equation:
967  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
968  */
969 static bool
970 chv_find_best_dpll(const struct intel_limit *limit,
971 		   struct intel_crtc_state *crtc_state,
972 		   int target, int refclk, struct dpll *match_clock,
973 		   struct dpll *best_clock)
974 {
975 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
976 	struct drm_device *dev = crtc->base.dev;
977 	unsigned int best_error_ppm;
978 	struct dpll clock;
979 	u64 m2;
980 	int found = false;
981 
982 	memset(best_clock, 0, sizeof(*best_clock));
983 	best_error_ppm = 1000000;
984 
985 	/*
986 	 * Based on hardware doc, the n always set to 1, and m1 always
987 	 * set to 2.  If requires to support 200Mhz refclk, we need to
988 	 * revisit this because n may not 1 anymore.
989 	 */
990 	clock.n = 1, clock.m1 = 2;
991 	target *= 5;	/* fast clock */
992 
993 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
994 		for (clock.p2 = limit->p2.p2_fast;
995 				clock.p2 >= limit->p2.p2_slow;
996 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
997 			unsigned int error_ppm;
998 
999 			clock.p = clock.p1 * clock.p2;
1000 
1001 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1002 						   refclk * clock.m1);
1003 
1004 			if (m2 > INT_MAX/clock.m1)
1005 				continue;
1006 
1007 			clock.m2 = m2;
1008 
1009 			chv_calc_dpll_params(refclk, &clock);
1010 
1011 			if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
1012 				continue;
1013 
1014 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1015 						best_error_ppm, &error_ppm))
1016 				continue;
1017 
1018 			*best_clock = clock;
1019 			best_error_ppm = error_ppm;
1020 			found = true;
1021 		}
1022 	}
1023 
1024 	return found;
1025 }
1026 
1027 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
1028 			struct dpll *best_clock)
1029 {
1030 	int refclk = 100000;
1031 	const struct intel_limit *limit = &intel_limits_bxt;
1032 
1033 	return chv_find_best_dpll(limit, crtc_state,
1034 				  crtc_state->port_clock, refclk,
1035 				  NULL, best_clock);
1036 }
1037 
1038 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1039 				    enum pipe pipe)
1040 {
1041 	i915_reg_t reg = PIPEDSL(pipe);
1042 	u32 line1, line2;
1043 	u32 line_mask;
1044 
1045 	if (IS_GEN(dev_priv, 2))
1046 		line_mask = DSL_LINEMASK_GEN2;
1047 	else
1048 		line_mask = DSL_LINEMASK_GEN3;
1049 
1050 	line1 = intel_de_read(dev_priv, reg) & line_mask;
1051 	msleep(5);
1052 	line2 = intel_de_read(dev_priv, reg) & line_mask;
1053 
1054 	return line1 != line2;
1055 }
1056 
1057 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1058 {
1059 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1060 	enum pipe pipe = crtc->pipe;
1061 
1062 	/* Wait for the display line to settle/start moving */
1063 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1064 		drm_err(&dev_priv->drm,
1065 			"pipe %c scanline %s wait timed out\n",
1066 			pipe_name(pipe), onoff(state));
1067 }
1068 
1069 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1070 {
1071 	wait_for_pipe_scanline_moving(crtc, false);
1072 }
1073 
1074 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1075 {
1076 	wait_for_pipe_scanline_moving(crtc, true);
1077 }
1078 
1079 static void
1080 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1081 {
1082 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1083 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1084 
1085 	if (INTEL_GEN(dev_priv) >= 4) {
1086 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1087 		i915_reg_t reg = PIPECONF(cpu_transcoder);
1088 
1089 		/* Wait for the Pipe State to go off */
1090 		if (intel_de_wait_for_clear(dev_priv, reg,
1091 					    I965_PIPECONF_ACTIVE, 100))
1092 			drm_WARN(&dev_priv->drm, 1,
1093 				 "pipe_off wait timed out\n");
1094 	} else {
1095 		intel_wait_for_pipe_scanline_stopped(crtc);
1096 	}
1097 }
1098 
1099 /* Only for pre-ILK configs */
1100 void assert_pll(struct drm_i915_private *dev_priv,
1101 		enum pipe pipe, bool state)
1102 {
1103 	u32 val;
1104 	bool cur_state;
1105 
1106 	val = intel_de_read(dev_priv, DPLL(pipe));
1107 	cur_state = !!(val & DPLL_VCO_ENABLE);
1108 	I915_STATE_WARN(cur_state != state,
1109 	     "PLL state assertion failure (expected %s, current %s)\n",
1110 			onoff(state), onoff(cur_state));
1111 }
1112 
1113 /* XXX: the dsi pll is shared between MIPI DSI ports */
1114 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1115 {
1116 	u32 val;
1117 	bool cur_state;
1118 
1119 	vlv_cck_get(dev_priv);
1120 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1121 	vlv_cck_put(dev_priv);
1122 
1123 	cur_state = val & DSI_PLL_VCO_EN;
1124 	I915_STATE_WARN(cur_state != state,
1125 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1126 			onoff(state), onoff(cur_state));
1127 }
1128 
1129 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1130 			  enum pipe pipe, bool state)
1131 {
1132 	bool cur_state;
1133 
1134 	if (HAS_DDI(dev_priv)) {
1135 		/*
1136 		 * DDI does not have a specific FDI_TX register.
1137 		 *
1138 		 * FDI is never fed from EDP transcoder
1139 		 * so pipe->transcoder cast is fine here.
1140 		 */
1141 		enum transcoder cpu_transcoder = (enum transcoder)pipe;
1142 		u32 val = intel_de_read(dev_priv,
1143 					TRANS_DDI_FUNC_CTL(cpu_transcoder));
1144 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1145 	} else {
1146 		u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1147 		cur_state = !!(val & FDI_TX_ENABLE);
1148 	}
1149 	I915_STATE_WARN(cur_state != state,
1150 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1151 			onoff(state), onoff(cur_state));
1152 }
1153 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1154 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1155 
1156 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1157 			  enum pipe pipe, bool state)
1158 {
1159 	u32 val;
1160 	bool cur_state;
1161 
1162 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1163 	cur_state = !!(val & FDI_RX_ENABLE);
1164 	I915_STATE_WARN(cur_state != state,
1165 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1166 			onoff(state), onoff(cur_state));
1167 }
1168 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1169 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1170 
1171 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1172 				      enum pipe pipe)
1173 {
1174 	u32 val;
1175 
1176 	/* ILK FDI PLL is always enabled */
1177 	if (IS_GEN(dev_priv, 5))
1178 		return;
1179 
1180 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1181 	if (HAS_DDI(dev_priv))
1182 		return;
1183 
1184 	val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1185 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1186 }
1187 
1188 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1189 		       enum pipe pipe, bool state)
1190 {
1191 	u32 val;
1192 	bool cur_state;
1193 
1194 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1195 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1196 	I915_STATE_WARN(cur_state != state,
1197 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1198 			onoff(state), onoff(cur_state));
1199 }
1200 
1201 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1202 {
1203 	i915_reg_t pp_reg;
1204 	u32 val;
1205 	enum pipe panel_pipe = INVALID_PIPE;
1206 	bool locked = true;
1207 
1208 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1209 		return;
1210 
1211 	if (HAS_PCH_SPLIT(dev_priv)) {
1212 		u32 port_sel;
1213 
1214 		pp_reg = PP_CONTROL(0);
1215 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1216 
1217 		switch (port_sel) {
1218 		case PANEL_PORT_SELECT_LVDS:
1219 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1220 			break;
1221 		case PANEL_PORT_SELECT_DPA:
1222 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1223 			break;
1224 		case PANEL_PORT_SELECT_DPC:
1225 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1226 			break;
1227 		case PANEL_PORT_SELECT_DPD:
1228 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1229 			break;
1230 		default:
1231 			MISSING_CASE(port_sel);
1232 			break;
1233 		}
1234 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1235 		/* presumably write lock depends on pipe, not port select */
1236 		pp_reg = PP_CONTROL(pipe);
1237 		panel_pipe = pipe;
1238 	} else {
1239 		u32 port_sel;
1240 
1241 		pp_reg = PP_CONTROL(0);
1242 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1243 
1244 		drm_WARN_ON(&dev_priv->drm,
1245 			    port_sel != PANEL_PORT_SELECT_LVDS);
1246 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1247 	}
1248 
1249 	val = intel_de_read(dev_priv, pp_reg);
1250 	if (!(val & PANEL_POWER_ON) ||
1251 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1252 		locked = false;
1253 
1254 	I915_STATE_WARN(panel_pipe == pipe && locked,
1255 	     "panel assertion failure, pipe %c regs locked\n",
1256 	     pipe_name(pipe));
1257 }
1258 
1259 void assert_pipe(struct drm_i915_private *dev_priv,
1260 		 enum transcoder cpu_transcoder, bool state)
1261 {
1262 	bool cur_state;
1263 	enum intel_display_power_domain power_domain;
1264 	intel_wakeref_t wakeref;
1265 
1266 	/* we keep both pipes enabled on 830 */
1267 	if (IS_I830(dev_priv))
1268 		state = true;
1269 
1270 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1271 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1272 	if (wakeref) {
1273 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1274 		cur_state = !!(val & PIPECONF_ENABLE);
1275 
1276 		intel_display_power_put(dev_priv, power_domain, wakeref);
1277 	} else {
1278 		cur_state = false;
1279 	}
1280 
1281 	I915_STATE_WARN(cur_state != state,
1282 			"transcoder %s assertion failure (expected %s, current %s)\n",
1283 			transcoder_name(cpu_transcoder),
1284 			onoff(state), onoff(cur_state));
1285 }
1286 
1287 static void assert_plane(struct intel_plane *plane, bool state)
1288 {
1289 	enum pipe pipe;
1290 	bool cur_state;
1291 
1292 	cur_state = plane->get_hw_state(plane, &pipe);
1293 
1294 	I915_STATE_WARN(cur_state != state,
1295 			"%s assertion failure (expected %s, current %s)\n",
1296 			plane->base.name, onoff(state), onoff(cur_state));
1297 }
1298 
1299 #define assert_plane_enabled(p) assert_plane(p, true)
1300 #define assert_plane_disabled(p) assert_plane(p, false)
1301 
1302 static void assert_planes_disabled(struct intel_crtc *crtc)
1303 {
1304 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1305 	struct intel_plane *plane;
1306 
1307 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1308 		assert_plane_disabled(plane);
1309 }
1310 
1311 static void assert_vblank_disabled(struct drm_crtc *crtc)
1312 {
1313 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1314 		drm_crtc_vblank_put(crtc);
1315 }
1316 
1317 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1318 				    enum pipe pipe)
1319 {
1320 	u32 val;
1321 	bool enabled;
1322 
1323 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
1324 	enabled = !!(val & TRANS_ENABLE);
1325 	I915_STATE_WARN(enabled,
1326 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1327 	     pipe_name(pipe));
1328 }
1329 
1330 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1331 				   enum pipe pipe, enum port port,
1332 				   i915_reg_t dp_reg)
1333 {
1334 	enum pipe port_pipe;
1335 	bool state;
1336 
1337 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1338 
1339 	I915_STATE_WARN(state && port_pipe == pipe,
1340 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
1341 			port_name(port), pipe_name(pipe));
1342 
1343 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1344 			"IBX PCH DP %c still using transcoder B\n",
1345 			port_name(port));
1346 }
1347 
1348 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1349 				     enum pipe pipe, enum port port,
1350 				     i915_reg_t hdmi_reg)
1351 {
1352 	enum pipe port_pipe;
1353 	bool state;
1354 
1355 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1356 
1357 	I915_STATE_WARN(state && port_pipe == pipe,
1358 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1359 			port_name(port), pipe_name(pipe));
1360 
1361 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1362 			"IBX PCH HDMI %c still using transcoder B\n",
1363 			port_name(port));
1364 }
1365 
1366 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1367 				      enum pipe pipe)
1368 {
1369 	enum pipe port_pipe;
1370 
1371 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1372 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1373 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1374 
1375 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1376 			port_pipe == pipe,
1377 			"PCH VGA enabled on transcoder %c, should be disabled\n",
1378 			pipe_name(pipe));
1379 
1380 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1381 			port_pipe == pipe,
1382 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
1383 			pipe_name(pipe));
1384 
1385 	/* PCH SDVOB multiplex with HDMIB */
1386 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1387 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1388 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1389 }
1390 
1391 static void _vlv_enable_pll(struct intel_crtc *crtc,
1392 			    const struct intel_crtc_state *pipe_config)
1393 {
1394 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1395 	enum pipe pipe = crtc->pipe;
1396 
1397 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1398 	intel_de_posting_read(dev_priv, DPLL(pipe));
1399 	udelay(150);
1400 
1401 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1402 		drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1403 }
1404 
1405 static void vlv_enable_pll(struct intel_crtc *crtc,
1406 			   const struct intel_crtc_state *pipe_config)
1407 {
1408 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1409 	enum pipe pipe = crtc->pipe;
1410 
1411 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1412 
1413 	/* PLL is protected by panel, make sure we can write it */
1414 	assert_panel_unlocked(dev_priv, pipe);
1415 
1416 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1417 		_vlv_enable_pll(crtc, pipe_config);
1418 
1419 	intel_de_write(dev_priv, DPLL_MD(pipe),
1420 		       pipe_config->dpll_hw_state.dpll_md);
1421 	intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1422 }
1423 
1424 
1425 static void _chv_enable_pll(struct intel_crtc *crtc,
1426 			    const struct intel_crtc_state *pipe_config)
1427 {
1428 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1429 	enum pipe pipe = crtc->pipe;
1430 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1431 	u32 tmp;
1432 
1433 	vlv_dpio_get(dev_priv);
1434 
1435 	/* Enable back the 10bit clock to display controller */
1436 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1437 	tmp |= DPIO_DCLKP_EN;
1438 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1439 
1440 	vlv_dpio_put(dev_priv);
1441 
1442 	/*
1443 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1444 	 */
1445 	udelay(1);
1446 
1447 	/* Enable PLL */
1448 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1449 
1450 	/* Check PLL is locked */
1451 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1452 		drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1453 }
1454 
1455 static void chv_enable_pll(struct intel_crtc *crtc,
1456 			   const struct intel_crtc_state *pipe_config)
1457 {
1458 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1459 	enum pipe pipe = crtc->pipe;
1460 
1461 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1462 
1463 	/* PLL is protected by panel, make sure we can write it */
1464 	assert_panel_unlocked(dev_priv, pipe);
1465 
1466 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1467 		_chv_enable_pll(crtc, pipe_config);
1468 
1469 	if (pipe != PIPE_A) {
1470 		/*
1471 		 * WaPixelRepeatModeFixForC0:chv
1472 		 *
1473 		 * DPLLCMD is AWOL. Use chicken bits to propagate
1474 		 * the value from DPLLBMD to either pipe B or C.
1475 		 */
1476 		intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1477 		intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1478 			       pipe_config->dpll_hw_state.dpll_md);
1479 		intel_de_write(dev_priv, CBR4_VLV, 0);
1480 		dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1481 
1482 		/*
1483 		 * DPLLB VGA mode also seems to cause problems.
1484 		 * We should always have it disabled.
1485 		 */
1486 		drm_WARN_ON(&dev_priv->drm,
1487 			    (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1488 			     DPLL_VGA_MODE_DIS) == 0);
1489 	} else {
1490 		intel_de_write(dev_priv, DPLL_MD(pipe),
1491 			       pipe_config->dpll_hw_state.dpll_md);
1492 		intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1493 	}
1494 }
1495 
1496 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1497 {
1498 	if (IS_I830(dev_priv))
1499 		return false;
1500 
1501 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1502 }
1503 
1504 static void i9xx_enable_pll(struct intel_crtc *crtc,
1505 			    const struct intel_crtc_state *crtc_state)
1506 {
1507 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1508 	i915_reg_t reg = DPLL(crtc->pipe);
1509 	u32 dpll = crtc_state->dpll_hw_state.dpll;
1510 	int i;
1511 
1512 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1513 
1514 	/* PLL is protected by panel, make sure we can write it */
1515 	if (i9xx_has_pps(dev_priv))
1516 		assert_panel_unlocked(dev_priv, crtc->pipe);
1517 
1518 	/*
1519 	 * Apparently we need to have VGA mode enabled prior to changing
1520 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1521 	 * dividers, even though the register value does change.
1522 	 */
1523 	intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1524 	intel_de_write(dev_priv, reg, dpll);
1525 
1526 	/* Wait for the clocks to stabilize. */
1527 	intel_de_posting_read(dev_priv, reg);
1528 	udelay(150);
1529 
1530 	if (INTEL_GEN(dev_priv) >= 4) {
1531 		intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1532 			       crtc_state->dpll_hw_state.dpll_md);
1533 	} else {
1534 		/* The pixel multiplier can only be updated once the
1535 		 * DPLL is enabled and the clocks are stable.
1536 		 *
1537 		 * So write it again.
1538 		 */
1539 		intel_de_write(dev_priv, reg, dpll);
1540 	}
1541 
1542 	/* We do this three times for luck */
1543 	for (i = 0; i < 3; i++) {
1544 		intel_de_write(dev_priv, reg, dpll);
1545 		intel_de_posting_read(dev_priv, reg);
1546 		udelay(150); /* wait for warmup */
1547 	}
1548 }
1549 
1550 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1551 {
1552 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1553 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1554 	enum pipe pipe = crtc->pipe;
1555 
1556 	/* Don't disable pipe or pipe PLLs if needed */
1557 	if (IS_I830(dev_priv))
1558 		return;
1559 
1560 	/* Make sure the pipe isn't still relying on us */
1561 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1562 
1563 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1564 	intel_de_posting_read(dev_priv, DPLL(pipe));
1565 }
1566 
1567 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1568 {
1569 	u32 val;
1570 
1571 	/* Make sure the pipe isn't still relying on us */
1572 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1573 
1574 	val = DPLL_INTEGRATED_REF_CLK_VLV |
1575 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1576 	if (pipe != PIPE_A)
1577 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1578 
1579 	intel_de_write(dev_priv, DPLL(pipe), val);
1580 	intel_de_posting_read(dev_priv, DPLL(pipe));
1581 }
1582 
1583 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1584 {
1585 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1586 	u32 val;
1587 
1588 	/* Make sure the pipe isn't still relying on us */
1589 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1590 
1591 	val = DPLL_SSC_REF_CLK_CHV |
1592 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1593 	if (pipe != PIPE_A)
1594 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1595 
1596 	intel_de_write(dev_priv, DPLL(pipe), val);
1597 	intel_de_posting_read(dev_priv, DPLL(pipe));
1598 
1599 	vlv_dpio_get(dev_priv);
1600 
1601 	/* Disable 10bit clock to display controller */
1602 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1603 	val &= ~DPIO_DCLKP_EN;
1604 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1605 
1606 	vlv_dpio_put(dev_priv);
1607 }
1608 
1609 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1610 			 struct intel_digital_port *dport,
1611 			 unsigned int expected_mask)
1612 {
1613 	u32 port_mask;
1614 	i915_reg_t dpll_reg;
1615 
1616 	switch (dport->base.port) {
1617 	case PORT_B:
1618 		port_mask = DPLL_PORTB_READY_MASK;
1619 		dpll_reg = DPLL(0);
1620 		break;
1621 	case PORT_C:
1622 		port_mask = DPLL_PORTC_READY_MASK;
1623 		dpll_reg = DPLL(0);
1624 		expected_mask <<= 4;
1625 		break;
1626 	case PORT_D:
1627 		port_mask = DPLL_PORTD_READY_MASK;
1628 		dpll_reg = DPIO_PHY_STATUS;
1629 		break;
1630 	default:
1631 		BUG();
1632 	}
1633 
1634 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
1635 				       port_mask, expected_mask, 1000))
1636 		drm_WARN(&dev_priv->drm, 1,
1637 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
1638 			 dport->base.base.base.id, dport->base.base.name,
1639 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
1640 			 expected_mask);
1641 }
1642 
1643 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1644 {
1645 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1646 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1647 	enum pipe pipe = crtc->pipe;
1648 	i915_reg_t reg;
1649 	u32 val, pipeconf_val;
1650 
1651 	/* Make sure PCH DPLL is enabled */
1652 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1653 
1654 	/* FDI must be feeding us bits for PCH ports */
1655 	assert_fdi_tx_enabled(dev_priv, pipe);
1656 	assert_fdi_rx_enabled(dev_priv, pipe);
1657 
1658 	if (HAS_PCH_CPT(dev_priv)) {
1659 		reg = TRANS_CHICKEN2(pipe);
1660 		val = intel_de_read(dev_priv, reg);
1661 		/*
1662 		 * Workaround: Set the timing override bit
1663 		 * before enabling the pch transcoder.
1664 		 */
1665 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1666 		/* Configure frame start delay to match the CPU */
1667 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1668 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1669 		intel_de_write(dev_priv, reg, val);
1670 	}
1671 
1672 	reg = PCH_TRANSCONF(pipe);
1673 	val = intel_de_read(dev_priv, reg);
1674 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
1675 
1676 	if (HAS_PCH_IBX(dev_priv)) {
1677 		/* Configure frame start delay to match the CPU */
1678 		val &= ~TRANS_FRAME_START_DELAY_MASK;
1679 		val |= TRANS_FRAME_START_DELAY(0);
1680 
1681 		/*
1682 		 * Make the BPC in transcoder be consistent with
1683 		 * that in pipeconf reg. For HDMI we must use 8bpc
1684 		 * here for both 8bpc and 12bpc.
1685 		 */
1686 		val &= ~PIPECONF_BPC_MASK;
1687 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1688 			val |= PIPECONF_8BPC;
1689 		else
1690 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1691 	}
1692 
1693 	val &= ~TRANS_INTERLACE_MASK;
1694 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1695 		if (HAS_PCH_IBX(dev_priv) &&
1696 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1697 			val |= TRANS_LEGACY_INTERLACED_ILK;
1698 		else
1699 			val |= TRANS_INTERLACED;
1700 	} else {
1701 		val |= TRANS_PROGRESSIVE;
1702 	}
1703 
1704 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
1705 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1706 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1707 			pipe_name(pipe));
1708 }
1709 
1710 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1711 				      enum transcoder cpu_transcoder)
1712 {
1713 	u32 val, pipeconf_val;
1714 
1715 	/* FDI must be feeding us bits for PCH ports */
1716 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1717 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
1718 
1719 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1720 	/* Workaround: set timing override bit. */
1721 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1722 	/* Configure frame start delay to match the CPU */
1723 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1724 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1725 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1726 
1727 	val = TRANS_ENABLE;
1728 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1729 
1730 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1731 	    PIPECONF_INTERLACED_ILK)
1732 		val |= TRANS_INTERLACED;
1733 	else
1734 		val |= TRANS_PROGRESSIVE;
1735 
1736 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1737 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1738 				  TRANS_STATE_ENABLE, 100))
1739 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
1740 }
1741 
1742 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1743 				       enum pipe pipe)
1744 {
1745 	i915_reg_t reg;
1746 	u32 val;
1747 
1748 	/* FDI relies on the transcoder */
1749 	assert_fdi_tx_disabled(dev_priv, pipe);
1750 	assert_fdi_rx_disabled(dev_priv, pipe);
1751 
1752 	/* Ports must be off as well */
1753 	assert_pch_ports_disabled(dev_priv, pipe);
1754 
1755 	reg = PCH_TRANSCONF(pipe);
1756 	val = intel_de_read(dev_priv, reg);
1757 	val &= ~TRANS_ENABLE;
1758 	intel_de_write(dev_priv, reg, val);
1759 	/* wait for PCH transcoder off, transcoder state */
1760 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1761 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1762 			pipe_name(pipe));
1763 
1764 	if (HAS_PCH_CPT(dev_priv)) {
1765 		/* Workaround: Clear the timing override chicken bit again. */
1766 		reg = TRANS_CHICKEN2(pipe);
1767 		val = intel_de_read(dev_priv, reg);
1768 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1769 		intel_de_write(dev_priv, reg, val);
1770 	}
1771 }
1772 
1773 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1774 {
1775 	u32 val;
1776 
1777 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
1778 	val &= ~TRANS_ENABLE;
1779 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1780 	/* wait for PCH transcoder off, transcoder state */
1781 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1782 				    TRANS_STATE_ENABLE, 50))
1783 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
1784 
1785 	/* Workaround: clear timing override bit. */
1786 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1787 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1788 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1789 }
1790 
1791 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1792 {
1793 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1794 
1795 	if (HAS_PCH_LPT(dev_priv))
1796 		return PIPE_A;
1797 	else
1798 		return crtc->pipe;
1799 }
1800 
1801 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1802 {
1803 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1804 
1805 	/*
1806 	 * On i965gm the hardware frame counter reads
1807 	 * zero when the TV encoder is enabled :(
1808 	 */
1809 	if (IS_I965GM(dev_priv) &&
1810 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1811 		return 0;
1812 
1813 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1814 		return 0xffffffff; /* full 32 bit counter */
1815 	else if (INTEL_GEN(dev_priv) >= 3)
1816 		return 0xffffff; /* only 24 bits of frame count */
1817 	else
1818 		return 0; /* Gen2 doesn't have a hardware frame counter */
1819 }
1820 
1821 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1822 {
1823 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1824 
1825 	assert_vblank_disabled(&crtc->base);
1826 	drm_crtc_set_max_vblank_count(&crtc->base,
1827 				      intel_crtc_max_vblank_count(crtc_state));
1828 	drm_crtc_vblank_on(&crtc->base);
1829 }
1830 
1831 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
1832 {
1833 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1834 
1835 	drm_crtc_vblank_off(&crtc->base);
1836 	assert_vblank_disabled(&crtc->base);
1837 }
1838 
1839 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1840 {
1841 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1842 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1843 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1844 	enum pipe pipe = crtc->pipe;
1845 	i915_reg_t reg;
1846 	u32 val;
1847 
1848 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
1849 
1850 	assert_planes_disabled(crtc);
1851 
1852 	/*
1853 	 * A pipe without a PLL won't actually be able to drive bits from
1854 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1855 	 * need the check.
1856 	 */
1857 	if (HAS_GMCH(dev_priv)) {
1858 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1859 			assert_dsi_pll_enabled(dev_priv);
1860 		else
1861 			assert_pll_enabled(dev_priv, pipe);
1862 	} else {
1863 		if (new_crtc_state->has_pch_encoder) {
1864 			/* if driving the PCH, we need FDI enabled */
1865 			assert_fdi_rx_pll_enabled(dev_priv,
1866 						  intel_crtc_pch_transcoder(crtc));
1867 			assert_fdi_tx_pll_enabled(dev_priv,
1868 						  (enum pipe) cpu_transcoder);
1869 		}
1870 		/* FIXME: assert CPU port conditions for SNB+ */
1871 	}
1872 
1873 	trace_intel_pipe_enable(crtc);
1874 
1875 	reg = PIPECONF(cpu_transcoder);
1876 	val = intel_de_read(dev_priv, reg);
1877 	if (val & PIPECONF_ENABLE) {
1878 		/* we keep both pipes enabled on 830 */
1879 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
1880 		return;
1881 	}
1882 
1883 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1884 	intel_de_posting_read(dev_priv, reg);
1885 
1886 	/*
1887 	 * Until the pipe starts PIPEDSL reads will return a stale value,
1888 	 * which causes an apparent vblank timestamp jump when PIPEDSL
1889 	 * resets to its proper value. That also messes up the frame count
1890 	 * when it's derived from the timestamps. So let's wait for the
1891 	 * pipe to start properly before we call drm_crtc_vblank_on()
1892 	 */
1893 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1894 		intel_wait_for_pipe_scanline_moving(crtc);
1895 }
1896 
1897 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1898 {
1899 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1900 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1901 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1902 	enum pipe pipe = crtc->pipe;
1903 	i915_reg_t reg;
1904 	u32 val;
1905 
1906 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1907 
1908 	/*
1909 	 * Make sure planes won't keep trying to pump pixels to us,
1910 	 * or we might hang the display.
1911 	 */
1912 	assert_planes_disabled(crtc);
1913 
1914 	trace_intel_pipe_disable(crtc);
1915 
1916 	reg = PIPECONF(cpu_transcoder);
1917 	val = intel_de_read(dev_priv, reg);
1918 	if ((val & PIPECONF_ENABLE) == 0)
1919 		return;
1920 
1921 	/*
1922 	 * Double wide has implications for planes
1923 	 * so best keep it disabled when not needed.
1924 	 */
1925 	if (old_crtc_state->double_wide)
1926 		val &= ~PIPECONF_DOUBLE_WIDE;
1927 
1928 	/* Don't disable pipe or pipe PLLs if needed */
1929 	if (!IS_I830(dev_priv))
1930 		val &= ~PIPECONF_ENABLE;
1931 
1932 	intel_de_write(dev_priv, reg, val);
1933 	if ((val & PIPECONF_ENABLE) == 0)
1934 		intel_wait_for_pipe_off(old_crtc_state);
1935 }
1936 
1937 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1938 {
1939 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1940 }
1941 
1942 static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1943 {
1944 	if (!is_ccs_modifier(fb->modifier))
1945 		return false;
1946 
1947 	return plane >= fb->format->num_planes / 2;
1948 }
1949 
1950 static bool is_gen12_ccs_modifier(u64 modifier)
1951 {
1952 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1953 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1954 
1955 }
1956 
1957 static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1958 {
1959 	return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1960 }
1961 
1962 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1963 {
1964 	if (is_ccs_modifier(fb->modifier))
1965 		return is_ccs_plane(fb, plane);
1966 
1967 	return plane == 1;
1968 }
1969 
1970 static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1971 {
1972 	WARN_ON(!is_ccs_modifier(fb->modifier) ||
1973 		(main_plane && main_plane >= fb->format->num_planes / 2));
1974 
1975 	return fb->format->num_planes / 2 + main_plane;
1976 }
1977 
1978 static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1979 {
1980 	WARN_ON(!is_ccs_modifier(fb->modifier) ||
1981 		ccs_plane < fb->format->num_planes / 2);
1982 
1983 	return ccs_plane - fb->format->num_planes / 2;
1984 }
1985 
1986 /* Return either the main plane's CCS or - if not a CCS FB - UV plane */
1987 int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
1988 {
1989 	if (is_ccs_modifier(fb->modifier))
1990 		return main_to_ccs_plane(fb, main_plane);
1991 
1992 	return 1;
1993 }
1994 
1995 bool
1996 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
1997 				    uint64_t modifier)
1998 {
1999 	return info->is_yuv &&
2000 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2001 }
2002 
2003 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2004 				   int color_plane)
2005 {
2006 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2007 	       color_plane == 1;
2008 }
2009 
2010 static unsigned int
2011 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
2012 {
2013 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2014 	unsigned int cpp = fb->format->cpp[color_plane];
2015 
2016 	switch (fb->modifier) {
2017 	case DRM_FORMAT_MOD_LINEAR:
2018 		return intel_tile_size(dev_priv);
2019 	case I915_FORMAT_MOD_X_TILED:
2020 		if (IS_GEN(dev_priv, 2))
2021 			return 128;
2022 		else
2023 			return 512;
2024 	case I915_FORMAT_MOD_Y_TILED_CCS:
2025 		if (is_ccs_plane(fb, color_plane))
2026 			return 128;
2027 		/* fall through */
2028 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2029 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2030 		if (is_ccs_plane(fb, color_plane))
2031 			return 64;
2032 		/* fall through */
2033 	case I915_FORMAT_MOD_Y_TILED:
2034 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
2035 			return 128;
2036 		else
2037 			return 512;
2038 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2039 		if (is_ccs_plane(fb, color_plane))
2040 			return 128;
2041 		/* fall through */
2042 	case I915_FORMAT_MOD_Yf_TILED:
2043 		switch (cpp) {
2044 		case 1:
2045 			return 64;
2046 		case 2:
2047 		case 4:
2048 			return 128;
2049 		case 8:
2050 		case 16:
2051 			return 256;
2052 		default:
2053 			MISSING_CASE(cpp);
2054 			return cpp;
2055 		}
2056 		break;
2057 	default:
2058 		MISSING_CASE(fb->modifier);
2059 		return cpp;
2060 	}
2061 }
2062 
2063 static unsigned int
2064 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
2065 {
2066 	if (is_gen12_ccs_plane(fb, color_plane))
2067 		return 1;
2068 
2069 	return intel_tile_size(to_i915(fb->dev)) /
2070 		intel_tile_width_bytes(fb, color_plane);
2071 }
2072 
2073 /* Return the tile dimensions in pixel units */
2074 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
2075 			    unsigned int *tile_width,
2076 			    unsigned int *tile_height)
2077 {
2078 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2079 	unsigned int cpp = fb->format->cpp[color_plane];
2080 
2081 	*tile_width = tile_width_bytes / cpp;
2082 	*tile_height = intel_tile_height(fb, color_plane);
2083 }
2084 
2085 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2086 					int color_plane)
2087 {
2088 	unsigned int tile_width, tile_height;
2089 
2090 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2091 
2092 	return fb->pitches[color_plane] * tile_height;
2093 }
2094 
2095 unsigned int
2096 intel_fb_align_height(const struct drm_framebuffer *fb,
2097 		      int color_plane, unsigned int height)
2098 {
2099 	unsigned int tile_height = intel_tile_height(fb, color_plane);
2100 
2101 	return ALIGN(height, tile_height);
2102 }
2103 
2104 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2105 {
2106 	unsigned int size = 0;
2107 	int i;
2108 
2109 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2110 		size += rot_info->plane[i].width * rot_info->plane[i].height;
2111 
2112 	return size;
2113 }
2114 
2115 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2116 {
2117 	unsigned int size = 0;
2118 	int i;
2119 
2120 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2121 		size += rem_info->plane[i].width * rem_info->plane[i].height;
2122 
2123 	return size;
2124 }
2125 
2126 static void
2127 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2128 			const struct drm_framebuffer *fb,
2129 			unsigned int rotation)
2130 {
2131 	view->type = I915_GGTT_VIEW_NORMAL;
2132 	if (drm_rotation_90_or_270(rotation)) {
2133 		view->type = I915_GGTT_VIEW_ROTATED;
2134 		view->rotated = to_intel_framebuffer(fb)->rot_info;
2135 	}
2136 }
2137 
2138 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2139 {
2140 	if (IS_I830(dev_priv))
2141 		return 16 * 1024;
2142 	else if (IS_I85X(dev_priv))
2143 		return 256;
2144 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2145 		return 32;
2146 	else
2147 		return 4 * 1024;
2148 }
2149 
2150 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2151 {
2152 	if (INTEL_GEN(dev_priv) >= 9)
2153 		return 256 * 1024;
2154 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2155 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2156 		return 128 * 1024;
2157 	else if (INTEL_GEN(dev_priv) >= 4)
2158 		return 4 * 1024;
2159 	else
2160 		return 0;
2161 }
2162 
2163 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2164 					 int color_plane)
2165 {
2166 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2167 
2168 	/* AUX_DIST needs only 4K alignment */
2169 	if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2170 	    is_ccs_plane(fb, color_plane))
2171 		return 4096;
2172 
2173 	switch (fb->modifier) {
2174 	case DRM_FORMAT_MOD_LINEAR:
2175 		return intel_linear_alignment(dev_priv);
2176 	case I915_FORMAT_MOD_X_TILED:
2177 		if (INTEL_GEN(dev_priv) >= 9)
2178 			return 256 * 1024;
2179 		return 0;
2180 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2181 		if (is_semiplanar_uv_plane(fb, color_plane))
2182 			return intel_tile_row_size(fb, color_plane);
2183 		/* Fall-through */
2184 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2185 		return 16 * 1024;
2186 	case I915_FORMAT_MOD_Y_TILED_CCS:
2187 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2188 	case I915_FORMAT_MOD_Y_TILED:
2189 		if (INTEL_GEN(dev_priv) >= 12 &&
2190 		    is_semiplanar_uv_plane(fb, color_plane))
2191 			return intel_tile_row_size(fb, color_plane);
2192 		/* Fall-through */
2193 	case I915_FORMAT_MOD_Yf_TILED:
2194 		return 1 * 1024 * 1024;
2195 	default:
2196 		MISSING_CASE(fb->modifier);
2197 		return 0;
2198 	}
2199 }
2200 
2201 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2202 {
2203 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2204 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2205 
2206 	return INTEL_GEN(dev_priv) < 4 ||
2207 		(plane->has_fbc &&
2208 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2209 }
2210 
2211 struct i915_vma *
2212 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2213 			   const struct i915_ggtt_view *view,
2214 			   bool uses_fence,
2215 			   unsigned long *out_flags)
2216 {
2217 	struct drm_device *dev = fb->dev;
2218 	struct drm_i915_private *dev_priv = to_i915(dev);
2219 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2220 	intel_wakeref_t wakeref;
2221 	struct i915_vma *vma;
2222 	unsigned int pinctl;
2223 	u32 alignment;
2224 
2225 	if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
2226 		return ERR_PTR(-EINVAL);
2227 
2228 	alignment = intel_surf_alignment(fb, 0);
2229 	if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
2230 		return ERR_PTR(-EINVAL);
2231 
2232 	/* Note that the w/a also requires 64 PTE of padding following the
2233 	 * bo. We currently fill all unused PTE with the shadow page and so
2234 	 * we should always have valid PTE following the scanout preventing
2235 	 * the VT-d warning.
2236 	 */
2237 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2238 		alignment = 256 * 1024;
2239 
2240 	/*
2241 	 * Global gtt pte registers are special registers which actually forward
2242 	 * writes to a chunk of system memory. Which means that there is no risk
2243 	 * that the register values disappear as soon as we call
2244 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2245 	 * pin/unpin/fence and not more.
2246 	 */
2247 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2248 
2249 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2250 
2251 	/*
2252 	 * Valleyview is definitely limited to scanning out the first
2253 	 * 512MiB. Lets presume this behaviour was inherited from the
2254 	 * g4x display engine and that all earlier gen are similarly
2255 	 * limited. Testing suggests that it is a little more
2256 	 * complicated than this. For example, Cherryview appears quite
2257 	 * happy to scanout from anywhere within its global aperture.
2258 	 */
2259 	pinctl = 0;
2260 	if (HAS_GMCH(dev_priv))
2261 		pinctl |= PIN_MAPPABLE;
2262 
2263 	vma = i915_gem_object_pin_to_display_plane(obj,
2264 						   alignment, view, pinctl);
2265 	if (IS_ERR(vma))
2266 		goto err;
2267 
2268 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2269 		int ret;
2270 
2271 		/*
2272 		 * Install a fence for tiled scan-out. Pre-i965 always needs a
2273 		 * fence, whereas 965+ only requires a fence if using
2274 		 * framebuffer compression.  For simplicity, we always, when
2275 		 * possible, install a fence as the cost is not that onerous.
2276 		 *
2277 		 * If we fail to fence the tiled scanout, then either the
2278 		 * modeset will reject the change (which is highly unlikely as
2279 		 * the affected systems, all but one, do not have unmappable
2280 		 * space) or we will not be able to enable full powersaving
2281 		 * techniques (also likely not to apply due to various limits
2282 		 * FBC and the like impose on the size of the buffer, which
2283 		 * presumably we violated anyway with this unmappable buffer).
2284 		 * Anyway, it is presumably better to stumble onwards with
2285 		 * something and try to run the system in a "less than optimal"
2286 		 * mode that matches the user configuration.
2287 		 */
2288 		ret = i915_vma_pin_fence(vma);
2289 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2290 			i915_gem_object_unpin_from_display_plane(vma);
2291 			vma = ERR_PTR(ret);
2292 			goto err;
2293 		}
2294 
2295 		if (ret == 0 && vma->fence)
2296 			*out_flags |= PLANE_HAS_FENCE;
2297 	}
2298 
2299 	i915_vma_get(vma);
2300 err:
2301 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2302 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2303 	return vma;
2304 }
2305 
2306 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2307 {
2308 	i915_gem_object_lock(vma->obj);
2309 	if (flags & PLANE_HAS_FENCE)
2310 		i915_vma_unpin_fence(vma);
2311 	i915_gem_object_unpin_from_display_plane(vma);
2312 	i915_gem_object_unlock(vma->obj);
2313 
2314 	i915_vma_put(vma);
2315 }
2316 
2317 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2318 			  unsigned int rotation)
2319 {
2320 	if (drm_rotation_90_or_270(rotation))
2321 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2322 	else
2323 		return fb->pitches[color_plane];
2324 }
2325 
2326 /*
2327  * Convert the x/y offsets into a linear offset.
2328  * Only valid with 0/180 degree rotation, which is fine since linear
2329  * offset is only used with linear buffers on pre-hsw and tiled buffers
2330  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2331  */
2332 u32 intel_fb_xy_to_linear(int x, int y,
2333 			  const struct intel_plane_state *state,
2334 			  int color_plane)
2335 {
2336 	const struct drm_framebuffer *fb = state->hw.fb;
2337 	unsigned int cpp = fb->format->cpp[color_plane];
2338 	unsigned int pitch = state->color_plane[color_plane].stride;
2339 
2340 	return y * pitch + x * cpp;
2341 }
2342 
2343 /*
2344  * Add the x/y offsets derived from fb->offsets[] to the user
2345  * specified plane src x/y offsets. The resulting x/y offsets
2346  * specify the start of scanout from the beginning of the gtt mapping.
2347  */
2348 void intel_add_fb_offsets(int *x, int *y,
2349 			  const struct intel_plane_state *state,
2350 			  int color_plane)
2351 
2352 {
2353 	*x += state->color_plane[color_plane].x;
2354 	*y += state->color_plane[color_plane].y;
2355 }
2356 
2357 static u32 intel_adjust_tile_offset(int *x, int *y,
2358 				    unsigned int tile_width,
2359 				    unsigned int tile_height,
2360 				    unsigned int tile_size,
2361 				    unsigned int pitch_tiles,
2362 				    u32 old_offset,
2363 				    u32 new_offset)
2364 {
2365 	unsigned int pitch_pixels = pitch_tiles * tile_width;
2366 	unsigned int tiles;
2367 
2368 	WARN_ON(old_offset & (tile_size - 1));
2369 	WARN_ON(new_offset & (tile_size - 1));
2370 	WARN_ON(new_offset > old_offset);
2371 
2372 	tiles = (old_offset - new_offset) / tile_size;
2373 
2374 	*y += tiles / pitch_tiles * tile_height;
2375 	*x += tiles % pitch_tiles * tile_width;
2376 
2377 	/* minimize x in case it got needlessly big */
2378 	*y += *x / pitch_pixels * tile_height;
2379 	*x %= pitch_pixels;
2380 
2381 	return new_offset;
2382 }
2383 
2384 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2385 {
2386 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2387 	       is_gen12_ccs_plane(fb, color_plane);
2388 }
2389 
2390 static u32 intel_adjust_aligned_offset(int *x, int *y,
2391 				       const struct drm_framebuffer *fb,
2392 				       int color_plane,
2393 				       unsigned int rotation,
2394 				       unsigned int pitch,
2395 				       u32 old_offset, u32 new_offset)
2396 {
2397 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2398 	unsigned int cpp = fb->format->cpp[color_plane];
2399 
2400 	drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
2401 
2402 	if (!is_surface_linear(fb, color_plane)) {
2403 		unsigned int tile_size, tile_width, tile_height;
2404 		unsigned int pitch_tiles;
2405 
2406 		tile_size = intel_tile_size(dev_priv);
2407 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2408 
2409 		if (drm_rotation_90_or_270(rotation)) {
2410 			pitch_tiles = pitch / tile_height;
2411 			swap(tile_width, tile_height);
2412 		} else {
2413 			pitch_tiles = pitch / (tile_width * cpp);
2414 		}
2415 
2416 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2417 					 tile_size, pitch_tiles,
2418 					 old_offset, new_offset);
2419 	} else {
2420 		old_offset += *y * pitch + *x * cpp;
2421 
2422 		*y = (old_offset - new_offset) / pitch;
2423 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
2424 	}
2425 
2426 	return new_offset;
2427 }
2428 
2429 /*
2430  * Adjust the tile offset by moving the difference into
2431  * the x/y offsets.
2432  */
2433 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2434 					     const struct intel_plane_state *state,
2435 					     int color_plane,
2436 					     u32 old_offset, u32 new_offset)
2437 {
2438 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2439 					   state->hw.rotation,
2440 					   state->color_plane[color_plane].stride,
2441 					   old_offset, new_offset);
2442 }
2443 
2444 /*
2445  * Computes the aligned offset to the base tile and adjusts
2446  * x, y. bytes per pixel is assumed to be a power-of-two.
2447  *
2448  * In the 90/270 rotated case, x and y are assumed
2449  * to be already rotated to match the rotated GTT view, and
2450  * pitch is the tile_height aligned framebuffer height.
2451  *
2452  * This function is used when computing the derived information
2453  * under intel_framebuffer, so using any of that information
2454  * here is not allowed. Anything under drm_framebuffer can be
2455  * used. This is why the user has to pass in the pitch since it
2456  * is specified in the rotated orientation.
2457  */
2458 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2459 					int *x, int *y,
2460 					const struct drm_framebuffer *fb,
2461 					int color_plane,
2462 					unsigned int pitch,
2463 					unsigned int rotation,
2464 					u32 alignment)
2465 {
2466 	unsigned int cpp = fb->format->cpp[color_plane];
2467 	u32 offset, offset_aligned;
2468 
2469 	if (!is_surface_linear(fb, color_plane)) {
2470 		unsigned int tile_size, tile_width, tile_height;
2471 		unsigned int tile_rows, tiles, pitch_tiles;
2472 
2473 		tile_size = intel_tile_size(dev_priv);
2474 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2475 
2476 		if (drm_rotation_90_or_270(rotation)) {
2477 			pitch_tiles = pitch / tile_height;
2478 			swap(tile_width, tile_height);
2479 		} else {
2480 			pitch_tiles = pitch / (tile_width * cpp);
2481 		}
2482 
2483 		tile_rows = *y / tile_height;
2484 		*y %= tile_height;
2485 
2486 		tiles = *x / tile_width;
2487 		*x %= tile_width;
2488 
2489 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2490 
2491 		offset_aligned = offset;
2492 		if (alignment)
2493 			offset_aligned = rounddown(offset_aligned, alignment);
2494 
2495 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2496 					 tile_size, pitch_tiles,
2497 					 offset, offset_aligned);
2498 	} else {
2499 		offset = *y * pitch + *x * cpp;
2500 		offset_aligned = offset;
2501 		if (alignment) {
2502 			offset_aligned = rounddown(offset_aligned, alignment);
2503 			*y = (offset % alignment) / pitch;
2504 			*x = ((offset % alignment) - *y * pitch) / cpp;
2505 		} else {
2506 			*y = *x = 0;
2507 		}
2508 	}
2509 
2510 	return offset_aligned;
2511 }
2512 
2513 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2514 					      const struct intel_plane_state *state,
2515 					      int color_plane)
2516 {
2517 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
2518 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2519 	const struct drm_framebuffer *fb = state->hw.fb;
2520 	unsigned int rotation = state->hw.rotation;
2521 	int pitch = state->color_plane[color_plane].stride;
2522 	u32 alignment;
2523 
2524 	if (intel_plane->id == PLANE_CURSOR)
2525 		alignment = intel_cursor_alignment(dev_priv);
2526 	else
2527 		alignment = intel_surf_alignment(fb, color_plane);
2528 
2529 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2530 					    pitch, rotation, alignment);
2531 }
2532 
2533 /* Convert the fb->offset[] into x/y offsets */
2534 static int intel_fb_offset_to_xy(int *x, int *y,
2535 				 const struct drm_framebuffer *fb,
2536 				 int color_plane)
2537 {
2538 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2539 	unsigned int height;
2540 	u32 alignment;
2541 
2542 	if (INTEL_GEN(dev_priv) >= 12 &&
2543 	    is_semiplanar_uv_plane(fb, color_plane))
2544 		alignment = intel_tile_row_size(fb, color_plane);
2545 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2546 		alignment = intel_tile_size(dev_priv);
2547 	else
2548 		alignment = 0;
2549 
2550 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
2551 		drm_dbg_kms(&dev_priv->drm,
2552 			    "Misaligned offset 0x%08x for color plane %d\n",
2553 			    fb->offsets[color_plane], color_plane);
2554 		return -EINVAL;
2555 	}
2556 
2557 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2558 	height = ALIGN(height, intel_tile_height(fb, color_plane));
2559 
2560 	/* Catch potential overflows early */
2561 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2562 			    fb->offsets[color_plane])) {
2563 		drm_dbg_kms(&dev_priv->drm,
2564 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
2565 			    fb->offsets[color_plane], fb->pitches[color_plane],
2566 			    color_plane);
2567 		return -ERANGE;
2568 	}
2569 
2570 	*x = 0;
2571 	*y = 0;
2572 
2573 	intel_adjust_aligned_offset(x, y,
2574 				    fb, color_plane, DRM_MODE_ROTATE_0,
2575 				    fb->pitches[color_plane],
2576 				    fb->offsets[color_plane], 0);
2577 
2578 	return 0;
2579 }
2580 
2581 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2582 {
2583 	switch (fb_modifier) {
2584 	case I915_FORMAT_MOD_X_TILED:
2585 		return I915_TILING_X;
2586 	case I915_FORMAT_MOD_Y_TILED:
2587 	case I915_FORMAT_MOD_Y_TILED_CCS:
2588 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2589 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2590 		return I915_TILING_Y;
2591 	default:
2592 		return I915_TILING_NONE;
2593 	}
2594 }
2595 
2596 /*
2597  * From the Sky Lake PRM:
2598  * "The Color Control Surface (CCS) contains the compression status of
2599  *  the cache-line pairs. The compression state of the cache-line pair
2600  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2601  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2602  *  cache-line-pairs. CCS is always Y tiled."
2603  *
2604  * Since cache line pairs refers to horizontally adjacent cache lines,
2605  * each cache line in the CCS corresponds to an area of 32x16 cache
2606  * lines on the main surface. Since each pixel is 4 bytes, this gives
2607  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2608  * main surface.
2609  */
2610 static const struct drm_format_info skl_ccs_formats[] = {
2611 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2612 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2613 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2614 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2615 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2616 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2617 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2618 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2619 };
2620 
2621 /*
2622  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2623  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2624  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2625  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2626  * the main surface.
2627  */
2628 static const struct drm_format_info gen12_ccs_formats[] = {
2629 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2630 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2631 	  .hsub = 1, .vsub = 1, },
2632 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2633 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2634 	  .hsub = 1, .vsub = 1, },
2635 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2636 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2637 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2638 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2639 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2640 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2641 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
2642 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2643 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2644 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
2645 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2646 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2647 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
2648 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2649 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2650 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
2651 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2652 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2653 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
2654 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2655 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2656 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
2657 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2658 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2659 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
2660 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2661 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2662 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
2663 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2664 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2665 };
2666 
2667 static const struct drm_format_info *
2668 lookup_format_info(const struct drm_format_info formats[],
2669 		   int num_formats, u32 format)
2670 {
2671 	int i;
2672 
2673 	for (i = 0; i < num_formats; i++) {
2674 		if (formats[i].format == format)
2675 			return &formats[i];
2676 	}
2677 
2678 	return NULL;
2679 }
2680 
2681 static const struct drm_format_info *
2682 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2683 {
2684 	switch (cmd->modifier[0]) {
2685 	case I915_FORMAT_MOD_Y_TILED_CCS:
2686 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2687 		return lookup_format_info(skl_ccs_formats,
2688 					  ARRAY_SIZE(skl_ccs_formats),
2689 					  cmd->pixel_format);
2690 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2691 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2692 		return lookup_format_info(gen12_ccs_formats,
2693 					  ARRAY_SIZE(gen12_ccs_formats),
2694 					  cmd->pixel_format);
2695 	default:
2696 		return NULL;
2697 	}
2698 }
2699 
2700 bool is_ccs_modifier(u64 modifier)
2701 {
2702 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2703 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
2704 	       modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2705 	       modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2706 }
2707 
2708 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2709 {
2710 	return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2711 			    512) * 64;
2712 }
2713 
2714 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2715 			      u32 pixel_format, u64 modifier)
2716 {
2717 	struct intel_crtc *crtc;
2718 	struct intel_plane *plane;
2719 
2720 	/*
2721 	 * We assume the primary plane for pipe A has
2722 	 * the highest stride limits of them all,
2723 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
2724 	 */
2725 	crtc = intel_get_first_crtc(dev_priv);
2726 	if (!crtc)
2727 		return 0;
2728 
2729 	plane = to_intel_plane(crtc->base.primary);
2730 
2731 	return plane->max_stride(plane, pixel_format, modifier,
2732 				 DRM_MODE_ROTATE_0);
2733 }
2734 
2735 static
2736 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2737 			u32 pixel_format, u64 modifier)
2738 {
2739 	/*
2740 	 * Arbitrary limit for gen4+ chosen to match the
2741 	 * render engine max stride.
2742 	 *
2743 	 * The new CCS hash mode makes remapping impossible
2744 	 */
2745 	if (!is_ccs_modifier(modifier)) {
2746 		if (INTEL_GEN(dev_priv) >= 7)
2747 			return 256*1024;
2748 		else if (INTEL_GEN(dev_priv) >= 4)
2749 			return 128*1024;
2750 	}
2751 
2752 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2753 }
2754 
2755 static u32
2756 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2757 {
2758 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2759 	u32 tile_width;
2760 
2761 	if (is_surface_linear(fb, color_plane)) {
2762 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2763 							   fb->format->format,
2764 							   fb->modifier);
2765 
2766 		/*
2767 		 * To make remapping with linear generally feasible
2768 		 * we need the stride to be page aligned.
2769 		 */
2770 		if (fb->pitches[color_plane] > max_stride &&
2771 		    !is_ccs_modifier(fb->modifier))
2772 			return intel_tile_size(dev_priv);
2773 		else
2774 			return 64;
2775 	}
2776 
2777 	tile_width = intel_tile_width_bytes(fb, color_plane);
2778 	if (is_ccs_modifier(fb->modifier)) {
2779 		/*
2780 		 * Display WA #0531: skl,bxt,kbl,glk
2781 		 *
2782 		 * Render decompression and plane width > 3840
2783 		 * combined with horizontal panning requires the
2784 		 * plane stride to be a multiple of 4. We'll just
2785 		 * require the entire fb to accommodate that to avoid
2786 		 * potential runtime errors at plane configuration time.
2787 		 */
2788 		if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
2789 			tile_width *= 4;
2790 		/*
2791 		 * The main surface pitch must be padded to a multiple of four
2792 		 * tile widths.
2793 		 */
2794 		else if (INTEL_GEN(dev_priv) >= 12)
2795 			tile_width *= 4;
2796 	}
2797 	return tile_width;
2798 }
2799 
2800 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2801 {
2802 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2803 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2804 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2805 	int i;
2806 
2807 	/* We don't want to deal with remapping with cursors */
2808 	if (plane->id == PLANE_CURSOR)
2809 		return false;
2810 
2811 	/*
2812 	 * The display engine limits already match/exceed the
2813 	 * render engine limits, so not much point in remapping.
2814 	 * Would also need to deal with the fence POT alignment
2815 	 * and gen2 2KiB GTT tile size.
2816 	 */
2817 	if (INTEL_GEN(dev_priv) < 4)
2818 		return false;
2819 
2820 	/*
2821 	 * The new CCS hash mode isn't compatible with remapping as
2822 	 * the virtual address of the pages affects the compressed data.
2823 	 */
2824 	if (is_ccs_modifier(fb->modifier))
2825 		return false;
2826 
2827 	/* Linear needs a page aligned stride for remapping */
2828 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2829 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
2830 
2831 		for (i = 0; i < fb->format->num_planes; i++) {
2832 			if (fb->pitches[i] & alignment)
2833 				return false;
2834 		}
2835 	}
2836 
2837 	return true;
2838 }
2839 
2840 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2841 {
2842 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2843 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2844 	unsigned int rotation = plane_state->hw.rotation;
2845 	u32 stride, max_stride;
2846 
2847 	/*
2848 	 * No remapping for invisible planes since we don't have
2849 	 * an actual source viewport to remap.
2850 	 */
2851 	if (!plane_state->uapi.visible)
2852 		return false;
2853 
2854 	if (!intel_plane_can_remap(plane_state))
2855 		return false;
2856 
2857 	/*
2858 	 * FIXME: aux plane limits on gen9+ are
2859 	 * unclear in Bspec, for now no checking.
2860 	 */
2861 	stride = intel_fb_pitch(fb, 0, rotation);
2862 	max_stride = plane->max_stride(plane, fb->format->format,
2863 				       fb->modifier, rotation);
2864 
2865 	return stride > max_stride;
2866 }
2867 
2868 static void
2869 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2870 			       const struct drm_framebuffer *fb,
2871 			       int color_plane)
2872 {
2873 	int main_plane;
2874 
2875 	if (color_plane == 0) {
2876 		*hsub = 1;
2877 		*vsub = 1;
2878 
2879 		return;
2880 	}
2881 
2882 	/*
2883 	 * TODO: Deduct the subsampling from the char block for all CCS
2884 	 * formats and planes.
2885 	 */
2886 	if (!is_gen12_ccs_plane(fb, color_plane)) {
2887 		*hsub = fb->format->hsub;
2888 		*vsub = fb->format->vsub;
2889 
2890 		return;
2891 	}
2892 
2893 	main_plane = ccs_to_main_plane(fb, color_plane);
2894 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
2895 		drm_format_info_block_width(fb->format, main_plane);
2896 
2897 	/*
2898 	 * The min stride check in the core framebuffer_check() function
2899 	 * assumes that format->hsub applies to every plane except for the
2900 	 * first plane. That's incorrect for the CCS AUX plane of the first
2901 	 * plane, but for the above check to pass we must define the block
2902 	 * width with that subsampling applied to it. Adjust the width here
2903 	 * accordingly, so we can calculate the actual subsampling factor.
2904 	 */
2905 	if (main_plane == 0)
2906 		*hsub *= fb->format->hsub;
2907 
2908 	*vsub = 32;
2909 }
2910 static int
2911 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2912 {
2913 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2914 	int main_plane;
2915 	int hsub, vsub;
2916 	int tile_width, tile_height;
2917 	int ccs_x, ccs_y;
2918 	int main_x, main_y;
2919 
2920 	if (!is_ccs_plane(fb, ccs_plane))
2921 		return 0;
2922 
2923 	intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2924 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2925 
2926 	tile_width *= hsub;
2927 	tile_height *= vsub;
2928 
2929 	ccs_x = (x * hsub) % tile_width;
2930 	ccs_y = (y * vsub) % tile_height;
2931 
2932 	main_plane = ccs_to_main_plane(fb, ccs_plane);
2933 	main_x = intel_fb->normal[main_plane].x % tile_width;
2934 	main_y = intel_fb->normal[main_plane].y % tile_height;
2935 
2936 	/*
2937 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2938 	 * x/y offsets must match between CCS and the main surface.
2939 	 */
2940 	if (main_x != ccs_x || main_y != ccs_y) {
2941 		DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2942 			      main_x, main_y,
2943 			      ccs_x, ccs_y,
2944 			      intel_fb->normal[main_plane].x,
2945 			      intel_fb->normal[main_plane].y,
2946 			      x, y);
2947 		return -EINVAL;
2948 	}
2949 
2950 	return 0;
2951 }
2952 
2953 static void
2954 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2955 {
2956 	int main_plane = is_ccs_plane(fb, color_plane) ?
2957 			 ccs_to_main_plane(fb, color_plane) : 0;
2958 	int main_hsub, main_vsub;
2959 	int hsub, vsub;
2960 
2961 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
2962 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
2963 	*w = fb->width / main_hsub / hsub;
2964 	*h = fb->height / main_vsub / vsub;
2965 }
2966 
2967 /*
2968  * Setup the rotated view for an FB plane and return the size the GTT mapping
2969  * requires for this view.
2970  */
2971 static u32
2972 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2973 		  u32 gtt_offset_rotated, int x, int y,
2974 		  unsigned int width, unsigned int height,
2975 		  unsigned int tile_size,
2976 		  unsigned int tile_width, unsigned int tile_height,
2977 		  struct drm_framebuffer *fb)
2978 {
2979 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2980 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2981 	unsigned int pitch_tiles;
2982 	struct drm_rect r;
2983 
2984 	/* Y or Yf modifiers required for 90/270 rotation */
2985 	if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
2986 	    fb->modifier != I915_FORMAT_MOD_Yf_TILED)
2987 		return 0;
2988 
2989 	if (WARN_ON(plane >= ARRAY_SIZE(rot_info->plane)))
2990 		return 0;
2991 
2992 	rot_info->plane[plane] = *plane_info;
2993 
2994 	intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
2995 
2996 	/* rotate the x/y offsets to match the GTT view */
2997 	drm_rect_init(&r, x, y, width, height);
2998 	drm_rect_rotate(&r,
2999 			plane_info->width * tile_width,
3000 			plane_info->height * tile_height,
3001 			DRM_MODE_ROTATE_270);
3002 	x = r.x1;
3003 	y = r.y1;
3004 
3005 	/* rotate the tile dimensions to match the GTT view */
3006 	pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3007 	swap(tile_width, tile_height);
3008 
3009 	/*
3010 	 * We only keep the x/y offsets, so push all of the
3011 	 * gtt offset into the x/y offsets.
3012 	 */
3013 	intel_adjust_tile_offset(&x, &y,
3014 				 tile_width, tile_height,
3015 				 tile_size, pitch_tiles,
3016 				 gtt_offset_rotated * tile_size, 0);
3017 
3018 	/*
3019 	 * First pixel of the framebuffer from
3020 	 * the start of the rotated gtt mapping.
3021 	 */
3022 	intel_fb->rotated[plane].x = x;
3023 	intel_fb->rotated[plane].y = y;
3024 
3025 	return plane_info->width * plane_info->height;
3026 }
3027 
3028 static int
3029 intel_fill_fb_info(struct drm_i915_private *dev_priv,
3030 		   struct drm_framebuffer *fb)
3031 {
3032 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3033 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3034 	u32 gtt_offset_rotated = 0;
3035 	unsigned int max_size = 0;
3036 	int i, num_planes = fb->format->num_planes;
3037 	unsigned int tile_size = intel_tile_size(dev_priv);
3038 
3039 	for (i = 0; i < num_planes; i++) {
3040 		unsigned int width, height;
3041 		unsigned int cpp, size;
3042 		u32 offset;
3043 		int x, y;
3044 		int ret;
3045 
3046 		cpp = fb->format->cpp[i];
3047 		intel_fb_plane_dims(&width, &height, fb, i);
3048 
3049 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3050 		if (ret) {
3051 			drm_dbg_kms(&dev_priv->drm,
3052 				    "bad fb plane %d offset: 0x%x\n",
3053 				    i, fb->offsets[i]);
3054 			return ret;
3055 		}
3056 
3057 		ret = intel_fb_check_ccs_xy(fb, i, x, y);
3058 		if (ret)
3059 			return ret;
3060 
3061 		/*
3062 		 * The fence (if used) is aligned to the start of the object
3063 		 * so having the framebuffer wrap around across the edge of the
3064 		 * fenced region doesn't really work. We have no API to configure
3065 		 * the fence start offset within the object (nor could we probably
3066 		 * on gen2/3). So it's just easier if we just require that the
3067 		 * fb layout agrees with the fence layout. We already check that the
3068 		 * fb stride matches the fence stride elsewhere.
3069 		 */
3070 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
3071 		    (x + width) * cpp > fb->pitches[i]) {
3072 			drm_dbg_kms(&dev_priv->drm,
3073 				    "bad fb plane %d offset: 0x%x\n",
3074 				     i, fb->offsets[i]);
3075 			return -EINVAL;
3076 		}
3077 
3078 		/*
3079 		 * First pixel of the framebuffer from
3080 		 * the start of the normal gtt mapping.
3081 		 */
3082 		intel_fb->normal[i].x = x;
3083 		intel_fb->normal[i].y = y;
3084 
3085 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3086 						      fb->pitches[i],
3087 						      DRM_MODE_ROTATE_0,
3088 						      tile_size);
3089 		offset /= tile_size;
3090 
3091 		if (!is_surface_linear(fb, i)) {
3092 			struct intel_remapped_plane_info plane_info;
3093 			unsigned int tile_width, tile_height;
3094 
3095 			intel_tile_dims(fb, i, &tile_width, &tile_height);
3096 
3097 			plane_info.offset = offset;
3098 			plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3099 							 tile_width * cpp);
3100 			plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3101 			plane_info.height = DIV_ROUND_UP(y + height,
3102 							 tile_height);
3103 
3104 			/* how many tiles does this plane need */
3105 			size = plane_info.stride * plane_info.height;
3106 			/*
3107 			 * If the plane isn't horizontally tile aligned,
3108 			 * we need one more tile.
3109 			 */
3110 			if (x != 0)
3111 				size++;
3112 
3113 			gtt_offset_rotated +=
3114 				setup_fb_rotation(i, &plane_info,
3115 						  gtt_offset_rotated,
3116 						  x, y, width, height,
3117 						  tile_size,
3118 						  tile_width, tile_height,
3119 						  fb);
3120 		} else {
3121 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3122 					    x * cpp, tile_size);
3123 		}
3124 
3125 		/* how many tiles in total needed in the bo */
3126 		max_size = max(max_size, offset + size);
3127 	}
3128 
3129 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
3130 		drm_dbg_kms(&dev_priv->drm,
3131 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3132 			    mul_u32_u32(max_size, tile_size), obj->base.size);
3133 		return -EINVAL;
3134 	}
3135 
3136 	return 0;
3137 }
3138 
3139 static void
3140 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3141 {
3142 	struct drm_i915_private *dev_priv =
3143 		to_i915(plane_state->uapi.plane->dev);
3144 	struct drm_framebuffer *fb = plane_state->hw.fb;
3145 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3146 	struct intel_rotation_info *info = &plane_state->view.rotated;
3147 	unsigned int rotation = plane_state->hw.rotation;
3148 	int i, num_planes = fb->format->num_planes;
3149 	unsigned int tile_size = intel_tile_size(dev_priv);
3150 	unsigned int src_x, src_y;
3151 	unsigned int src_w, src_h;
3152 	u32 gtt_offset = 0;
3153 
3154 	memset(&plane_state->view, 0, sizeof(plane_state->view));
3155 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3156 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3157 
3158 	src_x = plane_state->uapi.src.x1 >> 16;
3159 	src_y = plane_state->uapi.src.y1 >> 16;
3160 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3161 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
3162 
3163 	drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
3164 
3165 	/* Make src coordinates relative to the viewport */
3166 	drm_rect_translate(&plane_state->uapi.src,
3167 			   -(src_x << 16), -(src_y << 16));
3168 
3169 	/* Rotate src coordinates to match rotated GTT view */
3170 	if (drm_rotation_90_or_270(rotation))
3171 		drm_rect_rotate(&plane_state->uapi.src,
3172 				src_w << 16, src_h << 16,
3173 				DRM_MODE_ROTATE_270);
3174 
3175 	for (i = 0; i < num_planes; i++) {
3176 		unsigned int hsub = i ? fb->format->hsub : 1;
3177 		unsigned int vsub = i ? fb->format->vsub : 1;
3178 		unsigned int cpp = fb->format->cpp[i];
3179 		unsigned int tile_width, tile_height;
3180 		unsigned int width, height;
3181 		unsigned int pitch_tiles;
3182 		unsigned int x, y;
3183 		u32 offset;
3184 
3185 		intel_tile_dims(fb, i, &tile_width, &tile_height);
3186 
3187 		x = src_x / hsub;
3188 		y = src_y / vsub;
3189 		width = src_w / hsub;
3190 		height = src_h / vsub;
3191 
3192 		/*
3193 		 * First pixel of the src viewport from the
3194 		 * start of the normal gtt mapping.
3195 		 */
3196 		x += intel_fb->normal[i].x;
3197 		y += intel_fb->normal[i].y;
3198 
3199 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3200 						      fb, i, fb->pitches[i],
3201 						      DRM_MODE_ROTATE_0, tile_size);
3202 		offset /= tile_size;
3203 
3204 		drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
3205 		info->plane[i].offset = offset;
3206 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3207 						     tile_width * cpp);
3208 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3209 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3210 
3211 		if (drm_rotation_90_or_270(rotation)) {
3212 			struct drm_rect r;
3213 
3214 			/* rotate the x/y offsets to match the GTT view */
3215 			drm_rect_init(&r, x, y, width, height);
3216 			drm_rect_rotate(&r,
3217 					info->plane[i].width * tile_width,
3218 					info->plane[i].height * tile_height,
3219 					DRM_MODE_ROTATE_270);
3220 			x = r.x1;
3221 			y = r.y1;
3222 
3223 			pitch_tiles = info->plane[i].height;
3224 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3225 
3226 			/* rotate the tile dimensions to match the GTT view */
3227 			swap(tile_width, tile_height);
3228 		} else {
3229 			pitch_tiles = info->plane[i].width;
3230 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3231 		}
3232 
3233 		/*
3234 		 * We only keep the x/y offsets, so push all of the
3235 		 * gtt offset into the x/y offsets.
3236 		 */
3237 		intel_adjust_tile_offset(&x, &y,
3238 					 tile_width, tile_height,
3239 					 tile_size, pitch_tiles,
3240 					 gtt_offset * tile_size, 0);
3241 
3242 		gtt_offset += info->plane[i].width * info->plane[i].height;
3243 
3244 		plane_state->color_plane[i].offset = 0;
3245 		plane_state->color_plane[i].x = x;
3246 		plane_state->color_plane[i].y = y;
3247 	}
3248 }
3249 
3250 static int
3251 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3252 {
3253 	const struct intel_framebuffer *fb =
3254 		to_intel_framebuffer(plane_state->hw.fb);
3255 	unsigned int rotation = plane_state->hw.rotation;
3256 	int i, num_planes;
3257 
3258 	if (!fb)
3259 		return 0;
3260 
3261 	num_planes = fb->base.format->num_planes;
3262 
3263 	if (intel_plane_needs_remap(plane_state)) {
3264 		intel_plane_remap_gtt(plane_state);
3265 
3266 		/*
3267 		 * Sometimes even remapping can't overcome
3268 		 * the stride limitations :( Can happen with
3269 		 * big plane sizes and suitably misaligned
3270 		 * offsets.
3271 		 */
3272 		return intel_plane_check_stride(plane_state);
3273 	}
3274 
3275 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3276 
3277 	for (i = 0; i < num_planes; i++) {
3278 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3279 		plane_state->color_plane[i].offset = 0;
3280 
3281 		if (drm_rotation_90_or_270(rotation)) {
3282 			plane_state->color_plane[i].x = fb->rotated[i].x;
3283 			plane_state->color_plane[i].y = fb->rotated[i].y;
3284 		} else {
3285 			plane_state->color_plane[i].x = fb->normal[i].x;
3286 			plane_state->color_plane[i].y = fb->normal[i].y;
3287 		}
3288 	}
3289 
3290 	/* Rotate src coordinates to match rotated GTT view */
3291 	if (drm_rotation_90_or_270(rotation))
3292 		drm_rect_rotate(&plane_state->uapi.src,
3293 				fb->base.width << 16, fb->base.height << 16,
3294 				DRM_MODE_ROTATE_270);
3295 
3296 	return intel_plane_check_stride(plane_state);
3297 }
3298 
3299 static int i9xx_format_to_fourcc(int format)
3300 {
3301 	switch (format) {
3302 	case DISPPLANE_8BPP:
3303 		return DRM_FORMAT_C8;
3304 	case DISPPLANE_BGRA555:
3305 		return DRM_FORMAT_ARGB1555;
3306 	case DISPPLANE_BGRX555:
3307 		return DRM_FORMAT_XRGB1555;
3308 	case DISPPLANE_BGRX565:
3309 		return DRM_FORMAT_RGB565;
3310 	default:
3311 	case DISPPLANE_BGRX888:
3312 		return DRM_FORMAT_XRGB8888;
3313 	case DISPPLANE_RGBX888:
3314 		return DRM_FORMAT_XBGR8888;
3315 	case DISPPLANE_BGRA888:
3316 		return DRM_FORMAT_ARGB8888;
3317 	case DISPPLANE_RGBA888:
3318 		return DRM_FORMAT_ABGR8888;
3319 	case DISPPLANE_BGRX101010:
3320 		return DRM_FORMAT_XRGB2101010;
3321 	case DISPPLANE_RGBX101010:
3322 		return DRM_FORMAT_XBGR2101010;
3323 	case DISPPLANE_BGRA101010:
3324 		return DRM_FORMAT_ARGB2101010;
3325 	case DISPPLANE_RGBA101010:
3326 		return DRM_FORMAT_ABGR2101010;
3327 	case DISPPLANE_RGBX161616:
3328 		return DRM_FORMAT_XBGR16161616F;
3329 	}
3330 }
3331 
3332 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
3333 {
3334 	switch (format) {
3335 	case PLANE_CTL_FORMAT_RGB_565:
3336 		return DRM_FORMAT_RGB565;
3337 	case PLANE_CTL_FORMAT_NV12:
3338 		return DRM_FORMAT_NV12;
3339 	case PLANE_CTL_FORMAT_P010:
3340 		return DRM_FORMAT_P010;
3341 	case PLANE_CTL_FORMAT_P012:
3342 		return DRM_FORMAT_P012;
3343 	case PLANE_CTL_FORMAT_P016:
3344 		return DRM_FORMAT_P016;
3345 	case PLANE_CTL_FORMAT_Y210:
3346 		return DRM_FORMAT_Y210;
3347 	case PLANE_CTL_FORMAT_Y212:
3348 		return DRM_FORMAT_Y212;
3349 	case PLANE_CTL_FORMAT_Y216:
3350 		return DRM_FORMAT_Y216;
3351 	case PLANE_CTL_FORMAT_Y410:
3352 		return DRM_FORMAT_XVYU2101010;
3353 	case PLANE_CTL_FORMAT_Y412:
3354 		return DRM_FORMAT_XVYU12_16161616;
3355 	case PLANE_CTL_FORMAT_Y416:
3356 		return DRM_FORMAT_XVYU16161616;
3357 	default:
3358 	case PLANE_CTL_FORMAT_XRGB_8888:
3359 		if (rgb_order) {
3360 			if (alpha)
3361 				return DRM_FORMAT_ABGR8888;
3362 			else
3363 				return DRM_FORMAT_XBGR8888;
3364 		} else {
3365 			if (alpha)
3366 				return DRM_FORMAT_ARGB8888;
3367 			else
3368 				return DRM_FORMAT_XRGB8888;
3369 		}
3370 	case PLANE_CTL_FORMAT_XRGB_2101010:
3371 		if (rgb_order) {
3372 			if (alpha)
3373 				return DRM_FORMAT_ABGR2101010;
3374 			else
3375 				return DRM_FORMAT_XBGR2101010;
3376 		} else {
3377 			if (alpha)
3378 				return DRM_FORMAT_ARGB2101010;
3379 			else
3380 				return DRM_FORMAT_XRGB2101010;
3381 		}
3382 	case PLANE_CTL_FORMAT_XRGB_16161616F:
3383 		if (rgb_order) {
3384 			if (alpha)
3385 				return DRM_FORMAT_ABGR16161616F;
3386 			else
3387 				return DRM_FORMAT_XBGR16161616F;
3388 		} else {
3389 			if (alpha)
3390 				return DRM_FORMAT_ARGB16161616F;
3391 			else
3392 				return DRM_FORMAT_XRGB16161616F;
3393 		}
3394 	}
3395 }
3396 
3397 static struct i915_vma *
3398 initial_plane_vma(struct drm_i915_private *i915,
3399 		  struct intel_initial_plane_config *plane_config)
3400 {
3401 	struct drm_i915_gem_object *obj;
3402 	struct i915_vma *vma;
3403 	u32 base, size;
3404 
3405 	if (plane_config->size == 0)
3406 		return NULL;
3407 
3408 	base = round_down(plane_config->base,
3409 			  I915_GTT_MIN_ALIGNMENT);
3410 	size = round_up(plane_config->base + plane_config->size,
3411 			I915_GTT_MIN_ALIGNMENT);
3412 	size -= base;
3413 
3414 	/*
3415 	 * If the FB is too big, just don't use it since fbdev is not very
3416 	 * important and we should probably use that space with FBC or other
3417 	 * features.
3418 	 */
3419 	if (size * 2 > i915->stolen_usable_size)
3420 		return NULL;
3421 
3422 	obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3423 	if (IS_ERR(obj))
3424 		return NULL;
3425 
3426 	switch (plane_config->tiling) {
3427 	case I915_TILING_NONE:
3428 		break;
3429 	case I915_TILING_X:
3430 	case I915_TILING_Y:
3431 		obj->tiling_and_stride =
3432 			plane_config->fb->base.pitches[0] |
3433 			plane_config->tiling;
3434 		break;
3435 	default:
3436 		MISSING_CASE(plane_config->tiling);
3437 		goto err_obj;
3438 	}
3439 
3440 	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3441 	if (IS_ERR(vma))
3442 		goto err_obj;
3443 
3444 	if (i915_ggtt_pin(vma, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
3445 		goto err_obj;
3446 
3447 	if (i915_gem_object_is_tiled(obj) &&
3448 	    !i915_vma_is_map_and_fenceable(vma))
3449 		goto err_obj;
3450 
3451 	return vma;
3452 
3453 err_obj:
3454 	i915_gem_object_put(obj);
3455 	return NULL;
3456 }
3457 
3458 static bool
3459 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3460 			      struct intel_initial_plane_config *plane_config)
3461 {
3462 	struct drm_device *dev = crtc->base.dev;
3463 	struct drm_i915_private *dev_priv = to_i915(dev);
3464 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3465 	struct drm_framebuffer *fb = &plane_config->fb->base;
3466 	struct i915_vma *vma;
3467 
3468 	switch (fb->modifier) {
3469 	case DRM_FORMAT_MOD_LINEAR:
3470 	case I915_FORMAT_MOD_X_TILED:
3471 	case I915_FORMAT_MOD_Y_TILED:
3472 		break;
3473 	default:
3474 		drm_dbg(&dev_priv->drm,
3475 			"Unsupported modifier for initial FB: 0x%llx\n",
3476 			fb->modifier);
3477 		return false;
3478 	}
3479 
3480 	vma = initial_plane_vma(dev_priv, plane_config);
3481 	if (!vma)
3482 		return false;
3483 
3484 	mode_cmd.pixel_format = fb->format->format;
3485 	mode_cmd.width = fb->width;
3486 	mode_cmd.height = fb->height;
3487 	mode_cmd.pitches[0] = fb->pitches[0];
3488 	mode_cmd.modifier[0] = fb->modifier;
3489 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3490 
3491 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
3492 				   vma->obj, &mode_cmd)) {
3493 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
3494 		goto err_vma;
3495 	}
3496 
3497 	plane_config->vma = vma;
3498 	return true;
3499 
3500 err_vma:
3501 	i915_vma_put(vma);
3502 	return false;
3503 }
3504 
3505 static void
3506 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3507 			struct intel_plane_state *plane_state,
3508 			bool visible)
3509 {
3510 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3511 
3512 	plane_state->uapi.visible = visible;
3513 
3514 	if (visible)
3515 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
3516 	else
3517 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
3518 }
3519 
3520 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3521 {
3522 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3523 	struct drm_plane *plane;
3524 
3525 	/*
3526 	 * Active_planes aliases if multiple "primary" or cursor planes
3527 	 * have been used on the same (or wrong) pipe. plane_mask uses
3528 	 * unique ids, hence we can use that to reconstruct active_planes.
3529 	 */
3530 	crtc_state->active_planes = 0;
3531 
3532 	drm_for_each_plane_mask(plane, &dev_priv->drm,
3533 				crtc_state->uapi.plane_mask)
3534 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3535 }
3536 
3537 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3538 					 struct intel_plane *plane)
3539 {
3540 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3541 	struct intel_crtc_state *crtc_state =
3542 		to_intel_crtc_state(crtc->base.state);
3543 	struct intel_plane_state *plane_state =
3544 		to_intel_plane_state(plane->base.state);
3545 
3546 	drm_dbg_kms(&dev_priv->drm,
3547 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3548 		    plane->base.base.id, plane->base.name,
3549 		    crtc->base.base.id, crtc->base.name);
3550 
3551 	intel_set_plane_visible(crtc_state, plane_state, false);
3552 	fixup_active_planes(crtc_state);
3553 	crtc_state->data_rate[plane->id] = 0;
3554 	crtc_state->min_cdclk[plane->id] = 0;
3555 
3556 	if (plane->id == PLANE_PRIMARY)
3557 		hsw_disable_ips(crtc_state);
3558 
3559 	/*
3560 	 * Vblank time updates from the shadow to live plane control register
3561 	 * are blocked if the memory self-refresh mode is active at that
3562 	 * moment. So to make sure the plane gets truly disabled, disable
3563 	 * first the self-refresh mode. The self-refresh enable bit in turn
3564 	 * will be checked/applied by the HW only at the next frame start
3565 	 * event which is after the vblank start event, so we need to have a
3566 	 * wait-for-vblank between disabling the plane and the pipe.
3567 	 */
3568 	if (HAS_GMCH(dev_priv) &&
3569 	    intel_set_memory_cxsr(dev_priv, false))
3570 		intel_wait_for_vblank(dev_priv, crtc->pipe);
3571 
3572 	/*
3573 	 * Gen2 reports pipe underruns whenever all planes are disabled.
3574 	 * So disable underrun reporting before all the planes get disabled.
3575 	 */
3576 	if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3577 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3578 
3579 	intel_disable_plane(plane, crtc_state);
3580 }
3581 
3582 static struct intel_frontbuffer *
3583 to_intel_frontbuffer(struct drm_framebuffer *fb)
3584 {
3585 	return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3586 }
3587 
3588 static void
3589 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3590 			     struct intel_initial_plane_config *plane_config)
3591 {
3592 	struct drm_device *dev = intel_crtc->base.dev;
3593 	struct drm_i915_private *dev_priv = to_i915(dev);
3594 	struct drm_crtc *c;
3595 	struct drm_plane *primary = intel_crtc->base.primary;
3596 	struct drm_plane_state *plane_state = primary->state;
3597 	struct intel_plane *intel_plane = to_intel_plane(primary);
3598 	struct intel_plane_state *intel_state =
3599 		to_intel_plane_state(plane_state);
3600 	struct drm_framebuffer *fb;
3601 	struct i915_vma *vma;
3602 
3603 	if (!plane_config->fb)
3604 		return;
3605 
3606 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3607 		fb = &plane_config->fb->base;
3608 		vma = plane_config->vma;
3609 		goto valid_fb;
3610 	}
3611 
3612 	/*
3613 	 * Failed to alloc the obj, check to see if we should share
3614 	 * an fb with another CRTC instead
3615 	 */
3616 	for_each_crtc(dev, c) {
3617 		struct intel_plane_state *state;
3618 
3619 		if (c == &intel_crtc->base)
3620 			continue;
3621 
3622 		if (!to_intel_crtc(c)->active)
3623 			continue;
3624 
3625 		state = to_intel_plane_state(c->primary->state);
3626 		if (!state->vma)
3627 			continue;
3628 
3629 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
3630 			fb = state->hw.fb;
3631 			vma = state->vma;
3632 			goto valid_fb;
3633 		}
3634 	}
3635 
3636 	/*
3637 	 * We've failed to reconstruct the BIOS FB.  Current display state
3638 	 * indicates that the primary plane is visible, but has a NULL FB,
3639 	 * which will lead to problems later if we don't fix it up.  The
3640 	 * simplest solution is to just disable the primary plane now and
3641 	 * pretend the BIOS never had it enabled.
3642 	 */
3643 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
3644 
3645 	return;
3646 
3647 valid_fb:
3648 	intel_state->hw.rotation = plane_config->rotation;
3649 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
3650 				intel_state->hw.rotation);
3651 	intel_state->color_plane[0].stride =
3652 		intel_fb_pitch(fb, 0, intel_state->hw.rotation);
3653 
3654 	__i915_vma_pin(vma);
3655 	intel_state->vma = i915_vma_get(vma);
3656 	if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3657 		if (vma->fence)
3658 			intel_state->flags |= PLANE_HAS_FENCE;
3659 
3660 	plane_state->src_x = 0;
3661 	plane_state->src_y = 0;
3662 	plane_state->src_w = fb->width << 16;
3663 	plane_state->src_h = fb->height << 16;
3664 
3665 	plane_state->crtc_x = 0;
3666 	plane_state->crtc_y = 0;
3667 	plane_state->crtc_w = fb->width;
3668 	plane_state->crtc_h = fb->height;
3669 
3670 	intel_state->uapi.src = drm_plane_state_src(plane_state);
3671 	intel_state->uapi.dst = drm_plane_state_dest(plane_state);
3672 
3673 	if (plane_config->tiling)
3674 		dev_priv->preserve_bios_swizzle = true;
3675 
3676 	plane_state->fb = fb;
3677 	drm_framebuffer_get(fb);
3678 
3679 	plane_state->crtc = &intel_crtc->base;
3680 	intel_plane_copy_uapi_to_hw_state(intel_state, intel_state);
3681 
3682 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3683 
3684 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3685 		  &to_intel_frontbuffer(fb)->bits);
3686 }
3687 
3688 static int skl_max_plane_width(const struct drm_framebuffer *fb,
3689 			       int color_plane,
3690 			       unsigned int rotation)
3691 {
3692 	int cpp = fb->format->cpp[color_plane];
3693 
3694 	switch (fb->modifier) {
3695 	case DRM_FORMAT_MOD_LINEAR:
3696 	case I915_FORMAT_MOD_X_TILED:
3697 		/*
3698 		 * Validated limit is 4k, but has 5k should
3699 		 * work apart from the following features:
3700 		 * - Ytile (already limited to 4k)
3701 		 * - FP16 (already limited to 4k)
3702 		 * - render compression (already limited to 4k)
3703 		 * - KVMR sprite and cursor (don't care)
3704 		 * - horizontal panning (TODO verify this)
3705 		 * - pipe and plane scaling (TODO verify this)
3706 		 */
3707 		if (cpp == 8)
3708 			return 4096;
3709 		else
3710 			return 5120;
3711 	case I915_FORMAT_MOD_Y_TILED_CCS:
3712 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3713 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
3714 		/* FIXME AUX plane? */
3715 	case I915_FORMAT_MOD_Y_TILED:
3716 	case I915_FORMAT_MOD_Yf_TILED:
3717 		if (cpp == 8)
3718 			return 2048;
3719 		else
3720 			return 4096;
3721 	default:
3722 		MISSING_CASE(fb->modifier);
3723 		return 2048;
3724 	}
3725 }
3726 
3727 static int glk_max_plane_width(const struct drm_framebuffer *fb,
3728 			       int color_plane,
3729 			       unsigned int rotation)
3730 {
3731 	int cpp = fb->format->cpp[color_plane];
3732 
3733 	switch (fb->modifier) {
3734 	case DRM_FORMAT_MOD_LINEAR:
3735 	case I915_FORMAT_MOD_X_TILED:
3736 		if (cpp == 8)
3737 			return 4096;
3738 		else
3739 			return 5120;
3740 	case I915_FORMAT_MOD_Y_TILED_CCS:
3741 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3742 		/* FIXME AUX plane? */
3743 	case I915_FORMAT_MOD_Y_TILED:
3744 	case I915_FORMAT_MOD_Yf_TILED:
3745 		if (cpp == 8)
3746 			return 2048;
3747 		else
3748 			return 5120;
3749 	default:
3750 		MISSING_CASE(fb->modifier);
3751 		return 2048;
3752 	}
3753 }
3754 
3755 static int icl_max_plane_width(const struct drm_framebuffer *fb,
3756 			       int color_plane,
3757 			       unsigned int rotation)
3758 {
3759 	return 5120;
3760 }
3761 
3762 static int skl_max_plane_height(void)
3763 {
3764 	return 4096;
3765 }
3766 
3767 static int icl_max_plane_height(void)
3768 {
3769 	return 4320;
3770 }
3771 
3772 static bool
3773 skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3774 			       int main_x, int main_y, u32 main_offset,
3775 			       int ccs_plane)
3776 {
3777 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3778 	int aux_x = plane_state->color_plane[ccs_plane].x;
3779 	int aux_y = plane_state->color_plane[ccs_plane].y;
3780 	u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3781 	u32 alignment = intel_surf_alignment(fb, ccs_plane);
3782 	int hsub;
3783 	int vsub;
3784 
3785 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3786 	while (aux_offset >= main_offset && aux_y <= main_y) {
3787 		int x, y;
3788 
3789 		if (aux_x == main_x && aux_y == main_y)
3790 			break;
3791 
3792 		if (aux_offset == 0)
3793 			break;
3794 
3795 		x = aux_x / hsub;
3796 		y = aux_y / vsub;
3797 		aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3798 							       plane_state,
3799 							       ccs_plane,
3800 							       aux_offset,
3801 							       aux_offset -
3802 								alignment);
3803 		aux_x = x * hsub + aux_x % hsub;
3804 		aux_y = y * vsub + aux_y % vsub;
3805 	}
3806 
3807 	if (aux_x != main_x || aux_y != main_y)
3808 		return false;
3809 
3810 	plane_state->color_plane[ccs_plane].offset = aux_offset;
3811 	plane_state->color_plane[ccs_plane].x = aux_x;
3812 	plane_state->color_plane[ccs_plane].y = aux_y;
3813 
3814 	return true;
3815 }
3816 
3817 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3818 {
3819 	struct drm_i915_private *dev_priv = to_i915(plane_state->uapi.plane->dev);
3820 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3821 	unsigned int rotation = plane_state->hw.rotation;
3822 	int x = plane_state->uapi.src.x1 >> 16;
3823 	int y = plane_state->uapi.src.y1 >> 16;
3824 	int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3825 	int h = drm_rect_height(&plane_state->uapi.src) >> 16;
3826 	int max_width;
3827 	int max_height;
3828 	u32 alignment;
3829 	u32 offset;
3830 	int aux_plane = intel_main_to_aux_plane(fb, 0);
3831 	u32 aux_offset = plane_state->color_plane[aux_plane].offset;
3832 
3833 	if (INTEL_GEN(dev_priv) >= 11)
3834 		max_width = icl_max_plane_width(fb, 0, rotation);
3835 	else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
3836 		max_width = glk_max_plane_width(fb, 0, rotation);
3837 	else
3838 		max_width = skl_max_plane_width(fb, 0, rotation);
3839 
3840 	if (INTEL_GEN(dev_priv) >= 11)
3841 		max_height = icl_max_plane_height();
3842 	else
3843 		max_height = skl_max_plane_height();
3844 
3845 	if (w > max_width || h > max_height) {
3846 		drm_dbg_kms(&dev_priv->drm,
3847 			    "requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3848 			    w, h, max_width, max_height);
3849 		return -EINVAL;
3850 	}
3851 
3852 	intel_add_fb_offsets(&x, &y, plane_state, 0);
3853 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3854 	alignment = intel_surf_alignment(fb, 0);
3855 	if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
3856 		return -EINVAL;
3857 
3858 	/*
3859 	 * AUX surface offset is specified as the distance from the
3860 	 * main surface offset, and it must be non-negative. Make
3861 	 * sure that is what we will get.
3862 	 */
3863 	if (offset > aux_offset)
3864 		offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3865 							   offset, aux_offset & ~(alignment - 1));
3866 
3867 	/*
3868 	 * When using an X-tiled surface, the plane blows up
3869 	 * if the x offset + width exceed the stride.
3870 	 *
3871 	 * TODO: linear and Y-tiled seem fine, Yf untested,
3872 	 */
3873 	if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3874 		int cpp = fb->format->cpp[0];
3875 
3876 		while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3877 			if (offset == 0) {
3878 				drm_dbg_kms(&dev_priv->drm,
3879 					    "Unable to find suitable display surface offset due to X-tiling\n");
3880 				return -EINVAL;
3881 			}
3882 
3883 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3884 								   offset, offset - alignment);
3885 		}
3886 	}
3887 
3888 	/*
3889 	 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3890 	 * they match with the main surface x/y offsets.
3891 	 */
3892 	if (is_ccs_modifier(fb->modifier)) {
3893 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3894 						       offset, aux_plane)) {
3895 			if (offset == 0)
3896 				break;
3897 
3898 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3899 								   offset, offset - alignment);
3900 		}
3901 
3902 		if (x != plane_state->color_plane[aux_plane].x ||
3903 		    y != plane_state->color_plane[aux_plane].y) {
3904 			drm_dbg_kms(&dev_priv->drm,
3905 				    "Unable to find suitable display surface offset due to CCS\n");
3906 			return -EINVAL;
3907 		}
3908 	}
3909 
3910 	plane_state->color_plane[0].offset = offset;
3911 	plane_state->color_plane[0].x = x;
3912 	plane_state->color_plane[0].y = y;
3913 
3914 	/*
3915 	 * Put the final coordinates back so that the src
3916 	 * coordinate checks will see the right values.
3917 	 */
3918 	drm_rect_translate_to(&plane_state->uapi.src,
3919 			      x << 16, y << 16);
3920 
3921 	return 0;
3922 }
3923 
3924 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3925 {
3926 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
3927 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3928 	unsigned int rotation = plane_state->hw.rotation;
3929 	int uv_plane = 1;
3930 	int max_width = skl_max_plane_width(fb, uv_plane, rotation);
3931 	int max_height = 4096;
3932 	int x = plane_state->uapi.src.x1 >> 17;
3933 	int y = plane_state->uapi.src.y1 >> 17;
3934 	int w = drm_rect_width(&plane_state->uapi.src) >> 17;
3935 	int h = drm_rect_height(&plane_state->uapi.src) >> 17;
3936 	u32 offset;
3937 
3938 	intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
3939 	offset = intel_plane_compute_aligned_offset(&x, &y,
3940 						    plane_state, uv_plane);
3941 
3942 	/* FIXME not quite sure how/if these apply to the chroma plane */
3943 	if (w > max_width || h > max_height) {
3944 		drm_dbg_kms(&i915->drm,
3945 			    "CbCr source size %dx%d too big (limit %dx%d)\n",
3946 			    w, h, max_width, max_height);
3947 		return -EINVAL;
3948 	}
3949 
3950 	if (is_ccs_modifier(fb->modifier)) {
3951 		int ccs_plane = main_to_ccs_plane(fb, uv_plane);
3952 		int aux_offset = plane_state->color_plane[ccs_plane].offset;
3953 		int alignment = intel_surf_alignment(fb, uv_plane);
3954 
3955 		if (offset > aux_offset)
3956 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3957 								   plane_state,
3958 								   uv_plane,
3959 								   offset,
3960 								   aux_offset & ~(alignment - 1));
3961 
3962 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3963 						       offset, ccs_plane)) {
3964 			if (offset == 0)
3965 				break;
3966 
3967 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3968 								   plane_state,
3969 								   uv_plane,
3970 								   offset, offset - alignment);
3971 		}
3972 
3973 		if (x != plane_state->color_plane[ccs_plane].x ||
3974 		    y != plane_state->color_plane[ccs_plane].y) {
3975 			drm_dbg_kms(&i915->drm,
3976 				    "Unable to find suitable display surface offset due to CCS\n");
3977 			return -EINVAL;
3978 		}
3979 	}
3980 
3981 	plane_state->color_plane[uv_plane].offset = offset;
3982 	plane_state->color_plane[uv_plane].x = x;
3983 	plane_state->color_plane[uv_plane].y = y;
3984 
3985 	return 0;
3986 }
3987 
3988 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3989 {
3990 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3991 	int src_x = plane_state->uapi.src.x1 >> 16;
3992 	int src_y = plane_state->uapi.src.y1 >> 16;
3993 	u32 offset;
3994 	int ccs_plane;
3995 
3996 	for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
3997 		int main_hsub, main_vsub;
3998 		int hsub, vsub;
3999 		int x, y;
4000 
4001 		if (!is_ccs_plane(fb, ccs_plane))
4002 			continue;
4003 
4004 		intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
4005 					       ccs_to_main_plane(fb, ccs_plane));
4006 		intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
4007 
4008 		hsub *= main_hsub;
4009 		vsub *= main_vsub;
4010 		x = src_x / hsub;
4011 		y = src_y / vsub;
4012 
4013 		intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
4014 
4015 		offset = intel_plane_compute_aligned_offset(&x, &y,
4016 							    plane_state,
4017 							    ccs_plane);
4018 
4019 		plane_state->color_plane[ccs_plane].offset = offset;
4020 		plane_state->color_plane[ccs_plane].x = (x * hsub +
4021 							 src_x % hsub) /
4022 							main_hsub;
4023 		plane_state->color_plane[ccs_plane].y = (y * vsub +
4024 							 src_y % vsub) /
4025 							main_vsub;
4026 	}
4027 
4028 	return 0;
4029 }
4030 
4031 int skl_check_plane_surface(struct intel_plane_state *plane_state)
4032 {
4033 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4034 	int ret;
4035 	bool needs_aux = false;
4036 
4037 	ret = intel_plane_compute_gtt(plane_state);
4038 	if (ret)
4039 		return ret;
4040 
4041 	if (!plane_state->uapi.visible)
4042 		return 0;
4043 
4044 	/*
4045 	 * Handle the AUX surface first since the main surface setup depends on
4046 	 * it.
4047 	 */
4048 	if (is_ccs_modifier(fb->modifier)) {
4049 		needs_aux = true;
4050 		ret = skl_check_ccs_aux_surface(plane_state);
4051 		if (ret)
4052 			return ret;
4053 	}
4054 
4055 	if (intel_format_info_is_yuv_semiplanar(fb->format,
4056 						fb->modifier)) {
4057 		needs_aux = true;
4058 		ret = skl_check_nv12_aux_surface(plane_state);
4059 		if (ret)
4060 			return ret;
4061 	}
4062 
4063 	if (!needs_aux) {
4064 		int i;
4065 
4066 		for (i = 1; i < fb->format->num_planes; i++) {
4067 			plane_state->color_plane[i].offset = ~0xfff;
4068 			plane_state->color_plane[i].x = 0;
4069 			plane_state->color_plane[i].y = 0;
4070 		}
4071 	}
4072 
4073 	ret = skl_check_main_surface(plane_state);
4074 	if (ret)
4075 		return ret;
4076 
4077 	return 0;
4078 }
4079 
4080 static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4081 			     const struct intel_plane_state *plane_state,
4082 			     unsigned int *num, unsigned int *den)
4083 {
4084 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4085 	unsigned int cpp = fb->format->cpp[0];
4086 
4087 	/*
4088 	 * g4x bspec says 64bpp pixel rate can't exceed 80%
4089 	 * of cdclk when the sprite plane is enabled on the
4090 	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
4091 	 * never allowed to exceed 80% of cdclk. Let's just go
4092 	 * with the ilk/snb limit always.
4093 	 */
4094 	if (cpp == 8) {
4095 		*num = 10;
4096 		*den = 8;
4097 	} else {
4098 		*num = 1;
4099 		*den = 1;
4100 	}
4101 }
4102 
4103 static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4104 				const struct intel_plane_state *plane_state)
4105 {
4106 	unsigned int pixel_rate;
4107 	unsigned int num, den;
4108 
4109 	/*
4110 	 * Note that crtc_state->pixel_rate accounts for both
4111 	 * horizontal and vertical panel fitter downscaling factors.
4112 	 * Pre-HSW bspec tells us to only consider the horizontal
4113 	 * downscaling factor here. We ignore that and just consider
4114 	 * both for simplicity.
4115 	 */
4116 	pixel_rate = crtc_state->pixel_rate;
4117 
4118 	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4119 
4120 	/* two pixels per clock with double wide pipe */
4121 	if (crtc_state->double_wide)
4122 		den *= 2;
4123 
4124 	return DIV_ROUND_UP(pixel_rate * num, den);
4125 }
4126 
4127 unsigned int
4128 i9xx_plane_max_stride(struct intel_plane *plane,
4129 		      u32 pixel_format, u64 modifier,
4130 		      unsigned int rotation)
4131 {
4132 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4133 
4134 	if (!HAS_GMCH(dev_priv)) {
4135 		return 32*1024;
4136 	} else if (INTEL_GEN(dev_priv) >= 4) {
4137 		if (modifier == I915_FORMAT_MOD_X_TILED)
4138 			return 16*1024;
4139 		else
4140 			return 32*1024;
4141 	} else if (INTEL_GEN(dev_priv) >= 3) {
4142 		if (modifier == I915_FORMAT_MOD_X_TILED)
4143 			return 8*1024;
4144 		else
4145 			return 16*1024;
4146 	} else {
4147 		if (plane->i9xx_plane == PLANE_C)
4148 			return 4*1024;
4149 		else
4150 			return 8*1024;
4151 	}
4152 }
4153 
4154 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4155 {
4156 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4157 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4158 	u32 dspcntr = 0;
4159 
4160 	if (crtc_state->gamma_enable)
4161 		dspcntr |= DISPPLANE_GAMMA_ENABLE;
4162 
4163 	if (crtc_state->csc_enable)
4164 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4165 
4166 	if (INTEL_GEN(dev_priv) < 5)
4167 		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4168 
4169 	return dspcntr;
4170 }
4171 
4172 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4173 			  const struct intel_plane_state *plane_state)
4174 {
4175 	struct drm_i915_private *dev_priv =
4176 		to_i915(plane_state->uapi.plane->dev);
4177 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4178 	unsigned int rotation = plane_state->hw.rotation;
4179 	u32 dspcntr;
4180 
4181 	dspcntr = DISPLAY_PLANE_ENABLE;
4182 
4183 	if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4184 	    IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
4185 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
4186 
4187 	switch (fb->format->format) {
4188 	case DRM_FORMAT_C8:
4189 		dspcntr |= DISPPLANE_8BPP;
4190 		break;
4191 	case DRM_FORMAT_XRGB1555:
4192 		dspcntr |= DISPPLANE_BGRX555;
4193 		break;
4194 	case DRM_FORMAT_ARGB1555:
4195 		dspcntr |= DISPPLANE_BGRA555;
4196 		break;
4197 	case DRM_FORMAT_RGB565:
4198 		dspcntr |= DISPPLANE_BGRX565;
4199 		break;
4200 	case DRM_FORMAT_XRGB8888:
4201 		dspcntr |= DISPPLANE_BGRX888;
4202 		break;
4203 	case DRM_FORMAT_XBGR8888:
4204 		dspcntr |= DISPPLANE_RGBX888;
4205 		break;
4206 	case DRM_FORMAT_ARGB8888:
4207 		dspcntr |= DISPPLANE_BGRA888;
4208 		break;
4209 	case DRM_FORMAT_ABGR8888:
4210 		dspcntr |= DISPPLANE_RGBA888;
4211 		break;
4212 	case DRM_FORMAT_XRGB2101010:
4213 		dspcntr |= DISPPLANE_BGRX101010;
4214 		break;
4215 	case DRM_FORMAT_XBGR2101010:
4216 		dspcntr |= DISPPLANE_RGBX101010;
4217 		break;
4218 	case DRM_FORMAT_ARGB2101010:
4219 		dspcntr |= DISPPLANE_BGRA101010;
4220 		break;
4221 	case DRM_FORMAT_ABGR2101010:
4222 		dspcntr |= DISPPLANE_RGBA101010;
4223 		break;
4224 	case DRM_FORMAT_XBGR16161616F:
4225 		dspcntr |= DISPPLANE_RGBX161616;
4226 		break;
4227 	default:
4228 		MISSING_CASE(fb->format->format);
4229 		return 0;
4230 	}
4231 
4232 	if (INTEL_GEN(dev_priv) >= 4 &&
4233 	    fb->modifier == I915_FORMAT_MOD_X_TILED)
4234 		dspcntr |= DISPPLANE_TILED;
4235 
4236 	if (rotation & DRM_MODE_ROTATE_180)
4237 		dspcntr |= DISPPLANE_ROTATE_180;
4238 
4239 	if (rotation & DRM_MODE_REFLECT_X)
4240 		dspcntr |= DISPPLANE_MIRROR;
4241 
4242 	return dspcntr;
4243 }
4244 
4245 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
4246 {
4247 	struct drm_i915_private *dev_priv =
4248 		to_i915(plane_state->uapi.plane->dev);
4249 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4250 	int src_x, src_y, src_w;
4251 	u32 offset;
4252 	int ret;
4253 
4254 	ret = intel_plane_compute_gtt(plane_state);
4255 	if (ret)
4256 		return ret;
4257 
4258 	if (!plane_state->uapi.visible)
4259 		return 0;
4260 
4261 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4262 	src_x = plane_state->uapi.src.x1 >> 16;
4263 	src_y = plane_state->uapi.src.y1 >> 16;
4264 
4265 	/* Undocumented hardware limit on i965/g4x/vlv/chv */
4266 	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4267 		return -EINVAL;
4268 
4269 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
4270 
4271 	if (INTEL_GEN(dev_priv) >= 4)
4272 		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4273 							    plane_state, 0);
4274 	else
4275 		offset = 0;
4276 
4277 	/*
4278 	 * Put the final coordinates back so that the src
4279 	 * coordinate checks will see the right values.
4280 	 */
4281 	drm_rect_translate_to(&plane_state->uapi.src,
4282 			      src_x << 16, src_y << 16);
4283 
4284 	/* HSW/BDW do this automagically in hardware */
4285 	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
4286 		unsigned int rotation = plane_state->hw.rotation;
4287 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4288 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4289 
4290 		if (rotation & DRM_MODE_ROTATE_180) {
4291 			src_x += src_w - 1;
4292 			src_y += src_h - 1;
4293 		} else if (rotation & DRM_MODE_REFLECT_X) {
4294 			src_x += src_w - 1;
4295 		}
4296 	}
4297 
4298 	plane_state->color_plane[0].offset = offset;
4299 	plane_state->color_plane[0].x = src_x;
4300 	plane_state->color_plane[0].y = src_y;
4301 
4302 	return 0;
4303 }
4304 
4305 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4306 {
4307 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4308 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4309 
4310 	if (IS_CHERRYVIEW(dev_priv))
4311 		return i9xx_plane == PLANE_B;
4312 	else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4313 		return false;
4314 	else if (IS_GEN(dev_priv, 4))
4315 		return i9xx_plane == PLANE_C;
4316 	else
4317 		return i9xx_plane == PLANE_B ||
4318 			i9xx_plane == PLANE_C;
4319 }
4320 
4321 static int
4322 i9xx_plane_check(struct intel_crtc_state *crtc_state,
4323 		 struct intel_plane_state *plane_state)
4324 {
4325 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4326 	int ret;
4327 
4328 	ret = chv_plane_check_rotation(plane_state);
4329 	if (ret)
4330 		return ret;
4331 
4332 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
4333 						  &crtc_state->uapi,
4334 						  DRM_PLANE_HELPER_NO_SCALING,
4335 						  DRM_PLANE_HELPER_NO_SCALING,
4336 						  i9xx_plane_has_windowing(plane),
4337 						  true);
4338 	if (ret)
4339 		return ret;
4340 
4341 	ret = i9xx_check_plane_surface(plane_state);
4342 	if (ret)
4343 		return ret;
4344 
4345 	if (!plane_state->uapi.visible)
4346 		return 0;
4347 
4348 	ret = intel_plane_check_src_coordinates(plane_state);
4349 	if (ret)
4350 		return ret;
4351 
4352 	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4353 
4354 	return 0;
4355 }
4356 
4357 static void i9xx_update_plane(struct intel_plane *plane,
4358 			      const struct intel_crtc_state *crtc_state,
4359 			      const struct intel_plane_state *plane_state)
4360 {
4361 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4362 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4363 	u32 linear_offset;
4364 	int x = plane_state->color_plane[0].x;
4365 	int y = plane_state->color_plane[0].y;
4366 	int crtc_x = plane_state->uapi.dst.x1;
4367 	int crtc_y = plane_state->uapi.dst.y1;
4368 	int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4369 	int crtc_h = drm_rect_height(&plane_state->uapi.dst);
4370 	unsigned long irqflags;
4371 	u32 dspaddr_offset;
4372 	u32 dspcntr;
4373 
4374 	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
4375 
4376 	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
4377 
4378 	if (INTEL_GEN(dev_priv) >= 4)
4379 		dspaddr_offset = plane_state->color_plane[0].offset;
4380 	else
4381 		dspaddr_offset = linear_offset;
4382 
4383 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4384 
4385 	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4386 			  plane_state->color_plane[0].stride);
4387 
4388 	if (INTEL_GEN(dev_priv) < 4) {
4389 		/*
4390 		 * PLANE_A doesn't actually have a full window
4391 		 * generator but let's assume we still need to
4392 		 * program whatever is there.
4393 		 */
4394 		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4395 				  (crtc_y << 16) | crtc_x);
4396 		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4397 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4398 	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
4399 		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4400 				  (crtc_y << 16) | crtc_x);
4401 		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4402 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4403 		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
4404 	}
4405 
4406 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
4407 		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4408 				  (y << 16) | x);
4409 	} else if (INTEL_GEN(dev_priv) >= 4) {
4410 		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4411 				  linear_offset);
4412 		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4413 				  (y << 16) | x);
4414 	}
4415 
4416 	/*
4417 	 * The control register self-arms if the plane was previously
4418 	 * disabled. Try to make the plane enable atomic by writing
4419 	 * the control register just before the surface register.
4420 	 */
4421 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4422 	if (INTEL_GEN(dev_priv) >= 4)
4423 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4424 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4425 	else
4426 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4427 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4428 
4429 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4430 }
4431 
4432 static void i9xx_disable_plane(struct intel_plane *plane,
4433 			       const struct intel_crtc_state *crtc_state)
4434 {
4435 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4436 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4437 	unsigned long irqflags;
4438 	u32 dspcntr;
4439 
4440 	/*
4441 	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4442 	 * enable on ilk+ affect the pipe bottom color as
4443 	 * well, so we must configure them even if the plane
4444 	 * is disabled.
4445 	 *
4446 	 * On pre-g4x there is no way to gamma correct the
4447 	 * pipe bottom color but we'll keep on doing this
4448 	 * anyway so that the crtc state readout works correctly.
4449 	 */
4450 	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
4451 
4452 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4453 
4454 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4455 	if (INTEL_GEN(dev_priv) >= 4)
4456 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
4457 	else
4458 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
4459 
4460 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4461 }
4462 
4463 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4464 				    enum pipe *pipe)
4465 {
4466 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4467 	enum intel_display_power_domain power_domain;
4468 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4469 	intel_wakeref_t wakeref;
4470 	bool ret;
4471 	u32 val;
4472 
4473 	/*
4474 	 * Not 100% correct for planes that can move between pipes,
4475 	 * but that's only the case for gen2-4 which don't have any
4476 	 * display power wells.
4477 	 */
4478 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
4479 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4480 	if (!wakeref)
4481 		return false;
4482 
4483 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4484 
4485 	ret = val & DISPLAY_PLANE_ENABLE;
4486 
4487 	if (INTEL_GEN(dev_priv) >= 5)
4488 		*pipe = plane->pipe;
4489 	else
4490 		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4491 			DISPPLANE_SEL_PIPE_SHIFT;
4492 
4493 	intel_display_power_put(dev_priv, power_domain, wakeref);
4494 
4495 	return ret;
4496 }
4497 
4498 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4499 {
4500 	struct drm_device *dev = intel_crtc->base.dev;
4501 	struct drm_i915_private *dev_priv = to_i915(dev);
4502 	unsigned long irqflags;
4503 
4504 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4505 
4506 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4507 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4508 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
4509 
4510 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4511 }
4512 
4513 /*
4514  * This function detaches (aka. unbinds) unused scalers in hardware
4515  */
4516 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
4517 {
4518 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4519 	const struct intel_crtc_scaler_state *scaler_state =
4520 		&crtc_state->scaler_state;
4521 	int i;
4522 
4523 	/* loop through and disable scalers that aren't in use */
4524 	for (i = 0; i < intel_crtc->num_scalers; i++) {
4525 		if (!scaler_state->scalers[i].in_use)
4526 			skl_detach_scaler(intel_crtc, i);
4527 	}
4528 }
4529 
4530 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4531 					  int color_plane, unsigned int rotation)
4532 {
4533 	/*
4534 	 * The stride is either expressed as a multiple of 64 bytes chunks for
4535 	 * linear buffers or in number of tiles for tiled buffers.
4536 	 */
4537 	if (is_surface_linear(fb, color_plane))
4538 		return 64;
4539 	else if (drm_rotation_90_or_270(rotation))
4540 		return intel_tile_height(fb, color_plane);
4541 	else
4542 		return intel_tile_width_bytes(fb, color_plane);
4543 }
4544 
4545 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
4546 		     int color_plane)
4547 {
4548 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4549 	unsigned int rotation = plane_state->hw.rotation;
4550 	u32 stride = plane_state->color_plane[color_plane].stride;
4551 
4552 	if (color_plane >= fb->format->num_planes)
4553 		return 0;
4554 
4555 	return stride / skl_plane_stride_mult(fb, color_plane, rotation);
4556 }
4557 
4558 static u32 skl_plane_ctl_format(u32 pixel_format)
4559 {
4560 	switch (pixel_format) {
4561 	case DRM_FORMAT_C8:
4562 		return PLANE_CTL_FORMAT_INDEXED;
4563 	case DRM_FORMAT_RGB565:
4564 		return PLANE_CTL_FORMAT_RGB_565;
4565 	case DRM_FORMAT_XBGR8888:
4566 	case DRM_FORMAT_ABGR8888:
4567 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
4568 	case DRM_FORMAT_XRGB8888:
4569 	case DRM_FORMAT_ARGB8888:
4570 		return PLANE_CTL_FORMAT_XRGB_8888;
4571 	case DRM_FORMAT_XBGR2101010:
4572 	case DRM_FORMAT_ABGR2101010:
4573 		return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
4574 	case DRM_FORMAT_XRGB2101010:
4575 	case DRM_FORMAT_ARGB2101010:
4576 		return PLANE_CTL_FORMAT_XRGB_2101010;
4577 	case DRM_FORMAT_XBGR16161616F:
4578 	case DRM_FORMAT_ABGR16161616F:
4579 		return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4580 	case DRM_FORMAT_XRGB16161616F:
4581 	case DRM_FORMAT_ARGB16161616F:
4582 		return PLANE_CTL_FORMAT_XRGB_16161616F;
4583 	case DRM_FORMAT_YUYV:
4584 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
4585 	case DRM_FORMAT_YVYU:
4586 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
4587 	case DRM_FORMAT_UYVY:
4588 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
4589 	case DRM_FORMAT_VYUY:
4590 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
4591 	case DRM_FORMAT_NV12:
4592 		return PLANE_CTL_FORMAT_NV12;
4593 	case DRM_FORMAT_P010:
4594 		return PLANE_CTL_FORMAT_P010;
4595 	case DRM_FORMAT_P012:
4596 		return PLANE_CTL_FORMAT_P012;
4597 	case DRM_FORMAT_P016:
4598 		return PLANE_CTL_FORMAT_P016;
4599 	case DRM_FORMAT_Y210:
4600 		return PLANE_CTL_FORMAT_Y210;
4601 	case DRM_FORMAT_Y212:
4602 		return PLANE_CTL_FORMAT_Y212;
4603 	case DRM_FORMAT_Y216:
4604 		return PLANE_CTL_FORMAT_Y216;
4605 	case DRM_FORMAT_XVYU2101010:
4606 		return PLANE_CTL_FORMAT_Y410;
4607 	case DRM_FORMAT_XVYU12_16161616:
4608 		return PLANE_CTL_FORMAT_Y412;
4609 	case DRM_FORMAT_XVYU16161616:
4610 		return PLANE_CTL_FORMAT_Y416;
4611 	default:
4612 		MISSING_CASE(pixel_format);
4613 	}
4614 
4615 	return 0;
4616 }
4617 
4618 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4619 {
4620 	if (!plane_state->hw.fb->format->has_alpha)
4621 		return PLANE_CTL_ALPHA_DISABLE;
4622 
4623 	switch (plane_state->hw.pixel_blend_mode) {
4624 	case DRM_MODE_BLEND_PIXEL_NONE:
4625 		return PLANE_CTL_ALPHA_DISABLE;
4626 	case DRM_MODE_BLEND_PREMULTI:
4627 		return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4628 	case DRM_MODE_BLEND_COVERAGE:
4629 		return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4630 	default:
4631 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4632 		return PLANE_CTL_ALPHA_DISABLE;
4633 	}
4634 }
4635 
4636 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4637 {
4638 	if (!plane_state->hw.fb->format->has_alpha)
4639 		return PLANE_COLOR_ALPHA_DISABLE;
4640 
4641 	switch (plane_state->hw.pixel_blend_mode) {
4642 	case DRM_MODE_BLEND_PIXEL_NONE:
4643 		return PLANE_COLOR_ALPHA_DISABLE;
4644 	case DRM_MODE_BLEND_PREMULTI:
4645 		return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4646 	case DRM_MODE_BLEND_COVERAGE:
4647 		return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4648 	default:
4649 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4650 		return PLANE_COLOR_ALPHA_DISABLE;
4651 	}
4652 }
4653 
4654 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4655 {
4656 	switch (fb_modifier) {
4657 	case DRM_FORMAT_MOD_LINEAR:
4658 		break;
4659 	case I915_FORMAT_MOD_X_TILED:
4660 		return PLANE_CTL_TILED_X;
4661 	case I915_FORMAT_MOD_Y_TILED:
4662 		return PLANE_CTL_TILED_Y;
4663 	case I915_FORMAT_MOD_Y_TILED_CCS:
4664 		return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4665 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4666 		return PLANE_CTL_TILED_Y |
4667 		       PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4668 		       PLANE_CTL_CLEAR_COLOR_DISABLE;
4669 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4670 		return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
4671 	case I915_FORMAT_MOD_Yf_TILED:
4672 		return PLANE_CTL_TILED_YF;
4673 	case I915_FORMAT_MOD_Yf_TILED_CCS:
4674 		return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4675 	default:
4676 		MISSING_CASE(fb_modifier);
4677 	}
4678 
4679 	return 0;
4680 }
4681 
4682 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4683 {
4684 	switch (rotate) {
4685 	case DRM_MODE_ROTATE_0:
4686 		break;
4687 	/*
4688 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4689 	 * while i915 HW rotation is clockwise, thats why this swapping.
4690 	 */
4691 	case DRM_MODE_ROTATE_90:
4692 		return PLANE_CTL_ROTATE_270;
4693 	case DRM_MODE_ROTATE_180:
4694 		return PLANE_CTL_ROTATE_180;
4695 	case DRM_MODE_ROTATE_270:
4696 		return PLANE_CTL_ROTATE_90;
4697 	default:
4698 		MISSING_CASE(rotate);
4699 	}
4700 
4701 	return 0;
4702 }
4703 
4704 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4705 {
4706 	switch (reflect) {
4707 	case 0:
4708 		break;
4709 	case DRM_MODE_REFLECT_X:
4710 		return PLANE_CTL_FLIP_HORIZONTAL;
4711 	case DRM_MODE_REFLECT_Y:
4712 	default:
4713 		MISSING_CASE(reflect);
4714 	}
4715 
4716 	return 0;
4717 }
4718 
4719 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4720 {
4721 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4722 	u32 plane_ctl = 0;
4723 
4724 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4725 		return plane_ctl;
4726 
4727 	if (crtc_state->gamma_enable)
4728 		plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4729 
4730 	if (crtc_state->csc_enable)
4731 		plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4732 
4733 	return plane_ctl;
4734 }
4735 
4736 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4737 		  const struct intel_plane_state *plane_state)
4738 {
4739 	struct drm_i915_private *dev_priv =
4740 		to_i915(plane_state->uapi.plane->dev);
4741 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4742 	unsigned int rotation = plane_state->hw.rotation;
4743 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4744 	u32 plane_ctl;
4745 
4746 	plane_ctl = PLANE_CTL_ENABLE;
4747 
4748 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4749 		plane_ctl |= skl_plane_ctl_alpha(plane_state);
4750 		plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4751 
4752 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4753 			plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4754 
4755 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4756 			plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4757 	}
4758 
4759 	plane_ctl |= skl_plane_ctl_format(fb->format->format);
4760 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4761 	plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4762 
4763 	if (INTEL_GEN(dev_priv) >= 10)
4764 		plane_ctl |= cnl_plane_ctl_flip(rotation &
4765 						DRM_MODE_REFLECT_MASK);
4766 
4767 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
4768 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4769 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
4770 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4771 
4772 	return plane_ctl;
4773 }
4774 
4775 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4776 {
4777 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4778 	u32 plane_color_ctl = 0;
4779 
4780 	if (INTEL_GEN(dev_priv) >= 11)
4781 		return plane_color_ctl;
4782 
4783 	if (crtc_state->gamma_enable)
4784 		plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4785 
4786 	if (crtc_state->csc_enable)
4787 		plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4788 
4789 	return plane_color_ctl;
4790 }
4791 
4792 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4793 			const struct intel_plane_state *plane_state)
4794 {
4795 	struct drm_i915_private *dev_priv =
4796 		to_i915(plane_state->uapi.plane->dev);
4797 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4798 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4799 	u32 plane_color_ctl = 0;
4800 
4801 	plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4802 	plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4803 
4804 	if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4805 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4806 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4807 		else
4808 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
4809 
4810 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4811 			plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4812 	} else if (fb->format->is_yuv) {
4813 		plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4814 	}
4815 
4816 	return plane_color_ctl;
4817 }
4818 
4819 static int
4820 __intel_display_resume(struct drm_device *dev,
4821 		       struct drm_atomic_state *state,
4822 		       struct drm_modeset_acquire_ctx *ctx)
4823 {
4824 	struct drm_crtc_state *crtc_state;
4825 	struct drm_crtc *crtc;
4826 	int i, ret;
4827 
4828 	intel_modeset_setup_hw_state(dev, ctx);
4829 	intel_vga_redisable(to_i915(dev));
4830 
4831 	if (!state)
4832 		return 0;
4833 
4834 	/*
4835 	 * We've duplicated the state, pointers to the old state are invalid.
4836 	 *
4837 	 * Don't attempt to use the old state until we commit the duplicated state.
4838 	 */
4839 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4840 		/*
4841 		 * Force recalculation even if we restore
4842 		 * current state. With fast modeset this may not result
4843 		 * in a modeset when the state is compatible.
4844 		 */
4845 		crtc_state->mode_changed = true;
4846 	}
4847 
4848 	/* ignore any reset values/BIOS leftovers in the WM registers */
4849 	if (!HAS_GMCH(to_i915(dev)))
4850 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
4851 
4852 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4853 
4854 	drm_WARN_ON(dev, ret == -EDEADLK);
4855 	return ret;
4856 }
4857 
4858 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4859 {
4860 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4861 		intel_has_gpu_reset(&dev_priv->gt));
4862 }
4863 
4864 void intel_prepare_reset(struct drm_i915_private *dev_priv)
4865 {
4866 	struct drm_device *dev = &dev_priv->drm;
4867 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4868 	struct drm_atomic_state *state;
4869 	int ret;
4870 
4871 	/* reset doesn't touch the display */
4872 	if (!i915_modparams.force_reset_modeset_test &&
4873 	    !gpu_reset_clobbers_display(dev_priv))
4874 		return;
4875 
4876 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
4877 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4878 	smp_mb__after_atomic();
4879 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4880 
4881 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4882 		drm_dbg_kms(&dev_priv->drm,
4883 			    "Modeset potentially stuck, unbreaking through wedging\n");
4884 		intel_gt_set_wedged(&dev_priv->gt);
4885 	}
4886 
4887 	/*
4888 	 * Need mode_config.mutex so that we don't
4889 	 * trample ongoing ->detect() and whatnot.
4890 	 */
4891 	mutex_lock(&dev->mode_config.mutex);
4892 	drm_modeset_acquire_init(ctx, 0);
4893 	while (1) {
4894 		ret = drm_modeset_lock_all_ctx(dev, ctx);
4895 		if (ret != -EDEADLK)
4896 			break;
4897 
4898 		drm_modeset_backoff(ctx);
4899 	}
4900 	/*
4901 	 * Disabling the crtcs gracefully seems nicer. Also the
4902 	 * g33 docs say we should at least disable all the planes.
4903 	 */
4904 	state = drm_atomic_helper_duplicate_state(dev, ctx);
4905 	if (IS_ERR(state)) {
4906 		ret = PTR_ERR(state);
4907 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4908 			ret);
4909 		return;
4910 	}
4911 
4912 	ret = drm_atomic_helper_disable_all(dev, ctx);
4913 	if (ret) {
4914 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4915 			ret);
4916 		drm_atomic_state_put(state);
4917 		return;
4918 	}
4919 
4920 	dev_priv->modeset_restore_state = state;
4921 	state->acquire_ctx = ctx;
4922 }
4923 
4924 void intel_finish_reset(struct drm_i915_private *dev_priv)
4925 {
4926 	struct drm_device *dev = &dev_priv->drm;
4927 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4928 	struct drm_atomic_state *state;
4929 	int ret;
4930 
4931 	/* reset doesn't touch the display */
4932 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
4933 		return;
4934 
4935 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
4936 	if (!state)
4937 		goto unlock;
4938 
4939 	/* reset doesn't touch the display */
4940 	if (!gpu_reset_clobbers_display(dev_priv)) {
4941 		/* for testing only restore the display */
4942 		ret = __intel_display_resume(dev, state, ctx);
4943 		if (ret)
4944 			drm_err(&dev_priv->drm,
4945 				"Restoring old state failed with %i\n", ret);
4946 	} else {
4947 		/*
4948 		 * The display has been reset as well,
4949 		 * so need a full re-initialization.
4950 		 */
4951 		intel_pps_unlock_regs_wa(dev_priv);
4952 		intel_modeset_init_hw(dev_priv);
4953 		intel_init_clock_gating(dev_priv);
4954 
4955 		spin_lock_irq(&dev_priv->irq_lock);
4956 		if (dev_priv->display.hpd_irq_setup)
4957 			dev_priv->display.hpd_irq_setup(dev_priv);
4958 		spin_unlock_irq(&dev_priv->irq_lock);
4959 
4960 		ret = __intel_display_resume(dev, state, ctx);
4961 		if (ret)
4962 			drm_err(&dev_priv->drm,
4963 				"Restoring old state failed with %i\n", ret);
4964 
4965 		intel_hpd_init(dev_priv);
4966 	}
4967 
4968 	drm_atomic_state_put(state);
4969 unlock:
4970 	drm_modeset_drop_locks(ctx);
4971 	drm_modeset_acquire_fini(ctx);
4972 	mutex_unlock(&dev->mode_config.mutex);
4973 
4974 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4975 }
4976 
4977 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4978 {
4979 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4980 	enum pipe pipe = crtc->pipe;
4981 	u32 tmp;
4982 
4983 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
4984 
4985 	/*
4986 	 * Display WA #1153: icl
4987 	 * enable hardware to bypass the alpha math
4988 	 * and rounding for per-pixel values 00 and 0xff
4989 	 */
4990 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
4991 	/*
4992 	 * Display WA # 1605353570: icl
4993 	 * Set the pixel rounding bit to 1 for allowing
4994 	 * passthrough of Frame buffer pixels unmodified
4995 	 * across pipe
4996 	 */
4997 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
4998 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
4999 }
5000 
5001 static void icl_enable_trans_port_sync(const struct intel_crtc_state *crtc_state)
5002 {
5003 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5004 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5005 	u32 trans_ddi_func_ctl2_val;
5006 	u8 master_select;
5007 
5008 	/*
5009 	 * Configure the master select and enable Transcoder Port Sync for
5010 	 * Slave CRTCs transcoder.
5011 	 */
5012 	if (crtc_state->master_transcoder == INVALID_TRANSCODER)
5013 		return;
5014 
5015 	if (crtc_state->master_transcoder == TRANSCODER_EDP)
5016 		master_select = 0;
5017 	else
5018 		master_select = crtc_state->master_transcoder + 1;
5019 
5020 	/* Set the master select bits for Tranascoder Port Sync */
5021 	trans_ddi_func_ctl2_val = (PORT_SYNC_MODE_MASTER_SELECT(master_select) &
5022 				   PORT_SYNC_MODE_MASTER_SELECT_MASK) <<
5023 		PORT_SYNC_MODE_MASTER_SELECT_SHIFT;
5024 	/* Enable Transcoder Port Sync */
5025 	trans_ddi_func_ctl2_val |= PORT_SYNC_MODE_ENABLE;
5026 
5027 	intel_de_write(dev_priv,
5028 		       TRANS_DDI_FUNC_CTL2(crtc_state->cpu_transcoder),
5029 		       trans_ddi_func_ctl2_val);
5030 }
5031 
5032 static void intel_fdi_normal_train(struct intel_crtc *crtc)
5033 {
5034 	struct drm_device *dev = crtc->base.dev;
5035 	struct drm_i915_private *dev_priv = to_i915(dev);
5036 	enum pipe pipe = crtc->pipe;
5037 	i915_reg_t reg;
5038 	u32 temp;
5039 
5040 	/* enable normal train */
5041 	reg = FDI_TX_CTL(pipe);
5042 	temp = intel_de_read(dev_priv, reg);
5043 	if (IS_IVYBRIDGE(dev_priv)) {
5044 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5045 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
5046 	} else {
5047 		temp &= ~FDI_LINK_TRAIN_NONE;
5048 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
5049 	}
5050 	intel_de_write(dev_priv, reg, temp);
5051 
5052 	reg = FDI_RX_CTL(pipe);
5053 	temp = intel_de_read(dev_priv, reg);
5054 	if (HAS_PCH_CPT(dev_priv)) {
5055 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5056 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5057 	} else {
5058 		temp &= ~FDI_LINK_TRAIN_NONE;
5059 		temp |= FDI_LINK_TRAIN_NONE;
5060 	}
5061 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5062 
5063 	/* wait one idle pattern time */
5064 	intel_de_posting_read(dev_priv, reg);
5065 	udelay(1000);
5066 
5067 	/* IVB wants error correction enabled */
5068 	if (IS_IVYBRIDGE(dev_priv))
5069 		intel_de_write(dev_priv, reg,
5070 		               intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5071 }
5072 
5073 /* The FDI link training functions for ILK/Ibexpeak. */
5074 static void ilk_fdi_link_train(struct intel_crtc *crtc,
5075 			       const struct intel_crtc_state *crtc_state)
5076 {
5077 	struct drm_device *dev = crtc->base.dev;
5078 	struct drm_i915_private *dev_priv = to_i915(dev);
5079 	enum pipe pipe = crtc->pipe;
5080 	i915_reg_t reg;
5081 	u32 temp, tries;
5082 
5083 	/* FDI needs bits from pipe first */
5084 	assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
5085 
5086 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5087 	   for train result */
5088 	reg = FDI_RX_IMR(pipe);
5089 	temp = intel_de_read(dev_priv, reg);
5090 	temp &= ~FDI_RX_SYMBOL_LOCK;
5091 	temp &= ~FDI_RX_BIT_LOCK;
5092 	intel_de_write(dev_priv, reg, temp);
5093 	intel_de_read(dev_priv, reg);
5094 	udelay(150);
5095 
5096 	/* enable CPU FDI TX and PCH FDI RX */
5097 	reg = FDI_TX_CTL(pipe);
5098 	temp = intel_de_read(dev_priv, reg);
5099 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5100 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5101 	temp &= ~FDI_LINK_TRAIN_NONE;
5102 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5103 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5104 
5105 	reg = FDI_RX_CTL(pipe);
5106 	temp = intel_de_read(dev_priv, reg);
5107 	temp &= ~FDI_LINK_TRAIN_NONE;
5108 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5109 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5110 
5111 	intel_de_posting_read(dev_priv, reg);
5112 	udelay(150);
5113 
5114 	/* Ironlake workaround, enable clock pointer after FDI enable*/
5115 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5116 		       FDI_RX_PHASE_SYNC_POINTER_OVR);
5117 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5118 		       FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5119 
5120 	reg = FDI_RX_IIR(pipe);
5121 	for (tries = 0; tries < 5; tries++) {
5122 		temp = intel_de_read(dev_priv, reg);
5123 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5124 
5125 		if ((temp & FDI_RX_BIT_LOCK)) {
5126 			drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
5127 			intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
5128 			break;
5129 		}
5130 	}
5131 	if (tries == 5)
5132 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5133 
5134 	/* Train 2 */
5135 	reg = FDI_TX_CTL(pipe);
5136 	temp = intel_de_read(dev_priv, reg);
5137 	temp &= ~FDI_LINK_TRAIN_NONE;
5138 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5139 	intel_de_write(dev_priv, reg, temp);
5140 
5141 	reg = FDI_RX_CTL(pipe);
5142 	temp = intel_de_read(dev_priv, reg);
5143 	temp &= ~FDI_LINK_TRAIN_NONE;
5144 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5145 	intel_de_write(dev_priv, reg, temp);
5146 
5147 	intel_de_posting_read(dev_priv, reg);
5148 	udelay(150);
5149 
5150 	reg = FDI_RX_IIR(pipe);
5151 	for (tries = 0; tries < 5; tries++) {
5152 		temp = intel_de_read(dev_priv, reg);
5153 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5154 
5155 		if (temp & FDI_RX_SYMBOL_LOCK) {
5156 			intel_de_write(dev_priv, reg,
5157 				       temp | FDI_RX_SYMBOL_LOCK);
5158 			drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
5159 			break;
5160 		}
5161 	}
5162 	if (tries == 5)
5163 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5164 
5165 	drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5166 
5167 }
5168 
5169 static const int snb_b_fdi_train_param[] = {
5170 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5171 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5172 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5173 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5174 };
5175 
5176 /* The FDI link training functions for SNB/Cougarpoint. */
5177 static void gen6_fdi_link_train(struct intel_crtc *crtc,
5178 				const struct intel_crtc_state *crtc_state)
5179 {
5180 	struct drm_device *dev = crtc->base.dev;
5181 	struct drm_i915_private *dev_priv = to_i915(dev);
5182 	enum pipe pipe = crtc->pipe;
5183 	i915_reg_t reg;
5184 	u32 temp, i, retry;
5185 
5186 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5187 	   for train result */
5188 	reg = FDI_RX_IMR(pipe);
5189 	temp = intel_de_read(dev_priv, reg);
5190 	temp &= ~FDI_RX_SYMBOL_LOCK;
5191 	temp &= ~FDI_RX_BIT_LOCK;
5192 	intel_de_write(dev_priv, reg, temp);
5193 
5194 	intel_de_posting_read(dev_priv, reg);
5195 	udelay(150);
5196 
5197 	/* enable CPU FDI TX and PCH FDI RX */
5198 	reg = FDI_TX_CTL(pipe);
5199 	temp = intel_de_read(dev_priv, reg);
5200 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5201 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5202 	temp &= ~FDI_LINK_TRAIN_NONE;
5203 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5204 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5205 	/* SNB-B */
5206 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5207 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5208 
5209 	intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5210 		       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5211 
5212 	reg = FDI_RX_CTL(pipe);
5213 	temp = intel_de_read(dev_priv, reg);
5214 	if (HAS_PCH_CPT(dev_priv)) {
5215 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5216 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5217 	} else {
5218 		temp &= ~FDI_LINK_TRAIN_NONE;
5219 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5220 	}
5221 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5222 
5223 	intel_de_posting_read(dev_priv, reg);
5224 	udelay(150);
5225 
5226 	for (i = 0; i < 4; i++) {
5227 		reg = FDI_TX_CTL(pipe);
5228 		temp = intel_de_read(dev_priv, reg);
5229 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5230 		temp |= snb_b_fdi_train_param[i];
5231 		intel_de_write(dev_priv, reg, temp);
5232 
5233 		intel_de_posting_read(dev_priv, reg);
5234 		udelay(500);
5235 
5236 		for (retry = 0; retry < 5; retry++) {
5237 			reg = FDI_RX_IIR(pipe);
5238 			temp = intel_de_read(dev_priv, reg);
5239 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5240 			if (temp & FDI_RX_BIT_LOCK) {
5241 				intel_de_write(dev_priv, reg,
5242 					       temp | FDI_RX_BIT_LOCK);
5243 				drm_dbg_kms(&dev_priv->drm,
5244 					    "FDI train 1 done.\n");
5245 				break;
5246 			}
5247 			udelay(50);
5248 		}
5249 		if (retry < 5)
5250 			break;
5251 	}
5252 	if (i == 4)
5253 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5254 
5255 	/* Train 2 */
5256 	reg = FDI_TX_CTL(pipe);
5257 	temp = intel_de_read(dev_priv, reg);
5258 	temp &= ~FDI_LINK_TRAIN_NONE;
5259 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5260 	if (IS_GEN(dev_priv, 6)) {
5261 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5262 		/* SNB-B */
5263 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5264 	}
5265 	intel_de_write(dev_priv, reg, temp);
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_2_CPT;
5272 	} else {
5273 		temp &= ~FDI_LINK_TRAIN_NONE;
5274 		temp |= FDI_LINK_TRAIN_PATTERN_2;
5275 	}
5276 	intel_de_write(dev_priv, reg, temp);
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_SYMBOL_LOCK) {
5296 				intel_de_write(dev_priv, reg,
5297 					       temp | FDI_RX_SYMBOL_LOCK);
5298 				drm_dbg_kms(&dev_priv->drm,
5299 					    "FDI train 2 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 2 fail!\n");
5309 
5310 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5311 }
5312 
5313 /* Manual link training for Ivy Bridge A0 parts */
5314 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5315 				      const struct intel_crtc_state *crtc_state)
5316 {
5317 	struct drm_device *dev = crtc->base.dev;
5318 	struct drm_i915_private *dev_priv = to_i915(dev);
5319 	enum pipe pipe = crtc->pipe;
5320 	i915_reg_t reg;
5321 	u32 temp, i, j;
5322 
5323 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5324 	   for train result */
5325 	reg = FDI_RX_IMR(pipe);
5326 	temp = intel_de_read(dev_priv, reg);
5327 	temp &= ~FDI_RX_SYMBOL_LOCK;
5328 	temp &= ~FDI_RX_BIT_LOCK;
5329 	intel_de_write(dev_priv, reg, temp);
5330 
5331 	intel_de_posting_read(dev_priv, reg);
5332 	udelay(150);
5333 
5334 	drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
5335 		    intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
5336 
5337 	/* Try each vswing and preemphasis setting twice before moving on */
5338 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5339 		/* disable first in case we need to retry */
5340 		reg = FDI_TX_CTL(pipe);
5341 		temp = intel_de_read(dev_priv, reg);
5342 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5343 		temp &= ~FDI_TX_ENABLE;
5344 		intel_de_write(dev_priv, reg, temp);
5345 
5346 		reg = FDI_RX_CTL(pipe);
5347 		temp = intel_de_read(dev_priv, reg);
5348 		temp &= ~FDI_LINK_TRAIN_AUTO;
5349 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5350 		temp &= ~FDI_RX_ENABLE;
5351 		intel_de_write(dev_priv, reg, temp);
5352 
5353 		/* enable CPU FDI TX and PCH FDI RX */
5354 		reg = FDI_TX_CTL(pipe);
5355 		temp = intel_de_read(dev_priv, reg);
5356 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
5357 		temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5358 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
5359 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5360 		temp |= snb_b_fdi_train_param[j/2];
5361 		temp |= FDI_COMPOSITE_SYNC;
5362 		intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5363 
5364 		intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5365 			       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5366 
5367 		reg = FDI_RX_CTL(pipe);
5368 		temp = intel_de_read(dev_priv, reg);
5369 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5370 		temp |= FDI_COMPOSITE_SYNC;
5371 		intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5372 
5373 		intel_de_posting_read(dev_priv, reg);
5374 		udelay(1); /* should be 0.5us */
5375 
5376 		for (i = 0; i < 4; i++) {
5377 			reg = FDI_RX_IIR(pipe);
5378 			temp = intel_de_read(dev_priv, reg);
5379 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5380 
5381 			if (temp & FDI_RX_BIT_LOCK ||
5382 			    (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5383 				intel_de_write(dev_priv, reg,
5384 					       temp | FDI_RX_BIT_LOCK);
5385 				drm_dbg_kms(&dev_priv->drm,
5386 					    "FDI train 1 done, level %i.\n",
5387 					    i);
5388 				break;
5389 			}
5390 			udelay(1); /* should be 0.5us */
5391 		}
5392 		if (i == 4) {
5393 			drm_dbg_kms(&dev_priv->drm,
5394 				    "FDI train 1 fail on vswing %d\n", j / 2);
5395 			continue;
5396 		}
5397 
5398 		/* Train 2 */
5399 		reg = FDI_TX_CTL(pipe);
5400 		temp = intel_de_read(dev_priv, reg);
5401 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5402 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
5403 		intel_de_write(dev_priv, reg, temp);
5404 
5405 		reg = FDI_RX_CTL(pipe);
5406 		temp = intel_de_read(dev_priv, reg);
5407 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5408 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5409 		intel_de_write(dev_priv, reg, temp);
5410 
5411 		intel_de_posting_read(dev_priv, reg);
5412 		udelay(2); /* should be 1.5us */
5413 
5414 		for (i = 0; i < 4; i++) {
5415 			reg = FDI_RX_IIR(pipe);
5416 			temp = intel_de_read(dev_priv, reg);
5417 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5418 
5419 			if (temp & FDI_RX_SYMBOL_LOCK ||
5420 			    (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5421 				intel_de_write(dev_priv, reg,
5422 					       temp | FDI_RX_SYMBOL_LOCK);
5423 				drm_dbg_kms(&dev_priv->drm,
5424 					    "FDI train 2 done, level %i.\n",
5425 					    i);
5426 				goto train_done;
5427 			}
5428 			udelay(2); /* should be 1.5us */
5429 		}
5430 		if (i == 4)
5431 			drm_dbg_kms(&dev_priv->drm,
5432 				    "FDI train 2 fail on vswing %d\n", j / 2);
5433 	}
5434 
5435 train_done:
5436 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5437 }
5438 
5439 static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
5440 {
5441 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
5442 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5443 	enum pipe pipe = intel_crtc->pipe;
5444 	i915_reg_t reg;
5445 	u32 temp;
5446 
5447 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5448 	reg = FDI_RX_CTL(pipe);
5449 	temp = intel_de_read(dev_priv, reg);
5450 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
5451 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5452 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5453 	intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5454 
5455 	intel_de_posting_read(dev_priv, reg);
5456 	udelay(200);
5457 
5458 	/* Switch from Rawclk to PCDclk */
5459 	temp = intel_de_read(dev_priv, reg);
5460 	intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5461 
5462 	intel_de_posting_read(dev_priv, reg);
5463 	udelay(200);
5464 
5465 	/* Enable CPU FDI TX PLL, always on for Ironlake */
5466 	reg = FDI_TX_CTL(pipe);
5467 	temp = intel_de_read(dev_priv, reg);
5468 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
5469 		intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5470 
5471 		intel_de_posting_read(dev_priv, reg);
5472 		udelay(100);
5473 	}
5474 }
5475 
5476 static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
5477 {
5478 	struct drm_device *dev = intel_crtc->base.dev;
5479 	struct drm_i915_private *dev_priv = to_i915(dev);
5480 	enum pipe pipe = intel_crtc->pipe;
5481 	i915_reg_t reg;
5482 	u32 temp;
5483 
5484 	/* Switch from PCDclk to Rawclk */
5485 	reg = FDI_RX_CTL(pipe);
5486 	temp = intel_de_read(dev_priv, reg);
5487 	intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
5488 
5489 	/* Disable CPU FDI TX PLL */
5490 	reg = FDI_TX_CTL(pipe);
5491 	temp = intel_de_read(dev_priv, reg);
5492 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
5493 
5494 	intel_de_posting_read(dev_priv, reg);
5495 	udelay(100);
5496 
5497 	reg = FDI_RX_CTL(pipe);
5498 	temp = intel_de_read(dev_priv, reg);
5499 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
5500 
5501 	/* Wait for the clocks to turn off. */
5502 	intel_de_posting_read(dev_priv, reg);
5503 	udelay(100);
5504 }
5505 
5506 static void ilk_fdi_disable(struct intel_crtc *crtc)
5507 {
5508 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5509 	enum pipe pipe = crtc->pipe;
5510 	i915_reg_t reg;
5511 	u32 temp;
5512 
5513 	/* disable CPU FDI tx and PCH FDI rx */
5514 	reg = FDI_TX_CTL(pipe);
5515 	temp = intel_de_read(dev_priv, reg);
5516 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5517 	intel_de_posting_read(dev_priv, reg);
5518 
5519 	reg = FDI_RX_CTL(pipe);
5520 	temp = intel_de_read(dev_priv, reg);
5521 	temp &= ~(0x7 << 16);
5522 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5523 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
5524 
5525 	intel_de_posting_read(dev_priv, reg);
5526 	udelay(100);
5527 
5528 	/* Ironlake workaround, disable clock pointer after downing FDI */
5529 	if (HAS_PCH_IBX(dev_priv))
5530 		intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5531 			       FDI_RX_PHASE_SYNC_POINTER_OVR);
5532 
5533 	/* still set train pattern 1 */
5534 	reg = FDI_TX_CTL(pipe);
5535 	temp = intel_de_read(dev_priv, reg);
5536 	temp &= ~FDI_LINK_TRAIN_NONE;
5537 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5538 	intel_de_write(dev_priv, reg, temp);
5539 
5540 	reg = FDI_RX_CTL(pipe);
5541 	temp = intel_de_read(dev_priv, reg);
5542 	if (HAS_PCH_CPT(dev_priv)) {
5543 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5544 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5545 	} else {
5546 		temp &= ~FDI_LINK_TRAIN_NONE;
5547 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5548 	}
5549 	/* BPC in FDI rx is consistent with that in PIPECONF */
5550 	temp &= ~(0x07 << 16);
5551 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5552 	intel_de_write(dev_priv, reg, temp);
5553 
5554 	intel_de_posting_read(dev_priv, reg);
5555 	udelay(100);
5556 }
5557 
5558 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5559 {
5560 	struct drm_crtc *crtc;
5561 	bool cleanup_done;
5562 
5563 	drm_for_each_crtc(crtc, &dev_priv->drm) {
5564 		struct drm_crtc_commit *commit;
5565 		spin_lock(&crtc->commit_lock);
5566 		commit = list_first_entry_or_null(&crtc->commit_list,
5567 						  struct drm_crtc_commit, commit_entry);
5568 		cleanup_done = commit ?
5569 			try_wait_for_completion(&commit->cleanup_done) : true;
5570 		spin_unlock(&crtc->commit_lock);
5571 
5572 		if (cleanup_done)
5573 			continue;
5574 
5575 		drm_crtc_wait_one_vblank(crtc);
5576 
5577 		return true;
5578 	}
5579 
5580 	return false;
5581 }
5582 
5583 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
5584 {
5585 	u32 temp;
5586 
5587 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
5588 
5589 	mutex_lock(&dev_priv->sb_lock);
5590 
5591 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5592 	temp |= SBI_SSCCTL_DISABLE;
5593 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5594 
5595 	mutex_unlock(&dev_priv->sb_lock);
5596 }
5597 
5598 /* Program iCLKIP clock to the desired frequency */
5599 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
5600 {
5601 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5602 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5603 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
5604 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
5605 	u32 temp;
5606 
5607 	lpt_disable_iclkip(dev_priv);
5608 
5609 	/* The iCLK virtual clock root frequency is in MHz,
5610 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
5611 	 * divisors, it is necessary to divide one by another, so we
5612 	 * convert the virtual clock precision to KHz here for higher
5613 	 * precision.
5614 	 */
5615 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
5616 		u32 iclk_virtual_root_freq = 172800 * 1000;
5617 		u32 iclk_pi_range = 64;
5618 		u32 desired_divisor;
5619 
5620 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5621 						    clock << auxdiv);
5622 		divsel = (desired_divisor / iclk_pi_range) - 2;
5623 		phaseinc = desired_divisor % iclk_pi_range;
5624 
5625 		/*
5626 		 * Near 20MHz is a corner case which is
5627 		 * out of range for the 7-bit divisor
5628 		 */
5629 		if (divsel <= 0x7f)
5630 			break;
5631 	}
5632 
5633 	/* This should not happen with any sane values */
5634 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5635 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5636 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5637 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5638 
5639 	drm_dbg_kms(&dev_priv->drm,
5640 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5641 		    clock, auxdiv, divsel, phasedir, phaseinc);
5642 
5643 	mutex_lock(&dev_priv->sb_lock);
5644 
5645 	/* Program SSCDIVINTPHASE6 */
5646 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5647 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5648 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5649 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5650 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5651 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5652 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5653 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5654 
5655 	/* Program SSCAUXDIV */
5656 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5657 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5658 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5659 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5660 
5661 	/* Enable modulator and associated divider */
5662 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5663 	temp &= ~SBI_SSCCTL_DISABLE;
5664 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5665 
5666 	mutex_unlock(&dev_priv->sb_lock);
5667 
5668 	/* Wait for initialization time */
5669 	udelay(24);
5670 
5671 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5672 }
5673 
5674 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5675 {
5676 	u32 divsel, phaseinc, auxdiv;
5677 	u32 iclk_virtual_root_freq = 172800 * 1000;
5678 	u32 iclk_pi_range = 64;
5679 	u32 desired_divisor;
5680 	u32 temp;
5681 
5682 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5683 		return 0;
5684 
5685 	mutex_lock(&dev_priv->sb_lock);
5686 
5687 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5688 	if (temp & SBI_SSCCTL_DISABLE) {
5689 		mutex_unlock(&dev_priv->sb_lock);
5690 		return 0;
5691 	}
5692 
5693 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5694 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5695 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5696 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5697 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5698 
5699 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5700 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5701 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5702 
5703 	mutex_unlock(&dev_priv->sb_lock);
5704 
5705 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5706 
5707 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5708 				 desired_divisor << auxdiv);
5709 }
5710 
5711 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5712 					   enum pipe pch_transcoder)
5713 {
5714 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5715 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5716 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5717 
5718 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5719 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5720 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5721 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5722 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5723 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
5724 
5725 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5726 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5727 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5728 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5729 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5730 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5731 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5732 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
5733 }
5734 
5735 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5736 {
5737 	u32 temp;
5738 
5739 	temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
5740 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5741 		return;
5742 
5743 	drm_WARN_ON(&dev_priv->drm,
5744 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5745 		    FDI_RX_ENABLE);
5746 	drm_WARN_ON(&dev_priv->drm,
5747 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5748 		    FDI_RX_ENABLE);
5749 
5750 	temp &= ~FDI_BC_BIFURCATION_SELECT;
5751 	if (enable)
5752 		temp |= FDI_BC_BIFURCATION_SELECT;
5753 
5754 	drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5755 		    enable ? "en" : "dis");
5756 	intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5757 	intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
5758 }
5759 
5760 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5761 {
5762 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5763 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5764 
5765 	switch (crtc->pipe) {
5766 	case PIPE_A:
5767 		break;
5768 	case PIPE_B:
5769 		if (crtc_state->fdi_lanes > 2)
5770 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
5771 		else
5772 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
5773 
5774 		break;
5775 	case PIPE_C:
5776 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
5777 
5778 		break;
5779 	default:
5780 		BUG();
5781 	}
5782 }
5783 
5784 /*
5785  * Finds the encoder associated with the given CRTC. This can only be
5786  * used when we know that the CRTC isn't feeding multiple encoders!
5787  */
5788 static struct intel_encoder *
5789 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5790 			   const struct intel_crtc_state *crtc_state)
5791 {
5792 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5793 	const struct drm_connector_state *connector_state;
5794 	const struct drm_connector *connector;
5795 	struct intel_encoder *encoder = NULL;
5796 	int num_encoders = 0;
5797 	int i;
5798 
5799 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5800 		if (connector_state->crtc != &crtc->base)
5801 			continue;
5802 
5803 		encoder = to_intel_encoder(connector_state->best_encoder);
5804 		num_encoders++;
5805 	}
5806 
5807 	drm_WARN(encoder->base.dev, num_encoders != 1,
5808 		 "%d encoders for pipe %c\n",
5809 		 num_encoders, pipe_name(crtc->pipe));
5810 
5811 	return encoder;
5812 }
5813 
5814 /*
5815  * Enable PCH resources required for PCH ports:
5816  *   - PCH PLLs
5817  *   - FDI training & RX/TX
5818  *   - update transcoder timings
5819  *   - DP transcoding bits
5820  *   - transcoder
5821  */
5822 static void ilk_pch_enable(const struct intel_atomic_state *state,
5823 			   const struct intel_crtc_state *crtc_state)
5824 {
5825 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5826 	struct drm_device *dev = crtc->base.dev;
5827 	struct drm_i915_private *dev_priv = to_i915(dev);
5828 	enum pipe pipe = crtc->pipe;
5829 	u32 temp;
5830 
5831 	assert_pch_transcoder_disabled(dev_priv, pipe);
5832 
5833 	if (IS_IVYBRIDGE(dev_priv))
5834 		ivb_update_fdi_bc_bifurcation(crtc_state);
5835 
5836 	/* Write the TU size bits before fdi link training, so that error
5837 	 * detection works. */
5838 	intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5839 		       intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5840 
5841 	/* For PCH output, training FDI link */
5842 	dev_priv->display.fdi_link_train(crtc, crtc_state);
5843 
5844 	/* We need to program the right clock selection before writing the pixel
5845 	 * mutliplier into the DPLL. */
5846 	if (HAS_PCH_CPT(dev_priv)) {
5847 		u32 sel;
5848 
5849 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5850 		temp |= TRANS_DPLL_ENABLE(pipe);
5851 		sel = TRANS_DPLLB_SEL(pipe);
5852 		if (crtc_state->shared_dpll ==
5853 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5854 			temp |= sel;
5855 		else
5856 			temp &= ~sel;
5857 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
5858 	}
5859 
5860 	/* XXX: pch pll's can be enabled any time before we enable the PCH
5861 	 * transcoder, and we actually should do this to not upset any PCH
5862 	 * transcoder that already use the clock when we share it.
5863 	 *
5864 	 * Note that enable_shared_dpll tries to do the right thing, but
5865 	 * get_shared_dpll unconditionally resets the pll - we need that to have
5866 	 * the right LVDS enable sequence. */
5867 	intel_enable_shared_dpll(crtc_state);
5868 
5869 	/* set transcoder timing, panel must allow it */
5870 	assert_panel_unlocked(dev_priv, pipe);
5871 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
5872 
5873 	intel_fdi_normal_train(crtc);
5874 
5875 	/* For PCH DP, enable TRANS_DP_CTL */
5876 	if (HAS_PCH_CPT(dev_priv) &&
5877 	    intel_crtc_has_dp_encoder(crtc_state)) {
5878 		const struct drm_display_mode *adjusted_mode =
5879 			&crtc_state->hw.adjusted_mode;
5880 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5881 		i915_reg_t reg = TRANS_DP_CTL(pipe);
5882 		enum port port;
5883 
5884 		temp = intel_de_read(dev_priv, reg);
5885 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
5886 			  TRANS_DP_SYNC_MASK |
5887 			  TRANS_DP_BPC_MASK);
5888 		temp |= TRANS_DP_OUTPUT_ENABLE;
5889 		temp |= bpc << 9; /* same format but at 11:9 */
5890 
5891 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5892 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5893 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5894 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5895 
5896 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5897 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
5898 		temp |= TRANS_DP_PORT_SEL(port);
5899 
5900 		intel_de_write(dev_priv, reg, temp);
5901 	}
5902 
5903 	ilk_enable_pch_transcoder(crtc_state);
5904 }
5905 
5906 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
5907 {
5908 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5909 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5910 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5911 
5912 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5913 
5914 	lpt_program_iclkip(crtc_state);
5915 
5916 	/* Set transcoder timing. */
5917 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
5918 
5919 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5920 }
5921 
5922 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5923 			       enum pipe pipe)
5924 {
5925 	i915_reg_t dslreg = PIPEDSL(pipe);
5926 	u32 temp;
5927 
5928 	temp = intel_de_read(dev_priv, dslreg);
5929 	udelay(500);
5930 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5931 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
5932 			drm_err(&dev_priv->drm,
5933 				"mode set failed: pipe %c stuck\n",
5934 				pipe_name(pipe));
5935 	}
5936 }
5937 
5938 /*
5939  * The hardware phase 0.0 refers to the center of the pixel.
5940  * We want to start from the top/left edge which is phase
5941  * -0.5. That matches how the hardware calculates the scaling
5942  * factors (from top-left of the first pixel to bottom-right
5943  * of the last pixel, as opposed to the pixel centers).
5944  *
5945  * For 4:2:0 subsampled chroma planes we obviously have to
5946  * adjust that so that the chroma sample position lands in
5947  * the right spot.
5948  *
5949  * Note that for packed YCbCr 4:2:2 formats there is no way to
5950  * control chroma siting. The hardware simply replicates the
5951  * chroma samples for both of the luma samples, and thus we don't
5952  * actually get the expected MPEG2 chroma siting convention :(
5953  * The same behaviour is observed on pre-SKL platforms as well.
5954  *
5955  * Theory behind the formula (note that we ignore sub-pixel
5956  * source coordinates):
5957  * s = source sample position
5958  * d = destination sample position
5959  *
5960  * Downscaling 4:1:
5961  * -0.5
5962  * | 0.0
5963  * | |     1.5 (initial phase)
5964  * | |     |
5965  * v v     v
5966  * | s | s | s | s |
5967  * |       d       |
5968  *
5969  * Upscaling 1:4:
5970  * -0.5
5971  * | -0.375 (initial phase)
5972  * | |     0.0
5973  * | |     |
5974  * v v     v
5975  * |       s       |
5976  * | d | d | d | d |
5977  */
5978 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
5979 {
5980 	int phase = -0x8000;
5981 	u16 trip = 0;
5982 
5983 	if (chroma_cosited)
5984 		phase += (sub - 1) * 0x8000 / sub;
5985 
5986 	phase += scale / (2 * sub);
5987 
5988 	/*
5989 	 * Hardware initial phase limited to [-0.5:1.5].
5990 	 * Since the max hardware scale factor is 3.0, we
5991 	 * should never actually excdeed 1.0 here.
5992 	 */
5993 	WARN_ON(phase < -0x8000 || phase > 0x18000);
5994 
5995 	if (phase < 0)
5996 		phase = 0x10000 + phase;
5997 	else
5998 		trip = PS_PHASE_TRIP;
5999 
6000 	return ((phase >> 2) & PS_PHASE_MASK) | trip;
6001 }
6002 
6003 #define SKL_MIN_SRC_W 8
6004 #define SKL_MAX_SRC_W 4096
6005 #define SKL_MIN_SRC_H 8
6006 #define SKL_MAX_SRC_H 4096
6007 #define SKL_MIN_DST_W 8
6008 #define SKL_MAX_DST_W 4096
6009 #define SKL_MIN_DST_H 8
6010 #define SKL_MAX_DST_H 4096
6011 #define ICL_MAX_SRC_W 5120
6012 #define ICL_MAX_SRC_H 4096
6013 #define ICL_MAX_DST_W 5120
6014 #define ICL_MAX_DST_H 4096
6015 #define SKL_MIN_YUV_420_SRC_W 16
6016 #define SKL_MIN_YUV_420_SRC_H 16
6017 
6018 static int
6019 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
6020 		  unsigned int scaler_user, int *scaler_id,
6021 		  int src_w, int src_h, int dst_w, int dst_h,
6022 		  const struct drm_format_info *format,
6023 		  u64 modifier, bool need_scaler)
6024 {
6025 	struct intel_crtc_scaler_state *scaler_state =
6026 		&crtc_state->scaler_state;
6027 	struct intel_crtc *intel_crtc =
6028 		to_intel_crtc(crtc_state->uapi.crtc);
6029 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
6030 	const struct drm_display_mode *adjusted_mode =
6031 		&crtc_state->hw.adjusted_mode;
6032 
6033 	/*
6034 	 * Src coordinates are already rotated by 270 degrees for
6035 	 * the 90/270 degree plane rotation cases (to match the
6036 	 * GTT mapping), hence no need to account for rotation here.
6037 	 */
6038 	if (src_w != dst_w || src_h != dst_h)
6039 		need_scaler = true;
6040 
6041 	/*
6042 	 * Scaling/fitting not supported in IF-ID mode in GEN9+
6043 	 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6044 	 * Once NV12 is enabled, handle it here while allocating scaler
6045 	 * for NV12.
6046 	 */
6047 	if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
6048 	    need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6049 		drm_dbg_kms(&dev_priv->drm,
6050 			    "Pipe/Plane scaling not supported with IF-ID mode\n");
6051 		return -EINVAL;
6052 	}
6053 
6054 	/*
6055 	 * if plane is being disabled or scaler is no more required or force detach
6056 	 *  - free scaler binded to this plane/crtc
6057 	 *  - in order to do this, update crtc->scaler_usage
6058 	 *
6059 	 * Here scaler state in crtc_state is set free so that
6060 	 * scaler can be assigned to other user. Actual register
6061 	 * update to free the scaler is done in plane/panel-fit programming.
6062 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6063 	 */
6064 	if (force_detach || !need_scaler) {
6065 		if (*scaler_id >= 0) {
6066 			scaler_state->scaler_users &= ~(1 << scaler_user);
6067 			scaler_state->scalers[*scaler_id].in_use = 0;
6068 
6069 			drm_dbg_kms(&dev_priv->drm,
6070 				    "scaler_user index %u.%u: "
6071 				    "Staged freeing scaler id %d scaler_users = 0x%x\n",
6072 				    intel_crtc->pipe, scaler_user, *scaler_id,
6073 				    scaler_state->scaler_users);
6074 			*scaler_id = -1;
6075 		}
6076 		return 0;
6077 	}
6078 
6079 	if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
6080 	    (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
6081 		drm_dbg_kms(&dev_priv->drm,
6082 			    "Planar YUV: src dimensions not met\n");
6083 		return -EINVAL;
6084 	}
6085 
6086 	/* range checks */
6087 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
6088 	    dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
6089 	    (INTEL_GEN(dev_priv) >= 11 &&
6090 	     (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6091 	      dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
6092 	    (INTEL_GEN(dev_priv) < 11 &&
6093 	     (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6094 	      dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H)))	{
6095 		drm_dbg_kms(&dev_priv->drm,
6096 			    "scaler_user index %u.%u: src %ux%u dst %ux%u "
6097 			    "size is out of scaler range\n",
6098 			    intel_crtc->pipe, scaler_user, src_w, src_h,
6099 			    dst_w, dst_h);
6100 		return -EINVAL;
6101 	}
6102 
6103 	/* mark this plane as a scaler user in crtc_state */
6104 	scaler_state->scaler_users |= (1 << scaler_user);
6105 	drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6106 		    "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6107 		    intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6108 		    scaler_state->scaler_users);
6109 
6110 	return 0;
6111 }
6112 
6113 /**
6114  * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
6115  *
6116  * @state: crtc's scaler state
6117  *
6118  * Return
6119  *     0 - scaler_usage updated successfully
6120  *    error - requested scaling cannot be supported or other error condition
6121  */
6122 int skl_update_scaler_crtc(struct intel_crtc_state *state)
6123 {
6124 	const struct drm_display_mode *adjusted_mode = &state->hw.adjusted_mode;
6125 	bool need_scaler = false;
6126 
6127 	if (state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6128 	    state->pch_pfit.enabled)
6129 		need_scaler = true;
6130 
6131 	return skl_update_scaler(state, !state->hw.active, SKL_CRTC_INDEX,
6132 				 &state->scaler_state.scaler_id,
6133 				 state->pipe_src_w, state->pipe_src_h,
6134 				 adjusted_mode->crtc_hdisplay,
6135 				 adjusted_mode->crtc_vdisplay, NULL, 0,
6136 				 need_scaler);
6137 }
6138 
6139 /**
6140  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
6141  * @crtc_state: crtc's scaler state
6142  * @plane_state: atomic plane state to update
6143  *
6144  * Return
6145  *     0 - scaler_usage updated successfully
6146  *    error - requested scaling cannot be supported or other error condition
6147  */
6148 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6149 				   struct intel_plane_state *plane_state)
6150 {
6151 	struct intel_plane *intel_plane =
6152 		to_intel_plane(plane_state->uapi.plane);
6153 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
6154 	struct drm_framebuffer *fb = plane_state->hw.fb;
6155 	int ret;
6156 	bool force_detach = !fb || !plane_state->uapi.visible;
6157 	bool need_scaler = false;
6158 
6159 	/* Pre-gen11 and SDR planes always need a scaler for planar formats. */
6160 	if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
6161 	    fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
6162 		need_scaler = true;
6163 
6164 	ret = skl_update_scaler(crtc_state, force_detach,
6165 				drm_plane_index(&intel_plane->base),
6166 				&plane_state->scaler_id,
6167 				drm_rect_width(&plane_state->uapi.src) >> 16,
6168 				drm_rect_height(&plane_state->uapi.src) >> 16,
6169 				drm_rect_width(&plane_state->uapi.dst),
6170 				drm_rect_height(&plane_state->uapi.dst),
6171 				fb ? fb->format : NULL,
6172 				fb ? fb->modifier : 0,
6173 				need_scaler);
6174 
6175 	if (ret || plane_state->scaler_id < 0)
6176 		return ret;
6177 
6178 	/* check colorkey */
6179 	if (plane_state->ckey.flags) {
6180 		drm_dbg_kms(&dev_priv->drm,
6181 			    "[PLANE:%d:%s] scaling with color key not allowed",
6182 			    intel_plane->base.base.id,
6183 			    intel_plane->base.name);
6184 		return -EINVAL;
6185 	}
6186 
6187 	/* Check src format */
6188 	switch (fb->format->format) {
6189 	case DRM_FORMAT_RGB565:
6190 	case DRM_FORMAT_XBGR8888:
6191 	case DRM_FORMAT_XRGB8888:
6192 	case DRM_FORMAT_ABGR8888:
6193 	case DRM_FORMAT_ARGB8888:
6194 	case DRM_FORMAT_XRGB2101010:
6195 	case DRM_FORMAT_XBGR2101010:
6196 	case DRM_FORMAT_ARGB2101010:
6197 	case DRM_FORMAT_ABGR2101010:
6198 	case DRM_FORMAT_YUYV:
6199 	case DRM_FORMAT_YVYU:
6200 	case DRM_FORMAT_UYVY:
6201 	case DRM_FORMAT_VYUY:
6202 	case DRM_FORMAT_NV12:
6203 	case DRM_FORMAT_P010:
6204 	case DRM_FORMAT_P012:
6205 	case DRM_FORMAT_P016:
6206 	case DRM_FORMAT_Y210:
6207 	case DRM_FORMAT_Y212:
6208 	case DRM_FORMAT_Y216:
6209 	case DRM_FORMAT_XVYU2101010:
6210 	case DRM_FORMAT_XVYU12_16161616:
6211 	case DRM_FORMAT_XVYU16161616:
6212 		break;
6213 	case DRM_FORMAT_XBGR16161616F:
6214 	case DRM_FORMAT_ABGR16161616F:
6215 	case DRM_FORMAT_XRGB16161616F:
6216 	case DRM_FORMAT_ARGB16161616F:
6217 		if (INTEL_GEN(dev_priv) >= 11)
6218 			break;
6219 		/* fall through */
6220 	default:
6221 		drm_dbg_kms(&dev_priv->drm,
6222 			    "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6223 			    intel_plane->base.base.id, intel_plane->base.name,
6224 			    fb->base.id, fb->format->format);
6225 		return -EINVAL;
6226 	}
6227 
6228 	return 0;
6229 }
6230 
6231 void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
6232 {
6233 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6234 	int i;
6235 
6236 	for (i = 0; i < crtc->num_scalers; i++)
6237 		skl_detach_scaler(crtc, i);
6238 }
6239 
6240 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
6241 {
6242 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6243 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6244 	enum pipe pipe = crtc->pipe;
6245 	const struct intel_crtc_scaler_state *scaler_state =
6246 		&crtc_state->scaler_state;
6247 
6248 	if (crtc_state->pch_pfit.enabled) {
6249 		u16 uv_rgb_hphase, uv_rgb_vphase;
6250 		int pfit_w, pfit_h, hscale, vscale;
6251 		unsigned long irqflags;
6252 		int id;
6253 
6254 		if (drm_WARN_ON(&dev_priv->drm,
6255 				crtc_state->scaler_state.scaler_id < 0))
6256 			return;
6257 
6258 		pfit_w = (crtc_state->pch_pfit.size >> 16) & 0xFFFF;
6259 		pfit_h = crtc_state->pch_pfit.size & 0xFFFF;
6260 
6261 		hscale = (crtc_state->pipe_src_w << 16) / pfit_w;
6262 		vscale = (crtc_state->pipe_src_h << 16) / pfit_h;
6263 
6264 		uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6265 		uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
6266 
6267 		id = scaler_state->scaler_id;
6268 
6269 		spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6270 
6271 		intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
6272 				  PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
6273 		intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6274 				  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6275 		intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6276 				  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6277 		intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
6278 				  crtc_state->pch_pfit.pos);
6279 		intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
6280 				  crtc_state->pch_pfit.size);
6281 
6282 		spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6283 	}
6284 }
6285 
6286 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
6287 {
6288 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6289 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6290 	enum pipe pipe = crtc->pipe;
6291 
6292 	if (crtc_state->pch_pfit.enabled) {
6293 		/* Force use of hard-coded filter coefficients
6294 		 * as some pre-programmed values are broken,
6295 		 * e.g. x201.
6296 		 */
6297 		if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6298 			intel_de_write(dev_priv, PF_CTL(pipe),
6299 				       PF_ENABLE | PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6300 		else
6301 			intel_de_write(dev_priv, PF_CTL(pipe),
6302 				       PF_ENABLE | PF_FILTER_MED_3x3);
6303 		intel_de_write(dev_priv, PF_WIN_POS(pipe),
6304 			       crtc_state->pch_pfit.pos);
6305 		intel_de_write(dev_priv, PF_WIN_SZ(pipe),
6306 			       crtc_state->pch_pfit.size);
6307 	}
6308 }
6309 
6310 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
6311 {
6312 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6313 	struct drm_device *dev = crtc->base.dev;
6314 	struct drm_i915_private *dev_priv = to_i915(dev);
6315 
6316 	if (!crtc_state->ips_enabled)
6317 		return;
6318 
6319 	/*
6320 	 * We can only enable IPS after we enable a plane and wait for a vblank
6321 	 * This function is called from post_plane_update, which is run after
6322 	 * a vblank wait.
6323 	 */
6324 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
6325 
6326 	if (IS_BROADWELL(dev_priv)) {
6327 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6328 							 IPS_ENABLE | IPS_PCODE_CONTROL));
6329 		/* Quoting Art Runyan: "its not safe to expect any particular
6330 		 * value in IPS_CTL bit 31 after enabling IPS through the
6331 		 * mailbox." Moreover, the mailbox may return a bogus state,
6332 		 * so we need to just enable it and continue on.
6333 		 */
6334 	} else {
6335 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
6336 		/* The bit only becomes 1 in the next vblank, so this wait here
6337 		 * is essentially intel_wait_for_vblank. If we don't have this
6338 		 * and don't wait for vblanks until the end of crtc_enable, then
6339 		 * the HW state readout code will complain that the expected
6340 		 * IPS_CTL value is not the one we read. */
6341 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
6342 			drm_err(&dev_priv->drm,
6343 				"Timed out waiting for IPS enable\n");
6344 	}
6345 }
6346 
6347 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
6348 {
6349 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6350 	struct drm_device *dev = crtc->base.dev;
6351 	struct drm_i915_private *dev_priv = to_i915(dev);
6352 
6353 	if (!crtc_state->ips_enabled)
6354 		return;
6355 
6356 	if (IS_BROADWELL(dev_priv)) {
6357 		drm_WARN_ON(dev,
6358 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
6359 		/*
6360 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
6361 		 * 42ms timeout value leads to occasional timeouts so use 100ms
6362 		 * instead.
6363 		 */
6364 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
6365 			drm_err(&dev_priv->drm,
6366 				"Timed out waiting for IPS disable\n");
6367 	} else {
6368 		intel_de_write(dev_priv, IPS_CTL, 0);
6369 		intel_de_posting_read(dev_priv, IPS_CTL);
6370 	}
6371 
6372 	/* We need to wait for a vblank before we can disable the plane. */
6373 	intel_wait_for_vblank(dev_priv, crtc->pipe);
6374 }
6375 
6376 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
6377 {
6378 	if (intel_crtc->overlay)
6379 		(void) intel_overlay_switch_off(intel_crtc->overlay);
6380 
6381 	/* Let userspace switch the overlay on again. In most cases userspace
6382 	 * has to recompute where to put it anyway.
6383 	 */
6384 }
6385 
6386 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6387 				       const struct intel_crtc_state *new_crtc_state)
6388 {
6389 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6390 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6391 
6392 	if (!old_crtc_state->ips_enabled)
6393 		return false;
6394 
6395 	if (needs_modeset(new_crtc_state))
6396 		return true;
6397 
6398 	/*
6399 	 * Workaround : Do not read or write the pipe palette/gamma data while
6400 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6401 	 *
6402 	 * Disable IPS before we program the LUT.
6403 	 */
6404 	if (IS_HASWELL(dev_priv) &&
6405 	    (new_crtc_state->uapi.color_mgmt_changed ||
6406 	     new_crtc_state->update_pipe) &&
6407 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6408 		return true;
6409 
6410 	return !new_crtc_state->ips_enabled;
6411 }
6412 
6413 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6414 				       const struct intel_crtc_state *new_crtc_state)
6415 {
6416 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6417 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6418 
6419 	if (!new_crtc_state->ips_enabled)
6420 		return false;
6421 
6422 	if (needs_modeset(new_crtc_state))
6423 		return true;
6424 
6425 	/*
6426 	 * Workaround : Do not read or write the pipe palette/gamma data while
6427 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6428 	 *
6429 	 * Re-enable IPS after the LUT has been programmed.
6430 	 */
6431 	if (IS_HASWELL(dev_priv) &&
6432 	    (new_crtc_state->uapi.color_mgmt_changed ||
6433 	     new_crtc_state->update_pipe) &&
6434 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6435 		return true;
6436 
6437 	/*
6438 	 * We can't read out IPS on broadwell, assume the worst and
6439 	 * forcibly enable IPS on the first fastset.
6440 	 */
6441 	if (new_crtc_state->update_pipe &&
6442 	    old_crtc_state->hw.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
6443 		return true;
6444 
6445 	return !old_crtc_state->ips_enabled;
6446 }
6447 
6448 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
6449 {
6450 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6451 
6452 	if (!crtc_state->nv12_planes)
6453 		return false;
6454 
6455 	/* WA Display #0827: Gen9:all */
6456 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
6457 		return true;
6458 
6459 	return false;
6460 }
6461 
6462 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
6463 {
6464 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6465 
6466 	/* Wa_2006604312:icl */
6467 	if (crtc_state->scaler_state.scaler_users > 0 && IS_ICELAKE(dev_priv))
6468 		return true;
6469 
6470 	return false;
6471 }
6472 
6473 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6474 			    const struct intel_crtc_state *new_crtc_state)
6475 {
6476 	return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6477 		new_crtc_state->active_planes;
6478 }
6479 
6480 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6481 			     const struct intel_crtc_state *new_crtc_state)
6482 {
6483 	return old_crtc_state->active_planes &&
6484 		(!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6485 }
6486 
6487 static void intel_post_plane_update(struct intel_atomic_state *state,
6488 				    struct intel_crtc *crtc)
6489 {
6490 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6491 	const struct intel_crtc_state *old_crtc_state =
6492 		intel_atomic_get_old_crtc_state(state, crtc);
6493 	const struct intel_crtc_state *new_crtc_state =
6494 		intel_atomic_get_new_crtc_state(state, crtc);
6495 	enum pipe pipe = crtc->pipe;
6496 
6497 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
6498 
6499 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
6500 		intel_update_watermarks(crtc);
6501 
6502 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6503 		hsw_enable_ips(new_crtc_state);
6504 
6505 	intel_fbc_post_update(state, crtc);
6506 
6507 	if (needs_nv12_wa(old_crtc_state) &&
6508 	    !needs_nv12_wa(new_crtc_state))
6509 		skl_wa_827(dev_priv, pipe, false);
6510 
6511 	if (needs_scalerclk_wa(old_crtc_state) &&
6512 	    !needs_scalerclk_wa(new_crtc_state))
6513 		icl_wa_scalerclkgating(dev_priv, pipe, false);
6514 }
6515 
6516 static void intel_pre_plane_update(struct intel_atomic_state *state,
6517 				   struct intel_crtc *crtc)
6518 {
6519 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6520 	const struct intel_crtc_state *old_crtc_state =
6521 		intel_atomic_get_old_crtc_state(state, crtc);
6522 	const struct intel_crtc_state *new_crtc_state =
6523 		intel_atomic_get_new_crtc_state(state, crtc);
6524 	enum pipe pipe = crtc->pipe;
6525 
6526 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
6527 		hsw_disable_ips(old_crtc_state);
6528 
6529 	if (intel_fbc_pre_update(state, crtc))
6530 		intel_wait_for_vblank(dev_priv, pipe);
6531 
6532 	/* Display WA 827 */
6533 	if (!needs_nv12_wa(old_crtc_state) &&
6534 	    needs_nv12_wa(new_crtc_state))
6535 		skl_wa_827(dev_priv, pipe, true);
6536 
6537 	/* Wa_2006604312:icl */
6538 	if (!needs_scalerclk_wa(old_crtc_state) &&
6539 	    needs_scalerclk_wa(new_crtc_state))
6540 		icl_wa_scalerclkgating(dev_priv, pipe, true);
6541 
6542 	/*
6543 	 * Vblank time updates from the shadow to live plane control register
6544 	 * are blocked if the memory self-refresh mode is active at that
6545 	 * moment. So to make sure the plane gets truly disabled, disable
6546 	 * first the self-refresh mode. The self-refresh enable bit in turn
6547 	 * will be checked/applied by the HW only at the next frame start
6548 	 * event which is after the vblank start event, so we need to have a
6549 	 * wait-for-vblank between disabling the plane and the pipe.
6550 	 */
6551 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
6552 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
6553 		intel_wait_for_vblank(dev_priv, pipe);
6554 
6555 	/*
6556 	 * IVB workaround: must disable low power watermarks for at least
6557 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
6558 	 * when scaling is disabled.
6559 	 *
6560 	 * WaCxSRDisabledForSpriteScaling:ivb
6561 	 */
6562 	if (old_crtc_state->hw.active &&
6563 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
6564 		intel_wait_for_vblank(dev_priv, pipe);
6565 
6566 	/*
6567 	 * If we're doing a modeset we don't need to do any
6568 	 * pre-vblank watermark programming here.
6569 	 */
6570 	if (!needs_modeset(new_crtc_state)) {
6571 		/*
6572 		 * For platforms that support atomic watermarks, program the
6573 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6574 		 * will be the intermediate values that are safe for both pre- and
6575 		 * post- vblank; when vblank happens, the 'active' values will be set
6576 		 * to the final 'target' values and we'll do this again to get the
6577 		 * optimal watermarks.  For gen9+ platforms, the values we program here
6578 		 * will be the final target values which will get automatically latched
6579 		 * at vblank time; no further programming will be necessary.
6580 		 *
6581 		 * If a platform hasn't been transitioned to atomic watermarks yet,
6582 		 * we'll continue to update watermarks the old way, if flags tell
6583 		 * us to.
6584 		 */
6585 		if (dev_priv->display.initial_watermarks)
6586 			dev_priv->display.initial_watermarks(state, crtc);
6587 		else if (new_crtc_state->update_wm_pre)
6588 			intel_update_watermarks(crtc);
6589 	}
6590 
6591 	/*
6592 	 * Gen2 reports pipe underruns whenever all planes are disabled.
6593 	 * So disable underrun reporting before all the planes get disabled.
6594 	 *
6595 	 * We do this after .initial_watermarks() so that we have a
6596 	 * chance of catching underruns with the intermediate watermarks
6597 	 * vs. the old plane configuration.
6598 	 */
6599 	if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6600 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6601 }
6602 
6603 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6604 				      struct intel_crtc *crtc)
6605 {
6606 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6607 	const struct intel_crtc_state *new_crtc_state =
6608 		intel_atomic_get_new_crtc_state(state, crtc);
6609 	unsigned int update_mask = new_crtc_state->update_planes;
6610 	const struct intel_plane_state *old_plane_state;
6611 	struct intel_plane *plane;
6612 	unsigned fb_bits = 0;
6613 	int i;
6614 
6615 	intel_crtc_dpms_overlay_disable(crtc);
6616 
6617 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6618 		if (crtc->pipe != plane->pipe ||
6619 		    !(update_mask & BIT(plane->id)))
6620 			continue;
6621 
6622 		intel_disable_plane(plane, new_crtc_state);
6623 
6624 		if (old_plane_state->uapi.visible)
6625 			fb_bits |= plane->frontbuffer_bit;
6626 	}
6627 
6628 	intel_frontbuffer_flip(dev_priv, fb_bits);
6629 }
6630 
6631 /*
6632  * intel_connector_primary_encoder - get the primary encoder for a connector
6633  * @connector: connector for which to return the encoder
6634  *
6635  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6636  * all connectors to their encoder, except for DP-MST connectors which have
6637  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6638  * pointed to by as many DP-MST connectors as there are pipes.
6639  */
6640 static struct intel_encoder *
6641 intel_connector_primary_encoder(struct intel_connector *connector)
6642 {
6643 	struct intel_encoder *encoder;
6644 
6645 	if (connector->mst_port)
6646 		return &dp_to_dig_port(connector->mst_port)->base;
6647 
6648 	encoder = intel_attached_encoder(connector);
6649 	WARN_ON(!encoder);
6650 
6651 	return encoder;
6652 }
6653 
6654 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6655 {
6656 	struct drm_connector_state *new_conn_state;
6657 	struct drm_connector *connector;
6658 	int i;
6659 
6660 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6661 					i) {
6662 		struct intel_connector *intel_connector;
6663 		struct intel_encoder *encoder;
6664 		struct intel_crtc *crtc;
6665 
6666 		if (!intel_connector_needs_modeset(state, connector))
6667 			continue;
6668 
6669 		intel_connector = to_intel_connector(connector);
6670 		encoder = intel_connector_primary_encoder(intel_connector);
6671 		if (!encoder->update_prepare)
6672 			continue;
6673 
6674 		crtc = new_conn_state->crtc ?
6675 			to_intel_crtc(new_conn_state->crtc) : NULL;
6676 		encoder->update_prepare(state, encoder, crtc);
6677 	}
6678 }
6679 
6680 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6681 {
6682 	struct drm_connector_state *new_conn_state;
6683 	struct drm_connector *connector;
6684 	int i;
6685 
6686 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6687 					i) {
6688 		struct intel_connector *intel_connector;
6689 		struct intel_encoder *encoder;
6690 		struct intel_crtc *crtc;
6691 
6692 		if (!intel_connector_needs_modeset(state, connector))
6693 			continue;
6694 
6695 		intel_connector = to_intel_connector(connector);
6696 		encoder = intel_connector_primary_encoder(intel_connector);
6697 		if (!encoder->update_complete)
6698 			continue;
6699 
6700 		crtc = new_conn_state->crtc ?
6701 			to_intel_crtc(new_conn_state->crtc) : NULL;
6702 		encoder->update_complete(state, encoder, crtc);
6703 	}
6704 }
6705 
6706 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6707 					  struct intel_crtc *crtc)
6708 {
6709 	const struct intel_crtc_state *crtc_state =
6710 		intel_atomic_get_new_crtc_state(state, crtc);
6711 	const struct drm_connector_state *conn_state;
6712 	struct drm_connector *conn;
6713 	int i;
6714 
6715 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6716 		struct intel_encoder *encoder =
6717 			to_intel_encoder(conn_state->best_encoder);
6718 
6719 		if (conn_state->crtc != &crtc->base)
6720 			continue;
6721 
6722 		if (encoder->pre_pll_enable)
6723 			encoder->pre_pll_enable(encoder, crtc_state, conn_state);
6724 	}
6725 }
6726 
6727 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6728 				      struct intel_crtc *crtc)
6729 {
6730 	const struct intel_crtc_state *crtc_state =
6731 		intel_atomic_get_new_crtc_state(state, crtc);
6732 	const struct drm_connector_state *conn_state;
6733 	struct drm_connector *conn;
6734 	int i;
6735 
6736 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6737 		struct intel_encoder *encoder =
6738 			to_intel_encoder(conn_state->best_encoder);
6739 
6740 		if (conn_state->crtc != &crtc->base)
6741 			continue;
6742 
6743 		if (encoder->pre_enable)
6744 			encoder->pre_enable(encoder, crtc_state, conn_state);
6745 	}
6746 }
6747 
6748 static void intel_encoders_enable(struct intel_atomic_state *state,
6749 				  struct intel_crtc *crtc)
6750 {
6751 	const struct intel_crtc_state *crtc_state =
6752 		intel_atomic_get_new_crtc_state(state, crtc);
6753 	const struct drm_connector_state *conn_state;
6754 	struct drm_connector *conn;
6755 	int i;
6756 
6757 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6758 		struct intel_encoder *encoder =
6759 			to_intel_encoder(conn_state->best_encoder);
6760 
6761 		if (conn_state->crtc != &crtc->base)
6762 			continue;
6763 
6764 		if (encoder->enable)
6765 			encoder->enable(encoder, crtc_state, conn_state);
6766 		intel_opregion_notify_encoder(encoder, true);
6767 	}
6768 }
6769 
6770 static void intel_encoders_disable(struct intel_atomic_state *state,
6771 				   struct intel_crtc *crtc)
6772 {
6773 	const struct intel_crtc_state *old_crtc_state =
6774 		intel_atomic_get_old_crtc_state(state, crtc);
6775 	const struct drm_connector_state *old_conn_state;
6776 	struct drm_connector *conn;
6777 	int i;
6778 
6779 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6780 		struct intel_encoder *encoder =
6781 			to_intel_encoder(old_conn_state->best_encoder);
6782 
6783 		if (old_conn_state->crtc != &crtc->base)
6784 			continue;
6785 
6786 		intel_opregion_notify_encoder(encoder, false);
6787 		if (encoder->disable)
6788 			encoder->disable(encoder, old_crtc_state, old_conn_state);
6789 	}
6790 }
6791 
6792 static void intel_encoders_post_disable(struct intel_atomic_state *state,
6793 					struct intel_crtc *crtc)
6794 {
6795 	const struct intel_crtc_state *old_crtc_state =
6796 		intel_atomic_get_old_crtc_state(state, crtc);
6797 	const struct drm_connector_state *old_conn_state;
6798 	struct drm_connector *conn;
6799 	int i;
6800 
6801 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6802 		struct intel_encoder *encoder =
6803 			to_intel_encoder(old_conn_state->best_encoder);
6804 
6805 		if (old_conn_state->crtc != &crtc->base)
6806 			continue;
6807 
6808 		if (encoder->post_disable)
6809 			encoder->post_disable(encoder, old_crtc_state, old_conn_state);
6810 	}
6811 }
6812 
6813 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6814 					    struct intel_crtc *crtc)
6815 {
6816 	const struct intel_crtc_state *old_crtc_state =
6817 		intel_atomic_get_old_crtc_state(state, crtc);
6818 	const struct drm_connector_state *old_conn_state;
6819 	struct drm_connector *conn;
6820 	int i;
6821 
6822 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6823 		struct intel_encoder *encoder =
6824 			to_intel_encoder(old_conn_state->best_encoder);
6825 
6826 		if (old_conn_state->crtc != &crtc->base)
6827 			continue;
6828 
6829 		if (encoder->post_pll_disable)
6830 			encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
6831 	}
6832 }
6833 
6834 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6835 				       struct intel_crtc *crtc)
6836 {
6837 	const struct intel_crtc_state *crtc_state =
6838 		intel_atomic_get_new_crtc_state(state, crtc);
6839 	const struct drm_connector_state *conn_state;
6840 	struct drm_connector *conn;
6841 	int i;
6842 
6843 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6844 		struct intel_encoder *encoder =
6845 			to_intel_encoder(conn_state->best_encoder);
6846 
6847 		if (conn_state->crtc != &crtc->base)
6848 			continue;
6849 
6850 		if (encoder->update_pipe)
6851 			encoder->update_pipe(encoder, crtc_state, conn_state);
6852 	}
6853 }
6854 
6855 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6856 {
6857 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6858 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6859 
6860 	plane->disable_plane(plane, crtc_state);
6861 }
6862 
6863 static void ilk_crtc_enable(struct intel_atomic_state *state,
6864 			    struct intel_crtc *crtc)
6865 {
6866 	const struct intel_crtc_state *new_crtc_state =
6867 		intel_atomic_get_new_crtc_state(state, crtc);
6868 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6869 	enum pipe pipe = crtc->pipe;
6870 
6871 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
6872 		return;
6873 
6874 	/*
6875 	 * Sometimes spurious CPU pipe underruns happen during FDI
6876 	 * training, at least with VGA+HDMI cloning. Suppress them.
6877 	 *
6878 	 * On ILK we get an occasional spurious CPU pipe underruns
6879 	 * between eDP port A enable and vdd enable. Also PCH port
6880 	 * enable seems to result in the occasional CPU pipe underrun.
6881 	 *
6882 	 * Spurious PCH underruns also occur during PCH enabling.
6883 	 */
6884 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6885 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
6886 
6887 	if (new_crtc_state->has_pch_encoder)
6888 		intel_prepare_shared_dpll(new_crtc_state);
6889 
6890 	if (intel_crtc_has_dp_encoder(new_crtc_state))
6891 		intel_dp_set_m_n(new_crtc_state, M1_N1);
6892 
6893 	intel_set_pipe_timings(new_crtc_state);
6894 	intel_set_pipe_src_size(new_crtc_state);
6895 
6896 	if (new_crtc_state->has_pch_encoder)
6897 		intel_cpu_transcoder_set_m_n(new_crtc_state,
6898 					     &new_crtc_state->fdi_m_n, NULL);
6899 
6900 	ilk_set_pipeconf(new_crtc_state);
6901 
6902 	crtc->active = true;
6903 
6904 	intel_encoders_pre_enable(state, crtc);
6905 
6906 	if (new_crtc_state->has_pch_encoder) {
6907 		/* Note: FDI PLL enabling _must_ be done before we enable the
6908 		 * cpu pipes, hence this is separate from all the other fdi/pch
6909 		 * enabling. */
6910 		ilk_fdi_pll_enable(new_crtc_state);
6911 	} else {
6912 		assert_fdi_tx_disabled(dev_priv, pipe);
6913 		assert_fdi_rx_disabled(dev_priv, pipe);
6914 	}
6915 
6916 	ilk_pfit_enable(new_crtc_state);
6917 
6918 	/*
6919 	 * On ILK+ LUT must be loaded before the pipe is running but with
6920 	 * clocks enabled
6921 	 */
6922 	intel_color_load_luts(new_crtc_state);
6923 	intel_color_commit(new_crtc_state);
6924 	/* update DSPCNTR to configure gamma for pipe bottom color */
6925 	intel_disable_primary_plane(new_crtc_state);
6926 
6927 	if (dev_priv->display.initial_watermarks)
6928 		dev_priv->display.initial_watermarks(state, crtc);
6929 	intel_enable_pipe(new_crtc_state);
6930 
6931 	if (new_crtc_state->has_pch_encoder)
6932 		ilk_pch_enable(state, new_crtc_state);
6933 
6934 	intel_crtc_vblank_on(new_crtc_state);
6935 
6936 	intel_encoders_enable(state, crtc);
6937 
6938 	if (HAS_PCH_CPT(dev_priv))
6939 		cpt_verify_modeset(dev_priv, pipe);
6940 
6941 	/*
6942 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
6943 	 * And a second vblank wait is needed at least on ILK with
6944 	 * some interlaced HDMI modes. Let's do the double wait always
6945 	 * in case there are more corner cases we don't know about.
6946 	 */
6947 	if (new_crtc_state->has_pch_encoder) {
6948 		intel_wait_for_vblank(dev_priv, pipe);
6949 		intel_wait_for_vblank(dev_priv, pipe);
6950 	}
6951 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6952 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6953 }
6954 
6955 /* IPS only exists on ULT machines and is tied to pipe A. */
6956 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
6957 {
6958 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
6959 }
6960 
6961 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
6962 					    enum pipe pipe, bool apply)
6963 {
6964 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
6965 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
6966 
6967 	if (apply)
6968 		val |= mask;
6969 	else
6970 		val &= ~mask;
6971 
6972 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
6973 }
6974 
6975 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
6976 {
6977 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6978 	enum pipe pipe = crtc->pipe;
6979 	u32 val;
6980 
6981 	val = MBUS_DBOX_A_CREDIT(2);
6982 
6983 	if (INTEL_GEN(dev_priv) >= 12) {
6984 		val |= MBUS_DBOX_BW_CREDIT(2);
6985 		val |= MBUS_DBOX_B_CREDIT(12);
6986 	} else {
6987 		val |= MBUS_DBOX_BW_CREDIT(1);
6988 		val |= MBUS_DBOX_B_CREDIT(8);
6989 	}
6990 
6991 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
6992 }
6993 
6994 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
6995 {
6996 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6997 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6998 
6999 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7000 		       HSW_LINETIME(crtc_state->linetime) |
7001 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
7002 }
7003 
7004 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7005 {
7006 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7007 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7008 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7009 	u32 val;
7010 
7011 	val = intel_de_read(dev_priv, reg);
7012 	val &= ~HSW_FRAME_START_DELAY_MASK;
7013 	val |= HSW_FRAME_START_DELAY(0);
7014 	intel_de_write(dev_priv, reg, val);
7015 }
7016 
7017 static void hsw_crtc_enable(struct intel_atomic_state *state,
7018 			    struct intel_crtc *crtc)
7019 {
7020 	const struct intel_crtc_state *new_crtc_state =
7021 		intel_atomic_get_new_crtc_state(state, crtc);
7022 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7023 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
7024 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
7025 	bool psl_clkgate_wa;
7026 
7027 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7028 		return;
7029 
7030 	intel_encoders_pre_pll_enable(state, crtc);
7031 
7032 	if (new_crtc_state->shared_dpll)
7033 		intel_enable_shared_dpll(new_crtc_state);
7034 
7035 	intel_encoders_pre_enable(state, crtc);
7036 
7037 	if (!transcoder_is_dsi(cpu_transcoder))
7038 		intel_set_pipe_timings(new_crtc_state);
7039 
7040 	if (INTEL_GEN(dev_priv) >= 11)
7041 		icl_enable_trans_port_sync(new_crtc_state);
7042 
7043 	intel_set_pipe_src_size(new_crtc_state);
7044 
7045 	if (cpu_transcoder != TRANSCODER_EDP &&
7046 	    !transcoder_is_dsi(cpu_transcoder))
7047 		intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7048 			       new_crtc_state->pixel_multiplier - 1);
7049 
7050 	if (new_crtc_state->has_pch_encoder)
7051 		intel_cpu_transcoder_set_m_n(new_crtc_state,
7052 					     &new_crtc_state->fdi_m_n, NULL);
7053 
7054 	if (!transcoder_is_dsi(cpu_transcoder)) {
7055 		hsw_set_frame_start_delay(new_crtc_state);
7056 		hsw_set_pipeconf(new_crtc_state);
7057 	}
7058 
7059 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7060 		bdw_set_pipemisc(new_crtc_state);
7061 
7062 	crtc->active = true;
7063 
7064 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7065 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
7066 		new_crtc_state->pch_pfit.enabled;
7067 	if (psl_clkgate_wa)
7068 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7069 
7070 	if (INTEL_GEN(dev_priv) >= 9)
7071 		skl_pfit_enable(new_crtc_state);
7072 	else
7073 		ilk_pfit_enable(new_crtc_state);
7074 
7075 	/*
7076 	 * On ILK+ LUT must be loaded before the pipe is running but with
7077 	 * clocks enabled
7078 	 */
7079 	intel_color_load_luts(new_crtc_state);
7080 	intel_color_commit(new_crtc_state);
7081 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
7082 	if (INTEL_GEN(dev_priv) < 9)
7083 		intel_disable_primary_plane(new_crtc_state);
7084 
7085 	hsw_set_linetime_wm(new_crtc_state);
7086 
7087 	if (INTEL_GEN(dev_priv) >= 11)
7088 		icl_set_pipe_chicken(crtc);
7089 
7090 	if (!transcoder_is_dsi(cpu_transcoder))
7091 		intel_ddi_enable_transcoder_func(new_crtc_state);
7092 
7093 	if (dev_priv->display.initial_watermarks)
7094 		dev_priv->display.initial_watermarks(state, crtc);
7095 
7096 	if (INTEL_GEN(dev_priv) >= 11)
7097 		icl_pipe_mbus_enable(crtc);
7098 
7099 	intel_encoders_enable(state, crtc);
7100 
7101 	if (psl_clkgate_wa) {
7102 		intel_wait_for_vblank(dev_priv, pipe);
7103 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7104 	}
7105 
7106 	/* If we change the relative order between pipe/planes enabling, we need
7107 	 * to change the workaround. */
7108 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
7109 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
7110 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7111 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7112 	}
7113 }
7114 
7115 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7116 {
7117 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7118 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7119 	enum pipe pipe = crtc->pipe;
7120 
7121 	/* To avoid upsetting the power well on haswell only disable the pfit if
7122 	 * it's in use. The hw state code will make sure we get this right. */
7123 	if (old_crtc_state->pch_pfit.enabled) {
7124 		intel_de_write(dev_priv, PF_CTL(pipe), 0);
7125 		intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7126 		intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
7127 	}
7128 }
7129 
7130 static void ilk_crtc_disable(struct intel_atomic_state *state,
7131 			     struct intel_crtc *crtc)
7132 {
7133 	const struct intel_crtc_state *old_crtc_state =
7134 		intel_atomic_get_old_crtc_state(state, crtc);
7135 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7136 	enum pipe pipe = crtc->pipe;
7137 
7138 	/*
7139 	 * Sometimes spurious CPU pipe underruns happen when the
7140 	 * pipe is already disabled, but FDI RX/TX is still enabled.
7141 	 * Happens at least with VGA+HDMI cloning. Suppress them.
7142 	 */
7143 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7144 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7145 
7146 	intel_encoders_disable(state, crtc);
7147 
7148 	intel_crtc_vblank_off(old_crtc_state);
7149 
7150 	intel_disable_pipe(old_crtc_state);
7151 
7152 	ilk_pfit_disable(old_crtc_state);
7153 
7154 	if (old_crtc_state->has_pch_encoder)
7155 		ilk_fdi_disable(crtc);
7156 
7157 	intel_encoders_post_disable(state, crtc);
7158 
7159 	if (old_crtc_state->has_pch_encoder) {
7160 		ilk_disable_pch_transcoder(dev_priv, pipe);
7161 
7162 		if (HAS_PCH_CPT(dev_priv)) {
7163 			i915_reg_t reg;
7164 			u32 temp;
7165 
7166 			/* disable TRANS_DP_CTL */
7167 			reg = TRANS_DP_CTL(pipe);
7168 			temp = intel_de_read(dev_priv, reg);
7169 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7170 				  TRANS_DP_PORT_SEL_MASK);
7171 			temp |= TRANS_DP_PORT_SEL_NONE;
7172 			intel_de_write(dev_priv, reg, temp);
7173 
7174 			/* disable DPLL_SEL */
7175 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
7176 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
7177 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
7178 		}
7179 
7180 		ilk_fdi_pll_disable(crtc);
7181 	}
7182 
7183 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7184 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7185 }
7186 
7187 static void hsw_crtc_disable(struct intel_atomic_state *state,
7188 			     struct intel_crtc *crtc)
7189 {
7190 	/*
7191 	 * FIXME collapse everything to one hook.
7192 	 * Need care with mst->ddi interactions.
7193 	 */
7194 	intel_encoders_disable(state, crtc);
7195 	intel_encoders_post_disable(state, crtc);
7196 }
7197 
7198 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
7199 {
7200 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7201 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7202 
7203 	if (!crtc_state->gmch_pfit.control)
7204 		return;
7205 
7206 	/*
7207 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
7208 	 * according to register description and PRM.
7209 	 */
7210 	drm_WARN_ON(&dev_priv->drm,
7211 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
7212 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
7213 
7214 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7215 		       crtc_state->gmch_pfit.pgm_ratios);
7216 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
7217 
7218 	/* Border color in case we don't scale up to the full screen. Black by
7219 	 * default, change to something else for debugging. */
7220 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
7221 }
7222 
7223 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7224 {
7225 	if (phy == PHY_NONE)
7226 		return false;
7227 
7228 	if (IS_ELKHARTLAKE(dev_priv))
7229 		return phy <= PHY_C;
7230 
7231 	if (INTEL_GEN(dev_priv) >= 11)
7232 		return phy <= PHY_B;
7233 
7234 	return false;
7235 }
7236 
7237 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7238 {
7239 	if (INTEL_GEN(dev_priv) >= 12)
7240 		return phy >= PHY_D && phy <= PHY_I;
7241 
7242 	if (INTEL_GEN(dev_priv) >= 11 && !IS_ELKHARTLAKE(dev_priv))
7243 		return phy >= PHY_C && phy <= PHY_F;
7244 
7245 	return false;
7246 }
7247 
7248 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7249 {
7250 	if (IS_ELKHARTLAKE(i915) && port == PORT_D)
7251 		return PHY_A;
7252 
7253 	return (enum phy)port;
7254 }
7255 
7256 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7257 {
7258 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
7259 		return PORT_TC_NONE;
7260 
7261 	if (INTEL_GEN(dev_priv) >= 12)
7262 		return port - PORT_D;
7263 
7264 	return port - PORT_C;
7265 }
7266 
7267 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
7268 {
7269 	switch (port) {
7270 	case PORT_A:
7271 		return POWER_DOMAIN_PORT_DDI_A_LANES;
7272 	case PORT_B:
7273 		return POWER_DOMAIN_PORT_DDI_B_LANES;
7274 	case PORT_C:
7275 		return POWER_DOMAIN_PORT_DDI_C_LANES;
7276 	case PORT_D:
7277 		return POWER_DOMAIN_PORT_DDI_D_LANES;
7278 	case PORT_E:
7279 		return POWER_DOMAIN_PORT_DDI_E_LANES;
7280 	case PORT_F:
7281 		return POWER_DOMAIN_PORT_DDI_F_LANES;
7282 	case PORT_G:
7283 		return POWER_DOMAIN_PORT_DDI_G_LANES;
7284 	default:
7285 		MISSING_CASE(port);
7286 		return POWER_DOMAIN_PORT_OTHER;
7287 	}
7288 }
7289 
7290 enum intel_display_power_domain
7291 intel_aux_power_domain(struct intel_digital_port *dig_port)
7292 {
7293 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
7294 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
7295 
7296 	if (intel_phy_is_tc(dev_priv, phy) &&
7297 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
7298 		switch (dig_port->aux_ch) {
7299 		case AUX_CH_C:
7300 			return POWER_DOMAIN_AUX_C_TBT;
7301 		case AUX_CH_D:
7302 			return POWER_DOMAIN_AUX_D_TBT;
7303 		case AUX_CH_E:
7304 			return POWER_DOMAIN_AUX_E_TBT;
7305 		case AUX_CH_F:
7306 			return POWER_DOMAIN_AUX_F_TBT;
7307 		case AUX_CH_G:
7308 			return POWER_DOMAIN_AUX_G_TBT;
7309 		default:
7310 			MISSING_CASE(dig_port->aux_ch);
7311 			return POWER_DOMAIN_AUX_C_TBT;
7312 		}
7313 	}
7314 
7315 	switch (dig_port->aux_ch) {
7316 	case AUX_CH_A:
7317 		return POWER_DOMAIN_AUX_A;
7318 	case AUX_CH_B:
7319 		return POWER_DOMAIN_AUX_B;
7320 	case AUX_CH_C:
7321 		return POWER_DOMAIN_AUX_C;
7322 	case AUX_CH_D:
7323 		return POWER_DOMAIN_AUX_D;
7324 	case AUX_CH_E:
7325 		return POWER_DOMAIN_AUX_E;
7326 	case AUX_CH_F:
7327 		return POWER_DOMAIN_AUX_F;
7328 	case AUX_CH_G:
7329 		return POWER_DOMAIN_AUX_G;
7330 	default:
7331 		MISSING_CASE(dig_port->aux_ch);
7332 		return POWER_DOMAIN_AUX_A;
7333 	}
7334 }
7335 
7336 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7337 {
7338 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7339 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7340 	struct drm_encoder *encoder;
7341 	enum pipe pipe = crtc->pipe;
7342 	u64 mask;
7343 	enum transcoder transcoder = crtc_state->cpu_transcoder;
7344 
7345 	if (!crtc_state->hw.active)
7346 		return 0;
7347 
7348 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7349 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
7350 	if (crtc_state->pch_pfit.enabled ||
7351 	    crtc_state->pch_pfit.force_thru)
7352 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
7353 
7354 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
7355 				  crtc_state->uapi.encoder_mask) {
7356 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7357 
7358 		mask |= BIT_ULL(intel_encoder->power_domain);
7359 	}
7360 
7361 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
7362 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
7363 
7364 	if (crtc_state->shared_dpll)
7365 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
7366 
7367 	return mask;
7368 }
7369 
7370 static u64
7371 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7372 {
7373 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7374 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7375 	enum intel_display_power_domain domain;
7376 	u64 domains, new_domains, old_domains;
7377 
7378 	old_domains = crtc->enabled_power_domains;
7379 	crtc->enabled_power_domains = new_domains =
7380 		get_crtc_power_domains(crtc_state);
7381 
7382 	domains = new_domains & ~old_domains;
7383 
7384 	for_each_power_domain(domain, domains)
7385 		intel_display_power_get(dev_priv, domain);
7386 
7387 	return old_domains & ~new_domains;
7388 }
7389 
7390 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
7391 				      u64 domains)
7392 {
7393 	enum intel_display_power_domain domain;
7394 
7395 	for_each_power_domain(domain, domains)
7396 		intel_display_power_put_unchecked(dev_priv, domain);
7397 }
7398 
7399 static void valleyview_crtc_enable(struct intel_atomic_state *state,
7400 				   struct intel_crtc *crtc)
7401 {
7402 	const struct intel_crtc_state *new_crtc_state =
7403 		intel_atomic_get_new_crtc_state(state, crtc);
7404 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7405 	enum pipe pipe = crtc->pipe;
7406 
7407 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7408 		return;
7409 
7410 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7411 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7412 
7413 	intel_set_pipe_timings(new_crtc_state);
7414 	intel_set_pipe_src_size(new_crtc_state);
7415 
7416 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
7417 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7418 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
7419 	}
7420 
7421 	i9xx_set_pipeconf(new_crtc_state);
7422 
7423 	crtc->active = true;
7424 
7425 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7426 
7427 	intel_encoders_pre_pll_enable(state, crtc);
7428 
7429 	if (IS_CHERRYVIEW(dev_priv)) {
7430 		chv_prepare_pll(crtc, new_crtc_state);
7431 		chv_enable_pll(crtc, new_crtc_state);
7432 	} else {
7433 		vlv_prepare_pll(crtc, new_crtc_state);
7434 		vlv_enable_pll(crtc, new_crtc_state);
7435 	}
7436 
7437 	intel_encoders_pre_enable(state, crtc);
7438 
7439 	i9xx_pfit_enable(new_crtc_state);
7440 
7441 	intel_color_load_luts(new_crtc_state);
7442 	intel_color_commit(new_crtc_state);
7443 	/* update DSPCNTR to configure gamma for pipe bottom color */
7444 	intel_disable_primary_plane(new_crtc_state);
7445 
7446 	dev_priv->display.initial_watermarks(state, crtc);
7447 	intel_enable_pipe(new_crtc_state);
7448 
7449 	intel_crtc_vblank_on(new_crtc_state);
7450 
7451 	intel_encoders_enable(state, crtc);
7452 }
7453 
7454 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
7455 {
7456 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7457 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7458 
7459 	intel_de_write(dev_priv, FP0(crtc->pipe),
7460 		       crtc_state->dpll_hw_state.fp0);
7461 	intel_de_write(dev_priv, FP1(crtc->pipe),
7462 		       crtc_state->dpll_hw_state.fp1);
7463 }
7464 
7465 static void i9xx_crtc_enable(struct intel_atomic_state *state,
7466 			     struct intel_crtc *crtc)
7467 {
7468 	const struct intel_crtc_state *new_crtc_state =
7469 		intel_atomic_get_new_crtc_state(state, crtc);
7470 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7471 	enum pipe pipe = crtc->pipe;
7472 
7473 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7474 		return;
7475 
7476 	i9xx_set_pll_dividers(new_crtc_state);
7477 
7478 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7479 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7480 
7481 	intel_set_pipe_timings(new_crtc_state);
7482 	intel_set_pipe_src_size(new_crtc_state);
7483 
7484 	i9xx_set_pipeconf(new_crtc_state);
7485 
7486 	crtc->active = true;
7487 
7488 	if (!IS_GEN(dev_priv, 2))
7489 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7490 
7491 	intel_encoders_pre_enable(state, crtc);
7492 
7493 	i9xx_enable_pll(crtc, new_crtc_state);
7494 
7495 	i9xx_pfit_enable(new_crtc_state);
7496 
7497 	intel_color_load_luts(new_crtc_state);
7498 	intel_color_commit(new_crtc_state);
7499 	/* update DSPCNTR to configure gamma for pipe bottom color */
7500 	intel_disable_primary_plane(new_crtc_state);
7501 
7502 	if (dev_priv->display.initial_watermarks)
7503 		dev_priv->display.initial_watermarks(state, crtc);
7504 	else
7505 		intel_update_watermarks(crtc);
7506 	intel_enable_pipe(new_crtc_state);
7507 
7508 	intel_crtc_vblank_on(new_crtc_state);
7509 
7510 	intel_encoders_enable(state, crtc);
7511 }
7512 
7513 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7514 {
7515 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7516 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7517 
7518 	if (!old_crtc_state->gmch_pfit.control)
7519 		return;
7520 
7521 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7522 
7523 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
7524 		    intel_de_read(dev_priv, PFIT_CONTROL));
7525 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
7526 }
7527 
7528 static void i9xx_crtc_disable(struct intel_atomic_state *state,
7529 			      struct intel_crtc *crtc)
7530 {
7531 	struct intel_crtc_state *old_crtc_state =
7532 		intel_atomic_get_old_crtc_state(state, crtc);
7533 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7534 	enum pipe pipe = crtc->pipe;
7535 
7536 	/*
7537 	 * On gen2 planes are double buffered but the pipe isn't, so we must
7538 	 * wait for planes to fully turn off before disabling the pipe.
7539 	 */
7540 	if (IS_GEN(dev_priv, 2))
7541 		intel_wait_for_vblank(dev_priv, pipe);
7542 
7543 	intel_encoders_disable(state, crtc);
7544 
7545 	intel_crtc_vblank_off(old_crtc_state);
7546 
7547 	intel_disable_pipe(old_crtc_state);
7548 
7549 	i9xx_pfit_disable(old_crtc_state);
7550 
7551 	intel_encoders_post_disable(state, crtc);
7552 
7553 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7554 		if (IS_CHERRYVIEW(dev_priv))
7555 			chv_disable_pll(dev_priv, pipe);
7556 		else if (IS_VALLEYVIEW(dev_priv))
7557 			vlv_disable_pll(dev_priv, pipe);
7558 		else
7559 			i9xx_disable_pll(old_crtc_state);
7560 	}
7561 
7562 	intel_encoders_post_pll_disable(state, crtc);
7563 
7564 	if (!IS_GEN(dev_priv, 2))
7565 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7566 
7567 	if (!dev_priv->display.initial_watermarks)
7568 		intel_update_watermarks(crtc);
7569 
7570 	/* clock the pipe down to 640x480@60 to potentially save power */
7571 	if (IS_I830(dev_priv))
7572 		i830_enable_pipe(dev_priv, pipe);
7573 }
7574 
7575 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
7576 					struct drm_modeset_acquire_ctx *ctx)
7577 {
7578 	struct intel_encoder *encoder;
7579 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7580 	struct intel_bw_state *bw_state =
7581 		to_intel_bw_state(dev_priv->bw_obj.state);
7582 	struct intel_cdclk_state *cdclk_state =
7583 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
7584 	struct intel_crtc_state *crtc_state =
7585 		to_intel_crtc_state(crtc->base.state);
7586 	enum intel_display_power_domain domain;
7587 	struct intel_plane *plane;
7588 	struct drm_atomic_state *state;
7589 	struct intel_crtc_state *temp_crtc_state;
7590 	enum pipe pipe = crtc->pipe;
7591 	u64 domains;
7592 	int ret;
7593 
7594 	if (!crtc_state->hw.active)
7595 		return;
7596 
7597 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7598 		const struct intel_plane_state *plane_state =
7599 			to_intel_plane_state(plane->base.state);
7600 
7601 		if (plane_state->uapi.visible)
7602 			intel_plane_disable_noatomic(crtc, plane);
7603 	}
7604 
7605 	state = drm_atomic_state_alloc(&dev_priv->drm);
7606 	if (!state) {
7607 		drm_dbg_kms(&dev_priv->drm,
7608 			    "failed to disable [CRTC:%d:%s], out of memory",
7609 			    crtc->base.base.id, crtc->base.name);
7610 		return;
7611 	}
7612 
7613 	state->acquire_ctx = ctx;
7614 
7615 	/* Everything's already locked, -EDEADLK can't happen. */
7616 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7617 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
7618 
7619 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
7620 
7621 	dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
7622 
7623 	drm_atomic_state_put(state);
7624 
7625 	drm_dbg_kms(&dev_priv->drm,
7626 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7627 		    crtc->base.base.id, crtc->base.name);
7628 
7629 	crtc->active = false;
7630 	crtc->base.enabled = false;
7631 
7632 	drm_WARN_ON(&dev_priv->drm,
7633 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
7634 	crtc_state->uapi.active = false;
7635 	crtc_state->uapi.connector_mask = 0;
7636 	crtc_state->uapi.encoder_mask = 0;
7637 	intel_crtc_free_hw_state(crtc_state);
7638 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
7639 
7640 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
7641 		encoder->base.crtc = NULL;
7642 
7643 	intel_fbc_disable(crtc);
7644 	intel_update_watermarks(crtc);
7645 	intel_disable_shared_dpll(crtc_state);
7646 
7647 	domains = crtc->enabled_power_domains;
7648 	for_each_power_domain(domain, domains)
7649 		intel_display_power_put_unchecked(dev_priv, domain);
7650 	crtc->enabled_power_domains = 0;
7651 
7652 	dev_priv->active_pipes &= ~BIT(pipe);
7653 	cdclk_state->min_cdclk[pipe] = 0;
7654 	cdclk_state->min_voltage_level[pipe] = 0;
7655 	cdclk_state->active_pipes &= ~BIT(pipe);
7656 
7657 	bw_state->data_rate[pipe] = 0;
7658 	bw_state->num_active_planes[pipe] = 0;
7659 }
7660 
7661 /*
7662  * turn all crtc's off, but do not adjust state
7663  * This has to be paired with a call to intel_modeset_setup_hw_state.
7664  */
7665 int intel_display_suspend(struct drm_device *dev)
7666 {
7667 	struct drm_i915_private *dev_priv = to_i915(dev);
7668 	struct drm_atomic_state *state;
7669 	int ret;
7670 
7671 	state = drm_atomic_helper_suspend(dev);
7672 	ret = PTR_ERR_OR_ZERO(state);
7673 	if (ret)
7674 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7675 			ret);
7676 	else
7677 		dev_priv->modeset_restore_state = state;
7678 	return ret;
7679 }
7680 
7681 void intel_encoder_destroy(struct drm_encoder *encoder)
7682 {
7683 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7684 
7685 	drm_encoder_cleanup(encoder);
7686 	kfree(intel_encoder);
7687 }
7688 
7689 /* Cross check the actual hw state with our own modeset state tracking (and it's
7690  * internal consistency). */
7691 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7692 					 struct drm_connector_state *conn_state)
7693 {
7694 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7695 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
7696 
7697 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7698 		    connector->base.base.id, connector->base.name);
7699 
7700 	if (connector->get_hw_state(connector)) {
7701 		struct intel_encoder *encoder = intel_attached_encoder(connector);
7702 
7703 		I915_STATE_WARN(!crtc_state,
7704 			 "connector enabled without attached crtc\n");
7705 
7706 		if (!crtc_state)
7707 			return;
7708 
7709 		I915_STATE_WARN(!crtc_state->hw.active,
7710 				"connector is active, but attached crtc isn't\n");
7711 
7712 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7713 			return;
7714 
7715 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7716 			"atomic encoder doesn't match attached encoder\n");
7717 
7718 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7719 			"attached encoder crtc differs from connector crtc\n");
7720 	} else {
7721 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7722 				"attached crtc is active, but connector isn't\n");
7723 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7724 			"best encoder set without crtc!\n");
7725 	}
7726 }
7727 
7728 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7729 {
7730 	if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
7731 		return crtc_state->fdi_lanes;
7732 
7733 	return 0;
7734 }
7735 
7736 static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7737 			       struct intel_crtc_state *pipe_config)
7738 {
7739 	struct drm_i915_private *dev_priv = to_i915(dev);
7740 	struct drm_atomic_state *state = pipe_config->uapi.state;
7741 	struct intel_crtc *other_crtc;
7742 	struct intel_crtc_state *other_crtc_state;
7743 
7744 	drm_dbg_kms(&dev_priv->drm,
7745 		    "checking fdi config on pipe %c, lanes %i\n",
7746 		    pipe_name(pipe), pipe_config->fdi_lanes);
7747 	if (pipe_config->fdi_lanes > 4) {
7748 		drm_dbg_kms(&dev_priv->drm,
7749 			    "invalid fdi lane config on pipe %c: %i lanes\n",
7750 			    pipe_name(pipe), pipe_config->fdi_lanes);
7751 		return -EINVAL;
7752 	}
7753 
7754 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7755 		if (pipe_config->fdi_lanes > 2) {
7756 			drm_dbg_kms(&dev_priv->drm,
7757 				    "only 2 lanes on haswell, required: %i lanes\n",
7758 				    pipe_config->fdi_lanes);
7759 			return -EINVAL;
7760 		} else {
7761 			return 0;
7762 		}
7763 	}
7764 
7765 	if (INTEL_NUM_PIPES(dev_priv) == 2)
7766 		return 0;
7767 
7768 	/* Ivybridge 3 pipe is really complicated */
7769 	switch (pipe) {
7770 	case PIPE_A:
7771 		return 0;
7772 	case PIPE_B:
7773 		if (pipe_config->fdi_lanes <= 2)
7774 			return 0;
7775 
7776 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
7777 		other_crtc_state =
7778 			intel_atomic_get_crtc_state(state, other_crtc);
7779 		if (IS_ERR(other_crtc_state))
7780 			return PTR_ERR(other_crtc_state);
7781 
7782 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
7783 			drm_dbg_kms(&dev_priv->drm,
7784 				    "invalid shared fdi lane config on pipe %c: %i lanes\n",
7785 				    pipe_name(pipe), pipe_config->fdi_lanes);
7786 			return -EINVAL;
7787 		}
7788 		return 0;
7789 	case PIPE_C:
7790 		if (pipe_config->fdi_lanes > 2) {
7791 			drm_dbg_kms(&dev_priv->drm,
7792 				    "only 2 lanes on pipe %c: required %i lanes\n",
7793 				    pipe_name(pipe), pipe_config->fdi_lanes);
7794 			return -EINVAL;
7795 		}
7796 
7797 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
7798 		other_crtc_state =
7799 			intel_atomic_get_crtc_state(state, other_crtc);
7800 		if (IS_ERR(other_crtc_state))
7801 			return PTR_ERR(other_crtc_state);
7802 
7803 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
7804 			drm_dbg_kms(&dev_priv->drm,
7805 				    "fdi link B uses too many lanes to enable link C\n");
7806 			return -EINVAL;
7807 		}
7808 		return 0;
7809 	default:
7810 		BUG();
7811 	}
7812 }
7813 
7814 #define RETRY 1
7815 static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
7816 				  struct intel_crtc_state *pipe_config)
7817 {
7818 	struct drm_device *dev = intel_crtc->base.dev;
7819 	struct drm_i915_private *i915 = to_i915(dev);
7820 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7821 	int lane, link_bw, fdi_dotclock, ret;
7822 	bool needs_recompute = false;
7823 
7824 retry:
7825 	/* FDI is a binary signal running at ~2.7GHz, encoding
7826 	 * each output octet as 10 bits. The actual frequency
7827 	 * is stored as a divider into a 100MHz clock, and the
7828 	 * mode pixel clock is stored in units of 1KHz.
7829 	 * Hence the bw of each lane in terms of the mode signal
7830 	 * is:
7831 	 */
7832 	link_bw = intel_fdi_link_freq(i915, pipe_config);
7833 
7834 	fdi_dotclock = adjusted_mode->crtc_clock;
7835 
7836 	lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
7837 				      pipe_config->pipe_bpp);
7838 
7839 	pipe_config->fdi_lanes = lane;
7840 
7841 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
7842 			       link_bw, &pipe_config->fdi_m_n, false, false);
7843 
7844 	ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
7845 	if (ret == -EDEADLK)
7846 		return ret;
7847 
7848 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
7849 		pipe_config->pipe_bpp -= 2*3;
7850 		drm_dbg_kms(&i915->drm,
7851 			    "fdi link bw constraint, reducing pipe bpp to %i\n",
7852 			    pipe_config->pipe_bpp);
7853 		needs_recompute = true;
7854 		pipe_config->bw_constrained = true;
7855 
7856 		goto retry;
7857 	}
7858 
7859 	if (needs_recompute)
7860 		return RETRY;
7861 
7862 	return ret;
7863 }
7864 
7865 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
7866 {
7867 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7868 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7869 
7870 	/* IPS only exists on ULT machines and is tied to pipe A. */
7871 	if (!hsw_crtc_supports_ips(crtc))
7872 		return false;
7873 
7874 	if (!i915_modparams.enable_ips)
7875 		return false;
7876 
7877 	if (crtc_state->pipe_bpp > 24)
7878 		return false;
7879 
7880 	/*
7881 	 * We compare against max which means we must take
7882 	 * the increased cdclk requirement into account when
7883 	 * calculating the new cdclk.
7884 	 *
7885 	 * Should measure whether using a lower cdclk w/o IPS
7886 	 */
7887 	if (IS_BROADWELL(dev_priv) &&
7888 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
7889 		return false;
7890 
7891 	return true;
7892 }
7893 
7894 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7895 {
7896 	struct drm_i915_private *dev_priv =
7897 		to_i915(crtc_state->uapi.crtc->dev);
7898 	struct intel_atomic_state *state =
7899 		to_intel_atomic_state(crtc_state->uapi.state);
7900 
7901 	crtc_state->ips_enabled = false;
7902 
7903 	if (!hsw_crtc_state_ips_capable(crtc_state))
7904 		return 0;
7905 
7906 	/*
7907 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
7908 	 * enabled and disabled dynamically based on package C states,
7909 	 * user space can't make reliable use of the CRCs, so let's just
7910 	 * completely disable it.
7911 	 */
7912 	if (crtc_state->crc_enabled)
7913 		return 0;
7914 
7915 	/* IPS should be fine as long as at least one plane is enabled. */
7916 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
7917 		return 0;
7918 
7919 	if (IS_BROADWELL(dev_priv)) {
7920 		const struct intel_cdclk_state *cdclk_state;
7921 
7922 		cdclk_state = intel_atomic_get_cdclk_state(state);
7923 		if (IS_ERR(cdclk_state))
7924 			return PTR_ERR(cdclk_state);
7925 
7926 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
7927 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
7928 			return 0;
7929 	}
7930 
7931 	crtc_state->ips_enabled = true;
7932 
7933 	return 0;
7934 }
7935 
7936 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7937 {
7938 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7939 
7940 	/* GDG double wide on either pipe, otherwise pipe A only */
7941 	return INTEL_GEN(dev_priv) < 4 &&
7942 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7943 }
7944 
7945 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
7946 {
7947 	u32 pixel_rate;
7948 
7949 	pixel_rate = pipe_config->hw.adjusted_mode.crtc_clock;
7950 
7951 	/*
7952 	 * We only use IF-ID interlacing. If we ever use
7953 	 * PF-ID we'll need to adjust the pixel_rate here.
7954 	 */
7955 
7956 	if (pipe_config->pch_pfit.enabled) {
7957 		u64 pipe_w, pipe_h, pfit_w, pfit_h;
7958 		u32 pfit_size = pipe_config->pch_pfit.size;
7959 
7960 		pipe_w = pipe_config->pipe_src_w;
7961 		pipe_h = pipe_config->pipe_src_h;
7962 
7963 		pfit_w = (pfit_size >> 16) & 0xFFFF;
7964 		pfit_h = pfit_size & 0xFFFF;
7965 		if (pipe_w < pfit_w)
7966 			pipe_w = pfit_w;
7967 		if (pipe_h < pfit_h)
7968 			pipe_h = pfit_h;
7969 
7970 		if (WARN_ON(!pfit_w || !pfit_h))
7971 			return pixel_rate;
7972 
7973 		pixel_rate = div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
7974 				     pfit_w * pfit_h);
7975 	}
7976 
7977 	return pixel_rate;
7978 }
7979 
7980 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
7981 {
7982 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7983 
7984 	if (HAS_GMCH(dev_priv))
7985 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
7986 		crtc_state->pixel_rate =
7987 			crtc_state->hw.adjusted_mode.crtc_clock;
7988 	else
7989 		crtc_state->pixel_rate =
7990 			ilk_pipe_pixel_rate(crtc_state);
7991 }
7992 
7993 static int intel_crtc_compute_config(struct intel_crtc *crtc,
7994 				     struct intel_crtc_state *pipe_config)
7995 {
7996 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7997 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7998 	int clock_limit = dev_priv->max_dotclk_freq;
7999 
8000 	if (INTEL_GEN(dev_priv) < 4) {
8001 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
8002 
8003 		/*
8004 		 * Enable double wide mode when the dot clock
8005 		 * is > 90% of the (display) core speed.
8006 		 */
8007 		if (intel_crtc_supports_double_wide(crtc) &&
8008 		    adjusted_mode->crtc_clock > clock_limit) {
8009 			clock_limit = dev_priv->max_dotclk_freq;
8010 			pipe_config->double_wide = true;
8011 		}
8012 	}
8013 
8014 	if (adjusted_mode->crtc_clock > clock_limit) {
8015 		drm_dbg_kms(&dev_priv->drm,
8016 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
8017 			    adjusted_mode->crtc_clock, clock_limit,
8018 			    yesno(pipe_config->double_wide));
8019 		return -EINVAL;
8020 	}
8021 
8022 	if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8023 	     pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
8024 	     pipe_config->hw.ctm) {
8025 		/*
8026 		 * There is only one pipe CSC unit per pipe, and we need that
8027 		 * for output conversion from RGB->YCBCR. So if CTM is already
8028 		 * applied we can't support YCBCR420 output.
8029 		 */
8030 		drm_dbg_kms(&dev_priv->drm,
8031 			    "YCBCR420 and CTM together are not possible\n");
8032 		return -EINVAL;
8033 	}
8034 
8035 	/*
8036 	 * Pipe horizontal size must be even in:
8037 	 * - DVO ganged mode
8038 	 * - LVDS dual channel mode
8039 	 * - Double wide pipe
8040 	 */
8041 	if (pipe_config->pipe_src_w & 1) {
8042 		if (pipe_config->double_wide) {
8043 			drm_dbg_kms(&dev_priv->drm,
8044 				    "Odd pipe source width not supported with double wide pipe\n");
8045 			return -EINVAL;
8046 		}
8047 
8048 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
8049 		    intel_is_dual_link_lvds(dev_priv)) {
8050 			drm_dbg_kms(&dev_priv->drm,
8051 				    "Odd pipe source width not supported with dual link LVDS\n");
8052 			return -EINVAL;
8053 		}
8054 	}
8055 
8056 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
8057 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8058 	 */
8059 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8060 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
8061 		return -EINVAL;
8062 
8063 	intel_crtc_compute_pixel_rate(pipe_config);
8064 
8065 	if (pipe_config->has_pch_encoder)
8066 		return ilk_fdi_compute_config(crtc, pipe_config);
8067 
8068 	return 0;
8069 }
8070 
8071 static void
8072 intel_reduce_m_n_ratio(u32 *num, u32 *den)
8073 {
8074 	while (*num > DATA_LINK_M_N_MASK ||
8075 	       *den > DATA_LINK_M_N_MASK) {
8076 		*num >>= 1;
8077 		*den >>= 1;
8078 	}
8079 }
8080 
8081 static void compute_m_n(unsigned int m, unsigned int n,
8082 			u32 *ret_m, u32 *ret_n,
8083 			bool constant_n)
8084 {
8085 	/*
8086 	 * Several DP dongles in particular seem to be fussy about
8087 	 * too large link M/N values. Give N value as 0x8000 that
8088 	 * should be acceptable by specific devices. 0x8000 is the
8089 	 * specified fixed N value for asynchronous clock mode,
8090 	 * which the devices expect also in synchronous clock mode.
8091 	 */
8092 	if (constant_n)
8093 		*ret_n = 0x8000;
8094 	else
8095 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
8096 
8097 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
8098 	intel_reduce_m_n_ratio(ret_m, ret_n);
8099 }
8100 
8101 void
8102 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
8103 		       int pixel_clock, int link_clock,
8104 		       struct intel_link_m_n *m_n,
8105 		       bool constant_n, bool fec_enable)
8106 {
8107 	u32 data_clock = bits_per_pixel * pixel_clock;
8108 
8109 	if (fec_enable)
8110 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
8111 
8112 	m_n->tu = 64;
8113 	compute_m_n(data_clock,
8114 		    link_clock * nlanes * 8,
8115 		    &m_n->gmch_m, &m_n->gmch_n,
8116 		    constant_n);
8117 
8118 	compute_m_n(pixel_clock, link_clock,
8119 		    &m_n->link_m, &m_n->link_n,
8120 		    constant_n);
8121 }
8122 
8123 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8124 {
8125 	/*
8126 	 * There may be no VBT; and if the BIOS enabled SSC we can
8127 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
8128 	 * BIOS isn't using it, don't assume it will work even if the VBT
8129 	 * indicates as much.
8130 	 */
8131 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
8132 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8133 						       PCH_DREF_CONTROL) &
8134 			DREF_SSC1_ENABLE;
8135 
8136 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
8137 			drm_dbg_kms(&dev_priv->drm,
8138 				    "SSC %s by BIOS, overriding VBT which says %s\n",
8139 				    enableddisabled(bios_lvds_use_ssc),
8140 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
8141 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8142 		}
8143 	}
8144 }
8145 
8146 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
8147 {
8148 	if (i915_modparams.panel_use_ssc >= 0)
8149 		return i915_modparams.panel_use_ssc != 0;
8150 	return dev_priv->vbt.lvds_use_ssc
8151 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
8152 }
8153 
8154 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
8155 {
8156 	return (1 << dpll->n) << 16 | dpll->m2;
8157 }
8158 
8159 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
8160 {
8161 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
8162 }
8163 
8164 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
8165 				     struct intel_crtc_state *crtc_state,
8166 				     struct dpll *reduced_clock)
8167 {
8168 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8169 	u32 fp, fp2 = 0;
8170 
8171 	if (IS_PINEVIEW(dev_priv)) {
8172 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
8173 		if (reduced_clock)
8174 			fp2 = pnv_dpll_compute_fp(reduced_clock);
8175 	} else {
8176 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8177 		if (reduced_clock)
8178 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
8179 	}
8180 
8181 	crtc_state->dpll_hw_state.fp0 = fp;
8182 
8183 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8184 	    reduced_clock) {
8185 		crtc_state->dpll_hw_state.fp1 = fp2;
8186 	} else {
8187 		crtc_state->dpll_hw_state.fp1 = fp;
8188 	}
8189 }
8190 
8191 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8192 		pipe)
8193 {
8194 	u32 reg_val;
8195 
8196 	/*
8197 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8198 	 * and set it to a reasonable value instead.
8199 	 */
8200 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8201 	reg_val &= 0xffffff00;
8202 	reg_val |= 0x00000030;
8203 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8204 
8205 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8206 	reg_val &= 0x00ffffff;
8207 	reg_val |= 0x8c000000;
8208 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8209 
8210 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8211 	reg_val &= 0xffffff00;
8212 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8213 
8214 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8215 	reg_val &= 0x00ffffff;
8216 	reg_val |= 0xb0000000;
8217 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8218 }
8219 
8220 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8221 					 const struct intel_link_m_n *m_n)
8222 {
8223 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8224 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8225 	enum pipe pipe = crtc->pipe;
8226 
8227 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8228 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
8229 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8230 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8231 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
8232 }
8233 
8234 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8235 				 enum transcoder transcoder)
8236 {
8237 	if (IS_HASWELL(dev_priv))
8238 		return transcoder == TRANSCODER_EDP;
8239 
8240 	/*
8241 	 * Strictly speaking some registers are available before
8242 	 * gen7, but we only support DRRS on gen7+
8243 	 */
8244 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
8245 }
8246 
8247 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8248 					 const struct intel_link_m_n *m_n,
8249 					 const struct intel_link_m_n *m2_n2)
8250 {
8251 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8252 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8253 	enum pipe pipe = crtc->pipe;
8254 	enum transcoder transcoder = crtc_state->cpu_transcoder;
8255 
8256 	if (INTEL_GEN(dev_priv) >= 5) {
8257 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8258 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8259 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8260 			       m_n->gmch_n);
8261 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8262 			       m_n->link_m);
8263 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8264 			       m_n->link_n);
8265 		/*
8266 		 *  M2_N2 registers are set only if DRRS is supported
8267 		 * (to make sure the registers are not unnecessarily accessed).
8268 		 */
8269 		if (m2_n2 && crtc_state->has_drrs &&
8270 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
8271 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8272 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8273 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8274 				       m2_n2->gmch_n);
8275 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8276 				       m2_n2->link_m);
8277 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8278 				       m2_n2->link_n);
8279 		}
8280 	} else {
8281 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8282 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8283 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8284 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8285 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
8286 	}
8287 }
8288 
8289 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
8290 {
8291 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
8292 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
8293 
8294 	if (m_n == M1_N1) {
8295 		dp_m_n = &crtc_state->dp_m_n;
8296 		dp_m2_n2 = &crtc_state->dp_m2_n2;
8297 	} else if (m_n == M2_N2) {
8298 
8299 		/*
8300 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
8301 		 * needs to be programmed into M1_N1.
8302 		 */
8303 		dp_m_n = &crtc_state->dp_m2_n2;
8304 	} else {
8305 		drm_err(&i915->drm, "Unsupported divider value\n");
8306 		return;
8307 	}
8308 
8309 	if (crtc_state->has_pch_encoder)
8310 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
8311 	else
8312 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
8313 }
8314 
8315 static void vlv_compute_dpll(struct intel_crtc *crtc,
8316 			     struct intel_crtc_state *pipe_config)
8317 {
8318 	pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
8319 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8320 	if (crtc->pipe != PIPE_A)
8321 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8322 
8323 	/* DPLL not used with DSI, but still need the rest set up */
8324 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8325 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8326 			DPLL_EXT_BUFFER_ENABLE_VLV;
8327 
8328 	pipe_config->dpll_hw_state.dpll_md =
8329 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8330 }
8331 
8332 static void chv_compute_dpll(struct intel_crtc *crtc,
8333 			     struct intel_crtc_state *pipe_config)
8334 {
8335 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
8336 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8337 	if (crtc->pipe != PIPE_A)
8338 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8339 
8340 	/* DPLL not used with DSI, but still need the rest set up */
8341 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8342 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8343 
8344 	pipe_config->dpll_hw_state.dpll_md =
8345 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8346 }
8347 
8348 static void vlv_prepare_pll(struct intel_crtc *crtc,
8349 			    const struct intel_crtc_state *pipe_config)
8350 {
8351 	struct drm_device *dev = crtc->base.dev;
8352 	struct drm_i915_private *dev_priv = to_i915(dev);
8353 	enum pipe pipe = crtc->pipe;
8354 	u32 mdiv;
8355 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
8356 	u32 coreclk, reg_val;
8357 
8358 	/* Enable Refclk */
8359 	intel_de_write(dev_priv, DPLL(pipe),
8360 		       pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
8361 
8362 	/* No need to actually set up the DPLL with DSI */
8363 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8364 		return;
8365 
8366 	vlv_dpio_get(dev_priv);
8367 
8368 	bestn = pipe_config->dpll.n;
8369 	bestm1 = pipe_config->dpll.m1;
8370 	bestm2 = pipe_config->dpll.m2;
8371 	bestp1 = pipe_config->dpll.p1;
8372 	bestp2 = pipe_config->dpll.p2;
8373 
8374 	/* See eDP HDMI DPIO driver vbios notes doc */
8375 
8376 	/* PLL B needs special handling */
8377 	if (pipe == PIPE_B)
8378 		vlv_pllb_recal_opamp(dev_priv, pipe);
8379 
8380 	/* Set up Tx target for periodic Rcomp update */
8381 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
8382 
8383 	/* Disable target IRef on PLL */
8384 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
8385 	reg_val &= 0x00ffffff;
8386 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
8387 
8388 	/* Disable fast lock */
8389 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
8390 
8391 	/* Set idtafcrecal before PLL is enabled */
8392 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8393 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8394 	mdiv |= ((bestn << DPIO_N_SHIFT));
8395 	mdiv |= (1 << DPIO_K_SHIFT);
8396 
8397 	/*
8398 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8399 	 * but we don't support that).
8400 	 * Note: don't use the DAC post divider as it seems unstable.
8401 	 */
8402 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
8403 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8404 
8405 	mdiv |= DPIO_ENABLE_CALIBRATION;
8406 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8407 
8408 	/* Set HBR and RBR LPF coefficients */
8409 	if (pipe_config->port_clock == 162000 ||
8410 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8411 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
8412 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8413 				 0x009f0003);
8414 	else
8415 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8416 				 0x00d0000f);
8417 
8418 	if (intel_crtc_has_dp_encoder(pipe_config)) {
8419 		/* Use SSC source */
8420 		if (pipe == PIPE_A)
8421 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8422 					 0x0df40000);
8423 		else
8424 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8425 					 0x0df70000);
8426 	} else { /* HDMI or VGA */
8427 		/* Use bend source */
8428 		if (pipe == PIPE_A)
8429 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8430 					 0x0df70000);
8431 		else
8432 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8433 					 0x0df40000);
8434 	}
8435 
8436 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
8437 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
8438 	if (intel_crtc_has_dp_encoder(pipe_config))
8439 		coreclk |= 0x01000000;
8440 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
8441 
8442 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
8443 
8444 	vlv_dpio_put(dev_priv);
8445 }
8446 
8447 static void chv_prepare_pll(struct intel_crtc *crtc,
8448 			    const struct intel_crtc_state *pipe_config)
8449 {
8450 	struct drm_device *dev = crtc->base.dev;
8451 	struct drm_i915_private *dev_priv = to_i915(dev);
8452 	enum pipe pipe = crtc->pipe;
8453 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8454 	u32 loopfilter, tribuf_calcntr;
8455 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
8456 	u32 dpio_val;
8457 	int vco;
8458 
8459 	/* Enable Refclk and SSC */
8460 	intel_de_write(dev_priv, DPLL(pipe),
8461 		       pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8462 
8463 	/* No need to actually set up the DPLL with DSI */
8464 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8465 		return;
8466 
8467 	bestn = pipe_config->dpll.n;
8468 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8469 	bestm1 = pipe_config->dpll.m1;
8470 	bestm2 = pipe_config->dpll.m2 >> 22;
8471 	bestp1 = pipe_config->dpll.p1;
8472 	bestp2 = pipe_config->dpll.p2;
8473 	vco = pipe_config->dpll.vco;
8474 	dpio_val = 0;
8475 	loopfilter = 0;
8476 
8477 	vlv_dpio_get(dev_priv);
8478 
8479 	/* p1 and p2 divider */
8480 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8481 			5 << DPIO_CHV_S1_DIV_SHIFT |
8482 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8483 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8484 			1 << DPIO_CHV_K_DIV_SHIFT);
8485 
8486 	/* Feedback post-divider - m2 */
8487 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8488 
8489 	/* Feedback refclk divider - n and m1 */
8490 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8491 			DPIO_CHV_M1_DIV_BY_2 |
8492 			1 << DPIO_CHV_N_DIV_SHIFT);
8493 
8494 	/* M2 fraction division */
8495 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
8496 
8497 	/* M2 fraction division enable */
8498 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8499 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8500 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8501 	if (bestm2_frac)
8502 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8503 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
8504 
8505 	/* Program digital lock detect threshold */
8506 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8507 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8508 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8509 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8510 	if (!bestm2_frac)
8511 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8512 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8513 
8514 	/* Loop filter */
8515 	if (vco == 5400000) {
8516 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8517 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8518 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8519 		tribuf_calcntr = 0x9;
8520 	} else if (vco <= 6200000) {
8521 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8522 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8523 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8524 		tribuf_calcntr = 0x9;
8525 	} else if (vco <= 6480000) {
8526 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8527 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8528 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8529 		tribuf_calcntr = 0x8;
8530 	} else {
8531 		/* Not supported. Apply the same limits as in the max case */
8532 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8533 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8534 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8535 		tribuf_calcntr = 0;
8536 	}
8537 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8538 
8539 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
8540 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8541 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8542 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8543 
8544 	/* AFC Recal */
8545 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8546 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8547 			DPIO_AFC_RECAL);
8548 
8549 	vlv_dpio_put(dev_priv);
8550 }
8551 
8552 /**
8553  * vlv_force_pll_on - forcibly enable just the PLL
8554  * @dev_priv: i915 private structure
8555  * @pipe: pipe PLL to enable
8556  * @dpll: PLL configuration
8557  *
8558  * Enable the PLL for @pipe using the supplied @dpll config. To be used
8559  * in cases where we need the PLL enabled even when @pipe is not going to
8560  * be enabled.
8561  */
8562 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
8563 		     const struct dpll *dpll)
8564 {
8565 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
8566 	struct intel_crtc_state *pipe_config;
8567 
8568 	pipe_config = intel_crtc_state_alloc(crtc);
8569 	if (!pipe_config)
8570 		return -ENOMEM;
8571 
8572 	pipe_config->cpu_transcoder = (enum transcoder)pipe;
8573 	pipe_config->pixel_multiplier = 1;
8574 	pipe_config->dpll = *dpll;
8575 
8576 	if (IS_CHERRYVIEW(dev_priv)) {
8577 		chv_compute_dpll(crtc, pipe_config);
8578 		chv_prepare_pll(crtc, pipe_config);
8579 		chv_enable_pll(crtc, pipe_config);
8580 	} else {
8581 		vlv_compute_dpll(crtc, pipe_config);
8582 		vlv_prepare_pll(crtc, pipe_config);
8583 		vlv_enable_pll(crtc, pipe_config);
8584 	}
8585 
8586 	kfree(pipe_config);
8587 
8588 	return 0;
8589 }
8590 
8591 /**
8592  * vlv_force_pll_off - forcibly disable just the PLL
8593  * @dev_priv: i915 private structure
8594  * @pipe: pipe PLL to disable
8595  *
8596  * Disable the PLL for @pipe. To be used in cases where we need
8597  * the PLL enabled even when @pipe is not going to be enabled.
8598  */
8599 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
8600 {
8601 	if (IS_CHERRYVIEW(dev_priv))
8602 		chv_disable_pll(dev_priv, pipe);
8603 	else
8604 		vlv_disable_pll(dev_priv, pipe);
8605 }
8606 
8607 static void i9xx_compute_dpll(struct intel_crtc *crtc,
8608 			      struct intel_crtc_state *crtc_state,
8609 			      struct dpll *reduced_clock)
8610 {
8611 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8612 	u32 dpll;
8613 	struct dpll *clock = &crtc_state->dpll;
8614 
8615 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8616 
8617 	dpll = DPLL_VGA_MODE_DIS;
8618 
8619 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
8620 		dpll |= DPLLB_MODE_LVDS;
8621 	else
8622 		dpll |= DPLLB_MODE_DAC_SERIAL;
8623 
8624 	if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8625 	    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8626 		dpll |= (crtc_state->pixel_multiplier - 1)
8627 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
8628 	}
8629 
8630 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8631 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8632 		dpll |= DPLL_SDVO_HIGH_SPEED;
8633 
8634 	if (intel_crtc_has_dp_encoder(crtc_state))
8635 		dpll |= DPLL_SDVO_HIGH_SPEED;
8636 
8637 	/* compute bitmask from p1 value */
8638 	if (IS_PINEVIEW(dev_priv))
8639 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8640 	else {
8641 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8642 		if (IS_G4X(dev_priv) && reduced_clock)
8643 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8644 	}
8645 	switch (clock->p2) {
8646 	case 5:
8647 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8648 		break;
8649 	case 7:
8650 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8651 		break;
8652 	case 10:
8653 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8654 		break;
8655 	case 14:
8656 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8657 		break;
8658 	}
8659 	if (INTEL_GEN(dev_priv) >= 4)
8660 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8661 
8662 	if (crtc_state->sdvo_tv_clock)
8663 		dpll |= PLL_REF_INPUT_TVCLKINBC;
8664 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8665 		 intel_panel_use_ssc(dev_priv))
8666 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8667 	else
8668 		dpll |= PLL_REF_INPUT_DREFCLK;
8669 
8670 	dpll |= DPLL_VCO_ENABLE;
8671 	crtc_state->dpll_hw_state.dpll = dpll;
8672 
8673 	if (INTEL_GEN(dev_priv) >= 4) {
8674 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8675 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
8676 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
8677 	}
8678 }
8679 
8680 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8681 			      struct intel_crtc_state *crtc_state,
8682 			      struct dpll *reduced_clock)
8683 {
8684 	struct drm_device *dev = crtc->base.dev;
8685 	struct drm_i915_private *dev_priv = to_i915(dev);
8686 	u32 dpll;
8687 	struct dpll *clock = &crtc_state->dpll;
8688 
8689 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8690 
8691 	dpll = DPLL_VGA_MODE_DIS;
8692 
8693 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8694 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8695 	} else {
8696 		if (clock->p1 == 2)
8697 			dpll |= PLL_P1_DIVIDE_BY_TWO;
8698 		else
8699 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8700 		if (clock->p2 == 4)
8701 			dpll |= PLL_P2_DIVIDE_BY_4;
8702 	}
8703 
8704 	/*
8705 	 * Bspec:
8706 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8707 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8708 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8709 	 *  Enable) must be set to “1” in both the DPLL A Control Register
8710 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8711 	 *
8712 	 * For simplicity We simply keep both bits always enabled in
8713 	 * both DPLLS. The spec says we should disable the DVO 2X clock
8714 	 * when not needed, but this seems to work fine in practice.
8715 	 */
8716 	if (IS_I830(dev_priv) ||
8717 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8718 		dpll |= DPLL_DVO_2X_MODE;
8719 
8720 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8721 	    intel_panel_use_ssc(dev_priv))
8722 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8723 	else
8724 		dpll |= PLL_REF_INPUT_DREFCLK;
8725 
8726 	dpll |= DPLL_VCO_ENABLE;
8727 	crtc_state->dpll_hw_state.dpll = dpll;
8728 }
8729 
8730 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
8731 {
8732 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8733 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8734 	enum pipe pipe = crtc->pipe;
8735 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8736 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8737 	u32 crtc_vtotal, crtc_vblank_end;
8738 	int vsyncshift = 0;
8739 
8740 	/* We need to be careful not to changed the adjusted mode, for otherwise
8741 	 * the hw state checker will get angry at the mismatch. */
8742 	crtc_vtotal = adjusted_mode->crtc_vtotal;
8743 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8744 
8745 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8746 		/* the chip adds 2 halflines automatically */
8747 		crtc_vtotal -= 1;
8748 		crtc_vblank_end -= 1;
8749 
8750 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8751 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8752 		else
8753 			vsyncshift = adjusted_mode->crtc_hsync_start -
8754 				adjusted_mode->crtc_htotal / 2;
8755 		if (vsyncshift < 0)
8756 			vsyncshift += adjusted_mode->crtc_htotal;
8757 	}
8758 
8759 	if (INTEL_GEN(dev_priv) > 3)
8760 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
8761 		               vsyncshift);
8762 
8763 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
8764 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
8765 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
8766 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
8767 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
8768 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
8769 
8770 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
8771 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
8772 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
8773 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
8774 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
8775 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
8776 
8777 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8778 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8779 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8780 	 * bits. */
8781 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
8782 	    (pipe == PIPE_B || pipe == PIPE_C))
8783 		intel_de_write(dev_priv, VTOTAL(pipe),
8784 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
8785 
8786 }
8787 
8788 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
8789 {
8790 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8791 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8792 	enum pipe pipe = crtc->pipe;
8793 
8794 	/* pipesrc controls the size that is scaled from, which should
8795 	 * always be the user's requested size.
8796 	 */
8797 	intel_de_write(dev_priv, PIPESRC(pipe),
8798 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
8799 }
8800 
8801 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
8802 {
8803 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8804 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8805 
8806 	if (IS_GEN(dev_priv, 2))
8807 		return false;
8808 
8809 	if (INTEL_GEN(dev_priv) >= 9 ||
8810 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
8811 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
8812 	else
8813 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
8814 }
8815 
8816 static void intel_get_pipe_timings(struct intel_crtc *crtc,
8817 				   struct intel_crtc_state *pipe_config)
8818 {
8819 	struct drm_device *dev = crtc->base.dev;
8820 	struct drm_i915_private *dev_priv = to_i915(dev);
8821 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8822 	u32 tmp;
8823 
8824 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
8825 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8826 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
8827 
8828 	if (!transcoder_is_dsi(cpu_transcoder)) {
8829 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
8830 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
8831 							(tmp & 0xffff) + 1;
8832 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
8833 						((tmp >> 16) & 0xffff) + 1;
8834 	}
8835 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
8836 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8837 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
8838 
8839 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
8840 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8841 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
8842 
8843 	if (!transcoder_is_dsi(cpu_transcoder)) {
8844 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
8845 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
8846 							(tmp & 0xffff) + 1;
8847 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
8848 						((tmp >> 16) & 0xffff) + 1;
8849 	}
8850 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
8851 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8852 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
8853 
8854 	if (intel_pipe_is_interlaced(pipe_config)) {
8855 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8856 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
8857 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
8858 	}
8859 }
8860 
8861 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8862 				    struct intel_crtc_state *pipe_config)
8863 {
8864 	struct drm_device *dev = crtc->base.dev;
8865 	struct drm_i915_private *dev_priv = to_i915(dev);
8866 	u32 tmp;
8867 
8868 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
8869 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8870 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8871 
8872 	pipe_config->hw.mode.vdisplay = pipe_config->pipe_src_h;
8873 	pipe_config->hw.mode.hdisplay = pipe_config->pipe_src_w;
8874 }
8875 
8876 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
8877 				 struct intel_crtc_state *pipe_config)
8878 {
8879 	mode->hdisplay = pipe_config->hw.adjusted_mode.crtc_hdisplay;
8880 	mode->htotal = pipe_config->hw.adjusted_mode.crtc_htotal;
8881 	mode->hsync_start = pipe_config->hw.adjusted_mode.crtc_hsync_start;
8882 	mode->hsync_end = pipe_config->hw.adjusted_mode.crtc_hsync_end;
8883 
8884 	mode->vdisplay = pipe_config->hw.adjusted_mode.crtc_vdisplay;
8885 	mode->vtotal = pipe_config->hw.adjusted_mode.crtc_vtotal;
8886 	mode->vsync_start = pipe_config->hw.adjusted_mode.crtc_vsync_start;
8887 	mode->vsync_end = pipe_config->hw.adjusted_mode.crtc_vsync_end;
8888 
8889 	mode->flags = pipe_config->hw.adjusted_mode.flags;
8890 	mode->type = DRM_MODE_TYPE_DRIVER;
8891 
8892 	mode->clock = pipe_config->hw.adjusted_mode.crtc_clock;
8893 
8894 	drm_mode_set_name(mode);
8895 }
8896 
8897 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
8898 {
8899 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8900 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8901 	u32 pipeconf;
8902 
8903 	pipeconf = 0;
8904 
8905 	/* we keep both pipes enabled on 830 */
8906 	if (IS_I830(dev_priv))
8907 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
8908 
8909 	if (crtc_state->double_wide)
8910 		pipeconf |= PIPECONF_DOUBLE_WIDE;
8911 
8912 	/* only g4x and later have fancy bpc/dither controls */
8913 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8914 	    IS_CHERRYVIEW(dev_priv)) {
8915 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
8916 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
8917 			pipeconf |= PIPECONF_DITHER_EN |
8918 				    PIPECONF_DITHER_TYPE_SP;
8919 
8920 		switch (crtc_state->pipe_bpp) {
8921 		case 18:
8922 			pipeconf |= PIPECONF_6BPC;
8923 			break;
8924 		case 24:
8925 			pipeconf |= PIPECONF_8BPC;
8926 			break;
8927 		case 30:
8928 			pipeconf |= PIPECONF_10BPC;
8929 			break;
8930 		default:
8931 			/* Case prevented by intel_choose_pipe_bpp_dither. */
8932 			BUG();
8933 		}
8934 	}
8935 
8936 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
8937 		if (INTEL_GEN(dev_priv) < 4 ||
8938 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8939 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8940 		else
8941 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8942 	} else {
8943 		pipeconf |= PIPECONF_PROGRESSIVE;
8944 	}
8945 
8946 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
8947 	     crtc_state->limited_color_range)
8948 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
8949 
8950 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
8951 
8952 	pipeconf |= PIPECONF_FRAME_START_DELAY(0);
8953 
8954 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
8955 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
8956 }
8957 
8958 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8959 				   struct intel_crtc_state *crtc_state)
8960 {
8961 	struct drm_device *dev = crtc->base.dev;
8962 	struct drm_i915_private *dev_priv = to_i915(dev);
8963 	const struct intel_limit *limit;
8964 	int refclk = 48000;
8965 
8966 	memset(&crtc_state->dpll_hw_state, 0,
8967 	       sizeof(crtc_state->dpll_hw_state));
8968 
8969 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8970 		if (intel_panel_use_ssc(dev_priv)) {
8971 			refclk = dev_priv->vbt.lvds_ssc_freq;
8972 			drm_dbg_kms(&dev_priv->drm,
8973 				    "using SSC reference clock of %d kHz\n",
8974 				    refclk);
8975 		}
8976 
8977 		limit = &intel_limits_i8xx_lvds;
8978 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
8979 		limit = &intel_limits_i8xx_dvo;
8980 	} else {
8981 		limit = &intel_limits_i8xx_dac;
8982 	}
8983 
8984 	if (!crtc_state->clock_set &&
8985 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8986 				 refclk, NULL, &crtc_state->dpll)) {
8987 		drm_err(&dev_priv->drm,
8988 			"Couldn't find PLL settings for mode!\n");
8989 		return -EINVAL;
8990 	}
8991 
8992 	i8xx_compute_dpll(crtc, crtc_state, NULL);
8993 
8994 	return 0;
8995 }
8996 
8997 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8998 				  struct intel_crtc_state *crtc_state)
8999 {
9000 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9001 	const struct intel_limit *limit;
9002 	int refclk = 96000;
9003 
9004 	memset(&crtc_state->dpll_hw_state, 0,
9005 	       sizeof(crtc_state->dpll_hw_state));
9006 
9007 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9008 		if (intel_panel_use_ssc(dev_priv)) {
9009 			refclk = dev_priv->vbt.lvds_ssc_freq;
9010 			drm_dbg_kms(&dev_priv->drm,
9011 				    "using SSC reference clock of %d kHz\n",
9012 				    refclk);
9013 		}
9014 
9015 		if (intel_is_dual_link_lvds(dev_priv))
9016 			limit = &intel_limits_g4x_dual_channel_lvds;
9017 		else
9018 			limit = &intel_limits_g4x_single_channel_lvds;
9019 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9020 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
9021 		limit = &intel_limits_g4x_hdmi;
9022 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
9023 		limit = &intel_limits_g4x_sdvo;
9024 	} else {
9025 		/* The option is for other outputs */
9026 		limit = &intel_limits_i9xx_sdvo;
9027 	}
9028 
9029 	if (!crtc_state->clock_set &&
9030 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9031 				refclk, NULL, &crtc_state->dpll)) {
9032 		drm_err(&dev_priv->drm,
9033 			"Couldn't find PLL settings for mode!\n");
9034 		return -EINVAL;
9035 	}
9036 
9037 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9038 
9039 	return 0;
9040 }
9041 
9042 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9043 				  struct intel_crtc_state *crtc_state)
9044 {
9045 	struct drm_device *dev = crtc->base.dev;
9046 	struct drm_i915_private *dev_priv = to_i915(dev);
9047 	const struct intel_limit *limit;
9048 	int refclk = 96000;
9049 
9050 	memset(&crtc_state->dpll_hw_state, 0,
9051 	       sizeof(crtc_state->dpll_hw_state));
9052 
9053 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9054 		if (intel_panel_use_ssc(dev_priv)) {
9055 			refclk = dev_priv->vbt.lvds_ssc_freq;
9056 			drm_dbg_kms(&dev_priv->drm,
9057 				    "using SSC reference clock of %d kHz\n",
9058 				    refclk);
9059 		}
9060 
9061 		limit = &pnv_limits_lvds;
9062 	} else {
9063 		limit = &pnv_limits_sdvo;
9064 	}
9065 
9066 	if (!crtc_state->clock_set &&
9067 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9068 				refclk, NULL, &crtc_state->dpll)) {
9069 		drm_err(&dev_priv->drm,
9070 			"Couldn't find PLL settings for mode!\n");
9071 		return -EINVAL;
9072 	}
9073 
9074 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9075 
9076 	return 0;
9077 }
9078 
9079 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9080 				   struct intel_crtc_state *crtc_state)
9081 {
9082 	struct drm_device *dev = crtc->base.dev;
9083 	struct drm_i915_private *dev_priv = to_i915(dev);
9084 	const struct intel_limit *limit;
9085 	int refclk = 96000;
9086 
9087 	memset(&crtc_state->dpll_hw_state, 0,
9088 	       sizeof(crtc_state->dpll_hw_state));
9089 
9090 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9091 		if (intel_panel_use_ssc(dev_priv)) {
9092 			refclk = dev_priv->vbt.lvds_ssc_freq;
9093 			drm_dbg_kms(&dev_priv->drm,
9094 				    "using SSC reference clock of %d kHz\n",
9095 				    refclk);
9096 		}
9097 
9098 		limit = &intel_limits_i9xx_lvds;
9099 	} else {
9100 		limit = &intel_limits_i9xx_sdvo;
9101 	}
9102 
9103 	if (!crtc_state->clock_set &&
9104 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9105 				 refclk, NULL, &crtc_state->dpll)) {
9106 		drm_err(&dev_priv->drm,
9107 			"Couldn't find PLL settings for mode!\n");
9108 		return -EINVAL;
9109 	}
9110 
9111 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9112 
9113 	return 0;
9114 }
9115 
9116 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9117 				  struct intel_crtc_state *crtc_state)
9118 {
9119 	int refclk = 100000;
9120 	const struct intel_limit *limit = &intel_limits_chv;
9121 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9122 
9123 	memset(&crtc_state->dpll_hw_state, 0,
9124 	       sizeof(crtc_state->dpll_hw_state));
9125 
9126 	if (!crtc_state->clock_set &&
9127 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9128 				refclk, NULL, &crtc_state->dpll)) {
9129 		drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
9130 		return -EINVAL;
9131 	}
9132 
9133 	chv_compute_dpll(crtc, crtc_state);
9134 
9135 	return 0;
9136 }
9137 
9138 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9139 				  struct intel_crtc_state *crtc_state)
9140 {
9141 	int refclk = 100000;
9142 	const struct intel_limit *limit = &intel_limits_vlv;
9143 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9144 
9145 	memset(&crtc_state->dpll_hw_state, 0,
9146 	       sizeof(crtc_state->dpll_hw_state));
9147 
9148 	if (!crtc_state->clock_set &&
9149 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9150 				refclk, NULL, &crtc_state->dpll)) {
9151 		drm_err(&i915->drm,  "Couldn't find PLL settings for mode!\n");
9152 		return -EINVAL;
9153 	}
9154 
9155 	vlv_compute_dpll(crtc, crtc_state);
9156 
9157 	return 0;
9158 }
9159 
9160 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9161 {
9162 	if (IS_I830(dev_priv))
9163 		return false;
9164 
9165 	return INTEL_GEN(dev_priv) >= 4 ||
9166 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9167 }
9168 
9169 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
9170 				 struct intel_crtc_state *pipe_config)
9171 {
9172 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9173 	u32 tmp;
9174 
9175 	if (!i9xx_has_pfit(dev_priv))
9176 		return;
9177 
9178 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
9179 	if (!(tmp & PFIT_ENABLE))
9180 		return;
9181 
9182 	/* Check whether the pfit is attached to our pipe. */
9183 	if (INTEL_GEN(dev_priv) < 4) {
9184 		if (crtc->pipe != PIPE_B)
9185 			return;
9186 	} else {
9187 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9188 			return;
9189 	}
9190 
9191 	pipe_config->gmch_pfit.control = tmp;
9192 	pipe_config->gmch_pfit.pgm_ratios = intel_de_read(dev_priv,
9193 							  PFIT_PGM_RATIOS);
9194 }
9195 
9196 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
9197 			       struct intel_crtc_state *pipe_config)
9198 {
9199 	struct drm_device *dev = crtc->base.dev;
9200 	struct drm_i915_private *dev_priv = to_i915(dev);
9201 	enum pipe pipe = crtc->pipe;
9202 	struct dpll clock;
9203 	u32 mdiv;
9204 	int refclk = 100000;
9205 
9206 	/* In case of DSI, DPLL will not be used */
9207 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9208 		return;
9209 
9210 	vlv_dpio_get(dev_priv);
9211 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
9212 	vlv_dpio_put(dev_priv);
9213 
9214 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9215 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
9216 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9217 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9218 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9219 
9220 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
9221 }
9222 
9223 static void
9224 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9225 			      struct intel_initial_plane_config *plane_config)
9226 {
9227 	struct drm_device *dev = crtc->base.dev;
9228 	struct drm_i915_private *dev_priv = to_i915(dev);
9229 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9230 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9231 	enum pipe pipe;
9232 	u32 val, base, offset;
9233 	int fourcc, pixel_format;
9234 	unsigned int aligned_height;
9235 	struct drm_framebuffer *fb;
9236 	struct intel_framebuffer *intel_fb;
9237 
9238 	if (!plane->get_hw_state(plane, &pipe))
9239 		return;
9240 
9241 	drm_WARN_ON(dev, pipe != crtc->pipe);
9242 
9243 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9244 	if (!intel_fb) {
9245 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
9246 		return;
9247 	}
9248 
9249 	fb = &intel_fb->base;
9250 
9251 	fb->dev = dev;
9252 
9253 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9254 
9255 	if (INTEL_GEN(dev_priv) >= 4) {
9256 		if (val & DISPPLANE_TILED) {
9257 			plane_config->tiling = I915_TILING_X;
9258 			fb->modifier = I915_FORMAT_MOD_X_TILED;
9259 		}
9260 
9261 		if (val & DISPPLANE_ROTATE_180)
9262 			plane_config->rotation = DRM_MODE_ROTATE_180;
9263 	}
9264 
9265 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9266 	    val & DISPPLANE_MIRROR)
9267 		plane_config->rotation |= DRM_MODE_REFLECT_X;
9268 
9269 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9270 	fourcc = i9xx_format_to_fourcc(pixel_format);
9271 	fb->format = drm_format_info(fourcc);
9272 
9273 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
9274 		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9275 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9276 	} else if (INTEL_GEN(dev_priv) >= 4) {
9277 		if (plane_config->tiling)
9278 			offset = intel_de_read(dev_priv,
9279 					       DSPTILEOFF(i9xx_plane));
9280 		else
9281 			offset = intel_de_read(dev_priv,
9282 					       DSPLINOFF(i9xx_plane));
9283 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9284 	} else {
9285 		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
9286 	}
9287 	plane_config->base = base;
9288 
9289 	val = intel_de_read(dev_priv, PIPESRC(pipe));
9290 	fb->width = ((val >> 16) & 0xfff) + 1;
9291 	fb->height = ((val >> 0) & 0xfff) + 1;
9292 
9293 	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
9294 	fb->pitches[0] = val & 0xffffffc0;
9295 
9296 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
9297 
9298 	plane_config->size = fb->pitches[0] * aligned_height;
9299 
9300 	drm_dbg_kms(&dev_priv->drm,
9301 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9302 		    crtc->base.name, plane->base.name, fb->width, fb->height,
9303 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
9304 		    plane_config->size);
9305 
9306 	plane_config->fb = intel_fb;
9307 }
9308 
9309 static void chv_crtc_clock_get(struct intel_crtc *crtc,
9310 			       struct intel_crtc_state *pipe_config)
9311 {
9312 	struct drm_device *dev = crtc->base.dev;
9313 	struct drm_i915_private *dev_priv = to_i915(dev);
9314 	enum pipe pipe = crtc->pipe;
9315 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
9316 	struct dpll clock;
9317 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
9318 	int refclk = 100000;
9319 
9320 	/* In case of DSI, DPLL will not be used */
9321 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9322 		return;
9323 
9324 	vlv_dpio_get(dev_priv);
9325 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9326 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9327 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9328 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
9329 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
9330 	vlv_dpio_put(dev_priv);
9331 
9332 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
9333 	clock.m2 = (pll_dw0 & 0xff) << 22;
9334 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9335 		clock.m2 |= pll_dw2 & 0x3fffff;
9336 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9337 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9338 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9339 
9340 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
9341 }
9342 
9343 static enum intel_output_format
9344 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
9345 {
9346 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9347 	u32 tmp;
9348 
9349 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
9350 
9351 	if (tmp & PIPEMISC_YUV420_ENABLE) {
9352 		/* We support 4:2:0 in full blend mode only */
9353 		drm_WARN_ON(&dev_priv->drm,
9354 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
9355 
9356 		return INTEL_OUTPUT_FORMAT_YCBCR420;
9357 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9358 		return INTEL_OUTPUT_FORMAT_YCBCR444;
9359 	} else {
9360 		return INTEL_OUTPUT_FORMAT_RGB;
9361 	}
9362 }
9363 
9364 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9365 {
9366 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9367 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9368 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9369 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9370 	u32 tmp;
9371 
9372 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9373 
9374 	if (tmp & DISPPLANE_GAMMA_ENABLE)
9375 		crtc_state->gamma_enable = true;
9376 
9377 	if (!HAS_GMCH(dev_priv) &&
9378 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
9379 		crtc_state->csc_enable = true;
9380 }
9381 
9382 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
9383 				 struct intel_crtc_state *pipe_config)
9384 {
9385 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9386 	enum intel_display_power_domain power_domain;
9387 	intel_wakeref_t wakeref;
9388 	u32 tmp;
9389 	bool ret;
9390 
9391 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9392 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9393 	if (!wakeref)
9394 		return false;
9395 
9396 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9397 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9398 	pipe_config->shared_dpll = NULL;
9399 	pipe_config->master_transcoder = INVALID_TRANSCODER;
9400 
9401 	ret = false;
9402 
9403 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
9404 	if (!(tmp & PIPECONF_ENABLE))
9405 		goto out;
9406 
9407 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9408 	    IS_CHERRYVIEW(dev_priv)) {
9409 		switch (tmp & PIPECONF_BPC_MASK) {
9410 		case PIPECONF_6BPC:
9411 			pipe_config->pipe_bpp = 18;
9412 			break;
9413 		case PIPECONF_8BPC:
9414 			pipe_config->pipe_bpp = 24;
9415 			break;
9416 		case PIPECONF_10BPC:
9417 			pipe_config->pipe_bpp = 30;
9418 			break;
9419 		default:
9420 			break;
9421 		}
9422 	}
9423 
9424 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9425 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
9426 		pipe_config->limited_color_range = true;
9427 
9428 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9429 		PIPECONF_GAMMA_MODE_SHIFT;
9430 
9431 	if (IS_CHERRYVIEW(dev_priv))
9432 		pipe_config->cgm_mode = intel_de_read(dev_priv,
9433 						      CGM_PIPE_MODE(crtc->pipe));
9434 
9435 	i9xx_get_pipe_color_config(pipe_config);
9436 	intel_color_get_config(pipe_config);
9437 
9438 	if (INTEL_GEN(dev_priv) < 4)
9439 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9440 
9441 	intel_get_pipe_timings(crtc, pipe_config);
9442 	intel_get_pipe_src_size(crtc, pipe_config);
9443 
9444 	i9xx_get_pfit_config(crtc, pipe_config);
9445 
9446 	if (INTEL_GEN(dev_priv) >= 4) {
9447 		/* No way to read it out on pipes B and C */
9448 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
9449 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
9450 		else
9451 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
9452 		pipe_config->pixel_multiplier =
9453 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9454 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
9455 		pipe_config->dpll_hw_state.dpll_md = tmp;
9456 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
9457 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
9458 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
9459 		pipe_config->pixel_multiplier =
9460 			((tmp & SDVO_MULTIPLIER_MASK)
9461 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9462 	} else {
9463 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
9464 		 * port and will be fixed up in the encoder->get_config
9465 		 * function. */
9466 		pipe_config->pixel_multiplier = 1;
9467 	}
9468 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9469 							DPLL(crtc->pipe));
9470 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
9471 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9472 							       FP0(crtc->pipe));
9473 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9474 							       FP1(crtc->pipe));
9475 	} else {
9476 		/* Mask out read-only status bits. */
9477 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9478 						     DPLL_PORTC_READY_MASK |
9479 						     DPLL_PORTB_READY_MASK);
9480 	}
9481 
9482 	if (IS_CHERRYVIEW(dev_priv))
9483 		chv_crtc_clock_get(crtc, pipe_config);
9484 	else if (IS_VALLEYVIEW(dev_priv))
9485 		vlv_crtc_clock_get(crtc, pipe_config);
9486 	else
9487 		i9xx_crtc_clock_get(crtc, pipe_config);
9488 
9489 	/*
9490 	 * Normally the dotclock is filled in by the encoder .get_config()
9491 	 * but in case the pipe is enabled w/o any ports we need a sane
9492 	 * default.
9493 	 */
9494 	pipe_config->hw.adjusted_mode.crtc_clock =
9495 		pipe_config->port_clock / pipe_config->pixel_multiplier;
9496 
9497 	ret = true;
9498 
9499 out:
9500 	intel_display_power_put(dev_priv, power_domain, wakeref);
9501 
9502 	return ret;
9503 }
9504 
9505 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
9506 {
9507 	struct intel_encoder *encoder;
9508 	int i;
9509 	u32 val, final;
9510 	bool has_lvds = false;
9511 	bool has_cpu_edp = false;
9512 	bool has_panel = false;
9513 	bool has_ck505 = false;
9514 	bool can_ssc = false;
9515 	bool using_ssc_source = false;
9516 
9517 	/* We need to take the global config into account */
9518 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9519 		switch (encoder->type) {
9520 		case INTEL_OUTPUT_LVDS:
9521 			has_panel = true;
9522 			has_lvds = true;
9523 			break;
9524 		case INTEL_OUTPUT_EDP:
9525 			has_panel = true;
9526 			if (encoder->port == PORT_A)
9527 				has_cpu_edp = true;
9528 			break;
9529 		default:
9530 			break;
9531 		}
9532 	}
9533 
9534 	if (HAS_PCH_IBX(dev_priv)) {
9535 		has_ck505 = dev_priv->vbt.display_clock_mode;
9536 		can_ssc = has_ck505;
9537 	} else {
9538 		has_ck505 = false;
9539 		can_ssc = true;
9540 	}
9541 
9542 	/* Check if any DPLLs are using the SSC source */
9543 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
9544 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
9545 
9546 		if (!(temp & DPLL_VCO_ENABLE))
9547 			continue;
9548 
9549 		if ((temp & PLL_REF_INPUT_MASK) ==
9550 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9551 			using_ssc_source = true;
9552 			break;
9553 		}
9554 	}
9555 
9556 	drm_dbg_kms(&dev_priv->drm,
9557 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9558 		    has_panel, has_lvds, has_ck505, using_ssc_source);
9559 
9560 	/* Ironlake: try to setup display ref clock before DPLL
9561 	 * enabling. This is only under driver's control after
9562 	 * PCH B stepping, previous chipset stepping should be
9563 	 * ignoring this setting.
9564 	 */
9565 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
9566 
9567 	/* As we must carefully and slowly disable/enable each source in turn,
9568 	 * compute the final state we want first and check if we need to
9569 	 * make any changes at all.
9570 	 */
9571 	final = val;
9572 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
9573 	if (has_ck505)
9574 		final |= DREF_NONSPREAD_CK505_ENABLE;
9575 	else
9576 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
9577 
9578 	final &= ~DREF_SSC_SOURCE_MASK;
9579 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9580 	final &= ~DREF_SSC1_ENABLE;
9581 
9582 	if (has_panel) {
9583 		final |= DREF_SSC_SOURCE_ENABLE;
9584 
9585 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
9586 			final |= DREF_SSC1_ENABLE;
9587 
9588 		if (has_cpu_edp) {
9589 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
9590 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9591 			else
9592 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9593 		} else
9594 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9595 	} else if (using_ssc_source) {
9596 		final |= DREF_SSC_SOURCE_ENABLE;
9597 		final |= DREF_SSC1_ENABLE;
9598 	}
9599 
9600 	if (final == val)
9601 		return;
9602 
9603 	/* Always enable nonspread source */
9604 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
9605 
9606 	if (has_ck505)
9607 		val |= DREF_NONSPREAD_CK505_ENABLE;
9608 	else
9609 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
9610 
9611 	if (has_panel) {
9612 		val &= ~DREF_SSC_SOURCE_MASK;
9613 		val |= DREF_SSC_SOURCE_ENABLE;
9614 
9615 		/* SSC must be turned on before enabling the CPU output  */
9616 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9617 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
9618 			val |= DREF_SSC1_ENABLE;
9619 		} else
9620 			val &= ~DREF_SSC1_ENABLE;
9621 
9622 		/* Get SSC going before enabling the outputs */
9623 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9624 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9625 		udelay(200);
9626 
9627 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9628 
9629 		/* Enable CPU source on CPU attached eDP */
9630 		if (has_cpu_edp) {
9631 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9632 				drm_dbg_kms(&dev_priv->drm,
9633 					    "Using SSC on eDP\n");
9634 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9635 			} else
9636 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9637 		} else
9638 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9639 
9640 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9641 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9642 		udelay(200);
9643 	} else {
9644 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
9645 
9646 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9647 
9648 		/* Turn off CPU output */
9649 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9650 
9651 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9652 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9653 		udelay(200);
9654 
9655 		if (!using_ssc_source) {
9656 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
9657 
9658 			/* Turn off the SSC source */
9659 			val &= ~DREF_SSC_SOURCE_MASK;
9660 			val |= DREF_SSC_SOURCE_DISABLE;
9661 
9662 			/* Turn off SSC1 */
9663 			val &= ~DREF_SSC1_ENABLE;
9664 
9665 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9666 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9667 			udelay(200);
9668 		}
9669 	}
9670 
9671 	BUG_ON(val != final);
9672 }
9673 
9674 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9675 {
9676 	u32 tmp;
9677 
9678 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9679 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9680 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9681 
9682 	if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9683 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9684 		drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
9685 
9686 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9687 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9688 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9689 
9690 	if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9691 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9692 		drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
9693 }
9694 
9695 /* WaMPhyProgramming:hsw */
9696 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9697 {
9698 	u32 tmp;
9699 
9700 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9701 	tmp &= ~(0xFF << 24);
9702 	tmp |= (0x12 << 24);
9703 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9704 
9705 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9706 	tmp |= (1 << 11);
9707 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9708 
9709 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9710 	tmp |= (1 << 11);
9711 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9712 
9713 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9714 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9715 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9716 
9717 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9718 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9719 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9720 
9721 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9722 	tmp &= ~(7 << 13);
9723 	tmp |= (5 << 13);
9724 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9725 
9726 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9727 	tmp &= ~(7 << 13);
9728 	tmp |= (5 << 13);
9729 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9730 
9731 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9732 	tmp &= ~0xFF;
9733 	tmp |= 0x1C;
9734 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9735 
9736 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9737 	tmp &= ~0xFF;
9738 	tmp |= 0x1C;
9739 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9740 
9741 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9742 	tmp &= ~(0xFF << 16);
9743 	tmp |= (0x1C << 16);
9744 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9745 
9746 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9747 	tmp &= ~(0xFF << 16);
9748 	tmp |= (0x1C << 16);
9749 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9750 
9751 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9752 	tmp |= (1 << 27);
9753 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9754 
9755 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9756 	tmp |= (1 << 27);
9757 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
9758 
9759 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9760 	tmp &= ~(0xF << 28);
9761 	tmp |= (4 << 28);
9762 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
9763 
9764 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9765 	tmp &= ~(0xF << 28);
9766 	tmp |= (4 << 28);
9767 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
9768 }
9769 
9770 /* Implements 3 different sequences from BSpec chapter "Display iCLK
9771  * Programming" based on the parameters passed:
9772  * - Sequence to enable CLKOUT_DP
9773  * - Sequence to enable CLKOUT_DP without spread
9774  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9775  */
9776 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9777 				 bool with_spread, bool with_fdi)
9778 {
9779 	u32 reg, tmp;
9780 
9781 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
9782 		     "FDI requires downspread\n"))
9783 		with_spread = true;
9784 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
9785 		     with_fdi, "LP PCH doesn't have FDI\n"))
9786 		with_fdi = false;
9787 
9788 	mutex_lock(&dev_priv->sb_lock);
9789 
9790 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9791 	tmp &= ~SBI_SSCCTL_DISABLE;
9792 	tmp |= SBI_SSCCTL_PATHALT;
9793 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9794 
9795 	udelay(24);
9796 
9797 	if (with_spread) {
9798 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9799 		tmp &= ~SBI_SSCCTL_PATHALT;
9800 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9801 
9802 		if (with_fdi) {
9803 			lpt_reset_fdi_mphy(dev_priv);
9804 			lpt_program_fdi_mphy(dev_priv);
9805 		}
9806 	}
9807 
9808 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9809 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9810 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9811 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9812 
9813 	mutex_unlock(&dev_priv->sb_lock);
9814 }
9815 
9816 /* Sequence to disable CLKOUT_DP */
9817 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
9818 {
9819 	u32 reg, tmp;
9820 
9821 	mutex_lock(&dev_priv->sb_lock);
9822 
9823 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9824 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9825 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9826 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9827 
9828 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9829 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
9830 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
9831 			tmp |= SBI_SSCCTL_PATHALT;
9832 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9833 			udelay(32);
9834 		}
9835 		tmp |= SBI_SSCCTL_DISABLE;
9836 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9837 	}
9838 
9839 	mutex_unlock(&dev_priv->sb_lock);
9840 }
9841 
9842 #define BEND_IDX(steps) ((50 + (steps)) / 5)
9843 
9844 static const u16 sscdivintphase[] = {
9845 	[BEND_IDX( 50)] = 0x3B23,
9846 	[BEND_IDX( 45)] = 0x3B23,
9847 	[BEND_IDX( 40)] = 0x3C23,
9848 	[BEND_IDX( 35)] = 0x3C23,
9849 	[BEND_IDX( 30)] = 0x3D23,
9850 	[BEND_IDX( 25)] = 0x3D23,
9851 	[BEND_IDX( 20)] = 0x3E23,
9852 	[BEND_IDX( 15)] = 0x3E23,
9853 	[BEND_IDX( 10)] = 0x3F23,
9854 	[BEND_IDX(  5)] = 0x3F23,
9855 	[BEND_IDX(  0)] = 0x0025,
9856 	[BEND_IDX( -5)] = 0x0025,
9857 	[BEND_IDX(-10)] = 0x0125,
9858 	[BEND_IDX(-15)] = 0x0125,
9859 	[BEND_IDX(-20)] = 0x0225,
9860 	[BEND_IDX(-25)] = 0x0225,
9861 	[BEND_IDX(-30)] = 0x0325,
9862 	[BEND_IDX(-35)] = 0x0325,
9863 	[BEND_IDX(-40)] = 0x0425,
9864 	[BEND_IDX(-45)] = 0x0425,
9865 	[BEND_IDX(-50)] = 0x0525,
9866 };
9867 
9868 /*
9869  * Bend CLKOUT_DP
9870  * steps -50 to 50 inclusive, in steps of 5
9871  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9872  * change in clock period = -(steps / 10) * 5.787 ps
9873  */
9874 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9875 {
9876 	u32 tmp;
9877 	int idx = BEND_IDX(steps);
9878 
9879 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
9880 		return;
9881 
9882 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
9883 		return;
9884 
9885 	mutex_lock(&dev_priv->sb_lock);
9886 
9887 	if (steps % 10 != 0)
9888 		tmp = 0xAAAAAAAB;
9889 	else
9890 		tmp = 0x00000000;
9891 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9892 
9893 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9894 	tmp &= 0xffff0000;
9895 	tmp |= sscdivintphase[idx];
9896 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9897 
9898 	mutex_unlock(&dev_priv->sb_lock);
9899 }
9900 
9901 #undef BEND_IDX
9902 
9903 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
9904 {
9905 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9906 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
9907 
9908 	if ((ctl & SPLL_PLL_ENABLE) == 0)
9909 		return false;
9910 
9911 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
9912 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9913 		return true;
9914 
9915 	if (IS_BROADWELL(dev_priv) &&
9916 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
9917 		return true;
9918 
9919 	return false;
9920 }
9921 
9922 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
9923 			       enum intel_dpll_id id)
9924 {
9925 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9926 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
9927 
9928 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
9929 		return false;
9930 
9931 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
9932 		return true;
9933 
9934 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
9935 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
9936 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9937 		return true;
9938 
9939 	return false;
9940 }
9941 
9942 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
9943 {
9944 	struct intel_encoder *encoder;
9945 	bool has_fdi = false;
9946 
9947 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9948 		switch (encoder->type) {
9949 		case INTEL_OUTPUT_ANALOG:
9950 			has_fdi = true;
9951 			break;
9952 		default:
9953 			break;
9954 		}
9955 	}
9956 
9957 	/*
9958 	 * The BIOS may have decided to use the PCH SSC
9959 	 * reference so we must not disable it until the
9960 	 * relevant PLLs have stopped relying on it. We'll
9961 	 * just leave the PCH SSC reference enabled in case
9962 	 * any active PLL is using it. It will get disabled
9963 	 * after runtime suspend if we don't have FDI.
9964 	 *
9965 	 * TODO: Move the whole reference clock handling
9966 	 * to the modeset sequence proper so that we can
9967 	 * actually enable/disable/reconfigure these things
9968 	 * safely. To do that we need to introduce a real
9969 	 * clock hierarchy. That would also allow us to do
9970 	 * clock bending finally.
9971 	 */
9972 	dev_priv->pch_ssc_use = 0;
9973 
9974 	if (spll_uses_pch_ssc(dev_priv)) {
9975 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
9976 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
9977 	}
9978 
9979 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
9980 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
9981 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
9982 	}
9983 
9984 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
9985 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
9986 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
9987 	}
9988 
9989 	if (dev_priv->pch_ssc_use)
9990 		return;
9991 
9992 	if (has_fdi) {
9993 		lpt_bend_clkout_dp(dev_priv, 0);
9994 		lpt_enable_clkout_dp(dev_priv, true, true);
9995 	} else {
9996 		lpt_disable_clkout_dp(dev_priv);
9997 	}
9998 }
9999 
10000 /*
10001  * Initialize reference clocks when the driver loads
10002  */
10003 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
10004 {
10005 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
10006 		ilk_init_pch_refclk(dev_priv);
10007 	else if (HAS_PCH_LPT(dev_priv))
10008 		lpt_init_pch_refclk(dev_priv);
10009 }
10010 
10011 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
10012 {
10013 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10014 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10015 	enum pipe pipe = crtc->pipe;
10016 	u32 val;
10017 
10018 	val = 0;
10019 
10020 	switch (crtc_state->pipe_bpp) {
10021 	case 18:
10022 		val |= PIPECONF_6BPC;
10023 		break;
10024 	case 24:
10025 		val |= PIPECONF_8BPC;
10026 		break;
10027 	case 30:
10028 		val |= PIPECONF_10BPC;
10029 		break;
10030 	case 36:
10031 		val |= PIPECONF_12BPC;
10032 		break;
10033 	default:
10034 		/* Case prevented by intel_choose_pipe_bpp_dither. */
10035 		BUG();
10036 	}
10037 
10038 	if (crtc_state->dither)
10039 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10040 
10041 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10042 		val |= PIPECONF_INTERLACED_ILK;
10043 	else
10044 		val |= PIPECONF_PROGRESSIVE;
10045 
10046 	/*
10047 	 * This would end up with an odd purple hue over
10048 	 * the entire display. Make sure we don't do it.
10049 	 */
10050 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10051 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
10052 
10053 	if (crtc_state->limited_color_range)
10054 		val |= PIPECONF_COLOR_RANGE_SELECT;
10055 
10056 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10057 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10058 
10059 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10060 
10061 	val |= PIPECONF_FRAME_START_DELAY(0);
10062 
10063 	intel_de_write(dev_priv, PIPECONF(pipe), val);
10064 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
10065 }
10066 
10067 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
10068 {
10069 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10070 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10071 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
10072 	u32 val = 0;
10073 
10074 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
10075 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10076 
10077 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10078 		val |= PIPECONF_INTERLACED_ILK;
10079 	else
10080 		val |= PIPECONF_PROGRESSIVE;
10081 
10082 	if (IS_HASWELL(dev_priv) &&
10083 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10084 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10085 
10086 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10087 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
10088 }
10089 
10090 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
10091 {
10092 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10093 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10094 	u32 val = 0;
10095 
10096 	switch (crtc_state->pipe_bpp) {
10097 	case 18:
10098 		val |= PIPEMISC_DITHER_6_BPC;
10099 		break;
10100 	case 24:
10101 		val |= PIPEMISC_DITHER_8_BPC;
10102 		break;
10103 	case 30:
10104 		val |= PIPEMISC_DITHER_10_BPC;
10105 		break;
10106 	case 36:
10107 		val |= PIPEMISC_DITHER_12_BPC;
10108 		break;
10109 	default:
10110 		MISSING_CASE(crtc_state->pipe_bpp);
10111 		break;
10112 	}
10113 
10114 	if (crtc_state->dither)
10115 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
10116 
10117 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10118 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10119 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
10120 
10121 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10122 		val |= PIPEMISC_YUV420_ENABLE |
10123 			PIPEMISC_YUV420_MODE_FULL_BLEND;
10124 
10125 	if (INTEL_GEN(dev_priv) >= 11 &&
10126 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10127 					   BIT(PLANE_CURSOR))) == 0)
10128 		val |= PIPEMISC_HDR_MODE_PRECISION;
10129 
10130 	if (INTEL_GEN(dev_priv) >= 12)
10131 		val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10132 
10133 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
10134 }
10135 
10136 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10137 {
10138 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10139 	u32 tmp;
10140 
10141 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
10142 
10143 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10144 	case PIPEMISC_DITHER_6_BPC:
10145 		return 18;
10146 	case PIPEMISC_DITHER_8_BPC:
10147 		return 24;
10148 	case PIPEMISC_DITHER_10_BPC:
10149 		return 30;
10150 	case PIPEMISC_DITHER_12_BPC:
10151 		return 36;
10152 	default:
10153 		MISSING_CASE(tmp);
10154 		return 0;
10155 	}
10156 }
10157 
10158 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
10159 {
10160 	/*
10161 	 * Account for spread spectrum to avoid
10162 	 * oversubscribing the link. Max center spread
10163 	 * is 2.5%; use 5% for safety's sake.
10164 	 */
10165 	u32 bps = target_clock * bpp * 21 / 20;
10166 	return DIV_ROUND_UP(bps, link_bw * 8);
10167 }
10168 
10169 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
10170 {
10171 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
10172 }
10173 
10174 static void ilk_compute_dpll(struct intel_crtc *crtc,
10175 			     struct intel_crtc_state *crtc_state,
10176 			     struct dpll *reduced_clock)
10177 {
10178 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10179 	u32 dpll, fp, fp2;
10180 	int factor;
10181 
10182 	/* Enable autotuning of the PLL clock (if permissible) */
10183 	factor = 21;
10184 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10185 		if ((intel_panel_use_ssc(dev_priv) &&
10186 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
10187 		    (HAS_PCH_IBX(dev_priv) &&
10188 		     intel_is_dual_link_lvds(dev_priv)))
10189 			factor = 25;
10190 	} else if (crtc_state->sdvo_tv_clock) {
10191 		factor = 20;
10192 	}
10193 
10194 	fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10195 
10196 	if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
10197 		fp |= FP_CB_TUNE;
10198 
10199 	if (reduced_clock) {
10200 		fp2 = i9xx_dpll_compute_fp(reduced_clock);
10201 
10202 		if (reduced_clock->m < factor * reduced_clock->n)
10203 			fp2 |= FP_CB_TUNE;
10204 	} else {
10205 		fp2 = fp;
10206 	}
10207 
10208 	dpll = 0;
10209 
10210 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
10211 		dpll |= DPLLB_MODE_LVDS;
10212 	else
10213 		dpll |= DPLLB_MODE_DAC_SERIAL;
10214 
10215 	dpll |= (crtc_state->pixel_multiplier - 1)
10216 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
10217 
10218 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10219 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
10220 		dpll |= DPLL_SDVO_HIGH_SPEED;
10221 
10222 	if (intel_crtc_has_dp_encoder(crtc_state))
10223 		dpll |= DPLL_SDVO_HIGH_SPEED;
10224 
10225 	/*
10226 	 * The high speed IO clock is only really required for
10227 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10228 	 * possible to share the DPLL between CRT and HDMI. Enabling
10229 	 * the clock needlessly does no real harm, except use up a
10230 	 * bit of power potentially.
10231 	 *
10232 	 * We'll limit this to IVB with 3 pipes, since it has only two
10233 	 * DPLLs and so DPLL sharing is the only way to get three pipes
10234 	 * driving PCH ports at the same time. On SNB we could do this,
10235 	 * and potentially avoid enabling the second DPLL, but it's not
10236 	 * clear if it''s a win or loss power wise. No point in doing
10237 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10238 	 */
10239 	if (INTEL_NUM_PIPES(dev_priv) == 3 &&
10240 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10241 		dpll |= DPLL_SDVO_HIGH_SPEED;
10242 
10243 	/* compute bitmask from p1 value */
10244 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
10245 	/* also FPA1 */
10246 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
10247 
10248 	switch (crtc_state->dpll.p2) {
10249 	case 5:
10250 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10251 		break;
10252 	case 7:
10253 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10254 		break;
10255 	case 10:
10256 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10257 		break;
10258 	case 14:
10259 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10260 		break;
10261 	}
10262 
10263 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10264 	    intel_panel_use_ssc(dev_priv))
10265 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
10266 	else
10267 		dpll |= PLL_REF_INPUT_DREFCLK;
10268 
10269 	dpll |= DPLL_VCO_ENABLE;
10270 
10271 	crtc_state->dpll_hw_state.dpll = dpll;
10272 	crtc_state->dpll_hw_state.fp0 = fp;
10273 	crtc_state->dpll_hw_state.fp1 = fp2;
10274 }
10275 
10276 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10277 				  struct intel_crtc_state *crtc_state)
10278 {
10279 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10280 	struct intel_atomic_state *state =
10281 		to_intel_atomic_state(crtc_state->uapi.state);
10282 	const struct intel_limit *limit;
10283 	int refclk = 120000;
10284 
10285 	memset(&crtc_state->dpll_hw_state, 0,
10286 	       sizeof(crtc_state->dpll_hw_state));
10287 
10288 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10289 	if (!crtc_state->has_pch_encoder)
10290 		return 0;
10291 
10292 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10293 		if (intel_panel_use_ssc(dev_priv)) {
10294 			drm_dbg_kms(&dev_priv->drm,
10295 				    "using SSC reference clock of %d kHz\n",
10296 				    dev_priv->vbt.lvds_ssc_freq);
10297 			refclk = dev_priv->vbt.lvds_ssc_freq;
10298 		}
10299 
10300 		if (intel_is_dual_link_lvds(dev_priv)) {
10301 			if (refclk == 100000)
10302 				limit = &ilk_limits_dual_lvds_100m;
10303 			else
10304 				limit = &ilk_limits_dual_lvds;
10305 		} else {
10306 			if (refclk == 100000)
10307 				limit = &ilk_limits_single_lvds_100m;
10308 			else
10309 				limit = &ilk_limits_single_lvds;
10310 		}
10311 	} else {
10312 		limit = &ilk_limits_dac;
10313 	}
10314 
10315 	if (!crtc_state->clock_set &&
10316 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10317 				refclk, NULL, &crtc_state->dpll)) {
10318 		drm_err(&dev_priv->drm,
10319 			"Couldn't find PLL settings for mode!\n");
10320 		return -EINVAL;
10321 	}
10322 
10323 	ilk_compute_dpll(crtc, crtc_state, NULL);
10324 
10325 	if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
10326 		drm_dbg_kms(&dev_priv->drm,
10327 			    "failed to find PLL for pipe %c\n",
10328 			    pipe_name(crtc->pipe));
10329 		return -EINVAL;
10330 	}
10331 
10332 	return 0;
10333 }
10334 
10335 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10336 					 struct intel_link_m_n *m_n)
10337 {
10338 	struct drm_device *dev = crtc->base.dev;
10339 	struct drm_i915_private *dev_priv = to_i915(dev);
10340 	enum pipe pipe = crtc->pipe;
10341 
10342 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10343 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10344 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10345 		& ~TU_SIZE_MASK;
10346 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10347 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10348 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10349 }
10350 
10351 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10352 					 enum transcoder transcoder,
10353 					 struct intel_link_m_n *m_n,
10354 					 struct intel_link_m_n *m2_n2)
10355 {
10356 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10357 	enum pipe pipe = crtc->pipe;
10358 
10359 	if (INTEL_GEN(dev_priv) >= 5) {
10360 		m_n->link_m = intel_de_read(dev_priv,
10361 					    PIPE_LINK_M1(transcoder));
10362 		m_n->link_n = intel_de_read(dev_priv,
10363 					    PIPE_LINK_N1(transcoder));
10364 		m_n->gmch_m = intel_de_read(dev_priv,
10365 					    PIPE_DATA_M1(transcoder))
10366 			& ~TU_SIZE_MASK;
10367 		m_n->gmch_n = intel_de_read(dev_priv,
10368 					    PIPE_DATA_N1(transcoder));
10369 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
10370 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10371 
10372 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
10373 			m2_n2->link_m = intel_de_read(dev_priv,
10374 						      PIPE_LINK_M2(transcoder));
10375 			m2_n2->link_n =	intel_de_read(dev_priv,
10376 							     PIPE_LINK_N2(transcoder));
10377 			m2_n2->gmch_m =	intel_de_read(dev_priv,
10378 							     PIPE_DATA_M2(transcoder))
10379 					& ~TU_SIZE_MASK;
10380 			m2_n2->gmch_n =	intel_de_read(dev_priv,
10381 							     PIPE_DATA_N2(transcoder));
10382 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
10383 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10384 		}
10385 	} else {
10386 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10387 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10388 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10389 			& ~TU_SIZE_MASK;
10390 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10391 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10392 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10393 	}
10394 }
10395 
10396 void intel_dp_get_m_n(struct intel_crtc *crtc,
10397 		      struct intel_crtc_state *pipe_config)
10398 {
10399 	if (pipe_config->has_pch_encoder)
10400 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10401 	else
10402 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10403 					     &pipe_config->dp_m_n,
10404 					     &pipe_config->dp_m2_n2);
10405 }
10406 
10407 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10408 				   struct intel_crtc_state *pipe_config)
10409 {
10410 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10411 				     &pipe_config->fdi_m_n, NULL);
10412 }
10413 
10414 static void skl_get_pfit_config(struct intel_crtc *crtc,
10415 				struct intel_crtc_state *pipe_config)
10416 {
10417 	struct drm_device *dev = crtc->base.dev;
10418 	struct drm_i915_private *dev_priv = to_i915(dev);
10419 	struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
10420 	u32 ps_ctrl = 0;
10421 	int id = -1;
10422 	int i;
10423 
10424 	/* find scaler attached to this pipe */
10425 	for (i = 0; i < crtc->num_scalers; i++) {
10426 		ps_ctrl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10427 		if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
10428 			id = i;
10429 			pipe_config->pch_pfit.enabled = true;
10430 			pipe_config->pch_pfit.pos = intel_de_read(dev_priv,
10431 								  SKL_PS_WIN_POS(crtc->pipe, i));
10432 			pipe_config->pch_pfit.size = intel_de_read(dev_priv,
10433 								   SKL_PS_WIN_SZ(crtc->pipe, i));
10434 			scaler_state->scalers[i].in_use = true;
10435 			break;
10436 		}
10437 	}
10438 
10439 	scaler_state->scaler_id = id;
10440 	if (id >= 0) {
10441 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
10442 	} else {
10443 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
10444 	}
10445 }
10446 
10447 static void
10448 skl_get_initial_plane_config(struct intel_crtc *crtc,
10449 			     struct intel_initial_plane_config *plane_config)
10450 {
10451 	struct drm_device *dev = crtc->base.dev;
10452 	struct drm_i915_private *dev_priv = to_i915(dev);
10453 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10454 	enum plane_id plane_id = plane->id;
10455 	enum pipe pipe;
10456 	u32 val, base, offset, stride_mult, tiling, alpha;
10457 	int fourcc, pixel_format;
10458 	unsigned int aligned_height;
10459 	struct drm_framebuffer *fb;
10460 	struct intel_framebuffer *intel_fb;
10461 
10462 	if (!plane->get_hw_state(plane, &pipe))
10463 		return;
10464 
10465 	drm_WARN_ON(dev, pipe != crtc->pipe);
10466 
10467 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10468 	if (!intel_fb) {
10469 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
10470 		return;
10471 	}
10472 
10473 	fb = &intel_fb->base;
10474 
10475 	fb->dev = dev;
10476 
10477 	val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
10478 
10479 	if (INTEL_GEN(dev_priv) >= 11)
10480 		pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10481 	else
10482 		pixel_format = val & PLANE_CTL_FORMAT_MASK;
10483 
10484 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
10485 		alpha = intel_de_read(dev_priv,
10486 				      PLANE_COLOR_CTL(pipe, plane_id));
10487 		alpha &= PLANE_COLOR_ALPHA_MASK;
10488 	} else {
10489 		alpha = val & PLANE_CTL_ALPHA_MASK;
10490 	}
10491 
10492 	fourcc = skl_format_to_fourcc(pixel_format,
10493 				      val & PLANE_CTL_ORDER_RGBX, alpha);
10494 	fb->format = drm_format_info(fourcc);
10495 
10496 	tiling = val & PLANE_CTL_TILED_MASK;
10497 	switch (tiling) {
10498 	case PLANE_CTL_TILED_LINEAR:
10499 		fb->modifier = DRM_FORMAT_MOD_LINEAR;
10500 		break;
10501 	case PLANE_CTL_TILED_X:
10502 		plane_config->tiling = I915_TILING_X;
10503 		fb->modifier = I915_FORMAT_MOD_X_TILED;
10504 		break;
10505 	case PLANE_CTL_TILED_Y:
10506 		plane_config->tiling = I915_TILING_Y;
10507 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10508 			fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10509 				I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10510 				I915_FORMAT_MOD_Y_TILED_CCS;
10511 		else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10512 			fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
10513 		else
10514 			fb->modifier = I915_FORMAT_MOD_Y_TILED;
10515 		break;
10516 	case PLANE_CTL_TILED_YF:
10517 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10518 			fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10519 		else
10520 			fb->modifier = I915_FORMAT_MOD_Yf_TILED;
10521 		break;
10522 	default:
10523 		MISSING_CASE(tiling);
10524 		goto error;
10525 	}
10526 
10527 	/*
10528 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10529 	 * while i915 HW rotation is clockwise, thats why this swapping.
10530 	 */
10531 	switch (val & PLANE_CTL_ROTATE_MASK) {
10532 	case PLANE_CTL_ROTATE_0:
10533 		plane_config->rotation = DRM_MODE_ROTATE_0;
10534 		break;
10535 	case PLANE_CTL_ROTATE_90:
10536 		plane_config->rotation = DRM_MODE_ROTATE_270;
10537 		break;
10538 	case PLANE_CTL_ROTATE_180:
10539 		plane_config->rotation = DRM_MODE_ROTATE_180;
10540 		break;
10541 	case PLANE_CTL_ROTATE_270:
10542 		plane_config->rotation = DRM_MODE_ROTATE_90;
10543 		break;
10544 	}
10545 
10546 	if (INTEL_GEN(dev_priv) >= 10 &&
10547 	    val & PLANE_CTL_FLIP_HORIZONTAL)
10548 		plane_config->rotation |= DRM_MODE_REFLECT_X;
10549 
10550 	base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
10551 	plane_config->base = base;
10552 
10553 	offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
10554 
10555 	val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
10556 	fb->height = ((val >> 16) & 0xffff) + 1;
10557 	fb->width = ((val >> 0) & 0xffff) + 1;
10558 
10559 	val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
10560 	stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
10561 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
10562 
10563 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
10564 
10565 	plane_config->size = fb->pitches[0] * aligned_height;
10566 
10567 	drm_dbg_kms(&dev_priv->drm,
10568 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10569 		    crtc->base.name, plane->base.name, fb->width, fb->height,
10570 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
10571 		    plane_config->size);
10572 
10573 	plane_config->fb = intel_fb;
10574 	return;
10575 
10576 error:
10577 	kfree(intel_fb);
10578 }
10579 
10580 static void ilk_get_pfit_config(struct intel_crtc *crtc,
10581 				struct intel_crtc_state *pipe_config)
10582 {
10583 	struct drm_device *dev = crtc->base.dev;
10584 	struct drm_i915_private *dev_priv = to_i915(dev);
10585 	u32 tmp;
10586 
10587 	tmp = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10588 
10589 	if (tmp & PF_ENABLE) {
10590 		pipe_config->pch_pfit.enabled = true;
10591 		pipe_config->pch_pfit.pos = intel_de_read(dev_priv,
10592 							  PF_WIN_POS(crtc->pipe));
10593 		pipe_config->pch_pfit.size = intel_de_read(dev_priv,
10594 							   PF_WIN_SZ(crtc->pipe));
10595 
10596 		/* We currently do not free assignements of panel fitters on
10597 		 * ivb/hsw (since we don't use the higher upscaling modes which
10598 		 * differentiates them) so just WARN about this case for now. */
10599 		if (IS_GEN(dev_priv, 7)) {
10600 			drm_WARN_ON(dev, (tmp & PF_PIPE_SEL_MASK_IVB) !=
10601 				    PF_PIPE_SEL_IVB(crtc->pipe));
10602 		}
10603 	}
10604 }
10605 
10606 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10607 				struct intel_crtc_state *pipe_config)
10608 {
10609 	struct drm_device *dev = crtc->base.dev;
10610 	struct drm_i915_private *dev_priv = to_i915(dev);
10611 	enum intel_display_power_domain power_domain;
10612 	intel_wakeref_t wakeref;
10613 	u32 tmp;
10614 	bool ret;
10615 
10616 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10617 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10618 	if (!wakeref)
10619 		return false;
10620 
10621 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10622 	pipe_config->shared_dpll = NULL;
10623 	pipe_config->master_transcoder = INVALID_TRANSCODER;
10624 
10625 	ret = false;
10626 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
10627 	if (!(tmp & PIPECONF_ENABLE))
10628 		goto out;
10629 
10630 	switch (tmp & PIPECONF_BPC_MASK) {
10631 	case PIPECONF_6BPC:
10632 		pipe_config->pipe_bpp = 18;
10633 		break;
10634 	case PIPECONF_8BPC:
10635 		pipe_config->pipe_bpp = 24;
10636 		break;
10637 	case PIPECONF_10BPC:
10638 		pipe_config->pipe_bpp = 30;
10639 		break;
10640 	case PIPECONF_12BPC:
10641 		pipe_config->pipe_bpp = 36;
10642 		break;
10643 	default:
10644 		break;
10645 	}
10646 
10647 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10648 		pipe_config->limited_color_range = true;
10649 
10650 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10651 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10652 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10653 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10654 		break;
10655 	default:
10656 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10657 		break;
10658 	}
10659 
10660 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10661 		PIPECONF_GAMMA_MODE_SHIFT;
10662 
10663 	pipe_config->csc_mode = intel_de_read(dev_priv,
10664 					      PIPE_CSC_MODE(crtc->pipe));
10665 
10666 	i9xx_get_pipe_color_config(pipe_config);
10667 	intel_color_get_config(pipe_config);
10668 
10669 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
10670 		struct intel_shared_dpll *pll;
10671 		enum intel_dpll_id pll_id;
10672 
10673 		pipe_config->has_pch_encoder = true;
10674 
10675 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
10676 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10677 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
10678 
10679 		ilk_get_fdi_m_n_config(crtc, pipe_config);
10680 
10681 		if (HAS_PCH_IBX(dev_priv)) {
10682 			/*
10683 			 * The pipe->pch transcoder and pch transcoder->pll
10684 			 * mapping is fixed.
10685 			 */
10686 			pll_id = (enum intel_dpll_id) crtc->pipe;
10687 		} else {
10688 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
10689 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
10690 				pll_id = DPLL_ID_PCH_PLL_B;
10691 			else
10692 				pll_id= DPLL_ID_PCH_PLL_A;
10693 		}
10694 
10695 		pipe_config->shared_dpll =
10696 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
10697 		pll = pipe_config->shared_dpll;
10698 
10699 		drm_WARN_ON(dev, !pll->info->funcs->get_hw_state(dev_priv, pll,
10700 						 &pipe_config->dpll_hw_state));
10701 
10702 		tmp = pipe_config->dpll_hw_state.dpll;
10703 		pipe_config->pixel_multiplier =
10704 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10705 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10706 
10707 		ilk_pch_clock_get(crtc, pipe_config);
10708 	} else {
10709 		pipe_config->pixel_multiplier = 1;
10710 	}
10711 
10712 	intel_get_pipe_timings(crtc, pipe_config);
10713 	intel_get_pipe_src_size(crtc, pipe_config);
10714 
10715 	ilk_get_pfit_config(crtc, pipe_config);
10716 
10717 	ret = true;
10718 
10719 out:
10720 	intel_display_power_put(dev_priv, power_domain, wakeref);
10721 
10722 	return ret;
10723 }
10724 
10725 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
10726 				  struct intel_crtc_state *crtc_state)
10727 {
10728 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10729 	struct intel_atomic_state *state =
10730 		to_intel_atomic_state(crtc_state->uapi.state);
10731 
10732 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10733 	    INTEL_GEN(dev_priv) >= 11) {
10734 		struct intel_encoder *encoder =
10735 			intel_get_crtc_new_encoder(state, crtc_state);
10736 
10737 		if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10738 			drm_dbg_kms(&dev_priv->drm,
10739 				    "failed to find PLL for pipe %c\n",
10740 				    pipe_name(crtc->pipe));
10741 			return -EINVAL;
10742 		}
10743 	}
10744 
10745 	return 0;
10746 }
10747 
10748 static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10749 			    struct intel_crtc_state *pipe_config)
10750 {
10751 	enum intel_dpll_id id;
10752 	u32 temp;
10753 
10754 	temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
10755 	id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
10756 
10757 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
10758 		return;
10759 
10760 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10761 }
10762 
10763 static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10764 			    struct intel_crtc_state *pipe_config)
10765 {
10766 	enum phy phy = intel_port_to_phy(dev_priv, port);
10767 	enum icl_port_dpll_id port_dpll_id;
10768 	enum intel_dpll_id id;
10769 	u32 temp;
10770 
10771 	if (intel_phy_is_combo(dev_priv, phy)) {
10772 		temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) &
10773 			ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10774 		id = temp >> ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10775 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10776 	} else if (intel_phy_is_tc(dev_priv, phy)) {
10777 		u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
10778 
10779 		if (clk_sel == DDI_CLK_SEL_MG) {
10780 			id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
10781 								    port));
10782 			port_dpll_id = ICL_PORT_DPLL_MG_PHY;
10783 		} else {
10784 			drm_WARN_ON(&dev_priv->drm,
10785 				    clk_sel < DDI_CLK_SEL_TBT_162);
10786 			id = DPLL_ID_ICL_TBTPLL;
10787 			port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10788 		}
10789 	} else {
10790 		drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
10791 		return;
10792 	}
10793 
10794 	pipe_config->icl_port_dplls[port_dpll_id].pll =
10795 		intel_get_shared_dpll_by_id(dev_priv, id);
10796 
10797 	icl_set_active_port_dpll(pipe_config, port_dpll_id);
10798 }
10799 
10800 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10801 				enum port port,
10802 				struct intel_crtc_state *pipe_config)
10803 {
10804 	enum intel_dpll_id id;
10805 
10806 	switch (port) {
10807 	case PORT_A:
10808 		id = DPLL_ID_SKL_DPLL0;
10809 		break;
10810 	case PORT_B:
10811 		id = DPLL_ID_SKL_DPLL1;
10812 		break;
10813 	case PORT_C:
10814 		id = DPLL_ID_SKL_DPLL2;
10815 		break;
10816 	default:
10817 		drm_err(&dev_priv->drm, "Incorrect port type\n");
10818 		return;
10819 	}
10820 
10821 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10822 }
10823 
10824 static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10825 			    struct intel_crtc_state *pipe_config)
10826 {
10827 	enum intel_dpll_id id;
10828 	u32 temp;
10829 
10830 	temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10831 	id = temp >> (port * 3 + 1);
10832 
10833 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
10834 		return;
10835 
10836 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10837 }
10838 
10839 static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10840 			    struct intel_crtc_state *pipe_config)
10841 {
10842 	enum intel_dpll_id id;
10843 	u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
10844 
10845 	switch (ddi_pll_sel) {
10846 	case PORT_CLK_SEL_WRPLL1:
10847 		id = DPLL_ID_WRPLL1;
10848 		break;
10849 	case PORT_CLK_SEL_WRPLL2:
10850 		id = DPLL_ID_WRPLL2;
10851 		break;
10852 	case PORT_CLK_SEL_SPLL:
10853 		id = DPLL_ID_SPLL;
10854 		break;
10855 	case PORT_CLK_SEL_LCPLL_810:
10856 		id = DPLL_ID_LCPLL_810;
10857 		break;
10858 	case PORT_CLK_SEL_LCPLL_1350:
10859 		id = DPLL_ID_LCPLL_1350;
10860 		break;
10861 	case PORT_CLK_SEL_LCPLL_2700:
10862 		id = DPLL_ID_LCPLL_2700;
10863 		break;
10864 	default:
10865 		MISSING_CASE(ddi_pll_sel);
10866 		/* fall through */
10867 	case PORT_CLK_SEL_NONE:
10868 		return;
10869 	}
10870 
10871 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10872 }
10873 
10874 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10875 				     struct intel_crtc_state *pipe_config,
10876 				     u64 *power_domain_mask,
10877 				     intel_wakeref_t *wakerefs)
10878 {
10879 	struct drm_device *dev = crtc->base.dev;
10880 	struct drm_i915_private *dev_priv = to_i915(dev);
10881 	enum intel_display_power_domain power_domain;
10882 	unsigned long panel_transcoder_mask = 0;
10883 	unsigned long enabled_panel_transcoders = 0;
10884 	enum transcoder panel_transcoder;
10885 	intel_wakeref_t wf;
10886 	u32 tmp;
10887 
10888 	if (INTEL_GEN(dev_priv) >= 11)
10889 		panel_transcoder_mask |=
10890 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
10891 
10892 	if (HAS_TRANSCODER_EDP(dev_priv))
10893 		panel_transcoder_mask |= BIT(TRANSCODER_EDP);
10894 
10895 	/*
10896 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
10897 	 * and DSI transcoders handled below.
10898 	 */
10899 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10900 
10901 	/*
10902 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10903 	 * consistency and less surprising code; it's in always on power).
10904 	 */
10905 	for_each_set_bit(panel_transcoder,
10906 			 &panel_transcoder_mask,
10907 			 ARRAY_SIZE(INTEL_INFO(dev_priv)->trans_offsets)) {
10908 		bool force_thru = false;
10909 		enum pipe trans_pipe;
10910 
10911 		tmp = intel_de_read(dev_priv,
10912 				    TRANS_DDI_FUNC_CTL(panel_transcoder));
10913 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
10914 			continue;
10915 
10916 		/*
10917 		 * Log all enabled ones, only use the first one.
10918 		 *
10919 		 * FIXME: This won't work for two separate DSI displays.
10920 		 */
10921 		enabled_panel_transcoders |= BIT(panel_transcoder);
10922 		if (enabled_panel_transcoders != BIT(panel_transcoder))
10923 			continue;
10924 
10925 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10926 		default:
10927 			drm_WARN(dev, 1,
10928 				 "unknown pipe linked to transcoder %s\n",
10929 				 transcoder_name(panel_transcoder));
10930 			/* fall through */
10931 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
10932 			force_thru = true;
10933 			/* fall through */
10934 		case TRANS_DDI_EDP_INPUT_A_ON:
10935 			trans_pipe = PIPE_A;
10936 			break;
10937 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
10938 			trans_pipe = PIPE_B;
10939 			break;
10940 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
10941 			trans_pipe = PIPE_C;
10942 			break;
10943 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
10944 			trans_pipe = PIPE_D;
10945 			break;
10946 		}
10947 
10948 		if (trans_pipe == crtc->pipe) {
10949 			pipe_config->cpu_transcoder = panel_transcoder;
10950 			pipe_config->pch_pfit.force_thru = force_thru;
10951 		}
10952 	}
10953 
10954 	/*
10955 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
10956 	 */
10957 	drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
10958 		    enabled_panel_transcoders != BIT(TRANSCODER_EDP));
10959 
10960 	power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10961 	drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
10962 
10963 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10964 	if (!wf)
10965 		return false;
10966 
10967 	wakerefs[power_domain] = wf;
10968 	*power_domain_mask |= BIT_ULL(power_domain);
10969 
10970 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
10971 
10972 	return tmp & PIPECONF_ENABLE;
10973 }
10974 
10975 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10976 					 struct intel_crtc_state *pipe_config,
10977 					 u64 *power_domain_mask,
10978 					 intel_wakeref_t *wakerefs)
10979 {
10980 	struct drm_device *dev = crtc->base.dev;
10981 	struct drm_i915_private *dev_priv = to_i915(dev);
10982 	enum intel_display_power_domain power_domain;
10983 	enum transcoder cpu_transcoder;
10984 	intel_wakeref_t wf;
10985 	enum port port;
10986 	u32 tmp;
10987 
10988 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10989 		if (port == PORT_A)
10990 			cpu_transcoder = TRANSCODER_DSI_A;
10991 		else
10992 			cpu_transcoder = TRANSCODER_DSI_C;
10993 
10994 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10995 		drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
10996 
10997 		wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10998 		if (!wf)
10999 			continue;
11000 
11001 		wakerefs[power_domain] = wf;
11002 		*power_domain_mask |= BIT_ULL(power_domain);
11003 
11004 		/*
11005 		 * The PLL needs to be enabled with a valid divider
11006 		 * configuration, otherwise accessing DSI registers will hang
11007 		 * the machine. See BSpec North Display Engine
11008 		 * registers/MIPI[BXT]. We can break out here early, since we
11009 		 * need the same DSI PLL to be enabled for both DSI ports.
11010 		 */
11011 		if (!bxt_dsi_pll_is_enabled(dev_priv))
11012 			break;
11013 
11014 		/* XXX: this works for video mode only */
11015 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
11016 		if (!(tmp & DPI_ENABLE))
11017 			continue;
11018 
11019 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
11020 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11021 			continue;
11022 
11023 		pipe_config->cpu_transcoder = cpu_transcoder;
11024 		break;
11025 	}
11026 
11027 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
11028 }
11029 
11030 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11031 				   struct intel_crtc_state *pipe_config)
11032 {
11033 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11034 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
11035 	struct intel_shared_dpll *pll;
11036 	enum port port;
11037 	u32 tmp;
11038 
11039 	if (transcoder_is_dsi(cpu_transcoder)) {
11040 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11041 						PORT_A : PORT_B;
11042 	} else {
11043 		tmp = intel_de_read(dev_priv,
11044 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
11045 		if (INTEL_GEN(dev_priv) >= 12)
11046 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11047 		else
11048 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11049 	}
11050 
11051 	if (INTEL_GEN(dev_priv) >= 11)
11052 		icl_get_ddi_pll(dev_priv, port, pipe_config);
11053 	else if (IS_CANNONLAKE(dev_priv))
11054 		cnl_get_ddi_pll(dev_priv, port, pipe_config);
11055 	else if (IS_GEN9_BC(dev_priv))
11056 		skl_get_ddi_pll(dev_priv, port, pipe_config);
11057 	else if (IS_GEN9_LP(dev_priv))
11058 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
11059 	else
11060 		hsw_get_ddi_pll(dev_priv, port, pipe_config);
11061 
11062 	pll = pipe_config->shared_dpll;
11063 	if (pll) {
11064 		drm_WARN_ON(&dev_priv->drm,
11065 			    !pll->info->funcs->get_hw_state(dev_priv, pll,
11066 						&pipe_config->dpll_hw_state));
11067 	}
11068 
11069 	/*
11070 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11071 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
11072 	 * the PCH transcoder is on.
11073 	 */
11074 	if (INTEL_GEN(dev_priv) < 9 &&
11075 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
11076 		pipe_config->has_pch_encoder = true;
11077 
11078 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
11079 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11080 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
11081 
11082 		ilk_get_fdi_m_n_config(crtc, pipe_config);
11083 	}
11084 }
11085 
11086 static enum transcoder transcoder_master_readout(struct drm_i915_private *dev_priv,
11087 						 enum transcoder cpu_transcoder)
11088 {
11089 	u32 trans_port_sync, master_select;
11090 
11091 	trans_port_sync = intel_de_read(dev_priv,
11092 				        TRANS_DDI_FUNC_CTL2(cpu_transcoder));
11093 
11094 	if ((trans_port_sync & PORT_SYNC_MODE_ENABLE) == 0)
11095 		return INVALID_TRANSCODER;
11096 
11097 	master_select = trans_port_sync &
11098 			PORT_SYNC_MODE_MASTER_SELECT_MASK;
11099 	if (master_select == 0)
11100 		return TRANSCODER_EDP;
11101 	else
11102 		return master_select - 1;
11103 }
11104 
11105 static void icl_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
11106 {
11107 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
11108 	u32 transcoders;
11109 	enum transcoder cpu_transcoder;
11110 
11111 	crtc_state->master_transcoder = transcoder_master_readout(dev_priv,
11112 								  crtc_state->cpu_transcoder);
11113 
11114 	transcoders = BIT(TRANSCODER_A) |
11115 		BIT(TRANSCODER_B) |
11116 		BIT(TRANSCODER_C) |
11117 		BIT(TRANSCODER_D);
11118 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
11119 		enum intel_display_power_domain power_domain;
11120 		intel_wakeref_t trans_wakeref;
11121 
11122 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
11123 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
11124 								   power_domain);
11125 
11126 		if (!trans_wakeref)
11127 			continue;
11128 
11129 		if (transcoder_master_readout(dev_priv, cpu_transcoder) ==
11130 		    crtc_state->cpu_transcoder)
11131 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
11132 
11133 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
11134 	}
11135 
11136 	drm_WARN_ON(&dev_priv->drm,
11137 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
11138 		    crtc_state->sync_mode_slaves_mask);
11139 }
11140 
11141 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11142 				struct intel_crtc_state *pipe_config)
11143 {
11144 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11145 	intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
11146 	enum intel_display_power_domain power_domain;
11147 	u64 power_domain_mask;
11148 	bool active;
11149 	u32 tmp;
11150 
11151 	pipe_config->master_transcoder = INVALID_TRANSCODER;
11152 
11153 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
11154 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11155 	if (!wf)
11156 		return false;
11157 
11158 	wakerefs[power_domain] = wf;
11159 	power_domain_mask = BIT_ULL(power_domain);
11160 
11161 	pipe_config->shared_dpll = NULL;
11162 
11163 	active = hsw_get_transcoder_state(crtc, pipe_config,
11164 					  &power_domain_mask, wakerefs);
11165 
11166 	if (IS_GEN9_LP(dev_priv) &&
11167 	    bxt_get_dsi_transcoder_state(crtc, pipe_config,
11168 					 &power_domain_mask, wakerefs)) {
11169 		drm_WARN_ON(&dev_priv->drm, active);
11170 		active = true;
11171 	}
11172 
11173 	if (!active)
11174 		goto out;
11175 
11176 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
11177 	    INTEL_GEN(dev_priv) >= 11) {
11178 		hsw_get_ddi_port_state(crtc, pipe_config);
11179 		intel_get_pipe_timings(crtc, pipe_config);
11180 	}
11181 
11182 	intel_get_pipe_src_size(crtc, pipe_config);
11183 
11184 	if (IS_HASWELL(dev_priv)) {
11185 		u32 tmp = intel_de_read(dev_priv,
11186 					PIPECONF(pipe_config->cpu_transcoder));
11187 
11188 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11189 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11190 		else
11191 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11192 	} else {
11193 		pipe_config->output_format =
11194 			bdw_get_pipemisc_output_format(crtc);
11195 
11196 		/*
11197 		 * Currently there is no interface defined to
11198 		 * check user preference between RGB/YCBCR444
11199 		 * or YCBCR420. So the only possible case for
11200 		 * YCBCR444 usage is driving YCBCR420 output
11201 		 * with LSPCON, when pipe is configured for
11202 		 * YCBCR444 output and LSPCON takes care of
11203 		 * downsampling it.
11204 		 */
11205 		pipe_config->lspcon_downsampling =
11206 			pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444;
11207 	}
11208 
11209 	pipe_config->gamma_mode = intel_de_read(dev_priv,
11210 						GAMMA_MODE(crtc->pipe));
11211 
11212 	pipe_config->csc_mode = intel_de_read(dev_priv,
11213 					      PIPE_CSC_MODE(crtc->pipe));
11214 
11215 	if (INTEL_GEN(dev_priv) >= 9) {
11216 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
11217 
11218 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11219 			pipe_config->gamma_enable = true;
11220 
11221 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11222 			pipe_config->csc_enable = true;
11223 	} else {
11224 		i9xx_get_pipe_color_config(pipe_config);
11225 	}
11226 
11227 	intel_color_get_config(pipe_config);
11228 
11229 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11230 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11231 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11232 		pipe_config->ips_linetime =
11233 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11234 
11235 	power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
11236 	drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
11237 
11238 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11239 	if (wf) {
11240 		wakerefs[power_domain] = wf;
11241 		power_domain_mask |= BIT_ULL(power_domain);
11242 
11243 		if (INTEL_GEN(dev_priv) >= 9)
11244 			skl_get_pfit_config(crtc, pipe_config);
11245 		else
11246 			ilk_get_pfit_config(crtc, pipe_config);
11247 	}
11248 
11249 	if (hsw_crtc_supports_ips(crtc)) {
11250 		if (IS_HASWELL(dev_priv))
11251 			pipe_config->ips_enabled = intel_de_read(dev_priv,
11252 								 IPS_CTL) & IPS_ENABLE;
11253 		else {
11254 			/*
11255 			 * We cannot readout IPS state on broadwell, set to
11256 			 * true so we can set it to a defined state on first
11257 			 * commit.
11258 			 */
11259 			pipe_config->ips_enabled = true;
11260 		}
11261 	}
11262 
11263 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
11264 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
11265 		pipe_config->pixel_multiplier =
11266 			intel_de_read(dev_priv,
11267 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
11268 	} else {
11269 		pipe_config->pixel_multiplier = 1;
11270 	}
11271 
11272 	if (INTEL_GEN(dev_priv) >= 11 &&
11273 	    !transcoder_is_dsi(pipe_config->cpu_transcoder))
11274 		icl_get_trans_port_sync_config(pipe_config);
11275 
11276 out:
11277 	for_each_power_domain(power_domain, power_domain_mask)
11278 		intel_display_power_put(dev_priv,
11279 					power_domain, wakerefs[power_domain]);
11280 
11281 	return active;
11282 }
11283 
11284 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
11285 {
11286 	struct drm_i915_private *dev_priv =
11287 		to_i915(plane_state->uapi.plane->dev);
11288 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11289 	const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11290 	u32 base;
11291 
11292 	if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
11293 		base = sg_dma_address(obj->mm.pages->sgl);
11294 	else
11295 		base = intel_plane_ggtt_offset(plane_state);
11296 
11297 	return base + plane_state->color_plane[0].offset;
11298 }
11299 
11300 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11301 {
11302 	int x = plane_state->uapi.dst.x1;
11303 	int y = plane_state->uapi.dst.y1;
11304 	u32 pos = 0;
11305 
11306 	if (x < 0) {
11307 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11308 		x = -x;
11309 	}
11310 	pos |= x << CURSOR_X_SHIFT;
11311 
11312 	if (y < 0) {
11313 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11314 		y = -y;
11315 	}
11316 	pos |= y << CURSOR_Y_SHIFT;
11317 
11318 	return pos;
11319 }
11320 
11321 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11322 {
11323 	const struct drm_mode_config *config =
11324 		&plane_state->uapi.plane->dev->mode_config;
11325 	int width = drm_rect_width(&plane_state->uapi.dst);
11326 	int height = drm_rect_height(&plane_state->uapi.dst);
11327 
11328 	return width > 0 && width <= config->cursor_width &&
11329 		height > 0 && height <= config->cursor_height;
11330 }
11331 
11332 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
11333 {
11334 	struct drm_i915_private *dev_priv =
11335 		to_i915(plane_state->uapi.plane->dev);
11336 	unsigned int rotation = plane_state->hw.rotation;
11337 	int src_x, src_y;
11338 	u32 offset;
11339 	int ret;
11340 
11341 	ret = intel_plane_compute_gtt(plane_state);
11342 	if (ret)
11343 		return ret;
11344 
11345 	if (!plane_state->uapi.visible)
11346 		return 0;
11347 
11348 	src_x = plane_state->uapi.src.x1 >> 16;
11349 	src_y = plane_state->uapi.src.y1 >> 16;
11350 
11351 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11352 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11353 						    plane_state, 0);
11354 
11355 	if (src_x != 0 || src_y != 0) {
11356 		drm_dbg_kms(&dev_priv->drm,
11357 			    "Arbitrary cursor panning not supported\n");
11358 		return -EINVAL;
11359 	}
11360 
11361 	/*
11362 	 * Put the final coordinates back so that the src
11363 	 * coordinate checks will see the right values.
11364 	 */
11365 	drm_rect_translate_to(&plane_state->uapi.src,
11366 			      src_x << 16, src_y << 16);
11367 
11368 	/* ILK+ do this automagically in hardware */
11369 	if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
11370 		const struct drm_framebuffer *fb = plane_state->hw.fb;
11371 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11372 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
11373 
11374 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
11375 	}
11376 
11377 	plane_state->color_plane[0].offset = offset;
11378 	plane_state->color_plane[0].x = src_x;
11379 	plane_state->color_plane[0].y = src_y;
11380 
11381 	return 0;
11382 }
11383 
11384 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11385 			      struct intel_plane_state *plane_state)
11386 {
11387 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11388 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11389 	int ret;
11390 
11391 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
11392 		drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
11393 		return -EINVAL;
11394 	}
11395 
11396 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
11397 						  &crtc_state->uapi,
11398 						  DRM_PLANE_HELPER_NO_SCALING,
11399 						  DRM_PLANE_HELPER_NO_SCALING,
11400 						  true, true);
11401 	if (ret)
11402 		return ret;
11403 
11404 	/* Use the unclipped src/dst rectangles, which we program to hw */
11405 	plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11406 	plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
11407 
11408 	ret = intel_cursor_check_surface(plane_state);
11409 	if (ret)
11410 		return ret;
11411 
11412 	if (!plane_state->uapi.visible)
11413 		return 0;
11414 
11415 	ret = intel_plane_check_src_coordinates(plane_state);
11416 	if (ret)
11417 		return ret;
11418 
11419 	return 0;
11420 }
11421 
11422 static unsigned int
11423 i845_cursor_max_stride(struct intel_plane *plane,
11424 		       u32 pixel_format, u64 modifier,
11425 		       unsigned int rotation)
11426 {
11427 	return 2048;
11428 }
11429 
11430 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11431 {
11432 	u32 cntl = 0;
11433 
11434 	if (crtc_state->gamma_enable)
11435 		cntl |= CURSOR_GAMMA_ENABLE;
11436 
11437 	return cntl;
11438 }
11439 
11440 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11441 			   const struct intel_plane_state *plane_state)
11442 {
11443 	return CURSOR_ENABLE |
11444 		CURSOR_FORMAT_ARGB |
11445 		CURSOR_STRIDE(plane_state->color_plane[0].stride);
11446 }
11447 
11448 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11449 {
11450 	int width = drm_rect_width(&plane_state->uapi.dst);
11451 
11452 	/*
11453 	 * 845g/865g are only limited by the width of their cursors,
11454 	 * the height is arbitrary up to the precision of the register.
11455 	 */
11456 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
11457 }
11458 
11459 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
11460 			     struct intel_plane_state *plane_state)
11461 {
11462 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11463 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11464 	int ret;
11465 
11466 	ret = intel_check_cursor(crtc_state, plane_state);
11467 	if (ret)
11468 		return ret;
11469 
11470 	/* if we want to turn off the cursor ignore width and height */
11471 	if (!fb)
11472 		return 0;
11473 
11474 	/* Check for which cursor types we support */
11475 	if (!i845_cursor_size_ok(plane_state)) {
11476 		drm_dbg_kms(&i915->drm,
11477 			    "Cursor dimension %dx%d not supported\n",
11478 			    drm_rect_width(&plane_state->uapi.dst),
11479 			    drm_rect_height(&plane_state->uapi.dst));
11480 		return -EINVAL;
11481 	}
11482 
11483 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11484 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11485 
11486 	switch (fb->pitches[0]) {
11487 	case 256:
11488 	case 512:
11489 	case 1024:
11490 	case 2048:
11491 		break;
11492 	default:
11493 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11494 			     fb->pitches[0]);
11495 		return -EINVAL;
11496 	}
11497 
11498 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11499 
11500 	return 0;
11501 }
11502 
11503 static void i845_update_cursor(struct intel_plane *plane,
11504 			       const struct intel_crtc_state *crtc_state,
11505 			       const struct intel_plane_state *plane_state)
11506 {
11507 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11508 	u32 cntl = 0, base = 0, pos = 0, size = 0;
11509 	unsigned long irqflags;
11510 
11511 	if (plane_state && plane_state->uapi.visible) {
11512 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11513 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
11514 
11515 		cntl = plane_state->ctl |
11516 			i845_cursor_ctl_crtc(crtc_state);
11517 
11518 		size = (height << 12) | width;
11519 
11520 		base = intel_cursor_base(plane_state);
11521 		pos = intel_cursor_position(plane_state);
11522 	}
11523 
11524 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11525 
11526 	/* On these chipsets we can only modify the base/size/stride
11527 	 * whilst the cursor is disabled.
11528 	 */
11529 	if (plane->cursor.base != base ||
11530 	    plane->cursor.size != size ||
11531 	    plane->cursor.cntl != cntl) {
11532 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11533 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11534 		intel_de_write_fw(dev_priv, CURSIZE, size);
11535 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11536 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
11537 
11538 		plane->cursor.base = base;
11539 		plane->cursor.size = size;
11540 		plane->cursor.cntl = cntl;
11541 	} else {
11542 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11543 	}
11544 
11545 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11546 }
11547 
11548 static void i845_disable_cursor(struct intel_plane *plane,
11549 				const struct intel_crtc_state *crtc_state)
11550 {
11551 	i845_update_cursor(plane, crtc_state, NULL);
11552 }
11553 
11554 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11555 				     enum pipe *pipe)
11556 {
11557 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11558 	enum intel_display_power_domain power_domain;
11559 	intel_wakeref_t wakeref;
11560 	bool ret;
11561 
11562 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
11563 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11564 	if (!wakeref)
11565 		return false;
11566 
11567 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
11568 
11569 	*pipe = PIPE_A;
11570 
11571 	intel_display_power_put(dev_priv, power_domain, wakeref);
11572 
11573 	return ret;
11574 }
11575 
11576 static unsigned int
11577 i9xx_cursor_max_stride(struct intel_plane *plane,
11578 		       u32 pixel_format, u64 modifier,
11579 		       unsigned int rotation)
11580 {
11581 	return plane->base.dev->mode_config.cursor_width * 4;
11582 }
11583 
11584 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11585 {
11586 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11587 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11588 	u32 cntl = 0;
11589 
11590 	if (INTEL_GEN(dev_priv) >= 11)
11591 		return cntl;
11592 
11593 	if (crtc_state->gamma_enable)
11594 		cntl = MCURSOR_GAMMA_ENABLE;
11595 
11596 	if (crtc_state->csc_enable)
11597 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
11598 
11599 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11600 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
11601 
11602 	return cntl;
11603 }
11604 
11605 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11606 			   const struct intel_plane_state *plane_state)
11607 {
11608 	struct drm_i915_private *dev_priv =
11609 		to_i915(plane_state->uapi.plane->dev);
11610 	u32 cntl = 0;
11611 
11612 	if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11613 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11614 
11615 	switch (drm_rect_width(&plane_state->uapi.dst)) {
11616 	case 64:
11617 		cntl |= MCURSOR_MODE_64_ARGB_AX;
11618 		break;
11619 	case 128:
11620 		cntl |= MCURSOR_MODE_128_ARGB_AX;
11621 		break;
11622 	case 256:
11623 		cntl |= MCURSOR_MODE_256_ARGB_AX;
11624 		break;
11625 	default:
11626 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
11627 		return 0;
11628 	}
11629 
11630 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
11631 		cntl |= MCURSOR_ROTATE_180;
11632 
11633 	return cntl;
11634 }
11635 
11636 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
11637 {
11638 	struct drm_i915_private *dev_priv =
11639 		to_i915(plane_state->uapi.plane->dev);
11640 	int width = drm_rect_width(&plane_state->uapi.dst);
11641 	int height = drm_rect_height(&plane_state->uapi.dst);
11642 
11643 	if (!intel_cursor_size_ok(plane_state))
11644 		return false;
11645 
11646 	/* Cursor width is limited to a few power-of-two sizes */
11647 	switch (width) {
11648 	case 256:
11649 	case 128:
11650 	case 64:
11651 		break;
11652 	default:
11653 		return false;
11654 	}
11655 
11656 	/*
11657 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11658 	 * height from 8 lines up to the cursor width, when the
11659 	 * cursor is not rotated. Everything else requires square
11660 	 * cursors.
11661 	 */
11662 	if (HAS_CUR_FBC(dev_priv) &&
11663 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
11664 		if (height < 8 || height > width)
11665 			return false;
11666 	} else {
11667 		if (height != width)
11668 			return false;
11669 	}
11670 
11671 	return true;
11672 }
11673 
11674 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
11675 			     struct intel_plane_state *plane_state)
11676 {
11677 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11678 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11679 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11680 	enum pipe pipe = plane->pipe;
11681 	int ret;
11682 
11683 	ret = intel_check_cursor(crtc_state, plane_state);
11684 	if (ret)
11685 		return ret;
11686 
11687 	/* if we want to turn off the cursor ignore width and height */
11688 	if (!fb)
11689 		return 0;
11690 
11691 	/* Check for which cursor types we support */
11692 	if (!i9xx_cursor_size_ok(plane_state)) {
11693 		drm_dbg(&dev_priv->drm,
11694 			"Cursor dimension %dx%d not supported\n",
11695 			drm_rect_width(&plane_state->uapi.dst),
11696 			drm_rect_height(&plane_state->uapi.dst));
11697 		return -EINVAL;
11698 	}
11699 
11700 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
11701 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11702 
11703 	if (fb->pitches[0] !=
11704 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
11705 		drm_dbg_kms(&dev_priv->drm,
11706 			    "Invalid cursor stride (%u) (cursor width %d)\n",
11707 			    fb->pitches[0],
11708 			    drm_rect_width(&plane_state->uapi.dst));
11709 		return -EINVAL;
11710 	}
11711 
11712 	/*
11713 	 * There's something wrong with the cursor on CHV pipe C.
11714 	 * If it straddles the left edge of the screen then
11715 	 * moving it away from the edge or disabling it often
11716 	 * results in a pipe underrun, and often that can lead to
11717 	 * dead pipe (constant underrun reported, and it scans
11718 	 * out just a solid color). To recover from that, the
11719 	 * display power well must be turned off and on again.
11720 	 * Refuse the put the cursor into that compromised position.
11721 	 */
11722 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
11723 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
11724 		drm_dbg_kms(&dev_priv->drm,
11725 			    "CHV cursor C not allowed to straddle the left screen edge\n");
11726 		return -EINVAL;
11727 	}
11728 
11729 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
11730 
11731 	return 0;
11732 }
11733 
11734 static void i9xx_update_cursor(struct intel_plane *plane,
11735 			       const struct intel_crtc_state *crtc_state,
11736 			       const struct intel_plane_state *plane_state)
11737 {
11738 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11739 	enum pipe pipe = plane->pipe;
11740 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
11741 	unsigned long irqflags;
11742 
11743 	if (plane_state && plane_state->uapi.visible) {
11744 		unsigned width = drm_rect_width(&plane_state->uapi.dst);
11745 		unsigned height = drm_rect_height(&plane_state->uapi.dst);
11746 
11747 		cntl = plane_state->ctl |
11748 			i9xx_cursor_ctl_crtc(crtc_state);
11749 
11750 		if (width != height)
11751 			fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
11752 
11753 		base = intel_cursor_base(plane_state);
11754 		pos = intel_cursor_position(plane_state);
11755 	}
11756 
11757 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11758 
11759 	/*
11760 	 * On some platforms writing CURCNTR first will also
11761 	 * cause CURPOS to be armed by the CURBASE write.
11762 	 * Without the CURCNTR write the CURPOS write would
11763 	 * arm itself. Thus we always update CURCNTR before
11764 	 * CURPOS.
11765 	 *
11766 	 * On other platforms CURPOS always requires the
11767 	 * CURBASE write to arm the update. Additonally
11768 	 * a write to any of the cursor register will cancel
11769 	 * an already armed cursor update. Thus leaving out
11770 	 * the CURBASE write after CURPOS could lead to a
11771 	 * cursor that doesn't appear to move, or even change
11772 	 * shape. Thus we always write CURBASE.
11773 	 *
11774 	 * The other registers are armed by by the CURBASE write
11775 	 * except when the plane is getting enabled at which time
11776 	 * the CURCNTR write arms the update.
11777 	 */
11778 
11779 	if (INTEL_GEN(dev_priv) >= 9)
11780 		skl_write_cursor_wm(plane, crtc_state);
11781 
11782 	if (plane->cursor.base != base ||
11783 	    plane->cursor.size != fbc_ctl ||
11784 	    plane->cursor.cntl != cntl) {
11785 		if (HAS_CUR_FBC(dev_priv))
11786 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
11787 					  fbc_ctl);
11788 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
11789 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11790 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11791 
11792 		plane->cursor.base = base;
11793 		plane->cursor.size = fbc_ctl;
11794 		plane->cursor.cntl = cntl;
11795 	} else {
11796 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11797 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11798 	}
11799 
11800 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11801 }
11802 
11803 static void i9xx_disable_cursor(struct intel_plane *plane,
11804 				const struct intel_crtc_state *crtc_state)
11805 {
11806 	i9xx_update_cursor(plane, crtc_state, NULL);
11807 }
11808 
11809 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
11810 				     enum pipe *pipe)
11811 {
11812 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11813 	enum intel_display_power_domain power_domain;
11814 	intel_wakeref_t wakeref;
11815 	bool ret;
11816 	u32 val;
11817 
11818 	/*
11819 	 * Not 100% correct for planes that can move between pipes,
11820 	 * but that's only the case for gen2-3 which don't have any
11821 	 * display power wells.
11822 	 */
11823 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
11824 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11825 	if (!wakeref)
11826 		return false;
11827 
11828 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
11829 
11830 	ret = val & MCURSOR_MODE;
11831 
11832 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
11833 		*pipe = plane->pipe;
11834 	else
11835 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
11836 			MCURSOR_PIPE_SELECT_SHIFT;
11837 
11838 	intel_display_power_put(dev_priv, power_domain, wakeref);
11839 
11840 	return ret;
11841 }
11842 
11843 /* VESA 640x480x72Hz mode to set on the pipe */
11844 static const struct drm_display_mode load_detect_mode = {
11845 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11846 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11847 };
11848 
11849 struct drm_framebuffer *
11850 intel_framebuffer_create(struct drm_i915_gem_object *obj,
11851 			 struct drm_mode_fb_cmd2 *mode_cmd)
11852 {
11853 	struct intel_framebuffer *intel_fb;
11854 	int ret;
11855 
11856 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
11857 	if (!intel_fb)
11858 		return ERR_PTR(-ENOMEM);
11859 
11860 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
11861 	if (ret)
11862 		goto err;
11863 
11864 	return &intel_fb->base;
11865 
11866 err:
11867 	kfree(intel_fb);
11868 	return ERR_PTR(ret);
11869 }
11870 
11871 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
11872 					struct drm_crtc *crtc)
11873 {
11874 	struct drm_plane *plane;
11875 	struct drm_plane_state *plane_state;
11876 	int ret, i;
11877 
11878 	ret = drm_atomic_add_affected_planes(state, crtc);
11879 	if (ret)
11880 		return ret;
11881 
11882 	for_each_new_plane_in_state(state, plane, plane_state, i) {
11883 		if (plane_state->crtc != crtc)
11884 			continue;
11885 
11886 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
11887 		if (ret)
11888 			return ret;
11889 
11890 		drm_atomic_set_fb_for_plane(plane_state, NULL);
11891 	}
11892 
11893 	return 0;
11894 }
11895 
11896 int intel_get_load_detect_pipe(struct drm_connector *connector,
11897 			       struct intel_load_detect_pipe *old,
11898 			       struct drm_modeset_acquire_ctx *ctx)
11899 {
11900 	struct intel_crtc *intel_crtc;
11901 	struct intel_encoder *intel_encoder =
11902 		intel_attached_encoder(to_intel_connector(connector));
11903 	struct drm_crtc *possible_crtc;
11904 	struct drm_encoder *encoder = &intel_encoder->base;
11905 	struct drm_crtc *crtc = NULL;
11906 	struct drm_device *dev = encoder->dev;
11907 	struct drm_i915_private *dev_priv = to_i915(dev);
11908 	struct drm_mode_config *config = &dev->mode_config;
11909 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
11910 	struct drm_connector_state *connector_state;
11911 	struct intel_crtc_state *crtc_state;
11912 	int ret, i = -1;
11913 
11914 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
11915 		    connector->base.id, connector->name,
11916 		    encoder->base.id, encoder->name);
11917 
11918 	old->restore_state = NULL;
11919 
11920 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
11921 
11922 	/*
11923 	 * Algorithm gets a little messy:
11924 	 *
11925 	 *   - if the connector already has an assigned crtc, use it (but make
11926 	 *     sure it's on first)
11927 	 *
11928 	 *   - try to find the first unused crtc that can drive this connector,
11929 	 *     and use that if we find one
11930 	 */
11931 
11932 	/* See if we already have a CRTC for this connector */
11933 	if (connector->state->crtc) {
11934 		crtc = connector->state->crtc;
11935 
11936 		ret = drm_modeset_lock(&crtc->mutex, ctx);
11937 		if (ret)
11938 			goto fail;
11939 
11940 		/* Make sure the crtc and connector are running */
11941 		goto found;
11942 	}
11943 
11944 	/* Find an unused one (if possible) */
11945 	for_each_crtc(dev, possible_crtc) {
11946 		i++;
11947 		if (!(encoder->possible_crtcs & (1 << i)))
11948 			continue;
11949 
11950 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11951 		if (ret)
11952 			goto fail;
11953 
11954 		if (possible_crtc->state->enable) {
11955 			drm_modeset_unlock(&possible_crtc->mutex);
11956 			continue;
11957 		}
11958 
11959 		crtc = possible_crtc;
11960 		break;
11961 	}
11962 
11963 	/*
11964 	 * If we didn't find an unused CRTC, don't use any.
11965 	 */
11966 	if (!crtc) {
11967 		drm_dbg_kms(&dev_priv->drm,
11968 			    "no pipe available for load-detect\n");
11969 		ret = -ENODEV;
11970 		goto fail;
11971 	}
11972 
11973 found:
11974 	intel_crtc = to_intel_crtc(crtc);
11975 
11976 	state = drm_atomic_state_alloc(dev);
11977 	restore_state = drm_atomic_state_alloc(dev);
11978 	if (!state || !restore_state) {
11979 		ret = -ENOMEM;
11980 		goto fail;
11981 	}
11982 
11983 	state->acquire_ctx = ctx;
11984 	restore_state->acquire_ctx = ctx;
11985 
11986 	connector_state = drm_atomic_get_connector_state(state, connector);
11987 	if (IS_ERR(connector_state)) {
11988 		ret = PTR_ERR(connector_state);
11989 		goto fail;
11990 	}
11991 
11992 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11993 	if (ret)
11994 		goto fail;
11995 
11996 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11997 	if (IS_ERR(crtc_state)) {
11998 		ret = PTR_ERR(crtc_state);
11999 		goto fail;
12000 	}
12001 
12002 	crtc_state->uapi.active = true;
12003 
12004 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
12005 					   &load_detect_mode);
12006 	if (ret)
12007 		goto fail;
12008 
12009 	ret = intel_modeset_disable_planes(state, crtc);
12010 	if (ret)
12011 		goto fail;
12012 
12013 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12014 	if (!ret)
12015 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
12016 	if (!ret)
12017 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
12018 	if (ret) {
12019 		drm_dbg_kms(&dev_priv->drm,
12020 			    "Failed to create a copy of old state to restore: %i\n",
12021 			    ret);
12022 		goto fail;
12023 	}
12024 
12025 	ret = drm_atomic_commit(state);
12026 	if (ret) {
12027 		drm_dbg_kms(&dev_priv->drm,
12028 			    "failed to set mode on load-detect pipe\n");
12029 		goto fail;
12030 	}
12031 
12032 	old->restore_state = restore_state;
12033 	drm_atomic_state_put(state);
12034 
12035 	/* let the connector get through one full cycle before testing */
12036 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
12037 	return true;
12038 
12039 fail:
12040 	if (state) {
12041 		drm_atomic_state_put(state);
12042 		state = NULL;
12043 	}
12044 	if (restore_state) {
12045 		drm_atomic_state_put(restore_state);
12046 		restore_state = NULL;
12047 	}
12048 
12049 	if (ret == -EDEADLK)
12050 		return ret;
12051 
12052 	return false;
12053 }
12054 
12055 void intel_release_load_detect_pipe(struct drm_connector *connector,
12056 				    struct intel_load_detect_pipe *old,
12057 				    struct drm_modeset_acquire_ctx *ctx)
12058 {
12059 	struct intel_encoder *intel_encoder =
12060 		intel_attached_encoder(to_intel_connector(connector));
12061 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
12062 	struct drm_encoder *encoder = &intel_encoder->base;
12063 	struct drm_atomic_state *state = old->restore_state;
12064 	int ret;
12065 
12066 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12067 		    connector->base.id, connector->name,
12068 		    encoder->base.id, encoder->name);
12069 
12070 	if (!state)
12071 		return;
12072 
12073 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
12074 	if (ret)
12075 		drm_dbg_kms(&i915->drm,
12076 			    "Couldn't release load detect pipe: %i\n", ret);
12077 	drm_atomic_state_put(state);
12078 }
12079 
12080 static int i9xx_pll_refclk(struct drm_device *dev,
12081 			   const struct intel_crtc_state *pipe_config)
12082 {
12083 	struct drm_i915_private *dev_priv = to_i915(dev);
12084 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12085 
12086 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
12087 		return dev_priv->vbt.lvds_ssc_freq;
12088 	else if (HAS_PCH_SPLIT(dev_priv))
12089 		return 120000;
12090 	else if (!IS_GEN(dev_priv, 2))
12091 		return 96000;
12092 	else
12093 		return 48000;
12094 }
12095 
12096 /* Returns the clock of the currently programmed mode of the given pipe. */
12097 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
12098 				struct intel_crtc_state *pipe_config)
12099 {
12100 	struct drm_device *dev = crtc->base.dev;
12101 	struct drm_i915_private *dev_priv = to_i915(dev);
12102 	enum pipe pipe = crtc->pipe;
12103 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12104 	u32 fp;
12105 	struct dpll clock;
12106 	int port_clock;
12107 	int refclk = i9xx_pll_refclk(dev, pipe_config);
12108 
12109 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
12110 		fp = pipe_config->dpll_hw_state.fp0;
12111 	else
12112 		fp = pipe_config->dpll_hw_state.fp1;
12113 
12114 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
12115 	if (IS_PINEVIEW(dev_priv)) {
12116 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12117 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
12118 	} else {
12119 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12120 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12121 	}
12122 
12123 	if (!IS_GEN(dev_priv, 2)) {
12124 		if (IS_PINEVIEW(dev_priv))
12125 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12126 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
12127 		else
12128 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
12129 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
12130 
12131 		switch (dpll & DPLL_MODE_MASK) {
12132 		case DPLLB_MODE_DAC_SERIAL:
12133 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12134 				5 : 10;
12135 			break;
12136 		case DPLLB_MODE_LVDS:
12137 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12138 				7 : 14;
12139 			break;
12140 		default:
12141 			drm_dbg_kms(&dev_priv->drm,
12142 				    "Unknown DPLL mode %08x in programmed "
12143 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
12144 			return;
12145 		}
12146 
12147 		if (IS_PINEVIEW(dev_priv))
12148 			port_clock = pnv_calc_dpll_params(refclk, &clock);
12149 		else
12150 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
12151 	} else {
12152 		u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12153 								 LVDS);
12154 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
12155 
12156 		if (is_lvds) {
12157 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12158 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
12159 
12160 			if (lvds & LVDS_CLKB_POWER_UP)
12161 				clock.p2 = 7;
12162 			else
12163 				clock.p2 = 14;
12164 		} else {
12165 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
12166 				clock.p1 = 2;
12167 			else {
12168 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12169 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12170 			}
12171 			if (dpll & PLL_P2_DIVIDE_BY_4)
12172 				clock.p2 = 4;
12173 			else
12174 				clock.p2 = 2;
12175 		}
12176 
12177 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
12178 	}
12179 
12180 	/*
12181 	 * This value includes pixel_multiplier. We will use
12182 	 * port_clock to compute adjusted_mode.crtc_clock in the
12183 	 * encoder's get_config() function.
12184 	 */
12185 	pipe_config->port_clock = port_clock;
12186 }
12187 
12188 int intel_dotclock_calculate(int link_freq,
12189 			     const struct intel_link_m_n *m_n)
12190 {
12191 	/*
12192 	 * The calculation for the data clock is:
12193 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
12194 	 * But we want to avoid losing precison if possible, so:
12195 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
12196 	 *
12197 	 * and the link clock is simpler:
12198 	 * link_clock = (m * link_clock) / n
12199 	 */
12200 
12201 	if (!m_n->link_n)
12202 		return 0;
12203 
12204 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
12205 }
12206 
12207 static void ilk_pch_clock_get(struct intel_crtc *crtc,
12208 			      struct intel_crtc_state *pipe_config)
12209 {
12210 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12211 
12212 	/* read out port_clock from the DPLL */
12213 	i9xx_crtc_clock_get(crtc, pipe_config);
12214 
12215 	/*
12216 	 * In case there is an active pipe without active ports,
12217 	 * we may need some idea for the dotclock anyway.
12218 	 * Calculate one based on the FDI configuration.
12219 	 */
12220 	pipe_config->hw.adjusted_mode.crtc_clock =
12221 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12222 					 &pipe_config->fdi_m_n);
12223 }
12224 
12225 static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12226 				   struct intel_crtc *crtc)
12227 {
12228 	memset(crtc_state, 0, sizeof(*crtc_state));
12229 
12230 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12231 
12232 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12233 	crtc_state->master_transcoder = INVALID_TRANSCODER;
12234 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12235 	crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12236 	crtc_state->scaler_state.scaler_id = -1;
12237 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
12238 }
12239 
12240 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12241 {
12242 	struct intel_crtc_state *crtc_state;
12243 
12244 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12245 
12246 	if (crtc_state)
12247 		intel_crtc_state_reset(crtc_state, crtc);
12248 
12249 	return crtc_state;
12250 }
12251 
12252 /* Returns the currently programmed mode of the given encoder. */
12253 struct drm_display_mode *
12254 intel_encoder_current_mode(struct intel_encoder *encoder)
12255 {
12256 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12257 	struct intel_crtc_state *crtc_state;
12258 	struct drm_display_mode *mode;
12259 	struct intel_crtc *crtc;
12260 	enum pipe pipe;
12261 
12262 	if (!encoder->get_hw_state(encoder, &pipe))
12263 		return NULL;
12264 
12265 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12266 
12267 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12268 	if (!mode)
12269 		return NULL;
12270 
12271 	crtc_state = intel_crtc_state_alloc(crtc);
12272 	if (!crtc_state) {
12273 		kfree(mode);
12274 		return NULL;
12275 	}
12276 
12277 	if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
12278 		kfree(crtc_state);
12279 		kfree(mode);
12280 		return NULL;
12281 	}
12282 
12283 	encoder->get_config(encoder, crtc_state);
12284 
12285 	intel_mode_from_pipe_config(mode, crtc_state);
12286 
12287 	kfree(crtc_state);
12288 
12289 	return mode;
12290 }
12291 
12292 static void intel_crtc_destroy(struct drm_crtc *crtc)
12293 {
12294 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12295 
12296 	drm_crtc_cleanup(crtc);
12297 	kfree(intel_crtc);
12298 }
12299 
12300 /**
12301  * intel_wm_need_update - Check whether watermarks need updating
12302  * @cur: current plane state
12303  * @new: new plane state
12304  *
12305  * Check current plane state versus the new one to determine whether
12306  * watermarks need to be recalculated.
12307  *
12308  * Returns true or false.
12309  */
12310 static bool intel_wm_need_update(const struct intel_plane_state *cur,
12311 				 struct intel_plane_state *new)
12312 {
12313 	/* Update watermarks on tiling or size changes. */
12314 	if (new->uapi.visible != cur->uapi.visible)
12315 		return true;
12316 
12317 	if (!cur->hw.fb || !new->hw.fb)
12318 		return false;
12319 
12320 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12321 	    cur->hw.rotation != new->hw.rotation ||
12322 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12323 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12324 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12325 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
12326 		return true;
12327 
12328 	return false;
12329 }
12330 
12331 static bool needs_scaling(const struct intel_plane_state *state)
12332 {
12333 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
12334 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
12335 	int dst_w = drm_rect_width(&state->uapi.dst);
12336 	int dst_h = drm_rect_height(&state->uapi.dst);
12337 
12338 	return (src_w != dst_w || src_h != dst_h);
12339 }
12340 
12341 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
12342 				    struct intel_crtc_state *crtc_state,
12343 				    const struct intel_plane_state *old_plane_state,
12344 				    struct intel_plane_state *plane_state)
12345 {
12346 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12347 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12348 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12349 	bool mode_changed = needs_modeset(crtc_state);
12350 	bool was_crtc_enabled = old_crtc_state->hw.active;
12351 	bool is_crtc_enabled = crtc_state->hw.active;
12352 	bool turn_off, turn_on, visible, was_visible;
12353 	int ret;
12354 
12355 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
12356 		ret = skl_update_scaler_plane(crtc_state, plane_state);
12357 		if (ret)
12358 			return ret;
12359 	}
12360 
12361 	was_visible = old_plane_state->uapi.visible;
12362 	visible = plane_state->uapi.visible;
12363 
12364 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
12365 		was_visible = false;
12366 
12367 	/*
12368 	 * Visibility is calculated as if the crtc was on, but
12369 	 * after scaler setup everything depends on it being off
12370 	 * when the crtc isn't active.
12371 	 *
12372 	 * FIXME this is wrong for watermarks. Watermarks should also
12373 	 * be computed as if the pipe would be active. Perhaps move
12374 	 * per-plane wm computation to the .check_plane() hook, and
12375 	 * only combine the results from all planes in the current place?
12376 	 */
12377 	if (!is_crtc_enabled) {
12378 		plane_state->uapi.visible = visible = false;
12379 		crtc_state->active_planes &= ~BIT(plane->id);
12380 		crtc_state->data_rate[plane->id] = 0;
12381 		crtc_state->min_cdclk[plane->id] = 0;
12382 	}
12383 
12384 	if (!was_visible && !visible)
12385 		return 0;
12386 
12387 	turn_off = was_visible && (!visible || mode_changed);
12388 	turn_on = visible && (!was_visible || mode_changed);
12389 
12390 	drm_dbg_atomic(&dev_priv->drm,
12391 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12392 		       crtc->base.base.id, crtc->base.name,
12393 		       plane->base.base.id, plane->base.name,
12394 		       was_visible, visible,
12395 		       turn_off, turn_on, mode_changed);
12396 
12397 	if (turn_on) {
12398 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12399 			crtc_state->update_wm_pre = true;
12400 
12401 		/* must disable cxsr around plane enable/disable */
12402 		if (plane->id != PLANE_CURSOR)
12403 			crtc_state->disable_cxsr = true;
12404 	} else if (turn_off) {
12405 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12406 			crtc_state->update_wm_post = true;
12407 
12408 		/* must disable cxsr around plane enable/disable */
12409 		if (plane->id != PLANE_CURSOR)
12410 			crtc_state->disable_cxsr = true;
12411 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
12412 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
12413 			/* FIXME bollocks */
12414 			crtc_state->update_wm_pre = true;
12415 			crtc_state->update_wm_post = true;
12416 		}
12417 	}
12418 
12419 	if (visible || was_visible)
12420 		crtc_state->fb_bits |= plane->frontbuffer_bit;
12421 
12422 	/*
12423 	 * ILK/SNB DVSACNTR/Sprite Enable
12424 	 * IVB SPR_CTL/Sprite Enable
12425 	 * "When in Self Refresh Big FIFO mode, a write to enable the
12426 	 *  plane will be internally buffered and delayed while Big FIFO
12427 	 *  mode is exiting."
12428 	 *
12429 	 * Which means that enabling the sprite can take an extra frame
12430 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
12431 	 * down to LP0 and wait for vblank in order to make sure the
12432 	 * sprite gets enabled on the next vblank after the register write.
12433 	 * Doing otherwise would risk enabling the sprite one frame after
12434 	 * we've already signalled flip completion. We can resume LP1+
12435 	 * once the sprite has been enabled.
12436 	 *
12437 	 *
12438 	 * WaCxSRDisabledForSpriteScaling:ivb
12439 	 * IVB SPR_SCALE/Scaling Enable
12440 	 * "Low Power watermarks must be disabled for at least one
12441 	 *  frame before enabling sprite scaling, and kept disabled
12442 	 *  until sprite scaling is disabled."
12443 	 *
12444 	 * ILK/SNB DVSASCALE/Scaling Enable
12445 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
12446 	 *  masked off while Big FIFO mode is exiting."
12447 	 *
12448 	 * Despite the w/a only being listed for IVB we assume that
12449 	 * the ILK/SNB note has similar ramifications, hence we apply
12450 	 * the w/a on all three platforms.
12451 	 *
12452 	 * With experimental results seems this is needed also for primary
12453 	 * plane, not only sprite plane.
12454 	 */
12455 	if (plane->id != PLANE_CURSOR &&
12456 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
12457 	     IS_IVYBRIDGE(dev_priv)) &&
12458 	    (turn_on || (!needs_scaling(old_plane_state) &&
12459 			 needs_scaling(plane_state))))
12460 		crtc_state->disable_lp_wm = true;
12461 
12462 	return 0;
12463 }
12464 
12465 static bool encoders_cloneable(const struct intel_encoder *a,
12466 			       const struct intel_encoder *b)
12467 {
12468 	/* masks could be asymmetric, so check both ways */
12469 	return a == b || (a->cloneable & (1 << b->type) &&
12470 			  b->cloneable & (1 << a->type));
12471 }
12472 
12473 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12474 					 struct intel_crtc *crtc,
12475 					 struct intel_encoder *encoder)
12476 {
12477 	struct intel_encoder *source_encoder;
12478 	struct drm_connector *connector;
12479 	struct drm_connector_state *connector_state;
12480 	int i;
12481 
12482 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12483 		if (connector_state->crtc != &crtc->base)
12484 			continue;
12485 
12486 		source_encoder =
12487 			to_intel_encoder(connector_state->best_encoder);
12488 		if (!encoders_cloneable(encoder, source_encoder))
12489 			return false;
12490 	}
12491 
12492 	return true;
12493 }
12494 
12495 static int icl_add_linked_planes(struct intel_atomic_state *state)
12496 {
12497 	struct intel_plane *plane, *linked;
12498 	struct intel_plane_state *plane_state, *linked_plane_state;
12499 	int i;
12500 
12501 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12502 		linked = plane_state->planar_linked_plane;
12503 
12504 		if (!linked)
12505 			continue;
12506 
12507 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
12508 		if (IS_ERR(linked_plane_state))
12509 			return PTR_ERR(linked_plane_state);
12510 
12511 		WARN_ON(linked_plane_state->planar_linked_plane != plane);
12512 		WARN_ON(linked_plane_state->planar_slave == plane_state->planar_slave);
12513 	}
12514 
12515 	return 0;
12516 }
12517 
12518 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12519 {
12520 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12521 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12522 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
12523 	struct intel_plane *plane, *linked;
12524 	struct intel_plane_state *plane_state;
12525 	int i;
12526 
12527 	if (INTEL_GEN(dev_priv) < 11)
12528 		return 0;
12529 
12530 	/*
12531 	 * Destroy all old plane links and make the slave plane invisible
12532 	 * in the crtc_state->active_planes mask.
12533 	 */
12534 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12535 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
12536 			continue;
12537 
12538 		plane_state->planar_linked_plane = NULL;
12539 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
12540 			crtc_state->active_planes &= ~BIT(plane->id);
12541 			crtc_state->update_planes |= BIT(plane->id);
12542 		}
12543 
12544 		plane_state->planar_slave = false;
12545 	}
12546 
12547 	if (!crtc_state->nv12_planes)
12548 		return 0;
12549 
12550 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12551 		struct intel_plane_state *linked_state = NULL;
12552 
12553 		if (plane->pipe != crtc->pipe ||
12554 		    !(crtc_state->nv12_planes & BIT(plane->id)))
12555 			continue;
12556 
12557 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
12558 			if (!icl_is_nv12_y_plane(linked->id))
12559 				continue;
12560 
12561 			if (crtc_state->active_planes & BIT(linked->id))
12562 				continue;
12563 
12564 			linked_state = intel_atomic_get_plane_state(state, linked);
12565 			if (IS_ERR(linked_state))
12566 				return PTR_ERR(linked_state);
12567 
12568 			break;
12569 		}
12570 
12571 		if (!linked_state) {
12572 			drm_dbg_kms(&dev_priv->drm,
12573 				    "Need %d free Y planes for planar YUV\n",
12574 				    hweight8(crtc_state->nv12_planes));
12575 
12576 			return -EINVAL;
12577 		}
12578 
12579 		plane_state->planar_linked_plane = linked;
12580 
12581 		linked_state->planar_slave = true;
12582 		linked_state->planar_linked_plane = plane;
12583 		crtc_state->active_planes |= BIT(linked->id);
12584 		crtc_state->update_planes |= BIT(linked->id);
12585 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12586 			    linked->base.name, plane->base.name);
12587 
12588 		/* Copy parameters to slave plane */
12589 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12590 		linked_state->color_ctl = plane_state->color_ctl;
12591 		linked_state->view = plane_state->view;
12592 		memcpy(linked_state->color_plane, plane_state->color_plane,
12593 		       sizeof(linked_state->color_plane));
12594 
12595 		intel_plane_copy_uapi_to_hw_state(linked_state, plane_state);
12596 		linked_state->uapi.src = plane_state->uapi.src;
12597 		linked_state->uapi.dst = plane_state->uapi.dst;
12598 
12599 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
12600 			if (linked->id == PLANE_SPRITE5)
12601 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12602 			else if (linked->id == PLANE_SPRITE4)
12603 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
12604 			else
12605 				MISSING_CASE(linked->id);
12606 		}
12607 	}
12608 
12609 	return 0;
12610 }
12611 
12612 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12613 {
12614 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
12615 	struct intel_atomic_state *state =
12616 		to_intel_atomic_state(new_crtc_state->uapi.state);
12617 	const struct intel_crtc_state *old_crtc_state =
12618 		intel_atomic_get_old_crtc_state(state, crtc);
12619 
12620 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12621 }
12622 
12623 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12624 {
12625 	const struct drm_display_mode *adjusted_mode =
12626 		&crtc_state->hw.adjusted_mode;
12627 
12628 	if (!crtc_state->hw.enable)
12629 		return 0;
12630 
12631 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12632 				 adjusted_mode->crtc_clock);
12633 }
12634 
12635 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12636 			       const struct intel_cdclk_state *cdclk_state)
12637 {
12638 	const struct drm_display_mode *adjusted_mode =
12639 		&crtc_state->hw.adjusted_mode;
12640 
12641 	if (!crtc_state->hw.enable)
12642 		return 0;
12643 
12644 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12645 				 cdclk_state->logical.cdclk);
12646 }
12647 
12648 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12649 {
12650 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12651 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12652 	const struct drm_display_mode *adjusted_mode =
12653 		&crtc_state->hw.adjusted_mode;
12654 	u16 linetime_wm;
12655 
12656 	if (!crtc_state->hw.enable)
12657 		return 0;
12658 
12659 	linetime_wm = DIV_ROUND_UP(adjusted_mode->crtc_htotal * 1000 * 8,
12660 				   crtc_state->pixel_rate);
12661 
12662 	/* Display WA #1135: BXT:ALL GLK:ALL */
12663 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12664 		linetime_wm /= 2;
12665 
12666 	return linetime_wm;
12667 }
12668 
12669 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12670 				   struct intel_crtc *crtc)
12671 {
12672 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12673 	struct intel_crtc_state *crtc_state =
12674 		intel_atomic_get_new_crtc_state(state, crtc);
12675 	const struct intel_cdclk_state *cdclk_state;
12676 
12677 	if (INTEL_GEN(dev_priv) >= 9)
12678 		crtc_state->linetime = skl_linetime_wm(crtc_state);
12679 	else
12680 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
12681 
12682 	if (!hsw_crtc_supports_ips(crtc))
12683 		return 0;
12684 
12685 	cdclk_state = intel_atomic_get_cdclk_state(state);
12686 	if (IS_ERR(cdclk_state))
12687 		return PTR_ERR(cdclk_state);
12688 
12689 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
12690 						       cdclk_state);
12691 
12692 	return 0;
12693 }
12694 
12695 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
12696 				   struct intel_crtc *crtc)
12697 {
12698 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12699 	struct intel_crtc_state *crtc_state =
12700 		intel_atomic_get_new_crtc_state(state, crtc);
12701 	bool mode_changed = needs_modeset(crtc_state);
12702 	int ret;
12703 
12704 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
12705 	    mode_changed && !crtc_state->hw.active)
12706 		crtc_state->update_wm_post = true;
12707 
12708 	if (mode_changed && crtc_state->hw.enable &&
12709 	    dev_priv->display.crtc_compute_clock &&
12710 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
12711 		ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
12712 		if (ret)
12713 			return ret;
12714 	}
12715 
12716 	/*
12717 	 * May need to update pipe gamma enable bits
12718 	 * when C8 planes are getting enabled/disabled.
12719 	 */
12720 	if (c8_planes_changed(crtc_state))
12721 		crtc_state->uapi.color_mgmt_changed = true;
12722 
12723 	if (mode_changed || crtc_state->update_pipe ||
12724 	    crtc_state->uapi.color_mgmt_changed) {
12725 		ret = intel_color_check(crtc_state);
12726 		if (ret)
12727 			return ret;
12728 	}
12729 
12730 	if (dev_priv->display.compute_pipe_wm) {
12731 		ret = dev_priv->display.compute_pipe_wm(crtc_state);
12732 		if (ret) {
12733 			drm_dbg_kms(&dev_priv->drm,
12734 				    "Target pipe watermarks are invalid\n");
12735 			return ret;
12736 		}
12737 	}
12738 
12739 	if (dev_priv->display.compute_intermediate_wm) {
12740 		if (drm_WARN_ON(&dev_priv->drm,
12741 				!dev_priv->display.compute_pipe_wm))
12742 			return 0;
12743 
12744 		/*
12745 		 * Calculate 'intermediate' watermarks that satisfy both the
12746 		 * old state and the new state.  We can program these
12747 		 * immediately.
12748 		 */
12749 		ret = dev_priv->display.compute_intermediate_wm(crtc_state);
12750 		if (ret) {
12751 			drm_dbg_kms(&dev_priv->drm,
12752 				    "No valid intermediate pipe watermarks are possible\n");
12753 			return ret;
12754 		}
12755 	}
12756 
12757 	if (INTEL_GEN(dev_priv) >= 9) {
12758 		if (mode_changed || crtc_state->update_pipe) {
12759 			ret = skl_update_scaler_crtc(crtc_state);
12760 			if (ret)
12761 				return ret;
12762 		}
12763 
12764 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
12765 		if (ret)
12766 			return ret;
12767 	}
12768 
12769 	if (HAS_IPS(dev_priv)) {
12770 		ret = hsw_compute_ips_config(crtc_state);
12771 		if (ret)
12772 			return ret;
12773 	}
12774 
12775 	if (INTEL_GEN(dev_priv) >= 9 ||
12776 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12777 		ret = hsw_compute_linetime_wm(state, crtc);
12778 		if (ret)
12779 			return ret;
12780 
12781 	}
12782 
12783 	return 0;
12784 }
12785 
12786 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12787 {
12788 	struct intel_connector *connector;
12789 	struct drm_connector_list_iter conn_iter;
12790 
12791 	drm_connector_list_iter_begin(dev, &conn_iter);
12792 	for_each_intel_connector_iter(connector, &conn_iter) {
12793 		if (connector->base.state->crtc)
12794 			drm_connector_put(&connector->base);
12795 
12796 		if (connector->base.encoder) {
12797 			connector->base.state->best_encoder =
12798 				connector->base.encoder;
12799 			connector->base.state->crtc =
12800 				connector->base.encoder->crtc;
12801 
12802 			drm_connector_get(&connector->base);
12803 		} else {
12804 			connector->base.state->best_encoder = NULL;
12805 			connector->base.state->crtc = NULL;
12806 		}
12807 	}
12808 	drm_connector_list_iter_end(&conn_iter);
12809 }
12810 
12811 static int
12812 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
12813 		      struct intel_crtc_state *pipe_config)
12814 {
12815 	struct drm_connector *connector = conn_state->connector;
12816 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12817 	const struct drm_display_info *info = &connector->display_info;
12818 	int bpp;
12819 
12820 	switch (conn_state->max_bpc) {
12821 	case 6 ... 7:
12822 		bpp = 6 * 3;
12823 		break;
12824 	case 8 ... 9:
12825 		bpp = 8 * 3;
12826 		break;
12827 	case 10 ... 11:
12828 		bpp = 10 * 3;
12829 		break;
12830 	case 12:
12831 		bpp = 12 * 3;
12832 		break;
12833 	default:
12834 		return -EINVAL;
12835 	}
12836 
12837 	if (bpp < pipe_config->pipe_bpp) {
12838 		drm_dbg_kms(&i915->drm,
12839 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
12840 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
12841 			    connector->base.id, connector->name,
12842 			    bpp, 3 * info->bpc,
12843 			    3 * conn_state->max_requested_bpc,
12844 			    pipe_config->pipe_bpp);
12845 
12846 		pipe_config->pipe_bpp = bpp;
12847 	}
12848 
12849 	return 0;
12850 }
12851 
12852 static int
12853 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
12854 			  struct intel_crtc_state *pipe_config)
12855 {
12856 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12857 	struct drm_atomic_state *state = pipe_config->uapi.state;
12858 	struct drm_connector *connector;
12859 	struct drm_connector_state *connector_state;
12860 	int bpp, i;
12861 
12862 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12863 	    IS_CHERRYVIEW(dev_priv)))
12864 		bpp = 10*3;
12865 	else if (INTEL_GEN(dev_priv) >= 5)
12866 		bpp = 12*3;
12867 	else
12868 		bpp = 8*3;
12869 
12870 	pipe_config->pipe_bpp = bpp;
12871 
12872 	/* Clamp display bpp to connector max bpp */
12873 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12874 		int ret;
12875 
12876 		if (connector_state->crtc != &crtc->base)
12877 			continue;
12878 
12879 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
12880 		if (ret)
12881 			return ret;
12882 	}
12883 
12884 	return 0;
12885 }
12886 
12887 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12888 {
12889 	DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12890 		      "type: 0x%x flags: 0x%x\n",
12891 		      mode->crtc_clock,
12892 		      mode->crtc_hdisplay, mode->crtc_hsync_start,
12893 		      mode->crtc_hsync_end, mode->crtc_htotal,
12894 		      mode->crtc_vdisplay, mode->crtc_vsync_start,
12895 		      mode->crtc_vsync_end, mode->crtc_vtotal,
12896 		      mode->type, mode->flags);
12897 }
12898 
12899 static inline void
12900 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
12901 		      const char *id, unsigned int lane_count,
12902 		      const struct intel_link_m_n *m_n)
12903 {
12904 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12905 
12906 	drm_dbg_kms(&i915->drm,
12907 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12908 		    id, lane_count,
12909 		    m_n->gmch_m, m_n->gmch_n,
12910 		    m_n->link_m, m_n->link_n, m_n->tu);
12911 }
12912 
12913 static void
12914 intel_dump_infoframe(struct drm_i915_private *dev_priv,
12915 		     const union hdmi_infoframe *frame)
12916 {
12917 	if (!drm_debug_enabled(DRM_UT_KMS))
12918 		return;
12919 
12920 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
12921 }
12922 
12923 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
12924 
12925 static const char * const output_type_str[] = {
12926 	OUTPUT_TYPE(UNUSED),
12927 	OUTPUT_TYPE(ANALOG),
12928 	OUTPUT_TYPE(DVO),
12929 	OUTPUT_TYPE(SDVO),
12930 	OUTPUT_TYPE(LVDS),
12931 	OUTPUT_TYPE(TVOUT),
12932 	OUTPUT_TYPE(HDMI),
12933 	OUTPUT_TYPE(DP),
12934 	OUTPUT_TYPE(EDP),
12935 	OUTPUT_TYPE(DSI),
12936 	OUTPUT_TYPE(DDI),
12937 	OUTPUT_TYPE(DP_MST),
12938 };
12939 
12940 #undef OUTPUT_TYPE
12941 
12942 static void snprintf_output_types(char *buf, size_t len,
12943 				  unsigned int output_types)
12944 {
12945 	char *str = buf;
12946 	int i;
12947 
12948 	str[0] = '\0';
12949 
12950 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
12951 		int r;
12952 
12953 		if ((output_types & BIT(i)) == 0)
12954 			continue;
12955 
12956 		r = snprintf(str, len, "%s%s",
12957 			     str != buf ? "," : "", output_type_str[i]);
12958 		if (r >= len)
12959 			break;
12960 		str += r;
12961 		len -= r;
12962 
12963 		output_types &= ~BIT(i);
12964 	}
12965 
12966 	WARN_ON_ONCE(output_types != 0);
12967 }
12968 
12969 static const char * const output_format_str[] = {
12970 	[INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
12971 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
12972 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
12973 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
12974 };
12975 
12976 static const char *output_formats(enum intel_output_format format)
12977 {
12978 	if (format >= ARRAY_SIZE(output_format_str))
12979 		format = INTEL_OUTPUT_FORMAT_INVALID;
12980 	return output_format_str[format];
12981 }
12982 
12983 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
12984 {
12985 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12986 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
12987 	const struct drm_framebuffer *fb = plane_state->hw.fb;
12988 	struct drm_format_name_buf format_name;
12989 
12990 	if (!fb) {
12991 		drm_dbg_kms(&i915->drm,
12992 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
12993 			    plane->base.base.id, plane->base.name,
12994 			    yesno(plane_state->uapi.visible));
12995 		return;
12996 	}
12997 
12998 	drm_dbg_kms(&i915->drm,
12999 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s, visible: %s\n",
13000 		    plane->base.base.id, plane->base.name,
13001 		    fb->base.id, fb->width, fb->height,
13002 		    drm_get_format_name(fb->format->format, &format_name),
13003 		    yesno(plane_state->uapi.visible));
13004 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13005 		    plane_state->hw.rotation, plane_state->scaler_id);
13006 	if (plane_state->uapi.visible)
13007 		drm_dbg_kms(&i915->drm,
13008 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13009 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
13010 			    DRM_RECT_ARG(&plane_state->uapi.dst));
13011 }
13012 
13013 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
13014 				   struct intel_atomic_state *state,
13015 				   const char *context)
13016 {
13017 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13018 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13019 	const struct intel_plane_state *plane_state;
13020 	struct intel_plane *plane;
13021 	char buf[64];
13022 	int i;
13023 
13024 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13025 		    crtc->base.base.id, crtc->base.name,
13026 		    yesno(pipe_config->hw.enable), context);
13027 
13028 	if (!pipe_config->hw.enable)
13029 		goto dump_planes;
13030 
13031 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
13032 	drm_dbg_kms(&dev_priv->drm,
13033 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
13034 		    yesno(pipe_config->hw.active),
13035 		    buf, pipe_config->output_types,
13036 		    output_formats(pipe_config->output_format));
13037 
13038 	drm_dbg_kms(&dev_priv->drm,
13039 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13040 		    transcoder_name(pipe_config->cpu_transcoder),
13041 		    pipe_config->pipe_bpp, pipe_config->dither);
13042 
13043 	if (pipe_config->has_pch_encoder)
13044 		intel_dump_m_n_config(pipe_config, "fdi",
13045 				      pipe_config->fdi_lanes,
13046 				      &pipe_config->fdi_m_n);
13047 
13048 	if (intel_crtc_has_dp_encoder(pipe_config)) {
13049 		intel_dump_m_n_config(pipe_config, "dp m_n",
13050 				pipe_config->lane_count, &pipe_config->dp_m_n);
13051 		if (pipe_config->has_drrs)
13052 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
13053 					      pipe_config->lane_count,
13054 					      &pipe_config->dp_m2_n2);
13055 	}
13056 
13057 	drm_dbg_kms(&dev_priv->drm,
13058 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13059 		    pipe_config->has_audio, pipe_config->has_infoframe,
13060 		    pipe_config->infoframes.enable);
13061 
13062 	if (pipe_config->infoframes.enable &
13063 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
13064 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13065 			    pipe_config->infoframes.gcp);
13066 	if (pipe_config->infoframes.enable &
13067 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13068 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13069 	if (pipe_config->infoframes.enable &
13070 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13071 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13072 	if (pipe_config->infoframes.enable &
13073 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13074 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
13075 
13076 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
13077 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
13078 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
13079 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
13080 	intel_dump_crtc_timings(&pipe_config->hw.adjusted_mode);
13081 	drm_dbg_kms(&dev_priv->drm,
13082 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13083 		    pipe_config->port_clock,
13084 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13085 		    pipe_config->pixel_rate);
13086 
13087 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13088 		    pipe_config->linetime, pipe_config->ips_linetime);
13089 
13090 	if (INTEL_GEN(dev_priv) >= 9)
13091 		drm_dbg_kms(&dev_priv->drm,
13092 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13093 			    crtc->num_scalers,
13094 			    pipe_config->scaler_state.scaler_users,
13095 			    pipe_config->scaler_state.scaler_id);
13096 
13097 	if (HAS_GMCH(dev_priv))
13098 		drm_dbg_kms(&dev_priv->drm,
13099 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13100 			    pipe_config->gmch_pfit.control,
13101 			    pipe_config->gmch_pfit.pgm_ratios,
13102 			    pipe_config->gmch_pfit.lvds_border_bits);
13103 	else
13104 		drm_dbg_kms(&dev_priv->drm,
13105 			    "pch pfit: pos: 0x%08x, size: 0x%08x, %s, force thru: %s\n",
13106 			    pipe_config->pch_pfit.pos,
13107 			    pipe_config->pch_pfit.size,
13108 			    enableddisabled(pipe_config->pch_pfit.enabled),
13109 			    yesno(pipe_config->pch_pfit.force_thru));
13110 
13111 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13112 		    pipe_config->ips_enabled, pipe_config->double_wide);
13113 
13114 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
13115 
13116 	if (IS_CHERRYVIEW(dev_priv))
13117 		drm_dbg_kms(&dev_priv->drm,
13118 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13119 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
13120 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13121 	else
13122 		drm_dbg_kms(&dev_priv->drm,
13123 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13124 			    pipe_config->csc_mode, pipe_config->gamma_mode,
13125 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13126 
13127 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13128 		    transcoder_name(pipe_config->mst_master_transcoder));
13129 
13130 dump_planes:
13131 	if (!state)
13132 		return;
13133 
13134 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13135 		if (plane->pipe == crtc->pipe)
13136 			intel_dump_plane_state(plane_state);
13137 	}
13138 }
13139 
13140 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
13141 {
13142 	struct drm_device *dev = state->base.dev;
13143 	struct drm_connector *connector;
13144 	struct drm_connector_list_iter conn_iter;
13145 	unsigned int used_ports = 0;
13146 	unsigned int used_mst_ports = 0;
13147 	bool ret = true;
13148 
13149 	/*
13150 	 * We're going to peek into connector->state,
13151 	 * hence connection_mutex must be held.
13152 	 */
13153 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13154 
13155 	/*
13156 	 * Walk the connector list instead of the encoder
13157 	 * list to detect the problem on ddi platforms
13158 	 * where there's just one encoder per digital port.
13159 	 */
13160 	drm_connector_list_iter_begin(dev, &conn_iter);
13161 	drm_for_each_connector_iter(connector, &conn_iter) {
13162 		struct drm_connector_state *connector_state;
13163 		struct intel_encoder *encoder;
13164 
13165 		connector_state =
13166 			drm_atomic_get_new_connector_state(&state->base,
13167 							   connector);
13168 		if (!connector_state)
13169 			connector_state = connector->state;
13170 
13171 		if (!connector_state->best_encoder)
13172 			continue;
13173 
13174 		encoder = to_intel_encoder(connector_state->best_encoder);
13175 
13176 		drm_WARN_ON(dev, !connector_state->crtc);
13177 
13178 		switch (encoder->type) {
13179 		case INTEL_OUTPUT_DDI:
13180 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
13181 				break;
13182 			/* else, fall through */
13183 		case INTEL_OUTPUT_DP:
13184 		case INTEL_OUTPUT_HDMI:
13185 		case INTEL_OUTPUT_EDP:
13186 			/* the same port mustn't appear more than once */
13187 			if (used_ports & BIT(encoder->port))
13188 				ret = false;
13189 
13190 			used_ports |= BIT(encoder->port);
13191 			break;
13192 		case INTEL_OUTPUT_DP_MST:
13193 			used_mst_ports |=
13194 				1 << encoder->port;
13195 			break;
13196 		default:
13197 			break;
13198 		}
13199 	}
13200 	drm_connector_list_iter_end(&conn_iter);
13201 
13202 	/* can't mix MST and SST/HDMI on the same port */
13203 	if (used_ports & used_mst_ports)
13204 		return false;
13205 
13206 	return ret;
13207 }
13208 
13209 static void
13210 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_crtc_state *crtc_state)
13211 {
13212 	intel_crtc_copy_color_blobs(crtc_state);
13213 }
13214 
13215 static void
13216 intel_crtc_copy_uapi_to_hw_state(struct intel_crtc_state *crtc_state)
13217 {
13218 	crtc_state->hw.enable = crtc_state->uapi.enable;
13219 	crtc_state->hw.active = crtc_state->uapi.active;
13220 	crtc_state->hw.mode = crtc_state->uapi.mode;
13221 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
13222 	intel_crtc_copy_uapi_to_hw_state_nomodeset(crtc_state);
13223 }
13224 
13225 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13226 {
13227 	crtc_state->uapi.enable = crtc_state->hw.enable;
13228 	crtc_state->uapi.active = crtc_state->hw.active;
13229 	WARN_ON(drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
13230 
13231 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
13232 
13233 	/* copy color blobs to uapi */
13234 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13235 				  crtc_state->hw.degamma_lut);
13236 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13237 				  crtc_state->hw.gamma_lut);
13238 	drm_property_replace_blob(&crtc_state->uapi.ctm,
13239 				  crtc_state->hw.ctm);
13240 }
13241 
13242 static int
13243 intel_crtc_prepare_cleared_state(struct intel_crtc_state *crtc_state)
13244 {
13245 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13246 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13247 	struct intel_crtc_state *saved_state;
13248 
13249 	saved_state = intel_crtc_state_alloc(crtc);
13250 	if (!saved_state)
13251 		return -ENOMEM;
13252 
13253 	/* free the old crtc_state->hw members */
13254 	intel_crtc_free_hw_state(crtc_state);
13255 
13256 	/* FIXME: before the switch to atomic started, a new pipe_config was
13257 	 * kzalloc'd. Code that depends on any field being zero should be
13258 	 * fixed, so that the crtc_state can be safely duplicated. For now,
13259 	 * only fields that are know to not cause problems are preserved. */
13260 
13261 	saved_state->uapi = crtc_state->uapi;
13262 	saved_state->scaler_state = crtc_state->scaler_state;
13263 	saved_state->shared_dpll = crtc_state->shared_dpll;
13264 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13265 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13266 	       sizeof(saved_state->icl_port_dplls));
13267 	saved_state->crc_enabled = crtc_state->crc_enabled;
13268 	if (IS_G4X(dev_priv) ||
13269 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13270 		saved_state->wm = crtc_state->wm;
13271 
13272 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13273 	kfree(saved_state);
13274 
13275 	intel_crtc_copy_uapi_to_hw_state(crtc_state);
13276 
13277 	return 0;
13278 }
13279 
13280 static int
13281 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
13282 {
13283 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
13284 	struct drm_atomic_state *state = pipe_config->uapi.state;
13285 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13286 	struct drm_connector *connector;
13287 	struct drm_connector_state *connector_state;
13288 	int base_bpp, ret, i;
13289 	bool retry = true;
13290 
13291 	pipe_config->cpu_transcoder =
13292 		(enum transcoder) to_intel_crtc(crtc)->pipe;
13293 
13294 	/*
13295 	 * Sanitize sync polarity flags based on requested ones. If neither
13296 	 * positive or negative polarity is requested, treat this as meaning
13297 	 * negative polarity.
13298 	 */
13299 	if (!(pipe_config->hw.adjusted_mode.flags &
13300 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
13301 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
13302 
13303 	if (!(pipe_config->hw.adjusted_mode.flags &
13304 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
13305 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
13306 
13307 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13308 					pipe_config);
13309 	if (ret)
13310 		return ret;
13311 
13312 	base_bpp = pipe_config->pipe_bpp;
13313 
13314 	/*
13315 	 * Determine the real pipe dimensions. Note that stereo modes can
13316 	 * increase the actual pipe size due to the frame doubling and
13317 	 * insertion of additional space for blanks between the frame. This
13318 	 * is stored in the crtc timings. We use the requested mode to do this
13319 	 * computation to clearly distinguish it from the adjusted mode, which
13320 	 * can be changed by the connectors in the below retry loop.
13321 	 */
13322 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
13323 			       &pipe_config->pipe_src_w,
13324 			       &pipe_config->pipe_src_h);
13325 
13326 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13327 		struct intel_encoder *encoder =
13328 			to_intel_encoder(connector_state->best_encoder);
13329 
13330 		if (connector_state->crtc != crtc)
13331 			continue;
13332 
13333 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13334 			drm_dbg_kms(&i915->drm,
13335 				    "rejecting invalid cloning configuration\n");
13336 			return -EINVAL;
13337 		}
13338 
13339 		/*
13340 		 * Determine output_types before calling the .compute_config()
13341 		 * hooks so that the hooks can use this information safely.
13342 		 */
13343 		if (encoder->compute_output_type)
13344 			pipe_config->output_types |=
13345 				BIT(encoder->compute_output_type(encoder, pipe_config,
13346 								 connector_state));
13347 		else
13348 			pipe_config->output_types |= BIT(encoder->type);
13349 	}
13350 
13351 encoder_retry:
13352 	/* Ensure the port clock defaults are reset when retrying. */
13353 	pipe_config->port_clock = 0;
13354 	pipe_config->pixel_multiplier = 1;
13355 
13356 	/* Fill in default crtc timings, allow encoders to overwrite them. */
13357 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
13358 			      CRTC_STEREO_DOUBLE);
13359 
13360 	/* Pass our mode to the connectors and the CRTC to give them a chance to
13361 	 * adjust it according to limitations or connector properties, and also
13362 	 * a chance to reject the mode entirely.
13363 	 */
13364 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13365 		struct intel_encoder *encoder =
13366 			to_intel_encoder(connector_state->best_encoder);
13367 
13368 		if (connector_state->crtc != crtc)
13369 			continue;
13370 
13371 		ret = encoder->compute_config(encoder, pipe_config,
13372 					      connector_state);
13373 		if (ret < 0) {
13374 			if (ret != -EDEADLK)
13375 				drm_dbg_kms(&i915->drm,
13376 					    "Encoder config failure: %d\n",
13377 					    ret);
13378 			return ret;
13379 		}
13380 	}
13381 
13382 	/* Set default port clock if not overwritten by the encoder. Needs to be
13383 	 * done afterwards in case the encoder adjusts the mode. */
13384 	if (!pipe_config->port_clock)
13385 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
13386 			* pipe_config->pixel_multiplier;
13387 
13388 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
13389 	if (ret == -EDEADLK)
13390 		return ret;
13391 	if (ret < 0) {
13392 		drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
13393 		return ret;
13394 	}
13395 
13396 	if (ret == RETRY) {
13397 		if (drm_WARN(&i915->drm, !retry,
13398 			     "loop in pipe configuration computation\n"))
13399 			return -EINVAL;
13400 
13401 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
13402 		retry = false;
13403 		goto encoder_retry;
13404 	}
13405 
13406 	/* Dithering seems to not pass-through bits correctly when it should, so
13407 	 * only enable it on 6bpc panels and when its not a compliance
13408 	 * test requesting 6bpc video pattern.
13409 	 */
13410 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13411 		!pipe_config->dither_force_disable;
13412 	drm_dbg_kms(&i915->drm,
13413 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13414 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
13415 
13416 	/*
13417 	 * Make drm_calc_timestamping_constants in
13418 	 * drm_atomic_helper_update_legacy_modeset_state() happy
13419 	 */
13420 	pipe_config->uapi.adjusted_mode = pipe_config->hw.adjusted_mode;
13421 
13422 	return 0;
13423 }
13424 
13425 static int
13426 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13427 {
13428 	struct intel_atomic_state *state =
13429 		to_intel_atomic_state(crtc_state->uapi.state);
13430 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13431 	struct drm_connector_state *conn_state;
13432 	struct drm_connector *connector;
13433 	int i;
13434 
13435 	for_each_new_connector_in_state(&state->base, connector,
13436 					conn_state, i) {
13437 		struct intel_encoder *encoder =
13438 			to_intel_encoder(conn_state->best_encoder);
13439 		int ret;
13440 
13441 		if (conn_state->crtc != &crtc->base ||
13442 		    !encoder->compute_config_late)
13443 			continue;
13444 
13445 		ret = encoder->compute_config_late(encoder, crtc_state,
13446 						   conn_state);
13447 		if (ret)
13448 			return ret;
13449 	}
13450 
13451 	return 0;
13452 }
13453 
13454 bool intel_fuzzy_clock_check(int clock1, int clock2)
13455 {
13456 	int diff;
13457 
13458 	if (clock1 == clock2)
13459 		return true;
13460 
13461 	if (!clock1 || !clock2)
13462 		return false;
13463 
13464 	diff = abs(clock1 - clock2);
13465 
13466 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13467 		return true;
13468 
13469 	return false;
13470 }
13471 
13472 static bool
13473 intel_compare_m_n(unsigned int m, unsigned int n,
13474 		  unsigned int m2, unsigned int n2,
13475 		  bool exact)
13476 {
13477 	if (m == m2 && n == n2)
13478 		return true;
13479 
13480 	if (exact || !m || !n || !m2 || !n2)
13481 		return false;
13482 
13483 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13484 
13485 	if (n > n2) {
13486 		while (n > n2) {
13487 			m2 <<= 1;
13488 			n2 <<= 1;
13489 		}
13490 	} else if (n < n2) {
13491 		while (n < n2) {
13492 			m <<= 1;
13493 			n <<= 1;
13494 		}
13495 	}
13496 
13497 	if (n != n2)
13498 		return false;
13499 
13500 	return intel_fuzzy_clock_check(m, m2);
13501 }
13502 
13503 static bool
13504 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13505 		       const struct intel_link_m_n *m2_n2,
13506 		       bool exact)
13507 {
13508 	return m_n->tu == m2_n2->tu &&
13509 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13510 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13511 		intel_compare_m_n(m_n->link_m, m_n->link_n,
13512 				  m2_n2->link_m, m2_n2->link_n, exact);
13513 }
13514 
13515 static bool
13516 intel_compare_infoframe(const union hdmi_infoframe *a,
13517 			const union hdmi_infoframe *b)
13518 {
13519 	return memcmp(a, b, sizeof(*a)) == 0;
13520 }
13521 
13522 static void
13523 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13524 			       bool fastset, const char *name,
13525 			       const union hdmi_infoframe *a,
13526 			       const union hdmi_infoframe *b)
13527 {
13528 	if (fastset) {
13529 		if (!drm_debug_enabled(DRM_UT_KMS))
13530 			return;
13531 
13532 		drm_dbg_kms(&dev_priv->drm,
13533 			    "fastset mismatch in %s infoframe\n", name);
13534 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13535 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
13536 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13537 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13538 	} else {
13539 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13540 		drm_err(&dev_priv->drm, "expected:\n");
13541 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
13542 		drm_err(&dev_priv->drm, "found:\n");
13543 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13544 	}
13545 }
13546 
13547 static void __printf(4, 5)
13548 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13549 		     const char *name, const char *format, ...)
13550 {
13551 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
13552 	struct va_format vaf;
13553 	va_list args;
13554 
13555 	va_start(args, format);
13556 	vaf.fmt = format;
13557 	vaf.va = &args;
13558 
13559 	if (fastset)
13560 		drm_dbg_kms(&i915->drm,
13561 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
13562 			    crtc->base.base.id, crtc->base.name, name, &vaf);
13563 	else
13564 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
13565 			crtc->base.base.id, crtc->base.name, name, &vaf);
13566 
13567 	va_end(args);
13568 }
13569 
13570 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
13571 {
13572 	if (i915_modparams.fastboot != -1)
13573 		return i915_modparams.fastboot;
13574 
13575 	/* Enable fastboot by default on Skylake and newer */
13576 	if (INTEL_GEN(dev_priv) >= 9)
13577 		return true;
13578 
13579 	/* Enable fastboot by default on VLV and CHV */
13580 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13581 		return true;
13582 
13583 	/* Disabled by default on all others */
13584 	return false;
13585 }
13586 
13587 static bool
13588 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
13589 			  const struct intel_crtc_state *pipe_config,
13590 			  bool fastset)
13591 {
13592 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
13593 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13594 	bool ret = true;
13595 	u32 bp_gamma = 0;
13596 	bool fixup_inherited = fastset &&
13597 		(current_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
13598 		!(pipe_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED);
13599 
13600 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
13601 		drm_dbg_kms(&dev_priv->drm,
13602 			    "initial modeset and fastboot not set\n");
13603 		ret = false;
13604 	}
13605 
13606 #define PIPE_CONF_CHECK_X(name) do { \
13607 	if (current_config->name != pipe_config->name) { \
13608 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13609 				     "(expected 0x%08x, found 0x%08x)", \
13610 				     current_config->name, \
13611 				     pipe_config->name); \
13612 		ret = false; \
13613 	} \
13614 } while (0)
13615 
13616 #define PIPE_CONF_CHECK_I(name) do { \
13617 	if (current_config->name != pipe_config->name) { \
13618 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13619 				     "(expected %i, found %i)", \
13620 				     current_config->name, \
13621 				     pipe_config->name); \
13622 		ret = false; \
13623 	} \
13624 } while (0)
13625 
13626 #define PIPE_CONF_CHECK_BOOL(name) do { \
13627 	if (current_config->name != pipe_config->name) { \
13628 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
13629 				     "(expected %s, found %s)", \
13630 				     yesno(current_config->name), \
13631 				     yesno(pipe_config->name)); \
13632 		ret = false; \
13633 	} \
13634 } while (0)
13635 
13636 /*
13637  * Checks state where we only read out the enabling, but not the entire
13638  * state itself (like full infoframes or ELD for audio). These states
13639  * require a full modeset on bootup to fix up.
13640  */
13641 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
13642 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
13643 		PIPE_CONF_CHECK_BOOL(name); \
13644 	} else { \
13645 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13646 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
13647 				     yesno(current_config->name), \
13648 				     yesno(pipe_config->name)); \
13649 		ret = false; \
13650 	} \
13651 } while (0)
13652 
13653 #define PIPE_CONF_CHECK_P(name) do { \
13654 	if (current_config->name != pipe_config->name) { \
13655 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13656 				     "(expected %p, found %p)", \
13657 				     current_config->name, \
13658 				     pipe_config->name); \
13659 		ret = false; \
13660 	} \
13661 } while (0)
13662 
13663 #define PIPE_CONF_CHECK_M_N(name) do { \
13664 	if (!intel_compare_link_m_n(&current_config->name, \
13665 				    &pipe_config->name,\
13666 				    !fastset)) { \
13667 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13668 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13669 				     "found tu %i, gmch %i/%i link %i/%i)", \
13670 				     current_config->name.tu, \
13671 				     current_config->name.gmch_m, \
13672 				     current_config->name.gmch_n, \
13673 				     current_config->name.link_m, \
13674 				     current_config->name.link_n, \
13675 				     pipe_config->name.tu, \
13676 				     pipe_config->name.gmch_m, \
13677 				     pipe_config->name.gmch_n, \
13678 				     pipe_config->name.link_m, \
13679 				     pipe_config->name.link_n); \
13680 		ret = false; \
13681 	} \
13682 } while (0)
13683 
13684 /* This is required for BDW+ where there is only one set of registers for
13685  * switching between high and low RR.
13686  * This macro can be used whenever a comparison has to be made between one
13687  * hw state and multiple sw state variables.
13688  */
13689 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
13690 	if (!intel_compare_link_m_n(&current_config->name, \
13691 				    &pipe_config->name, !fastset) && \
13692 	    !intel_compare_link_m_n(&current_config->alt_name, \
13693 				    &pipe_config->name, !fastset)) { \
13694 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13695 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13696 				     "or tu %i gmch %i/%i link %i/%i, " \
13697 				     "found tu %i, gmch %i/%i link %i/%i)", \
13698 				     current_config->name.tu, \
13699 				     current_config->name.gmch_m, \
13700 				     current_config->name.gmch_n, \
13701 				     current_config->name.link_m, \
13702 				     current_config->name.link_n, \
13703 				     current_config->alt_name.tu, \
13704 				     current_config->alt_name.gmch_m, \
13705 				     current_config->alt_name.gmch_n, \
13706 				     current_config->alt_name.link_m, \
13707 				     current_config->alt_name.link_n, \
13708 				     pipe_config->name.tu, \
13709 				     pipe_config->name.gmch_m, \
13710 				     pipe_config->name.gmch_n, \
13711 				     pipe_config->name.link_m, \
13712 				     pipe_config->name.link_n); \
13713 		ret = false; \
13714 	} \
13715 } while (0)
13716 
13717 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
13718 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
13719 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13720 				     "(%x) (expected %i, found %i)", \
13721 				     (mask), \
13722 				     current_config->name & (mask), \
13723 				     pipe_config->name & (mask)); \
13724 		ret = false; \
13725 	} \
13726 } while (0)
13727 
13728 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
13729 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
13730 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13731 				     "(expected %i, found %i)", \
13732 				     current_config->name, \
13733 				     pipe_config->name); \
13734 		ret = false; \
13735 	} \
13736 } while (0)
13737 
13738 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
13739 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
13740 				     &pipe_config->infoframes.name)) { \
13741 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
13742 					       &current_config->infoframes.name, \
13743 					       &pipe_config->infoframes.name); \
13744 		ret = false; \
13745 	} \
13746 } while (0)
13747 
13748 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
13749 	if (current_config->name1 != pipe_config->name1) { \
13750 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
13751 				"(expected %i, found %i, won't compare lut values)", \
13752 				current_config->name1, \
13753 				pipe_config->name1); \
13754 		ret = false;\
13755 	} else { \
13756 		if (!intel_color_lut_equal(current_config->name2, \
13757 					pipe_config->name2, pipe_config->name1, \
13758 					bit_precision)) { \
13759 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
13760 					"hw_state doesn't match sw_state"); \
13761 			ret = false; \
13762 		} \
13763 	} \
13764 } while (0)
13765 
13766 #define PIPE_CONF_QUIRK(quirk) \
13767 	((current_config->quirks | pipe_config->quirks) & (quirk))
13768 
13769 	PIPE_CONF_CHECK_I(cpu_transcoder);
13770 
13771 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
13772 	PIPE_CONF_CHECK_I(fdi_lanes);
13773 	PIPE_CONF_CHECK_M_N(fdi_m_n);
13774 
13775 	PIPE_CONF_CHECK_I(lane_count);
13776 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
13777 
13778 	if (INTEL_GEN(dev_priv) < 8) {
13779 		PIPE_CONF_CHECK_M_N(dp_m_n);
13780 
13781 		if (current_config->has_drrs)
13782 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
13783 	} else
13784 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
13785 
13786 	PIPE_CONF_CHECK_X(output_types);
13787 
13788 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
13789 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
13790 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
13791 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
13792 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
13793 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
13794 
13795 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
13796 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
13797 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
13798 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
13799 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
13800 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
13801 
13802 	PIPE_CONF_CHECK_I(pixel_multiplier);
13803 	PIPE_CONF_CHECK_I(output_format);
13804 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
13805 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
13806 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13807 		PIPE_CONF_CHECK_BOOL(limited_color_range);
13808 
13809 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
13810 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
13811 	PIPE_CONF_CHECK_BOOL(has_infoframe);
13812 	PIPE_CONF_CHECK_BOOL(fec_enable);
13813 
13814 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
13815 
13816 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13817 			      DRM_MODE_FLAG_INTERLACE);
13818 
13819 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
13820 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13821 				      DRM_MODE_FLAG_PHSYNC);
13822 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13823 				      DRM_MODE_FLAG_NHSYNC);
13824 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13825 				      DRM_MODE_FLAG_PVSYNC);
13826 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13827 				      DRM_MODE_FLAG_NVSYNC);
13828 	}
13829 
13830 	PIPE_CONF_CHECK_X(gmch_pfit.control);
13831 	/* pfit ratios are autocomputed by the hw on gen4+ */
13832 	if (INTEL_GEN(dev_priv) < 4)
13833 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
13834 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
13835 
13836 	/*
13837 	 * Changing the EDP transcoder input mux
13838 	 * (A_ONOFF vs. A_ON) requires a full modeset.
13839 	 */
13840 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
13841 
13842 	if (!fastset) {
13843 		PIPE_CONF_CHECK_I(pipe_src_w);
13844 		PIPE_CONF_CHECK_I(pipe_src_h);
13845 
13846 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
13847 		if (current_config->pch_pfit.enabled) {
13848 			PIPE_CONF_CHECK_X(pch_pfit.pos);
13849 			PIPE_CONF_CHECK_X(pch_pfit.size);
13850 		}
13851 
13852 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13853 		PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
13854 
13855 		PIPE_CONF_CHECK_X(gamma_mode);
13856 		if (IS_CHERRYVIEW(dev_priv))
13857 			PIPE_CONF_CHECK_X(cgm_mode);
13858 		else
13859 			PIPE_CONF_CHECK_X(csc_mode);
13860 		PIPE_CONF_CHECK_BOOL(gamma_enable);
13861 		PIPE_CONF_CHECK_BOOL(csc_enable);
13862 
13863 		PIPE_CONF_CHECK_I(linetime);
13864 		PIPE_CONF_CHECK_I(ips_linetime);
13865 
13866 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
13867 		if (bp_gamma)
13868 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
13869 	}
13870 
13871 	PIPE_CONF_CHECK_BOOL(double_wide);
13872 
13873 	PIPE_CONF_CHECK_P(shared_dpll);
13874 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
13875 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
13876 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13877 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
13878 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
13879 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
13880 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13881 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13882 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
13883 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
13884 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
13885 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
13886 	PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
13887 	PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
13888 	PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
13889 	PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
13890 	PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
13891 	PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
13892 	PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
13893 	PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
13894 	PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
13895 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
13896 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
13897 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
13898 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
13899 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
13900 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
13901 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
13902 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
13903 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
13904 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
13905 
13906 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13907 	PIPE_CONF_CHECK_X(dsi_pll.div);
13908 
13909 	if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
13910 		PIPE_CONF_CHECK_I(pipe_bpp);
13911 
13912 	PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
13913 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
13914 
13915 	PIPE_CONF_CHECK_I(min_voltage_level);
13916 
13917 	PIPE_CONF_CHECK_X(infoframes.enable);
13918 	PIPE_CONF_CHECK_X(infoframes.gcp);
13919 	PIPE_CONF_CHECK_INFOFRAME(avi);
13920 	PIPE_CONF_CHECK_INFOFRAME(spd);
13921 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
13922 	PIPE_CONF_CHECK_INFOFRAME(drm);
13923 
13924 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
13925 	PIPE_CONF_CHECK_I(master_transcoder);
13926 
13927 	PIPE_CONF_CHECK_I(dsc.compression_enable);
13928 	PIPE_CONF_CHECK_I(dsc.dsc_split);
13929 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
13930 
13931 	PIPE_CONF_CHECK_I(mst_master_transcoder);
13932 
13933 #undef PIPE_CONF_CHECK_X
13934 #undef PIPE_CONF_CHECK_I
13935 #undef PIPE_CONF_CHECK_BOOL
13936 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
13937 #undef PIPE_CONF_CHECK_P
13938 #undef PIPE_CONF_CHECK_FLAGS
13939 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
13940 #undef PIPE_CONF_CHECK_COLOR_LUT
13941 #undef PIPE_CONF_QUIRK
13942 
13943 	return ret;
13944 }
13945 
13946 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13947 					   const struct intel_crtc_state *pipe_config)
13948 {
13949 	if (pipe_config->has_pch_encoder) {
13950 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
13951 							    &pipe_config->fdi_m_n);
13952 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
13953 
13954 		/*
13955 		 * FDI already provided one idea for the dotclock.
13956 		 * Yell if the encoder disagrees.
13957 		 */
13958 		drm_WARN(&dev_priv->drm,
13959 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13960 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13961 			 fdi_dotclock, dotclock);
13962 	}
13963 }
13964 
13965 static void verify_wm_state(struct intel_crtc *crtc,
13966 			    struct intel_crtc_state *new_crtc_state)
13967 {
13968 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13969 	struct skl_hw_state {
13970 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
13971 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
13972 		struct skl_pipe_wm wm;
13973 	} *hw;
13974 	struct skl_pipe_wm *sw_wm;
13975 	struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
13976 	u8 hw_enabled_slices;
13977 	const enum pipe pipe = crtc->pipe;
13978 	int plane, level, max_level = ilk_wm_max_level(dev_priv);
13979 
13980 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
13981 		return;
13982 
13983 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
13984 	if (!hw)
13985 		return;
13986 
13987 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
13988 	sw_wm = &new_crtc_state->wm.skl.optimal;
13989 
13990 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
13991 
13992 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
13993 
13994 	if (INTEL_GEN(dev_priv) >= 11 &&
13995 	    hw_enabled_slices != dev_priv->enabled_dbuf_slices_mask)
13996 		drm_err(&dev_priv->drm,
13997 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
13998 			dev_priv->enabled_dbuf_slices_mask,
13999 			hw_enabled_slices);
14000 
14001 	/* planes */
14002 	for_each_universal_plane(dev_priv, pipe, plane) {
14003 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14004 
14005 		hw_plane_wm = &hw->wm.planes[plane];
14006 		sw_plane_wm = &sw_wm->planes[plane];
14007 
14008 		/* Watermarks */
14009 		for (level = 0; level <= max_level; level++) {
14010 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14011 						&sw_plane_wm->wm[level]))
14012 				continue;
14013 
14014 			drm_err(&dev_priv->drm,
14015 				"mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14016 				pipe_name(pipe), plane + 1, level,
14017 				sw_plane_wm->wm[level].plane_en,
14018 				sw_plane_wm->wm[level].plane_res_b,
14019 				sw_plane_wm->wm[level].plane_res_l,
14020 				hw_plane_wm->wm[level].plane_en,
14021 				hw_plane_wm->wm[level].plane_res_b,
14022 				hw_plane_wm->wm[level].plane_res_l);
14023 		}
14024 
14025 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14026 					 &sw_plane_wm->trans_wm)) {
14027 			drm_err(&dev_priv->drm,
14028 				"mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14029 				pipe_name(pipe), plane + 1,
14030 				sw_plane_wm->trans_wm.plane_en,
14031 				sw_plane_wm->trans_wm.plane_res_b,
14032 				sw_plane_wm->trans_wm.plane_res_l,
14033 				hw_plane_wm->trans_wm.plane_en,
14034 				hw_plane_wm->trans_wm.plane_res_b,
14035 				hw_plane_wm->trans_wm.plane_res_l);
14036 		}
14037 
14038 		/* DDB */
14039 		hw_ddb_entry = &hw->ddb_y[plane];
14040 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
14041 
14042 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14043 			drm_err(&dev_priv->drm,
14044 				"mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14045 				pipe_name(pipe), plane + 1,
14046 				sw_ddb_entry->start, sw_ddb_entry->end,
14047 				hw_ddb_entry->start, hw_ddb_entry->end);
14048 		}
14049 	}
14050 
14051 	/*
14052 	 * cursor
14053 	 * If the cursor plane isn't active, we may not have updated it's ddb
14054 	 * allocation. In that case since the ddb allocation will be updated
14055 	 * once the plane becomes visible, we can skip this check
14056 	 */
14057 	if (1) {
14058 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14059 
14060 		hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
14061 		sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14062 
14063 		/* Watermarks */
14064 		for (level = 0; level <= max_level; level++) {
14065 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14066 						&sw_plane_wm->wm[level]))
14067 				continue;
14068 
14069 			drm_err(&dev_priv->drm,
14070 				"mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14071 				pipe_name(pipe), level,
14072 				sw_plane_wm->wm[level].plane_en,
14073 				sw_plane_wm->wm[level].plane_res_b,
14074 				sw_plane_wm->wm[level].plane_res_l,
14075 				hw_plane_wm->wm[level].plane_en,
14076 				hw_plane_wm->wm[level].plane_res_b,
14077 				hw_plane_wm->wm[level].plane_res_l);
14078 		}
14079 
14080 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14081 					 &sw_plane_wm->trans_wm)) {
14082 			drm_err(&dev_priv->drm,
14083 				"mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14084 				pipe_name(pipe),
14085 				sw_plane_wm->trans_wm.plane_en,
14086 				sw_plane_wm->trans_wm.plane_res_b,
14087 				sw_plane_wm->trans_wm.plane_res_l,
14088 				hw_plane_wm->trans_wm.plane_en,
14089 				hw_plane_wm->trans_wm.plane_res_b,
14090 				hw_plane_wm->trans_wm.plane_res_l);
14091 		}
14092 
14093 		/* DDB */
14094 		hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
14095 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
14096 
14097 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14098 			drm_err(&dev_priv->drm,
14099 				"mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14100 				pipe_name(pipe),
14101 				sw_ddb_entry->start, sw_ddb_entry->end,
14102 				hw_ddb_entry->start, hw_ddb_entry->end);
14103 		}
14104 	}
14105 
14106 	kfree(hw);
14107 }
14108 
14109 static void
14110 verify_connector_state(struct intel_atomic_state *state,
14111 		       struct intel_crtc *crtc)
14112 {
14113 	struct drm_connector *connector;
14114 	struct drm_connector_state *new_conn_state;
14115 	int i;
14116 
14117 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
14118 		struct drm_encoder *encoder = connector->encoder;
14119 		struct intel_crtc_state *crtc_state = NULL;
14120 
14121 		if (new_conn_state->crtc != &crtc->base)
14122 			continue;
14123 
14124 		if (crtc)
14125 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
14126 
14127 		intel_connector_verify_state(crtc_state, new_conn_state);
14128 
14129 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
14130 		     "connector's atomic encoder doesn't match legacy encoder\n");
14131 	}
14132 }
14133 
14134 static void
14135 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
14136 {
14137 	struct intel_encoder *encoder;
14138 	struct drm_connector *connector;
14139 	struct drm_connector_state *old_conn_state, *new_conn_state;
14140 	int i;
14141 
14142 	for_each_intel_encoder(&dev_priv->drm, encoder) {
14143 		bool enabled = false, found = false;
14144 		enum pipe pipe;
14145 
14146 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14147 			    encoder->base.base.id,
14148 			    encoder->base.name);
14149 
14150 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
14151 						   new_conn_state, i) {
14152 			if (old_conn_state->best_encoder == &encoder->base)
14153 				found = true;
14154 
14155 			if (new_conn_state->best_encoder != &encoder->base)
14156 				continue;
14157 			found = enabled = true;
14158 
14159 			I915_STATE_WARN(new_conn_state->crtc !=
14160 					encoder->base.crtc,
14161 			     "connector's crtc doesn't match encoder crtc\n");
14162 		}
14163 
14164 		if (!found)
14165 			continue;
14166 
14167 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
14168 		     "encoder's enabled state mismatch "
14169 		     "(expected %i, found %i)\n",
14170 		     !!encoder->base.crtc, enabled);
14171 
14172 		if (!encoder->base.crtc) {
14173 			bool active;
14174 
14175 			active = encoder->get_hw_state(encoder, &pipe);
14176 			I915_STATE_WARN(active,
14177 			     "encoder detached but still enabled on pipe %c.\n",
14178 			     pipe_name(pipe));
14179 		}
14180 	}
14181 }
14182 
14183 static void
14184 verify_crtc_state(struct intel_crtc *crtc,
14185 		  struct intel_crtc_state *old_crtc_state,
14186 		  struct intel_crtc_state *new_crtc_state)
14187 {
14188 	struct drm_device *dev = crtc->base.dev;
14189 	struct drm_i915_private *dev_priv = to_i915(dev);
14190 	struct intel_encoder *encoder;
14191 	struct intel_crtc_state *pipe_config = old_crtc_state;
14192 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
14193 	bool active;
14194 
14195 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
14196 	intel_crtc_free_hw_state(old_crtc_state);
14197 	intel_crtc_state_reset(old_crtc_state, crtc);
14198 	old_crtc_state->uapi.state = state;
14199 
14200 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14201 		    crtc->base.name);
14202 
14203 	active = dev_priv->display.get_pipe_config(crtc, pipe_config);
14204 
14205 	/* we keep both pipes enabled on 830 */
14206 	if (IS_I830(dev_priv))
14207 		active = new_crtc_state->hw.active;
14208 
14209 	I915_STATE_WARN(new_crtc_state->hw.active != active,
14210 			"crtc active state doesn't match with hw state "
14211 			"(expected %i, found %i)\n",
14212 			new_crtc_state->hw.active, active);
14213 
14214 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14215 			"transitional active state does not match atomic hw state "
14216 			"(expected %i, found %i)\n",
14217 			new_crtc_state->hw.active, crtc->active);
14218 
14219 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14220 		enum pipe pipe;
14221 
14222 		active = encoder->get_hw_state(encoder, &pipe);
14223 		I915_STATE_WARN(active != new_crtc_state->hw.active,
14224 				"[ENCODER:%i] active %i with crtc active %i\n",
14225 				encoder->base.base.id, active,
14226 				new_crtc_state->hw.active);
14227 
14228 		I915_STATE_WARN(active && crtc->pipe != pipe,
14229 				"Encoder connected to wrong pipe %c\n",
14230 				pipe_name(pipe));
14231 
14232 		if (active)
14233 			encoder->get_config(encoder, pipe_config);
14234 	}
14235 
14236 	intel_crtc_compute_pixel_rate(pipe_config);
14237 
14238 	if (!new_crtc_state->hw.active)
14239 		return;
14240 
14241 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
14242 
14243 	if (!intel_pipe_config_compare(new_crtc_state,
14244 				       pipe_config, false)) {
14245 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
14246 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
14247 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
14248 	}
14249 }
14250 
14251 static void
14252 intel_verify_planes(struct intel_atomic_state *state)
14253 {
14254 	struct intel_plane *plane;
14255 	const struct intel_plane_state *plane_state;
14256 	int i;
14257 
14258 	for_each_new_intel_plane_in_state(state, plane,
14259 					  plane_state, i)
14260 		assert_plane(plane, plane_state->planar_slave ||
14261 			     plane_state->uapi.visible);
14262 }
14263 
14264 static void
14265 verify_single_dpll_state(struct drm_i915_private *dev_priv,
14266 			 struct intel_shared_dpll *pll,
14267 			 struct intel_crtc *crtc,
14268 			 struct intel_crtc_state *new_crtc_state)
14269 {
14270 	struct intel_dpll_hw_state dpll_hw_state;
14271 	unsigned int crtc_mask;
14272 	bool active;
14273 
14274 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
14275 
14276 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
14277 
14278 	active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
14279 
14280 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
14281 		I915_STATE_WARN(!pll->on && pll->active_mask,
14282 		     "pll in active use but not on in sw tracking\n");
14283 		I915_STATE_WARN(pll->on && !pll->active_mask,
14284 		     "pll is on but not used by any active crtc\n");
14285 		I915_STATE_WARN(pll->on != active,
14286 		     "pll on state mismatch (expected %i, found %i)\n",
14287 		     pll->on, active);
14288 	}
14289 
14290 	if (!crtc) {
14291 		I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
14292 				"more active pll users than references: %x vs %x\n",
14293 				pll->active_mask, pll->state.crtc_mask);
14294 
14295 		return;
14296 	}
14297 
14298 	crtc_mask = drm_crtc_mask(&crtc->base);
14299 
14300 	if (new_crtc_state->hw.active)
14301 		I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14302 				"pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
14303 				pipe_name(crtc->pipe), pll->active_mask);
14304 	else
14305 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14306 				"pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
14307 				pipe_name(crtc->pipe), pll->active_mask);
14308 
14309 	I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
14310 			"pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
14311 			crtc_mask, pll->state.crtc_mask);
14312 
14313 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
14314 					  &dpll_hw_state,
14315 					  sizeof(dpll_hw_state)),
14316 			"pll hw state mismatch\n");
14317 }
14318 
14319 static void
14320 verify_shared_dpll_state(struct intel_crtc *crtc,
14321 			 struct intel_crtc_state *old_crtc_state,
14322 			 struct intel_crtc_state *new_crtc_state)
14323 {
14324 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14325 
14326 	if (new_crtc_state->shared_dpll)
14327 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
14328 
14329 	if (old_crtc_state->shared_dpll &&
14330 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14331 		unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14332 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
14333 
14334 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14335 				"pll active mismatch (didn't expect pipe %c in active mask)\n",
14336 				pipe_name(crtc->pipe));
14337 		I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
14338 				"pll enabled crtcs mismatch (found %x in enabled mask)\n",
14339 				pipe_name(crtc->pipe));
14340 	}
14341 }
14342 
14343 static void
14344 intel_modeset_verify_crtc(struct intel_crtc *crtc,
14345 			  struct intel_atomic_state *state,
14346 			  struct intel_crtc_state *old_crtc_state,
14347 			  struct intel_crtc_state *new_crtc_state)
14348 {
14349 	if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
14350 		return;
14351 
14352 	verify_wm_state(crtc, new_crtc_state);
14353 	verify_connector_state(state, crtc);
14354 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14355 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
14356 }
14357 
14358 static void
14359 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
14360 {
14361 	int i;
14362 
14363 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14364 		verify_single_dpll_state(dev_priv,
14365 					 &dev_priv->dpll.shared_dplls[i],
14366 					 NULL, NULL);
14367 }
14368 
14369 static void
14370 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
14371 			      struct intel_atomic_state *state)
14372 {
14373 	verify_encoder_state(dev_priv, state);
14374 	verify_connector_state(state, NULL);
14375 	verify_disabled_dpll_state(dev_priv);
14376 }
14377 
14378 static void
14379 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
14380 {
14381 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
14382 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14383 	const struct drm_display_mode *adjusted_mode =
14384 		&crtc_state->hw.adjusted_mode;
14385 
14386 	drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
14387 
14388 	/*
14389 	 * The scanline counter increments at the leading edge of hsync.
14390 	 *
14391 	 * On most platforms it starts counting from vtotal-1 on the
14392 	 * first active line. That means the scanline counter value is
14393 	 * always one less than what we would expect. Ie. just after
14394 	 * start of vblank, which also occurs at start of hsync (on the
14395 	 * last active line), the scanline counter will read vblank_start-1.
14396 	 *
14397 	 * On gen2 the scanline counter starts counting from 1 instead
14398 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14399 	 * to keep the value positive), instead of adding one.
14400 	 *
14401 	 * On HSW+ the behaviour of the scanline counter depends on the output
14402 	 * type. For DP ports it behaves like most other platforms, but on HDMI
14403 	 * there's an extra 1 line difference. So we need to add two instead of
14404 	 * one to the value.
14405 	 *
14406 	 * On VLV/CHV DSI the scanline counter would appear to increment
14407 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14408 	 * that means we can't tell whether we're in vblank or not while
14409 	 * we're on that particular line. We must still set scanline_offset
14410 	 * to 1 so that the vblank timestamps come out correct when we query
14411 	 * the scanline counter from within the vblank interrupt handler.
14412 	 * However if queried just before the start of vblank we'll get an
14413 	 * answer that's slightly in the future.
14414 	 */
14415 	if (IS_GEN(dev_priv, 2)) {
14416 		int vtotal;
14417 
14418 		vtotal = adjusted_mode->crtc_vtotal;
14419 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
14420 			vtotal /= 2;
14421 
14422 		crtc->scanline_offset = vtotal - 1;
14423 	} else if (HAS_DDI(dev_priv) &&
14424 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
14425 		crtc->scanline_offset = 2;
14426 	} else {
14427 		crtc->scanline_offset = 1;
14428 	}
14429 }
14430 
14431 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
14432 {
14433 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14434 	struct intel_crtc_state *new_crtc_state;
14435 	struct intel_crtc *crtc;
14436 	int i;
14437 
14438 	if (!dev_priv->display.crtc_compute_clock)
14439 		return;
14440 
14441 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14442 		if (!needs_modeset(new_crtc_state))
14443 			continue;
14444 
14445 		intel_release_shared_dplls(state, crtc);
14446 	}
14447 }
14448 
14449 /*
14450  * This implements the workaround described in the "notes" section of the mode
14451  * set sequence documentation. When going from no pipes or single pipe to
14452  * multiple pipes, and planes are enabled after the pipe, we need to wait at
14453  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14454  */
14455 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
14456 {
14457 	struct intel_crtc_state *crtc_state;
14458 	struct intel_crtc *crtc;
14459 	struct intel_crtc_state *first_crtc_state = NULL;
14460 	struct intel_crtc_state *other_crtc_state = NULL;
14461 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14462 	int i;
14463 
14464 	/* look at all crtc's that are going to be enabled in during modeset */
14465 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14466 		if (!crtc_state->hw.active ||
14467 		    !needs_modeset(crtc_state))
14468 			continue;
14469 
14470 		if (first_crtc_state) {
14471 			other_crtc_state = crtc_state;
14472 			break;
14473 		} else {
14474 			first_crtc_state = crtc_state;
14475 			first_pipe = crtc->pipe;
14476 		}
14477 	}
14478 
14479 	/* No workaround needed? */
14480 	if (!first_crtc_state)
14481 		return 0;
14482 
14483 	/* w/a possibly needed, check how many crtc's are already enabled. */
14484 	for_each_intel_crtc(state->base.dev, crtc) {
14485 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14486 		if (IS_ERR(crtc_state))
14487 			return PTR_ERR(crtc_state);
14488 
14489 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
14490 
14491 		if (!crtc_state->hw.active ||
14492 		    needs_modeset(crtc_state))
14493 			continue;
14494 
14495 		/* 2 or more enabled crtcs means no need for w/a */
14496 		if (enabled_pipe != INVALID_PIPE)
14497 			return 0;
14498 
14499 		enabled_pipe = crtc->pipe;
14500 	}
14501 
14502 	if (enabled_pipe != INVALID_PIPE)
14503 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
14504 	else if (other_crtc_state)
14505 		other_crtc_state->hsw_workaround_pipe = first_pipe;
14506 
14507 	return 0;
14508 }
14509 
14510 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
14511 			   u8 active_pipes)
14512 {
14513 	const struct intel_crtc_state *crtc_state;
14514 	struct intel_crtc *crtc;
14515 	int i;
14516 
14517 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14518 		if (crtc_state->hw.active)
14519 			active_pipes |= BIT(crtc->pipe);
14520 		else
14521 			active_pipes &= ~BIT(crtc->pipe);
14522 	}
14523 
14524 	return active_pipes;
14525 }
14526 
14527 static int intel_modeset_checks(struct intel_atomic_state *state)
14528 {
14529 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14530 	int ret;
14531 
14532 	state->modeset = true;
14533 	state->active_pipes = intel_calc_active_pipes(state, dev_priv->active_pipes);
14534 
14535 	state->active_pipe_changes = state->active_pipes ^ dev_priv->active_pipes;
14536 
14537 	if (state->active_pipe_changes) {
14538 		ret = _intel_atomic_lock_global_state(state);
14539 		if (ret)
14540 			return ret;
14541 	}
14542 
14543 	ret = intel_modeset_calc_cdclk(state);
14544 	if (ret)
14545 		return ret;
14546 
14547 	intel_modeset_clear_plls(state);
14548 
14549 	if (IS_HASWELL(dev_priv))
14550 		return hsw_mode_set_planes_workaround(state);
14551 
14552 	return 0;
14553 }
14554 
14555 /*
14556  * Handle calculation of various watermark data at the end of the atomic check
14557  * phase.  The code here should be run after the per-crtc and per-plane 'check'
14558  * handlers to ensure that all derived state has been updated.
14559  */
14560 static int calc_watermark_data(struct intel_atomic_state *state)
14561 {
14562 	struct drm_device *dev = state->base.dev;
14563 	struct drm_i915_private *dev_priv = to_i915(dev);
14564 
14565 	/* Is there platform-specific watermark information to calculate? */
14566 	if (dev_priv->display.compute_global_watermarks)
14567 		return dev_priv->display.compute_global_watermarks(state);
14568 
14569 	return 0;
14570 }
14571 
14572 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
14573 				     struct intel_crtc_state *new_crtc_state)
14574 {
14575 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
14576 		return;
14577 
14578 	new_crtc_state->uapi.mode_changed = false;
14579 	new_crtc_state->update_pipe = true;
14580 }
14581 
14582 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
14583 				    struct intel_crtc_state *new_crtc_state)
14584 {
14585 	/*
14586 	 * If we're not doing the full modeset we want to
14587 	 * keep the current M/N values as they may be
14588 	 * sufficiently different to the computed values
14589 	 * to cause problems.
14590 	 *
14591 	 * FIXME: should really copy more fuzzy state here
14592 	 */
14593 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
14594 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
14595 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
14596 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
14597 }
14598 
14599 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
14600 					  struct intel_crtc *crtc,
14601 					  u8 plane_ids_mask)
14602 {
14603 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14604 	struct intel_plane *plane;
14605 
14606 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14607 		struct intel_plane_state *plane_state;
14608 
14609 		if ((plane_ids_mask & BIT(plane->id)) == 0)
14610 			continue;
14611 
14612 		plane_state = intel_atomic_get_plane_state(state, plane);
14613 		if (IS_ERR(plane_state))
14614 			return PTR_ERR(plane_state);
14615 	}
14616 
14617 	return 0;
14618 }
14619 
14620 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
14621 {
14622 	/* See {hsw,vlv,ivb}_plane_ratio() */
14623 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
14624 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
14625 		IS_IVYBRIDGE(dev_priv);
14626 }
14627 
14628 static int intel_atomic_check_planes(struct intel_atomic_state *state,
14629 				     bool *need_cdclk_calc)
14630 {
14631 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14632 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14633 	struct intel_plane_state *plane_state;
14634 	struct intel_plane *plane;
14635 	struct intel_crtc *crtc;
14636 	int i, ret;
14637 
14638 	ret = icl_add_linked_planes(state);
14639 	if (ret)
14640 		return ret;
14641 
14642 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14643 		ret = intel_plane_atomic_check(state, plane);
14644 		if (ret) {
14645 			drm_dbg_atomic(&dev_priv->drm,
14646 				       "[PLANE:%d:%s] atomic driver check failed\n",
14647 				       plane->base.base.id, plane->base.name);
14648 			return ret;
14649 		}
14650 	}
14651 
14652 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14653 					    new_crtc_state, i) {
14654 		u8 old_active_planes, new_active_planes;
14655 
14656 		ret = icl_check_nv12_planes(new_crtc_state);
14657 		if (ret)
14658 			return ret;
14659 
14660 		/*
14661 		 * On some platforms the number of active planes affects
14662 		 * the planes' minimum cdclk calculation. Add such planes
14663 		 * to the state before we compute the minimum cdclk.
14664 		 */
14665 		if (!active_planes_affects_min_cdclk(dev_priv))
14666 			continue;
14667 
14668 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14669 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14670 
14671 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
14672 			continue;
14673 
14674 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
14675 		if (ret)
14676 			return ret;
14677 	}
14678 
14679 	/*
14680 	 * active_planes bitmask has been updated, and potentially
14681 	 * affected planes are part of the state. We can now
14682 	 * compute the minimum cdclk for each plane.
14683 	 */
14684 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14685 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
14686 		if (ret)
14687 			return ret;
14688 	}
14689 
14690 	return 0;
14691 }
14692 
14693 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
14694 {
14695 	struct intel_crtc_state *crtc_state;
14696 	struct intel_crtc *crtc;
14697 	int i;
14698 
14699 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14700 		int ret = intel_crtc_atomic_check(state, crtc);
14701 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
14702 		if (ret) {
14703 			drm_dbg_atomic(&i915->drm,
14704 				       "[CRTC:%d:%s] atomic driver check failed\n",
14705 				       crtc->base.base.id, crtc->base.name);
14706 			return ret;
14707 		}
14708 	}
14709 
14710 	return 0;
14711 }
14712 
14713 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
14714 					       u8 transcoders)
14715 {
14716 	const struct intel_crtc_state *new_crtc_state;
14717 	struct intel_crtc *crtc;
14718 	int i;
14719 
14720 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14721 		if (new_crtc_state->hw.enable &&
14722 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
14723 		    needs_modeset(new_crtc_state))
14724 			return true;
14725 	}
14726 
14727 	return false;
14728 }
14729 
14730 /**
14731  * intel_atomic_check - validate state object
14732  * @dev: drm device
14733  * @_state: state to validate
14734  */
14735 static int intel_atomic_check(struct drm_device *dev,
14736 			      struct drm_atomic_state *_state)
14737 {
14738 	struct drm_i915_private *dev_priv = to_i915(dev);
14739 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
14740 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14741 	struct intel_cdclk_state *new_cdclk_state;
14742 	struct intel_crtc *crtc;
14743 	int ret, i;
14744 	bool any_ms = false;
14745 
14746 	/* Catch I915_MODE_FLAG_INHERITED */
14747 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14748 					    new_crtc_state, i) {
14749 		if (new_crtc_state->uapi.mode.private_flags !=
14750 		    old_crtc_state->uapi.mode.private_flags)
14751 			new_crtc_state->uapi.mode_changed = true;
14752 	}
14753 
14754 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
14755 	if (ret)
14756 		goto fail;
14757 
14758 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14759 					    new_crtc_state, i) {
14760 		if (!needs_modeset(new_crtc_state)) {
14761 			/* Light copy */
14762 			intel_crtc_copy_uapi_to_hw_state_nomodeset(new_crtc_state);
14763 
14764 			continue;
14765 		}
14766 
14767 		ret = intel_crtc_prepare_cleared_state(new_crtc_state);
14768 		if (ret)
14769 			goto fail;
14770 
14771 		if (!new_crtc_state->hw.enable)
14772 			continue;
14773 
14774 		ret = intel_modeset_pipe_config(new_crtc_state);
14775 		if (ret)
14776 			goto fail;
14777 	}
14778 
14779 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14780 					    new_crtc_state, i) {
14781 		if (!needs_modeset(new_crtc_state))
14782 			continue;
14783 
14784 		ret = intel_modeset_pipe_config_late(new_crtc_state);
14785 		if (ret)
14786 			goto fail;
14787 
14788 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
14789 	}
14790 
14791 	/**
14792 	 * Check if fastset is allowed by external dependencies like other
14793 	 * pipes and transcoders.
14794 	 *
14795 	 * Right now it only forces a fullmodeset when the MST master
14796 	 * transcoder did not changed but the pipe of the master transcoder
14797 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
14798 	 * in case of port synced crtcs, if one of the synced crtcs
14799 	 * needs a full modeset, all other synced crtcs should be
14800 	 * forced a full modeset.
14801 	 */
14802 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14803 		if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
14804 			continue;
14805 
14806 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
14807 			enum transcoder master = new_crtc_state->mst_master_transcoder;
14808 
14809 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
14810 				new_crtc_state->uapi.mode_changed = true;
14811 				new_crtc_state->update_pipe = false;
14812 			}
14813 		}
14814 
14815 		if (is_trans_port_sync_mode(new_crtc_state)) {
14816 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
14817 
14818 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
14819 				trans |= BIT(new_crtc_state->master_transcoder);
14820 
14821 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
14822 				new_crtc_state->uapi.mode_changed = true;
14823 				new_crtc_state->update_pipe = false;
14824 			}
14825 		}
14826 	}
14827 
14828 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14829 					    new_crtc_state, i) {
14830 		if (needs_modeset(new_crtc_state)) {
14831 			any_ms = true;
14832 			continue;
14833 		}
14834 
14835 		if (!new_crtc_state->update_pipe)
14836 			continue;
14837 
14838 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
14839 	}
14840 
14841 	if (any_ms && !check_digital_port_conflicts(state)) {
14842 		drm_dbg_kms(&dev_priv->drm,
14843 			    "rejecting conflicting digital port configuration\n");
14844 		ret = EINVAL;
14845 		goto fail;
14846 	}
14847 
14848 	ret = drm_dp_mst_atomic_check(&state->base);
14849 	if (ret)
14850 		goto fail;
14851 
14852 	ret = intel_atomic_check_planes(state, &any_ms);
14853 	if (ret)
14854 		goto fail;
14855 
14856 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
14857 	if (new_cdclk_state && new_cdclk_state->force_min_cdclk_changed)
14858 		any_ms = true;
14859 
14860 	/*
14861 	 * distrust_bios_wm will force a full dbuf recomputation
14862 	 * but the hardware state will only get updated accordingly
14863 	 * if state->modeset==true. Hence distrust_bios_wm==true &&
14864 	 * state->modeset==false is an invalid combination which
14865 	 * would cause the hardware and software dbuf state to get
14866 	 * out of sync. We must prevent that.
14867 	 *
14868 	 * FIXME clean up this mess and introduce better
14869 	 * state tracking for dbuf.
14870 	 */
14871 	if (dev_priv->wm.distrust_bios_wm)
14872 		any_ms = true;
14873 
14874 	if (any_ms) {
14875 		ret = intel_modeset_checks(state);
14876 		if (ret)
14877 			goto fail;
14878 	}
14879 
14880 	ret = intel_atomic_check_crtcs(state);
14881 	if (ret)
14882 		goto fail;
14883 
14884 	intel_fbc_choose_crtc(dev_priv, state);
14885 	ret = calc_watermark_data(state);
14886 	if (ret)
14887 		goto fail;
14888 
14889 	ret = intel_bw_atomic_check(state);
14890 	if (ret)
14891 		goto fail;
14892 
14893 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14894 					    new_crtc_state, i) {
14895 		if (!needs_modeset(new_crtc_state) &&
14896 		    !new_crtc_state->update_pipe)
14897 			continue;
14898 
14899 		intel_dump_pipe_config(new_crtc_state, state,
14900 				       needs_modeset(new_crtc_state) ?
14901 				       "[modeset]" : "[fastset]");
14902 	}
14903 
14904 	return 0;
14905 
14906  fail:
14907 	if (ret == -EDEADLK)
14908 		return ret;
14909 
14910 	/*
14911 	 * FIXME would probably be nice to know which crtc specifically
14912 	 * caused the failure, in cases where we can pinpoint it.
14913 	 */
14914 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14915 					    new_crtc_state, i)
14916 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
14917 
14918 	return ret;
14919 }
14920 
14921 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
14922 {
14923 	return drm_atomic_helper_prepare_planes(state->base.dev,
14924 						&state->base);
14925 }
14926 
14927 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14928 {
14929 	struct drm_device *dev = crtc->base.dev;
14930 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
14931 
14932 	if (!vblank->max_vblank_count)
14933 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
14934 
14935 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
14936 }
14937 
14938 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
14939 				  struct intel_crtc_state *crtc_state)
14940 {
14941 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14942 
14943 	if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
14944 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
14945 
14946 	if (crtc_state->has_pch_encoder) {
14947 		enum pipe pch_transcoder =
14948 			intel_crtc_pch_transcoder(crtc);
14949 
14950 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
14951 	}
14952 }
14953 
14954 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
14955 			       const struct intel_crtc_state *new_crtc_state)
14956 {
14957 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
14958 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14959 
14960 	/*
14961 	 * Update pipe size and adjust fitter if needed: the reason for this is
14962 	 * that in compute_mode_changes we check the native mode (not the pfit
14963 	 * mode) to see if we can flip rather than do a full mode set. In the
14964 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
14965 	 * pfit state, we'll end up with a big fb scanned out into the wrong
14966 	 * sized surface.
14967 	 */
14968 	intel_set_pipe_src_size(new_crtc_state);
14969 
14970 	/* on skylake this is done by detaching scalers */
14971 	if (INTEL_GEN(dev_priv) >= 9) {
14972 		skl_detach_scalers(new_crtc_state);
14973 
14974 		if (new_crtc_state->pch_pfit.enabled)
14975 			skl_pfit_enable(new_crtc_state);
14976 	} else if (HAS_PCH_SPLIT(dev_priv)) {
14977 		if (new_crtc_state->pch_pfit.enabled)
14978 			ilk_pfit_enable(new_crtc_state);
14979 		else if (old_crtc_state->pch_pfit.enabled)
14980 			ilk_pfit_disable(old_crtc_state);
14981 	}
14982 
14983 	/*
14984 	 * The register is supposedly single buffered so perhaps
14985 	 * not 100% correct to do this here. But SKL+ calculate
14986 	 * this based on the adjust pixel rate so pfit changes do
14987 	 * affect it and so it must be updated for fastsets.
14988 	 * HSW/BDW only really need this here for fastboot, after
14989 	 * that the value should not change without a full modeset.
14990 	 */
14991 	if (INTEL_GEN(dev_priv) >= 9 ||
14992 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
14993 		hsw_set_linetime_wm(new_crtc_state);
14994 
14995 	if (INTEL_GEN(dev_priv) >= 11)
14996 		icl_set_pipe_chicken(crtc);
14997 }
14998 
14999 static void commit_pipe_config(struct intel_atomic_state *state,
15000 			       struct intel_crtc_state *old_crtc_state,
15001 			       struct intel_crtc_state *new_crtc_state)
15002 {
15003 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
15004 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15005 	bool modeset = needs_modeset(new_crtc_state);
15006 
15007 	/*
15008 	 * During modesets pipe configuration was programmed as the
15009 	 * CRTC was enabled.
15010 	 */
15011 	if (!modeset) {
15012 		if (new_crtc_state->uapi.color_mgmt_changed ||
15013 		    new_crtc_state->update_pipe)
15014 			intel_color_commit(new_crtc_state);
15015 
15016 		if (INTEL_GEN(dev_priv) >= 9)
15017 			skl_detach_scalers(new_crtc_state);
15018 
15019 		if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15020 			bdw_set_pipemisc(new_crtc_state);
15021 
15022 		if (new_crtc_state->update_pipe)
15023 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
15024 	}
15025 
15026 	if (dev_priv->display.atomic_update_watermarks)
15027 		dev_priv->display.atomic_update_watermarks(state, crtc);
15028 }
15029 
15030 static void intel_update_crtc(struct intel_crtc *crtc,
15031 			      struct intel_atomic_state *state,
15032 			      struct intel_crtc_state *old_crtc_state,
15033 			      struct intel_crtc_state *new_crtc_state)
15034 {
15035 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15036 	bool modeset = needs_modeset(new_crtc_state);
15037 
15038 	if (modeset) {
15039 		intel_crtc_update_active_timings(new_crtc_state);
15040 
15041 		dev_priv->display.crtc_enable(state, crtc);
15042 
15043 		/* vblanks work again, re-enable pipe CRC. */
15044 		intel_crtc_enable_pipe_crc(crtc);
15045 	} else {
15046 		if (new_crtc_state->preload_luts &&
15047 		    (new_crtc_state->uapi.color_mgmt_changed ||
15048 		     new_crtc_state->update_pipe))
15049 			intel_color_load_luts(new_crtc_state);
15050 
15051 		intel_pre_plane_update(state, crtc);
15052 
15053 		if (new_crtc_state->update_pipe)
15054 			intel_encoders_update_pipe(state, crtc);
15055 	}
15056 
15057 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15058 		intel_fbc_disable(crtc);
15059 	else
15060 		intel_fbc_enable(state, crtc);
15061 
15062 	/* Perform vblank evasion around commit operation */
15063 	intel_pipe_update_start(new_crtc_state);
15064 
15065 	commit_pipe_config(state, old_crtc_state, new_crtc_state);
15066 
15067 	if (INTEL_GEN(dev_priv) >= 9)
15068 		skl_update_planes_on_crtc(state, crtc);
15069 	else
15070 		i9xx_update_planes_on_crtc(state, crtc);
15071 
15072 	intel_pipe_update_end(new_crtc_state);
15073 
15074 	/*
15075 	 * We usually enable FIFO underrun interrupts as part of the
15076 	 * CRTC enable sequence during modesets.  But when we inherit a
15077 	 * valid pipe configuration from the BIOS we need to take care
15078 	 * of enabling them on the CRTC's first fastset.
15079 	 */
15080 	if (new_crtc_state->update_pipe && !modeset &&
15081 	    old_crtc_state->hw.mode.private_flags & I915_MODE_FLAG_INHERITED)
15082 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15083 }
15084 
15085 static struct intel_crtc *intel_get_slave_crtc(const struct intel_crtc_state *new_crtc_state)
15086 {
15087 	struct drm_i915_private *dev_priv = to_i915(new_crtc_state->uapi.crtc->dev);
15088 	enum transcoder slave_transcoder;
15089 
15090 	drm_WARN_ON(&dev_priv->drm,
15091 		    !is_power_of_2(new_crtc_state->sync_mode_slaves_mask));
15092 
15093 	slave_transcoder = ffs(new_crtc_state->sync_mode_slaves_mask) - 1;
15094 	return intel_get_crtc_for_pipe(dev_priv,
15095 				       (enum pipe)slave_transcoder);
15096 }
15097 
15098 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15099 					  struct intel_crtc_state *old_crtc_state,
15100 					  struct intel_crtc_state *new_crtc_state,
15101 					  struct intel_crtc *crtc)
15102 {
15103 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15104 
15105 	intel_crtc_disable_planes(state, crtc);
15106 
15107 	/*
15108 	 * We need to disable pipe CRC before disabling the pipe,
15109 	 * or we race against vblank off.
15110 	 */
15111 	intel_crtc_disable_pipe_crc(crtc);
15112 
15113 	dev_priv->display.crtc_disable(state, crtc);
15114 	crtc->active = false;
15115 	intel_fbc_disable(crtc);
15116 	intel_disable_shared_dpll(old_crtc_state);
15117 
15118 	/* FIXME unify this for all platforms */
15119 	if (!new_crtc_state->hw.active &&
15120 	    !HAS_GMCH(dev_priv) &&
15121 	    dev_priv->display.initial_watermarks)
15122 		dev_priv->display.initial_watermarks(state, crtc);
15123 }
15124 
15125 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15126 {
15127 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15128 	struct intel_crtc *crtc;
15129 	u32 handled = 0;
15130 	int i;
15131 
15132 	/* Only disable port sync and MST slaves */
15133 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15134 					    new_crtc_state, i) {
15135 		if (!needs_modeset(new_crtc_state))
15136 			continue;
15137 
15138 		if (!old_crtc_state->hw.active)
15139 			continue;
15140 
15141 		/* In case of Transcoder port Sync master slave CRTCs can be
15142 		 * assigned in any order and we need to make sure that
15143 		 * slave CRTCs are disabled first and then master CRTC since
15144 		 * Slave vblanks are masked till Master Vblanks.
15145 		 */
15146 		if (!is_trans_port_sync_slave(old_crtc_state) &&
15147 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
15148 			continue;
15149 
15150 		intel_pre_plane_update(state, crtc);
15151 		intel_old_crtc_state_disables(state, old_crtc_state,
15152 					      new_crtc_state, crtc);
15153 		handled |= BIT(crtc->pipe);
15154 	}
15155 
15156 	/* Disable everything else left on */
15157 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15158 					    new_crtc_state, i) {
15159 		if (!needs_modeset(new_crtc_state) ||
15160 		    (handled & BIT(crtc->pipe)))
15161 			continue;
15162 
15163 		intel_pre_plane_update(state, crtc);
15164 		if (old_crtc_state->hw.active)
15165 			intel_old_crtc_state_disables(state, old_crtc_state,
15166 						      new_crtc_state, crtc);
15167 	}
15168 }
15169 
15170 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
15171 {
15172 	struct intel_crtc *crtc;
15173 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15174 	int i;
15175 
15176 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15177 		if (!new_crtc_state->hw.active)
15178 			continue;
15179 
15180 		intel_update_crtc(crtc, state, old_crtc_state,
15181 				  new_crtc_state);
15182 	}
15183 }
15184 
15185 static void intel_crtc_enable_trans_port_sync(struct intel_crtc *crtc,
15186 					      struct intel_atomic_state *state,
15187 					      struct intel_crtc_state *new_crtc_state)
15188 {
15189 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15190 
15191 	intel_crtc_update_active_timings(new_crtc_state);
15192 	dev_priv->display.crtc_enable(state, crtc);
15193 	intel_crtc_enable_pipe_crc(crtc);
15194 }
15195 
15196 static void intel_set_dp_tp_ctl_normal(struct intel_crtc *crtc,
15197 				       struct intel_atomic_state *state)
15198 {
15199 	struct drm_connector *uninitialized_var(conn);
15200 	struct drm_connector_state *conn_state;
15201 	struct intel_dp *intel_dp;
15202 	int i;
15203 
15204 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
15205 		if (conn_state->crtc == &crtc->base)
15206 			break;
15207 	}
15208 	intel_dp = intel_attached_dp(to_intel_connector(conn));
15209 	intel_dp_stop_link_train(intel_dp);
15210 }
15211 
15212 /*
15213  * TODO: This is only called from port sync and it is identical to what will be
15214  * executed again in intel_update_crtc() over port sync pipes
15215  */
15216 static void intel_post_crtc_enable_updates(struct intel_crtc *crtc,
15217 					   struct intel_atomic_state *state)
15218 {
15219 	struct intel_crtc_state *new_crtc_state =
15220 		intel_atomic_get_new_crtc_state(state, crtc);
15221 	struct intel_crtc_state *old_crtc_state =
15222 		intel_atomic_get_old_crtc_state(state, crtc);
15223 	bool modeset = needs_modeset(new_crtc_state);
15224 
15225 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15226 		intel_fbc_disable(crtc);
15227 	else
15228 		intel_fbc_enable(state, crtc);
15229 
15230 	/* Perform vblank evasion around commit operation */
15231 	intel_pipe_update_start(new_crtc_state);
15232 	commit_pipe_config(state, old_crtc_state, new_crtc_state);
15233 	skl_update_planes_on_crtc(state, crtc);
15234 	intel_pipe_update_end(new_crtc_state);
15235 
15236 	/*
15237 	 * We usually enable FIFO underrun interrupts as part of the
15238 	 * CRTC enable sequence during modesets.  But when we inherit a
15239 	 * valid pipe configuration from the BIOS we need to take care
15240 	 * of enabling them on the CRTC's first fastset.
15241 	 */
15242 	if (new_crtc_state->update_pipe && !modeset &&
15243 	    old_crtc_state->hw.mode.private_flags & I915_MODE_FLAG_INHERITED)
15244 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15245 }
15246 
15247 static void intel_update_trans_port_sync_crtcs(struct intel_crtc *crtc,
15248 					       struct intel_atomic_state *state,
15249 					       struct intel_crtc_state *old_crtc_state,
15250 					       struct intel_crtc_state *new_crtc_state)
15251 {
15252 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
15253 	struct intel_crtc *slave_crtc = intel_get_slave_crtc(new_crtc_state);
15254 	struct intel_crtc_state *new_slave_crtc_state =
15255 		intel_atomic_get_new_crtc_state(state, slave_crtc);
15256 	struct intel_crtc_state *old_slave_crtc_state =
15257 		intel_atomic_get_old_crtc_state(state, slave_crtc);
15258 
15259 	drm_WARN_ON(&i915->drm, !slave_crtc || !new_slave_crtc_state ||
15260 		    !old_slave_crtc_state);
15261 
15262 	drm_dbg_kms(&i915->drm,
15263 		    "Updating Transcoder Port Sync Master CRTC = %d %s and Slave CRTC %d %s\n",
15264 		    crtc->base.base.id, crtc->base.name,
15265 		    slave_crtc->base.base.id, slave_crtc->base.name);
15266 
15267 	/* Enable seq for slave with with DP_TP_CTL left Idle until the
15268 	 * master is ready
15269 	 */
15270 	intel_crtc_enable_trans_port_sync(slave_crtc,
15271 					  state,
15272 					  new_slave_crtc_state);
15273 
15274 	/* Enable seq for master with with DP_TP_CTL left Idle */
15275 	intel_crtc_enable_trans_port_sync(crtc,
15276 					  state,
15277 					  new_crtc_state);
15278 
15279 	/* Set Slave's DP_TP_CTL to Normal */
15280 	intel_set_dp_tp_ctl_normal(slave_crtc,
15281 				   state);
15282 
15283 	/* Set Master's DP_TP_CTL To Normal */
15284 	usleep_range(200, 400);
15285 	intel_set_dp_tp_ctl_normal(crtc,
15286 				   state);
15287 
15288 	/* Now do the post crtc enable for all master and slaves */
15289 	intel_post_crtc_enable_updates(slave_crtc,
15290 				       state);
15291 	intel_post_crtc_enable_updates(crtc,
15292 				       state);
15293 }
15294 
15295 static void icl_dbuf_slice_pre_update(struct intel_atomic_state *state)
15296 {
15297 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15298 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15299 	u8 required_slices = state->enabled_dbuf_slices_mask;
15300 	u8 slices_union = hw_enabled_slices | required_slices;
15301 
15302 	/* If 2nd DBuf slice required, enable it here */
15303 	if (INTEL_GEN(dev_priv) >= 11 && slices_union != hw_enabled_slices)
15304 		icl_dbuf_slices_update(dev_priv, slices_union);
15305 }
15306 
15307 static void icl_dbuf_slice_post_update(struct intel_atomic_state *state)
15308 {
15309 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15310 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15311 	u8 required_slices = state->enabled_dbuf_slices_mask;
15312 
15313 	/* If 2nd DBuf slice is no more required disable it */
15314 	if (INTEL_GEN(dev_priv) >= 11 && required_slices != hw_enabled_slices)
15315 		icl_dbuf_slices_update(dev_priv, required_slices);
15316 }
15317 
15318 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
15319 {
15320 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15321 	struct intel_crtc *crtc;
15322 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15323 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
15324 	u8 update_pipes = 0, modeset_pipes = 0;
15325 	int i;
15326 
15327 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15328 		enum pipe pipe = crtc->pipe;
15329 
15330 		if (!new_crtc_state->hw.active)
15331 			continue;
15332 
15333 		/* ignore allocations for crtc's that have been turned off. */
15334 		if (!needs_modeset(new_crtc_state)) {
15335 			entries[pipe] = old_crtc_state->wm.skl.ddb;
15336 			update_pipes |= BIT(pipe);
15337 		} else {
15338 			modeset_pipes |= BIT(pipe);
15339 		}
15340 	}
15341 
15342 	/*
15343 	 * Whenever the number of active pipes changes, we need to make sure we
15344 	 * update the pipes in the right order so that their ddb allocations
15345 	 * never overlap with each other between CRTC updates. Otherwise we'll
15346 	 * cause pipe underruns and other bad stuff.
15347 	 *
15348 	 * So first lets enable all pipes that do not need a fullmodeset as
15349 	 * those don't have any external dependency.
15350 	 */
15351 	while (update_pipes) {
15352 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15353 						    new_crtc_state, i) {
15354 			enum pipe pipe = crtc->pipe;
15355 
15356 			if ((update_pipes & BIT(pipe)) == 0)
15357 				continue;
15358 
15359 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15360 							entries, I915_MAX_PIPES, pipe))
15361 				continue;
15362 
15363 			entries[pipe] = new_crtc_state->wm.skl.ddb;
15364 			update_pipes &= ~BIT(pipe);
15365 
15366 			intel_update_crtc(crtc, state, old_crtc_state,
15367 					  new_crtc_state);
15368 
15369 			/*
15370 			 * If this is an already active pipe, it's DDB changed,
15371 			 * and this isn't the last pipe that needs updating
15372 			 * then we need to wait for a vblank to pass for the
15373 			 * new ddb allocation to take effect.
15374 			 */
15375 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
15376 						 &old_crtc_state->wm.skl.ddb) &&
15377 			    (update_pipes | modeset_pipes))
15378 				intel_wait_for_vblank(dev_priv, pipe);
15379 		}
15380 	}
15381 
15382 	/*
15383 	 * Enable all pipes that needs a modeset and do not depends on other
15384 	 * pipes
15385 	 */
15386 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15387 					    new_crtc_state, i) {
15388 		enum pipe pipe = crtc->pipe;
15389 
15390 		if ((modeset_pipes & BIT(pipe)) == 0)
15391 			continue;
15392 
15393 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
15394 		    is_trans_port_sync_slave(new_crtc_state))
15395 			continue;
15396 
15397 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15398 									entries, I915_MAX_PIPES, pipe));
15399 
15400 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15401 		modeset_pipes &= ~BIT(pipe);
15402 
15403 		if (is_trans_port_sync_mode(new_crtc_state)) {
15404 			struct intel_crtc *slave_crtc;
15405 
15406 			intel_update_trans_port_sync_crtcs(crtc, state,
15407 							   old_crtc_state,
15408 							   new_crtc_state);
15409 
15410 			slave_crtc = intel_get_slave_crtc(new_crtc_state);
15411 			/* TODO: update entries[] of slave */
15412 			modeset_pipes &= ~BIT(slave_crtc->pipe);
15413 
15414 		} else {
15415 			intel_update_crtc(crtc, state, old_crtc_state,
15416 					  new_crtc_state);
15417 		}
15418 	}
15419 
15420 	/*
15421 	 * Finally enable all pipes that needs a modeset and depends on
15422 	 * other pipes, right now it is only MST slaves as both port sync slave
15423 	 * and master are enabled together
15424 	 */
15425 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15426 					    new_crtc_state, i) {
15427 		enum pipe pipe = crtc->pipe;
15428 
15429 		if ((modeset_pipes & BIT(pipe)) == 0)
15430 			continue;
15431 
15432 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15433 									entries, I915_MAX_PIPES, pipe));
15434 
15435 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15436 		modeset_pipes &= ~BIT(pipe);
15437 
15438 		intel_update_crtc(crtc, state, old_crtc_state, new_crtc_state);
15439 	}
15440 
15441 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
15442 
15443 }
15444 
15445 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
15446 {
15447 	struct intel_atomic_state *state, *next;
15448 	struct llist_node *freed;
15449 
15450 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
15451 	llist_for_each_entry_safe(state, next, freed, freed)
15452 		drm_atomic_state_put(&state->base);
15453 }
15454 
15455 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
15456 {
15457 	struct drm_i915_private *dev_priv =
15458 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
15459 
15460 	intel_atomic_helper_free_state(dev_priv);
15461 }
15462 
15463 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
15464 {
15465 	struct wait_queue_entry wait_fence, wait_reset;
15466 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
15467 
15468 	init_wait_entry(&wait_fence, 0);
15469 	init_wait_entry(&wait_reset, 0);
15470 	for (;;) {
15471 		prepare_to_wait(&intel_state->commit_ready.wait,
15472 				&wait_fence, TASK_UNINTERRUPTIBLE);
15473 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15474 					      I915_RESET_MODESET),
15475 				&wait_reset, TASK_UNINTERRUPTIBLE);
15476 
15477 
15478 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
15479 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
15480 			break;
15481 
15482 		schedule();
15483 	}
15484 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
15485 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15486 				  I915_RESET_MODESET),
15487 		    &wait_reset);
15488 }
15489 
15490 static void intel_atomic_cleanup_work(struct work_struct *work)
15491 {
15492 	struct drm_atomic_state *state =
15493 		container_of(work, struct drm_atomic_state, commit_work);
15494 	struct drm_i915_private *i915 = to_i915(state->dev);
15495 
15496 	drm_atomic_helper_cleanup_planes(&i915->drm, state);
15497 	drm_atomic_helper_commit_cleanup_done(state);
15498 	drm_atomic_state_put(state);
15499 
15500 	intel_atomic_helper_free_state(i915);
15501 }
15502 
15503 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
15504 {
15505 	struct drm_device *dev = state->base.dev;
15506 	struct drm_i915_private *dev_priv = to_i915(dev);
15507 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15508 	struct intel_crtc *crtc;
15509 	u64 put_domains[I915_MAX_PIPES] = {};
15510 	intel_wakeref_t wakeref = 0;
15511 	int i;
15512 
15513 	intel_atomic_commit_fence_wait(state);
15514 
15515 	drm_atomic_helper_wait_for_dependencies(&state->base);
15516 
15517 	if (state->modeset)
15518 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
15519 
15520 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15521 					    new_crtc_state, i) {
15522 		if (needs_modeset(new_crtc_state) ||
15523 		    new_crtc_state->update_pipe) {
15524 
15525 			put_domains[crtc->pipe] =
15526 				modeset_get_crtc_power_domains(new_crtc_state);
15527 		}
15528 	}
15529 
15530 	intel_commit_modeset_disables(state);
15531 
15532 	/* FIXME: Eventually get rid of our crtc->config pointer */
15533 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15534 		crtc->config = new_crtc_state;
15535 
15536 	if (state->modeset) {
15537 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
15538 
15539 		intel_set_cdclk_pre_plane_update(state);
15540 
15541 		/*
15542 		 * SKL workaround: bspec recommends we disable the SAGV when we
15543 		 * have more then one pipe enabled
15544 		 */
15545 		if (!intel_can_enable_sagv(state))
15546 			intel_disable_sagv(dev_priv);
15547 
15548 		intel_modeset_verify_disabled(dev_priv, state);
15549 	}
15550 
15551 	/* Complete the events for pipes that have now been disabled */
15552 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15553 		bool modeset = needs_modeset(new_crtc_state);
15554 
15555 		/* Complete events for now disable pipes here. */
15556 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
15557 			spin_lock_irq(&dev->event_lock);
15558 			drm_crtc_send_vblank_event(&crtc->base,
15559 						   new_crtc_state->uapi.event);
15560 			spin_unlock_irq(&dev->event_lock);
15561 
15562 			new_crtc_state->uapi.event = NULL;
15563 		}
15564 	}
15565 
15566 	if (state->modeset)
15567 		intel_encoders_update_prepare(state);
15568 
15569 	/* Enable all new slices, we might need */
15570 	if (state->modeset)
15571 		icl_dbuf_slice_pre_update(state);
15572 
15573 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
15574 	dev_priv->display.commit_modeset_enables(state);
15575 
15576 	if (state->modeset) {
15577 		intel_encoders_update_complete(state);
15578 
15579 		intel_set_cdclk_post_plane_update(state);
15580 	}
15581 
15582 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
15583 	 * already, but still need the state for the delayed optimization. To
15584 	 * fix this:
15585 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
15586 	 * - schedule that vblank worker _before_ calling hw_done
15587 	 * - at the start of commit_tail, cancel it _synchrously
15588 	 * - switch over to the vblank wait helper in the core after that since
15589 	 *   we don't need out special handling any more.
15590 	 */
15591 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
15592 
15593 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15594 		if (new_crtc_state->hw.active &&
15595 		    !needs_modeset(new_crtc_state) &&
15596 		    !new_crtc_state->preload_luts &&
15597 		    (new_crtc_state->uapi.color_mgmt_changed ||
15598 		     new_crtc_state->update_pipe))
15599 			intel_color_load_luts(new_crtc_state);
15600 	}
15601 
15602 	/*
15603 	 * Now that the vblank has passed, we can go ahead and program the
15604 	 * optimal watermarks on platforms that need two-step watermark
15605 	 * programming.
15606 	 *
15607 	 * TODO: Move this (and other cleanup) to an async worker eventually.
15608 	 */
15609 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15610 					    new_crtc_state, i) {
15611 		/*
15612 		 * Gen2 reports pipe underruns whenever all planes are disabled.
15613 		 * So re-enable underrun reporting after some planes get enabled.
15614 		 *
15615 		 * We do this before .optimize_watermarks() so that we have a
15616 		 * chance of catching underruns with the intermediate watermarks
15617 		 * vs. the new plane configuration.
15618 		 */
15619 		if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
15620 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15621 
15622 		if (dev_priv->display.optimize_watermarks)
15623 			dev_priv->display.optimize_watermarks(state, crtc);
15624 	}
15625 
15626 	/* Disable all slices, we don't need */
15627 	if (state->modeset)
15628 		icl_dbuf_slice_post_update(state);
15629 
15630 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15631 		intel_post_plane_update(state, crtc);
15632 
15633 		if (put_domains[i])
15634 			modeset_put_power_domains(dev_priv, put_domains[i]);
15635 
15636 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
15637 	}
15638 
15639 	/* Underruns don't always raise interrupts, so check manually */
15640 	intel_check_cpu_fifo_underruns(dev_priv);
15641 	intel_check_pch_fifo_underruns(dev_priv);
15642 
15643 	if (state->modeset)
15644 		intel_verify_planes(state);
15645 
15646 	if (state->modeset && intel_can_enable_sagv(state))
15647 		intel_enable_sagv(dev_priv);
15648 
15649 	drm_atomic_helper_commit_hw_done(&state->base);
15650 
15651 	if (state->modeset) {
15652 		/* As one of the primary mmio accessors, KMS has a high
15653 		 * likelihood of triggering bugs in unclaimed access. After we
15654 		 * finish modesetting, see if an error has been flagged, and if
15655 		 * so enable debugging for the next modeset - and hope we catch
15656 		 * the culprit.
15657 		 */
15658 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
15659 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
15660 	}
15661 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15662 
15663 	/*
15664 	 * Defer the cleanup of the old state to a separate worker to not
15665 	 * impede the current task (userspace for blocking modesets) that
15666 	 * are executed inline. For out-of-line asynchronous modesets/flips,
15667 	 * deferring to a new worker seems overkill, but we would place a
15668 	 * schedule point (cond_resched()) here anyway to keep latencies
15669 	 * down.
15670 	 */
15671 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
15672 	queue_work(system_highpri_wq, &state->base.commit_work);
15673 }
15674 
15675 static void intel_atomic_commit_work(struct work_struct *work)
15676 {
15677 	struct intel_atomic_state *state =
15678 		container_of(work, struct intel_atomic_state, base.commit_work);
15679 
15680 	intel_atomic_commit_tail(state);
15681 }
15682 
15683 static int __i915_sw_fence_call
15684 intel_atomic_commit_ready(struct i915_sw_fence *fence,
15685 			  enum i915_sw_fence_notify notify)
15686 {
15687 	struct intel_atomic_state *state =
15688 		container_of(fence, struct intel_atomic_state, commit_ready);
15689 
15690 	switch (notify) {
15691 	case FENCE_COMPLETE:
15692 		/* we do blocking waits in the worker, nothing to do here */
15693 		break;
15694 	case FENCE_FREE:
15695 		{
15696 			struct intel_atomic_helper *helper =
15697 				&to_i915(state->base.dev)->atomic_helper;
15698 
15699 			if (llist_add(&state->freed, &helper->free_list))
15700 				schedule_work(&helper->free_work);
15701 			break;
15702 		}
15703 	}
15704 
15705 	return NOTIFY_DONE;
15706 }
15707 
15708 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
15709 {
15710 	struct intel_plane_state *old_plane_state, *new_plane_state;
15711 	struct intel_plane *plane;
15712 	int i;
15713 
15714 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15715 					     new_plane_state, i)
15716 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
15717 					to_intel_frontbuffer(new_plane_state->hw.fb),
15718 					plane->frontbuffer_bit);
15719 }
15720 
15721 static void assert_global_state_locked(struct drm_i915_private *dev_priv)
15722 {
15723 	struct intel_crtc *crtc;
15724 
15725 	for_each_intel_crtc(&dev_priv->drm, crtc)
15726 		drm_modeset_lock_assert_held(&crtc->base.mutex);
15727 }
15728 
15729 static int intel_atomic_commit(struct drm_device *dev,
15730 			       struct drm_atomic_state *_state,
15731 			       bool nonblock)
15732 {
15733 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
15734 	struct drm_i915_private *dev_priv = to_i915(dev);
15735 	int ret = 0;
15736 
15737 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
15738 
15739 	drm_atomic_state_get(&state->base);
15740 	i915_sw_fence_init(&state->commit_ready,
15741 			   intel_atomic_commit_ready);
15742 
15743 	/*
15744 	 * The intel_legacy_cursor_update() fast path takes care
15745 	 * of avoiding the vblank waits for simple cursor
15746 	 * movement and flips. For cursor on/off and size changes,
15747 	 * we want to perform the vblank waits so that watermark
15748 	 * updates happen during the correct frames. Gen9+ have
15749 	 * double buffered watermarks and so shouldn't need this.
15750 	 *
15751 	 * Unset state->legacy_cursor_update before the call to
15752 	 * drm_atomic_helper_setup_commit() because otherwise
15753 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
15754 	 * we get FIFO underruns because we didn't wait
15755 	 * for vblank.
15756 	 *
15757 	 * FIXME doing watermarks and fb cleanup from a vblank worker
15758 	 * (assuming we had any) would solve these problems.
15759 	 */
15760 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
15761 		struct intel_crtc_state *new_crtc_state;
15762 		struct intel_crtc *crtc;
15763 		int i;
15764 
15765 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15766 			if (new_crtc_state->wm.need_postvbl_update ||
15767 			    new_crtc_state->update_wm_post)
15768 				state->base.legacy_cursor_update = false;
15769 	}
15770 
15771 	ret = intel_atomic_prepare_commit(state);
15772 	if (ret) {
15773 		drm_dbg_atomic(&dev_priv->drm,
15774 			       "Preparing state failed with %i\n", ret);
15775 		i915_sw_fence_commit(&state->commit_ready);
15776 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15777 		return ret;
15778 	}
15779 
15780 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
15781 	if (!ret)
15782 		ret = drm_atomic_helper_swap_state(&state->base, true);
15783 	if (!ret)
15784 		intel_atomic_swap_global_state(state);
15785 
15786 	if (ret) {
15787 		i915_sw_fence_commit(&state->commit_ready);
15788 
15789 		drm_atomic_helper_cleanup_planes(dev, &state->base);
15790 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15791 		return ret;
15792 	}
15793 	dev_priv->wm.distrust_bios_wm = false;
15794 	intel_shared_dpll_swap_state(state);
15795 	intel_atomic_track_fbs(state);
15796 
15797 	if (state->global_state_changed) {
15798 		assert_global_state_locked(dev_priv);
15799 
15800 		dev_priv->active_pipes = state->active_pipes;
15801 	}
15802 
15803 	drm_atomic_state_get(&state->base);
15804 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
15805 
15806 	i915_sw_fence_commit(&state->commit_ready);
15807 	if (nonblock && state->modeset) {
15808 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
15809 	} else if (nonblock) {
15810 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
15811 	} else {
15812 		if (state->modeset)
15813 			flush_workqueue(dev_priv->modeset_wq);
15814 		intel_atomic_commit_tail(state);
15815 	}
15816 
15817 	return 0;
15818 }
15819 
15820 struct wait_rps_boost {
15821 	struct wait_queue_entry wait;
15822 
15823 	struct drm_crtc *crtc;
15824 	struct i915_request *request;
15825 };
15826 
15827 static int do_rps_boost(struct wait_queue_entry *_wait,
15828 			unsigned mode, int sync, void *key)
15829 {
15830 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
15831 	struct i915_request *rq = wait->request;
15832 
15833 	/*
15834 	 * If we missed the vblank, but the request is already running it
15835 	 * is reasonable to assume that it will complete before the next
15836 	 * vblank without our intervention, so leave RPS alone.
15837 	 */
15838 	if (!i915_request_started(rq))
15839 		intel_rps_boost(rq);
15840 	i915_request_put(rq);
15841 
15842 	drm_crtc_vblank_put(wait->crtc);
15843 
15844 	list_del(&wait->wait.entry);
15845 	kfree(wait);
15846 	return 1;
15847 }
15848 
15849 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
15850 				       struct dma_fence *fence)
15851 {
15852 	struct wait_rps_boost *wait;
15853 
15854 	if (!dma_fence_is_i915(fence))
15855 		return;
15856 
15857 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
15858 		return;
15859 
15860 	if (drm_crtc_vblank_get(crtc))
15861 		return;
15862 
15863 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
15864 	if (!wait) {
15865 		drm_crtc_vblank_put(crtc);
15866 		return;
15867 	}
15868 
15869 	wait->request = to_request(dma_fence_get(fence));
15870 	wait->crtc = crtc;
15871 
15872 	wait->wait.func = do_rps_boost;
15873 	wait->wait.flags = 0;
15874 
15875 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
15876 }
15877 
15878 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
15879 {
15880 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
15881 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15882 	struct drm_framebuffer *fb = plane_state->hw.fb;
15883 	struct i915_vma *vma;
15884 
15885 	if (plane->id == PLANE_CURSOR &&
15886 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
15887 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
15888 		const int align = intel_cursor_alignment(dev_priv);
15889 		int err;
15890 
15891 		err = i915_gem_object_attach_phys(obj, align);
15892 		if (err)
15893 			return err;
15894 	}
15895 
15896 	vma = intel_pin_and_fence_fb_obj(fb,
15897 					 &plane_state->view,
15898 					 intel_plane_uses_fence(plane_state),
15899 					 &plane_state->flags);
15900 	if (IS_ERR(vma))
15901 		return PTR_ERR(vma);
15902 
15903 	plane_state->vma = vma;
15904 
15905 	return 0;
15906 }
15907 
15908 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
15909 {
15910 	struct i915_vma *vma;
15911 
15912 	vma = fetch_and_zero(&old_plane_state->vma);
15913 	if (vma)
15914 		intel_unpin_fb_vma(vma, old_plane_state->flags);
15915 }
15916 
15917 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
15918 {
15919 	struct i915_sched_attr attr = {
15920 		.priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
15921 	};
15922 
15923 	i915_gem_object_wait_priority(obj, 0, &attr);
15924 }
15925 
15926 /**
15927  * intel_prepare_plane_fb - Prepare fb for usage on plane
15928  * @_plane: drm plane to prepare for
15929  * @_new_plane_state: the plane state being prepared
15930  *
15931  * Prepares a framebuffer for usage on a display plane.  Generally this
15932  * involves pinning the underlying object and updating the frontbuffer tracking
15933  * bits.  Some older platforms need special physical address handling for
15934  * cursor planes.
15935  *
15936  * Returns 0 on success, negative error code on failure.
15937  */
15938 int
15939 intel_prepare_plane_fb(struct drm_plane *_plane,
15940 		       struct drm_plane_state *_new_plane_state)
15941 {
15942 	struct intel_plane *plane = to_intel_plane(_plane);
15943 	struct intel_plane_state *new_plane_state =
15944 		to_intel_plane_state(_new_plane_state);
15945 	struct intel_atomic_state *state =
15946 		to_intel_atomic_state(new_plane_state->uapi.state);
15947 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15948 	const struct intel_plane_state *old_plane_state =
15949 		intel_atomic_get_old_plane_state(state, plane);
15950 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
15951 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
15952 	int ret;
15953 
15954 	if (old_obj) {
15955 		const struct intel_crtc_state *crtc_state =
15956 			intel_atomic_get_new_crtc_state(state,
15957 							to_intel_crtc(old_plane_state->hw.crtc));
15958 
15959 		/* Big Hammer, we also need to ensure that any pending
15960 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
15961 		 * current scanout is retired before unpinning the old
15962 		 * framebuffer. Note that we rely on userspace rendering
15963 		 * into the buffer attached to the pipe they are waiting
15964 		 * on. If not, userspace generates a GPU hang with IPEHR
15965 		 * point to the MI_WAIT_FOR_EVENT.
15966 		 *
15967 		 * This should only fail upon a hung GPU, in which case we
15968 		 * can safely continue.
15969 		 */
15970 		if (needs_modeset(crtc_state)) {
15971 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
15972 							      old_obj->base.resv, NULL,
15973 							      false, 0,
15974 							      GFP_KERNEL);
15975 			if (ret < 0)
15976 				return ret;
15977 		}
15978 	}
15979 
15980 	if (new_plane_state->uapi.fence) { /* explicit fencing */
15981 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
15982 						    new_plane_state->uapi.fence,
15983 						    I915_FENCE_TIMEOUT,
15984 						    GFP_KERNEL);
15985 		if (ret < 0)
15986 			return ret;
15987 	}
15988 
15989 	if (!obj)
15990 		return 0;
15991 
15992 	ret = i915_gem_object_pin_pages(obj);
15993 	if (ret)
15994 		return ret;
15995 
15996 	ret = intel_plane_pin_fb(new_plane_state);
15997 
15998 	i915_gem_object_unpin_pages(obj);
15999 	if (ret)
16000 		return ret;
16001 
16002 	fb_obj_bump_render_priority(obj);
16003 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
16004 
16005 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
16006 		struct dma_fence *fence;
16007 
16008 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
16009 						      obj->base.resv, NULL,
16010 						      false, I915_FENCE_TIMEOUT,
16011 						      GFP_KERNEL);
16012 		if (ret < 0)
16013 			goto unpin_fb;
16014 
16015 		fence = dma_resv_get_excl_rcu(obj->base.resv);
16016 		if (fence) {
16017 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16018 						   fence);
16019 			dma_fence_put(fence);
16020 		}
16021 	} else {
16022 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16023 					   new_plane_state->uapi.fence);
16024 	}
16025 
16026 	/*
16027 	 * We declare pageflips to be interactive and so merit a small bias
16028 	 * towards upclocking to deliver the frame on time. By only changing
16029 	 * the RPS thresholds to sample more regularly and aim for higher
16030 	 * clocks we can hopefully deliver low power workloads (like kodi)
16031 	 * that are not quite steady state without resorting to forcing
16032 	 * maximum clocks following a vblank miss (see do_rps_boost()).
16033 	 */
16034 	if (!state->rps_interactive) {
16035 		intel_rps_mark_interactive(&dev_priv->gt.rps, true);
16036 		state->rps_interactive = true;
16037 	}
16038 
16039 	return 0;
16040 
16041 unpin_fb:
16042 	intel_plane_unpin_fb(new_plane_state);
16043 
16044 	return ret;
16045 }
16046 
16047 /**
16048  * intel_cleanup_plane_fb - Cleans up an fb after plane use
16049  * @plane: drm plane to clean up for
16050  * @_old_plane_state: the state from the previous modeset
16051  *
16052  * Cleans up a framebuffer that has just been removed from a plane.
16053  */
16054 void
16055 intel_cleanup_plane_fb(struct drm_plane *plane,
16056 		       struct drm_plane_state *_old_plane_state)
16057 {
16058 	struct intel_plane_state *old_plane_state =
16059 		to_intel_plane_state(_old_plane_state);
16060 	struct intel_atomic_state *state =
16061 		to_intel_atomic_state(old_plane_state->uapi.state);
16062 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
16063 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16064 
16065 	if (!obj)
16066 		return;
16067 
16068 	if (state->rps_interactive) {
16069 		intel_rps_mark_interactive(&dev_priv->gt.rps, false);
16070 		state->rps_interactive = false;
16071 	}
16072 
16073 	/* Should only be called after a successful intel_prepare_plane_fb()! */
16074 	intel_plane_unpin_fb(old_plane_state);
16075 }
16076 
16077 /**
16078  * intel_plane_destroy - destroy a plane
16079  * @plane: plane to destroy
16080  *
16081  * Common destruction function for all types of planes (primary, cursor,
16082  * sprite).
16083  */
16084 void intel_plane_destroy(struct drm_plane *plane)
16085 {
16086 	drm_plane_cleanup(plane);
16087 	kfree(to_intel_plane(plane));
16088 }
16089 
16090 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16091 					    u32 format, u64 modifier)
16092 {
16093 	switch (modifier) {
16094 	case DRM_FORMAT_MOD_LINEAR:
16095 	case I915_FORMAT_MOD_X_TILED:
16096 		break;
16097 	default:
16098 		return false;
16099 	}
16100 
16101 	switch (format) {
16102 	case DRM_FORMAT_C8:
16103 	case DRM_FORMAT_RGB565:
16104 	case DRM_FORMAT_XRGB1555:
16105 	case DRM_FORMAT_XRGB8888:
16106 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16107 			modifier == I915_FORMAT_MOD_X_TILED;
16108 	default:
16109 		return false;
16110 	}
16111 }
16112 
16113 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16114 					    u32 format, u64 modifier)
16115 {
16116 	switch (modifier) {
16117 	case DRM_FORMAT_MOD_LINEAR:
16118 	case I915_FORMAT_MOD_X_TILED:
16119 		break;
16120 	default:
16121 		return false;
16122 	}
16123 
16124 	switch (format) {
16125 	case DRM_FORMAT_C8:
16126 	case DRM_FORMAT_RGB565:
16127 	case DRM_FORMAT_XRGB8888:
16128 	case DRM_FORMAT_XBGR8888:
16129 	case DRM_FORMAT_ARGB8888:
16130 	case DRM_FORMAT_ABGR8888:
16131 	case DRM_FORMAT_XRGB2101010:
16132 	case DRM_FORMAT_XBGR2101010:
16133 	case DRM_FORMAT_ARGB2101010:
16134 	case DRM_FORMAT_ABGR2101010:
16135 	case DRM_FORMAT_XBGR16161616F:
16136 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16137 			modifier == I915_FORMAT_MOD_X_TILED;
16138 	default:
16139 		return false;
16140 	}
16141 }
16142 
16143 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16144 					      u32 format, u64 modifier)
16145 {
16146 	return modifier == DRM_FORMAT_MOD_LINEAR &&
16147 		format == DRM_FORMAT_ARGB8888;
16148 }
16149 
16150 static const struct drm_plane_funcs i965_plane_funcs = {
16151 	.update_plane = drm_atomic_helper_update_plane,
16152 	.disable_plane = drm_atomic_helper_disable_plane,
16153 	.destroy = intel_plane_destroy,
16154 	.atomic_duplicate_state = intel_plane_duplicate_state,
16155 	.atomic_destroy_state = intel_plane_destroy_state,
16156 	.format_mod_supported = i965_plane_format_mod_supported,
16157 };
16158 
16159 static const struct drm_plane_funcs i8xx_plane_funcs = {
16160 	.update_plane = drm_atomic_helper_update_plane,
16161 	.disable_plane = drm_atomic_helper_disable_plane,
16162 	.destroy = intel_plane_destroy,
16163 	.atomic_duplicate_state = intel_plane_duplicate_state,
16164 	.atomic_destroy_state = intel_plane_destroy_state,
16165 	.format_mod_supported = i8xx_plane_format_mod_supported,
16166 };
16167 
16168 static int
16169 intel_legacy_cursor_update(struct drm_plane *_plane,
16170 			   struct drm_crtc *_crtc,
16171 			   struct drm_framebuffer *fb,
16172 			   int crtc_x, int crtc_y,
16173 			   unsigned int crtc_w, unsigned int crtc_h,
16174 			   u32 src_x, u32 src_y,
16175 			   u32 src_w, u32 src_h,
16176 			   struct drm_modeset_acquire_ctx *ctx)
16177 {
16178 	struct intel_plane *plane = to_intel_plane(_plane);
16179 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
16180 	struct intel_plane_state *old_plane_state =
16181 		to_intel_plane_state(plane->base.state);
16182 	struct intel_plane_state *new_plane_state;
16183 	struct intel_crtc_state *crtc_state =
16184 		to_intel_crtc_state(crtc->base.state);
16185 	struct intel_crtc_state *new_crtc_state;
16186 	int ret;
16187 
16188 	/*
16189 	 * When crtc is inactive or there is a modeset pending,
16190 	 * wait for it to complete in the slowpath
16191 	 */
16192 	if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
16193 	    crtc_state->update_pipe)
16194 		goto slow;
16195 
16196 	/*
16197 	 * Don't do an async update if there is an outstanding commit modifying
16198 	 * the plane.  This prevents our async update's changes from getting
16199 	 * overridden by a previous synchronous update's state.
16200 	 */
16201 	if (old_plane_state->uapi.commit &&
16202 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
16203 		goto slow;
16204 
16205 	/*
16206 	 * If any parameters change that may affect watermarks,
16207 	 * take the slowpath. Only changing fb or position should be
16208 	 * in the fastpath.
16209 	 */
16210 	if (old_plane_state->uapi.crtc != &crtc->base ||
16211 	    old_plane_state->uapi.src_w != src_w ||
16212 	    old_plane_state->uapi.src_h != src_h ||
16213 	    old_plane_state->uapi.crtc_w != crtc_w ||
16214 	    old_plane_state->uapi.crtc_h != crtc_h ||
16215 	    !old_plane_state->uapi.fb != !fb)
16216 		goto slow;
16217 
16218 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
16219 	if (!new_plane_state)
16220 		return -ENOMEM;
16221 
16222 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
16223 	if (!new_crtc_state) {
16224 		ret = -ENOMEM;
16225 		goto out_free;
16226 	}
16227 
16228 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
16229 
16230 	new_plane_state->uapi.src_x = src_x;
16231 	new_plane_state->uapi.src_y = src_y;
16232 	new_plane_state->uapi.src_w = src_w;
16233 	new_plane_state->uapi.src_h = src_h;
16234 	new_plane_state->uapi.crtc_x = crtc_x;
16235 	new_plane_state->uapi.crtc_y = crtc_y;
16236 	new_plane_state->uapi.crtc_w = crtc_w;
16237 	new_plane_state->uapi.crtc_h = crtc_h;
16238 
16239 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state);
16240 
16241 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
16242 						  old_plane_state, new_plane_state);
16243 	if (ret)
16244 		goto out_free;
16245 
16246 	ret = intel_plane_pin_fb(new_plane_state);
16247 	if (ret)
16248 		goto out_free;
16249 
16250 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16251 				ORIGIN_FLIP);
16252 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16253 				to_intel_frontbuffer(new_plane_state->hw.fb),
16254 				plane->frontbuffer_bit);
16255 
16256 	/* Swap plane state */
16257 	plane->base.state = &new_plane_state->uapi;
16258 
16259 	/*
16260 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
16261 	 * page flip that's running simultaneously. If we swap crtc_state and
16262 	 * destroy the old state, we will cause a use-after-free there.
16263 	 *
16264 	 * Only update active_planes, which is needed for our internal
16265 	 * bookkeeping. Either value will do the right thing when updating
16266 	 * planes atomically. If the cursor was part of the atomic update then
16267 	 * we would have taken the slowpath.
16268 	 */
16269 	crtc_state->active_planes = new_crtc_state->active_planes;
16270 
16271 	if (new_plane_state->uapi.visible)
16272 		intel_update_plane(plane, crtc_state, new_plane_state);
16273 	else
16274 		intel_disable_plane(plane, crtc_state);
16275 
16276 	intel_plane_unpin_fb(old_plane_state);
16277 
16278 out_free:
16279 	if (new_crtc_state)
16280 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
16281 	if (ret)
16282 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
16283 	else
16284 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
16285 	return ret;
16286 
16287 slow:
16288 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
16289 					      crtc_x, crtc_y, crtc_w, crtc_h,
16290 					      src_x, src_y, src_w, src_h, ctx);
16291 }
16292 
16293 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
16294 	.update_plane = intel_legacy_cursor_update,
16295 	.disable_plane = drm_atomic_helper_disable_plane,
16296 	.destroy = intel_plane_destroy,
16297 	.atomic_duplicate_state = intel_plane_duplicate_state,
16298 	.atomic_destroy_state = intel_plane_destroy_state,
16299 	.format_mod_supported = intel_cursor_format_mod_supported,
16300 };
16301 
16302 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
16303 			       enum i9xx_plane_id i9xx_plane)
16304 {
16305 	if (!HAS_FBC(dev_priv))
16306 		return false;
16307 
16308 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16309 		return i9xx_plane == PLANE_A; /* tied to pipe A */
16310 	else if (IS_IVYBRIDGE(dev_priv))
16311 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
16312 			i9xx_plane == PLANE_C;
16313 	else if (INTEL_GEN(dev_priv) >= 4)
16314 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
16315 	else
16316 		return i9xx_plane == PLANE_A;
16317 }
16318 
16319 static struct intel_plane *
16320 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
16321 {
16322 	struct intel_plane *plane;
16323 	const struct drm_plane_funcs *plane_funcs;
16324 	unsigned int supported_rotations;
16325 	const u32 *formats;
16326 	int num_formats;
16327 	int ret, zpos;
16328 
16329 	if (INTEL_GEN(dev_priv) >= 9)
16330 		return skl_universal_plane_create(dev_priv, pipe,
16331 						  PLANE_PRIMARY);
16332 
16333 	plane = intel_plane_alloc();
16334 	if (IS_ERR(plane))
16335 		return plane;
16336 
16337 	plane->pipe = pipe;
16338 	/*
16339 	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
16340 	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
16341 	 */
16342 	if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
16343 		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
16344 	else
16345 		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
16346 	plane->id = PLANE_PRIMARY;
16347 	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
16348 
16349 	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
16350 	if (plane->has_fbc) {
16351 		struct intel_fbc *fbc = &dev_priv->fbc;
16352 
16353 		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
16354 	}
16355 
16356 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16357 		formats = vlv_primary_formats;
16358 		num_formats = ARRAY_SIZE(vlv_primary_formats);
16359 	} else if (INTEL_GEN(dev_priv) >= 4) {
16360 		/*
16361 		 * WaFP16GammaEnabling:ivb
16362 		 * "Workaround : When using the 64-bit format, the plane
16363 		 *  output on each color channel has one quarter amplitude.
16364 		 *  It can be brought up to full amplitude by using pipe
16365 		 *  gamma correction or pipe color space conversion to
16366 		 *  multiply the plane output by four."
16367 		 *
16368 		 * There is no dedicated plane gamma for the primary plane,
16369 		 * and using the pipe gamma/csc could conflict with other
16370 		 * planes, so we choose not to expose fp16 on IVB primary
16371 		 * planes. HSW primary planes no longer have this problem.
16372 		 */
16373 		if (IS_IVYBRIDGE(dev_priv)) {
16374 			formats = ivb_primary_formats;
16375 			num_formats = ARRAY_SIZE(ivb_primary_formats);
16376 		} else {
16377 			formats = i965_primary_formats;
16378 			num_formats = ARRAY_SIZE(i965_primary_formats);
16379 		}
16380 	} else {
16381 		formats = i8xx_primary_formats;
16382 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
16383 	}
16384 
16385 	if (INTEL_GEN(dev_priv) >= 4)
16386 		plane_funcs = &i965_plane_funcs;
16387 	else
16388 		plane_funcs = &i8xx_plane_funcs;
16389 
16390 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16391 		plane->min_cdclk = vlv_plane_min_cdclk;
16392 	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16393 		plane->min_cdclk = hsw_plane_min_cdclk;
16394 	else if (IS_IVYBRIDGE(dev_priv))
16395 		plane->min_cdclk = ivb_plane_min_cdclk;
16396 	else
16397 		plane->min_cdclk = i9xx_plane_min_cdclk;
16398 
16399 	plane->max_stride = i9xx_plane_max_stride;
16400 	plane->update_plane = i9xx_update_plane;
16401 	plane->disable_plane = i9xx_disable_plane;
16402 	plane->get_hw_state = i9xx_plane_get_hw_state;
16403 	plane->check_plane = i9xx_plane_check;
16404 
16405 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16406 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16407 					       0, plane_funcs,
16408 					       formats, num_formats,
16409 					       i9xx_format_modifiers,
16410 					       DRM_PLANE_TYPE_PRIMARY,
16411 					       "primary %c", pipe_name(pipe));
16412 	else
16413 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16414 					       0, plane_funcs,
16415 					       formats, num_formats,
16416 					       i9xx_format_modifiers,
16417 					       DRM_PLANE_TYPE_PRIMARY,
16418 					       "plane %c",
16419 					       plane_name(plane->i9xx_plane));
16420 	if (ret)
16421 		goto fail;
16422 
16423 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
16424 		supported_rotations =
16425 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
16426 			DRM_MODE_REFLECT_X;
16427 	} else if (INTEL_GEN(dev_priv) >= 4) {
16428 		supported_rotations =
16429 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
16430 	} else {
16431 		supported_rotations = DRM_MODE_ROTATE_0;
16432 	}
16433 
16434 	if (INTEL_GEN(dev_priv) >= 4)
16435 		drm_plane_create_rotation_property(&plane->base,
16436 						   DRM_MODE_ROTATE_0,
16437 						   supported_rotations);
16438 
16439 	zpos = 0;
16440 	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
16441 
16442 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
16443 
16444 	return plane;
16445 
16446 fail:
16447 	intel_plane_free(plane);
16448 
16449 	return ERR_PTR(ret);
16450 }
16451 
16452 static struct intel_plane *
16453 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
16454 			  enum pipe pipe)
16455 {
16456 	struct intel_plane *cursor;
16457 	int ret, zpos;
16458 
16459 	cursor = intel_plane_alloc();
16460 	if (IS_ERR(cursor))
16461 		return cursor;
16462 
16463 	cursor->pipe = pipe;
16464 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
16465 	cursor->id = PLANE_CURSOR;
16466 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
16467 
16468 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16469 		cursor->max_stride = i845_cursor_max_stride;
16470 		cursor->update_plane = i845_update_cursor;
16471 		cursor->disable_plane = i845_disable_cursor;
16472 		cursor->get_hw_state = i845_cursor_get_hw_state;
16473 		cursor->check_plane = i845_check_cursor;
16474 	} else {
16475 		cursor->max_stride = i9xx_cursor_max_stride;
16476 		cursor->update_plane = i9xx_update_cursor;
16477 		cursor->disable_plane = i9xx_disable_cursor;
16478 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
16479 		cursor->check_plane = i9xx_check_cursor;
16480 	}
16481 
16482 	cursor->cursor.base = ~0;
16483 	cursor->cursor.cntl = ~0;
16484 
16485 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
16486 		cursor->cursor.size = ~0;
16487 
16488 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
16489 				       0, &intel_cursor_plane_funcs,
16490 				       intel_cursor_formats,
16491 				       ARRAY_SIZE(intel_cursor_formats),
16492 				       cursor_format_modifiers,
16493 				       DRM_PLANE_TYPE_CURSOR,
16494 				       "cursor %c", pipe_name(pipe));
16495 	if (ret)
16496 		goto fail;
16497 
16498 	if (INTEL_GEN(dev_priv) >= 4)
16499 		drm_plane_create_rotation_property(&cursor->base,
16500 						   DRM_MODE_ROTATE_0,
16501 						   DRM_MODE_ROTATE_0 |
16502 						   DRM_MODE_ROTATE_180);
16503 
16504 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
16505 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
16506 
16507 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
16508 
16509 	return cursor;
16510 
16511 fail:
16512 	intel_plane_free(cursor);
16513 
16514 	return ERR_PTR(ret);
16515 }
16516 
16517 #define INTEL_CRTC_FUNCS \
16518 	.gamma_set = drm_atomic_helper_legacy_gamma_set, \
16519 	.set_config = drm_atomic_helper_set_config, \
16520 	.destroy = intel_crtc_destroy, \
16521 	.page_flip = drm_atomic_helper_page_flip, \
16522 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
16523 	.atomic_destroy_state = intel_crtc_destroy_state, \
16524 	.set_crc_source = intel_crtc_set_crc_source, \
16525 	.verify_crc_source = intel_crtc_verify_crc_source, \
16526 	.get_crc_sources = intel_crtc_get_crc_sources
16527 
16528 static const struct drm_crtc_funcs bdw_crtc_funcs = {
16529 	INTEL_CRTC_FUNCS,
16530 
16531 	.get_vblank_counter = g4x_get_vblank_counter,
16532 	.enable_vblank = bdw_enable_vblank,
16533 	.disable_vblank = bdw_disable_vblank,
16534 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16535 };
16536 
16537 static const struct drm_crtc_funcs ilk_crtc_funcs = {
16538 	INTEL_CRTC_FUNCS,
16539 
16540 	.get_vblank_counter = g4x_get_vblank_counter,
16541 	.enable_vblank = ilk_enable_vblank,
16542 	.disable_vblank = ilk_disable_vblank,
16543 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16544 };
16545 
16546 static const struct drm_crtc_funcs g4x_crtc_funcs = {
16547 	INTEL_CRTC_FUNCS,
16548 
16549 	.get_vblank_counter = g4x_get_vblank_counter,
16550 	.enable_vblank = i965_enable_vblank,
16551 	.disable_vblank = i965_disable_vblank,
16552 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16553 };
16554 
16555 static const struct drm_crtc_funcs i965_crtc_funcs = {
16556 	INTEL_CRTC_FUNCS,
16557 
16558 	.get_vblank_counter = i915_get_vblank_counter,
16559 	.enable_vblank = i965_enable_vblank,
16560 	.disable_vblank = i965_disable_vblank,
16561 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16562 };
16563 
16564 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
16565 	INTEL_CRTC_FUNCS,
16566 
16567 	.get_vblank_counter = i915_get_vblank_counter,
16568 	.enable_vblank = i915gm_enable_vblank,
16569 	.disable_vblank = i915gm_disable_vblank,
16570 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16571 };
16572 
16573 static const struct drm_crtc_funcs i915_crtc_funcs = {
16574 	INTEL_CRTC_FUNCS,
16575 
16576 	.get_vblank_counter = i915_get_vblank_counter,
16577 	.enable_vblank = i8xx_enable_vblank,
16578 	.disable_vblank = i8xx_disable_vblank,
16579 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16580 };
16581 
16582 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
16583 	INTEL_CRTC_FUNCS,
16584 
16585 	/* no hw vblank counter */
16586 	.enable_vblank = i8xx_enable_vblank,
16587 	.disable_vblank = i8xx_disable_vblank,
16588 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16589 };
16590 
16591 static struct intel_crtc *intel_crtc_alloc(void)
16592 {
16593 	struct intel_crtc_state *crtc_state;
16594 	struct intel_crtc *crtc;
16595 
16596 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
16597 	if (!crtc)
16598 		return ERR_PTR(-ENOMEM);
16599 
16600 	crtc_state = intel_crtc_state_alloc(crtc);
16601 	if (!crtc_state) {
16602 		kfree(crtc);
16603 		return ERR_PTR(-ENOMEM);
16604 	}
16605 
16606 	crtc->base.state = &crtc_state->uapi;
16607 	crtc->config = crtc_state;
16608 
16609 	return crtc;
16610 }
16611 
16612 static void intel_crtc_free(struct intel_crtc *crtc)
16613 {
16614 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
16615 	kfree(crtc);
16616 }
16617 
16618 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
16619 {
16620 	struct intel_plane *plane;
16621 
16622 	for_each_intel_plane(&dev_priv->drm, plane) {
16623 		struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
16624 								  plane->pipe);
16625 
16626 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
16627 	}
16628 }
16629 
16630 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
16631 {
16632 	struct intel_plane *primary, *cursor;
16633 	const struct drm_crtc_funcs *funcs;
16634 	struct intel_crtc *crtc;
16635 	int sprite, ret;
16636 
16637 	crtc = intel_crtc_alloc();
16638 	if (IS_ERR(crtc))
16639 		return PTR_ERR(crtc);
16640 
16641 	crtc->pipe = pipe;
16642 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
16643 
16644 	primary = intel_primary_plane_create(dev_priv, pipe);
16645 	if (IS_ERR(primary)) {
16646 		ret = PTR_ERR(primary);
16647 		goto fail;
16648 	}
16649 	crtc->plane_ids_mask |= BIT(primary->id);
16650 
16651 	for_each_sprite(dev_priv, pipe, sprite) {
16652 		struct intel_plane *plane;
16653 
16654 		plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
16655 		if (IS_ERR(plane)) {
16656 			ret = PTR_ERR(plane);
16657 			goto fail;
16658 		}
16659 		crtc->plane_ids_mask |= BIT(plane->id);
16660 	}
16661 
16662 	cursor = intel_cursor_plane_create(dev_priv, pipe);
16663 	if (IS_ERR(cursor)) {
16664 		ret = PTR_ERR(cursor);
16665 		goto fail;
16666 	}
16667 	crtc->plane_ids_mask |= BIT(cursor->id);
16668 
16669 	if (HAS_GMCH(dev_priv)) {
16670 		if (IS_CHERRYVIEW(dev_priv) ||
16671 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
16672 			funcs = &g4x_crtc_funcs;
16673 		else if (IS_GEN(dev_priv, 4))
16674 			funcs = &i965_crtc_funcs;
16675 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
16676 			funcs = &i915gm_crtc_funcs;
16677 		else if (IS_GEN(dev_priv, 3))
16678 			funcs = &i915_crtc_funcs;
16679 		else
16680 			funcs = &i8xx_crtc_funcs;
16681 	} else {
16682 		if (INTEL_GEN(dev_priv) >= 8)
16683 			funcs = &bdw_crtc_funcs;
16684 		else
16685 			funcs = &ilk_crtc_funcs;
16686 	}
16687 
16688 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
16689 					&primary->base, &cursor->base,
16690 					funcs, "pipe %c", pipe_name(pipe));
16691 	if (ret)
16692 		goto fail;
16693 
16694 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
16695 	       dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
16696 	dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
16697 
16698 	if (INTEL_GEN(dev_priv) < 9) {
16699 		enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
16700 
16701 		BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
16702 		       dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
16703 		dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
16704 	}
16705 
16706 	intel_color_init(crtc);
16707 
16708 	intel_crtc_crc_init(crtc);
16709 
16710 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
16711 
16712 	return 0;
16713 
16714 fail:
16715 	intel_crtc_free(crtc);
16716 
16717 	return ret;
16718 }
16719 
16720 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
16721 				      struct drm_file *file)
16722 {
16723 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
16724 	struct drm_crtc *drmmode_crtc;
16725 	struct intel_crtc *crtc;
16726 
16727 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
16728 	if (!drmmode_crtc)
16729 		return -ENOENT;
16730 
16731 	crtc = to_intel_crtc(drmmode_crtc);
16732 	pipe_from_crtc_id->pipe = crtc->pipe;
16733 
16734 	return 0;
16735 }
16736 
16737 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
16738 {
16739 	struct drm_device *dev = encoder->base.dev;
16740 	struct intel_encoder *source_encoder;
16741 	u32 possible_clones = 0;
16742 
16743 	for_each_intel_encoder(dev, source_encoder) {
16744 		if (encoders_cloneable(encoder, source_encoder))
16745 			possible_clones |= drm_encoder_mask(&source_encoder->base);
16746 	}
16747 
16748 	return possible_clones;
16749 }
16750 
16751 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
16752 {
16753 	struct drm_device *dev = encoder->base.dev;
16754 	struct intel_crtc *crtc;
16755 	u32 possible_crtcs = 0;
16756 
16757 	for_each_intel_crtc(dev, crtc) {
16758 		if (encoder->pipe_mask & BIT(crtc->pipe))
16759 			possible_crtcs |= drm_crtc_mask(&crtc->base);
16760 	}
16761 
16762 	return possible_crtcs;
16763 }
16764 
16765 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
16766 {
16767 	if (!IS_MOBILE(dev_priv))
16768 		return false;
16769 
16770 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
16771 		return false;
16772 
16773 	if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
16774 		return false;
16775 
16776 	return true;
16777 }
16778 
16779 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
16780 {
16781 	if (INTEL_GEN(dev_priv) >= 9)
16782 		return false;
16783 
16784 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
16785 		return false;
16786 
16787 	if (HAS_PCH_LPT_H(dev_priv) &&
16788 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
16789 		return false;
16790 
16791 	/* DDI E can't be used if DDI A requires 4 lanes */
16792 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
16793 		return false;
16794 
16795 	if (!dev_priv->vbt.int_crt_support)
16796 		return false;
16797 
16798 	return true;
16799 }
16800 
16801 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
16802 {
16803 	int pps_num;
16804 	int pps_idx;
16805 
16806 	if (HAS_DDI(dev_priv))
16807 		return;
16808 	/*
16809 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
16810 	 * everywhere where registers can be write protected.
16811 	 */
16812 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16813 		pps_num = 2;
16814 	else
16815 		pps_num = 1;
16816 
16817 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
16818 		u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
16819 
16820 		val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
16821 		intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
16822 	}
16823 }
16824 
16825 static void intel_pps_init(struct drm_i915_private *dev_priv)
16826 {
16827 	if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
16828 		dev_priv->pps_mmio_base = PCH_PPS_BASE;
16829 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16830 		dev_priv->pps_mmio_base = VLV_PPS_BASE;
16831 	else
16832 		dev_priv->pps_mmio_base = PPS_BASE;
16833 
16834 	intel_pps_unlock_regs_wa(dev_priv);
16835 }
16836 
16837 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
16838 {
16839 	struct intel_encoder *encoder;
16840 	bool dpd_is_edp = false;
16841 
16842 	intel_pps_init(dev_priv);
16843 
16844 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
16845 		return;
16846 
16847 	if (INTEL_GEN(dev_priv) >= 12) {
16848 		intel_ddi_init(dev_priv, PORT_A);
16849 		intel_ddi_init(dev_priv, PORT_B);
16850 		intel_ddi_init(dev_priv, PORT_D);
16851 		intel_ddi_init(dev_priv, PORT_E);
16852 		intel_ddi_init(dev_priv, PORT_F);
16853 		intel_ddi_init(dev_priv, PORT_G);
16854 		intel_ddi_init(dev_priv, PORT_H);
16855 		intel_ddi_init(dev_priv, PORT_I);
16856 		icl_dsi_init(dev_priv);
16857 	} else if (IS_ELKHARTLAKE(dev_priv)) {
16858 		intel_ddi_init(dev_priv, PORT_A);
16859 		intel_ddi_init(dev_priv, PORT_B);
16860 		intel_ddi_init(dev_priv, PORT_C);
16861 		intel_ddi_init(dev_priv, PORT_D);
16862 		icl_dsi_init(dev_priv);
16863 	} else if (IS_GEN(dev_priv, 11)) {
16864 		intel_ddi_init(dev_priv, PORT_A);
16865 		intel_ddi_init(dev_priv, PORT_B);
16866 		intel_ddi_init(dev_priv, PORT_C);
16867 		intel_ddi_init(dev_priv, PORT_D);
16868 		intel_ddi_init(dev_priv, PORT_E);
16869 		/*
16870 		 * On some ICL SKUs port F is not present. No strap bits for
16871 		 * this, so rely on VBT.
16872 		 * Work around broken VBTs on SKUs known to have no port F.
16873 		 */
16874 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
16875 		    intel_bios_is_port_present(dev_priv, PORT_F))
16876 			intel_ddi_init(dev_priv, PORT_F);
16877 
16878 		icl_dsi_init(dev_priv);
16879 	} else if (IS_GEN9_LP(dev_priv)) {
16880 		/*
16881 		 * FIXME: Broxton doesn't support port detection via the
16882 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
16883 		 * detect the ports.
16884 		 */
16885 		intel_ddi_init(dev_priv, PORT_A);
16886 		intel_ddi_init(dev_priv, PORT_B);
16887 		intel_ddi_init(dev_priv, PORT_C);
16888 
16889 		vlv_dsi_init(dev_priv);
16890 	} else if (HAS_DDI(dev_priv)) {
16891 		int found;
16892 
16893 		if (intel_ddi_crt_present(dev_priv))
16894 			intel_crt_init(dev_priv);
16895 
16896 		/*
16897 		 * Haswell uses DDI functions to detect digital outputs.
16898 		 * On SKL pre-D0 the strap isn't connected, so we assume
16899 		 * it's there.
16900 		 */
16901 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
16902 		/* WaIgnoreDDIAStrap: skl */
16903 		if (found || IS_GEN9_BC(dev_priv))
16904 			intel_ddi_init(dev_priv, PORT_A);
16905 
16906 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
16907 		 * register */
16908 		found = intel_de_read(dev_priv, SFUSE_STRAP);
16909 
16910 		if (found & SFUSE_STRAP_DDIB_DETECTED)
16911 			intel_ddi_init(dev_priv, PORT_B);
16912 		if (found & SFUSE_STRAP_DDIC_DETECTED)
16913 			intel_ddi_init(dev_priv, PORT_C);
16914 		if (found & SFUSE_STRAP_DDID_DETECTED)
16915 			intel_ddi_init(dev_priv, PORT_D);
16916 		if (found & SFUSE_STRAP_DDIF_DETECTED)
16917 			intel_ddi_init(dev_priv, PORT_F);
16918 		/*
16919 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
16920 		 */
16921 		if (IS_GEN9_BC(dev_priv) &&
16922 		    intel_bios_is_port_present(dev_priv, PORT_E))
16923 			intel_ddi_init(dev_priv, PORT_E);
16924 
16925 	} else if (HAS_PCH_SPLIT(dev_priv)) {
16926 		int found;
16927 
16928 		/*
16929 		 * intel_edp_init_connector() depends on this completing first,
16930 		 * to prevent the registration of both eDP and LVDS and the
16931 		 * incorrect sharing of the PPS.
16932 		 */
16933 		intel_lvds_init(dev_priv);
16934 		intel_crt_init(dev_priv);
16935 
16936 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
16937 
16938 		if (ilk_has_edp_a(dev_priv))
16939 			intel_dp_init(dev_priv, DP_A, PORT_A);
16940 
16941 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
16942 			/* PCH SDVOB multiplex with HDMIB */
16943 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
16944 			if (!found)
16945 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
16946 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
16947 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
16948 		}
16949 
16950 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
16951 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
16952 
16953 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
16954 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
16955 
16956 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
16957 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
16958 
16959 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
16960 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
16961 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16962 		bool has_edp, has_port;
16963 
16964 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
16965 			intel_crt_init(dev_priv);
16966 
16967 		/*
16968 		 * The DP_DETECTED bit is the latched state of the DDC
16969 		 * SDA pin at boot. However since eDP doesn't require DDC
16970 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
16971 		 * eDP ports may have been muxed to an alternate function.
16972 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
16973 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
16974 		 * detect eDP ports.
16975 		 *
16976 		 * Sadly the straps seem to be missing sometimes even for HDMI
16977 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
16978 		 * and VBT for the presence of the port. Additionally we can't
16979 		 * trust the port type the VBT declares as we've seen at least
16980 		 * HDMI ports that the VBT claim are DP or eDP.
16981 		 */
16982 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
16983 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
16984 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
16985 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
16986 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
16987 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
16988 
16989 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
16990 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
16991 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
16992 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
16993 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
16994 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
16995 
16996 		if (IS_CHERRYVIEW(dev_priv)) {
16997 			/*
16998 			 * eDP not supported on port D,
16999 			 * so no need to worry about it
17000 			 */
17001 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
17002 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
17003 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
17004 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
17005 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
17006 		}
17007 
17008 		vlv_dsi_init(dev_priv);
17009 	} else if (IS_PINEVIEW(dev_priv)) {
17010 		intel_lvds_init(dev_priv);
17011 		intel_crt_init(dev_priv);
17012 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
17013 		bool found = false;
17014 
17015 		if (IS_MOBILE(dev_priv))
17016 			intel_lvds_init(dev_priv);
17017 
17018 		intel_crt_init(dev_priv);
17019 
17020 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17021 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
17022 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
17023 			if (!found && IS_G4X(dev_priv)) {
17024 				drm_dbg_kms(&dev_priv->drm,
17025 					    "probing HDMI on SDVOB\n");
17026 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
17027 			}
17028 
17029 			if (!found && IS_G4X(dev_priv))
17030 				intel_dp_init(dev_priv, DP_B, PORT_B);
17031 		}
17032 
17033 		/* Before G4X SDVOC doesn't have its own detect register */
17034 
17035 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17036 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
17037 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
17038 		}
17039 
17040 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
17041 
17042 			if (IS_G4X(dev_priv)) {
17043 				drm_dbg_kms(&dev_priv->drm,
17044 					    "probing HDMI on SDVOC\n");
17045 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
17046 			}
17047 			if (IS_G4X(dev_priv))
17048 				intel_dp_init(dev_priv, DP_C, PORT_C);
17049 		}
17050 
17051 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
17052 			intel_dp_init(dev_priv, DP_D, PORT_D);
17053 
17054 		if (SUPPORTS_TV(dev_priv))
17055 			intel_tv_init(dev_priv);
17056 	} else if (IS_GEN(dev_priv, 2)) {
17057 		if (IS_I85X(dev_priv))
17058 			intel_lvds_init(dev_priv);
17059 
17060 		intel_crt_init(dev_priv);
17061 		intel_dvo_init(dev_priv);
17062 	}
17063 
17064 	intel_psr_init(dev_priv);
17065 
17066 	for_each_intel_encoder(&dev_priv->drm, encoder) {
17067 		encoder->base.possible_crtcs =
17068 			intel_encoder_possible_crtcs(encoder);
17069 		encoder->base.possible_clones =
17070 			intel_encoder_possible_clones(encoder);
17071 	}
17072 
17073 	intel_init_pch_refclk(dev_priv);
17074 
17075 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
17076 }
17077 
17078 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17079 {
17080 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
17081 
17082 	drm_framebuffer_cleanup(fb);
17083 	intel_frontbuffer_put(intel_fb->frontbuffer);
17084 
17085 	kfree(intel_fb);
17086 }
17087 
17088 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
17089 						struct drm_file *file,
17090 						unsigned int *handle)
17091 {
17092 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17093 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
17094 
17095 	if (obj->userptr.mm) {
17096 		drm_dbg(&i915->drm,
17097 			"attempting to use a userptr for a framebuffer, denied\n");
17098 		return -EINVAL;
17099 	}
17100 
17101 	return drm_gem_handle_create(file, &obj->base, handle);
17102 }
17103 
17104 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17105 					struct drm_file *file,
17106 					unsigned flags, unsigned color,
17107 					struct drm_clip_rect *clips,
17108 					unsigned num_clips)
17109 {
17110 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17111 
17112 	i915_gem_object_flush_if_display(obj);
17113 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
17114 
17115 	return 0;
17116 }
17117 
17118 static const struct drm_framebuffer_funcs intel_fb_funcs = {
17119 	.destroy = intel_user_framebuffer_destroy,
17120 	.create_handle = intel_user_framebuffer_create_handle,
17121 	.dirty = intel_user_framebuffer_dirty,
17122 };
17123 
17124 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17125 				  struct drm_i915_gem_object *obj,
17126 				  struct drm_mode_fb_cmd2 *mode_cmd)
17127 {
17128 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
17129 	struct drm_framebuffer *fb = &intel_fb->base;
17130 	u32 max_stride;
17131 	unsigned int tiling, stride;
17132 	int ret = -EINVAL;
17133 	int i;
17134 
17135 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17136 	if (!intel_fb->frontbuffer)
17137 		return -ENOMEM;
17138 
17139 	i915_gem_object_lock(obj);
17140 	tiling = i915_gem_object_get_tiling(obj);
17141 	stride = i915_gem_object_get_stride(obj);
17142 	i915_gem_object_unlock(obj);
17143 
17144 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
17145 		/*
17146 		 * If there's a fence, enforce that
17147 		 * the fb modifier and tiling mode match.
17148 		 */
17149 		if (tiling != I915_TILING_NONE &&
17150 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17151 			drm_dbg_kms(&dev_priv->drm,
17152 				    "tiling_mode doesn't match fb modifier\n");
17153 			goto err;
17154 		}
17155 	} else {
17156 		if (tiling == I915_TILING_X) {
17157 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
17158 		} else if (tiling == I915_TILING_Y) {
17159 			drm_dbg_kms(&dev_priv->drm,
17160 				    "No Y tiling for legacy addfb\n");
17161 			goto err;
17162 		}
17163 	}
17164 
17165 	if (!drm_any_plane_has_format(&dev_priv->drm,
17166 				      mode_cmd->pixel_format,
17167 				      mode_cmd->modifier[0])) {
17168 		struct drm_format_name_buf format_name;
17169 
17170 		drm_dbg_kms(&dev_priv->drm,
17171 			    "unsupported pixel format %s / modifier 0x%llx\n",
17172 			    drm_get_format_name(mode_cmd->pixel_format,
17173 						&format_name),
17174 			    mode_cmd->modifier[0]);
17175 		goto err;
17176 	}
17177 
17178 	/*
17179 	 * gen2/3 display engine uses the fence if present,
17180 	 * so the tiling mode must match the fb modifier exactly.
17181 	 */
17182 	if (INTEL_GEN(dev_priv) < 4 &&
17183 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17184 		drm_dbg_kms(&dev_priv->drm,
17185 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
17186 		goto err;
17187 	}
17188 
17189 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17190 					 mode_cmd->modifier[0]);
17191 	if (mode_cmd->pitches[0] > max_stride) {
17192 		drm_dbg_kms(&dev_priv->drm,
17193 			    "%s pitch (%u) must be at most %d\n",
17194 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17195 			    "tiled" : "linear",
17196 			    mode_cmd->pitches[0], max_stride);
17197 		goto err;
17198 	}
17199 
17200 	/*
17201 	 * If there's a fence, enforce that
17202 	 * the fb pitch and fence stride match.
17203 	 */
17204 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
17205 		drm_dbg_kms(&dev_priv->drm,
17206 			    "pitch (%d) must match tiling stride (%d)\n",
17207 			    mode_cmd->pitches[0], stride);
17208 		goto err;
17209 	}
17210 
17211 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
17212 	if (mode_cmd->offsets[0] != 0) {
17213 		drm_dbg_kms(&dev_priv->drm,
17214 			    "plane 0 offset (0x%08x) must be 0\n",
17215 			    mode_cmd->offsets[0]);
17216 		goto err;
17217 	}
17218 
17219 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
17220 
17221 	for (i = 0; i < fb->format->num_planes; i++) {
17222 		u32 stride_alignment;
17223 
17224 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
17225 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17226 				    i);
17227 			goto err;
17228 		}
17229 
17230 		stride_alignment = intel_fb_stride_alignment(fb, i);
17231 		if (fb->pitches[i] & (stride_alignment - 1)) {
17232 			drm_dbg_kms(&dev_priv->drm,
17233 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
17234 				    i, fb->pitches[i], stride_alignment);
17235 			goto err;
17236 		}
17237 
17238 		if (is_gen12_ccs_plane(fb, i)) {
17239 			int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17240 
17241 			if (fb->pitches[i] != ccs_aux_stride) {
17242 				drm_dbg_kms(&dev_priv->drm,
17243 					    "ccs aux plane %d pitch (%d) must be %d\n",
17244 					    i,
17245 					    fb->pitches[i], ccs_aux_stride);
17246 				goto err;
17247 			}
17248 		}
17249 
17250 		fb->obj[i] = &obj->base;
17251 	}
17252 
17253 	ret = intel_fill_fb_info(dev_priv, fb);
17254 	if (ret)
17255 		goto err;
17256 
17257 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
17258 	if (ret) {
17259 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
17260 		goto err;
17261 	}
17262 
17263 	return 0;
17264 
17265 err:
17266 	intel_frontbuffer_put(intel_fb->frontbuffer);
17267 	return ret;
17268 }
17269 
17270 static struct drm_framebuffer *
17271 intel_user_framebuffer_create(struct drm_device *dev,
17272 			      struct drm_file *filp,
17273 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
17274 {
17275 	struct drm_framebuffer *fb;
17276 	struct drm_i915_gem_object *obj;
17277 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
17278 
17279 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
17280 	if (!obj)
17281 		return ERR_PTR(-ENOENT);
17282 
17283 	fb = intel_framebuffer_create(obj, &mode_cmd);
17284 	i915_gem_object_put(obj);
17285 
17286 	return fb;
17287 }
17288 
17289 static enum drm_mode_status
17290 intel_mode_valid(struct drm_device *dev,
17291 		 const struct drm_display_mode *mode)
17292 {
17293 	struct drm_i915_private *dev_priv = to_i915(dev);
17294 	int hdisplay_max, htotal_max;
17295 	int vdisplay_max, vtotal_max;
17296 
17297 	/*
17298 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
17299 	 * of DBLSCAN modes to the output's mode list when they detect
17300 	 * the scaling mode property on the connector. And they don't
17301 	 * ask the kernel to validate those modes in any way until
17302 	 * modeset time at which point the client gets a protocol error.
17303 	 * So in order to not upset those clients we silently ignore the
17304 	 * DBLSCAN flag on such connectors. For other connectors we will
17305 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
17306 	 * And we always reject DBLSCAN modes in connector->mode_valid()
17307 	 * as we never want such modes on the connector's mode list.
17308 	 */
17309 
17310 	if (mode->vscan > 1)
17311 		return MODE_NO_VSCAN;
17312 
17313 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
17314 		return MODE_H_ILLEGAL;
17315 
17316 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
17317 			   DRM_MODE_FLAG_NCSYNC |
17318 			   DRM_MODE_FLAG_PCSYNC))
17319 		return MODE_HSYNC;
17320 
17321 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
17322 			   DRM_MODE_FLAG_PIXMUX |
17323 			   DRM_MODE_FLAG_CLKDIV2))
17324 		return MODE_BAD;
17325 
17326 	/* Transcoder timing limits */
17327 	if (INTEL_GEN(dev_priv) >= 11) {
17328 		hdisplay_max = 16384;
17329 		vdisplay_max = 8192;
17330 		htotal_max = 16384;
17331 		vtotal_max = 8192;
17332 	} else if (INTEL_GEN(dev_priv) >= 9 ||
17333 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17334 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
17335 		vdisplay_max = 4096;
17336 		htotal_max = 8192;
17337 		vtotal_max = 8192;
17338 	} else if (INTEL_GEN(dev_priv) >= 3) {
17339 		hdisplay_max = 4096;
17340 		vdisplay_max = 4096;
17341 		htotal_max = 8192;
17342 		vtotal_max = 8192;
17343 	} else {
17344 		hdisplay_max = 2048;
17345 		vdisplay_max = 2048;
17346 		htotal_max = 4096;
17347 		vtotal_max = 4096;
17348 	}
17349 
17350 	if (mode->hdisplay > hdisplay_max ||
17351 	    mode->hsync_start > htotal_max ||
17352 	    mode->hsync_end > htotal_max ||
17353 	    mode->htotal > htotal_max)
17354 		return MODE_H_ILLEGAL;
17355 
17356 	if (mode->vdisplay > vdisplay_max ||
17357 	    mode->vsync_start > vtotal_max ||
17358 	    mode->vsync_end > vtotal_max ||
17359 	    mode->vtotal > vtotal_max)
17360 		return MODE_V_ILLEGAL;
17361 
17362 	if (INTEL_GEN(dev_priv) >= 5) {
17363 		if (mode->hdisplay < 64 ||
17364 		    mode->htotal - mode->hdisplay < 32)
17365 			return MODE_H_ILLEGAL;
17366 
17367 		if (mode->vtotal - mode->vdisplay < 5)
17368 			return MODE_V_ILLEGAL;
17369 	} else {
17370 		if (mode->htotal - mode->hdisplay < 32)
17371 			return MODE_H_ILLEGAL;
17372 
17373 		if (mode->vtotal - mode->vdisplay < 3)
17374 			return MODE_V_ILLEGAL;
17375 	}
17376 
17377 	return MODE_OK;
17378 }
17379 
17380 enum drm_mode_status
17381 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
17382 				const struct drm_display_mode *mode)
17383 {
17384 	int plane_width_max, plane_height_max;
17385 
17386 	/*
17387 	 * intel_mode_valid() should be
17388 	 * sufficient on older platforms.
17389 	 */
17390 	if (INTEL_GEN(dev_priv) < 9)
17391 		return MODE_OK;
17392 
17393 	/*
17394 	 * Most people will probably want a fullscreen
17395 	 * plane so let's not advertize modes that are
17396 	 * too big for that.
17397 	 */
17398 	if (INTEL_GEN(dev_priv) >= 11) {
17399 		plane_width_max = 5120;
17400 		plane_height_max = 4320;
17401 	} else {
17402 		plane_width_max = 5120;
17403 		plane_height_max = 4096;
17404 	}
17405 
17406 	if (mode->hdisplay > plane_width_max)
17407 		return MODE_H_ILLEGAL;
17408 
17409 	if (mode->vdisplay > plane_height_max)
17410 		return MODE_V_ILLEGAL;
17411 
17412 	return MODE_OK;
17413 }
17414 
17415 static const struct drm_mode_config_funcs intel_mode_funcs = {
17416 	.fb_create = intel_user_framebuffer_create,
17417 	.get_format_info = intel_get_format_info,
17418 	.output_poll_changed = intel_fbdev_output_poll_changed,
17419 	.mode_valid = intel_mode_valid,
17420 	.atomic_check = intel_atomic_check,
17421 	.atomic_commit = intel_atomic_commit,
17422 	.atomic_state_alloc = intel_atomic_state_alloc,
17423 	.atomic_state_clear = intel_atomic_state_clear,
17424 	.atomic_state_free = intel_atomic_state_free,
17425 };
17426 
17427 /**
17428  * intel_init_display_hooks - initialize the display modesetting hooks
17429  * @dev_priv: device private
17430  */
17431 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
17432 {
17433 	intel_init_cdclk_hooks(dev_priv);
17434 
17435 	if (INTEL_GEN(dev_priv) >= 9) {
17436 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17437 		dev_priv->display.get_initial_plane_config =
17438 			skl_get_initial_plane_config;
17439 		dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
17440 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17441 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17442 	} else if (HAS_DDI(dev_priv)) {
17443 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17444 		dev_priv->display.get_initial_plane_config =
17445 			i9xx_get_initial_plane_config;
17446 		dev_priv->display.crtc_compute_clock =
17447 			hsw_crtc_compute_clock;
17448 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17449 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17450 	} else if (HAS_PCH_SPLIT(dev_priv)) {
17451 		dev_priv->display.get_pipe_config = ilk_get_pipe_config;
17452 		dev_priv->display.get_initial_plane_config =
17453 			i9xx_get_initial_plane_config;
17454 		dev_priv->display.crtc_compute_clock =
17455 			ilk_crtc_compute_clock;
17456 		dev_priv->display.crtc_enable = ilk_crtc_enable;
17457 		dev_priv->display.crtc_disable = ilk_crtc_disable;
17458 	} else if (IS_CHERRYVIEW(dev_priv)) {
17459 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17460 		dev_priv->display.get_initial_plane_config =
17461 			i9xx_get_initial_plane_config;
17462 		dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
17463 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17464 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17465 	} else if (IS_VALLEYVIEW(dev_priv)) {
17466 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17467 		dev_priv->display.get_initial_plane_config =
17468 			i9xx_get_initial_plane_config;
17469 		dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
17470 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17471 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17472 	} else if (IS_G4X(dev_priv)) {
17473 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17474 		dev_priv->display.get_initial_plane_config =
17475 			i9xx_get_initial_plane_config;
17476 		dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
17477 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17478 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17479 	} else if (IS_PINEVIEW(dev_priv)) {
17480 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17481 		dev_priv->display.get_initial_plane_config =
17482 			i9xx_get_initial_plane_config;
17483 		dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
17484 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17485 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17486 	} else if (!IS_GEN(dev_priv, 2)) {
17487 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17488 		dev_priv->display.get_initial_plane_config =
17489 			i9xx_get_initial_plane_config;
17490 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
17491 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17492 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17493 	} else {
17494 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17495 		dev_priv->display.get_initial_plane_config =
17496 			i9xx_get_initial_plane_config;
17497 		dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
17498 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17499 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17500 	}
17501 
17502 	if (IS_GEN(dev_priv, 5)) {
17503 		dev_priv->display.fdi_link_train = ilk_fdi_link_train;
17504 	} else if (IS_GEN(dev_priv, 6)) {
17505 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
17506 	} else if (IS_IVYBRIDGE(dev_priv)) {
17507 		/* FIXME: detect B0+ stepping and use auto training */
17508 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
17509 	}
17510 
17511 	if (INTEL_GEN(dev_priv) >= 9)
17512 		dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
17513 	else
17514 		dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
17515 
17516 }
17517 
17518 void intel_modeset_init_hw(struct drm_i915_private *i915)
17519 {
17520 	struct intel_cdclk_state *cdclk_state =
17521 		to_intel_cdclk_state(i915->cdclk.obj.state);
17522 
17523 	intel_update_cdclk(i915);
17524 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
17525 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
17526 }
17527 
17528 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
17529 {
17530 	struct drm_plane *plane;
17531 	struct drm_crtc *crtc;
17532 
17533 	drm_for_each_crtc(crtc, state->dev) {
17534 		struct drm_crtc_state *crtc_state;
17535 
17536 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
17537 		if (IS_ERR(crtc_state))
17538 			return PTR_ERR(crtc_state);
17539 	}
17540 
17541 	drm_for_each_plane(plane, state->dev) {
17542 		struct drm_plane_state *plane_state;
17543 
17544 		plane_state = drm_atomic_get_plane_state(state, plane);
17545 		if (IS_ERR(plane_state))
17546 			return PTR_ERR(plane_state);
17547 	}
17548 
17549 	return 0;
17550 }
17551 
17552 /*
17553  * Calculate what we think the watermarks should be for the state we've read
17554  * out of the hardware and then immediately program those watermarks so that
17555  * we ensure the hardware settings match our internal state.
17556  *
17557  * We can calculate what we think WM's should be by creating a duplicate of the
17558  * current state (which was constructed during hardware readout) and running it
17559  * through the atomic check code to calculate new watermark values in the
17560  * state object.
17561  */
17562 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
17563 {
17564 	struct drm_atomic_state *state;
17565 	struct intel_atomic_state *intel_state;
17566 	struct intel_crtc *crtc;
17567 	struct intel_crtc_state *crtc_state;
17568 	struct drm_modeset_acquire_ctx ctx;
17569 	int ret;
17570 	int i;
17571 
17572 	/* Only supported on platforms that use atomic watermark design */
17573 	if (!dev_priv->display.optimize_watermarks)
17574 		return;
17575 
17576 	state = drm_atomic_state_alloc(&dev_priv->drm);
17577 	if (drm_WARN_ON(&dev_priv->drm, !state))
17578 		return;
17579 
17580 	intel_state = to_intel_atomic_state(state);
17581 
17582 	drm_modeset_acquire_init(&ctx, 0);
17583 
17584 retry:
17585 	state->acquire_ctx = &ctx;
17586 
17587 	/*
17588 	 * Hardware readout is the only time we don't want to calculate
17589 	 * intermediate watermarks (since we don't trust the current
17590 	 * watermarks).
17591 	 */
17592 	if (!HAS_GMCH(dev_priv))
17593 		intel_state->skip_intermediate_wm = true;
17594 
17595 	ret = sanitize_watermarks_add_affected(state);
17596 	if (ret)
17597 		goto fail;
17598 
17599 	ret = intel_atomic_check(&dev_priv->drm, state);
17600 	if (ret)
17601 		goto fail;
17602 
17603 	/* Write calculated watermark values back */
17604 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
17605 		crtc_state->wm.need_postvbl_update = true;
17606 		dev_priv->display.optimize_watermarks(intel_state, crtc);
17607 
17608 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
17609 	}
17610 
17611 fail:
17612 	if (ret == -EDEADLK) {
17613 		drm_atomic_state_clear(state);
17614 		drm_modeset_backoff(&ctx);
17615 		goto retry;
17616 	}
17617 
17618 	/*
17619 	 * If we fail here, it means that the hardware appears to be
17620 	 * programmed in a way that shouldn't be possible, given our
17621 	 * understanding of watermark requirements.  This might mean a
17622 	 * mistake in the hardware readout code or a mistake in the
17623 	 * watermark calculations for a given platform.  Raise a WARN
17624 	 * so that this is noticeable.
17625 	 *
17626 	 * If this actually happens, we'll have to just leave the
17627 	 * BIOS-programmed watermarks untouched and hope for the best.
17628 	 */
17629 	drm_WARN(&dev_priv->drm, ret,
17630 		 "Could not determine valid watermarks for inherited state\n");
17631 
17632 	drm_atomic_state_put(state);
17633 
17634 	drm_modeset_drop_locks(&ctx);
17635 	drm_modeset_acquire_fini(&ctx);
17636 }
17637 
17638 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
17639 {
17640 	if (IS_GEN(dev_priv, 5)) {
17641 		u32 fdi_pll_clk =
17642 			intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
17643 
17644 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
17645 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
17646 		dev_priv->fdi_pll_freq = 270000;
17647 	} else {
17648 		return;
17649 	}
17650 
17651 	drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
17652 }
17653 
17654 static int intel_initial_commit(struct drm_device *dev)
17655 {
17656 	struct drm_atomic_state *state = NULL;
17657 	struct drm_modeset_acquire_ctx ctx;
17658 	struct intel_crtc *crtc;
17659 	int ret = 0;
17660 
17661 	state = drm_atomic_state_alloc(dev);
17662 	if (!state)
17663 		return -ENOMEM;
17664 
17665 	drm_modeset_acquire_init(&ctx, 0);
17666 
17667 retry:
17668 	state->acquire_ctx = &ctx;
17669 
17670 	for_each_intel_crtc(dev, crtc) {
17671 		struct intel_crtc_state *crtc_state =
17672 			intel_atomic_get_crtc_state(state, crtc);
17673 
17674 		if (IS_ERR(crtc_state)) {
17675 			ret = PTR_ERR(crtc_state);
17676 			goto out;
17677 		}
17678 
17679 		if (crtc_state->hw.active) {
17680 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
17681 			if (ret)
17682 				goto out;
17683 
17684 			/*
17685 			 * FIXME hack to force a LUT update to avoid the
17686 			 * plane update forcing the pipe gamma on without
17687 			 * having a proper LUT loaded. Remove once we
17688 			 * have readout for pipe gamma enable.
17689 			 */
17690 			crtc_state->uapi.color_mgmt_changed = true;
17691 
17692 			/*
17693 			 * FIXME hack to force full modeset when DSC is being
17694 			 * used.
17695 			 *
17696 			 * As long as we do not have full state readout and
17697 			 * config comparison of crtc_state->dsc, we have no way
17698 			 * to ensure reliable fastset. Remove once we have
17699 			 * readout for DSC.
17700 			 */
17701 			if (crtc_state->dsc.compression_enable) {
17702 				ret = drm_atomic_add_affected_connectors(state,
17703 									 &crtc->base);
17704 				if (ret)
17705 					goto out;
17706 				crtc_state->uapi.mode_changed = true;
17707 				drm_dbg_kms(dev, "Force full modeset for DSC\n");
17708 			}
17709 		}
17710 	}
17711 
17712 	ret = drm_atomic_commit(state);
17713 
17714 out:
17715 	if (ret == -EDEADLK) {
17716 		drm_atomic_state_clear(state);
17717 		drm_modeset_backoff(&ctx);
17718 		goto retry;
17719 	}
17720 
17721 	drm_atomic_state_put(state);
17722 
17723 	drm_modeset_drop_locks(&ctx);
17724 	drm_modeset_acquire_fini(&ctx);
17725 
17726 	return ret;
17727 }
17728 
17729 static void intel_mode_config_init(struct drm_i915_private *i915)
17730 {
17731 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
17732 
17733 	drm_mode_config_init(&i915->drm);
17734 	INIT_LIST_HEAD(&i915->global_obj_list);
17735 
17736 	mode_config->min_width = 0;
17737 	mode_config->min_height = 0;
17738 
17739 	mode_config->preferred_depth = 24;
17740 	mode_config->prefer_shadow = 1;
17741 
17742 	mode_config->allow_fb_modifiers = true;
17743 
17744 	mode_config->funcs = &intel_mode_funcs;
17745 
17746 	/*
17747 	 * Maximum framebuffer dimensions, chosen to match
17748 	 * the maximum render engine surface size on gen4+.
17749 	 */
17750 	if (INTEL_GEN(i915) >= 7) {
17751 		mode_config->max_width = 16384;
17752 		mode_config->max_height = 16384;
17753 	} else if (INTEL_GEN(i915) >= 4) {
17754 		mode_config->max_width = 8192;
17755 		mode_config->max_height = 8192;
17756 	} else if (IS_GEN(i915, 3)) {
17757 		mode_config->max_width = 4096;
17758 		mode_config->max_height = 4096;
17759 	} else {
17760 		mode_config->max_width = 2048;
17761 		mode_config->max_height = 2048;
17762 	}
17763 
17764 	if (IS_I845G(i915) || IS_I865G(i915)) {
17765 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
17766 		mode_config->cursor_height = 1023;
17767 	} else if (IS_GEN(i915, 2)) {
17768 		mode_config->cursor_width = 64;
17769 		mode_config->cursor_height = 64;
17770 	} else {
17771 		mode_config->cursor_width = 256;
17772 		mode_config->cursor_height = 256;
17773 	}
17774 }
17775 
17776 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
17777 {
17778 	intel_atomic_global_obj_cleanup(i915);
17779 	drm_mode_config_cleanup(&i915->drm);
17780 }
17781 
17782 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
17783 {
17784 	if (plane_config->fb) {
17785 		struct drm_framebuffer *fb = &plane_config->fb->base;
17786 
17787 		/* We may only have the stub and not a full framebuffer */
17788 		if (drm_framebuffer_read_refcount(fb))
17789 			drm_framebuffer_put(fb);
17790 		else
17791 			kfree(fb);
17792 	}
17793 
17794 	if (plane_config->vma)
17795 		i915_vma_put(plane_config->vma);
17796 }
17797 
17798 /* part #1: call before irq install */
17799 int intel_modeset_init_noirq(struct drm_i915_private *i915)
17800 {
17801 	int ret;
17802 
17803 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
17804 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
17805 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
17806 
17807 	intel_mode_config_init(i915);
17808 
17809 	ret = intel_cdclk_init(i915);
17810 	if (ret)
17811 		return ret;
17812 
17813 	ret = intel_bw_init(i915);
17814 	if (ret)
17815 		return ret;
17816 
17817 	init_llist_head(&i915->atomic_helper.free_list);
17818 	INIT_WORK(&i915->atomic_helper.free_work,
17819 		  intel_atomic_helper_free_state_worker);
17820 
17821 	intel_init_quirks(i915);
17822 
17823 	intel_fbc_init(i915);
17824 
17825 	return 0;
17826 }
17827 
17828 /* part #2: call after irq install */
17829 int intel_modeset_init(struct drm_i915_private *i915)
17830 {
17831 	struct drm_device *dev = &i915->drm;
17832 	enum pipe pipe;
17833 	struct intel_crtc *crtc;
17834 	int ret;
17835 
17836 	intel_init_pm(i915);
17837 
17838 	intel_panel_sanitize_ssc(i915);
17839 
17840 	intel_gmbus_setup(i915);
17841 
17842 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
17843 		    INTEL_NUM_PIPES(i915),
17844 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
17845 
17846 	if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
17847 		for_each_pipe(i915, pipe) {
17848 			ret = intel_crtc_init(i915, pipe);
17849 			if (ret) {
17850 				intel_mode_config_cleanup(i915);
17851 				return ret;
17852 			}
17853 		}
17854 	}
17855 
17856 	intel_plane_possible_crtcs_init(i915);
17857 	intel_shared_dpll_init(dev);
17858 	intel_update_fdi_pll_freq(i915);
17859 
17860 	intel_update_czclk(i915);
17861 	intel_modeset_init_hw(i915);
17862 
17863 	intel_hdcp_component_init(i915);
17864 
17865 	if (i915->max_cdclk_freq == 0)
17866 		intel_update_max_cdclk(i915);
17867 
17868 	/* Just disable it once at startup */
17869 	intel_vga_disable(i915);
17870 	intel_setup_outputs(i915);
17871 
17872 	drm_modeset_lock_all(dev);
17873 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
17874 	drm_modeset_unlock_all(dev);
17875 
17876 	for_each_intel_crtc(dev, crtc) {
17877 		struct intel_initial_plane_config plane_config = {};
17878 
17879 		if (!crtc->active)
17880 			continue;
17881 
17882 		/*
17883 		 * Note that reserving the BIOS fb up front prevents us
17884 		 * from stuffing other stolen allocations like the ring
17885 		 * on top.  This prevents some ugliness at boot time, and
17886 		 * can even allow for smooth boot transitions if the BIOS
17887 		 * fb is large enough for the active pipe configuration.
17888 		 */
17889 		i915->display.get_initial_plane_config(crtc, &plane_config);
17890 
17891 		/*
17892 		 * If the fb is shared between multiple heads, we'll
17893 		 * just get the first one.
17894 		 */
17895 		intel_find_initial_plane_obj(crtc, &plane_config);
17896 
17897 		plane_config_fini(&plane_config);
17898 	}
17899 
17900 	/*
17901 	 * Make sure hardware watermarks really match the state we read out.
17902 	 * Note that we need to do this after reconstructing the BIOS fb's
17903 	 * since the watermark calculation done here will use pstate->fb.
17904 	 */
17905 	if (!HAS_GMCH(i915))
17906 		sanitize_watermarks(i915);
17907 
17908 	/*
17909 	 * Force all active planes to recompute their states. So that on
17910 	 * mode_setcrtc after probe, all the intel_plane_state variables
17911 	 * are already calculated and there is no assert_plane warnings
17912 	 * during bootup.
17913 	 */
17914 	ret = intel_initial_commit(dev);
17915 	if (ret)
17916 		drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
17917 
17918 	return 0;
17919 }
17920 
17921 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17922 {
17923 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17924 	/* 640x480@60Hz, ~25175 kHz */
17925 	struct dpll clock = {
17926 		.m1 = 18,
17927 		.m2 = 7,
17928 		.p1 = 13,
17929 		.p2 = 4,
17930 		.n = 2,
17931 	};
17932 	u32 dpll, fp;
17933 	int i;
17934 
17935 	drm_WARN_ON(&dev_priv->drm,
17936 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
17937 
17938 	drm_dbg_kms(&dev_priv->drm,
17939 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
17940 		    pipe_name(pipe), clock.vco, clock.dot);
17941 
17942 	fp = i9xx_dpll_compute_fp(&clock);
17943 	dpll = DPLL_DVO_2X_MODE |
17944 		DPLL_VGA_MODE_DIS |
17945 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
17946 		PLL_P2_DIVIDE_BY_4 |
17947 		PLL_REF_INPUT_DREFCLK |
17948 		DPLL_VCO_ENABLE;
17949 
17950 	intel_de_write(dev_priv, FP0(pipe), fp);
17951 	intel_de_write(dev_priv, FP1(pipe), fp);
17952 
17953 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
17954 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
17955 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
17956 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
17957 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
17958 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
17959 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
17960 
17961 	/*
17962 	 * Apparently we need to have VGA mode enabled prior to changing
17963 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
17964 	 * dividers, even though the register value does change.
17965 	 */
17966 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
17967 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17968 
17969 	/* Wait for the clocks to stabilize. */
17970 	intel_de_posting_read(dev_priv, DPLL(pipe));
17971 	udelay(150);
17972 
17973 	/* The pixel multiplier can only be updated once the
17974 	 * DPLL is enabled and the clocks are stable.
17975 	 *
17976 	 * So write it again.
17977 	 */
17978 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17979 
17980 	/* We do this three times for luck */
17981 	for (i = 0; i < 3 ; i++) {
17982 		intel_de_write(dev_priv, DPLL(pipe), dpll);
17983 		intel_de_posting_read(dev_priv, DPLL(pipe));
17984 		udelay(150); /* wait for warmup */
17985 	}
17986 
17987 	intel_de_write(dev_priv, PIPECONF(pipe),
17988 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
17989 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
17990 
17991 	intel_wait_for_pipe_scanline_moving(crtc);
17992 }
17993 
17994 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17995 {
17996 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17997 
17998 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
17999 		    pipe_name(pipe));
18000 
18001 	drm_WARN_ON(&dev_priv->drm,
18002 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
18003 		    DISPLAY_PLANE_ENABLE);
18004 	drm_WARN_ON(&dev_priv->drm,
18005 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
18006 		    DISPLAY_PLANE_ENABLE);
18007 	drm_WARN_ON(&dev_priv->drm,
18008 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
18009 		    DISPLAY_PLANE_ENABLE);
18010 	drm_WARN_ON(&dev_priv->drm,
18011 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
18012 	drm_WARN_ON(&dev_priv->drm,
18013 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
18014 
18015 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
18016 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
18017 
18018 	intel_wait_for_pipe_scanline_stopped(crtc);
18019 
18020 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18021 	intel_de_posting_read(dev_priv, DPLL(pipe));
18022 }
18023 
18024 static void
18025 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18026 {
18027 	struct intel_crtc *crtc;
18028 
18029 	if (INTEL_GEN(dev_priv) >= 4)
18030 		return;
18031 
18032 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18033 		struct intel_plane *plane =
18034 			to_intel_plane(crtc->base.primary);
18035 		struct intel_crtc *plane_crtc;
18036 		enum pipe pipe;
18037 
18038 		if (!plane->get_hw_state(plane, &pipe))
18039 			continue;
18040 
18041 		if (pipe == crtc->pipe)
18042 			continue;
18043 
18044 		drm_dbg_kms(&dev_priv->drm,
18045 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18046 			    plane->base.base.id, plane->base.name);
18047 
18048 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18049 		intel_plane_disable_noatomic(plane_crtc, plane);
18050 	}
18051 }
18052 
18053 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18054 {
18055 	struct drm_device *dev = crtc->base.dev;
18056 	struct intel_encoder *encoder;
18057 
18058 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18059 		return true;
18060 
18061 	return false;
18062 }
18063 
18064 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18065 {
18066 	struct drm_device *dev = encoder->base.dev;
18067 	struct intel_connector *connector;
18068 
18069 	for_each_connector_on_encoder(dev, &encoder->base, connector)
18070 		return connector;
18071 
18072 	return NULL;
18073 }
18074 
18075 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
18076 			      enum pipe pch_transcoder)
18077 {
18078 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
18079 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
18080 }
18081 
18082 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
18083 {
18084 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18085 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
18086 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
18087 
18088 	if (INTEL_GEN(dev_priv) >= 9 ||
18089 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18090 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18091 		u32 val;
18092 
18093 		if (transcoder_is_dsi(cpu_transcoder))
18094 			return;
18095 
18096 		val = intel_de_read(dev_priv, reg);
18097 		val &= ~HSW_FRAME_START_DELAY_MASK;
18098 		val |= HSW_FRAME_START_DELAY(0);
18099 		intel_de_write(dev_priv, reg, val);
18100 	} else {
18101 		i915_reg_t reg = PIPECONF(cpu_transcoder);
18102 		u32 val;
18103 
18104 		val = intel_de_read(dev_priv, reg);
18105 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18106 		val |= PIPECONF_FRAME_START_DELAY(0);
18107 		intel_de_write(dev_priv, reg, val);
18108 	}
18109 
18110 	if (!crtc_state->has_pch_encoder)
18111 		return;
18112 
18113 	if (HAS_PCH_IBX(dev_priv)) {
18114 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18115 		u32 val;
18116 
18117 		val = intel_de_read(dev_priv, reg);
18118 		val &= ~TRANS_FRAME_START_DELAY_MASK;
18119 		val |= TRANS_FRAME_START_DELAY(0);
18120 		intel_de_write(dev_priv, reg, val);
18121 	} else {
18122 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18123 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
18124 		u32 val;
18125 
18126 		val = intel_de_read(dev_priv, reg);
18127 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18128 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
18129 		intel_de_write(dev_priv, reg, val);
18130 	}
18131 }
18132 
18133 static void intel_sanitize_crtc(struct intel_crtc *crtc,
18134 				struct drm_modeset_acquire_ctx *ctx)
18135 {
18136 	struct drm_device *dev = crtc->base.dev;
18137 	struct drm_i915_private *dev_priv = to_i915(dev);
18138 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
18139 
18140 	if (crtc_state->hw.active) {
18141 		struct intel_plane *plane;
18142 
18143 		/* Clear any frame start delays used for debugging left by the BIOS */
18144 		intel_sanitize_frame_start_delay(crtc_state);
18145 
18146 		/* Disable everything but the primary plane */
18147 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
18148 			const struct intel_plane_state *plane_state =
18149 				to_intel_plane_state(plane->base.state);
18150 
18151 			if (plane_state->uapi.visible &&
18152 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18153 				intel_plane_disable_noatomic(crtc, plane);
18154 		}
18155 
18156 		/*
18157 		 * Disable any background color set by the BIOS, but enable the
18158 		 * gamma and CSC to match how we program our planes.
18159 		 */
18160 		if (INTEL_GEN(dev_priv) >= 9)
18161 			intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
18162 				       SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
18163 	}
18164 
18165 	/* Adjust the state of the output pipe according to whether we
18166 	 * have active connectors/encoders. */
18167 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc))
18168 		intel_crtc_disable_noatomic(crtc, ctx);
18169 
18170 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
18171 		/*
18172 		 * We start out with underrun reporting disabled to avoid races.
18173 		 * For correct bookkeeping mark this on active crtcs.
18174 		 *
18175 		 * Also on gmch platforms we dont have any hardware bits to
18176 		 * disable the underrun reporting. Which means we need to start
18177 		 * out with underrun reporting disabled also on inactive pipes,
18178 		 * since otherwise we'll complain about the garbage we read when
18179 		 * e.g. coming up after runtime pm.
18180 		 *
18181 		 * No protection against concurrent access is required - at
18182 		 * worst a fifo underrun happens which also sets this to false.
18183 		 */
18184 		crtc->cpu_fifo_underrun_disabled = true;
18185 		/*
18186 		 * We track the PCH trancoder underrun reporting state
18187 		 * within the crtc. With crtc for pipe A housing the underrun
18188 		 * reporting state for PCH transcoder A, crtc for pipe B housing
18189 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
18190 		 * and marking underrun reporting as disabled for the non-existing
18191 		 * PCH transcoders B and C would prevent enabling the south
18192 		 * error interrupt (see cpt_can_enable_serr_int()).
18193 		 */
18194 		if (has_pch_trancoder(dev_priv, crtc->pipe))
18195 			crtc->pch_fifo_underrun_disabled = true;
18196 	}
18197 }
18198 
18199 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
18200 {
18201 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
18202 
18203 	/*
18204 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
18205 	 * the hardware when a high res displays plugged in. DPLL P
18206 	 * divider is zero, and the pipe timings are bonkers. We'll
18207 	 * try to disable everything in that case.
18208 	 *
18209 	 * FIXME would be nice to be able to sanitize this state
18210 	 * without several WARNs, but for now let's take the easy
18211 	 * road.
18212 	 */
18213 	return IS_GEN(dev_priv, 6) &&
18214 		crtc_state->hw.active &&
18215 		crtc_state->shared_dpll &&
18216 		crtc_state->port_clock == 0;
18217 }
18218 
18219 static void intel_sanitize_encoder(struct intel_encoder *encoder)
18220 {
18221 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
18222 	struct intel_connector *connector;
18223 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
18224 	struct intel_crtc_state *crtc_state = crtc ?
18225 		to_intel_crtc_state(crtc->base.state) : NULL;
18226 
18227 	/* We need to check both for a crtc link (meaning that the
18228 	 * encoder is active and trying to read from a pipe) and the
18229 	 * pipe itself being active. */
18230 	bool has_active_crtc = crtc_state &&
18231 		crtc_state->hw.active;
18232 
18233 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
18234 		drm_dbg_kms(&dev_priv->drm,
18235 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
18236 			    pipe_name(crtc->pipe));
18237 		has_active_crtc = false;
18238 	}
18239 
18240 	connector = intel_encoder_find_connector(encoder);
18241 	if (connector && !has_active_crtc) {
18242 		drm_dbg_kms(&dev_priv->drm,
18243 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
18244 			    encoder->base.base.id,
18245 			    encoder->base.name);
18246 
18247 		/* Connector is active, but has no active pipe. This is
18248 		 * fallout from our resume register restoring. Disable
18249 		 * the encoder manually again. */
18250 		if (crtc_state) {
18251 			struct drm_encoder *best_encoder;
18252 
18253 			drm_dbg_kms(&dev_priv->drm,
18254 				    "[ENCODER:%d:%s] manually disabled\n",
18255 				    encoder->base.base.id,
18256 				    encoder->base.name);
18257 
18258 			/* avoid oopsing in case the hooks consult best_encoder */
18259 			best_encoder = connector->base.state->best_encoder;
18260 			connector->base.state->best_encoder = &encoder->base;
18261 
18262 			if (encoder->disable)
18263 				encoder->disable(encoder, crtc_state,
18264 						 connector->base.state);
18265 			if (encoder->post_disable)
18266 				encoder->post_disable(encoder, crtc_state,
18267 						      connector->base.state);
18268 
18269 			connector->base.state->best_encoder = best_encoder;
18270 		}
18271 		encoder->base.crtc = NULL;
18272 
18273 		/* Inconsistent output/port/pipe state happens presumably due to
18274 		 * a bug in one of the get_hw_state functions. Or someplace else
18275 		 * in our code, like the register restore mess on resume. Clamp
18276 		 * things to off as a safer default. */
18277 
18278 		connector->base.dpms = DRM_MODE_DPMS_OFF;
18279 		connector->base.encoder = NULL;
18280 	}
18281 
18282 	/* notify opregion of the sanitized encoder state */
18283 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
18284 
18285 	if (INTEL_GEN(dev_priv) >= 11)
18286 		icl_sanitize_encoder_pll_mapping(encoder);
18287 }
18288 
18289 /* FIXME read out full plane state for all planes */
18290 static void readout_plane_state(struct drm_i915_private *dev_priv)
18291 {
18292 	struct intel_plane *plane;
18293 	struct intel_crtc *crtc;
18294 
18295 	for_each_intel_plane(&dev_priv->drm, plane) {
18296 		struct intel_plane_state *plane_state =
18297 			to_intel_plane_state(plane->base.state);
18298 		struct intel_crtc_state *crtc_state;
18299 		enum pipe pipe = PIPE_A;
18300 		bool visible;
18301 
18302 		visible = plane->get_hw_state(plane, &pipe);
18303 
18304 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18305 		crtc_state = to_intel_crtc_state(crtc->base.state);
18306 
18307 		intel_set_plane_visible(crtc_state, plane_state, visible);
18308 
18309 		drm_dbg_kms(&dev_priv->drm,
18310 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
18311 			    plane->base.base.id, plane->base.name,
18312 			    enableddisabled(visible), pipe_name(pipe));
18313 	}
18314 
18315 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18316 		struct intel_crtc_state *crtc_state =
18317 			to_intel_crtc_state(crtc->base.state);
18318 
18319 		fixup_active_planes(crtc_state);
18320 	}
18321 }
18322 
18323 static void intel_modeset_readout_hw_state(struct drm_device *dev)
18324 {
18325 	struct drm_i915_private *dev_priv = to_i915(dev);
18326 	struct intel_cdclk_state *cdclk_state =
18327 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
18328 	enum pipe pipe;
18329 	struct intel_crtc *crtc;
18330 	struct intel_encoder *encoder;
18331 	struct intel_connector *connector;
18332 	struct drm_connector_list_iter conn_iter;
18333 	u8 active_pipes = 0;
18334 
18335 	for_each_intel_crtc(dev, crtc) {
18336 		struct intel_crtc_state *crtc_state =
18337 			to_intel_crtc_state(crtc->base.state);
18338 
18339 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
18340 		intel_crtc_free_hw_state(crtc_state);
18341 		intel_crtc_state_reset(crtc_state, crtc);
18342 
18343 		crtc_state->hw.active = crtc_state->hw.enable =
18344 			dev_priv->display.get_pipe_config(crtc, crtc_state);
18345 
18346 		crtc->base.enabled = crtc_state->hw.enable;
18347 		crtc->active = crtc_state->hw.active;
18348 
18349 		if (crtc_state->hw.active)
18350 			active_pipes |= BIT(crtc->pipe);
18351 
18352 		drm_dbg_kms(&dev_priv->drm,
18353 			    "[CRTC:%d:%s] hw state readout: %s\n",
18354 			    crtc->base.base.id, crtc->base.name,
18355 			    enableddisabled(crtc_state->hw.active));
18356 	}
18357 
18358 	dev_priv->active_pipes = cdclk_state->active_pipes = active_pipes;
18359 
18360 	readout_plane_state(dev_priv);
18361 
18362 	intel_dpll_readout_hw_state(dev_priv);
18363 
18364 	for_each_intel_encoder(dev, encoder) {
18365 		pipe = 0;
18366 
18367 		if (encoder->get_hw_state(encoder, &pipe)) {
18368 			struct intel_crtc_state *crtc_state;
18369 
18370 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18371 			crtc_state = to_intel_crtc_state(crtc->base.state);
18372 
18373 			encoder->base.crtc = &crtc->base;
18374 			encoder->get_config(encoder, crtc_state);
18375 		} else {
18376 			encoder->base.crtc = NULL;
18377 		}
18378 
18379 		drm_dbg_kms(&dev_priv->drm,
18380 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
18381 			    encoder->base.base.id, encoder->base.name,
18382 			    enableddisabled(encoder->base.crtc),
18383 			    pipe_name(pipe));
18384 	}
18385 
18386 	drm_connector_list_iter_begin(dev, &conn_iter);
18387 	for_each_intel_connector_iter(connector, &conn_iter) {
18388 		if (connector->get_hw_state(connector)) {
18389 			struct intel_crtc_state *crtc_state;
18390 			struct intel_crtc *crtc;
18391 
18392 			connector->base.dpms = DRM_MODE_DPMS_ON;
18393 
18394 			encoder = intel_attached_encoder(connector);
18395 			connector->base.encoder = &encoder->base;
18396 
18397 			crtc = to_intel_crtc(encoder->base.crtc);
18398 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
18399 
18400 			if (crtc_state && crtc_state->hw.active) {
18401 				/*
18402 				 * This has to be done during hardware readout
18403 				 * because anything calling .crtc_disable may
18404 				 * rely on the connector_mask being accurate.
18405 				 */
18406 				crtc_state->uapi.connector_mask |=
18407 					drm_connector_mask(&connector->base);
18408 				crtc_state->uapi.encoder_mask |=
18409 					drm_encoder_mask(&encoder->base);
18410 			}
18411 		} else {
18412 			connector->base.dpms = DRM_MODE_DPMS_OFF;
18413 			connector->base.encoder = NULL;
18414 		}
18415 		drm_dbg_kms(&dev_priv->drm,
18416 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
18417 			    connector->base.base.id, connector->base.name,
18418 			    enableddisabled(connector->base.encoder));
18419 	}
18420 	drm_connector_list_iter_end(&conn_iter);
18421 
18422 	for_each_intel_crtc(dev, crtc) {
18423 		struct intel_bw_state *bw_state =
18424 			to_intel_bw_state(dev_priv->bw_obj.state);
18425 		struct intel_crtc_state *crtc_state =
18426 			to_intel_crtc_state(crtc->base.state);
18427 		struct intel_plane *plane;
18428 		int min_cdclk = 0;
18429 
18430 		if (crtc_state->hw.active) {
18431 			struct drm_display_mode *mode = &crtc_state->hw.mode;
18432 
18433 			intel_mode_from_pipe_config(&crtc_state->hw.adjusted_mode,
18434 						    crtc_state);
18435 
18436 			*mode = crtc_state->hw.adjusted_mode;
18437 			mode->hdisplay = crtc_state->pipe_src_w;
18438 			mode->vdisplay = crtc_state->pipe_src_h;
18439 
18440 			/*
18441 			 * The initial mode needs to be set in order to keep
18442 			 * the atomic core happy. It wants a valid mode if the
18443 			 * crtc's enabled, so we do the above call.
18444 			 *
18445 			 * But we don't set all the derived state fully, hence
18446 			 * set a flag to indicate that a full recalculation is
18447 			 * needed on the next commit.
18448 			 */
18449 			mode->private_flags = I915_MODE_FLAG_INHERITED;
18450 
18451 			intel_crtc_compute_pixel_rate(crtc_state);
18452 
18453 			intel_crtc_update_active_timings(crtc_state);
18454 
18455 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
18456 		}
18457 
18458 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
18459 			const struct intel_plane_state *plane_state =
18460 				to_intel_plane_state(plane->base.state);
18461 
18462 			/*
18463 			 * FIXME don't have the fb yet, so can't
18464 			 * use intel_plane_data_rate() :(
18465 			 */
18466 			if (plane_state->uapi.visible)
18467 				crtc_state->data_rate[plane->id] =
18468 					4 * crtc_state->pixel_rate;
18469 			/*
18470 			 * FIXME don't have the fb yet, so can't
18471 			 * use plane->min_cdclk() :(
18472 			 */
18473 			if (plane_state->uapi.visible && plane->min_cdclk) {
18474 				if (crtc_state->double_wide ||
18475 				    INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
18476 					crtc_state->min_cdclk[plane->id] =
18477 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
18478 				else
18479 					crtc_state->min_cdclk[plane->id] =
18480 						crtc_state->pixel_rate;
18481 			}
18482 			drm_dbg_kms(&dev_priv->drm,
18483 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
18484 				    plane->base.base.id, plane->base.name,
18485 				    crtc_state->min_cdclk[plane->id]);
18486 		}
18487 
18488 		if (crtc_state->hw.active) {
18489 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
18490 			if (drm_WARN_ON(dev, min_cdclk < 0))
18491 				min_cdclk = 0;
18492 		}
18493 
18494 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
18495 		cdclk_state->min_voltage_level[crtc->pipe] =
18496 			crtc_state->min_voltage_level;
18497 
18498 		intel_bw_crtc_update(bw_state, crtc_state);
18499 
18500 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
18501 	}
18502 }
18503 
18504 static void
18505 get_encoder_power_domains(struct drm_i915_private *dev_priv)
18506 {
18507 	struct intel_encoder *encoder;
18508 
18509 	for_each_intel_encoder(&dev_priv->drm, encoder) {
18510 		struct intel_crtc_state *crtc_state;
18511 
18512 		if (!encoder->get_power_domains)
18513 			continue;
18514 
18515 		/*
18516 		 * MST-primary and inactive encoders don't have a crtc state
18517 		 * and neither of these require any power domain references.
18518 		 */
18519 		if (!encoder->base.crtc)
18520 			continue;
18521 
18522 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
18523 		encoder->get_power_domains(encoder, crtc_state);
18524 	}
18525 }
18526 
18527 static void intel_early_display_was(struct drm_i915_private *dev_priv)
18528 {
18529 	/*
18530 	 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
18531 	 * Also known as Wa_14010480278.
18532 	 */
18533 	if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
18534 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
18535 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
18536 
18537 	if (IS_HASWELL(dev_priv)) {
18538 		/*
18539 		 * WaRsPkgCStateDisplayPMReq:hsw
18540 		 * System hang if this isn't done before disabling all planes!
18541 		 */
18542 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
18543 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
18544 	}
18545 }
18546 
18547 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
18548 				       enum port port, i915_reg_t hdmi_reg)
18549 {
18550 	u32 val = intel_de_read(dev_priv, hdmi_reg);
18551 
18552 	if (val & SDVO_ENABLE ||
18553 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
18554 		return;
18555 
18556 	drm_dbg_kms(&dev_priv->drm,
18557 		    "Sanitizing transcoder select for HDMI %c\n",
18558 		    port_name(port));
18559 
18560 	val &= ~SDVO_PIPE_SEL_MASK;
18561 	val |= SDVO_PIPE_SEL(PIPE_A);
18562 
18563 	intel_de_write(dev_priv, hdmi_reg, val);
18564 }
18565 
18566 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
18567 				     enum port port, i915_reg_t dp_reg)
18568 {
18569 	u32 val = intel_de_read(dev_priv, dp_reg);
18570 
18571 	if (val & DP_PORT_EN ||
18572 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
18573 		return;
18574 
18575 	drm_dbg_kms(&dev_priv->drm,
18576 		    "Sanitizing transcoder select for DP %c\n",
18577 		    port_name(port));
18578 
18579 	val &= ~DP_PIPE_SEL_MASK;
18580 	val |= DP_PIPE_SEL(PIPE_A);
18581 
18582 	intel_de_write(dev_priv, dp_reg, val);
18583 }
18584 
18585 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
18586 {
18587 	/*
18588 	 * The BIOS may select transcoder B on some of the PCH
18589 	 * ports even it doesn't enable the port. This would trip
18590 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
18591 	 * Sanitize the transcoder select bits to prevent that. We
18592 	 * assume that the BIOS never actually enabled the port,
18593 	 * because if it did we'd actually have to toggle the port
18594 	 * on and back off to make the transcoder A select stick
18595 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
18596 	 * intel_disable_sdvo()).
18597 	 */
18598 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
18599 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
18600 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
18601 
18602 	/* PCH SDVOB multiplex with HDMIB */
18603 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
18604 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
18605 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
18606 }
18607 
18608 /* Scan out the current hw modeset state,
18609  * and sanitizes it to the current state
18610  */
18611 static void
18612 intel_modeset_setup_hw_state(struct drm_device *dev,
18613 			     struct drm_modeset_acquire_ctx *ctx)
18614 {
18615 	struct drm_i915_private *dev_priv = to_i915(dev);
18616 	struct intel_encoder *encoder;
18617 	struct intel_crtc *crtc;
18618 	intel_wakeref_t wakeref;
18619 
18620 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
18621 
18622 	intel_early_display_was(dev_priv);
18623 	intel_modeset_readout_hw_state(dev);
18624 
18625 	/* HW state is read out, now we need to sanitize this mess. */
18626 
18627 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
18628 	for_each_intel_encoder(dev, encoder) {
18629 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
18630 
18631 		/* We need to sanitize only the MST primary port. */
18632 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
18633 		    intel_phy_is_tc(dev_priv, phy))
18634 			intel_tc_port_sanitize(enc_to_dig_port(encoder));
18635 	}
18636 
18637 	get_encoder_power_domains(dev_priv);
18638 
18639 	if (HAS_PCH_IBX(dev_priv))
18640 		ibx_sanitize_pch_ports(dev_priv);
18641 
18642 	/*
18643 	 * intel_sanitize_plane_mapping() may need to do vblank
18644 	 * waits, so we need vblank interrupts restored beforehand.
18645 	 */
18646 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18647 		struct intel_crtc_state *crtc_state =
18648 			to_intel_crtc_state(crtc->base.state);
18649 
18650 		drm_crtc_vblank_reset(&crtc->base);
18651 
18652 		if (crtc_state->hw.active)
18653 			intel_crtc_vblank_on(crtc_state);
18654 	}
18655 
18656 	intel_sanitize_plane_mapping(dev_priv);
18657 
18658 	for_each_intel_encoder(dev, encoder)
18659 		intel_sanitize_encoder(encoder);
18660 
18661 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18662 		struct intel_crtc_state *crtc_state =
18663 			to_intel_crtc_state(crtc->base.state);
18664 
18665 		intel_sanitize_crtc(crtc, ctx);
18666 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
18667 	}
18668 
18669 	intel_modeset_update_connector_atomic_state(dev);
18670 
18671 	intel_dpll_sanitize_state(dev_priv);
18672 
18673 	if (IS_G4X(dev_priv)) {
18674 		g4x_wm_get_hw_state(dev_priv);
18675 		g4x_wm_sanitize(dev_priv);
18676 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
18677 		vlv_wm_get_hw_state(dev_priv);
18678 		vlv_wm_sanitize(dev_priv);
18679 	} else if (INTEL_GEN(dev_priv) >= 9) {
18680 		skl_wm_get_hw_state(dev_priv);
18681 	} else if (HAS_PCH_SPLIT(dev_priv)) {
18682 		ilk_wm_get_hw_state(dev_priv);
18683 	}
18684 
18685 	for_each_intel_crtc(dev, crtc) {
18686 		struct intel_crtc_state *crtc_state =
18687 			to_intel_crtc_state(crtc->base.state);
18688 		u64 put_domains;
18689 
18690 		put_domains = modeset_get_crtc_power_domains(crtc_state);
18691 		if (drm_WARN_ON(dev, put_domains))
18692 			modeset_put_power_domains(dev_priv, put_domains);
18693 	}
18694 
18695 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
18696 }
18697 
18698 void intel_display_resume(struct drm_device *dev)
18699 {
18700 	struct drm_i915_private *dev_priv = to_i915(dev);
18701 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
18702 	struct drm_modeset_acquire_ctx ctx;
18703 	int ret;
18704 
18705 	dev_priv->modeset_restore_state = NULL;
18706 	if (state)
18707 		state->acquire_ctx = &ctx;
18708 
18709 	drm_modeset_acquire_init(&ctx, 0);
18710 
18711 	while (1) {
18712 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
18713 		if (ret != -EDEADLK)
18714 			break;
18715 
18716 		drm_modeset_backoff(&ctx);
18717 	}
18718 
18719 	if (!ret)
18720 		ret = __intel_display_resume(dev, state, &ctx);
18721 
18722 	intel_enable_ipc(dev_priv);
18723 	drm_modeset_drop_locks(&ctx);
18724 	drm_modeset_acquire_fini(&ctx);
18725 
18726 	if (ret)
18727 		drm_err(&dev_priv->drm,
18728 			"Restoring old state failed with %i\n", ret);
18729 	if (state)
18730 		drm_atomic_state_put(state);
18731 }
18732 
18733 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
18734 {
18735 	struct intel_connector *connector;
18736 	struct drm_connector_list_iter conn_iter;
18737 
18738 	/* Kill all the work that may have been queued by hpd. */
18739 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
18740 	for_each_intel_connector_iter(connector, &conn_iter) {
18741 		if (connector->modeset_retry_work.func)
18742 			cancel_work_sync(&connector->modeset_retry_work);
18743 		if (connector->hdcp.shim) {
18744 			cancel_delayed_work_sync(&connector->hdcp.check_work);
18745 			cancel_work_sync(&connector->hdcp.prop_work);
18746 		}
18747 	}
18748 	drm_connector_list_iter_end(&conn_iter);
18749 }
18750 
18751 /* part #1: call before irq uninstall */
18752 void intel_modeset_driver_remove(struct drm_i915_private *i915)
18753 {
18754 	flush_workqueue(i915->flip_wq);
18755 	flush_workqueue(i915->modeset_wq);
18756 
18757 	flush_work(&i915->atomic_helper.free_work);
18758 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
18759 }
18760 
18761 /* part #2: call after irq uninstall */
18762 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
18763 {
18764 	/*
18765 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
18766 	 * poll handlers. Hence disable polling after hpd handling is shut down.
18767 	 */
18768 	intel_hpd_poll_fini(i915);
18769 
18770 	/*
18771 	 * MST topology needs to be suspended so we don't have any calls to
18772 	 * fbdev after it's finalized. MST will be destroyed later as part of
18773 	 * drm_mode_config_cleanup()
18774 	 */
18775 	intel_dp_mst_suspend(i915);
18776 
18777 	/* poll work can call into fbdev, hence clean that up afterwards */
18778 	intel_fbdev_fini(i915);
18779 
18780 	intel_unregister_dsm_handler();
18781 
18782 	intel_fbc_global_disable(i915);
18783 
18784 	/* flush any delayed tasks or pending work */
18785 	flush_scheduled_work();
18786 
18787 	intel_hdcp_component_fini(i915);
18788 
18789 	intel_mode_config_cleanup(i915);
18790 
18791 	intel_overlay_cleanup(i915);
18792 
18793 	intel_gmbus_teardown(i915);
18794 
18795 	destroy_workqueue(i915->flip_wq);
18796 	destroy_workqueue(i915->modeset_wq);
18797 
18798 	intel_fbc_cleanup_cfb(i915);
18799 }
18800 
18801 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
18802 
18803 static bool
18804 has_transcoder(struct drm_i915_private *dev_priv, enum transcoder cpu_transcoder)
18805 {
18806 	if (cpu_transcoder == TRANSCODER_EDP)
18807 		return HAS_TRANSCODER_EDP(dev_priv);
18808 	else
18809 		return INTEL_INFO(dev_priv)->pipe_mask & BIT(cpu_transcoder);
18810 }
18811 
18812 struct intel_display_error_state {
18813 
18814 	u32 power_well_driver;
18815 
18816 	struct intel_cursor_error_state {
18817 		u32 control;
18818 		u32 position;
18819 		u32 base;
18820 		u32 size;
18821 	} cursor[I915_MAX_PIPES];
18822 
18823 	struct intel_pipe_error_state {
18824 		bool power_domain_on;
18825 		u32 source;
18826 		u32 stat;
18827 	} pipe[I915_MAX_PIPES];
18828 
18829 	struct intel_plane_error_state {
18830 		u32 control;
18831 		u32 stride;
18832 		u32 size;
18833 		u32 pos;
18834 		u32 addr;
18835 		u32 surface;
18836 		u32 tile_offset;
18837 	} plane[I915_MAX_PIPES];
18838 
18839 	struct intel_transcoder_error_state {
18840 		bool available;
18841 		bool power_domain_on;
18842 		enum transcoder cpu_transcoder;
18843 
18844 		u32 conf;
18845 
18846 		u32 htotal;
18847 		u32 hblank;
18848 		u32 hsync;
18849 		u32 vtotal;
18850 		u32 vblank;
18851 		u32 vsync;
18852 	} transcoder[5];
18853 };
18854 
18855 struct intel_display_error_state *
18856 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
18857 {
18858 	struct intel_display_error_state *error;
18859 	int transcoders[] = {
18860 		TRANSCODER_A,
18861 		TRANSCODER_B,
18862 		TRANSCODER_C,
18863 		TRANSCODER_D,
18864 		TRANSCODER_EDP,
18865 	};
18866 	int i;
18867 
18868 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
18869 
18870 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
18871 		return NULL;
18872 
18873 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
18874 	if (error == NULL)
18875 		return NULL;
18876 
18877 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18878 		error->power_well_driver = intel_de_read(dev_priv,
18879 							 HSW_PWR_WELL_CTL2);
18880 
18881 	for_each_pipe(dev_priv, i) {
18882 		error->pipe[i].power_domain_on =
18883 			__intel_display_power_is_enabled(dev_priv,
18884 							 POWER_DOMAIN_PIPE(i));
18885 		if (!error->pipe[i].power_domain_on)
18886 			continue;
18887 
18888 		error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
18889 		error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
18890 		error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
18891 
18892 		error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
18893 		error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
18894 		if (INTEL_GEN(dev_priv) <= 3) {
18895 			error->plane[i].size = intel_de_read(dev_priv,
18896 							     DSPSIZE(i));
18897 			error->plane[i].pos = intel_de_read(dev_priv,
18898 							    DSPPOS(i));
18899 		}
18900 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18901 			error->plane[i].addr = intel_de_read(dev_priv,
18902 							     DSPADDR(i));
18903 		if (INTEL_GEN(dev_priv) >= 4) {
18904 			error->plane[i].surface = intel_de_read(dev_priv,
18905 								DSPSURF(i));
18906 			error->plane[i].tile_offset = intel_de_read(dev_priv,
18907 								    DSPTILEOFF(i));
18908 		}
18909 
18910 		error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
18911 
18912 		if (HAS_GMCH(dev_priv))
18913 			error->pipe[i].stat = intel_de_read(dev_priv,
18914 							    PIPESTAT(i));
18915 	}
18916 
18917 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
18918 		enum transcoder cpu_transcoder = transcoders[i];
18919 
18920 		if (!has_transcoder(dev_priv, cpu_transcoder))
18921 			continue;
18922 
18923 		error->transcoder[i].available = true;
18924 		error->transcoder[i].power_domain_on =
18925 			__intel_display_power_is_enabled(dev_priv,
18926 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
18927 		if (!error->transcoder[i].power_domain_on)
18928 			continue;
18929 
18930 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
18931 
18932 		error->transcoder[i].conf = intel_de_read(dev_priv,
18933 							  PIPECONF(cpu_transcoder));
18934 		error->transcoder[i].htotal = intel_de_read(dev_priv,
18935 							    HTOTAL(cpu_transcoder));
18936 		error->transcoder[i].hblank = intel_de_read(dev_priv,
18937 							    HBLANK(cpu_transcoder));
18938 		error->transcoder[i].hsync = intel_de_read(dev_priv,
18939 							   HSYNC(cpu_transcoder));
18940 		error->transcoder[i].vtotal = intel_de_read(dev_priv,
18941 							    VTOTAL(cpu_transcoder));
18942 		error->transcoder[i].vblank = intel_de_read(dev_priv,
18943 							    VBLANK(cpu_transcoder));
18944 		error->transcoder[i].vsync = intel_de_read(dev_priv,
18945 							   VSYNC(cpu_transcoder));
18946 	}
18947 
18948 	return error;
18949 }
18950 
18951 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
18952 
18953 void
18954 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
18955 				struct intel_display_error_state *error)
18956 {
18957 	struct drm_i915_private *dev_priv = m->i915;
18958 	int i;
18959 
18960 	if (!error)
18961 		return;
18962 
18963 	err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
18964 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18965 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
18966 			   error->power_well_driver);
18967 	for_each_pipe(dev_priv, i) {
18968 		err_printf(m, "Pipe [%d]:\n", i);
18969 		err_printf(m, "  Power: %s\n",
18970 			   onoff(error->pipe[i].power_domain_on));
18971 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
18972 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
18973 
18974 		err_printf(m, "Plane [%d]:\n", i);
18975 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
18976 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
18977 		if (INTEL_GEN(dev_priv) <= 3) {
18978 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
18979 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
18980 		}
18981 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18982 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
18983 		if (INTEL_GEN(dev_priv) >= 4) {
18984 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
18985 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
18986 		}
18987 
18988 		err_printf(m, "Cursor [%d]:\n", i);
18989 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
18990 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
18991 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
18992 	}
18993 
18994 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
18995 		if (!error->transcoder[i].available)
18996 			continue;
18997 
18998 		err_printf(m, "CPU transcoder: %s\n",
18999 			   transcoder_name(error->transcoder[i].cpu_transcoder));
19000 		err_printf(m, "  Power: %s\n",
19001 			   onoff(error->transcoder[i].power_domain_on));
19002 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
19003 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
19004 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
19005 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
19006 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
19007 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
19008 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
19009 	}
19010 }
19011 
19012 #endif
19013