xref: /dragonfly/sys/dev/drm/i915/intel_display.c (revision 277350a0)
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/dmi.h>
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include <drm/drm_edid.h>
32 #include <drm/drmP.h>
33 #include "intel_drv.h"
34 #include <drm/i915_drm.h>
35 #include "i915_drv.h"
36 #include "i915_trace.h"
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_crtc_helper.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_rect.h>
43 
44 /* Primary plane formats for gen <= 3 */
45 static const uint32_t i8xx_primary_formats[] = {
46 	DRM_FORMAT_C8,
47 	DRM_FORMAT_RGB565,
48 	DRM_FORMAT_XRGB1555,
49 	DRM_FORMAT_XRGB8888,
50 };
51 
52 /* Primary plane formats for gen >= 4 */
53 static const uint32_t i965_primary_formats[] = {
54 	DRM_FORMAT_C8,
55 	DRM_FORMAT_RGB565,
56 	DRM_FORMAT_XRGB8888,
57 	DRM_FORMAT_XBGR8888,
58 	DRM_FORMAT_XRGB2101010,
59 	DRM_FORMAT_XBGR2101010,
60 };
61 
62 static const uint32_t skl_primary_formats[] = {
63 	DRM_FORMAT_C8,
64 	DRM_FORMAT_RGB565,
65 	DRM_FORMAT_XRGB8888,
66 	DRM_FORMAT_XBGR8888,
67 	DRM_FORMAT_ARGB8888,
68 	DRM_FORMAT_ABGR8888,
69 	DRM_FORMAT_XRGB2101010,
70 	DRM_FORMAT_XBGR2101010,
71 	DRM_FORMAT_YUYV,
72 	DRM_FORMAT_YVYU,
73 	DRM_FORMAT_UYVY,
74 	DRM_FORMAT_VYUY,
75 };
76 
77 /* Cursor formats */
78 static const uint32_t intel_cursor_formats[] = {
79 	DRM_FORMAT_ARGB8888,
80 };
81 
82 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
83 
84 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
85 				struct intel_crtc_state *pipe_config);
86 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
87 				   struct intel_crtc_state *pipe_config);
88 
89 static int intel_framebuffer_init(struct drm_device *dev,
90 				  struct intel_framebuffer *ifb,
91 				  struct drm_mode_fb_cmd2 *mode_cmd,
92 				  struct drm_i915_gem_object *obj);
93 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
94 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
95 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
96 					 struct intel_link_m_n *m_n,
97 					 struct intel_link_m_n *m2_n2);
98 static void ironlake_set_pipeconf(struct drm_crtc *crtc);
99 static void haswell_set_pipeconf(struct drm_crtc *crtc);
100 static void intel_set_pipe_csc(struct drm_crtc *crtc);
101 static void vlv_prepare_pll(struct intel_crtc *crtc,
102 			    const struct intel_crtc_state *pipe_config);
103 static void chv_prepare_pll(struct intel_crtc *crtc,
104 			    const struct intel_crtc_state *pipe_config);
105 static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
106 static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
107 static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
108 	struct intel_crtc_state *crtc_state);
109 static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
110 			   int num_connectors);
111 static void skylake_pfit_enable(struct intel_crtc *crtc);
112 static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
113 static void ironlake_pfit_enable(struct intel_crtc *crtc);
114 static void intel_modeset_setup_hw_state(struct drm_device *dev);
115 static void intel_pre_disable_primary(struct drm_crtc *crtc);
116 
117 typedef struct {
118 	int	min, max;
119 } intel_range_t;
120 
121 typedef struct {
122 	int	dot_limit;
123 	int	p2_slow, p2_fast;
124 } intel_p2_t;
125 
126 typedef struct intel_limit intel_limit_t;
127 struct intel_limit {
128 	intel_range_t   dot, vco, n, m, m1, m2, p, p1;
129 	intel_p2_t	    p2;
130 };
131 
132 /* returns HPLL frequency in kHz */
133 static int valleyview_get_vco(struct drm_i915_private *dev_priv)
134 {
135 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
136 
137 	/* Obtain SKU information */
138 	mutex_lock(&dev_priv->sb_lock);
139 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
140 		CCK_FUSE_HPLL_FREQ_MASK;
141 	mutex_unlock(&dev_priv->sb_lock);
142 
143 	return vco_freq[hpll_freq] * 1000;
144 }
145 
146 static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
147 				  const char *name, u32 reg)
148 {
149 	u32 val;
150 	int divider;
151 
152 	if (dev_priv->hpll_freq == 0)
153 		dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
154 
155 	mutex_lock(&dev_priv->sb_lock);
156 	val = vlv_cck_read(dev_priv, reg);
157 	mutex_unlock(&dev_priv->sb_lock);
158 
159 	divider = val & CCK_FREQUENCY_VALUES;
160 
161 	WARN((val & CCK_FREQUENCY_STATUS) !=
162 	     (divider << CCK_FREQUENCY_STATUS_SHIFT),
163 	     "%s change in progress\n", name);
164 
165 	return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
166 }
167 
168 int
169 intel_pch_rawclk(struct drm_device *dev)
170 {
171 	struct drm_i915_private *dev_priv = dev->dev_private;
172 
173 	WARN_ON(!HAS_PCH_SPLIT(dev));
174 
175 	return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
176 }
177 
178 /* hrawclock is 1/4 the FSB frequency */
179 int intel_hrawclk(struct drm_device *dev)
180 {
181 	struct drm_i915_private *dev_priv = dev->dev_private;
182 	uint32_t clkcfg;
183 
184 	/* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
185 	if (IS_VALLEYVIEW(dev))
186 		return 200;
187 
188 	clkcfg = I915_READ(CLKCFG);
189 	switch (clkcfg & CLKCFG_FSB_MASK) {
190 	case CLKCFG_FSB_400:
191 		return 100;
192 	case CLKCFG_FSB_533:
193 		return 133;
194 	case CLKCFG_FSB_667:
195 		return 166;
196 	case CLKCFG_FSB_800:
197 		return 200;
198 	case CLKCFG_FSB_1067:
199 		return 266;
200 	case CLKCFG_FSB_1333:
201 		return 333;
202 	/* these two are just a guess; one of them might be right */
203 	case CLKCFG_FSB_1600:
204 	case CLKCFG_FSB_1600_ALT:
205 		return 400;
206 	default:
207 		return 133;
208 	}
209 }
210 
211 static void intel_update_czclk(struct drm_i915_private *dev_priv)
212 {
213 	if (!IS_VALLEYVIEW(dev_priv))
214 		return;
215 
216 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
217 						      CCK_CZ_CLOCK_CONTROL);
218 
219 	DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
220 }
221 
222 static inline u32 /* units of 100MHz */
223 intel_fdi_link_freq(struct drm_device *dev)
224 {
225 	if (IS_GEN5(dev)) {
226 		struct drm_i915_private *dev_priv = dev->dev_private;
227 		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
228 	} else
229 		return 27;
230 }
231 
232 static const intel_limit_t intel_limits_i8xx_dac = {
233 	.dot = { .min = 25000, .max = 350000 },
234 	.vco = { .min = 908000, .max = 1512000 },
235 	.n = { .min = 2, .max = 16 },
236 	.m = { .min = 96, .max = 140 },
237 	.m1 = { .min = 18, .max = 26 },
238 	.m2 = { .min = 6, .max = 16 },
239 	.p = { .min = 4, .max = 128 },
240 	.p1 = { .min = 2, .max = 33 },
241 	.p2 = { .dot_limit = 165000,
242 		.p2_slow = 4, .p2_fast = 2 },
243 };
244 
245 static const intel_limit_t intel_limits_i8xx_dvo = {
246 	.dot = { .min = 25000, .max = 350000 },
247 	.vco = { .min = 908000, .max = 1512000 },
248 	.n = { .min = 2, .max = 16 },
249 	.m = { .min = 96, .max = 140 },
250 	.m1 = { .min = 18, .max = 26 },
251 	.m2 = { .min = 6, .max = 16 },
252 	.p = { .min = 4, .max = 128 },
253 	.p1 = { .min = 2, .max = 33 },
254 	.p2 = { .dot_limit = 165000,
255 		.p2_slow = 4, .p2_fast = 4 },
256 };
257 
258 static const intel_limit_t intel_limits_i8xx_lvds = {
259 	.dot = { .min = 25000, .max = 350000 },
260 	.vco = { .min = 908000, .max = 1512000 },
261 	.n = { .min = 2, .max = 16 },
262 	.m = { .min = 96, .max = 140 },
263 	.m1 = { .min = 18, .max = 26 },
264 	.m2 = { .min = 6, .max = 16 },
265 	.p = { .min = 4, .max = 128 },
266 	.p1 = { .min = 1, .max = 6 },
267 	.p2 = { .dot_limit = 165000,
268 		.p2_slow = 14, .p2_fast = 7 },
269 };
270 
271 static const intel_limit_t intel_limits_i9xx_sdvo = {
272 	.dot = { .min = 20000, .max = 400000 },
273 	.vco = { .min = 1400000, .max = 2800000 },
274 	.n = { .min = 1, .max = 6 },
275 	.m = { .min = 70, .max = 120 },
276 	.m1 = { .min = 8, .max = 18 },
277 	.m2 = { .min = 3, .max = 7 },
278 	.p = { .min = 5, .max = 80 },
279 	.p1 = { .min = 1, .max = 8 },
280 	.p2 = { .dot_limit = 200000,
281 		.p2_slow = 10, .p2_fast = 5 },
282 };
283 
284 static const intel_limit_t intel_limits_i9xx_lvds = {
285 	.dot = { .min = 20000, .max = 400000 },
286 	.vco = { .min = 1400000, .max = 2800000 },
287 	.n = { .min = 1, .max = 6 },
288 	.m = { .min = 70, .max = 120 },
289 	.m1 = { .min = 8, .max = 18 },
290 	.m2 = { .min = 3, .max = 7 },
291 	.p = { .min = 7, .max = 98 },
292 	.p1 = { .min = 1, .max = 8 },
293 	.p2 = { .dot_limit = 112000,
294 		.p2_slow = 14, .p2_fast = 7 },
295 };
296 
297 
298 static const intel_limit_t intel_limits_g4x_sdvo = {
299 	.dot = { .min = 25000, .max = 270000 },
300 	.vco = { .min = 1750000, .max = 3500000},
301 	.n = { .min = 1, .max = 4 },
302 	.m = { .min = 104, .max = 138 },
303 	.m1 = { .min = 17, .max = 23 },
304 	.m2 = { .min = 5, .max = 11 },
305 	.p = { .min = 10, .max = 30 },
306 	.p1 = { .min = 1, .max = 3},
307 	.p2 = { .dot_limit = 270000,
308 		.p2_slow = 10,
309 		.p2_fast = 10
310 	},
311 };
312 
313 static const intel_limit_t intel_limits_g4x_hdmi = {
314 	.dot = { .min = 22000, .max = 400000 },
315 	.vco = { .min = 1750000, .max = 3500000},
316 	.n = { .min = 1, .max = 4 },
317 	.m = { .min = 104, .max = 138 },
318 	.m1 = { .min = 16, .max = 23 },
319 	.m2 = { .min = 5, .max = 11 },
320 	.p = { .min = 5, .max = 80 },
321 	.p1 = { .min = 1, .max = 8},
322 	.p2 = { .dot_limit = 165000,
323 		.p2_slow = 10, .p2_fast = 5 },
324 };
325 
326 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
327 	.dot = { .min = 20000, .max = 115000 },
328 	.vco = { .min = 1750000, .max = 3500000 },
329 	.n = { .min = 1, .max = 3 },
330 	.m = { .min = 104, .max = 138 },
331 	.m1 = { .min = 17, .max = 23 },
332 	.m2 = { .min = 5, .max = 11 },
333 	.p = { .min = 28, .max = 112 },
334 	.p1 = { .min = 2, .max = 8 },
335 	.p2 = { .dot_limit = 0,
336 		.p2_slow = 14, .p2_fast = 14
337 	},
338 };
339 
340 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
341 	.dot = { .min = 80000, .max = 224000 },
342 	.vco = { .min = 1750000, .max = 3500000 },
343 	.n = { .min = 1, .max = 3 },
344 	.m = { .min = 104, .max = 138 },
345 	.m1 = { .min = 17, .max = 23 },
346 	.m2 = { .min = 5, .max = 11 },
347 	.p = { .min = 14, .max = 42 },
348 	.p1 = { .min = 2, .max = 6 },
349 	.p2 = { .dot_limit = 0,
350 		.p2_slow = 7, .p2_fast = 7
351 	},
352 };
353 
354 static const intel_limit_t intel_limits_pineview_sdvo = {
355 	.dot = { .min = 20000, .max = 400000},
356 	.vco = { .min = 1700000, .max = 3500000 },
357 	/* Pineview's Ncounter is a ring counter */
358 	.n = { .min = 3, .max = 6 },
359 	.m = { .min = 2, .max = 256 },
360 	/* Pineview only has one combined m divider, which we treat as m2. */
361 	.m1 = { .min = 0, .max = 0 },
362 	.m2 = { .min = 0, .max = 254 },
363 	.p = { .min = 5, .max = 80 },
364 	.p1 = { .min = 1, .max = 8 },
365 	.p2 = { .dot_limit = 200000,
366 		.p2_slow = 10, .p2_fast = 5 },
367 };
368 
369 static const intel_limit_t intel_limits_pineview_lvds = {
370 	.dot = { .min = 20000, .max = 400000 },
371 	.vco = { .min = 1700000, .max = 3500000 },
372 	.n = { .min = 3, .max = 6 },
373 	.m = { .min = 2, .max = 256 },
374 	.m1 = { .min = 0, .max = 0 },
375 	.m2 = { .min = 0, .max = 254 },
376 	.p = { .min = 7, .max = 112 },
377 	.p1 = { .min = 1, .max = 8 },
378 	.p2 = { .dot_limit = 112000,
379 		.p2_slow = 14, .p2_fast = 14 },
380 };
381 
382 /* Ironlake / Sandybridge
383  *
384  * We calculate clock using (register_value + 2) for N/M1/M2, so here
385  * the range value for them is (actual_value - 2).
386  */
387 static const intel_limit_t intel_limits_ironlake_dac = {
388 	.dot = { .min = 25000, .max = 350000 },
389 	.vco = { .min = 1760000, .max = 3510000 },
390 	.n = { .min = 1, .max = 5 },
391 	.m = { .min = 79, .max = 127 },
392 	.m1 = { .min = 12, .max = 22 },
393 	.m2 = { .min = 5, .max = 9 },
394 	.p = { .min = 5, .max = 80 },
395 	.p1 = { .min = 1, .max = 8 },
396 	.p2 = { .dot_limit = 225000,
397 		.p2_slow = 10, .p2_fast = 5 },
398 };
399 
400 static const intel_limit_t intel_limits_ironlake_single_lvds = {
401 	.dot = { .min = 25000, .max = 350000 },
402 	.vco = { .min = 1760000, .max = 3510000 },
403 	.n = { .min = 1, .max = 3 },
404 	.m = { .min = 79, .max = 118 },
405 	.m1 = { .min = 12, .max = 22 },
406 	.m2 = { .min = 5, .max = 9 },
407 	.p = { .min = 28, .max = 112 },
408 	.p1 = { .min = 2, .max = 8 },
409 	.p2 = { .dot_limit = 225000,
410 		.p2_slow = 14, .p2_fast = 14 },
411 };
412 
413 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
414 	.dot = { .min = 25000, .max = 350000 },
415 	.vco = { .min = 1760000, .max = 3510000 },
416 	.n = { .min = 1, .max = 3 },
417 	.m = { .min = 79, .max = 127 },
418 	.m1 = { .min = 12, .max = 22 },
419 	.m2 = { .min = 5, .max = 9 },
420 	.p = { .min = 14, .max = 56 },
421 	.p1 = { .min = 2, .max = 8 },
422 	.p2 = { .dot_limit = 225000,
423 		.p2_slow = 7, .p2_fast = 7 },
424 };
425 
426 /* LVDS 100mhz refclk limits. */
427 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
428 	.dot = { .min = 25000, .max = 350000 },
429 	.vco = { .min = 1760000, .max = 3510000 },
430 	.n = { .min = 1, .max = 2 },
431 	.m = { .min = 79, .max = 126 },
432 	.m1 = { .min = 12, .max = 22 },
433 	.m2 = { .min = 5, .max = 9 },
434 	.p = { .min = 28, .max = 112 },
435 	.p1 = { .min = 2, .max = 8 },
436 	.p2 = { .dot_limit = 225000,
437 		.p2_slow = 14, .p2_fast = 14 },
438 };
439 
440 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
441 	.dot = { .min = 25000, .max = 350000 },
442 	.vco = { .min = 1760000, .max = 3510000 },
443 	.n = { .min = 1, .max = 3 },
444 	.m = { .min = 79, .max = 126 },
445 	.m1 = { .min = 12, .max = 22 },
446 	.m2 = { .min = 5, .max = 9 },
447 	.p = { .min = 14, .max = 42 },
448 	.p1 = { .min = 2, .max = 6 },
449 	.p2 = { .dot_limit = 225000,
450 		.p2_slow = 7, .p2_fast = 7 },
451 };
452 
453 static const intel_limit_t intel_limits_vlv = {
454 	 /*
455 	  * These are the data rate limits (measured in fast clocks)
456 	  * since those are the strictest limits we have. The fast
457 	  * clock and actual rate limits are more relaxed, so checking
458 	  * them would make no difference.
459 	  */
460 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
461 	.vco = { .min = 4000000, .max = 6000000 },
462 	.n = { .min = 1, .max = 7 },
463 	.m1 = { .min = 2, .max = 3 },
464 	.m2 = { .min = 11, .max = 156 },
465 	.p1 = { .min = 2, .max = 3 },
466 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
467 };
468 
469 static const intel_limit_t intel_limits_chv = {
470 	/*
471 	 * These are the data rate limits (measured in fast clocks)
472 	 * since those are the strictest limits we have.  The fast
473 	 * clock and actual rate limits are more relaxed, so checking
474 	 * them would make no difference.
475 	 */
476 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
477 	.vco = { .min = 4800000, .max = 6480000 },
478 	.n = { .min = 1, .max = 1 },
479 	.m1 = { .min = 2, .max = 2 },
480 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
481 	.p1 = { .min = 2, .max = 4 },
482 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
483 };
484 
485 static const intel_limit_t intel_limits_bxt = {
486 	/* FIXME: find real dot limits */
487 	.dot = { .min = 0, .max = INT_MAX },
488 	.vco = { .min = 4800000, .max = 6700000 },
489 	.n = { .min = 1, .max = 1 },
490 	.m1 = { .min = 2, .max = 2 },
491 	/* FIXME: find real m2 limits */
492 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
493 	.p1 = { .min = 2, .max = 4 },
494 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
495 };
496 
497 static bool
498 needs_modeset(struct drm_crtc_state *state)
499 {
500 	return drm_atomic_crtc_needs_modeset(state);
501 }
502 
503 /**
504  * Returns whether any output on the specified pipe is of the specified type
505  */
506 bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
507 {
508 	struct drm_device *dev = crtc->base.dev;
509 	struct intel_encoder *encoder;
510 
511 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
512 		if (encoder->type == type)
513 			return true;
514 
515 	return false;
516 }
517 
518 /**
519  * Returns whether any output on the specified pipe will have the specified
520  * type after a staged modeset is complete, i.e., the same as
521  * intel_pipe_has_type() but looking at encoder->new_crtc instead of
522  * encoder->crtc.
523  */
524 static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
525 				      int type)
526 {
527 	struct drm_atomic_state *state = crtc_state->base.state;
528 	struct drm_connector *connector;
529 	struct drm_connector_state *connector_state;
530 	struct intel_encoder *encoder;
531 	int i, num_connectors = 0;
532 
533 	for_each_connector_in_state(state, connector, connector_state, i) {
534 		if (connector_state->crtc != crtc_state->base.crtc)
535 			continue;
536 
537 		num_connectors++;
538 
539 		encoder = to_intel_encoder(connector_state->best_encoder);
540 		if (encoder->type == type)
541 			return true;
542 	}
543 
544 	WARN_ON(num_connectors == 0);
545 
546 	return false;
547 }
548 
549 static const intel_limit_t *
550 intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
551 {
552 	struct drm_device *dev = crtc_state->base.crtc->dev;
553 	const intel_limit_t *limit;
554 
555 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
556 		if (intel_is_dual_link_lvds(dev)) {
557 			if (refclk == 100000)
558 				limit = &intel_limits_ironlake_dual_lvds_100m;
559 			else
560 				limit = &intel_limits_ironlake_dual_lvds;
561 		} else {
562 			if (refclk == 100000)
563 				limit = &intel_limits_ironlake_single_lvds_100m;
564 			else
565 				limit = &intel_limits_ironlake_single_lvds;
566 		}
567 	} else
568 		limit = &intel_limits_ironlake_dac;
569 
570 	return limit;
571 }
572 
573 static const intel_limit_t *
574 intel_g4x_limit(struct intel_crtc_state *crtc_state)
575 {
576 	struct drm_device *dev = crtc_state->base.crtc->dev;
577 	const intel_limit_t *limit;
578 
579 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
580 		if (intel_is_dual_link_lvds(dev))
581 			limit = &intel_limits_g4x_dual_channel_lvds;
582 		else
583 			limit = &intel_limits_g4x_single_channel_lvds;
584 	} else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
585 		   intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
586 		limit = &intel_limits_g4x_hdmi;
587 	} else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
588 		limit = &intel_limits_g4x_sdvo;
589 	} else /* The option is for other outputs */
590 		limit = &intel_limits_i9xx_sdvo;
591 
592 	return limit;
593 }
594 
595 static const intel_limit_t *
596 intel_limit(struct intel_crtc_state *crtc_state, int refclk)
597 {
598 	struct drm_device *dev = crtc_state->base.crtc->dev;
599 	const intel_limit_t *limit;
600 
601 	if (IS_BROXTON(dev))
602 		limit = &intel_limits_bxt;
603 	else if (HAS_PCH_SPLIT(dev))
604 		limit = intel_ironlake_limit(crtc_state, refclk);
605 	else if (IS_G4X(dev)) {
606 		limit = intel_g4x_limit(crtc_state);
607 	} else if (IS_PINEVIEW(dev)) {
608 		if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
609 			limit = &intel_limits_pineview_lvds;
610 		else
611 			limit = &intel_limits_pineview_sdvo;
612 	} else if (IS_CHERRYVIEW(dev)) {
613 		limit = &intel_limits_chv;
614 	} else if (IS_VALLEYVIEW(dev)) {
615 		limit = &intel_limits_vlv;
616 	} else if (!IS_GEN2(dev)) {
617 		if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
618 			limit = &intel_limits_i9xx_lvds;
619 		else
620 			limit = &intel_limits_i9xx_sdvo;
621 	} else {
622 		if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
623 			limit = &intel_limits_i8xx_lvds;
624 		else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
625 			limit = &intel_limits_i8xx_dvo;
626 		else
627 			limit = &intel_limits_i8xx_dac;
628 	}
629 	return limit;
630 }
631 
632 /*
633  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
634  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
635  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
636  * The helpers' return value is the rate of the clock that is fed to the
637  * display engine's pipe which can be the above fast dot clock rate or a
638  * divided-down version of it.
639  */
640 /* m1 is reserved as 0 in Pineview, n is a ring counter */
641 static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
642 {
643 	clock->m = clock->m2 + 2;
644 	clock->p = clock->p1 * clock->p2;
645 	if (WARN_ON(clock->n == 0 || clock->p == 0))
646 		return 0;
647 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
648 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
649 
650 	return clock->dot;
651 }
652 
653 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
654 {
655 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
656 }
657 
658 static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
659 {
660 	clock->m = i9xx_dpll_compute_m(clock);
661 	clock->p = clock->p1 * clock->p2;
662 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
663 		return 0;
664 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
665 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
666 
667 	return clock->dot;
668 }
669 
670 static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
671 {
672 	clock->m = clock->m1 * clock->m2;
673 	clock->p = clock->p1 * clock->p2;
674 	if (WARN_ON(clock->n == 0 || clock->p == 0))
675 		return 0;
676 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
677 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
678 
679 	return clock->dot / 5;
680 }
681 
682 int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
683 {
684 	clock->m = clock->m1 * clock->m2;
685 	clock->p = clock->p1 * clock->p2;
686 	if (WARN_ON(clock->n == 0 || clock->p == 0))
687 		return 0;
688 	clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
689 			clock->n << 22);
690 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
691 
692 	return clock->dot / 5;
693 }
694 
695 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
696 /**
697  * Returns whether the given set of divisors are valid for a given refclk with
698  * the given connectors.
699  */
700 
701 static bool intel_PLL_is_valid(struct drm_device *dev,
702 			       const intel_limit_t *limit,
703 			       const intel_clock_t *clock)
704 {
705 	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
706 		INTELPllInvalid("n out of range\n");
707 	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
708 		INTELPllInvalid("p1 out of range\n");
709 	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
710 		INTELPllInvalid("m2 out of range\n");
711 	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
712 		INTELPllInvalid("m1 out of range\n");
713 
714 	if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
715 		if (clock->m1 <= clock->m2)
716 			INTELPllInvalid("m1 <= m2\n");
717 
718 	if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
719 		if (clock->p < limit->p.min || limit->p.max < clock->p)
720 			INTELPllInvalid("p out of range\n");
721 		if (clock->m < limit->m.min || limit->m.max < clock->m)
722 			INTELPllInvalid("m out of range\n");
723 	}
724 
725 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
726 		INTELPllInvalid("vco out of range\n");
727 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
728 	 * connector, etc., rather than just a single range.
729 	 */
730 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
731 		INTELPllInvalid("dot out of range\n");
732 
733 	return true;
734 }
735 
736 static int
737 i9xx_select_p2_div(const intel_limit_t *limit,
738 		   const struct intel_crtc_state *crtc_state,
739 		   int target)
740 {
741 	struct drm_device *dev = crtc_state->base.crtc->dev;
742 
743 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
744 		/*
745 		 * For LVDS just rely on its current settings for dual-channel.
746 		 * We haven't figured out how to reliably set up different
747 		 * single/dual channel state, if we even can.
748 		 */
749 		if (intel_is_dual_link_lvds(dev))
750 			return limit->p2.p2_fast;
751 		else
752 			return limit->p2.p2_slow;
753 	} else {
754 		if (target < limit->p2.dot_limit)
755 			return limit->p2.p2_slow;
756 		else
757 			return limit->p2.p2_fast;
758 	}
759 }
760 
761 static bool
762 i9xx_find_best_dpll(const intel_limit_t *limit,
763 		    struct intel_crtc_state *crtc_state,
764 		    int target, int refclk, intel_clock_t *match_clock,
765 		    intel_clock_t *best_clock)
766 {
767 	struct drm_device *dev = crtc_state->base.crtc->dev;
768 	intel_clock_t clock;
769 	int err = target;
770 
771 	memset(best_clock, 0, sizeof(*best_clock));
772 
773 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
774 
775 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
776 	     clock.m1++) {
777 		for (clock.m2 = limit->m2.min;
778 		     clock.m2 <= limit->m2.max; clock.m2++) {
779 			if (clock.m2 >= clock.m1)
780 				break;
781 			for (clock.n = limit->n.min;
782 			     clock.n <= limit->n.max; clock.n++) {
783 				for (clock.p1 = limit->p1.min;
784 					clock.p1 <= limit->p1.max; clock.p1++) {
785 					int this_err;
786 
787 					i9xx_calc_dpll_params(refclk, &clock);
788 					if (!intel_PLL_is_valid(dev, limit,
789 								&clock))
790 						continue;
791 					if (match_clock &&
792 					    clock.p != match_clock->p)
793 						continue;
794 
795 					this_err = abs(clock.dot - target);
796 					if (this_err < err) {
797 						*best_clock = clock;
798 						err = this_err;
799 					}
800 				}
801 			}
802 		}
803 	}
804 
805 	return (err != target);
806 }
807 
808 static bool
809 pnv_find_best_dpll(const intel_limit_t *limit,
810 		   struct intel_crtc_state *crtc_state,
811 		   int target, int refclk, intel_clock_t *match_clock,
812 		   intel_clock_t *best_clock)
813 {
814 	struct drm_device *dev = crtc_state->base.crtc->dev;
815 	intel_clock_t clock;
816 	int err = target;
817 
818 	memset(best_clock, 0, sizeof(*best_clock));
819 
820 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
821 
822 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
823 	     clock.m1++) {
824 		for (clock.m2 = limit->m2.min;
825 		     clock.m2 <= limit->m2.max; clock.m2++) {
826 			for (clock.n = limit->n.min;
827 			     clock.n <= limit->n.max; clock.n++) {
828 				for (clock.p1 = limit->p1.min;
829 					clock.p1 <= limit->p1.max; clock.p1++) {
830 					int this_err;
831 
832 					pnv_calc_dpll_params(refclk, &clock);
833 					if (!intel_PLL_is_valid(dev, limit,
834 								&clock))
835 						continue;
836 					if (match_clock &&
837 					    clock.p != match_clock->p)
838 						continue;
839 
840 					this_err = abs(clock.dot - target);
841 					if (this_err < err) {
842 						*best_clock = clock;
843 						err = this_err;
844 					}
845 				}
846 			}
847 		}
848 	}
849 
850 	return (err != target);
851 }
852 
853 static bool
854 g4x_find_best_dpll(const intel_limit_t *limit,
855 		   struct intel_crtc_state *crtc_state,
856 		   int target, int refclk, intel_clock_t *match_clock,
857 		   intel_clock_t *best_clock)
858 {
859 	struct drm_device *dev = crtc_state->base.crtc->dev;
860 	intel_clock_t clock;
861 	int max_n;
862 	bool found = false;
863 	/* approximately equals target * 0.00585 */
864 	int err_most = (target >> 8) + (target >> 9);
865 
866 	memset(best_clock, 0, sizeof(*best_clock));
867 
868 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
869 
870 	max_n = limit->n.max;
871 	/* based on hardware requirement, prefer smaller n to precision */
872 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
873 		/* based on hardware requirement, prefere larger m1,m2 */
874 		for (clock.m1 = limit->m1.max;
875 		     clock.m1 >= limit->m1.min; clock.m1--) {
876 			for (clock.m2 = limit->m2.max;
877 			     clock.m2 >= limit->m2.min; clock.m2--) {
878 				for (clock.p1 = limit->p1.max;
879 				     clock.p1 >= limit->p1.min; clock.p1--) {
880 					int this_err;
881 
882 					i9xx_calc_dpll_params(refclk, &clock);
883 					if (!intel_PLL_is_valid(dev, limit,
884 								&clock))
885 						continue;
886 
887 					this_err = abs(clock.dot - target);
888 					if (this_err < err_most) {
889 						*best_clock = clock;
890 						err_most = this_err;
891 						max_n = clock.n;
892 						found = true;
893 					}
894 				}
895 			}
896 		}
897 	}
898 	return found;
899 }
900 
901 /*
902  * Check if the calculated PLL configuration is more optimal compared to the
903  * best configuration and error found so far. Return the calculated error.
904  */
905 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
906 			       const intel_clock_t *calculated_clock,
907 			       const intel_clock_t *best_clock,
908 			       unsigned int best_error_ppm,
909 			       unsigned int *error_ppm)
910 {
911 	/*
912 	 * For CHV ignore the error and consider only the P value.
913 	 * Prefer a bigger P value based on HW requirements.
914 	 */
915 	if (IS_CHERRYVIEW(dev)) {
916 		*error_ppm = 0;
917 
918 		return calculated_clock->p > best_clock->p;
919 	}
920 
921 	if (WARN_ON_ONCE(!target_freq))
922 		return false;
923 
924 	*error_ppm = div_u64(1000000ULL *
925 				abs(target_freq - calculated_clock->dot),
926 			     target_freq);
927 	/*
928 	 * Prefer a better P value over a better (smaller) error if the error
929 	 * is small. Ensure this preference for future configurations too by
930 	 * setting the error to 0.
931 	 */
932 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
933 		*error_ppm = 0;
934 
935 		return true;
936 	}
937 
938 	return *error_ppm + 10 < best_error_ppm;
939 }
940 
941 static bool
942 vlv_find_best_dpll(const intel_limit_t *limit,
943 		   struct intel_crtc_state *crtc_state,
944 		   int target, int refclk, intel_clock_t *match_clock,
945 		   intel_clock_t *best_clock)
946 {
947 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
948 	struct drm_device *dev = crtc->base.dev;
949 	intel_clock_t clock;
950 	unsigned int bestppm = 1000000;
951 	/* min update 19.2 MHz */
952 	int max_n = min(limit->n.max, refclk / 19200);
953 	bool found = false;
954 
955 	target *= 5; /* fast clock */
956 
957 	memset(best_clock, 0, sizeof(*best_clock));
958 
959 	/* based on hardware requirement, prefer smaller n to precision */
960 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
961 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
962 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
963 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
964 				clock.p = clock.p1 * clock.p2;
965 				/* based on hardware requirement, prefer bigger m1,m2 values */
966 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
967 					unsigned int ppm;
968 
969 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
970 								     refclk * clock.m1);
971 
972 					vlv_calc_dpll_params(refclk, &clock);
973 
974 					if (!intel_PLL_is_valid(dev, limit,
975 								&clock))
976 						continue;
977 
978 					if (!vlv_PLL_is_optimal(dev, target,
979 								&clock,
980 								best_clock,
981 								bestppm, &ppm))
982 						continue;
983 
984 					*best_clock = clock;
985 					bestppm = ppm;
986 					found = true;
987 				}
988 			}
989 		}
990 	}
991 
992 	return found;
993 }
994 
995 static bool
996 chv_find_best_dpll(const intel_limit_t *limit,
997 		   struct intel_crtc_state *crtc_state,
998 		   int target, int refclk, intel_clock_t *match_clock,
999 		   intel_clock_t *best_clock)
1000 {
1001 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1002 	struct drm_device *dev = crtc->base.dev;
1003 	unsigned int best_error_ppm;
1004 	intel_clock_t clock;
1005 	uint64_t m2;
1006 	int found = false;
1007 
1008 	memset(best_clock, 0, sizeof(*best_clock));
1009 	best_error_ppm = 1000000;
1010 
1011 	/*
1012 	 * Based on hardware doc, the n always set to 1, and m1 always
1013 	 * set to 2.  If requires to support 200Mhz refclk, we need to
1014 	 * revisit this because n may not 1 anymore.
1015 	 */
1016 	clock.n = 1, clock.m1 = 2;
1017 	target *= 5;	/* fast clock */
1018 
1019 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1020 		for (clock.p2 = limit->p2.p2_fast;
1021 				clock.p2 >= limit->p2.p2_slow;
1022 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
1023 			unsigned int error_ppm;
1024 
1025 			clock.p = clock.p1 * clock.p2;
1026 
1027 			m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
1028 					clock.n) << 22, refclk * clock.m1);
1029 
1030 			if (m2 > INT_MAX/clock.m1)
1031 				continue;
1032 
1033 			clock.m2 = m2;
1034 
1035 			chv_calc_dpll_params(refclk, &clock);
1036 
1037 			if (!intel_PLL_is_valid(dev, limit, &clock))
1038 				continue;
1039 
1040 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1041 						best_error_ppm, &error_ppm))
1042 				continue;
1043 
1044 			*best_clock = clock;
1045 			best_error_ppm = error_ppm;
1046 			found = true;
1047 		}
1048 	}
1049 
1050 	return found;
1051 }
1052 
1053 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1054 			intel_clock_t *best_clock)
1055 {
1056 	int refclk = i9xx_get_refclk(crtc_state, 0);
1057 
1058 	return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1059 				  target_clock, refclk, NULL, best_clock);
1060 }
1061 
1062 bool intel_crtc_active(struct drm_crtc *crtc)
1063 {
1064 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1065 
1066 	/* Be paranoid as we can arrive here with only partial
1067 	 * state retrieved from the hardware during setup.
1068 	 *
1069 	 * We can ditch the adjusted_mode.crtc_clock check as soon
1070 	 * as Haswell has gained clock readout/fastboot support.
1071 	 *
1072 	 * We can ditch the crtc->primary->fb check as soon as we can
1073 	 * properly reconstruct framebuffers.
1074 	 *
1075 	 * FIXME: The intel_crtc->active here should be switched to
1076 	 * crtc->state->active once we have proper CRTC states wired up
1077 	 * for atomic.
1078 	 */
1079 	return intel_crtc->active && crtc->primary->state->fb &&
1080 		intel_crtc->config->base.adjusted_mode.crtc_clock;
1081 }
1082 
1083 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1084 					     enum i915_pipe pipe)
1085 {
1086 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1087 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1088 
1089 	return intel_crtc->config->cpu_transcoder;
1090 }
1091 
1092 static bool pipe_dsl_stopped(struct drm_device *dev, enum i915_pipe pipe)
1093 {
1094 	struct drm_i915_private *dev_priv = dev->dev_private;
1095 	u32 reg = PIPEDSL(pipe);
1096 	u32 line1, line2;
1097 	u32 line_mask;
1098 
1099 	if (IS_GEN2(dev))
1100 		line_mask = DSL_LINEMASK_GEN2;
1101 	else
1102 		line_mask = DSL_LINEMASK_GEN3;
1103 
1104 	line1 = I915_READ(reg) & line_mask;
1105 	msleep(5);
1106 	line2 = I915_READ(reg) & line_mask;
1107 
1108 	return line1 == line2;
1109 }
1110 
1111 /*
1112  * intel_wait_for_pipe_off - wait for pipe to turn off
1113  * @crtc: crtc whose pipe to wait for
1114  *
1115  * After disabling a pipe, we can't wait for vblank in the usual way,
1116  * spinning on the vblank interrupt status bit, since we won't actually
1117  * see an interrupt when the pipe is disabled.
1118  *
1119  * On Gen4 and above:
1120  *   wait for the pipe register state bit to turn off
1121  *
1122  * Otherwise:
1123  *   wait for the display line value to settle (it usually
1124  *   ends up stopping at the start of the next frame).
1125  *
1126  */
1127 static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
1128 {
1129 	struct drm_device *dev = crtc->base.dev;
1130 	struct drm_i915_private *dev_priv = dev->dev_private;
1131 	enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1132 	enum i915_pipe pipe = crtc->pipe;
1133 
1134 	if (INTEL_INFO(dev)->gen >= 4) {
1135 		int reg = PIPECONF(cpu_transcoder);
1136 
1137 		/* Wait for the Pipe State to go off */
1138 		if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1139 			     100))
1140 			WARN(1, "pipe_off wait timed out\n");
1141 	} else {
1142 		/* Wait for the display line to settle */
1143 		if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
1144 			WARN(1, "pipe_off wait timed out\n");
1145 	}
1146 }
1147 
1148 static const char *state_string(bool enabled)
1149 {
1150 	return enabled ? "on" : "off";
1151 }
1152 
1153 /* Only for pre-ILK configs */
1154 void assert_pll(struct drm_i915_private *dev_priv,
1155 		enum i915_pipe pipe, bool state)
1156 {
1157 	u32 val;
1158 	bool cur_state;
1159 
1160 	val = I915_READ(DPLL(pipe));
1161 	cur_state = !!(val & DPLL_VCO_ENABLE);
1162 	I915_STATE_WARN(cur_state != state,
1163 	     "PLL state assertion failure (expected %s, current %s)\n",
1164 	     state_string(state), state_string(cur_state));
1165 }
1166 
1167 /* XXX: the dsi pll is shared between MIPI DSI ports */
1168 static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1169 {
1170 	u32 val;
1171 	bool cur_state;
1172 
1173 	mutex_lock(&dev_priv->sb_lock);
1174 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1175 	mutex_unlock(&dev_priv->sb_lock);
1176 
1177 	cur_state = val & DSI_PLL_VCO_EN;
1178 	I915_STATE_WARN(cur_state != state,
1179 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1180 	     state_string(state), state_string(cur_state));
1181 }
1182 #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1183 #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1184 
1185 struct intel_shared_dpll *
1186 intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1187 {
1188 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1189 
1190 	if (crtc->config->shared_dpll < 0)
1191 		return NULL;
1192 
1193 	return &dev_priv->shared_dplls[crtc->config->shared_dpll];
1194 }
1195 
1196 /* For ILK+ */
1197 void assert_shared_dpll(struct drm_i915_private *dev_priv,
1198 			struct intel_shared_dpll *pll,
1199 			bool state)
1200 {
1201 	bool cur_state;
1202 	struct intel_dpll_hw_state hw_state;
1203 
1204 	if (WARN (!pll,
1205 		  "asserting DPLL %s with no DPLL\n", state_string(state)))
1206 		return;
1207 
1208 	cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
1209 	I915_STATE_WARN(cur_state != state,
1210 	     "%s assertion failure (expected %s, current %s)\n",
1211 	     pll->name, state_string(state), state_string(cur_state));
1212 }
1213 
1214 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1215 			  enum i915_pipe pipe, bool state)
1216 {
1217 	bool cur_state;
1218 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1219 								      pipe);
1220 
1221 	if (HAS_DDI(dev_priv->dev)) {
1222 		/* DDI does not have a specific FDI_TX register */
1223 		u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1224 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1225 	} else {
1226 		u32 val = I915_READ(FDI_TX_CTL(pipe));
1227 		cur_state = !!(val & FDI_TX_ENABLE);
1228 	}
1229 	I915_STATE_WARN(cur_state != state,
1230 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1231 	     state_string(state), state_string(cur_state));
1232 }
1233 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1234 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1235 
1236 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1237 			  enum i915_pipe pipe, bool state)
1238 {
1239 	u32 val;
1240 	bool cur_state;
1241 
1242 	val = I915_READ(FDI_RX_CTL(pipe));
1243 	cur_state = !!(val & FDI_RX_ENABLE);
1244 	I915_STATE_WARN(cur_state != state,
1245 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1246 	     state_string(state), state_string(cur_state));
1247 }
1248 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1249 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1250 
1251 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1252 				      enum i915_pipe pipe)
1253 {
1254 	u32 val;
1255 
1256 	/* ILK FDI PLL is always enabled */
1257 	if (INTEL_INFO(dev_priv->dev)->gen == 5)
1258 		return;
1259 
1260 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1261 	if (HAS_DDI(dev_priv->dev))
1262 		return;
1263 
1264 	val = I915_READ(FDI_TX_CTL(pipe));
1265 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1266 }
1267 
1268 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1269 		       enum i915_pipe pipe, bool state)
1270 {
1271 	u32 val;
1272 	bool cur_state;
1273 
1274 	val = I915_READ(FDI_RX_CTL(pipe));
1275 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1276 	I915_STATE_WARN(cur_state != state,
1277 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1278 	     state_string(state), state_string(cur_state));
1279 }
1280 
1281 void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1282 			   enum i915_pipe pipe)
1283 {
1284 	struct drm_device *dev = dev_priv->dev;
1285 	int pp_reg;
1286 	u32 val;
1287 	enum i915_pipe panel_pipe = PIPE_A;
1288 	bool locked = true;
1289 
1290 	if (WARN_ON(HAS_DDI(dev)))
1291 		return;
1292 
1293 	if (HAS_PCH_SPLIT(dev)) {
1294 		u32 port_sel;
1295 
1296 		pp_reg = PCH_PP_CONTROL;
1297 		port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1298 
1299 		if (port_sel == PANEL_PORT_SELECT_LVDS &&
1300 		    I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1301 			panel_pipe = PIPE_B;
1302 		/* XXX: else fix for eDP */
1303 	} else if (IS_VALLEYVIEW(dev)) {
1304 		/* presumably write lock depends on pipe, not port select */
1305 		pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1306 		panel_pipe = pipe;
1307 	} else {
1308 		pp_reg = PP_CONTROL;
1309 		if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1310 			panel_pipe = PIPE_B;
1311 	}
1312 
1313 	val = I915_READ(pp_reg);
1314 	if (!(val & PANEL_POWER_ON) ||
1315 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1316 		locked = false;
1317 
1318 	I915_STATE_WARN(panel_pipe == pipe && locked,
1319 	     "panel assertion failure, pipe %c regs locked\n",
1320 	     pipe_name(pipe));
1321 }
1322 
1323 static void assert_cursor(struct drm_i915_private *dev_priv,
1324 			  enum i915_pipe pipe, bool state)
1325 {
1326 	struct drm_device *dev = dev_priv->dev;
1327 	bool cur_state;
1328 
1329 	if (IS_845G(dev) || IS_I865G(dev))
1330 		cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
1331 	else
1332 		cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1333 
1334 	I915_STATE_WARN(cur_state != state,
1335 	     "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1336 	     pipe_name(pipe), state_string(state), state_string(cur_state));
1337 }
1338 #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1339 #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1340 
1341 void assert_pipe(struct drm_i915_private *dev_priv,
1342 		 enum i915_pipe pipe, bool state)
1343 {
1344 	bool cur_state;
1345 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1346 								      pipe);
1347 
1348 	/* if we need the pipe quirk it must be always on */
1349 	if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1350 	    (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1351 		state = true;
1352 
1353 	if (!intel_display_power_is_enabled(dev_priv,
1354 				POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1355 		cur_state = false;
1356 	} else {
1357 		u32 val = I915_READ(PIPECONF(cpu_transcoder));
1358 		cur_state = !!(val & PIPECONF_ENABLE);
1359 	}
1360 
1361 	I915_STATE_WARN(cur_state != state,
1362 	     "pipe %c assertion failure (expected %s, current %s)\n",
1363 	     pipe_name(pipe), state_string(state), state_string(cur_state));
1364 }
1365 
1366 static void assert_plane(struct drm_i915_private *dev_priv,
1367 			 enum plane plane, bool state)
1368 {
1369 	u32 val;
1370 	bool cur_state;
1371 
1372 	val = I915_READ(DSPCNTR(plane));
1373 	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1374 	I915_STATE_WARN(cur_state != state,
1375 	     "plane %c assertion failure (expected %s, current %s)\n",
1376 	     plane_name(plane), state_string(state), state_string(cur_state));
1377 }
1378 
1379 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1380 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1381 
1382 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1383 				   enum i915_pipe pipe)
1384 {
1385 	struct drm_device *dev = dev_priv->dev;
1386 	int i;
1387 
1388 	/* Primary planes are fixed to pipes on gen4+ */
1389 	if (INTEL_INFO(dev)->gen >= 4) {
1390 		u32 val = I915_READ(DSPCNTR(pipe));
1391 		I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
1392 		     "plane %c assertion failure, should be disabled but not\n",
1393 		     plane_name(pipe));
1394 		return;
1395 	}
1396 
1397 	/* Need to check both planes against the pipe */
1398 	for_each_pipe(dev_priv, i) {
1399 		u32 val = I915_READ(DSPCNTR(i));
1400 		enum i915_pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1401 			DISPPLANE_SEL_PIPE_SHIFT;
1402 		I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1403 		     "plane %c assertion failure, should be off on pipe %c but is still active\n",
1404 		     plane_name(i), pipe_name(pipe));
1405 	}
1406 }
1407 
1408 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1409 				    enum i915_pipe pipe)
1410 {
1411 	struct drm_device *dev = dev_priv->dev;
1412 	int sprite;
1413 
1414 	if (INTEL_INFO(dev)->gen >= 9) {
1415 		for_each_sprite(dev_priv, pipe, sprite) {
1416 			u32 val = I915_READ(PLANE_CTL(pipe, sprite));
1417 			I915_STATE_WARN(val & PLANE_CTL_ENABLE,
1418 			     "plane %d assertion failure, should be off on pipe %c but is still active\n",
1419 			     sprite, pipe_name(pipe));
1420 		}
1421 	} else if (IS_VALLEYVIEW(dev)) {
1422 		for_each_sprite(dev_priv, pipe, sprite) {
1423 			u32 val = I915_READ(SPCNTR(pipe, sprite));
1424 			I915_STATE_WARN(val & SP_ENABLE,
1425 			     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1426 			     sprite_name(pipe, sprite), pipe_name(pipe));
1427 		}
1428 	} else if (INTEL_INFO(dev)->gen >= 7) {
1429 		u32 val = I915_READ(SPRCTL(pipe));
1430 		I915_STATE_WARN(val & SPRITE_ENABLE,
1431 		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1432 		     plane_name(pipe), pipe_name(pipe));
1433 	} else if (INTEL_INFO(dev)->gen >= 5) {
1434 		u32 val = I915_READ(DVSCNTR(pipe));
1435 		I915_STATE_WARN(val & DVS_ENABLE,
1436 		     "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1437 		     plane_name(pipe), pipe_name(pipe));
1438 	}
1439 }
1440 
1441 static void assert_vblank_disabled(struct drm_crtc *crtc)
1442 {
1443 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1444 		drm_crtc_vblank_put(crtc);
1445 }
1446 
1447 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1448 {
1449 	u32 val;
1450 	bool enabled;
1451 
1452 	I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
1453 
1454 	val = I915_READ(PCH_DREF_CONTROL);
1455 	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1456 			    DREF_SUPERSPREAD_SOURCE_MASK));
1457 	I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1458 }
1459 
1460 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1461 					   enum i915_pipe pipe)
1462 {
1463 	u32 val;
1464 	bool enabled;
1465 
1466 	val = I915_READ(PCH_TRANSCONF(pipe));
1467 	enabled = !!(val & TRANS_ENABLE);
1468 	I915_STATE_WARN(enabled,
1469 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1470 	     pipe_name(pipe));
1471 }
1472 
1473 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1474 			    enum i915_pipe pipe, u32 port_sel, u32 val)
1475 {
1476 	if ((val & DP_PORT_EN) == 0)
1477 		return false;
1478 
1479 	if (HAS_PCH_CPT(dev_priv->dev)) {
1480 		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1481 		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1482 		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1483 			return false;
1484 	} else if (IS_CHERRYVIEW(dev_priv->dev)) {
1485 		if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1486 			return false;
1487 	} else {
1488 		if ((val & DP_PIPE_MASK) != (pipe << 30))
1489 			return false;
1490 	}
1491 	return true;
1492 }
1493 
1494 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1495 			      enum i915_pipe pipe, u32 val)
1496 {
1497 	if ((val & SDVO_ENABLE) == 0)
1498 		return false;
1499 
1500 	if (HAS_PCH_CPT(dev_priv->dev)) {
1501 		if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1502 			return false;
1503 	} else if (IS_CHERRYVIEW(dev_priv->dev)) {
1504 		if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1505 			return false;
1506 	} else {
1507 		if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1508 			return false;
1509 	}
1510 	return true;
1511 }
1512 
1513 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1514 			      enum i915_pipe pipe, u32 val)
1515 {
1516 	if ((val & LVDS_PORT_EN) == 0)
1517 		return false;
1518 
1519 	if (HAS_PCH_CPT(dev_priv->dev)) {
1520 		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1521 			return false;
1522 	} else {
1523 		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1524 			return false;
1525 	}
1526 	return true;
1527 }
1528 
1529 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1530 			      enum i915_pipe pipe, u32 val)
1531 {
1532 	if ((val & ADPA_DAC_ENABLE) == 0)
1533 		return false;
1534 	if (HAS_PCH_CPT(dev_priv->dev)) {
1535 		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1536 			return false;
1537 	} else {
1538 		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1539 			return false;
1540 	}
1541 	return true;
1542 }
1543 
1544 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1545 				   enum i915_pipe pipe, int reg, u32 port_sel)
1546 {
1547 	u32 val = I915_READ(reg);
1548 	I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1549 	     "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1550 	     reg, pipe_name(pipe));
1551 
1552 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1553 	     && (val & DP_PIPEB_SELECT),
1554 	     "IBX PCH dp port still using transcoder B\n");
1555 }
1556 
1557 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1558 				     enum i915_pipe pipe, int reg)
1559 {
1560 	u32 val = I915_READ(reg);
1561 	I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1562 	     "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1563 	     reg, pipe_name(pipe));
1564 
1565 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1566 	     && (val & SDVO_PIPE_B_SELECT),
1567 	     "IBX PCH hdmi port still using transcoder B\n");
1568 }
1569 
1570 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1571 				      enum i915_pipe pipe)
1572 {
1573 	u32 val;
1574 
1575 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1576 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1577 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1578 
1579 	val = I915_READ(PCH_ADPA);
1580 	I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1581 	     "PCH VGA enabled on transcoder %c, should be disabled\n",
1582 	     pipe_name(pipe));
1583 
1584 	val = I915_READ(PCH_LVDS);
1585 	I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1586 	     "PCH LVDS enabled on transcoder %c, should be disabled\n",
1587 	     pipe_name(pipe));
1588 
1589 	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1590 	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1591 	assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1592 }
1593 
1594 static void vlv_enable_pll(struct intel_crtc *crtc,
1595 			   const struct intel_crtc_state *pipe_config)
1596 {
1597 	struct drm_device *dev = crtc->base.dev;
1598 	struct drm_i915_private *dev_priv = dev->dev_private;
1599 	int reg = DPLL(crtc->pipe);
1600 	u32 dpll = pipe_config->dpll_hw_state.dpll;
1601 
1602 	assert_pipe_disabled(dev_priv, crtc->pipe);
1603 
1604 	/* No really, not for ILK+ */
1605 	BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1606 
1607 	/* PLL is protected by panel, make sure we can write it */
1608 	if (IS_MOBILE(dev_priv->dev))
1609 		assert_panel_unlocked(dev_priv, crtc->pipe);
1610 
1611 	I915_WRITE(reg, dpll);
1612 	POSTING_READ(reg);
1613 	udelay(150);
1614 
1615 	if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1616 		DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1617 
1618 	I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
1619 	POSTING_READ(DPLL_MD(crtc->pipe));
1620 
1621 	/* We do this three times for luck */
1622 	I915_WRITE(reg, dpll);
1623 	POSTING_READ(reg);
1624 	udelay(150); /* wait for warmup */
1625 	I915_WRITE(reg, dpll);
1626 	POSTING_READ(reg);
1627 	udelay(150); /* wait for warmup */
1628 	I915_WRITE(reg, dpll);
1629 	POSTING_READ(reg);
1630 	udelay(150); /* wait for warmup */
1631 }
1632 
1633 static void chv_enable_pll(struct intel_crtc *crtc,
1634 			   const struct intel_crtc_state *pipe_config)
1635 {
1636 	struct drm_device *dev = crtc->base.dev;
1637 	struct drm_i915_private *dev_priv = dev->dev_private;
1638 	int pipe = crtc->pipe;
1639 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1640 	u32 tmp;
1641 
1642 	assert_pipe_disabled(dev_priv, crtc->pipe);
1643 
1644 	BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1645 
1646 	mutex_lock(&dev_priv->sb_lock);
1647 
1648 	/* Enable back the 10bit clock to display controller */
1649 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1650 	tmp |= DPIO_DCLKP_EN;
1651 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1652 
1653 	mutex_unlock(&dev_priv->sb_lock);
1654 
1655 	/*
1656 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1657 	 */
1658 	udelay(1);
1659 
1660 	/* Enable PLL */
1661 	I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1662 
1663 	/* Check PLL is locked */
1664 	if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1665 		DRM_ERROR("PLL %d failed to lock\n", pipe);
1666 
1667 	/* not sure when this should be written */
1668 	I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1669 	POSTING_READ(DPLL_MD(pipe));
1670 }
1671 
1672 static int intel_num_dvo_pipes(struct drm_device *dev)
1673 {
1674 	struct intel_crtc *crtc;
1675 	int count = 0;
1676 
1677 	for_each_intel_crtc(dev, crtc)
1678 		count += crtc->base.state->active &&
1679 			intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1680 
1681 	return count;
1682 }
1683 
1684 static void i9xx_enable_pll(struct intel_crtc *crtc)
1685 {
1686 	struct drm_device *dev = crtc->base.dev;
1687 	struct drm_i915_private *dev_priv = dev->dev_private;
1688 	int reg = DPLL(crtc->pipe);
1689 	u32 dpll = crtc->config->dpll_hw_state.dpll;
1690 
1691 	assert_pipe_disabled(dev_priv, crtc->pipe);
1692 
1693 	/* No really, not for ILK+ */
1694 	BUG_ON(INTEL_INFO(dev)->gen >= 5);
1695 
1696 	/* PLL is protected by panel, make sure we can write it */
1697 	if (IS_MOBILE(dev) && !IS_I830(dev))
1698 		assert_panel_unlocked(dev_priv, crtc->pipe);
1699 
1700 	/* Enable DVO 2x clock on both PLLs if necessary */
1701 	if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1702 		/*
1703 		 * It appears to be important that we don't enable this
1704 		 * for the current pipe before otherwise configuring the
1705 		 * PLL. No idea how this should be handled if multiple
1706 		 * DVO outputs are enabled simultaneosly.
1707 		 */
1708 		dpll |= DPLL_DVO_2X_MODE;
1709 		I915_WRITE(DPLL(!crtc->pipe),
1710 			   I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1711 	}
1712 
1713 	/*
1714 	 * Apparently we need to have VGA mode enabled prior to changing
1715 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1716 	 * dividers, even though the register value does change.
1717 	 */
1718 	I915_WRITE(reg, 0);
1719 
1720 	I915_WRITE(reg, dpll);
1721 
1722 	/* Wait for the clocks to stabilize. */
1723 	POSTING_READ(reg);
1724 	udelay(150);
1725 
1726 	if (INTEL_INFO(dev)->gen >= 4) {
1727 		I915_WRITE(DPLL_MD(crtc->pipe),
1728 			   crtc->config->dpll_hw_state.dpll_md);
1729 	} else {
1730 		/* The pixel multiplier can only be updated once the
1731 		 * DPLL is enabled and the clocks are stable.
1732 		 *
1733 		 * So write it again.
1734 		 */
1735 		I915_WRITE(reg, dpll);
1736 	}
1737 
1738 	/* We do this three times for luck */
1739 	I915_WRITE(reg, dpll);
1740 	POSTING_READ(reg);
1741 	udelay(150); /* wait for warmup */
1742 	I915_WRITE(reg, dpll);
1743 	POSTING_READ(reg);
1744 	udelay(150); /* wait for warmup */
1745 	I915_WRITE(reg, dpll);
1746 	POSTING_READ(reg);
1747 	udelay(150); /* wait for warmup */
1748 }
1749 
1750 /**
1751  * i9xx_disable_pll - disable a PLL
1752  * @dev_priv: i915 private structure
1753  * @pipe: pipe PLL to disable
1754  *
1755  * Disable the PLL for @pipe, making sure the pipe is off first.
1756  *
1757  * Note!  This is for pre-ILK only.
1758  */
1759 static void i9xx_disable_pll(struct intel_crtc *crtc)
1760 {
1761 	struct drm_device *dev = crtc->base.dev;
1762 	struct drm_i915_private *dev_priv = dev->dev_private;
1763 	enum i915_pipe pipe = crtc->pipe;
1764 
1765 	/* Disable DVO 2x clock on both PLLs if necessary */
1766 	if (IS_I830(dev) &&
1767 	    intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1768 	    !intel_num_dvo_pipes(dev)) {
1769 		I915_WRITE(DPLL(PIPE_B),
1770 			   I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1771 		I915_WRITE(DPLL(PIPE_A),
1772 			   I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1773 	}
1774 
1775 	/* Don't disable pipe or pipe PLLs if needed */
1776 	if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1777 	    (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1778 		return;
1779 
1780 	/* Make sure the pipe isn't still relying on us */
1781 	assert_pipe_disabled(dev_priv, pipe);
1782 
1783 	I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
1784 	POSTING_READ(DPLL(pipe));
1785 }
1786 
1787 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1788 {
1789 	u32 val;
1790 
1791 	/* Make sure the pipe isn't still relying on us */
1792 	assert_pipe_disabled(dev_priv, pipe);
1793 
1794 	/*
1795 	 * Leave integrated clock source and reference clock enabled for pipe B.
1796 	 * The latter is needed for VGA hotplug / manual detection.
1797 	 */
1798 	val = DPLL_VGA_MODE_DIS;
1799 	if (pipe == PIPE_B)
1800 		val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
1801 	I915_WRITE(DPLL(pipe), val);
1802 	POSTING_READ(DPLL(pipe));
1803 
1804 }
1805 
1806 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1807 {
1808 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1809 	u32 val;
1810 
1811 	/* Make sure the pipe isn't still relying on us */
1812 	assert_pipe_disabled(dev_priv, pipe);
1813 
1814 	/* Set PLL en = 0 */
1815 	val = DPLL_SSC_REF_CLK_CHV |
1816 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1817 	if (pipe != PIPE_A)
1818 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1819 	I915_WRITE(DPLL(pipe), val);
1820 	POSTING_READ(DPLL(pipe));
1821 
1822 	mutex_lock(&dev_priv->sb_lock);
1823 
1824 	/* Disable 10bit clock to display controller */
1825 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1826 	val &= ~DPIO_DCLKP_EN;
1827 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1828 
1829 	mutex_unlock(&dev_priv->sb_lock);
1830 }
1831 
1832 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1833 			 struct intel_digital_port *dport,
1834 			 unsigned int expected_mask)
1835 {
1836 	u32 port_mask;
1837 	int dpll_reg;
1838 
1839 	switch (dport->port) {
1840 	case PORT_B:
1841 		port_mask = DPLL_PORTB_READY_MASK;
1842 		dpll_reg = DPLL(0);
1843 		break;
1844 	case PORT_C:
1845 		port_mask = DPLL_PORTC_READY_MASK;
1846 		dpll_reg = DPLL(0);
1847 		expected_mask <<= 4;
1848 		break;
1849 	case PORT_D:
1850 		port_mask = DPLL_PORTD_READY_MASK;
1851 		dpll_reg = DPIO_PHY_STATUS;
1852 		break;
1853 	default:
1854 		BUG();
1855 	}
1856 
1857 	if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1858 		WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1859 		     port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
1860 }
1861 
1862 static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1863 {
1864 	struct drm_device *dev = crtc->base.dev;
1865 	struct drm_i915_private *dev_priv = dev->dev_private;
1866 	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1867 
1868 	if (WARN_ON(pll == NULL))
1869 		return;
1870 
1871 	WARN_ON(!pll->config.crtc_mask);
1872 	if (pll->active == 0) {
1873 		DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1874 		WARN_ON(pll->on);
1875 		assert_shared_dpll_disabled(dev_priv, pll);
1876 
1877 		pll->mode_set(dev_priv, pll);
1878 	}
1879 }
1880 
1881 /**
1882  * intel_enable_shared_dpll - enable PCH PLL
1883  * @dev_priv: i915 private structure
1884  * @pipe: pipe PLL to enable
1885  *
1886  * The PCH PLL needs to be enabled before the PCH transcoder, since it
1887  * drives the transcoder clock.
1888  */
1889 static void intel_enable_shared_dpll(struct intel_crtc *crtc)
1890 {
1891 	struct drm_device *dev = crtc->base.dev;
1892 	struct drm_i915_private *dev_priv = dev->dev_private;
1893 	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1894 
1895 	if (WARN_ON(pll == NULL))
1896 		return;
1897 
1898 	if (WARN_ON(pll->config.crtc_mask == 0))
1899 		return;
1900 
1901 	DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
1902 		      pll->name, pll->active, pll->on,
1903 		      crtc->base.base.id);
1904 
1905 	if (pll->active++) {
1906 		WARN_ON(!pll->on);
1907 		assert_shared_dpll_enabled(dev_priv, pll);
1908 		return;
1909 	}
1910 	WARN_ON(pll->on);
1911 
1912 	intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1913 
1914 	DRM_DEBUG_KMS("enabling %s\n", pll->name);
1915 	pll->enable(dev_priv, pll);
1916 	pll->on = true;
1917 }
1918 
1919 static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1920 {
1921 	struct drm_device *dev = crtc->base.dev;
1922 	struct drm_i915_private *dev_priv = dev->dev_private;
1923 	struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1924 
1925 	/* PCH only available on ILK+ */
1926 	if (INTEL_INFO(dev)->gen < 5)
1927 		return;
1928 
1929 	if (pll == NULL)
1930 		return;
1931 
1932 	if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
1933 		return;
1934 
1935 	DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1936 		      pll->name, pll->active, pll->on,
1937 		      crtc->base.base.id);
1938 
1939 	if (WARN_ON(pll->active == 0)) {
1940 		assert_shared_dpll_disabled(dev_priv, pll);
1941 		return;
1942 	}
1943 
1944 	assert_shared_dpll_enabled(dev_priv, pll);
1945 	WARN_ON(!pll->on);
1946 	if (--pll->active)
1947 		return;
1948 
1949 	DRM_DEBUG_KMS("disabling %s\n", pll->name);
1950 	pll->disable(dev_priv, pll);
1951 	pll->on = false;
1952 
1953 	intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1954 }
1955 
1956 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1957 					   enum i915_pipe pipe)
1958 {
1959 	struct drm_device *dev = dev_priv->dev;
1960 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1961 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1962 	uint32_t reg, val, pipeconf_val;
1963 
1964 	/* PCH only available on ILK+ */
1965 	BUG_ON(!HAS_PCH_SPLIT(dev));
1966 
1967 	/* Make sure PCH DPLL is enabled */
1968 	assert_shared_dpll_enabled(dev_priv,
1969 				   intel_crtc_to_shared_dpll(intel_crtc));
1970 
1971 	/* FDI must be feeding us bits for PCH ports */
1972 	assert_fdi_tx_enabled(dev_priv, pipe);
1973 	assert_fdi_rx_enabled(dev_priv, pipe);
1974 
1975 	if (HAS_PCH_CPT(dev)) {
1976 		/* Workaround: Set the timing override bit before enabling the
1977 		 * pch transcoder. */
1978 		reg = TRANS_CHICKEN2(pipe);
1979 		val = I915_READ(reg);
1980 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1981 		I915_WRITE(reg, val);
1982 	}
1983 
1984 	reg = PCH_TRANSCONF(pipe);
1985 	val = I915_READ(reg);
1986 	pipeconf_val = I915_READ(PIPECONF(pipe));
1987 
1988 	if (HAS_PCH_IBX(dev_priv->dev)) {
1989 		/*
1990 		 * Make the BPC in transcoder be consistent with
1991 		 * that in pipeconf reg. For HDMI we must use 8bpc
1992 		 * here for both 8bpc and 12bpc.
1993 		 */
1994 		val &= ~PIPECONF_BPC_MASK;
1995 		if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1996 			val |= PIPECONF_8BPC;
1997 		else
1998 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1999 	}
2000 
2001 	val &= ~TRANS_INTERLACE_MASK;
2002 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2003 		if (HAS_PCH_IBX(dev_priv->dev) &&
2004 		    intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
2005 			val |= TRANS_LEGACY_INTERLACED_ILK;
2006 		else
2007 			val |= TRANS_INTERLACED;
2008 	else
2009 		val |= TRANS_PROGRESSIVE;
2010 
2011 	I915_WRITE(reg, val | TRANS_ENABLE);
2012 	if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
2013 		DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
2014 }
2015 
2016 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
2017 				      enum transcoder cpu_transcoder)
2018 {
2019 	u32 val, pipeconf_val;
2020 
2021 	/* PCH only available on ILK+ */
2022 	BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
2023 
2024 	/* FDI must be feeding us bits for PCH ports */
2025 	assert_fdi_tx_enabled(dev_priv, (enum i915_pipe) cpu_transcoder);
2026 	assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
2027 
2028 	/* Workaround: set timing override bit. */
2029 	val = I915_READ(TRANS_CHICKEN2(PIPE_A));
2030 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2031 	I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
2032 
2033 	val = TRANS_ENABLE;
2034 	pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
2035 
2036 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2037 	    PIPECONF_INTERLACED_ILK)
2038 		val |= TRANS_INTERLACED;
2039 	else
2040 		val |= TRANS_PROGRESSIVE;
2041 
2042 	I915_WRITE(LPT_TRANSCONF, val);
2043 	if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
2044 		DRM_ERROR("Failed to enable PCH transcoder\n");
2045 }
2046 
2047 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2048 					    enum i915_pipe pipe)
2049 {
2050 	struct drm_device *dev = dev_priv->dev;
2051 	uint32_t reg, val;
2052 
2053 	/* FDI relies on the transcoder */
2054 	assert_fdi_tx_disabled(dev_priv, pipe);
2055 	assert_fdi_rx_disabled(dev_priv, pipe);
2056 
2057 	/* Ports must be off as well */
2058 	assert_pch_ports_disabled(dev_priv, pipe);
2059 
2060 	reg = PCH_TRANSCONF(pipe);
2061 	val = I915_READ(reg);
2062 	val &= ~TRANS_ENABLE;
2063 	I915_WRITE(reg, val);
2064 	/* wait for PCH transcoder off, transcoder state */
2065 	if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
2066 		DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
2067 
2068 	if (!HAS_PCH_IBX(dev)) {
2069 		/* Workaround: Clear the timing override chicken bit again. */
2070 		reg = TRANS_CHICKEN2(pipe);
2071 		val = I915_READ(reg);
2072 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2073 		I915_WRITE(reg, val);
2074 	}
2075 }
2076 
2077 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
2078 {
2079 	u32 val;
2080 
2081 	val = I915_READ(LPT_TRANSCONF);
2082 	val &= ~TRANS_ENABLE;
2083 	I915_WRITE(LPT_TRANSCONF, val);
2084 	/* wait for PCH transcoder off, transcoder state */
2085 	if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
2086 		DRM_ERROR("Failed to disable PCH transcoder\n");
2087 
2088 	/* Workaround: clear timing override bit. */
2089 	val = I915_READ(TRANS_CHICKEN2(PIPE_A));
2090 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2091 	I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
2092 }
2093 
2094 /**
2095  * intel_enable_pipe - enable a pipe, asserting requirements
2096  * @crtc: crtc responsible for the pipe
2097  *
2098  * Enable @crtc's pipe, making sure that various hardware specific requirements
2099  * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
2100  */
2101 static void intel_enable_pipe(struct intel_crtc *crtc)
2102 {
2103 	struct drm_device *dev = crtc->base.dev;
2104 	struct drm_i915_private *dev_priv = dev->dev_private;
2105 	enum i915_pipe pipe = crtc->pipe;
2106 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2107 								      pipe);
2108 	enum i915_pipe pch_transcoder;
2109 	int reg;
2110 	u32 val;
2111 
2112 	DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2113 
2114 	assert_planes_disabled(dev_priv, pipe);
2115 	assert_cursor_disabled(dev_priv, pipe);
2116 	assert_sprites_disabled(dev_priv, pipe);
2117 
2118 	if (HAS_PCH_LPT(dev_priv->dev))
2119 		pch_transcoder = TRANSCODER_A;
2120 	else
2121 		pch_transcoder = pipe;
2122 
2123 	/*
2124 	 * A pipe without a PLL won't actually be able to drive bits from
2125 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
2126 	 * need the check.
2127 	 */
2128 	if (HAS_GMCH_DISPLAY(dev_priv->dev))
2129 		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
2130 			assert_dsi_pll_enabled(dev_priv);
2131 		else
2132 			assert_pll_enabled(dev_priv, pipe);
2133 	else {
2134 		if (crtc->config->has_pch_encoder) {
2135 			/* if driving the PCH, we need FDI enabled */
2136 			assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
2137 			assert_fdi_tx_pll_enabled(dev_priv,
2138 						  (enum i915_pipe) cpu_transcoder);
2139 		}
2140 		/* FIXME: assert CPU port conditions for SNB+ */
2141 	}
2142 
2143 	reg = PIPECONF(cpu_transcoder);
2144 	val = I915_READ(reg);
2145 	if (val & PIPECONF_ENABLE) {
2146 		WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2147 			  (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
2148 		return;
2149 	}
2150 
2151 	I915_WRITE(reg, val | PIPECONF_ENABLE);
2152 	POSTING_READ(reg);
2153 }
2154 
2155 /**
2156  * intel_disable_pipe - disable a pipe, asserting requirements
2157  * @crtc: crtc whose pipes is to be disabled
2158  *
2159  * Disable the pipe of @crtc, making sure that various hardware
2160  * specific requirements are met, if applicable, e.g. plane
2161  * disabled, panel fitter off, etc.
2162  *
2163  * Will wait until the pipe has shut down before returning.
2164  */
2165 static void intel_disable_pipe(struct intel_crtc *crtc)
2166 {
2167 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2168 	enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
2169 	enum i915_pipe pipe = crtc->pipe;
2170 	int reg;
2171 	u32 val;
2172 
2173 	DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2174 
2175 	/*
2176 	 * Make sure planes won't keep trying to pump pixels to us,
2177 	 * or we might hang the display.
2178 	 */
2179 	assert_planes_disabled(dev_priv, pipe);
2180 	assert_cursor_disabled(dev_priv, pipe);
2181 	assert_sprites_disabled(dev_priv, pipe);
2182 
2183 	reg = PIPECONF(cpu_transcoder);
2184 	val = I915_READ(reg);
2185 	if ((val & PIPECONF_ENABLE) == 0)
2186 		return;
2187 
2188 	/*
2189 	 * Double wide has implications for planes
2190 	 * so best keep it disabled when not needed.
2191 	 */
2192 	if (crtc->config->double_wide)
2193 		val &= ~PIPECONF_DOUBLE_WIDE;
2194 
2195 	/* Don't disable pipe or pipe PLLs if needed */
2196 	if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2197 	    !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
2198 		val &= ~PIPECONF_ENABLE;
2199 
2200 	I915_WRITE(reg, val);
2201 	if ((val & PIPECONF_ENABLE) == 0)
2202 		intel_wait_for_pipe_off(crtc);
2203 }
2204 
2205 static bool need_vtd_wa(struct drm_device *dev)
2206 {
2207 #ifdef CONFIG_INTEL_IOMMU
2208 	if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2209 		return true;
2210 #endif
2211 	return false;
2212 }
2213 
2214 unsigned int
2215 intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2216 		  uint64_t fb_format_modifier, unsigned int plane)
2217 {
2218 	unsigned int tile_height;
2219 	uint32_t pixel_bytes;
2220 
2221 	switch (fb_format_modifier) {
2222 	case DRM_FORMAT_MOD_NONE:
2223 		tile_height = 1;
2224 		break;
2225 	case I915_FORMAT_MOD_X_TILED:
2226 		tile_height = IS_GEN2(dev) ? 16 : 8;
2227 		break;
2228 	case I915_FORMAT_MOD_Y_TILED:
2229 		tile_height = 32;
2230 		break;
2231 	case I915_FORMAT_MOD_Yf_TILED:
2232 		pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
2233 		switch (pixel_bytes) {
2234 		default:
2235 		case 1:
2236 			tile_height = 64;
2237 			break;
2238 		case 2:
2239 		case 4:
2240 			tile_height = 32;
2241 			break;
2242 		case 8:
2243 			tile_height = 16;
2244 			break;
2245 		case 16:
2246 			WARN_ONCE(1,
2247 				  "128-bit pixels are not supported for display!");
2248 			tile_height = 16;
2249 			break;
2250 		}
2251 		break;
2252 	default:
2253 		MISSING_CASE(fb_format_modifier);
2254 		tile_height = 1;
2255 		break;
2256 	}
2257 
2258 	return tile_height;
2259 }
2260 
2261 unsigned int
2262 intel_fb_align_height(struct drm_device *dev, unsigned int height,
2263 		      uint32_t pixel_format, uint64_t fb_format_modifier)
2264 {
2265 	return ALIGN(height, intel_tile_height(dev, pixel_format,
2266 					       fb_format_modifier, 0));
2267 }
2268 
2269 static int
2270 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2271 			const struct drm_plane_state *plane_state)
2272 {
2273 	struct intel_rotation_info *info = &view->rotation_info;
2274 	unsigned int tile_height, tile_pitch;
2275 
2276 	*view = i915_ggtt_view_normal;
2277 
2278 	if (!plane_state)
2279 		return 0;
2280 
2281 	if (!intel_rotation_90_or_270(plane_state->rotation))
2282 		return 0;
2283 
2284 	*view = i915_ggtt_view_rotated;
2285 
2286 	info->height = fb->height;
2287 	info->pixel_format = fb->pixel_format;
2288 	info->pitch = fb->pitches[0];
2289 	info->uv_offset = fb->offsets[1];
2290 	info->fb_modifier = fb->modifier[0];
2291 
2292 	tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2293 					fb->modifier[0], 0);
2294 	tile_pitch = PAGE_SIZE / tile_height;
2295 	info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2296 	info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2297 	info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2298 
2299 	if (info->pixel_format == DRM_FORMAT_NV12) {
2300 		tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2301 						fb->modifier[0], 1);
2302 		tile_pitch = PAGE_SIZE / tile_height;
2303 		info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2304 		info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
2305 						     tile_height);
2306 		info->size_uv = info->width_pages_uv * info->height_pages_uv *
2307 				PAGE_SIZE;
2308 	}
2309 
2310 	return 0;
2311 }
2312 
2313 static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2314 {
2315 	if (INTEL_INFO(dev_priv)->gen >= 9)
2316 		return 256 * 1024;
2317 	else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2318 		 IS_VALLEYVIEW(dev_priv))
2319 		return 128 * 1024;
2320 	else if (INTEL_INFO(dev_priv)->gen >= 4)
2321 		return 4 * 1024;
2322 	else
2323 		return 0;
2324 }
2325 
2326 int
2327 intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2328 			   struct drm_framebuffer *fb,
2329 			   const struct drm_plane_state *plane_state,
2330 			   struct intel_engine_cs *pipelined,
2331 			   struct drm_i915_gem_request **pipelined_request)
2332 {
2333 	struct drm_device *dev = fb->dev;
2334 	struct drm_i915_private *dev_priv = dev->dev_private;
2335 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2336 	struct i915_ggtt_view view;
2337 	u32 alignment;
2338 	int ret;
2339 
2340 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2341 
2342 	switch (fb->modifier[0]) {
2343 	case DRM_FORMAT_MOD_NONE:
2344 		alignment = intel_linear_alignment(dev_priv);
2345 		break;
2346 	case I915_FORMAT_MOD_X_TILED:
2347 		if (INTEL_INFO(dev)->gen >= 9)
2348 			alignment = 256 * 1024;
2349 		else {
2350 			/* pin() will align the object as required by fence */
2351 			alignment = 0;
2352 		}
2353 		break;
2354 	case I915_FORMAT_MOD_Y_TILED:
2355 	case I915_FORMAT_MOD_Yf_TILED:
2356 		if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2357 			  "Y tiling bo slipped through, driver bug!\n"))
2358 			return -EINVAL;
2359 		alignment = 1 * 1024 * 1024;
2360 		break;
2361 	default:
2362 		MISSING_CASE(fb->modifier[0]);
2363 		return -EINVAL;
2364 	}
2365 
2366 	ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2367 	if (ret)
2368 		return ret;
2369 
2370 	/* Note that the w/a also requires 64 PTE of padding following the
2371 	 * bo. We currently fill all unused PTE with the shadow page and so
2372 	 * we should always have valid PTE following the scanout preventing
2373 	 * the VT-d warning.
2374 	 */
2375 	if (need_vtd_wa(dev) && alignment < 256 * 1024)
2376 		alignment = 256 * 1024;
2377 
2378 	/*
2379 	 * Global gtt pte registers are special registers which actually forward
2380 	 * writes to a chunk of system memory. Which means that there is no risk
2381 	 * that the register values disappear as soon as we call
2382 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2383 	 * pin/unpin/fence and not more.
2384 	 */
2385 	intel_runtime_pm_get(dev_priv);
2386 
2387 	dev_priv->mm.interruptible = false;
2388 	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
2389 						   pipelined_request, &view);
2390 	if (ret)
2391 		goto err_interruptible;
2392 
2393 	/* Install a fence for tiled scan-out. Pre-i965 always needs a
2394 	 * fence, whereas 965+ only requires a fence if using
2395 	 * framebuffer compression.  For simplicity, we always install
2396 	 * a fence as the cost is not that onerous.
2397 	 */
2398 	if (view.type == I915_GGTT_VIEW_NORMAL) {
2399 		ret = i915_gem_object_get_fence(obj);
2400 		if (ret == -EDEADLK) {
2401 			/*
2402 			 * -EDEADLK means there are no free fences
2403 			 * no pending flips.
2404 			 *
2405 			 * This is propagated to atomic, but it uses
2406 			 * -EDEADLK to force a locking recovery, so
2407 			 * change the returned error to -EBUSY.
2408 			 */
2409 			ret = -EBUSY;
2410 			goto err_unpin;
2411 		} else if (ret)
2412 			goto err_unpin;
2413 
2414 		i915_gem_object_pin_fence(obj);
2415 	}
2416 
2417 	dev_priv->mm.interruptible = true;
2418 	intel_runtime_pm_put(dev_priv);
2419 	return 0;
2420 
2421 err_unpin:
2422 	i915_gem_object_unpin_from_display_plane(obj, &view);
2423 err_interruptible:
2424 	dev_priv->mm.interruptible = true;
2425 	intel_runtime_pm_put(dev_priv);
2426 	return ret;
2427 }
2428 
2429 static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2430 			       const struct drm_plane_state *plane_state)
2431 {
2432 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2433 	struct i915_ggtt_view view;
2434 	int ret;
2435 
2436 	WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2437 
2438 	ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2439 	WARN_ONCE(ret, "Couldn't get view from plane state!");
2440 
2441 	if (view.type == I915_GGTT_VIEW_NORMAL)
2442 		i915_gem_object_unpin_fence(obj);
2443 
2444 	i915_gem_object_unpin_from_display_plane(obj, &view);
2445 }
2446 
2447 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2448  * is assumed to be a power-of-two. */
2449 unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2450 					     int *x, int *y,
2451 					     unsigned int tiling_mode,
2452 					     unsigned int cpp,
2453 					     unsigned int pitch)
2454 {
2455 	if (tiling_mode != I915_TILING_NONE) {
2456 		unsigned int tile_rows, tiles;
2457 
2458 		tile_rows = *y / 8;
2459 		*y %= 8;
2460 
2461 		tiles = *x / (512/cpp);
2462 		*x %= 512/cpp;
2463 
2464 		return tile_rows * pitch * 8 + tiles * 4096;
2465 	} else {
2466 		unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
2467 		unsigned int offset;
2468 
2469 		offset = *y * pitch + *x * cpp;
2470 		*y = (offset & alignment) / pitch;
2471 		*x = ((offset & alignment) - *y * pitch) / cpp;
2472 		return offset & ~alignment;
2473 	}
2474 }
2475 
2476 static int i9xx_format_to_fourcc(int format)
2477 {
2478 	switch (format) {
2479 	case DISPPLANE_8BPP:
2480 		return DRM_FORMAT_C8;
2481 	case DISPPLANE_BGRX555:
2482 		return DRM_FORMAT_XRGB1555;
2483 	case DISPPLANE_BGRX565:
2484 		return DRM_FORMAT_RGB565;
2485 	default:
2486 	case DISPPLANE_BGRX888:
2487 		return DRM_FORMAT_XRGB8888;
2488 	case DISPPLANE_RGBX888:
2489 		return DRM_FORMAT_XBGR8888;
2490 	case DISPPLANE_BGRX101010:
2491 		return DRM_FORMAT_XRGB2101010;
2492 	case DISPPLANE_RGBX101010:
2493 		return DRM_FORMAT_XBGR2101010;
2494 	}
2495 }
2496 
2497 static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2498 {
2499 	switch (format) {
2500 	case PLANE_CTL_FORMAT_RGB_565:
2501 		return DRM_FORMAT_RGB565;
2502 	default:
2503 	case PLANE_CTL_FORMAT_XRGB_8888:
2504 		if (rgb_order) {
2505 			if (alpha)
2506 				return DRM_FORMAT_ABGR8888;
2507 			else
2508 				return DRM_FORMAT_XBGR8888;
2509 		} else {
2510 			if (alpha)
2511 				return DRM_FORMAT_ARGB8888;
2512 			else
2513 				return DRM_FORMAT_XRGB8888;
2514 		}
2515 	case PLANE_CTL_FORMAT_XRGB_2101010:
2516 		if (rgb_order)
2517 			return DRM_FORMAT_XBGR2101010;
2518 		else
2519 			return DRM_FORMAT_XRGB2101010;
2520 	}
2521 }
2522 
2523 static bool
2524 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2525 			      struct intel_initial_plane_config *plane_config)
2526 {
2527 	struct drm_device *dev = crtc->base.dev;
2528 	struct drm_i915_private *dev_priv = to_i915(dev);
2529 	struct drm_i915_gem_object *obj = NULL;
2530 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2531 	struct drm_framebuffer *fb = &plane_config->fb->base;
2532 	u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2533 	u32 size_aligned = round_up(plane_config->base + plane_config->size,
2534 				    PAGE_SIZE);
2535 
2536 	size_aligned -= base_aligned;
2537 
2538 	if (plane_config->size == 0)
2539 		return false;
2540 
2541 	/* If the FB is too big, just don't use it since fbdev is not very
2542 	 * important and we should probably use that space with FBC or other
2543 	 * features. */
2544 	if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
2545 		return false;
2546 
2547 	obj = i915_gem_object_create_stolen_for_preallocated(dev,
2548 							     base_aligned,
2549 							     base_aligned,
2550 							     size_aligned);
2551 	if (!obj)
2552 		return false;
2553 
2554 	obj->tiling_mode = plane_config->tiling;
2555 	if (obj->tiling_mode == I915_TILING_X)
2556 		obj->stride = fb->pitches[0];
2557 
2558 	mode_cmd.pixel_format = fb->pixel_format;
2559 	mode_cmd.width = fb->width;
2560 	mode_cmd.height = fb->height;
2561 	mode_cmd.pitches[0] = fb->pitches[0];
2562 	mode_cmd.modifier[0] = fb->modifier[0];
2563 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
2564 
2565 	mutex_lock(&dev->struct_mutex);
2566 	if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
2567 				   &mode_cmd, obj)) {
2568 		DRM_DEBUG_KMS("intel fb init failed\n");
2569 		goto out_unref_obj;
2570 	}
2571 	mutex_unlock(&dev->struct_mutex);
2572 
2573 	DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
2574 	return true;
2575 
2576 out_unref_obj:
2577 	drm_gem_object_unreference(&obj->base);
2578 	mutex_unlock(&dev->struct_mutex);
2579 	return false;
2580 }
2581 
2582 /* Update plane->state->fb to match plane->fb after driver-internal updates */
2583 static void
2584 update_state_fb(struct drm_plane *plane)
2585 {
2586 	if (plane->fb == plane->state->fb)
2587 		return;
2588 
2589 	if (plane->state->fb)
2590 		drm_framebuffer_unreference(plane->state->fb);
2591 	plane->state->fb = plane->fb;
2592 	if (plane->state->fb)
2593 		drm_framebuffer_reference(plane->state->fb);
2594 }
2595 
2596 static void
2597 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2598 			     struct intel_initial_plane_config *plane_config)
2599 {
2600 	struct drm_device *dev = intel_crtc->base.dev;
2601 	struct drm_i915_private *dev_priv = dev->dev_private;
2602 	struct drm_crtc *c;
2603 	struct intel_crtc *i;
2604 	struct drm_i915_gem_object *obj;
2605 	struct drm_plane *primary = intel_crtc->base.primary;
2606 	struct drm_plane_state *plane_state = primary->state;
2607 	struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2608 	struct intel_plane *intel_plane = to_intel_plane(primary);
2609 	struct drm_framebuffer *fb;
2610 
2611 	if (!plane_config->fb)
2612 		return;
2613 
2614 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
2615 		fb = &plane_config->fb->base;
2616 		goto valid_fb;
2617 	}
2618 
2619 	kfree(plane_config->fb);
2620 
2621 	/*
2622 	 * Failed to alloc the obj, check to see if we should share
2623 	 * an fb with another CRTC instead
2624 	 */
2625 	for_each_crtc(dev, c) {
2626 		i = to_intel_crtc(c);
2627 
2628 		if (c == &intel_crtc->base)
2629 			continue;
2630 
2631 		if (!i->active)
2632 			continue;
2633 
2634 		fb = c->primary->fb;
2635 		if (!fb)
2636 			continue;
2637 
2638 		obj = intel_fb_obj(fb);
2639 		if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
2640 			drm_framebuffer_reference(fb);
2641 			goto valid_fb;
2642 		}
2643 	}
2644 
2645 	/*
2646 	 * We've failed to reconstruct the BIOS FB.  Current display state
2647 	 * indicates that the primary plane is visible, but has a NULL FB,
2648 	 * which will lead to problems later if we don't fix it up.  The
2649 	 * simplest solution is to just disable the primary plane now and
2650 	 * pretend the BIOS never had it enabled.
2651 	 */
2652 	to_intel_plane_state(plane_state)->visible = false;
2653 	crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2654 	intel_pre_disable_primary(&intel_crtc->base);
2655 	intel_plane->disable_plane(primary, &intel_crtc->base);
2656 
2657 	return;
2658 
2659 valid_fb:
2660 	plane_state->src_x = 0;
2661 	plane_state->src_y = 0;
2662 	plane_state->src_w = fb->width << 16;
2663 	plane_state->src_h = fb->height << 16;
2664 
2665 	plane_state->crtc_x = 0;
2666 	plane_state->crtc_y = 0;
2667 	plane_state->crtc_w = fb->width;
2668 	plane_state->crtc_h = fb->height;
2669 
2670 	obj = intel_fb_obj(fb);
2671 	if (obj->tiling_mode != I915_TILING_NONE)
2672 		dev_priv->preserve_bios_swizzle = true;
2673 
2674 	drm_framebuffer_reference(fb);
2675 	primary->fb = primary->state->fb = fb;
2676 	primary->crtc = primary->state->crtc = &intel_crtc->base;
2677 	intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
2678 	obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
2679 }
2680 
2681 static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2682 				      struct drm_framebuffer *fb,
2683 				      int x, int y)
2684 {
2685 	struct drm_device *dev = crtc->dev;
2686 	struct drm_i915_private *dev_priv = dev->dev_private;
2687 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2688 	struct drm_plane *primary = crtc->primary;
2689 	bool visible = to_intel_plane_state(primary->state)->visible;
2690 	struct drm_i915_gem_object *obj;
2691 	int plane = intel_crtc->plane;
2692 	unsigned long linear_offset;
2693 	u32 dspcntr;
2694 	u32 reg = DSPCNTR(plane);
2695 	int pixel_size;
2696 
2697 	if (!visible || !fb) {
2698 		I915_WRITE(reg, 0);
2699 		if (INTEL_INFO(dev)->gen >= 4)
2700 			I915_WRITE(DSPSURF(plane), 0);
2701 		else
2702 			I915_WRITE(DSPADDR(plane), 0);
2703 		POSTING_READ(reg);
2704 		return;
2705 	}
2706 
2707 	obj = intel_fb_obj(fb);
2708 	if (WARN_ON(obj == NULL))
2709 		return;
2710 
2711 	pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2712 
2713 	dspcntr = DISPPLANE_GAMMA_ENABLE;
2714 
2715 	dspcntr |= DISPLAY_PLANE_ENABLE;
2716 
2717 	if (INTEL_INFO(dev)->gen < 4) {
2718 		if (intel_crtc->pipe == PIPE_B)
2719 			dspcntr |= DISPPLANE_SEL_PIPE_B;
2720 
2721 		/* pipesrc and dspsize control the size that is scaled from,
2722 		 * which should always be the user's requested size.
2723 		 */
2724 		I915_WRITE(DSPSIZE(plane),
2725 			   ((intel_crtc->config->pipe_src_h - 1) << 16) |
2726 			   (intel_crtc->config->pipe_src_w - 1));
2727 		I915_WRITE(DSPPOS(plane), 0);
2728 	} else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2729 		I915_WRITE(PRIMSIZE(plane),
2730 			   ((intel_crtc->config->pipe_src_h - 1) << 16) |
2731 			   (intel_crtc->config->pipe_src_w - 1));
2732 		I915_WRITE(PRIMPOS(plane), 0);
2733 		I915_WRITE(PRIMCNSTALPHA(plane), 0);
2734 	}
2735 
2736 	switch (fb->pixel_format) {
2737 	case DRM_FORMAT_C8:
2738 		dspcntr |= DISPPLANE_8BPP;
2739 		break;
2740 	case DRM_FORMAT_XRGB1555:
2741 		dspcntr |= DISPPLANE_BGRX555;
2742 		break;
2743 	case DRM_FORMAT_RGB565:
2744 		dspcntr |= DISPPLANE_BGRX565;
2745 		break;
2746 	case DRM_FORMAT_XRGB8888:
2747 		dspcntr |= DISPPLANE_BGRX888;
2748 		break;
2749 	case DRM_FORMAT_XBGR8888:
2750 		dspcntr |= DISPPLANE_RGBX888;
2751 		break;
2752 	case DRM_FORMAT_XRGB2101010:
2753 		dspcntr |= DISPPLANE_BGRX101010;
2754 		break;
2755 	case DRM_FORMAT_XBGR2101010:
2756 		dspcntr |= DISPPLANE_RGBX101010;
2757 		break;
2758 	default:
2759 		BUG();
2760 	}
2761 
2762 	if (INTEL_INFO(dev)->gen >= 4 &&
2763 	    obj->tiling_mode != I915_TILING_NONE)
2764 		dspcntr |= DISPPLANE_TILED;
2765 
2766 	if (IS_G4X(dev))
2767 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2768 
2769 	linear_offset = y * fb->pitches[0] + x * pixel_size;
2770 
2771 	if (INTEL_INFO(dev)->gen >= 4) {
2772 		intel_crtc->dspaddr_offset =
2773 			intel_gen4_compute_page_offset(dev_priv,
2774 						       &x, &y, obj->tiling_mode,
2775 						       pixel_size,
2776 						       fb->pitches[0]);
2777 		linear_offset -= intel_crtc->dspaddr_offset;
2778 	} else {
2779 		intel_crtc->dspaddr_offset = linear_offset;
2780 	}
2781 
2782 	if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2783 		dspcntr |= DISPPLANE_ROTATE_180;
2784 
2785 		x += (intel_crtc->config->pipe_src_w - 1);
2786 		y += (intel_crtc->config->pipe_src_h - 1);
2787 
2788 		/* Finding the last pixel of the last line of the display
2789 		data and adding to linear_offset*/
2790 		linear_offset +=
2791 			(intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2792 			(intel_crtc->config->pipe_src_w - 1) * pixel_size;
2793 	}
2794 
2795 	intel_crtc->adjusted_x = x;
2796 	intel_crtc->adjusted_y = y;
2797 
2798 	I915_WRITE(reg, dspcntr);
2799 
2800 	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2801 	if (INTEL_INFO(dev)->gen >= 4) {
2802 		I915_WRITE(DSPSURF(plane),
2803 			   i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2804 		I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2805 		I915_WRITE(DSPLINOFF(plane), linear_offset);
2806 	} else
2807 		I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2808 	POSTING_READ(reg);
2809 }
2810 
2811 static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2812 					  struct drm_framebuffer *fb,
2813 					  int x, int y)
2814 {
2815 	struct drm_device *dev = crtc->dev;
2816 	struct drm_i915_private *dev_priv = dev->dev_private;
2817 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2818 	struct drm_plane *primary = crtc->primary;
2819 	bool visible = to_intel_plane_state(primary->state)->visible;
2820 	struct drm_i915_gem_object *obj;
2821 	int plane = intel_crtc->plane;
2822 	unsigned long linear_offset;
2823 	u32 dspcntr;
2824 	u32 reg = DSPCNTR(plane);
2825 	int pixel_size;
2826 
2827 	if (!visible || !fb) {
2828 		I915_WRITE(reg, 0);
2829 		I915_WRITE(DSPSURF(plane), 0);
2830 		POSTING_READ(reg);
2831 		return;
2832 	}
2833 
2834 	obj = intel_fb_obj(fb);
2835 	if (WARN_ON(obj == NULL))
2836 		return;
2837 
2838 	pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2839 
2840 	dspcntr = DISPPLANE_GAMMA_ENABLE;
2841 
2842 	dspcntr |= DISPLAY_PLANE_ENABLE;
2843 
2844 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2845 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2846 
2847 	switch (fb->pixel_format) {
2848 	case DRM_FORMAT_C8:
2849 		dspcntr |= DISPPLANE_8BPP;
2850 		break;
2851 	case DRM_FORMAT_RGB565:
2852 		dspcntr |= DISPPLANE_BGRX565;
2853 		break;
2854 	case DRM_FORMAT_XRGB8888:
2855 		dspcntr |= DISPPLANE_BGRX888;
2856 		break;
2857 	case DRM_FORMAT_XBGR8888:
2858 		dspcntr |= DISPPLANE_RGBX888;
2859 		break;
2860 	case DRM_FORMAT_XRGB2101010:
2861 		dspcntr |= DISPPLANE_BGRX101010;
2862 		break;
2863 	case DRM_FORMAT_XBGR2101010:
2864 		dspcntr |= DISPPLANE_RGBX101010;
2865 		break;
2866 	default:
2867 		BUG();
2868 	}
2869 
2870 	if (obj->tiling_mode != I915_TILING_NONE)
2871 		dspcntr |= DISPPLANE_TILED;
2872 
2873 	if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
2874 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2875 
2876 	linear_offset = y * fb->pitches[0] + x * pixel_size;
2877 	intel_crtc->dspaddr_offset =
2878 		intel_gen4_compute_page_offset(dev_priv,
2879 					       &x, &y, obj->tiling_mode,
2880 					       pixel_size,
2881 					       fb->pitches[0]);
2882 	linear_offset -= intel_crtc->dspaddr_offset;
2883 	if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2884 		dspcntr |= DISPPLANE_ROTATE_180;
2885 
2886 		if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2887 			x += (intel_crtc->config->pipe_src_w - 1);
2888 			y += (intel_crtc->config->pipe_src_h - 1);
2889 
2890 			/* Finding the last pixel of the last line of the display
2891 			data and adding to linear_offset*/
2892 			linear_offset +=
2893 				(intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2894 				(intel_crtc->config->pipe_src_w - 1) * pixel_size;
2895 		}
2896 	}
2897 
2898 	intel_crtc->adjusted_x = x;
2899 	intel_crtc->adjusted_y = y;
2900 
2901 	I915_WRITE(reg, dspcntr);
2902 
2903 	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2904 	I915_WRITE(DSPSURF(plane),
2905 		   i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2906 	if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2907 		I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2908 	} else {
2909 		I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2910 		I915_WRITE(DSPLINOFF(plane), linear_offset);
2911 	}
2912 	POSTING_READ(reg);
2913 }
2914 
2915 u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2916 			      uint32_t pixel_format)
2917 {
2918 	u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2919 
2920 	/*
2921 	 * The stride is either expressed as a multiple of 64 bytes
2922 	 * chunks for linear buffers or in number of tiles for tiled
2923 	 * buffers.
2924 	 */
2925 	switch (fb_modifier) {
2926 	case DRM_FORMAT_MOD_NONE:
2927 		return 64;
2928 	case I915_FORMAT_MOD_X_TILED:
2929 		if (INTEL_INFO(dev)->gen == 2)
2930 			return 128;
2931 		return 512;
2932 	case I915_FORMAT_MOD_Y_TILED:
2933 		/* No need to check for old gens and Y tiling since this is
2934 		 * about the display engine and those will be blocked before
2935 		 * we get here.
2936 		 */
2937 		return 128;
2938 	case I915_FORMAT_MOD_Yf_TILED:
2939 		if (bits_per_pixel == 8)
2940 			return 64;
2941 		else
2942 			return 128;
2943 	default:
2944 		MISSING_CASE(fb_modifier);
2945 		return 64;
2946 	}
2947 }
2948 
2949 unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2950 				     struct drm_i915_gem_object *obj,
2951 				     unsigned int plane)
2952 {
2953 	const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
2954 	struct i915_vma *vma;
2955 	unsigned char *offset;
2956 
2957 	if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
2958 		view = &i915_ggtt_view_rotated;
2959 
2960 	vma = i915_gem_obj_to_ggtt_view(obj, view);
2961 	if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
2962 		view->type))
2963 		return -1;
2964 
2965 	offset = (unsigned char *)vma->node.start;
2966 
2967 	if (plane == 1) {
2968 		offset += vma->ggtt_view.rotation_info.uv_start_page *
2969 			  PAGE_SIZE;
2970 	}
2971 
2972 	return (unsigned long)offset;
2973 }
2974 
2975 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2976 {
2977 	struct drm_device *dev = intel_crtc->base.dev;
2978 	struct drm_i915_private *dev_priv = dev->dev_private;
2979 
2980 	I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2981 	I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2982 	I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
2983 }
2984 
2985 /*
2986  * This function detaches (aka. unbinds) unused scalers in hardware
2987  */
2988 static void skl_detach_scalers(struct intel_crtc *intel_crtc)
2989 {
2990 	struct intel_crtc_scaler_state *scaler_state;
2991 	int i;
2992 
2993 	scaler_state = &intel_crtc->config->scaler_state;
2994 
2995 	/* loop through and disable scalers that aren't in use */
2996 	for (i = 0; i < intel_crtc->num_scalers; i++) {
2997 		if (!scaler_state->scalers[i].in_use)
2998 			skl_detach_scaler(intel_crtc, i);
2999 	}
3000 }
3001 
3002 u32 skl_plane_ctl_format(uint32_t pixel_format)
3003 {
3004 	switch (pixel_format) {
3005 	case DRM_FORMAT_C8:
3006 		return PLANE_CTL_FORMAT_INDEXED;
3007 	case DRM_FORMAT_RGB565:
3008 		return PLANE_CTL_FORMAT_RGB_565;
3009 	case DRM_FORMAT_XBGR8888:
3010 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
3011 	case DRM_FORMAT_XRGB8888:
3012 		return PLANE_CTL_FORMAT_XRGB_8888;
3013 	/*
3014 	 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3015 	 * to be already pre-multiplied. We need to add a knob (or a different
3016 	 * DRM_FORMAT) for user-space to configure that.
3017 	 */
3018 	case DRM_FORMAT_ABGR8888:
3019 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
3020 			PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3021 	case DRM_FORMAT_ARGB8888:
3022 		return PLANE_CTL_FORMAT_XRGB_8888 |
3023 			PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3024 	case DRM_FORMAT_XRGB2101010:
3025 		return PLANE_CTL_FORMAT_XRGB_2101010;
3026 	case DRM_FORMAT_XBGR2101010:
3027 		return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
3028 	case DRM_FORMAT_YUYV:
3029 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
3030 	case DRM_FORMAT_YVYU:
3031 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
3032 	case DRM_FORMAT_UYVY:
3033 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3034 	case DRM_FORMAT_VYUY:
3035 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
3036 	default:
3037 		MISSING_CASE(pixel_format);
3038 	}
3039 
3040 	return 0;
3041 }
3042 
3043 u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3044 {
3045 	switch (fb_modifier) {
3046 	case DRM_FORMAT_MOD_NONE:
3047 		break;
3048 	case I915_FORMAT_MOD_X_TILED:
3049 		return PLANE_CTL_TILED_X;
3050 	case I915_FORMAT_MOD_Y_TILED:
3051 		return PLANE_CTL_TILED_Y;
3052 	case I915_FORMAT_MOD_Yf_TILED:
3053 		return PLANE_CTL_TILED_YF;
3054 	default:
3055 		MISSING_CASE(fb_modifier);
3056 	}
3057 
3058 	return 0;
3059 }
3060 
3061 u32 skl_plane_ctl_rotation(unsigned int rotation)
3062 {
3063 	switch (rotation) {
3064 	case BIT(DRM_ROTATE_0):
3065 		break;
3066 	/*
3067 	 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3068 	 * while i915 HW rotation is clockwise, thats why this swapping.
3069 	 */
3070 	case BIT(DRM_ROTATE_90):
3071 		return PLANE_CTL_ROTATE_270;
3072 	case BIT(DRM_ROTATE_180):
3073 		return PLANE_CTL_ROTATE_180;
3074 	case BIT(DRM_ROTATE_270):
3075 		return PLANE_CTL_ROTATE_90;
3076 	default:
3077 		MISSING_CASE(rotation);
3078 	}
3079 
3080 	return 0;
3081 }
3082 
3083 static void skylake_update_primary_plane(struct drm_crtc *crtc,
3084 					 struct drm_framebuffer *fb,
3085 					 int x, int y)
3086 {
3087 	struct drm_device *dev = crtc->dev;
3088 	struct drm_i915_private *dev_priv = dev->dev_private;
3089 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3090 	struct drm_plane *plane = crtc->primary;
3091 	bool visible = to_intel_plane_state(plane->state)->visible;
3092 	struct drm_i915_gem_object *obj;
3093 	int pipe = intel_crtc->pipe;
3094 	u32 plane_ctl, stride_div, stride;
3095 	u32 tile_height, plane_offset, plane_size;
3096 	unsigned int rotation;
3097 	int x_offset, y_offset;
3098 	unsigned long surf_addr;
3099 	struct intel_crtc_state *crtc_state = intel_crtc->config;
3100 	struct intel_plane_state *plane_state;
3101 	int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3102 	int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3103 	int scaler_id = -1;
3104 
3105 	plane_state = to_intel_plane_state(plane->state);
3106 
3107 	if (!visible || !fb) {
3108 		I915_WRITE(PLANE_CTL(pipe, 0), 0);
3109 		I915_WRITE(PLANE_SURF(pipe, 0), 0);
3110 		POSTING_READ(PLANE_CTL(pipe, 0));
3111 		return;
3112 	}
3113 
3114 	plane_ctl = PLANE_CTL_ENABLE |
3115 		    PLANE_CTL_PIPE_GAMMA_ENABLE |
3116 		    PLANE_CTL_PIPE_CSC_ENABLE;
3117 
3118 	plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3119 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3120 	plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3121 
3122 	rotation = plane->state->rotation;
3123 	plane_ctl |= skl_plane_ctl_rotation(rotation);
3124 
3125 	obj = intel_fb_obj(fb);
3126 	stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3127 					       fb->pixel_format);
3128 	surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
3129 
3130 	WARN_ON(drm_rect_width(&plane_state->src) == 0);
3131 
3132 	scaler_id = plane_state->scaler_id;
3133 	src_x = plane_state->src.x1 >> 16;
3134 	src_y = plane_state->src.y1 >> 16;
3135 	src_w = drm_rect_width(&plane_state->src) >> 16;
3136 	src_h = drm_rect_height(&plane_state->src) >> 16;
3137 	dst_x = plane_state->dst.x1;
3138 	dst_y = plane_state->dst.y1;
3139 	dst_w = drm_rect_width(&plane_state->dst);
3140 	dst_h = drm_rect_height(&plane_state->dst);
3141 
3142 	WARN_ON(x != src_x || y != src_y);
3143 
3144 	if (intel_rotation_90_or_270(rotation)) {
3145 		/* stride = Surface height in tiles */
3146 		tile_height = intel_tile_height(dev, fb->pixel_format,
3147 						fb->modifier[0], 0);
3148 		stride = DIV_ROUND_UP(fb->height, tile_height);
3149 		x_offset = stride * tile_height - y - src_h;
3150 		y_offset = x;
3151 		plane_size = (src_w - 1) << 16 | (src_h - 1);
3152 	} else {
3153 		stride = fb->pitches[0] / stride_div;
3154 		x_offset = x;
3155 		y_offset = y;
3156 		plane_size = (src_h - 1) << 16 | (src_w - 1);
3157 	}
3158 	plane_offset = y_offset << 16 | x_offset;
3159 
3160 	intel_crtc->adjusted_x = x_offset;
3161 	intel_crtc->adjusted_y = y_offset;
3162 
3163 	I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3164 	I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3165 	I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3166 	I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
3167 
3168 	if (scaler_id >= 0) {
3169 		uint32_t ps_ctrl = 0;
3170 
3171 		WARN_ON(!dst_w || !dst_h);
3172 		ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3173 			crtc_state->scaler_state.scalers[scaler_id].mode;
3174 		I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3175 		I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3176 		I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3177 		I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3178 		I915_WRITE(PLANE_POS(pipe, 0), 0);
3179 	} else {
3180 		I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3181 	}
3182 
3183 	I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
3184 
3185 	POSTING_READ(PLANE_SURF(pipe, 0));
3186 }
3187 
3188 /* Assume fb object is pinned & idle & fenced and just update base pointers */
3189 static int
3190 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3191 			   int x, int y, enum mode_set_atomic state)
3192 {
3193 	struct drm_device *dev = crtc->dev;
3194 	struct drm_i915_private *dev_priv = dev->dev_private;
3195 
3196 	if (dev_priv->fbc.disable_fbc)
3197 		dev_priv->fbc.disable_fbc(dev_priv);
3198 
3199 	dev_priv->display.update_primary_plane(crtc, fb, x, y);
3200 
3201 	return 0;
3202 }
3203 
3204 static void intel_complete_page_flips(struct drm_device *dev)
3205 {
3206 	struct drm_crtc *crtc;
3207 
3208 	for_each_crtc(dev, crtc) {
3209 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3210 		enum plane plane = intel_crtc->plane;
3211 
3212 		intel_prepare_page_flip(dev, plane);
3213 		intel_finish_page_flip_plane(dev, plane);
3214 	}
3215 }
3216 
3217 static void intel_update_primary_planes(struct drm_device *dev)
3218 {
3219 	struct drm_crtc *crtc;
3220 
3221 	for_each_crtc(dev, crtc) {
3222 		struct intel_plane *plane = to_intel_plane(crtc->primary);
3223 		struct intel_plane_state *plane_state;
3224 
3225 		drm_modeset_lock_crtc(crtc, &plane->base);
3226 
3227 		plane_state = to_intel_plane_state(plane->base.state);
3228 
3229 		if (plane_state->base.fb)
3230 			plane->commit_plane(&plane->base, plane_state);
3231 
3232 		drm_modeset_unlock_crtc(crtc);
3233 	}
3234 }
3235 
3236 void intel_prepare_reset(struct drm_device *dev)
3237 {
3238 	/* no reset support for gen2 */
3239 	if (IS_GEN2(dev))
3240 		return;
3241 
3242 	/* reset doesn't touch the display */
3243 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3244 		return;
3245 
3246 	drm_modeset_lock_all(dev);
3247 	/*
3248 	 * Disabling the crtcs gracefully seems nicer. Also the
3249 	 * g33 docs say we should at least disable all the planes.
3250 	 */
3251 	intel_display_suspend(dev);
3252 }
3253 
3254 void intel_finish_reset(struct drm_device *dev)
3255 {
3256 	struct drm_i915_private *dev_priv = to_i915(dev);
3257 
3258 	/*
3259 	 * Flips in the rings will be nuked by the reset,
3260 	 * so complete all pending flips so that user space
3261 	 * will get its events and not get stuck.
3262 	 */
3263 	intel_complete_page_flips(dev);
3264 
3265 	/* no reset support for gen2 */
3266 	if (IS_GEN2(dev))
3267 		return;
3268 
3269 	/* reset doesn't touch the display */
3270 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3271 		/*
3272 		 * Flips in the rings have been nuked by the reset,
3273 		 * so update the base address of all primary
3274 		 * planes to the the last fb to make sure we're
3275 		 * showing the correct fb after a reset.
3276 		 *
3277 		 * FIXME: Atomic will make this obsolete since we won't schedule
3278 		 * CS-based flips (which might get lost in gpu resets) any more.
3279 		 */
3280 		intel_update_primary_planes(dev);
3281 		return;
3282 	}
3283 
3284 	/*
3285 	 * The display has been reset as well,
3286 	 * so need a full re-initialization.
3287 	 */
3288 	intel_runtime_pm_disable_interrupts(dev_priv);
3289 	intel_runtime_pm_enable_interrupts(dev_priv);
3290 
3291 	intel_modeset_init_hw(dev);
3292 
3293 	spin_lock_irq(&dev_priv->irq_lock);
3294 	if (dev_priv->display.hpd_irq_setup)
3295 		dev_priv->display.hpd_irq_setup(dev);
3296 	spin_unlock_irq(&dev_priv->irq_lock);
3297 
3298 	intel_display_resume(dev);
3299 
3300 	intel_hpd_init(dev_priv);
3301 
3302 	drm_modeset_unlock_all(dev);
3303 }
3304 
3305 static void
3306 intel_finish_fb(struct drm_framebuffer *old_fb)
3307 {
3308 	struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
3309 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3310 	bool was_interruptible = dev_priv->mm.interruptible;
3311 	int ret;
3312 
3313 	/* Big Hammer, we also need to ensure that any pending
3314 	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3315 	 * current scanout is retired before unpinning the old
3316 	 * framebuffer. Note that we rely on userspace rendering
3317 	 * into the buffer attached to the pipe they are waiting
3318 	 * on. If not, userspace generates a GPU hang with IPEHR
3319 	 * point to the MI_WAIT_FOR_EVENT.
3320 	 *
3321 	 * This should only fail upon a hung GPU, in which case we
3322 	 * can safely continue.
3323 	 */
3324 	dev_priv->mm.interruptible = false;
3325 	ret = i915_gem_object_wait_rendering(obj, true);
3326 	dev_priv->mm.interruptible = was_interruptible;
3327 
3328 	WARN_ON(ret);
3329 }
3330 
3331 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3332 {
3333 	struct drm_device *dev = crtc->dev;
3334 	struct drm_i915_private *dev_priv = dev->dev_private;
3335 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3336 	bool pending;
3337 
3338 	if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3339 	    intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3340 		return false;
3341 
3342 	spin_lock_irq(&dev->event_lock);
3343 	pending = to_intel_crtc(crtc)->unpin_work != NULL;
3344 	spin_unlock_irq(&dev->event_lock);
3345 
3346 	return pending;
3347 }
3348 
3349 static void intel_update_pipe_config(struct intel_crtc *crtc,
3350 				     struct intel_crtc_state *old_crtc_state)
3351 {
3352 	struct drm_device *dev = crtc->base.dev;
3353 	struct drm_i915_private *dev_priv = dev->dev_private;
3354 	struct intel_crtc_state *pipe_config =
3355 		to_intel_crtc_state(crtc->base.state);
3356 
3357 	/* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3358 	crtc->base.mode = crtc->base.state->mode;
3359 
3360 	DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3361 		      old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3362 		      pipe_config->pipe_src_w, pipe_config->pipe_src_h);
3363 
3364 	if (HAS_DDI(dev))
3365 		intel_set_pipe_csc(&crtc->base);
3366 
3367 	/*
3368 	 * Update pipe size and adjust fitter if needed: the reason for this is
3369 	 * that in compute_mode_changes we check the native mode (not the pfit
3370 	 * mode) to see if we can flip rather than do a full mode set. In the
3371 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
3372 	 * pfit state, we'll end up with a big fb scanned out into the wrong
3373 	 * sized surface.
3374 	 */
3375 
3376 	I915_WRITE(PIPESRC(crtc->pipe),
3377 		   ((pipe_config->pipe_src_w - 1) << 16) |
3378 		   (pipe_config->pipe_src_h - 1));
3379 
3380 	/* on skylake this is done by detaching scalers */
3381 	if (INTEL_INFO(dev)->gen >= 9) {
3382 		skl_detach_scalers(crtc);
3383 
3384 		if (pipe_config->pch_pfit.enabled)
3385 			skylake_pfit_enable(crtc);
3386 	} else if (HAS_PCH_SPLIT(dev)) {
3387 		if (pipe_config->pch_pfit.enabled)
3388 			ironlake_pfit_enable(crtc);
3389 		else if (old_crtc_state->pch_pfit.enabled)
3390 			ironlake_pfit_disable(crtc, true);
3391 	}
3392 }
3393 
3394 static void intel_fdi_normal_train(struct drm_crtc *crtc)
3395 {
3396 	struct drm_device *dev = crtc->dev;
3397 	struct drm_i915_private *dev_priv = dev->dev_private;
3398 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3399 	int pipe = intel_crtc->pipe;
3400 	u32 reg, temp;
3401 
3402 	/* enable normal train */
3403 	reg = FDI_TX_CTL(pipe);
3404 	temp = I915_READ(reg);
3405 	if (IS_IVYBRIDGE(dev)) {
3406 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3407 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
3408 	} else {
3409 		temp &= ~FDI_LINK_TRAIN_NONE;
3410 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
3411 	}
3412 	I915_WRITE(reg, temp);
3413 
3414 	reg = FDI_RX_CTL(pipe);
3415 	temp = I915_READ(reg);
3416 	if (HAS_PCH_CPT(dev)) {
3417 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3418 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3419 	} else {
3420 		temp &= ~FDI_LINK_TRAIN_NONE;
3421 		temp |= FDI_LINK_TRAIN_NONE;
3422 	}
3423 	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3424 
3425 	/* wait one idle pattern time */
3426 	POSTING_READ(reg);
3427 	udelay(1000);
3428 
3429 	/* IVB wants error correction enabled */
3430 	if (IS_IVYBRIDGE(dev))
3431 		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3432 			   FDI_FE_ERRC_ENABLE);
3433 }
3434 
3435 /* The FDI link training functions for ILK/Ibexpeak. */
3436 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3437 {
3438 	struct drm_device *dev = crtc->dev;
3439 	struct drm_i915_private *dev_priv = dev->dev_private;
3440 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3441 	int pipe = intel_crtc->pipe;
3442 	u32 reg, temp, tries;
3443 
3444 	/* FDI needs bits from pipe first */
3445 	assert_pipe_enabled(dev_priv, pipe);
3446 
3447 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3448 	   for train result */
3449 	reg = FDI_RX_IMR(pipe);
3450 	temp = I915_READ(reg);
3451 	temp &= ~FDI_RX_SYMBOL_LOCK;
3452 	temp &= ~FDI_RX_BIT_LOCK;
3453 	I915_WRITE(reg, temp);
3454 	I915_READ(reg);
3455 	udelay(150);
3456 
3457 	/* enable CPU FDI TX and PCH FDI RX */
3458 	reg = FDI_TX_CTL(pipe);
3459 	temp = I915_READ(reg);
3460 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
3461 	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3462 	temp &= ~FDI_LINK_TRAIN_NONE;
3463 	temp |= FDI_LINK_TRAIN_PATTERN_1;
3464 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
3465 
3466 	reg = FDI_RX_CTL(pipe);
3467 	temp = I915_READ(reg);
3468 	temp &= ~FDI_LINK_TRAIN_NONE;
3469 	temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
3471 
3472 	POSTING_READ(reg);
3473 	udelay(150);
3474 
3475 	/* Ironlake workaround, enable clock pointer after FDI enable*/
3476 	I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3477 	I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3478 		   FDI_RX_PHASE_SYNC_POINTER_EN);
3479 
3480 	reg = FDI_RX_IIR(pipe);
3481 	for (tries = 0; tries < 5; tries++) {
3482 		temp = I915_READ(reg);
3483 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3484 
3485 		if ((temp & FDI_RX_BIT_LOCK)) {
3486 			DRM_DEBUG_KMS("FDI train 1 done.\n");
3487 			I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3488 			break;
3489 		}
3490 	}
3491 	if (tries == 5)
3492 		DRM_ERROR("FDI train 1 fail!\n");
3493 
3494 	/* Train 2 */
3495 	reg = FDI_TX_CTL(pipe);
3496 	temp = I915_READ(reg);
3497 	temp &= ~FDI_LINK_TRAIN_NONE;
3498 	temp |= FDI_LINK_TRAIN_PATTERN_2;
3499 	I915_WRITE(reg, temp);
3500 
3501 	reg = FDI_RX_CTL(pipe);
3502 	temp = I915_READ(reg);
3503 	temp &= ~FDI_LINK_TRAIN_NONE;
3504 	temp |= FDI_LINK_TRAIN_PATTERN_2;
3505 	I915_WRITE(reg, temp);
3506 
3507 	POSTING_READ(reg);
3508 	udelay(150);
3509 
3510 	reg = FDI_RX_IIR(pipe);
3511 	for (tries = 0; tries < 5; tries++) {
3512 		temp = I915_READ(reg);
3513 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3514 
3515 		if (temp & FDI_RX_SYMBOL_LOCK) {
3516 			I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3517 			DRM_DEBUG_KMS("FDI train 2 done.\n");
3518 			break;
3519 		}
3520 	}
3521 	if (tries == 5)
3522 		DRM_ERROR("FDI train 2 fail!\n");
3523 
3524 	DRM_DEBUG_KMS("FDI train done\n");
3525 
3526 }
3527 
3528 static const int snb_b_fdi_train_param[] = {
3529 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3530 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3531 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3532 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3533 };
3534 
3535 /* The FDI link training functions for SNB/Cougarpoint. */
3536 static void gen6_fdi_link_train(struct drm_crtc *crtc)
3537 {
3538 	struct drm_device *dev = crtc->dev;
3539 	struct drm_i915_private *dev_priv = dev->dev_private;
3540 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3541 	int pipe = intel_crtc->pipe;
3542 	u32 reg, temp, i, retry;
3543 
3544 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3545 	   for train result */
3546 	reg = FDI_RX_IMR(pipe);
3547 	temp = I915_READ(reg);
3548 	temp &= ~FDI_RX_SYMBOL_LOCK;
3549 	temp &= ~FDI_RX_BIT_LOCK;
3550 	I915_WRITE(reg, temp);
3551 
3552 	POSTING_READ(reg);
3553 	udelay(150);
3554 
3555 	/* enable CPU FDI TX and PCH FDI RX */
3556 	reg = FDI_TX_CTL(pipe);
3557 	temp = I915_READ(reg);
3558 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
3559 	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3560 	temp &= ~FDI_LINK_TRAIN_NONE;
3561 	temp |= FDI_LINK_TRAIN_PATTERN_1;
3562 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3563 	/* SNB-B */
3564 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3565 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
3566 
3567 	I915_WRITE(FDI_RX_MISC(pipe),
3568 		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3569 
3570 	reg = FDI_RX_CTL(pipe);
3571 	temp = I915_READ(reg);
3572 	if (HAS_PCH_CPT(dev)) {
3573 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3574 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3575 	} else {
3576 		temp &= ~FDI_LINK_TRAIN_NONE;
3577 		temp |= FDI_LINK_TRAIN_PATTERN_1;
3578 	}
3579 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
3580 
3581 	POSTING_READ(reg);
3582 	udelay(150);
3583 
3584 	for (i = 0; i < 4; i++) {
3585 		reg = FDI_TX_CTL(pipe);
3586 		temp = I915_READ(reg);
3587 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3588 		temp |= snb_b_fdi_train_param[i];
3589 		I915_WRITE(reg, temp);
3590 
3591 		POSTING_READ(reg);
3592 		udelay(500);
3593 
3594 		for (retry = 0; retry < 5; retry++) {
3595 			reg = FDI_RX_IIR(pipe);
3596 			temp = I915_READ(reg);
3597 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3598 			if (temp & FDI_RX_BIT_LOCK) {
3599 				I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3600 				DRM_DEBUG_KMS("FDI train 1 done.\n");
3601 				break;
3602 			}
3603 			udelay(50);
3604 		}
3605 		if (retry < 5)
3606 			break;
3607 	}
3608 	if (i == 4)
3609 		DRM_ERROR("FDI train 1 fail!\n");
3610 
3611 	/* Train 2 */
3612 	reg = FDI_TX_CTL(pipe);
3613 	temp = I915_READ(reg);
3614 	temp &= ~FDI_LINK_TRAIN_NONE;
3615 	temp |= FDI_LINK_TRAIN_PATTERN_2;
3616 	if (IS_GEN6(dev)) {
3617 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3618 		/* SNB-B */
3619 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3620 	}
3621 	I915_WRITE(reg, temp);
3622 
3623 	reg = FDI_RX_CTL(pipe);
3624 	temp = I915_READ(reg);
3625 	if (HAS_PCH_CPT(dev)) {
3626 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3627 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3628 	} else {
3629 		temp &= ~FDI_LINK_TRAIN_NONE;
3630 		temp |= FDI_LINK_TRAIN_PATTERN_2;
3631 	}
3632 	I915_WRITE(reg, temp);
3633 
3634 	POSTING_READ(reg);
3635 	udelay(150);
3636 
3637 	for (i = 0; i < 4; i++) {
3638 		reg = FDI_TX_CTL(pipe);
3639 		temp = I915_READ(reg);
3640 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3641 		temp |= snb_b_fdi_train_param[i];
3642 		I915_WRITE(reg, temp);
3643 
3644 		POSTING_READ(reg);
3645 		udelay(500);
3646 
3647 		for (retry = 0; retry < 5; retry++) {
3648 			reg = FDI_RX_IIR(pipe);
3649 			temp = I915_READ(reg);
3650 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3651 			if (temp & FDI_RX_SYMBOL_LOCK) {
3652 				I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3653 				DRM_DEBUG_KMS("FDI train 2 done.\n");
3654 				break;
3655 			}
3656 			udelay(50);
3657 		}
3658 		if (retry < 5)
3659 			break;
3660 	}
3661 	if (i == 4)
3662 		DRM_ERROR("FDI train 2 fail!\n");
3663 
3664 	DRM_DEBUG_KMS("FDI train done.\n");
3665 }
3666 
3667 /* Manual link training for Ivy Bridge A0 parts */
3668 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3669 {
3670 	struct drm_device *dev = crtc->dev;
3671 	struct drm_i915_private *dev_priv = dev->dev_private;
3672 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3673 	int pipe = intel_crtc->pipe;
3674 	u32 reg, temp, i, j;
3675 
3676 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3677 	   for train result */
3678 	reg = FDI_RX_IMR(pipe);
3679 	temp = I915_READ(reg);
3680 	temp &= ~FDI_RX_SYMBOL_LOCK;
3681 	temp &= ~FDI_RX_BIT_LOCK;
3682 	I915_WRITE(reg, temp);
3683 
3684 	POSTING_READ(reg);
3685 	udelay(150);
3686 
3687 	DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3688 		      I915_READ(FDI_RX_IIR(pipe)));
3689 
3690 	/* Try each vswing and preemphasis setting twice before moving on */
3691 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3692 		/* disable first in case we need to retry */
3693 		reg = FDI_TX_CTL(pipe);
3694 		temp = I915_READ(reg);
3695 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3696 		temp &= ~FDI_TX_ENABLE;
3697 		I915_WRITE(reg, temp);
3698 
3699 		reg = FDI_RX_CTL(pipe);
3700 		temp = I915_READ(reg);
3701 		temp &= ~FDI_LINK_TRAIN_AUTO;
3702 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3703 		temp &= ~FDI_RX_ENABLE;
3704 		I915_WRITE(reg, temp);
3705 
3706 		/* enable CPU FDI TX and PCH FDI RX */
3707 		reg = FDI_TX_CTL(pipe);
3708 		temp = I915_READ(reg);
3709 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
3710 		temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3711 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
3712 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3713 		temp |= snb_b_fdi_train_param[j/2];
3714 		temp |= FDI_COMPOSITE_SYNC;
3715 		I915_WRITE(reg, temp | FDI_TX_ENABLE);
3716 
3717 		I915_WRITE(FDI_RX_MISC(pipe),
3718 			   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3719 
3720 		reg = FDI_RX_CTL(pipe);
3721 		temp = I915_READ(reg);
3722 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3723 		temp |= FDI_COMPOSITE_SYNC;
3724 		I915_WRITE(reg, temp | FDI_RX_ENABLE);
3725 
3726 		POSTING_READ(reg);
3727 		udelay(1); /* should be 0.5us */
3728 
3729 		for (i = 0; i < 4; i++) {
3730 			reg = FDI_RX_IIR(pipe);
3731 			temp = I915_READ(reg);
3732 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3733 
3734 			if (temp & FDI_RX_BIT_LOCK ||
3735 			    (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3736 				I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3737 				DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3738 					      i);
3739 				break;
3740 			}
3741 			udelay(1); /* should be 0.5us */
3742 		}
3743 		if (i == 4) {
3744 			DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3745 			continue;
3746 		}
3747 
3748 		/* Train 2 */
3749 		reg = FDI_TX_CTL(pipe);
3750 		temp = I915_READ(reg);
3751 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3752 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3753 		I915_WRITE(reg, temp);
3754 
3755 		reg = FDI_RX_CTL(pipe);
3756 		temp = I915_READ(reg);
3757 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3758 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3759 		I915_WRITE(reg, temp);
3760 
3761 		POSTING_READ(reg);
3762 		udelay(2); /* should be 1.5us */
3763 
3764 		for (i = 0; i < 4; i++) {
3765 			reg = FDI_RX_IIR(pipe);
3766 			temp = I915_READ(reg);
3767 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3768 
3769 			if (temp & FDI_RX_SYMBOL_LOCK ||
3770 			    (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3771 				I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3772 				DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3773 					      i);
3774 				goto train_done;
3775 			}
3776 			udelay(2); /* should be 1.5us */
3777 		}
3778 		if (i == 4)
3779 			DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
3780 	}
3781 
3782 train_done:
3783 	DRM_DEBUG_KMS("FDI train done.\n");
3784 }
3785 
3786 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
3787 {
3788 	struct drm_device *dev = intel_crtc->base.dev;
3789 	struct drm_i915_private *dev_priv = dev->dev_private;
3790 	int pipe = intel_crtc->pipe;
3791 	u32 reg, temp;
3792 
3793 
3794 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
3795 	reg = FDI_RX_CTL(pipe);
3796 	temp = I915_READ(reg);
3797 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3798 	temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3799 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3800 	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3801 
3802 	POSTING_READ(reg);
3803 	udelay(200);
3804 
3805 	/* Switch from Rawclk to PCDclk */
3806 	temp = I915_READ(reg);
3807 	I915_WRITE(reg, temp | FDI_PCDCLK);
3808 
3809 	POSTING_READ(reg);
3810 	udelay(200);
3811 
3812 	/* Enable CPU FDI TX PLL, always on for Ironlake */
3813 	reg = FDI_TX_CTL(pipe);
3814 	temp = I915_READ(reg);
3815 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3816 		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
3817 
3818 		POSTING_READ(reg);
3819 		udelay(100);
3820 	}
3821 }
3822 
3823 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3824 {
3825 	struct drm_device *dev = intel_crtc->base.dev;
3826 	struct drm_i915_private *dev_priv = dev->dev_private;
3827 	int pipe = intel_crtc->pipe;
3828 	u32 reg, temp;
3829 
3830 	/* Switch from PCDclk to Rawclk */
3831 	reg = FDI_RX_CTL(pipe);
3832 	temp = I915_READ(reg);
3833 	I915_WRITE(reg, temp & ~FDI_PCDCLK);
3834 
3835 	/* Disable CPU FDI TX PLL */
3836 	reg = FDI_TX_CTL(pipe);
3837 	temp = I915_READ(reg);
3838 	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3839 
3840 	POSTING_READ(reg);
3841 	udelay(100);
3842 
3843 	reg = FDI_RX_CTL(pipe);
3844 	temp = I915_READ(reg);
3845 	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3846 
3847 	/* Wait for the clocks to turn off. */
3848 	POSTING_READ(reg);
3849 	udelay(100);
3850 }
3851 
3852 static void ironlake_fdi_disable(struct drm_crtc *crtc)
3853 {
3854 	struct drm_device *dev = crtc->dev;
3855 	struct drm_i915_private *dev_priv = dev->dev_private;
3856 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3857 	int pipe = intel_crtc->pipe;
3858 	u32 reg, temp;
3859 
3860 	/* disable CPU FDI tx and PCH FDI rx */
3861 	reg = FDI_TX_CTL(pipe);
3862 	temp = I915_READ(reg);
3863 	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3864 	POSTING_READ(reg);
3865 
3866 	reg = FDI_RX_CTL(pipe);
3867 	temp = I915_READ(reg);
3868 	temp &= ~(0x7 << 16);
3869 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3870 	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3871 
3872 	POSTING_READ(reg);
3873 	udelay(100);
3874 
3875 	/* Ironlake workaround, disable clock pointer after downing FDI */
3876 	if (HAS_PCH_IBX(dev))
3877 		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3878 
3879 	/* still set train pattern 1 */
3880 	reg = FDI_TX_CTL(pipe);
3881 	temp = I915_READ(reg);
3882 	temp &= ~FDI_LINK_TRAIN_NONE;
3883 	temp |= FDI_LINK_TRAIN_PATTERN_1;
3884 	I915_WRITE(reg, temp);
3885 
3886 	reg = FDI_RX_CTL(pipe);
3887 	temp = I915_READ(reg);
3888 	if (HAS_PCH_CPT(dev)) {
3889 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3890 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3891 	} else {
3892 		temp &= ~FDI_LINK_TRAIN_NONE;
3893 		temp |= FDI_LINK_TRAIN_PATTERN_1;
3894 	}
3895 	/* BPC in FDI rx is consistent with that in PIPECONF */
3896 	temp &= ~(0x07 << 16);
3897 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3898 	I915_WRITE(reg, temp);
3899 
3900 	POSTING_READ(reg);
3901 	udelay(100);
3902 }
3903 
3904 bool intel_has_pending_fb_unpin(struct drm_device *dev)
3905 {
3906 	struct intel_crtc *crtc;
3907 
3908 	/* Note that we don't need to be called with mode_config.lock here
3909 	 * as our list of CRTC objects is static for the lifetime of the
3910 	 * device and so cannot disappear as we iterate. Similarly, we can
3911 	 * happily treat the predicates as racy, atomic checks as userspace
3912 	 * cannot claim and pin a new fb without at least acquring the
3913 	 * struct_mutex and so serialising with us.
3914 	 */
3915 	for_each_intel_crtc(dev, crtc) {
3916 		if (atomic_read(&crtc->unpin_work_count) == 0)
3917 			continue;
3918 
3919 		if (crtc->unpin_work)
3920 			intel_wait_for_vblank(dev, crtc->pipe);
3921 
3922 		return true;
3923 	}
3924 
3925 	return false;
3926 }
3927 
3928 static void page_flip_completed(struct intel_crtc *intel_crtc)
3929 {
3930 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3931 	struct intel_unpin_work *work = intel_crtc->unpin_work;
3932 
3933 	/* ensure that the unpin work is consistent wrt ->pending. */
3934 	smp_rmb();
3935 	intel_crtc->unpin_work = NULL;
3936 
3937 	if (work->event)
3938 		drm_send_vblank_event(intel_crtc->base.dev,
3939 				      intel_crtc->pipe,
3940 				      work->event);
3941 
3942 	drm_crtc_vblank_put(&intel_crtc->base);
3943 
3944 	wake_up_all(&dev_priv->pending_flip_queue);
3945 	queue_work(dev_priv->wq, &work->work);
3946 
3947 	trace_i915_flip_complete(intel_crtc->plane,
3948 				 work->pending_flip_obj);
3949 }
3950 
3951 void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3952 {
3953 	struct drm_device *dev = crtc->dev;
3954 	struct drm_i915_private *dev_priv = dev->dev_private;
3955 
3956 	WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3957 	if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3958 				       !intel_crtc_has_pending_flip(crtc),
3959 				       60*HZ) == 0)) {
3960 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3961 
3962 		spin_lock_irq(&dev->event_lock);
3963 		if (intel_crtc->unpin_work) {
3964 			WARN_ONCE(1, "Removing stuck page flip\n");
3965 			page_flip_completed(intel_crtc);
3966 		}
3967 		spin_unlock_irq(&dev->event_lock);
3968 	}
3969 
3970 	if (crtc->primary->fb) {
3971 		mutex_lock(&dev->struct_mutex);
3972 		intel_finish_fb(crtc->primary->fb);
3973 		mutex_unlock(&dev->struct_mutex);
3974 	}
3975 }
3976 
3977 /* Program iCLKIP clock to the desired frequency */
3978 static void lpt_program_iclkip(struct drm_crtc *crtc)
3979 {
3980 	struct drm_device *dev = crtc->dev;
3981 	struct drm_i915_private *dev_priv = dev->dev_private;
3982 	int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
3983 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
3984 	u32 temp;
3985 
3986 	mutex_lock(&dev_priv->sb_lock);
3987 
3988 	/* It is necessary to ungate the pixclk gate prior to programming
3989 	 * the divisors, and gate it back when it is done.
3990 	 */
3991 	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3992 
3993 	/* Disable SSCCTL */
3994 	intel_sbi_write(dev_priv, SBI_SSCCTL6,
3995 			intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3996 				SBI_SSCCTL_DISABLE,
3997 			SBI_ICLK);
3998 
3999 	/* 20MHz is a corner case which is out of range for the 7-bit divisor */
4000 	if (clock == 20000) {
4001 		auxdiv = 1;
4002 		divsel = 0x41;
4003 		phaseinc = 0x20;
4004 	} else {
4005 		/* The iCLK virtual clock root frequency is in MHz,
4006 		 * but the adjusted_mode->crtc_clock in in KHz. To get the
4007 		 * divisors, it is necessary to divide one by another, so we
4008 		 * convert the virtual clock precision to KHz here for higher
4009 		 * precision.
4010 		 */
4011 		u32 iclk_virtual_root_freq = 172800 * 1000;
4012 		u32 iclk_pi_range = 64;
4013 		u32 desired_divisor, msb_divisor_value, pi_value;
4014 
4015 		desired_divisor = (iclk_virtual_root_freq / clock);
4016 		msb_divisor_value = desired_divisor / iclk_pi_range;
4017 		pi_value = desired_divisor % iclk_pi_range;
4018 
4019 		auxdiv = 0;
4020 		divsel = msb_divisor_value - 2;
4021 		phaseinc = pi_value;
4022 	}
4023 
4024 	/* This should not happen with any sane values */
4025 	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4026 		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4027 	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4028 		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4029 
4030 	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
4031 			clock,
4032 			auxdiv,
4033 			divsel,
4034 			phasedir,
4035 			phaseinc);
4036 
4037 	/* Program SSCDIVINTPHASE6 */
4038 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4039 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4040 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4041 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4042 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4043 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4044 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
4045 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
4046 
4047 	/* Program SSCAUXDIV */
4048 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4049 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4050 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
4051 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
4052 
4053 	/* Enable modulator and associated divider */
4054 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4055 	temp &= ~SBI_SSCCTL_DISABLE;
4056 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4057 
4058 	/* Wait for initialization time */
4059 	udelay(24);
4060 
4061 	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4062 
4063 	mutex_unlock(&dev_priv->sb_lock);
4064 }
4065 
4066 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4067 						enum i915_pipe pch_transcoder)
4068 {
4069 	struct drm_device *dev = crtc->base.dev;
4070 	struct drm_i915_private *dev_priv = dev->dev_private;
4071 	enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
4072 
4073 	I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4074 		   I915_READ(HTOTAL(cpu_transcoder)));
4075 	I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4076 		   I915_READ(HBLANK(cpu_transcoder)));
4077 	I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4078 		   I915_READ(HSYNC(cpu_transcoder)));
4079 
4080 	I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4081 		   I915_READ(VTOTAL(cpu_transcoder)));
4082 	I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4083 		   I915_READ(VBLANK(cpu_transcoder)));
4084 	I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4085 		   I915_READ(VSYNC(cpu_transcoder)));
4086 	I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4087 		   I915_READ(VSYNCSHIFT(cpu_transcoder)));
4088 }
4089 
4090 static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
4091 {
4092 	struct drm_i915_private *dev_priv = dev->dev_private;
4093 	uint32_t temp;
4094 
4095 	temp = I915_READ(SOUTH_CHICKEN1);
4096 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
4097 		return;
4098 
4099 	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4100 	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4101 
4102 	temp &= ~FDI_BC_BIFURCATION_SELECT;
4103 	if (enable)
4104 		temp |= FDI_BC_BIFURCATION_SELECT;
4105 
4106 	DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
4107 	I915_WRITE(SOUTH_CHICKEN1, temp);
4108 	POSTING_READ(SOUTH_CHICKEN1);
4109 }
4110 
4111 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4112 {
4113 	struct drm_device *dev = intel_crtc->base.dev;
4114 
4115 	switch (intel_crtc->pipe) {
4116 	case PIPE_A:
4117 		break;
4118 	case PIPE_B:
4119 		if (intel_crtc->config->fdi_lanes > 2)
4120 			cpt_set_fdi_bc_bifurcation(dev, false);
4121 		else
4122 			cpt_set_fdi_bc_bifurcation(dev, true);
4123 
4124 		break;
4125 	case PIPE_C:
4126 		cpt_set_fdi_bc_bifurcation(dev, true);
4127 
4128 		break;
4129 	default:
4130 		BUG();
4131 	}
4132 }
4133 
4134 /*
4135  * Enable PCH resources required for PCH ports:
4136  *   - PCH PLLs
4137  *   - FDI training & RX/TX
4138  *   - update transcoder timings
4139  *   - DP transcoding bits
4140  *   - transcoder
4141  */
4142 static void ironlake_pch_enable(struct drm_crtc *crtc)
4143 {
4144 	struct drm_device *dev = crtc->dev;
4145 	struct drm_i915_private *dev_priv = dev->dev_private;
4146 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4147 	int pipe = intel_crtc->pipe;
4148 	u32 reg, temp;
4149 
4150 	assert_pch_transcoder_disabled(dev_priv, pipe);
4151 
4152 	if (IS_IVYBRIDGE(dev))
4153 		ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4154 
4155 	/* Write the TU size bits before fdi link training, so that error
4156 	 * detection works. */
4157 	I915_WRITE(FDI_RX_TUSIZE1(pipe),
4158 		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4159 
4160 	/* For PCH output, training FDI link */
4161 	dev_priv->display.fdi_link_train(crtc);
4162 
4163 	/* We need to program the right clock selection before writing the pixel
4164 	 * mutliplier into the DPLL. */
4165 	if (HAS_PCH_CPT(dev)) {
4166 		u32 sel;
4167 
4168 		temp = I915_READ(PCH_DPLL_SEL);
4169 		temp |= TRANS_DPLL_ENABLE(pipe);
4170 		sel = TRANS_DPLLB_SEL(pipe);
4171 		if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
4172 			temp |= sel;
4173 		else
4174 			temp &= ~sel;
4175 		I915_WRITE(PCH_DPLL_SEL, temp);
4176 	}
4177 
4178 	/* XXX: pch pll's can be enabled any time before we enable the PCH
4179 	 * transcoder, and we actually should do this to not upset any PCH
4180 	 * transcoder that already use the clock when we share it.
4181 	 *
4182 	 * Note that enable_shared_dpll tries to do the right thing, but
4183 	 * get_shared_dpll unconditionally resets the pll - we need that to have
4184 	 * the right LVDS enable sequence. */
4185 	intel_enable_shared_dpll(intel_crtc);
4186 
4187 	/* set transcoder timing, panel must allow it */
4188 	assert_panel_unlocked(dev_priv, pipe);
4189 	ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
4190 
4191 	intel_fdi_normal_train(crtc);
4192 
4193 	/* For PCH DP, enable TRANS_DP_CTL */
4194 	if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
4195 		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
4196 		reg = TRANS_DP_CTL(pipe);
4197 		temp = I915_READ(reg);
4198 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
4199 			  TRANS_DP_SYNC_MASK |
4200 			  TRANS_DP_BPC_MASK);
4201 		temp |= TRANS_DP_OUTPUT_ENABLE;
4202 		temp |= bpc << 9; /* same format but at 11:9 */
4203 
4204 		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
4205 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
4206 		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
4207 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
4208 
4209 		switch (intel_trans_dp_port_sel(crtc)) {
4210 		case PCH_DP_B:
4211 			temp |= TRANS_DP_PORT_SEL_B;
4212 			break;
4213 		case PCH_DP_C:
4214 			temp |= TRANS_DP_PORT_SEL_C;
4215 			break;
4216 		case PCH_DP_D:
4217 			temp |= TRANS_DP_PORT_SEL_D;
4218 			break;
4219 		default:
4220 			BUG();
4221 		}
4222 
4223 		I915_WRITE(reg, temp);
4224 	}
4225 
4226 	ironlake_enable_pch_transcoder(dev_priv, pipe);
4227 }
4228 
4229 static void lpt_pch_enable(struct drm_crtc *crtc)
4230 {
4231 	struct drm_device *dev = crtc->dev;
4232 	struct drm_i915_private *dev_priv = dev->dev_private;
4233 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4234 	enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4235 
4236 	assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
4237 
4238 	lpt_program_iclkip(crtc);
4239 
4240 	/* Set transcoder timing. */
4241 	ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
4242 
4243 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
4244 }
4245 
4246 struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4247 						struct intel_crtc_state *crtc_state)
4248 {
4249 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
4250 	struct intel_shared_dpll *pll;
4251 	struct intel_shared_dpll_config *shared_dpll;
4252 	enum intel_dpll_id i;
4253 	int max = dev_priv->num_shared_dpll;
4254 
4255 	shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4256 
4257 	if (HAS_PCH_IBX(dev_priv->dev)) {
4258 		/* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
4259 		i = (enum intel_dpll_id) crtc->pipe;
4260 		pll = &dev_priv->shared_dplls[i];
4261 
4262 		DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4263 			      crtc->base.base.id, pll->name);
4264 
4265 		WARN_ON(shared_dpll[i].crtc_mask);
4266 
4267 		goto found;
4268 	}
4269 
4270 	if (IS_BROXTON(dev_priv->dev)) {
4271 		/* PLL is attached to port in bxt */
4272 		struct intel_encoder *encoder;
4273 		struct intel_digital_port *intel_dig_port;
4274 
4275 		encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4276 		if (WARN_ON(!encoder))
4277 			return NULL;
4278 
4279 		intel_dig_port = enc_to_dig_port(&encoder->base);
4280 		/* 1:1 mapping between ports and PLLs */
4281 		i = (enum intel_dpll_id)intel_dig_port->port;
4282 		pll = &dev_priv->shared_dplls[i];
4283 		DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4284 			crtc->base.base.id, pll->name);
4285 		WARN_ON(shared_dpll[i].crtc_mask);
4286 
4287 		goto found;
4288 	} else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
4289 		/* Do not consider SPLL */
4290 		max = 2;
4291 
4292 	for (i = 0; i < max; i++) {
4293 		pll = &dev_priv->shared_dplls[i];
4294 
4295 		/* Only want to check enabled timings first */
4296 		if (shared_dpll[i].crtc_mask == 0)
4297 			continue;
4298 
4299 		if (memcmp(&crtc_state->dpll_hw_state,
4300 			   &shared_dpll[i].hw_state,
4301 			   sizeof(crtc_state->dpll_hw_state)) == 0) {
4302 			DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
4303 				      crtc->base.base.id, pll->name,
4304 				      shared_dpll[i].crtc_mask,
4305 				      pll->active);
4306 			goto found;
4307 		}
4308 	}
4309 
4310 	/* Ok no matching timings, maybe there's a free one? */
4311 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4312 		pll = &dev_priv->shared_dplls[i];
4313 		if (shared_dpll[i].crtc_mask == 0) {
4314 			DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4315 				      crtc->base.base.id, pll->name);
4316 			goto found;
4317 		}
4318 	}
4319 
4320 	return NULL;
4321 
4322 found:
4323 	if (shared_dpll[i].crtc_mask == 0)
4324 		shared_dpll[i].hw_state =
4325 			crtc_state->dpll_hw_state;
4326 
4327 	crtc_state->shared_dpll = i;
4328 	DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4329 			 pipe_name(crtc->pipe));
4330 
4331 	shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
4332 
4333 	return pll;
4334 }
4335 
4336 static void intel_shared_dpll_commit(struct drm_atomic_state *state)
4337 {
4338 	struct drm_i915_private *dev_priv = to_i915(state->dev);
4339 	struct intel_shared_dpll_config *shared_dpll;
4340 	struct intel_shared_dpll *pll;
4341 	enum intel_dpll_id i;
4342 
4343 	if (!to_intel_atomic_state(state)->dpll_set)
4344 		return;
4345 
4346 	shared_dpll = to_intel_atomic_state(state)->shared_dpll;
4347 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4348 		pll = &dev_priv->shared_dplls[i];
4349 		pll->config = shared_dpll[i];
4350 	}
4351 }
4352 
4353 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
4354 {
4355 	struct drm_i915_private *dev_priv = dev->dev_private;
4356 	int dslreg = PIPEDSL(pipe);
4357 	u32 temp;
4358 
4359 	temp = I915_READ(dslreg);
4360 	udelay(500);
4361 	if (wait_for(I915_READ(dslreg) != temp, 5)) {
4362 		if (wait_for(I915_READ(dslreg) != temp, 5))
4363 			DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
4364 	}
4365 }
4366 
4367 static int
4368 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4369 		  unsigned scaler_user, int *scaler_id, unsigned int rotation,
4370 		  int src_w, int src_h, int dst_w, int dst_h)
4371 {
4372 	struct intel_crtc_scaler_state *scaler_state =
4373 		&crtc_state->scaler_state;
4374 	struct intel_crtc *intel_crtc =
4375 		to_intel_crtc(crtc_state->base.crtc);
4376 	int need_scaling;
4377 
4378 	need_scaling = intel_rotation_90_or_270(rotation) ?
4379 		(src_h != dst_w || src_w != dst_h):
4380 		(src_w != dst_w || src_h != dst_h);
4381 
4382 	/*
4383 	 * if plane is being disabled or scaler is no more required or force detach
4384 	 *  - free scaler binded to this plane/crtc
4385 	 *  - in order to do this, update crtc->scaler_usage
4386 	 *
4387 	 * Here scaler state in crtc_state is set free so that
4388 	 * scaler can be assigned to other user. Actual register
4389 	 * update to free the scaler is done in plane/panel-fit programming.
4390 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4391 	 */
4392 	if (force_detach || !need_scaling) {
4393 		if (*scaler_id >= 0) {
4394 			scaler_state->scaler_users &= ~(1 << scaler_user);
4395 			scaler_state->scalers[*scaler_id].in_use = 0;
4396 
4397 			DRM_DEBUG_KMS("scaler_user index %u.%u: "
4398 				"Staged freeing scaler id %d scaler_users = 0x%x\n",
4399 				intel_crtc->pipe, scaler_user, *scaler_id,
4400 				scaler_state->scaler_users);
4401 			*scaler_id = -1;
4402 		}
4403 		return 0;
4404 	}
4405 
4406 	/* range checks */
4407 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4408 		dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4409 
4410 		src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4411 		dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4412 		DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
4413 			"size is out of scaler range\n",
4414 			intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
4415 		return -EINVAL;
4416 	}
4417 
4418 	/* mark this plane as a scaler user in crtc_state */
4419 	scaler_state->scaler_users |= (1 << scaler_user);
4420 	DRM_DEBUG_KMS("scaler_user index %u.%u: "
4421 		"staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4422 		intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4423 		scaler_state->scaler_users);
4424 
4425 	return 0;
4426 }
4427 
4428 /**
4429  * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4430  *
4431  * @state: crtc's scaler state
4432  *
4433  * Return
4434  *     0 - scaler_usage updated successfully
4435  *    error - requested scaling cannot be supported or other error condition
4436  */
4437 int skl_update_scaler_crtc(struct intel_crtc_state *state)
4438 {
4439 	struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4440 	const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
4441 
4442 	DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4443 		      intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4444 
4445 	return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
4446 		&state->scaler_state.scaler_id, DRM_ROTATE_0,
4447 		state->pipe_src_w, state->pipe_src_h,
4448 		adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
4449 }
4450 
4451 /**
4452  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4453  *
4454  * @state: crtc's scaler state
4455  * @plane_state: atomic plane state to update
4456  *
4457  * Return
4458  *     0 - scaler_usage updated successfully
4459  *    error - requested scaling cannot be supported or other error condition
4460  */
4461 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4462 				   struct intel_plane_state *plane_state)
4463 {
4464 
4465 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
4466 	struct intel_plane *intel_plane =
4467 		to_intel_plane(plane_state->base.plane);
4468 	struct drm_framebuffer *fb = plane_state->base.fb;
4469 	int ret;
4470 
4471 	bool force_detach = !fb || !plane_state->visible;
4472 
4473 	DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4474 		      intel_plane->base.base.id, intel_crtc->pipe,
4475 		      drm_plane_index(&intel_plane->base));
4476 
4477 	ret = skl_update_scaler(crtc_state, force_detach,
4478 				drm_plane_index(&intel_plane->base),
4479 				&plane_state->scaler_id,
4480 				plane_state->base.rotation,
4481 				drm_rect_width(&plane_state->src) >> 16,
4482 				drm_rect_height(&plane_state->src) >> 16,
4483 				drm_rect_width(&plane_state->dst),
4484 				drm_rect_height(&plane_state->dst));
4485 
4486 	if (ret || plane_state->scaler_id < 0)
4487 		return ret;
4488 
4489 	/* check colorkey */
4490 	if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
4491 		DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
4492 			      intel_plane->base.base.id);
4493 		return -EINVAL;
4494 	}
4495 
4496 	/* Check src format */
4497 	switch (fb->pixel_format) {
4498 	case DRM_FORMAT_RGB565:
4499 	case DRM_FORMAT_XBGR8888:
4500 	case DRM_FORMAT_XRGB8888:
4501 	case DRM_FORMAT_ABGR8888:
4502 	case DRM_FORMAT_ARGB8888:
4503 	case DRM_FORMAT_XRGB2101010:
4504 	case DRM_FORMAT_XBGR2101010:
4505 	case DRM_FORMAT_YUYV:
4506 	case DRM_FORMAT_YVYU:
4507 	case DRM_FORMAT_UYVY:
4508 	case DRM_FORMAT_VYUY:
4509 		break;
4510 	default:
4511 		DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4512 			intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4513 		return -EINVAL;
4514 	}
4515 
4516 	return 0;
4517 }
4518 
4519 static void skylake_scaler_disable(struct intel_crtc *crtc)
4520 {
4521 	int i;
4522 
4523 	for (i = 0; i < crtc->num_scalers; i++)
4524 		skl_detach_scaler(crtc, i);
4525 }
4526 
4527 static void skylake_pfit_enable(struct intel_crtc *crtc)
4528 {
4529 	struct drm_device *dev = crtc->base.dev;
4530 	struct drm_i915_private *dev_priv = dev->dev_private;
4531 	int pipe = crtc->pipe;
4532 	struct intel_crtc_scaler_state *scaler_state =
4533 		&crtc->config->scaler_state;
4534 
4535 	DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4536 
4537 	if (crtc->config->pch_pfit.enabled) {
4538 		int id;
4539 
4540 		if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4541 			DRM_ERROR("Requesting pfit without getting a scaler first\n");
4542 			return;
4543 		}
4544 
4545 		id = scaler_state->scaler_id;
4546 		I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4547 			PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4548 		I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4549 		I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4550 
4551 		DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
4552 	}
4553 }
4554 
4555 static void ironlake_pfit_enable(struct intel_crtc *crtc)
4556 {
4557 	struct drm_device *dev = crtc->base.dev;
4558 	struct drm_i915_private *dev_priv = dev->dev_private;
4559 	int pipe = crtc->pipe;
4560 
4561 	if (crtc->config->pch_pfit.enabled) {
4562 		/* Force use of hard-coded filter coefficients
4563 		 * as some pre-programmed values are broken,
4564 		 * e.g. x201.
4565 		 */
4566 		if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4567 			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4568 						 PF_PIPE_SEL_IVB(pipe));
4569 		else
4570 			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
4571 		I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4572 		I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
4573 	}
4574 }
4575 
4576 void hsw_enable_ips(struct intel_crtc *crtc)
4577 {
4578 	struct drm_device *dev = crtc->base.dev;
4579 	struct drm_i915_private *dev_priv = dev->dev_private;
4580 
4581 	if (!crtc->config->ips_enabled)
4582 		return;
4583 
4584 	/* We can only enable IPS after we enable a plane and wait for a vblank */
4585 	intel_wait_for_vblank(dev, crtc->pipe);
4586 
4587 	assert_plane_enabled(dev_priv, crtc->plane);
4588 	if (IS_BROADWELL(dev)) {
4589 		mutex_lock(&dev_priv->rps.hw_lock);
4590 		WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4591 		mutex_unlock(&dev_priv->rps.hw_lock);
4592 		/* Quoting Art Runyan: "its not safe to expect any particular
4593 		 * value in IPS_CTL bit 31 after enabling IPS through the
4594 		 * mailbox." Moreover, the mailbox may return a bogus state,
4595 		 * so we need to just enable it and continue on.
4596 		 */
4597 	} else {
4598 		I915_WRITE(IPS_CTL, IPS_ENABLE);
4599 		/* The bit only becomes 1 in the next vblank, so this wait here
4600 		 * is essentially intel_wait_for_vblank. If we don't have this
4601 		 * and don't wait for vblanks until the end of crtc_enable, then
4602 		 * the HW state readout code will complain that the expected
4603 		 * IPS_CTL value is not the one we read. */
4604 		if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4605 			DRM_ERROR("Timed out waiting for IPS enable\n");
4606 	}
4607 }
4608 
4609 void hsw_disable_ips(struct intel_crtc *crtc)
4610 {
4611 	struct drm_device *dev = crtc->base.dev;
4612 	struct drm_i915_private *dev_priv = dev->dev_private;
4613 
4614 	if (!crtc->config->ips_enabled)
4615 		return;
4616 
4617 	assert_plane_enabled(dev_priv, crtc->plane);
4618 	if (IS_BROADWELL(dev)) {
4619 		mutex_lock(&dev_priv->rps.hw_lock);
4620 		WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4621 		mutex_unlock(&dev_priv->rps.hw_lock);
4622 		/* wait for pcode to finish disabling IPS, which may take up to 42ms */
4623 		if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4624 			DRM_ERROR("Timed out waiting for IPS disable\n");
4625 	} else {
4626 		I915_WRITE(IPS_CTL, 0);
4627 		POSTING_READ(IPS_CTL);
4628 	}
4629 
4630 	/* We need to wait for a vblank before we can disable the plane. */
4631 	intel_wait_for_vblank(dev, crtc->pipe);
4632 }
4633 
4634 /** Loads the palette/gamma unit for the CRTC with the prepared values */
4635 static void intel_crtc_load_lut(struct drm_crtc *crtc)
4636 {
4637 	struct drm_device *dev = crtc->dev;
4638 	struct drm_i915_private *dev_priv = dev->dev_private;
4639 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4640 	enum i915_pipe pipe = intel_crtc->pipe;
4641 	int i;
4642 	bool reenable_ips = false;
4643 
4644 	/* The clocks have to be on to load the palette. */
4645 	if (!crtc->state->active)
4646 		return;
4647 
4648 	if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
4649 		if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
4650 			assert_dsi_pll_enabled(dev_priv);
4651 		else
4652 			assert_pll_enabled(dev_priv, pipe);
4653 	}
4654 
4655 	/* Workaround : Do not read or write the pipe palette/gamma data while
4656 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4657 	 */
4658 	if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
4659 	    ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4660 	     GAMMA_MODE_MODE_SPLIT)) {
4661 		hsw_disable_ips(intel_crtc);
4662 		reenable_ips = true;
4663 	}
4664 
4665 	for (i = 0; i < 256; i++) {
4666 		u32 palreg;
4667 
4668 		if (HAS_GMCH_DISPLAY(dev))
4669 			palreg = PALETTE(pipe, i);
4670 		else
4671 			palreg = LGC_PALETTE(pipe, i);
4672 
4673 		I915_WRITE(palreg,
4674 			   (intel_crtc->lut_r[i] << 16) |
4675 			   (intel_crtc->lut_g[i] << 8) |
4676 			   intel_crtc->lut_b[i]);
4677 	}
4678 
4679 	if (reenable_ips)
4680 		hsw_enable_ips(intel_crtc);
4681 }
4682 
4683 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
4684 {
4685 	if (intel_crtc->overlay) {
4686 		struct drm_device *dev = intel_crtc->base.dev;
4687 		struct drm_i915_private *dev_priv = dev->dev_private;
4688 
4689 		mutex_lock(&dev->struct_mutex);
4690 		dev_priv->mm.interruptible = false;
4691 		(void) intel_overlay_switch_off(intel_crtc->overlay);
4692 		dev_priv->mm.interruptible = true;
4693 		mutex_unlock(&dev->struct_mutex);
4694 	}
4695 
4696 	/* Let userspace switch the overlay on again. In most cases userspace
4697 	 * has to recompute where to put it anyway.
4698 	 */
4699 }
4700 
4701 /**
4702  * intel_post_enable_primary - Perform operations after enabling primary plane
4703  * @crtc: the CRTC whose primary plane was just enabled
4704  *
4705  * Performs potentially sleeping operations that must be done after the primary
4706  * plane is enabled, such as updating FBC and IPS.  Note that this may be
4707  * called due to an explicit primary plane update, or due to an implicit
4708  * re-enable that is caused when a sprite plane is updated to no longer
4709  * completely hide the primary plane.
4710  */
4711 static void
4712 intel_post_enable_primary(struct drm_crtc *crtc)
4713 {
4714 	struct drm_device *dev = crtc->dev;
4715 	struct drm_i915_private *dev_priv = dev->dev_private;
4716 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4717 	int pipe = intel_crtc->pipe;
4718 
4719 	/*
4720 	 * BDW signals flip done immediately if the plane
4721 	 * is disabled, even if the plane enable is already
4722 	 * armed to occur at the next vblank :(
4723 	 */
4724 	if (IS_BROADWELL(dev))
4725 		intel_wait_for_vblank(dev, pipe);
4726 
4727 	/*
4728 	 * FIXME IPS should be fine as long as one plane is
4729 	 * enabled, but in practice it seems to have problems
4730 	 * when going from primary only to sprite only and vice
4731 	 * versa.
4732 	 */
4733 	hsw_enable_ips(intel_crtc);
4734 
4735 	/*
4736 	 * Gen2 reports pipe underruns whenever all planes are disabled.
4737 	 * So don't enable underrun reporting before at least some planes
4738 	 * are enabled.
4739 	 * FIXME: Need to fix the logic to work when we turn off all planes
4740 	 * but leave the pipe running.
4741 	 */
4742 	if (IS_GEN2(dev))
4743 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4744 
4745 	/* Underruns don't raise interrupts, so check manually. */
4746 	if (HAS_GMCH_DISPLAY(dev))
4747 		i9xx_check_fifo_underruns(dev_priv);
4748 }
4749 
4750 /**
4751  * intel_pre_disable_primary - Perform operations before disabling primary plane
4752  * @crtc: the CRTC whose primary plane is to be disabled
4753  *
4754  * Performs potentially sleeping operations that must be done before the
4755  * primary plane is disabled, such as updating FBC and IPS.  Note that this may
4756  * be called due to an explicit primary plane update, or due to an implicit
4757  * disable that is caused when a sprite plane completely hides the primary
4758  * plane.
4759  */
4760 static void
4761 intel_pre_disable_primary(struct drm_crtc *crtc)
4762 {
4763 	struct drm_device *dev = crtc->dev;
4764 	struct drm_i915_private *dev_priv = dev->dev_private;
4765 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4766 	int pipe = intel_crtc->pipe;
4767 
4768 	/*
4769 	 * Gen2 reports pipe underruns whenever all planes are disabled.
4770 	 * So diasble underrun reporting before all the planes get disabled.
4771 	 * FIXME: Need to fix the logic to work when we turn off all planes
4772 	 * but leave the pipe running.
4773 	 */
4774 	if (IS_GEN2(dev))
4775 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4776 
4777 	/*
4778 	 * Vblank time updates from the shadow to live plane control register
4779 	 * are blocked if the memory self-refresh mode is active at that
4780 	 * moment. So to make sure the plane gets truly disabled, disable
4781 	 * first the self-refresh mode. The self-refresh enable bit in turn
4782 	 * will be checked/applied by the HW only at the next frame start
4783 	 * event which is after the vblank start event, so we need to have a
4784 	 * wait-for-vblank between disabling the plane and the pipe.
4785 	 */
4786 	if (HAS_GMCH_DISPLAY(dev)) {
4787 		intel_set_memory_cxsr(dev_priv, false);
4788 		dev_priv->wm.vlv.cxsr = false;
4789 		intel_wait_for_vblank(dev, pipe);
4790 	}
4791 
4792 	/*
4793 	 * FIXME IPS should be fine as long as one plane is
4794 	 * enabled, but in practice it seems to have problems
4795 	 * when going from primary only to sprite only and vice
4796 	 * versa.
4797 	 */
4798 	hsw_disable_ips(intel_crtc);
4799 }
4800 
4801 static void intel_post_plane_update(struct intel_crtc *crtc)
4802 {
4803 	struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4804 	struct drm_device *dev = crtc->base.dev;
4805 	struct drm_i915_private *dev_priv = dev->dev_private;
4806 	struct drm_plane *plane;
4807 
4808 	if (atomic->wait_vblank)
4809 		intel_wait_for_vblank(dev, crtc->pipe);
4810 
4811 	intel_frontbuffer_flip(dev, atomic->fb_bits);
4812 
4813 	if (atomic->disable_cxsr)
4814 		crtc->wm.cxsr_allowed = true;
4815 
4816 	if (crtc->atomic.update_wm_post)
4817 		intel_update_watermarks(&crtc->base);
4818 
4819 	if (atomic->update_fbc)
4820 		intel_fbc_update(dev_priv);
4821 
4822 	if (atomic->post_enable_primary)
4823 		intel_post_enable_primary(&crtc->base);
4824 
4825 	drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4826 		intel_update_sprite_watermarks(plane, &crtc->base,
4827 					       0, 0, 0, false, false);
4828 
4829 	memset(atomic, 0, sizeof(*atomic));
4830 }
4831 
4832 static void intel_pre_plane_update(struct intel_crtc *crtc)
4833 {
4834 	struct drm_device *dev = crtc->base.dev;
4835 	struct drm_i915_private *dev_priv = dev->dev_private;
4836 	struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4837 	struct drm_plane *p;
4838 
4839 	/* Track fb's for any planes being disabled */
4840 	drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4841 		struct intel_plane *plane = to_intel_plane(p);
4842 
4843 		mutex_lock(&dev->struct_mutex);
4844 		i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4845 				  plane->frontbuffer_bit);
4846 		mutex_unlock(&dev->struct_mutex);
4847 	}
4848 
4849 	if (atomic->wait_for_flips)
4850 		intel_crtc_wait_for_pending_flips(&crtc->base);
4851 
4852 	if (atomic->disable_fbc)
4853 		intel_fbc_disable_crtc(crtc);
4854 
4855 	if (crtc->atomic.disable_ips)
4856 		hsw_disable_ips(crtc);
4857 
4858 	if (atomic->pre_disable_primary)
4859 		intel_pre_disable_primary(&crtc->base);
4860 
4861 	if (atomic->disable_cxsr) {
4862 		crtc->wm.cxsr_allowed = false;
4863 		intel_set_memory_cxsr(dev_priv, false);
4864 	}
4865 }
4866 
4867 static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
4868 {
4869 	struct drm_device *dev = crtc->dev;
4870 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4871 	struct drm_plane *p;
4872 	int pipe = intel_crtc->pipe;
4873 
4874 	intel_crtc_dpms_overlay_disable(intel_crtc);
4875 
4876 	drm_for_each_plane_mask(p, dev, plane_mask)
4877 		to_intel_plane(p)->disable_plane(p, crtc);
4878 
4879 	/*
4880 	 * FIXME: Once we grow proper nuclear flip support out of this we need
4881 	 * to compute the mask of flip planes precisely. For the time being
4882 	 * consider this a flip to a NULL plane.
4883 	 */
4884 	intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4885 }
4886 
4887 static void ironlake_crtc_enable(struct drm_crtc *crtc)
4888 {
4889 	struct drm_device *dev = crtc->dev;
4890 	struct drm_i915_private *dev_priv = dev->dev_private;
4891 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4892 	struct intel_encoder *encoder;
4893 	int pipe = intel_crtc->pipe;
4894 
4895 	if (WARN_ON(intel_crtc->active))
4896 		return;
4897 
4898 	if (intel_crtc->config->has_pch_encoder)
4899 		intel_prepare_shared_dpll(intel_crtc);
4900 
4901 	if (intel_crtc->config->has_dp_encoder)
4902 		intel_dp_set_m_n(intel_crtc, M1_N1);
4903 
4904 	intel_set_pipe_timings(intel_crtc);
4905 
4906 	if (intel_crtc->config->has_pch_encoder) {
4907 		intel_cpu_transcoder_set_m_n(intel_crtc,
4908 				     &intel_crtc->config->fdi_m_n, NULL);
4909 	}
4910 
4911 	ironlake_set_pipeconf(crtc);
4912 
4913 	intel_crtc->active = true;
4914 
4915 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4916 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
4917 
4918 	for_each_encoder_on_crtc(dev, crtc, encoder)
4919 		if (encoder->pre_enable)
4920 			encoder->pre_enable(encoder);
4921 
4922 	if (intel_crtc->config->has_pch_encoder) {
4923 		/* Note: FDI PLL enabling _must_ be done before we enable the
4924 		 * cpu pipes, hence this is separate from all the other fdi/pch
4925 		 * enabling. */
4926 		ironlake_fdi_pll_enable(intel_crtc);
4927 	} else {
4928 		assert_fdi_tx_disabled(dev_priv, pipe);
4929 		assert_fdi_rx_disabled(dev_priv, pipe);
4930 	}
4931 
4932 	ironlake_pfit_enable(intel_crtc);
4933 
4934 	/*
4935 	 * On ILK+ LUT must be loaded before the pipe is running but with
4936 	 * clocks enabled
4937 	 */
4938 	intel_crtc_load_lut(crtc);
4939 
4940 	intel_update_watermarks(crtc);
4941 	intel_enable_pipe(intel_crtc);
4942 
4943 	if (intel_crtc->config->has_pch_encoder)
4944 		ironlake_pch_enable(crtc);
4945 
4946 	assert_vblank_disabled(crtc);
4947 	drm_crtc_vblank_on(crtc);
4948 
4949 	for_each_encoder_on_crtc(dev, crtc, encoder)
4950 		encoder->enable(encoder);
4951 
4952 	if (HAS_PCH_CPT(dev))
4953 		cpt_verify_modeset(dev, intel_crtc->pipe);
4954 }
4955 
4956 /* IPS only exists on ULT machines and is tied to pipe A. */
4957 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4958 {
4959 	return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
4960 }
4961 
4962 static void haswell_crtc_enable(struct drm_crtc *crtc)
4963 {
4964 	struct drm_device *dev = crtc->dev;
4965 	struct drm_i915_private *dev_priv = dev->dev_private;
4966 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4967 	struct intel_encoder *encoder;
4968 	int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4969 	struct intel_crtc_state *pipe_config =
4970 		to_intel_crtc_state(crtc->state);
4971 	bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
4972 
4973 	if (WARN_ON(intel_crtc->active))
4974 		return;
4975 
4976 	if (intel_crtc_to_shared_dpll(intel_crtc))
4977 		intel_enable_shared_dpll(intel_crtc);
4978 
4979 	if (intel_crtc->config->has_dp_encoder)
4980 		intel_dp_set_m_n(intel_crtc, M1_N1);
4981 
4982 	intel_set_pipe_timings(intel_crtc);
4983 
4984 	if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4985 		I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4986 			   intel_crtc->config->pixel_multiplier - 1);
4987 	}
4988 
4989 	if (intel_crtc->config->has_pch_encoder) {
4990 		intel_cpu_transcoder_set_m_n(intel_crtc,
4991 				     &intel_crtc->config->fdi_m_n, NULL);
4992 	}
4993 
4994 	haswell_set_pipeconf(crtc);
4995 
4996 	intel_set_pipe_csc(crtc);
4997 
4998 	intel_crtc->active = true;
4999 
5000 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5001 	for_each_encoder_on_crtc(dev, crtc, encoder) {
5002 		if (encoder->pre_pll_enable)
5003 			encoder->pre_pll_enable(encoder);
5004 		if (encoder->pre_enable)
5005 			encoder->pre_enable(encoder);
5006 	}
5007 
5008 	if (intel_crtc->config->has_pch_encoder) {
5009 		intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5010 						      true);
5011 		dev_priv->display.fdi_link_train(crtc);
5012 	}
5013 
5014 	if (!is_dsi)
5015 		intel_ddi_enable_pipe_clock(intel_crtc);
5016 
5017 	if (INTEL_INFO(dev)->gen >= 9)
5018 		skylake_pfit_enable(intel_crtc);
5019 	else
5020 		ironlake_pfit_enable(intel_crtc);
5021 
5022 	/*
5023 	 * On ILK+ LUT must be loaded before the pipe is running but with
5024 	 * clocks enabled
5025 	 */
5026 	intel_crtc_load_lut(crtc);
5027 
5028 	intel_ddi_set_pipe_settings(crtc);
5029 	if (!is_dsi)
5030 		intel_ddi_enable_transcoder_func(crtc);
5031 
5032 	intel_update_watermarks(crtc);
5033 	intel_enable_pipe(intel_crtc);
5034 
5035 	if (intel_crtc->config->has_pch_encoder)
5036 		lpt_pch_enable(crtc);
5037 
5038 	if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
5039 		intel_ddi_set_vc_payload_alloc(crtc, true);
5040 
5041 	assert_vblank_disabled(crtc);
5042 	drm_crtc_vblank_on(crtc);
5043 
5044 	for_each_encoder_on_crtc(dev, crtc, encoder) {
5045 		encoder->enable(encoder);
5046 		intel_opregion_notify_encoder(encoder, true);
5047 	}
5048 
5049 	/* If we change the relative order between pipe/planes enabling, we need
5050 	 * to change the workaround. */
5051 	hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
5052 	if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
5053 		intel_wait_for_vblank(dev, hsw_workaround_pipe);
5054 		intel_wait_for_vblank(dev, hsw_workaround_pipe);
5055 	}
5056 }
5057 
5058 static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
5059 {
5060 	struct drm_device *dev = crtc->base.dev;
5061 	struct drm_i915_private *dev_priv = dev->dev_private;
5062 	int pipe = crtc->pipe;
5063 
5064 	/* To avoid upsetting the power well on haswell only disable the pfit if
5065 	 * it's in use. The hw state code will make sure we get this right. */
5066 	if (force || crtc->config->pch_pfit.enabled) {
5067 		I915_WRITE(PF_CTL(pipe), 0);
5068 		I915_WRITE(PF_WIN_POS(pipe), 0);
5069 		I915_WRITE(PF_WIN_SZ(pipe), 0);
5070 	}
5071 }
5072 
5073 static void ironlake_crtc_disable(struct drm_crtc *crtc)
5074 {
5075 	struct drm_device *dev = crtc->dev;
5076 	struct drm_i915_private *dev_priv = dev->dev_private;
5077 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5078 	struct intel_encoder *encoder;
5079 	int pipe = intel_crtc->pipe;
5080 	u32 reg, temp;
5081 
5082 	for_each_encoder_on_crtc(dev, crtc, encoder)
5083 		encoder->disable(encoder);
5084 
5085 	drm_crtc_vblank_off(crtc);
5086 	assert_vblank_disabled(crtc);
5087 
5088 	if (intel_crtc->config->has_pch_encoder)
5089 		intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5090 
5091 	intel_disable_pipe(intel_crtc);
5092 
5093 	ironlake_pfit_disable(intel_crtc, false);
5094 
5095 	if (intel_crtc->config->has_pch_encoder)
5096 		ironlake_fdi_disable(crtc);
5097 
5098 	for_each_encoder_on_crtc(dev, crtc, encoder)
5099 		if (encoder->post_disable)
5100 			encoder->post_disable(encoder);
5101 
5102 	if (intel_crtc->config->has_pch_encoder) {
5103 		ironlake_disable_pch_transcoder(dev_priv, pipe);
5104 
5105 		if (HAS_PCH_CPT(dev)) {
5106 			/* disable TRANS_DP_CTL */
5107 			reg = TRANS_DP_CTL(pipe);
5108 			temp = I915_READ(reg);
5109 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5110 				  TRANS_DP_PORT_SEL_MASK);
5111 			temp |= TRANS_DP_PORT_SEL_NONE;
5112 			I915_WRITE(reg, temp);
5113 
5114 			/* disable DPLL_SEL */
5115 			temp = I915_READ(PCH_DPLL_SEL);
5116 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
5117 			I915_WRITE(PCH_DPLL_SEL, temp);
5118 		}
5119 
5120 		ironlake_fdi_pll_disable(intel_crtc);
5121 	}
5122 }
5123 
5124 static void haswell_crtc_disable(struct drm_crtc *crtc)
5125 {
5126 	struct drm_device *dev = crtc->dev;
5127 	struct drm_i915_private *dev_priv = dev->dev_private;
5128 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5129 	struct intel_encoder *encoder;
5130 	enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
5131 	bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
5132 
5133 	for_each_encoder_on_crtc(dev, crtc, encoder) {
5134 		intel_opregion_notify_encoder(encoder, false);
5135 		encoder->disable(encoder);
5136 	}
5137 
5138 	drm_crtc_vblank_off(crtc);
5139 	assert_vblank_disabled(crtc);
5140 
5141 	if (intel_crtc->config->has_pch_encoder)
5142 		intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5143 						      false);
5144 	intel_disable_pipe(intel_crtc);
5145 
5146 	if (intel_crtc->config->dp_encoder_is_mst)
5147 		intel_ddi_set_vc_payload_alloc(crtc, false);
5148 
5149 	if (!is_dsi)
5150 		intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
5151 
5152 	if (INTEL_INFO(dev)->gen >= 9)
5153 		skylake_scaler_disable(intel_crtc);
5154 	else
5155 		ironlake_pfit_disable(intel_crtc, false);
5156 
5157 	if (!is_dsi)
5158 		intel_ddi_disable_pipe_clock(intel_crtc);
5159 
5160 	if (intel_crtc->config->has_pch_encoder) {
5161 		lpt_disable_pch_transcoder(dev_priv);
5162 		intel_ddi_fdi_disable(crtc);
5163 	}
5164 
5165 	for_each_encoder_on_crtc(dev, crtc, encoder)
5166 		if (encoder->post_disable)
5167 			encoder->post_disable(encoder);
5168 }
5169 
5170 static void i9xx_pfit_enable(struct intel_crtc *crtc)
5171 {
5172 	struct drm_device *dev = crtc->base.dev;
5173 	struct drm_i915_private *dev_priv = dev->dev_private;
5174 	struct intel_crtc_state *pipe_config = crtc->config;
5175 
5176 	if (!pipe_config->gmch_pfit.control)
5177 		return;
5178 
5179 	/*
5180 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
5181 	 * according to register description and PRM.
5182 	 */
5183 	WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5184 	assert_pipe_disabled(dev_priv, crtc->pipe);
5185 
5186 	I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5187 	I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5188 
5189 	/* Border color in case we don't scale up to the full screen. Black by
5190 	 * default, change to something else for debugging. */
5191 	I915_WRITE(BCLRPAT(crtc->pipe), 0);
5192 }
5193 
5194 static enum intel_display_power_domain port_to_power_domain(enum port port)
5195 {
5196 	switch (port) {
5197 	case PORT_A:
5198 		return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5199 	case PORT_B:
5200 		return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5201 	case PORT_C:
5202 		return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5203 	case PORT_D:
5204 		return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5205 	case PORT_E:
5206 		return POWER_DOMAIN_PORT_DDI_E_2_LANES;
5207 	default:
5208 		MISSING_CASE(port);
5209 		return POWER_DOMAIN_PORT_OTHER;
5210 	}
5211 }
5212 
5213 static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5214 {
5215 	switch (port) {
5216 	case PORT_A:
5217 		return POWER_DOMAIN_AUX_A;
5218 	case PORT_B:
5219 		return POWER_DOMAIN_AUX_B;
5220 	case PORT_C:
5221 		return POWER_DOMAIN_AUX_C;
5222 	case PORT_D:
5223 		return POWER_DOMAIN_AUX_D;
5224 	case PORT_E:
5225 		/* FIXME: Check VBT for actual wiring of PORT E */
5226 		return POWER_DOMAIN_AUX_D;
5227 	default:
5228 		MISSING_CASE(port);
5229 		return POWER_DOMAIN_AUX_A;
5230 	}
5231 }
5232 
5233 #define for_each_power_domain(domain, mask)				\
5234 	for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++)	\
5235 		if ((1 << (domain)) & (mask))
5236 
5237 enum intel_display_power_domain
5238 intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5239 {
5240 	struct drm_device *dev = intel_encoder->base.dev;
5241 	struct intel_digital_port *intel_dig_port;
5242 
5243 	switch (intel_encoder->type) {
5244 	case INTEL_OUTPUT_UNKNOWN:
5245 		/* Only DDI platforms should ever use this output type */
5246 		WARN_ON_ONCE(!HAS_DDI(dev));
5247 	case INTEL_OUTPUT_DISPLAYPORT:
5248 	case INTEL_OUTPUT_HDMI:
5249 	case INTEL_OUTPUT_EDP:
5250 		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5251 		return port_to_power_domain(intel_dig_port->port);
5252 	case INTEL_OUTPUT_DP_MST:
5253 		intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5254 		return port_to_power_domain(intel_dig_port->port);
5255 	case INTEL_OUTPUT_ANALOG:
5256 		return POWER_DOMAIN_PORT_CRT;
5257 	case INTEL_OUTPUT_DSI:
5258 		return POWER_DOMAIN_PORT_DSI;
5259 	default:
5260 		return POWER_DOMAIN_PORT_OTHER;
5261 	}
5262 }
5263 
5264 enum intel_display_power_domain
5265 intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5266 {
5267 	struct drm_device *dev = intel_encoder->base.dev;
5268 	struct intel_digital_port *intel_dig_port;
5269 
5270 	switch (intel_encoder->type) {
5271 	case INTEL_OUTPUT_UNKNOWN:
5272 	case INTEL_OUTPUT_HDMI:
5273 		/*
5274 		 * Only DDI platforms should ever use these output types.
5275 		 * We can get here after the HDMI detect code has already set
5276 		 * the type of the shared encoder. Since we can't be sure
5277 		 * what's the status of the given connectors, play safe and
5278 		 * run the DP detection too.
5279 		 */
5280 		WARN_ON_ONCE(!HAS_DDI(dev));
5281 	case INTEL_OUTPUT_DISPLAYPORT:
5282 	case INTEL_OUTPUT_EDP:
5283 		intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5284 		return port_to_aux_power_domain(intel_dig_port->port);
5285 	case INTEL_OUTPUT_DP_MST:
5286 		intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5287 		return port_to_aux_power_domain(intel_dig_port->port);
5288 	default:
5289 		MISSING_CASE(intel_encoder->type);
5290 		return POWER_DOMAIN_AUX_A;
5291 	}
5292 }
5293 
5294 static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5295 {
5296 	struct drm_device *dev = crtc->dev;
5297 	struct intel_encoder *intel_encoder;
5298 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5299 	enum i915_pipe pipe = intel_crtc->pipe;
5300 	unsigned long mask;
5301 	enum transcoder transcoder;
5302 
5303 	if (!crtc->state->active)
5304 		return 0;
5305 
5306 	transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5307 
5308 	mask = BIT(POWER_DOMAIN_PIPE(pipe));
5309 	mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
5310 	if (intel_crtc->config->pch_pfit.enabled ||
5311 	    intel_crtc->config->pch_pfit.force_thru)
5312 		mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5313 
5314 	for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5315 		mask |= BIT(intel_display_port_power_domain(intel_encoder));
5316 
5317 	return mask;
5318 }
5319 
5320 static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5321 {
5322 	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5323 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5324 	enum intel_display_power_domain domain;
5325 	unsigned long domains, new_domains, old_domains;
5326 
5327 	old_domains = intel_crtc->enabled_power_domains;
5328 	intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5329 
5330 	domains = new_domains & ~old_domains;
5331 
5332 	for_each_power_domain(domain, domains)
5333 		intel_display_power_get(dev_priv, domain);
5334 
5335 	return old_domains & ~new_domains;
5336 }
5337 
5338 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5339 				      unsigned long domains)
5340 {
5341 	enum intel_display_power_domain domain;
5342 
5343 	for_each_power_domain(domain, domains)
5344 		intel_display_power_put(dev_priv, domain);
5345 }
5346 
5347 static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
5348 {
5349 	struct drm_device *dev = state->dev;
5350 	struct drm_i915_private *dev_priv = dev->dev_private;
5351 	unsigned long put_domains[I915_MAX_PIPES] = {};
5352 	struct drm_crtc_state *crtc_state;
5353 	struct drm_crtc *crtc;
5354 	int i;
5355 
5356 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
5357 		if (needs_modeset(crtc->state))
5358 			put_domains[to_intel_crtc(crtc)->pipe] =
5359 				modeset_get_crtc_power_domains(crtc);
5360 	}
5361 
5362 	if (dev_priv->display.modeset_commit_cdclk) {
5363 		unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5364 
5365 		if (cdclk != dev_priv->cdclk_freq &&
5366 		    !WARN_ON(!state->allow_modeset))
5367 			dev_priv->display.modeset_commit_cdclk(state);
5368 	}
5369 
5370 	for (i = 0; i < I915_MAX_PIPES; i++)
5371 		if (put_domains[i])
5372 			modeset_put_power_domains(dev_priv, put_domains[i]);
5373 }
5374 
5375 static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5376 {
5377 	int max_cdclk_freq = dev_priv->max_cdclk_freq;
5378 
5379 	if (INTEL_INFO(dev_priv)->gen >= 9 ||
5380 	    IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5381 		return max_cdclk_freq;
5382 	else if (IS_CHERRYVIEW(dev_priv))
5383 		return max_cdclk_freq*95/100;
5384 	else if (INTEL_INFO(dev_priv)->gen < 4)
5385 		return 2*max_cdclk_freq*90/100;
5386 	else
5387 		return max_cdclk_freq*90/100;
5388 }
5389 
5390 static void intel_update_max_cdclk(struct drm_device *dev)
5391 {
5392 	struct drm_i915_private *dev_priv = dev->dev_private;
5393 
5394 	if (IS_SKYLAKE(dev)) {
5395 		u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5396 
5397 		if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5398 			dev_priv->max_cdclk_freq = 675000;
5399 		else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5400 			dev_priv->max_cdclk_freq = 540000;
5401 		else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5402 			dev_priv->max_cdclk_freq = 450000;
5403 		else
5404 			dev_priv->max_cdclk_freq = 337500;
5405 	} else if (IS_BROADWELL(dev))  {
5406 		/*
5407 		 * FIXME with extra cooling we can allow
5408 		 * 540 MHz for ULX and 675 Mhz for ULT.
5409 		 * How can we know if extra cooling is
5410 		 * available? PCI ID, VTB, something else?
5411 		 */
5412 		if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5413 			dev_priv->max_cdclk_freq = 450000;
5414 		else if (IS_BDW_ULX(dev))
5415 			dev_priv->max_cdclk_freq = 450000;
5416 		else if (IS_BDW_ULT(dev))
5417 			dev_priv->max_cdclk_freq = 540000;
5418 		else
5419 			dev_priv->max_cdclk_freq = 675000;
5420 	} else if (IS_CHERRYVIEW(dev)) {
5421 		dev_priv->max_cdclk_freq = 320000;
5422 	} else if (IS_VALLEYVIEW(dev)) {
5423 		dev_priv->max_cdclk_freq = 400000;
5424 	} else {
5425 		/* otherwise assume cdclk is fixed */
5426 		dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5427 	}
5428 
5429 	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5430 
5431 	DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5432 			 dev_priv->max_cdclk_freq);
5433 
5434 	DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5435 			 dev_priv->max_dotclk_freq);
5436 }
5437 
5438 static void intel_update_cdclk(struct drm_device *dev)
5439 {
5440 	struct drm_i915_private *dev_priv = dev->dev_private;
5441 
5442 	dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5443 	DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5444 			 dev_priv->cdclk_freq);
5445 
5446 	/*
5447 	 * Program the gmbus_freq based on the cdclk frequency.
5448 	 * BSpec erroneously claims we should aim for 4MHz, but
5449 	 * in fact 1MHz is the correct frequency.
5450 	 */
5451 	if (IS_VALLEYVIEW(dev)) {
5452 		/*
5453 		 * Program the gmbus_freq based on the cdclk frequency.
5454 		 * BSpec erroneously claims we should aim for 4MHz, but
5455 		 * in fact 1MHz is the correct frequency.
5456 		 */
5457 		I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5458 	}
5459 
5460 	if (dev_priv->max_cdclk_freq == 0)
5461 		intel_update_max_cdclk(dev);
5462 }
5463 
5464 static void broxton_set_cdclk(struct drm_device *dev, int frequency)
5465 {
5466 	struct drm_i915_private *dev_priv = dev->dev_private;
5467 	uint32_t divider;
5468 	uint32_t ratio;
5469 	uint32_t current_freq;
5470 	int ret;
5471 
5472 	/* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5473 	switch (frequency) {
5474 	case 144000:
5475 		divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5476 		ratio = BXT_DE_PLL_RATIO(60);
5477 		break;
5478 	case 288000:
5479 		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5480 		ratio = BXT_DE_PLL_RATIO(60);
5481 		break;
5482 	case 384000:
5483 		divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5484 		ratio = BXT_DE_PLL_RATIO(60);
5485 		break;
5486 	case 576000:
5487 		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5488 		ratio = BXT_DE_PLL_RATIO(60);
5489 		break;
5490 	case 624000:
5491 		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5492 		ratio = BXT_DE_PLL_RATIO(65);
5493 		break;
5494 	case 19200:
5495 		/*
5496 		 * Bypass frequency with DE PLL disabled. Init ratio, divider
5497 		 * to suppress GCC warning.
5498 		 */
5499 		ratio = 0;
5500 		divider = 0;
5501 		break;
5502 	default:
5503 		DRM_ERROR("unsupported CDCLK freq %d", frequency);
5504 
5505 		return;
5506 	}
5507 
5508 	mutex_lock(&dev_priv->rps.hw_lock);
5509 	/* Inform power controller of upcoming frequency change */
5510 	ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5511 				      0x80000000);
5512 	mutex_unlock(&dev_priv->rps.hw_lock);
5513 
5514 	if (ret) {
5515 		DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5516 			  ret, frequency);
5517 		return;
5518 	}
5519 
5520 	current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5521 	/* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5522 	current_freq = current_freq * 500 + 1000;
5523 
5524 	/*
5525 	 * DE PLL has to be disabled when
5526 	 * - setting to 19.2MHz (bypass, PLL isn't used)
5527 	 * - before setting to 624MHz (PLL needs toggling)
5528 	 * - before setting to any frequency from 624MHz (PLL needs toggling)
5529 	 */
5530 	if (frequency == 19200 || frequency == 624000 ||
5531 	    current_freq == 624000) {
5532 		I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5533 		/* Timeout 200us */
5534 		if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5535 			     1))
5536 			DRM_ERROR("timout waiting for DE PLL unlock\n");
5537 	}
5538 
5539 	if (frequency != 19200) {
5540 		uint32_t val;
5541 
5542 		val = I915_READ(BXT_DE_PLL_CTL);
5543 		val &= ~BXT_DE_PLL_RATIO_MASK;
5544 		val |= ratio;
5545 		I915_WRITE(BXT_DE_PLL_CTL, val);
5546 
5547 		I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5548 		/* Timeout 200us */
5549 		if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5550 			DRM_ERROR("timeout waiting for DE PLL lock\n");
5551 
5552 		val = I915_READ(CDCLK_CTL);
5553 		val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5554 		val |= divider;
5555 		/*
5556 		 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5557 		 * enable otherwise.
5558 		 */
5559 		val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5560 		if (frequency >= 500000)
5561 			val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5562 
5563 		val &= ~CDCLK_FREQ_DECIMAL_MASK;
5564 		/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5565 		val |= (frequency - 1000) / 500;
5566 		I915_WRITE(CDCLK_CTL, val);
5567 	}
5568 
5569 	mutex_lock(&dev_priv->rps.hw_lock);
5570 	ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5571 				      DIV_ROUND_UP(frequency, 25000));
5572 	mutex_unlock(&dev_priv->rps.hw_lock);
5573 
5574 	if (ret) {
5575 		DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5576 			  ret, frequency);
5577 		return;
5578 	}
5579 
5580 	intel_update_cdclk(dev);
5581 }
5582 
5583 void broxton_init_cdclk(struct drm_device *dev)
5584 {
5585 	struct drm_i915_private *dev_priv = dev->dev_private;
5586 	uint32_t val;
5587 
5588 	/*
5589 	 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5590 	 * or else the reset will hang because there is no PCH to respond.
5591 	 * Move the handshake programming to initialization sequence.
5592 	 * Previously was left up to BIOS.
5593 	 */
5594 	val = I915_READ(HSW_NDE_RSTWRN_OPT);
5595 	val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5596 	I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5597 
5598 	/* Enable PG1 for cdclk */
5599 	intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5600 
5601 	/* check if cd clock is enabled */
5602 	if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5603 		DRM_DEBUG_KMS("Display already initialized\n");
5604 		return;
5605 	}
5606 
5607 	/*
5608 	 * FIXME:
5609 	 * - The initial CDCLK needs to be read from VBT.
5610 	 *   Need to make this change after VBT has changes for BXT.
5611 	 * - check if setting the max (or any) cdclk freq is really necessary
5612 	 *   here, it belongs to modeset time
5613 	 */
5614 	broxton_set_cdclk(dev, 624000);
5615 
5616 	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5617 	POSTING_READ(DBUF_CTL);
5618 
5619 	udelay(10);
5620 
5621 	if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5622 		DRM_ERROR("DBuf power enable timeout!\n");
5623 }
5624 
5625 void broxton_uninit_cdclk(struct drm_device *dev)
5626 {
5627 	struct drm_i915_private *dev_priv = dev->dev_private;
5628 
5629 	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5630 	POSTING_READ(DBUF_CTL);
5631 
5632 	udelay(10);
5633 
5634 	if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5635 		DRM_ERROR("DBuf power disable timeout!\n");
5636 
5637 	/* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5638 	broxton_set_cdclk(dev, 19200);
5639 
5640 	intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5641 }
5642 
5643 static const struct skl_cdclk_entry {
5644 	unsigned int freq;
5645 	unsigned int vco;
5646 } skl_cdclk_frequencies[] = {
5647 	{ .freq = 308570, .vco = 8640 },
5648 	{ .freq = 337500, .vco = 8100 },
5649 	{ .freq = 432000, .vco = 8640 },
5650 	{ .freq = 450000, .vco = 8100 },
5651 	{ .freq = 540000, .vco = 8100 },
5652 	{ .freq = 617140, .vco = 8640 },
5653 	{ .freq = 675000, .vco = 8100 },
5654 };
5655 
5656 static unsigned int skl_cdclk_decimal(unsigned int freq)
5657 {
5658 	return (freq - 1000) / 500;
5659 }
5660 
5661 static unsigned int skl_cdclk_get_vco(unsigned int freq)
5662 {
5663 	unsigned int i;
5664 
5665 	for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5666 		const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5667 
5668 		if (e->freq == freq)
5669 			return e->vco;
5670 	}
5671 
5672 	return 8100;
5673 }
5674 
5675 static void
5676 skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5677 {
5678 	unsigned int min_freq;
5679 	u32 val;
5680 
5681 	/* select the minimum CDCLK before enabling DPLL 0 */
5682 	val = I915_READ(CDCLK_CTL);
5683 	val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5684 	val |= CDCLK_FREQ_337_308;
5685 
5686 	if (required_vco == 8640)
5687 		min_freq = 308570;
5688 	else
5689 		min_freq = 337500;
5690 
5691 	val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5692 
5693 	I915_WRITE(CDCLK_CTL, val);
5694 	POSTING_READ(CDCLK_CTL);
5695 
5696 	/*
5697 	 * We always enable DPLL0 with the lowest link rate possible, but still
5698 	 * taking into account the VCO required to operate the eDP panel at the
5699 	 * desired frequency. The usual DP link rates operate with a VCO of
5700 	 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5701 	 * The modeset code is responsible for the selection of the exact link
5702 	 * rate later on, with the constraint of choosing a frequency that
5703 	 * works with required_vco.
5704 	 */
5705 	val = I915_READ(DPLL_CTRL1);
5706 
5707 	val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5708 		 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5709 	val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5710 	if (required_vco == 8640)
5711 		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5712 					    SKL_DPLL0);
5713 	else
5714 		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5715 					    SKL_DPLL0);
5716 
5717 	I915_WRITE(DPLL_CTRL1, val);
5718 	POSTING_READ(DPLL_CTRL1);
5719 
5720 	I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5721 
5722 	if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5723 		DRM_ERROR("DPLL0 not locked\n");
5724 }
5725 
5726 static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5727 {
5728 	int ret;
5729 	u32 val;
5730 
5731 	/* inform PCU we want to change CDCLK */
5732 	val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5733 	mutex_lock(&dev_priv->rps.hw_lock);
5734 	ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5735 	mutex_unlock(&dev_priv->rps.hw_lock);
5736 
5737 	return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5738 }
5739 
5740 static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5741 {
5742 	unsigned int i;
5743 
5744 	for (i = 0; i < 15; i++) {
5745 		if (skl_cdclk_pcu_ready(dev_priv))
5746 			return true;
5747 		udelay(10);
5748 	}
5749 
5750 	return false;
5751 }
5752 
5753 static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5754 {
5755 	struct drm_device *dev = dev_priv->dev;
5756 	u32 freq_select, pcu_ack;
5757 
5758 	DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5759 
5760 	if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5761 		DRM_ERROR("failed to inform PCU about cdclk change\n");
5762 		return;
5763 	}
5764 
5765 	/* set CDCLK_CTL */
5766 	switch(freq) {
5767 	case 450000:
5768 	case 432000:
5769 		freq_select = CDCLK_FREQ_450_432;
5770 		pcu_ack = 1;
5771 		break;
5772 	case 540000:
5773 		freq_select = CDCLK_FREQ_540;
5774 		pcu_ack = 2;
5775 		break;
5776 	case 308570:
5777 	case 337500:
5778 	default:
5779 		freq_select = CDCLK_FREQ_337_308;
5780 		pcu_ack = 0;
5781 		break;
5782 	case 617140:
5783 	case 675000:
5784 		freq_select = CDCLK_FREQ_675_617;
5785 		pcu_ack = 3;
5786 		break;
5787 	}
5788 
5789 	I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5790 	POSTING_READ(CDCLK_CTL);
5791 
5792 	/* inform PCU of the change */
5793 	mutex_lock(&dev_priv->rps.hw_lock);
5794 	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5795 	mutex_unlock(&dev_priv->rps.hw_lock);
5796 
5797 	intel_update_cdclk(dev);
5798 }
5799 
5800 void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5801 {
5802 	/* disable DBUF power */
5803 	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5804 	POSTING_READ(DBUF_CTL);
5805 
5806 	udelay(10);
5807 
5808 	if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5809 		DRM_ERROR("DBuf power disable timeout\n");
5810 
5811 	/*
5812 	 * DMC assumes ownership of LCPLL and will get confused if we touch it.
5813 	 */
5814 	if (dev_priv->csr.dmc_payload) {
5815 		/* disable DPLL0 */
5816 		I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
5817 					~LCPLL_PLL_ENABLE);
5818 		if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5819 			DRM_ERROR("Couldn't disable DPLL0\n");
5820 	}
5821 
5822 	intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5823 }
5824 
5825 void skl_init_cdclk(struct drm_i915_private *dev_priv)
5826 {
5827 	u32 val;
5828 	unsigned int required_vco;
5829 
5830 	/* enable PCH reset handshake */
5831 	val = I915_READ(HSW_NDE_RSTWRN_OPT);
5832 	I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5833 
5834 	/* enable PG1 and Misc I/O */
5835 	intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5836 
5837 	/* DPLL0 not enabled (happens on early BIOS versions) */
5838 	if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5839 		/* enable DPLL0 */
5840 		required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5841 		skl_dpll0_enable(dev_priv, required_vco);
5842 	}
5843 
5844 	/* set CDCLK to the frequency the BIOS chose */
5845 	skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5846 
5847 	/* enable DBUF power */
5848 	I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5849 	POSTING_READ(DBUF_CTL);
5850 
5851 	udelay(10);
5852 
5853 	if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5854 		DRM_ERROR("DBuf power enable timeout\n");
5855 }
5856 
5857 /* Adjust CDclk dividers to allow high res or save power if possible */
5858 static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5859 {
5860 	struct drm_i915_private *dev_priv = dev->dev_private;
5861 	u32 val, cmd;
5862 
5863 	WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5864 					!= dev_priv->cdclk_freq);
5865 
5866 	if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
5867 		cmd = 2;
5868 	else if (cdclk == 266667)
5869 		cmd = 1;
5870 	else
5871 		cmd = 0;
5872 
5873 	mutex_lock(&dev_priv->rps.hw_lock);
5874 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5875 	val &= ~DSPFREQGUAR_MASK;
5876 	val |= (cmd << DSPFREQGUAR_SHIFT);
5877 	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5878 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5879 		      DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5880 		     50)) {
5881 		DRM_ERROR("timed out waiting for CDclk change\n");
5882 	}
5883 	mutex_unlock(&dev_priv->rps.hw_lock);
5884 
5885 	mutex_lock(&dev_priv->sb_lock);
5886 
5887 	if (cdclk == 400000) {
5888 		u32 divider;
5889 
5890 		divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5891 
5892 		/* adjust cdclk divider */
5893 		val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5894 		val &= ~CCK_FREQUENCY_VALUES;
5895 		val |= divider;
5896 		vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
5897 
5898 		if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5899 			      CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
5900 			     50))
5901 			DRM_ERROR("timed out waiting for CDclk change\n");
5902 	}
5903 
5904 	/* adjust self-refresh exit latency value */
5905 	val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5906 	val &= ~0x7f;
5907 
5908 	/*
5909 	 * For high bandwidth configs, we set a higher latency in the bunit
5910 	 * so that the core display fetch happens in time to avoid underruns.
5911 	 */
5912 	if (cdclk == 400000)
5913 		val |= 4500 / 250; /* 4.5 usec */
5914 	else
5915 		val |= 3000 / 250; /* 3.0 usec */
5916 	vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5917 
5918 	mutex_unlock(&dev_priv->sb_lock);
5919 
5920 	intel_update_cdclk(dev);
5921 }
5922 
5923 static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5924 {
5925 	struct drm_i915_private *dev_priv = dev->dev_private;
5926 	u32 val, cmd;
5927 
5928 	WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5929 						!= dev_priv->cdclk_freq);
5930 
5931 	switch (cdclk) {
5932 	case 333333:
5933 	case 320000:
5934 	case 266667:
5935 	case 200000:
5936 		break;
5937 	default:
5938 		MISSING_CASE(cdclk);
5939 		return;
5940 	}
5941 
5942 	/*
5943 	 * Specs are full of misinformation, but testing on actual
5944 	 * hardware has shown that we just need to write the desired
5945 	 * CCK divider into the Punit register.
5946 	 */
5947 	cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5948 
5949 	mutex_lock(&dev_priv->rps.hw_lock);
5950 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5951 	val &= ~DSPFREQGUAR_MASK_CHV;
5952 	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5953 	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5954 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5955 		      DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5956 		     50)) {
5957 		DRM_ERROR("timed out waiting for CDclk change\n");
5958 	}
5959 	mutex_unlock(&dev_priv->rps.hw_lock);
5960 
5961 	intel_update_cdclk(dev);
5962 }
5963 
5964 static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5965 				 int max_pixclk)
5966 {
5967 	int freq_320 = (dev_priv->hpll_freq <<  1) % 320000 != 0 ? 333333 : 320000;
5968 	int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
5969 
5970 	/*
5971 	 * Really only a few cases to deal with, as only 4 CDclks are supported:
5972 	 *   200MHz
5973 	 *   267MHz
5974 	 *   320/333MHz (depends on HPLL freq)
5975 	 *   400MHz (VLV only)
5976 	 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5977 	 * of the lower bin and adjust if needed.
5978 	 *
5979 	 * We seem to get an unstable or solid color picture at 200MHz.
5980 	 * Not sure what's wrong. For now use 200MHz only when all pipes
5981 	 * are off.
5982 	 */
5983 	if (!IS_CHERRYVIEW(dev_priv) &&
5984 	    max_pixclk > freq_320*limit/100)
5985 		return 400000;
5986 	else if (max_pixclk > 266667*limit/100)
5987 		return freq_320;
5988 	else if (max_pixclk > 0)
5989 		return 266667;
5990 	else
5991 		return 200000;
5992 }
5993 
5994 static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5995 			      int max_pixclk)
5996 {
5997 	/*
5998 	 * FIXME:
5999 	 * - remove the guardband, it's not needed on BXT
6000 	 * - set 19.2MHz bypass frequency if there are no active pipes
6001 	 */
6002 	if (max_pixclk > 576000*9/10)
6003 		return 624000;
6004 	else if (max_pixclk > 384000*9/10)
6005 		return 576000;
6006 	else if (max_pixclk > 288000*9/10)
6007 		return 384000;
6008 	else if (max_pixclk > 144000*9/10)
6009 		return 288000;
6010 	else
6011 		return 144000;
6012 }
6013 
6014 /* Compute the max pixel clock for new configuration. Uses atomic state if
6015  * that's non-NULL, look at current state otherwise. */
6016 static int intel_mode_max_pixclk(struct drm_device *dev,
6017 				 struct drm_atomic_state *state)
6018 {
6019 	struct intel_crtc *intel_crtc;
6020 	struct intel_crtc_state *crtc_state;
6021 	int max_pixclk = 0;
6022 
6023 	for_each_intel_crtc(dev, intel_crtc) {
6024 		crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6025 		if (IS_ERR(crtc_state))
6026 			return PTR_ERR(crtc_state);
6027 
6028 		if (!crtc_state->base.enable)
6029 			continue;
6030 
6031 		max_pixclk = max(max_pixclk,
6032 				 crtc_state->base.adjusted_mode.crtc_clock);
6033 	}
6034 
6035 	return max_pixclk;
6036 }
6037 
6038 static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
6039 {
6040 	struct drm_device *dev = state->dev;
6041 	struct drm_i915_private *dev_priv = dev->dev_private;
6042 	int max_pixclk = intel_mode_max_pixclk(dev, state);
6043 
6044 	if (max_pixclk < 0)
6045 		return max_pixclk;
6046 
6047 	to_intel_atomic_state(state)->cdclk =
6048 		valleyview_calc_cdclk(dev_priv, max_pixclk);
6049 
6050 	return 0;
6051 }
6052 
6053 static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
6054 {
6055 	struct drm_device *dev = state->dev;
6056 	struct drm_i915_private *dev_priv = dev->dev_private;
6057 	int max_pixclk = intel_mode_max_pixclk(dev, state);
6058 
6059 	if (max_pixclk < 0)
6060 		return max_pixclk;
6061 
6062 	to_intel_atomic_state(state)->cdclk =
6063 		broxton_calc_cdclk(dev_priv, max_pixclk);
6064 
6065 	return 0;
6066 }
6067 
6068 static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6069 {
6070 	unsigned int credits, default_credits;
6071 
6072 	if (IS_CHERRYVIEW(dev_priv))
6073 		default_credits = PFI_CREDIT(12);
6074 	else
6075 		default_credits = PFI_CREDIT(8);
6076 
6077 	if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
6078 		/* CHV suggested value is 31 or 63 */
6079 		if (IS_CHERRYVIEW(dev_priv))
6080 			credits = PFI_CREDIT_63;
6081 		else
6082 			credits = PFI_CREDIT(15);
6083 	} else {
6084 		credits = default_credits;
6085 	}
6086 
6087 	/*
6088 	 * WA - write default credits before re-programming
6089 	 * FIXME: should we also set the resend bit here?
6090 	 */
6091 	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6092 		   default_credits);
6093 
6094 	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6095 		   credits | PFI_CREDIT_RESEND);
6096 
6097 	/*
6098 	 * FIXME is this guaranteed to clear
6099 	 * immediately or should we poll for it?
6100 	 */
6101 	WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6102 }
6103 
6104 static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
6105 {
6106 	struct drm_device *dev = old_state->dev;
6107 	unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
6108 	struct drm_i915_private *dev_priv = dev->dev_private;
6109 
6110 	/*
6111 	 * FIXME: We can end up here with all power domains off, yet
6112 	 * with a CDCLK frequency other than the minimum. To account
6113 	 * for this take the PIPE-A power domain, which covers the HW
6114 	 * blocks needed for the following programming. This can be
6115 	 * removed once it's guaranteed that we get here either with
6116 	 * the minimum CDCLK set, or the required power domains
6117 	 * enabled.
6118 	 */
6119 	intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6120 
6121 	if (IS_CHERRYVIEW(dev))
6122 		cherryview_set_cdclk(dev, req_cdclk);
6123 	else
6124 		valleyview_set_cdclk(dev, req_cdclk);
6125 
6126 	vlv_program_pfi_credits(dev_priv);
6127 
6128 	intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
6129 }
6130 
6131 static void valleyview_crtc_enable(struct drm_crtc *crtc)
6132 {
6133 	struct drm_device *dev = crtc->dev;
6134 	struct drm_i915_private *dev_priv = to_i915(dev);
6135 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6136 	struct intel_encoder *encoder;
6137 	int pipe = intel_crtc->pipe;
6138 	bool is_dsi;
6139 
6140 	if (WARN_ON(intel_crtc->active))
6141 		return;
6142 
6143 	is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
6144 
6145 	if (intel_crtc->config->has_dp_encoder)
6146 		intel_dp_set_m_n(intel_crtc, M1_N1);
6147 
6148 	intel_set_pipe_timings(intel_crtc);
6149 
6150 	if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6151 		struct drm_i915_private *dev_priv = dev->dev_private;
6152 
6153 		I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6154 		I915_WRITE(CHV_CANVAS(pipe), 0);
6155 	}
6156 
6157 	i9xx_set_pipeconf(intel_crtc);
6158 
6159 	intel_crtc->active = true;
6160 
6161 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6162 
6163 	for_each_encoder_on_crtc(dev, crtc, encoder)
6164 		if (encoder->pre_pll_enable)
6165 			encoder->pre_pll_enable(encoder);
6166 
6167 	if (!is_dsi) {
6168 		if (IS_CHERRYVIEW(dev)) {
6169 			chv_prepare_pll(intel_crtc, intel_crtc->config);
6170 			chv_enable_pll(intel_crtc, intel_crtc->config);
6171 		} else {
6172 			vlv_prepare_pll(intel_crtc, intel_crtc->config);
6173 			vlv_enable_pll(intel_crtc, intel_crtc->config);
6174 		}
6175 	}
6176 
6177 	for_each_encoder_on_crtc(dev, crtc, encoder)
6178 		if (encoder->pre_enable)
6179 			encoder->pre_enable(encoder);
6180 
6181 	i9xx_pfit_enable(intel_crtc);
6182 
6183 	intel_crtc_load_lut(crtc);
6184 
6185 	intel_enable_pipe(intel_crtc);
6186 
6187 	assert_vblank_disabled(crtc);
6188 	drm_crtc_vblank_on(crtc);
6189 
6190 	for_each_encoder_on_crtc(dev, crtc, encoder)
6191 		encoder->enable(encoder);
6192 }
6193 
6194 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6195 {
6196 	struct drm_device *dev = crtc->base.dev;
6197 	struct drm_i915_private *dev_priv = dev->dev_private;
6198 
6199 	I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6200 	I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
6201 }
6202 
6203 static void i9xx_crtc_enable(struct drm_crtc *crtc)
6204 {
6205 	struct drm_device *dev = crtc->dev;
6206 	struct drm_i915_private *dev_priv = to_i915(dev);
6207 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6208 	struct intel_encoder *encoder;
6209 	int pipe = intel_crtc->pipe;
6210 
6211 	if (WARN_ON(intel_crtc->active))
6212 		return;
6213 
6214 	i9xx_set_pll_dividers(intel_crtc);
6215 
6216 	if (intel_crtc->config->has_dp_encoder)
6217 		intel_dp_set_m_n(intel_crtc, M1_N1);
6218 
6219 	intel_set_pipe_timings(intel_crtc);
6220 
6221 	i9xx_set_pipeconf(intel_crtc);
6222 
6223 	intel_crtc->active = true;
6224 
6225 	if (!IS_GEN2(dev))
6226 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6227 
6228 	for_each_encoder_on_crtc(dev, crtc, encoder)
6229 		if (encoder->pre_enable)
6230 			encoder->pre_enable(encoder);
6231 
6232 	i9xx_enable_pll(intel_crtc);
6233 
6234 	i9xx_pfit_enable(intel_crtc);
6235 
6236 	intel_crtc_load_lut(crtc);
6237 
6238 	intel_update_watermarks(crtc);
6239 	intel_enable_pipe(intel_crtc);
6240 
6241 	assert_vblank_disabled(crtc);
6242 	drm_crtc_vblank_on(crtc);
6243 
6244 	for_each_encoder_on_crtc(dev, crtc, encoder)
6245 		encoder->enable(encoder);
6246 }
6247 
6248 static void i9xx_pfit_disable(struct intel_crtc *crtc)
6249 {
6250 	struct drm_device *dev = crtc->base.dev;
6251 	struct drm_i915_private *dev_priv = dev->dev_private;
6252 
6253 	if (!crtc->config->gmch_pfit.control)
6254 		return;
6255 
6256 	assert_pipe_disabled(dev_priv, crtc->pipe);
6257 
6258 	DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6259 			 I915_READ(PFIT_CONTROL));
6260 	I915_WRITE(PFIT_CONTROL, 0);
6261 }
6262 
6263 static void i9xx_crtc_disable(struct drm_crtc *crtc)
6264 {
6265 	struct drm_device *dev = crtc->dev;
6266 	struct drm_i915_private *dev_priv = dev->dev_private;
6267 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6268 	struct intel_encoder *encoder;
6269 	int pipe = intel_crtc->pipe;
6270 
6271 	/*
6272 	 * On gen2 planes are double buffered but the pipe isn't, so we must
6273 	 * wait for planes to fully turn off before disabling the pipe.
6274 	 * We also need to wait on all gmch platforms because of the
6275 	 * self-refresh mode constraint explained above.
6276 	 */
6277 	intel_wait_for_vblank(dev, pipe);
6278 
6279 	for_each_encoder_on_crtc(dev, crtc, encoder)
6280 		encoder->disable(encoder);
6281 
6282 	drm_crtc_vblank_off(crtc);
6283 	assert_vblank_disabled(crtc);
6284 
6285 	intel_disable_pipe(intel_crtc);
6286 
6287 	i9xx_pfit_disable(intel_crtc);
6288 
6289 	for_each_encoder_on_crtc(dev, crtc, encoder)
6290 		if (encoder->post_disable)
6291 			encoder->post_disable(encoder);
6292 
6293 	if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
6294 		if (IS_CHERRYVIEW(dev))
6295 			chv_disable_pll(dev_priv, pipe);
6296 		else if (IS_VALLEYVIEW(dev))
6297 			vlv_disable_pll(dev_priv, pipe);
6298 		else
6299 			i9xx_disable_pll(intel_crtc);
6300 	}
6301 
6302 	for_each_encoder_on_crtc(dev, crtc, encoder)
6303 		if (encoder->post_pll_disable)
6304 			encoder->post_pll_disable(encoder);
6305 
6306 	if (!IS_GEN2(dev))
6307 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6308 }
6309 
6310 static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6311 {
6312 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6313 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6314 	enum intel_display_power_domain domain;
6315 	unsigned long domains;
6316 
6317 	if (!intel_crtc->active)
6318 		return;
6319 
6320 	if (to_intel_plane_state(crtc->primary->state)->visible) {
6321 		intel_crtc_wait_for_pending_flips(crtc);
6322 		intel_pre_disable_primary(crtc);
6323 
6324 		intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
6325 		to_intel_plane_state(crtc->primary->state)->visible = false;
6326 	}
6327 
6328 	dev_priv->display.crtc_disable(crtc);
6329 	intel_crtc->active = false;
6330 	intel_update_watermarks(crtc);
6331 	intel_disable_shared_dpll(intel_crtc);
6332 
6333 	domains = intel_crtc->enabled_power_domains;
6334 	for_each_power_domain(domain, domains)
6335 		intel_display_power_put(dev_priv, domain);
6336 	intel_crtc->enabled_power_domains = 0;
6337 }
6338 
6339 /*
6340  * turn all crtc's off, but do not adjust state
6341  * This has to be paired with a call to intel_modeset_setup_hw_state.
6342  */
6343 int intel_display_suspend(struct drm_device *dev)
6344 {
6345 	struct drm_mode_config *config = &dev->mode_config;
6346 	struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6347 	struct drm_atomic_state *state;
6348 	struct drm_crtc *crtc;
6349 	unsigned crtc_mask = 0;
6350 	int ret = 0;
6351 
6352 	if (WARN_ON(!ctx))
6353 		return 0;
6354 
6355 #if 0
6356 	lockdep_assert_held(&ctx->ww_ctx);
6357 #endif
6358 	state = drm_atomic_state_alloc(dev);
6359 	if (WARN_ON(!state))
6360 		return -ENOMEM;
6361 
6362 	state->acquire_ctx = ctx;
6363 	state->allow_modeset = true;
6364 
6365 	for_each_crtc(dev, crtc) {
6366 		struct drm_crtc_state *crtc_state =
6367 			drm_atomic_get_crtc_state(state, crtc);
6368 
6369 		ret = PTR_ERR_OR_ZERO(crtc_state);
6370 		if (ret)
6371 			goto free;
6372 
6373 		if (!crtc_state->active)
6374 			continue;
6375 
6376 		crtc_state->active = false;
6377 		crtc_mask |= 1 << drm_crtc_index(crtc);
6378 	}
6379 
6380 	if (crtc_mask) {
6381 		ret = drm_atomic_commit(state);
6382 
6383 		if (!ret) {
6384 			for_each_crtc(dev, crtc)
6385 				if (crtc_mask & (1 << drm_crtc_index(crtc)))
6386 					crtc->state->active = true;
6387 
6388 			return ret;
6389 		}
6390 	}
6391 
6392 free:
6393 	if (ret)
6394 		DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6395 	drm_atomic_state_free(state);
6396 	return ret;
6397 }
6398 
6399 void intel_encoder_destroy(struct drm_encoder *encoder)
6400 {
6401 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
6402 
6403 	drm_encoder_cleanup(encoder);
6404 	kfree(intel_encoder);
6405 }
6406 
6407 /* Cross check the actual hw state with our own modeset state tracking (and it's
6408  * internal consistency). */
6409 static void intel_connector_check_state(struct intel_connector *connector)
6410 {
6411 	struct drm_crtc *crtc = connector->base.state->crtc;
6412 
6413 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6414 		      connector->base.base.id,
6415 		      connector->base.name);
6416 
6417 	if (connector->get_hw_state(connector)) {
6418 		struct intel_encoder *encoder = connector->encoder;
6419 		struct drm_connector_state *conn_state = connector->base.state;
6420 
6421 		I915_STATE_WARN(!crtc,
6422 			 "connector enabled without attached crtc\n");
6423 
6424 		if (!crtc)
6425 			return;
6426 
6427 		I915_STATE_WARN(!crtc->state->active,
6428 		      "connector is active, but attached crtc isn't\n");
6429 
6430 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
6431 			return;
6432 
6433 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
6434 			"atomic encoder doesn't match attached encoder\n");
6435 
6436 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
6437 			"attached encoder crtc differs from connector crtc\n");
6438 	} else {
6439 		I915_STATE_WARN(crtc && crtc->state->active,
6440 			"attached crtc is active, but connector isn't\n");
6441 		I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6442 			"best encoder set without crtc!\n");
6443 	}
6444 }
6445 
6446 int intel_connector_init(struct intel_connector *connector)
6447 {
6448 	struct drm_connector_state *connector_state;
6449 
6450 	connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6451 	if (!connector_state)
6452 		return -ENOMEM;
6453 
6454 	connector->base.state = connector_state;
6455 	return 0;
6456 }
6457 
6458 struct intel_connector *intel_connector_alloc(void)
6459 {
6460 	struct intel_connector *connector;
6461 
6462 	connector = kzalloc(sizeof *connector, GFP_KERNEL);
6463 	if (!connector)
6464 		return NULL;
6465 
6466 	if (intel_connector_init(connector) < 0) {
6467 		kfree(connector);
6468 		return NULL;
6469 	}
6470 
6471 	return connector;
6472 }
6473 
6474 /* Simple connector->get_hw_state implementation for encoders that support only
6475  * one connector and no cloning and hence the encoder state determines the state
6476  * of the connector. */
6477 bool intel_connector_get_hw_state(struct intel_connector *connector)
6478 {
6479 	enum i915_pipe pipe = 0;
6480 	struct intel_encoder *encoder = connector->encoder;
6481 
6482 	return encoder->get_hw_state(encoder, &pipe);
6483 }
6484 
6485 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
6486 {
6487 	if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6488 		return crtc_state->fdi_lanes;
6489 
6490 	return 0;
6491 }
6492 
6493 static int ironlake_check_fdi_lanes(struct drm_device *dev, enum i915_pipe pipe,
6494 				     struct intel_crtc_state *pipe_config)
6495 {
6496 	struct drm_atomic_state *state = pipe_config->base.state;
6497 	struct intel_crtc *other_crtc;
6498 	struct intel_crtc_state *other_crtc_state;
6499 
6500 	DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6501 		      pipe_name(pipe), pipe_config->fdi_lanes);
6502 	if (pipe_config->fdi_lanes > 4) {
6503 		DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6504 			      pipe_name(pipe), pipe_config->fdi_lanes);
6505 		return -EINVAL;
6506 	}
6507 
6508 	if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
6509 		if (pipe_config->fdi_lanes > 2) {
6510 			DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6511 				      pipe_config->fdi_lanes);
6512 			return -EINVAL;
6513 		} else {
6514 			return 0;
6515 		}
6516 	}
6517 
6518 	if (INTEL_INFO(dev)->num_pipes == 2)
6519 		return 0;
6520 
6521 	/* Ivybridge 3 pipe is really complicated */
6522 	switch (pipe) {
6523 	case PIPE_A:
6524 		return 0;
6525 	case PIPE_B:
6526 		if (pipe_config->fdi_lanes <= 2)
6527 			return 0;
6528 
6529 		other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6530 		other_crtc_state =
6531 			intel_atomic_get_crtc_state(state, other_crtc);
6532 		if (IS_ERR(other_crtc_state))
6533 			return PTR_ERR(other_crtc_state);
6534 
6535 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
6536 			DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6537 				      pipe_name(pipe), pipe_config->fdi_lanes);
6538 			return -EINVAL;
6539 		}
6540 		return 0;
6541 	case PIPE_C:
6542 		if (pipe_config->fdi_lanes > 2) {
6543 			DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6544 				      pipe_name(pipe), pipe_config->fdi_lanes);
6545 			return -EINVAL;
6546 		}
6547 
6548 		other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6549 		other_crtc_state =
6550 			intel_atomic_get_crtc_state(state, other_crtc);
6551 		if (IS_ERR(other_crtc_state))
6552 			return PTR_ERR(other_crtc_state);
6553 
6554 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
6555 			DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6556 			return -EINVAL;
6557 		}
6558 		return 0;
6559 	default:
6560 		BUG();
6561 	}
6562 }
6563 
6564 #define RETRY 1
6565 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
6566 				       struct intel_crtc_state *pipe_config)
6567 {
6568 	struct drm_device *dev = intel_crtc->base.dev;
6569 	const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6570 	int lane, link_bw, fdi_dotclock, ret;
6571 	bool needs_recompute = false;
6572 
6573 retry:
6574 	/* FDI is a binary signal running at ~2.7GHz, encoding
6575 	 * each output octet as 10 bits. The actual frequency
6576 	 * is stored as a divider into a 100MHz clock, and the
6577 	 * mode pixel clock is stored in units of 1KHz.
6578 	 * Hence the bw of each lane in terms of the mode signal
6579 	 * is:
6580 	 */
6581 	link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6582 
6583 	fdi_dotclock = adjusted_mode->crtc_clock;
6584 
6585 	lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
6586 					   pipe_config->pipe_bpp);
6587 
6588 	pipe_config->fdi_lanes = lane;
6589 
6590 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
6591 			       link_bw, &pipe_config->fdi_m_n);
6592 
6593 	ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6594 				       intel_crtc->pipe, pipe_config);
6595 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
6596 		pipe_config->pipe_bpp -= 2*3;
6597 		DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6598 			      pipe_config->pipe_bpp);
6599 		needs_recompute = true;
6600 		pipe_config->bw_constrained = true;
6601 
6602 		goto retry;
6603 	}
6604 
6605 	if (needs_recompute)
6606 		return RETRY;
6607 
6608 	return ret;
6609 }
6610 
6611 static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6612 				     struct intel_crtc_state *pipe_config)
6613 {
6614 	if (pipe_config->pipe_bpp > 24)
6615 		return false;
6616 
6617 	/* HSW can handle pixel rate up to cdclk? */
6618 	if (IS_HASWELL(dev_priv->dev))
6619 		return true;
6620 
6621 	/*
6622 	 * We compare against max which means we must take
6623 	 * the increased cdclk requirement into account when
6624 	 * calculating the new cdclk.
6625 	 *
6626 	 * Should measure whether using a lower cdclk w/o IPS
6627 	 */
6628 	return ilk_pipe_pixel_rate(pipe_config) <=
6629 		dev_priv->max_cdclk_freq * 95 / 100;
6630 }
6631 
6632 static void hsw_compute_ips_config(struct intel_crtc *crtc,
6633 				   struct intel_crtc_state *pipe_config)
6634 {
6635 	struct drm_device *dev = crtc->base.dev;
6636 	struct drm_i915_private *dev_priv = dev->dev_private;
6637 
6638 	pipe_config->ips_enabled = i915.enable_ips &&
6639 		hsw_crtc_supports_ips(crtc) &&
6640 		pipe_config_supports_ips(dev_priv, pipe_config);
6641 }
6642 
6643 static int intel_crtc_compute_config(struct intel_crtc *crtc,
6644 				     struct intel_crtc_state *pipe_config)
6645 {
6646 	struct drm_device *dev = crtc->base.dev;
6647 	struct drm_i915_private *dev_priv = dev->dev_private;
6648 	const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6649 
6650 	/* FIXME should check pixel clock limits on all platforms */
6651 	if (INTEL_INFO(dev)->gen < 4) {
6652 		int clock_limit = dev_priv->max_cdclk_freq;
6653 
6654 		/*
6655 		 * Enable pixel doubling when the dot clock
6656 		 * is > 90% of the (display) core speed.
6657 		 *
6658 		 * GDG double wide on either pipe,
6659 		 * otherwise pipe A only.
6660 		 */
6661 		if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
6662 		    adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
6663 			clock_limit *= 2;
6664 			pipe_config->double_wide = true;
6665 		}
6666 
6667 		if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
6668 			return -EINVAL;
6669 	}
6670 
6671 	/*
6672 	 * Pipe horizontal size must be even in:
6673 	 * - DVO ganged mode
6674 	 * - LVDS dual channel mode
6675 	 * - Double wide pipe
6676 	 */
6677 	if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6678 	     intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6679 		pipe_config->pipe_src_w &= ~1;
6680 
6681 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
6682 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6683 	 */
6684 	if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6685 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
6686 		return -EINVAL;
6687 
6688 	if (HAS_IPS(dev))
6689 		hsw_compute_ips_config(crtc, pipe_config);
6690 
6691 	if (pipe_config->has_pch_encoder)
6692 		return ironlake_fdi_compute_config(crtc, pipe_config);
6693 
6694 	return 0;
6695 }
6696 
6697 static int skylake_get_display_clock_speed(struct drm_device *dev)
6698 {
6699 	struct drm_i915_private *dev_priv = to_i915(dev);
6700 	uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6701 	uint32_t cdctl = I915_READ(CDCLK_CTL);
6702 	uint32_t linkrate;
6703 
6704 	if (!(lcpll1 & LCPLL_PLL_ENABLE))
6705 		return 24000; /* 24MHz is the cd freq with NSSC ref */
6706 
6707 	if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6708 		return 540000;
6709 
6710 	linkrate = (I915_READ(DPLL_CTRL1) &
6711 		    DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6712 
6713 	if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6714 	    linkrate == DPLL_CTRL1_LINK_RATE_1080) {
6715 		/* vco 8640 */
6716 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6717 		case CDCLK_FREQ_450_432:
6718 			return 432000;
6719 		case CDCLK_FREQ_337_308:
6720 			return 308570;
6721 		case CDCLK_FREQ_675_617:
6722 			return 617140;
6723 		default:
6724 			WARN(1, "Unknown cd freq selection\n");
6725 		}
6726 	} else {
6727 		/* vco 8100 */
6728 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6729 		case CDCLK_FREQ_450_432:
6730 			return 450000;
6731 		case CDCLK_FREQ_337_308:
6732 			return 337500;
6733 		case CDCLK_FREQ_675_617:
6734 			return 675000;
6735 		default:
6736 			WARN(1, "Unknown cd freq selection\n");
6737 		}
6738 	}
6739 
6740 	/* error case, do as if DPLL0 isn't enabled */
6741 	return 24000;
6742 }
6743 
6744 static int broxton_get_display_clock_speed(struct drm_device *dev)
6745 {
6746 	struct drm_i915_private *dev_priv = to_i915(dev);
6747 	uint32_t cdctl = I915_READ(CDCLK_CTL);
6748 	uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6749 	uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6750 	int cdclk;
6751 
6752 	if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6753 		return 19200;
6754 
6755 	cdclk = 19200 * pll_ratio / 2;
6756 
6757 	switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6758 	case BXT_CDCLK_CD2X_DIV_SEL_1:
6759 		return cdclk;  /* 576MHz or 624MHz */
6760 	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6761 		return cdclk * 2 / 3; /* 384MHz */
6762 	case BXT_CDCLK_CD2X_DIV_SEL_2:
6763 		return cdclk / 2; /* 288MHz */
6764 	case BXT_CDCLK_CD2X_DIV_SEL_4:
6765 		return cdclk / 4; /* 144MHz */
6766 	}
6767 
6768 	/* error case, do as if DE PLL isn't enabled */
6769 	return 19200;
6770 }
6771 
6772 static int broadwell_get_display_clock_speed(struct drm_device *dev)
6773 {
6774 	struct drm_i915_private *dev_priv = dev->dev_private;
6775 	uint32_t lcpll = I915_READ(LCPLL_CTL);
6776 	uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6777 
6778 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
6779 		return 800000;
6780 	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6781 		return 450000;
6782 	else if (freq == LCPLL_CLK_FREQ_450)
6783 		return 450000;
6784 	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6785 		return 540000;
6786 	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6787 		return 337500;
6788 	else
6789 		return 675000;
6790 }
6791 
6792 static int haswell_get_display_clock_speed(struct drm_device *dev)
6793 {
6794 	struct drm_i915_private *dev_priv = dev->dev_private;
6795 	uint32_t lcpll = I915_READ(LCPLL_CTL);
6796 	uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6797 
6798 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
6799 		return 800000;
6800 	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6801 		return 450000;
6802 	else if (freq == LCPLL_CLK_FREQ_450)
6803 		return 450000;
6804 	else if (IS_HSW_ULT(dev))
6805 		return 337500;
6806 	else
6807 		return 540000;
6808 }
6809 
6810 static int valleyview_get_display_clock_speed(struct drm_device *dev)
6811 {
6812 	return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
6813 				      CCK_DISPLAY_CLOCK_CONTROL);
6814 }
6815 
6816 static int ilk_get_display_clock_speed(struct drm_device *dev)
6817 {
6818 	return 450000;
6819 }
6820 
6821 static int i945_get_display_clock_speed(struct drm_device *dev)
6822 {
6823 	return 400000;
6824 }
6825 
6826 static int i915_get_display_clock_speed(struct drm_device *dev)
6827 {
6828 	return 333333;
6829 }
6830 
6831 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6832 {
6833 	return 200000;
6834 }
6835 
6836 static int pnv_get_display_clock_speed(struct drm_device *dev)
6837 {
6838 	u16 gcfgc = 0;
6839 
6840 	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6841 
6842 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6843 	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
6844 		return 266667;
6845 	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
6846 		return 333333;
6847 	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
6848 		return 444444;
6849 	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6850 		return 200000;
6851 	default:
6852 		DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6853 	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
6854 		return 133333;
6855 	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
6856 		return 166667;
6857 	}
6858 }
6859 
6860 static int i915gm_get_display_clock_speed(struct drm_device *dev)
6861 {
6862 	u16 gcfgc = 0;
6863 
6864 	pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6865 
6866 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
6867 		return 133333;
6868 	else {
6869 		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6870 		case GC_DISPLAY_CLOCK_333_MHZ:
6871 			return 333333;
6872 		default:
6873 		case GC_DISPLAY_CLOCK_190_200_MHZ:
6874 			return 190000;
6875 		}
6876 	}
6877 }
6878 
6879 static int i865_get_display_clock_speed(struct drm_device *dev)
6880 {
6881 	return 266667;
6882 }
6883 
6884 static int i85x_get_display_clock_speed(struct drm_device *dev)
6885 {
6886 	u16 hpllcc = 0;
6887 
6888 	/*
6889 	 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6890 	 * encoding is different :(
6891 	 * FIXME is this the right way to detect 852GM/852GMV?
6892 	 */
6893 	if (dev->pdev->revision == 0x1)
6894 		return 133333;
6895 
6896 #if 0
6897 	pci_bus_read_config_word(dev->pdev->bus,
6898 				 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6899 #endif
6900 
6901 	/* Assume that the hardware is in the high speed state.  This
6902 	 * should be the default.
6903 	 */
6904 	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6905 	case GC_CLOCK_133_200:
6906 	case GC_CLOCK_133_200_2:
6907 	case GC_CLOCK_100_200:
6908 		return 200000;
6909 	case GC_CLOCK_166_250:
6910 		return 250000;
6911 	case GC_CLOCK_100_133:
6912 		return 133333;
6913 	case GC_CLOCK_133_266:
6914 	case GC_CLOCK_133_266_2:
6915 	case GC_CLOCK_166_266:
6916 		return 266667;
6917 	}
6918 
6919 	/* Shouldn't happen */
6920 	return 0;
6921 }
6922 
6923 static int i830_get_display_clock_speed(struct drm_device *dev)
6924 {
6925 	return 133333;
6926 }
6927 
6928 static unsigned int intel_hpll_vco(struct drm_device *dev)
6929 {
6930 	struct drm_i915_private *dev_priv = dev->dev_private;
6931 	static const unsigned int blb_vco[8] = {
6932 		[0] = 3200000,
6933 		[1] = 4000000,
6934 		[2] = 5333333,
6935 		[3] = 4800000,
6936 		[4] = 6400000,
6937 	};
6938 	static const unsigned int pnv_vco[8] = {
6939 		[0] = 3200000,
6940 		[1] = 4000000,
6941 		[2] = 5333333,
6942 		[3] = 4800000,
6943 		[4] = 2666667,
6944 	};
6945 	static const unsigned int cl_vco[8] = {
6946 		[0] = 3200000,
6947 		[1] = 4000000,
6948 		[2] = 5333333,
6949 		[3] = 6400000,
6950 		[4] = 3333333,
6951 		[5] = 3566667,
6952 		[6] = 4266667,
6953 	};
6954 	static const unsigned int elk_vco[8] = {
6955 		[0] = 3200000,
6956 		[1] = 4000000,
6957 		[2] = 5333333,
6958 		[3] = 4800000,
6959 	};
6960 	static const unsigned int ctg_vco[8] = {
6961 		[0] = 3200000,
6962 		[1] = 4000000,
6963 		[2] = 5333333,
6964 		[3] = 6400000,
6965 		[4] = 2666667,
6966 		[5] = 4266667,
6967 	};
6968 	const unsigned int *vco_table;
6969 	unsigned int vco;
6970 	uint8_t tmp = 0;
6971 
6972 	/* FIXME other chipsets? */
6973 	if (IS_GM45(dev))
6974 		vco_table = ctg_vco;
6975 	else if (IS_G4X(dev))
6976 		vco_table = elk_vco;
6977 	else if (IS_CRESTLINE(dev))
6978 		vco_table = cl_vco;
6979 	else if (IS_PINEVIEW(dev))
6980 		vco_table = pnv_vco;
6981 	else if (IS_G33(dev))
6982 		vco_table = blb_vco;
6983 	else
6984 		return 0;
6985 
6986 	tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6987 
6988 	vco = vco_table[tmp & 0x7];
6989 	if (vco == 0)
6990 		DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6991 	else
6992 		DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6993 
6994 	return vco;
6995 }
6996 
6997 static int gm45_get_display_clock_speed(struct drm_device *dev)
6998 {
6999 	unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7000 	uint16_t tmp = 0;
7001 
7002 	pci_read_config_word(dev->pdev, GCFGC, &tmp);
7003 
7004 	cdclk_sel = (tmp >> 12) & 0x1;
7005 
7006 	switch (vco) {
7007 	case 2666667:
7008 	case 4000000:
7009 	case 5333333:
7010 		return cdclk_sel ? 333333 : 222222;
7011 	case 3200000:
7012 		return cdclk_sel ? 320000 : 228571;
7013 	default:
7014 		DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7015 		return 222222;
7016 	}
7017 }
7018 
7019 static int i965gm_get_display_clock_speed(struct drm_device *dev)
7020 {
7021 	static const uint8_t div_3200[] = { 16, 10,  8 };
7022 	static const uint8_t div_4000[] = { 20, 12, 10 };
7023 	static const uint8_t div_5333[] = { 24, 16, 14 };
7024 	const uint8_t *div_table;
7025 	unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7026 	uint16_t tmp = 0;
7027 
7028 	pci_read_config_word(dev->pdev, GCFGC, &tmp);
7029 
7030 	cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7031 
7032 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
7033 		goto fail;
7034 
7035 	switch (vco) {
7036 	case 3200000:
7037 		div_table = div_3200;
7038 		break;
7039 	case 4000000:
7040 		div_table = div_4000;
7041 		break;
7042 	case 5333333:
7043 		div_table = div_5333;
7044 		break;
7045 	default:
7046 		goto fail;
7047 	}
7048 
7049 	return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7050 
7051 fail:
7052 	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7053 	return 200000;
7054 }
7055 
7056 static int g33_get_display_clock_speed(struct drm_device *dev)
7057 {
7058 	static const uint8_t div_3200[] = { 12, 10,  8,  7, 5, 16 };
7059 	static const uint8_t div_4000[] = { 14, 12, 10,  8, 6, 20 };
7060 	static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7061 	static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7062 	const uint8_t *div_table;
7063 	unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7064 	uint16_t tmp = 0;
7065 
7066 	pci_read_config_word(dev->pdev, GCFGC, &tmp);
7067 
7068 	cdclk_sel = (tmp >> 4) & 0x7;
7069 
7070 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
7071 		goto fail;
7072 
7073 	switch (vco) {
7074 	case 3200000:
7075 		div_table = div_3200;
7076 		break;
7077 	case 4000000:
7078 		div_table = div_4000;
7079 		break;
7080 	case 4800000:
7081 		div_table = div_4800;
7082 		break;
7083 	case 5333333:
7084 		div_table = div_5333;
7085 		break;
7086 	default:
7087 		goto fail;
7088 	}
7089 
7090 	return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7091 
7092 fail:
7093 	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7094 	return 190476;
7095 }
7096 
7097 static void
7098 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
7099 {
7100 	while (*num > DATA_LINK_M_N_MASK ||
7101 	       *den > DATA_LINK_M_N_MASK) {
7102 		*num >>= 1;
7103 		*den >>= 1;
7104 	}
7105 }
7106 
7107 static void compute_m_n(unsigned int m, unsigned int n,
7108 			uint32_t *ret_m, uint32_t *ret_n)
7109 {
7110 	*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7111 	*ret_m = div_u64((uint64_t) m * *ret_n, n);
7112 	intel_reduce_m_n_ratio(ret_m, ret_n);
7113 }
7114 
7115 void
7116 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7117 		       int pixel_clock, int link_clock,
7118 		       struct intel_link_m_n *m_n)
7119 {
7120 	m_n->tu = 64;
7121 
7122 	compute_m_n(bits_per_pixel * pixel_clock,
7123 		    link_clock * nlanes * 8,
7124 		    &m_n->gmch_m, &m_n->gmch_n);
7125 
7126 	compute_m_n(pixel_clock, link_clock,
7127 		    &m_n->link_m, &m_n->link_n);
7128 }
7129 
7130 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7131 {
7132 	if (i915.panel_use_ssc >= 0)
7133 		return i915.panel_use_ssc != 0;
7134 	return dev_priv->vbt.lvds_use_ssc
7135 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
7136 }
7137 
7138 static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7139 			   int num_connectors)
7140 {
7141 	struct drm_device *dev = crtc_state->base.crtc->dev;
7142 	struct drm_i915_private *dev_priv = dev->dev_private;
7143 	int refclk;
7144 
7145 	WARN_ON(!crtc_state->base.state);
7146 
7147 	if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
7148 		refclk = 100000;
7149 	} else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7150 	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
7151 		refclk = dev_priv->vbt.lvds_ssc_freq;
7152 		DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7153 	} else if (!IS_GEN2(dev)) {
7154 		refclk = 96000;
7155 	} else {
7156 		refclk = 48000;
7157 	}
7158 
7159 	return refclk;
7160 }
7161 
7162 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
7163 {
7164 	return (1 << dpll->n) << 16 | dpll->m2;
7165 }
7166 
7167 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7168 {
7169 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
7170 }
7171 
7172 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
7173 				     struct intel_crtc_state *crtc_state,
7174 				     intel_clock_t *reduced_clock)
7175 {
7176 	struct drm_device *dev = crtc->base.dev;
7177 	u32 fp, fp2 = 0;
7178 
7179 	if (IS_PINEVIEW(dev)) {
7180 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
7181 		if (reduced_clock)
7182 			fp2 = pnv_dpll_compute_fp(reduced_clock);
7183 	} else {
7184 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
7185 		if (reduced_clock)
7186 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
7187 	}
7188 
7189 	crtc_state->dpll_hw_state.fp0 = fp;
7190 
7191 	crtc->lowfreq_avail = false;
7192 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7193 	    reduced_clock) {
7194 		crtc_state->dpll_hw_state.fp1 = fp2;
7195 		crtc->lowfreq_avail = true;
7196 	} else {
7197 		crtc_state->dpll_hw_state.fp1 = fp;
7198 	}
7199 }
7200 
7201 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum i915_pipe
7202 		pipe)
7203 {
7204 	u32 reg_val;
7205 
7206 	/*
7207 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7208 	 * and set it to a reasonable value instead.
7209 	 */
7210 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7211 	reg_val &= 0xffffff00;
7212 	reg_val |= 0x00000030;
7213 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7214 
7215 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7216 	reg_val &= 0x8cffffff;
7217 	reg_val = 0x8c000000;
7218 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7219 
7220 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7221 	reg_val &= 0xffffff00;
7222 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7223 
7224 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7225 	reg_val &= 0x00ffffff;
7226 	reg_val |= 0xb0000000;
7227 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7228 }
7229 
7230 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7231 					 struct intel_link_m_n *m_n)
7232 {
7233 	struct drm_device *dev = crtc->base.dev;
7234 	struct drm_i915_private *dev_priv = dev->dev_private;
7235 	int pipe = crtc->pipe;
7236 
7237 	I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7238 	I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7239 	I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7240 	I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
7241 }
7242 
7243 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
7244 					 struct intel_link_m_n *m_n,
7245 					 struct intel_link_m_n *m2_n2)
7246 {
7247 	struct drm_device *dev = crtc->base.dev;
7248 	struct drm_i915_private *dev_priv = dev->dev_private;
7249 	int pipe = crtc->pipe;
7250 	enum transcoder transcoder = crtc->config->cpu_transcoder;
7251 
7252 	if (INTEL_INFO(dev)->gen >= 5) {
7253 		I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7254 		I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7255 		I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7256 		I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
7257 		/* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7258 		 * for gen < 8) and if DRRS is supported (to make sure the
7259 		 * registers are not unnecessarily accessed).
7260 		 */
7261 		if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
7262 			crtc->config->has_drrs) {
7263 			I915_WRITE(PIPE_DATA_M2(transcoder),
7264 					TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7265 			I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7266 			I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7267 			I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7268 		}
7269 	} else {
7270 		I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7271 		I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7272 		I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7273 		I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
7274 	}
7275 }
7276 
7277 void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
7278 {
7279 	struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7280 
7281 	if (m_n == M1_N1) {
7282 		dp_m_n = &crtc->config->dp_m_n;
7283 		dp_m2_n2 = &crtc->config->dp_m2_n2;
7284 	} else if (m_n == M2_N2) {
7285 
7286 		/*
7287 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
7288 		 * needs to be programmed into M1_N1.
7289 		 */
7290 		dp_m_n = &crtc->config->dp_m2_n2;
7291 	} else {
7292 		DRM_ERROR("Unsupported divider value\n");
7293 		return;
7294 	}
7295 
7296 	if (crtc->config->has_pch_encoder)
7297 		intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
7298 	else
7299 		intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
7300 }
7301 
7302 static void vlv_compute_dpll(struct intel_crtc *crtc,
7303 			     struct intel_crtc_state *pipe_config)
7304 {
7305 	u32 dpll, dpll_md;
7306 
7307 	/*
7308 	 * Enable DPIO clock input. We should never disable the reference
7309 	 * clock for pipe B, since VGA hotplug / manual detection depends
7310 	 * on it.
7311 	 */
7312 	dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7313 		DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
7314 	/* We should never disable this, set it here for state tracking */
7315 	if (crtc->pipe == PIPE_B)
7316 		dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7317 	dpll |= DPLL_VCO_ENABLE;
7318 	pipe_config->dpll_hw_state.dpll = dpll;
7319 
7320 	dpll_md = (pipe_config->pixel_multiplier - 1)
7321 		<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
7322 	pipe_config->dpll_hw_state.dpll_md = dpll_md;
7323 }
7324 
7325 static void vlv_prepare_pll(struct intel_crtc *crtc,
7326 			    const struct intel_crtc_state *pipe_config)
7327 {
7328 	struct drm_device *dev = crtc->base.dev;
7329 	struct drm_i915_private *dev_priv = dev->dev_private;
7330 	int pipe = crtc->pipe;
7331 	u32 mdiv;
7332 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
7333 	u32 coreclk, reg_val;
7334 
7335 	mutex_lock(&dev_priv->sb_lock);
7336 
7337 	bestn = pipe_config->dpll.n;
7338 	bestm1 = pipe_config->dpll.m1;
7339 	bestm2 = pipe_config->dpll.m2;
7340 	bestp1 = pipe_config->dpll.p1;
7341 	bestp2 = pipe_config->dpll.p2;
7342 
7343 	/* See eDP HDMI DPIO driver vbios notes doc */
7344 
7345 	/* PLL B needs special handling */
7346 	if (pipe == PIPE_B)
7347 		vlv_pllb_recal_opamp(dev_priv, pipe);
7348 
7349 	/* Set up Tx target for periodic Rcomp update */
7350 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
7351 
7352 	/* Disable target IRef on PLL */
7353 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
7354 	reg_val &= 0x00ffffff;
7355 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
7356 
7357 	/* Disable fast lock */
7358 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
7359 
7360 	/* Set idtafcrecal before PLL is enabled */
7361 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7362 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7363 	mdiv |= ((bestn << DPIO_N_SHIFT));
7364 	mdiv |= (1 << DPIO_K_SHIFT);
7365 
7366 	/*
7367 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7368 	 * but we don't support that).
7369 	 * Note: don't use the DAC post divider as it seems unstable.
7370 	 */
7371 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
7372 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7373 
7374 	mdiv |= DPIO_ENABLE_CALIBRATION;
7375 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7376 
7377 	/* Set HBR and RBR LPF coefficients */
7378 	if (pipe_config->port_clock == 162000 ||
7379 	    intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7380 	    intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
7381 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7382 				 0x009f0003);
7383 	else
7384 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7385 				 0x00d0000f);
7386 
7387 	if (pipe_config->has_dp_encoder) {
7388 		/* Use SSC source */
7389 		if (pipe == PIPE_A)
7390 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7391 					 0x0df40000);
7392 		else
7393 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7394 					 0x0df70000);
7395 	} else { /* HDMI or VGA */
7396 		/* Use bend source */
7397 		if (pipe == PIPE_A)
7398 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7399 					 0x0df70000);
7400 		else
7401 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7402 					 0x0df40000);
7403 	}
7404 
7405 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
7406 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
7407 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7408 	    intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
7409 		coreclk |= 0x01000000;
7410 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
7411 
7412 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
7413 	mutex_unlock(&dev_priv->sb_lock);
7414 }
7415 
7416 static void chv_compute_dpll(struct intel_crtc *crtc,
7417 			     struct intel_crtc_state *pipe_config)
7418 {
7419 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7420 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7421 		DPLL_VCO_ENABLE;
7422 	if (crtc->pipe != PIPE_A)
7423 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7424 
7425 	pipe_config->dpll_hw_state.dpll_md =
7426 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7427 }
7428 
7429 static void chv_prepare_pll(struct intel_crtc *crtc,
7430 			    const struct intel_crtc_state *pipe_config)
7431 {
7432 	struct drm_device *dev = crtc->base.dev;
7433 	struct drm_i915_private *dev_priv = dev->dev_private;
7434 	int pipe = crtc->pipe;
7435 	int dpll_reg = DPLL(crtc->pipe);
7436 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
7437 	u32 loopfilter, tribuf_calcntr;
7438 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
7439 	u32 dpio_val;
7440 	int vco;
7441 
7442 	bestn = pipe_config->dpll.n;
7443 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7444 	bestm1 = pipe_config->dpll.m1;
7445 	bestm2 = pipe_config->dpll.m2 >> 22;
7446 	bestp1 = pipe_config->dpll.p1;
7447 	bestp2 = pipe_config->dpll.p2;
7448 	vco = pipe_config->dpll.vco;
7449 	dpio_val = 0;
7450 	loopfilter = 0;
7451 
7452 	/*
7453 	 * Enable Refclk and SSC
7454 	 */
7455 	I915_WRITE(dpll_reg,
7456 		   pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7457 
7458 	mutex_lock(&dev_priv->sb_lock);
7459 
7460 	/* p1 and p2 divider */
7461 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7462 			5 << DPIO_CHV_S1_DIV_SHIFT |
7463 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7464 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7465 			1 << DPIO_CHV_K_DIV_SHIFT);
7466 
7467 	/* Feedback post-divider - m2 */
7468 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7469 
7470 	/* Feedback refclk divider - n and m1 */
7471 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7472 			DPIO_CHV_M1_DIV_BY_2 |
7473 			1 << DPIO_CHV_N_DIV_SHIFT);
7474 
7475 	/* M2 fraction division */
7476 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
7477 
7478 	/* M2 fraction division enable */
7479 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7480 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7481 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7482 	if (bestm2_frac)
7483 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7484 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
7485 
7486 	/* Program digital lock detect threshold */
7487 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7488 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7489 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7490 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7491 	if (!bestm2_frac)
7492 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7493 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7494 
7495 	/* Loop filter */
7496 	if (vco == 5400000) {
7497 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7499 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 		tribuf_calcntr = 0x9;
7501 	} else if (vco <= 6200000) {
7502 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7503 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7504 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7505 		tribuf_calcntr = 0x9;
7506 	} else if (vco <= 6480000) {
7507 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7508 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7509 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7510 		tribuf_calcntr = 0x8;
7511 	} else {
7512 		/* Not supported. Apply the same limits as in the max case */
7513 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7514 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7515 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7516 		tribuf_calcntr = 0;
7517 	}
7518 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7519 
7520 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
7521 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7522 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7523 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7524 
7525 	/* AFC Recal */
7526 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7527 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7528 			DPIO_AFC_RECAL);
7529 
7530 	mutex_unlock(&dev_priv->sb_lock);
7531 }
7532 
7533 /**
7534  * vlv_force_pll_on - forcibly enable just the PLL
7535  * @dev_priv: i915 private structure
7536  * @pipe: pipe PLL to enable
7537  * @dpll: PLL configuration
7538  *
7539  * Enable the PLL for @pipe using the supplied @dpll config. To be used
7540  * in cases where we need the PLL enabled even when @pipe is not going to
7541  * be enabled.
7542  */
7543 void vlv_force_pll_on(struct drm_device *dev, enum i915_pipe pipe,
7544 		      const struct dpll *dpll)
7545 {
7546 	struct intel_crtc *crtc =
7547 		to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
7548 	struct intel_crtc_state pipe_config = {
7549 		.base.crtc = &crtc->base,
7550 		.pixel_multiplier = 1,
7551 		.dpll = *dpll,
7552 	};
7553 
7554 	if (IS_CHERRYVIEW(dev)) {
7555 		chv_compute_dpll(crtc, &pipe_config);
7556 		chv_prepare_pll(crtc, &pipe_config);
7557 		chv_enable_pll(crtc, &pipe_config);
7558 	} else {
7559 		vlv_compute_dpll(crtc, &pipe_config);
7560 		vlv_prepare_pll(crtc, &pipe_config);
7561 		vlv_enable_pll(crtc, &pipe_config);
7562 	}
7563 }
7564 
7565 /**
7566  * vlv_force_pll_off - forcibly disable just the PLL
7567  * @dev_priv: i915 private structure
7568  * @pipe: pipe PLL to disable
7569  *
7570  * Disable the PLL for @pipe. To be used in cases where we need
7571  * the PLL enabled even when @pipe is not going to be enabled.
7572  */
7573 void vlv_force_pll_off(struct drm_device *dev, enum i915_pipe pipe)
7574 {
7575 	if (IS_CHERRYVIEW(dev))
7576 		chv_disable_pll(to_i915(dev), pipe);
7577 	else
7578 		vlv_disable_pll(to_i915(dev), pipe);
7579 }
7580 
7581 static void i9xx_compute_dpll(struct intel_crtc *crtc,
7582 			      struct intel_crtc_state *crtc_state,
7583 			      intel_clock_t *reduced_clock,
7584 			      int num_connectors)
7585 {
7586 	struct drm_device *dev = crtc->base.dev;
7587 	struct drm_i915_private *dev_priv = dev->dev_private;
7588 	u32 dpll;
7589 	bool is_sdvo;
7590 	struct dpll *clock = &crtc_state->dpll;
7591 
7592 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
7593 
7594 	is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7595 		intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
7596 
7597 	dpll = DPLL_VGA_MODE_DIS;
7598 
7599 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
7600 		dpll |= DPLLB_MODE_LVDS;
7601 	else
7602 		dpll |= DPLLB_MODE_DAC_SERIAL;
7603 
7604 	if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7605 		dpll |= (crtc_state->pixel_multiplier - 1)
7606 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
7607 	}
7608 
7609 	if (is_sdvo)
7610 		dpll |= DPLL_SDVO_HIGH_SPEED;
7611 
7612 	if (crtc_state->has_dp_encoder)
7613 		dpll |= DPLL_SDVO_HIGH_SPEED;
7614 
7615 	/* compute bitmask from p1 value */
7616 	if (IS_PINEVIEW(dev))
7617 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7618 	else {
7619 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7620 		if (IS_G4X(dev) && reduced_clock)
7621 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7622 	}
7623 	switch (clock->p2) {
7624 	case 5:
7625 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7626 		break;
7627 	case 7:
7628 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7629 		break;
7630 	case 10:
7631 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7632 		break;
7633 	case 14:
7634 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7635 		break;
7636 	}
7637 	if (INTEL_INFO(dev)->gen >= 4)
7638 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7639 
7640 	if (crtc_state->sdvo_tv_clock)
7641 		dpll |= PLL_REF_INPUT_TVCLKINBC;
7642 	else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7643 		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7644 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7645 	else
7646 		dpll |= PLL_REF_INPUT_DREFCLK;
7647 
7648 	dpll |= DPLL_VCO_ENABLE;
7649 	crtc_state->dpll_hw_state.dpll = dpll;
7650 
7651 	if (INTEL_INFO(dev)->gen >= 4) {
7652 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
7653 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
7654 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
7655 	}
7656 }
7657 
7658 static void i8xx_compute_dpll(struct intel_crtc *crtc,
7659 			      struct intel_crtc_state *crtc_state,
7660 			      intel_clock_t *reduced_clock,
7661 			      int num_connectors)
7662 {
7663 	struct drm_device *dev = crtc->base.dev;
7664 	struct drm_i915_private *dev_priv = dev->dev_private;
7665 	u32 dpll;
7666 	struct dpll *clock = &crtc_state->dpll;
7667 
7668 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
7669 
7670 	dpll = DPLL_VGA_MODE_DIS;
7671 
7672 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7673 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7674 	} else {
7675 		if (clock->p1 == 2)
7676 			dpll |= PLL_P1_DIVIDE_BY_TWO;
7677 		else
7678 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7679 		if (clock->p2 == 4)
7680 			dpll |= PLL_P2_DIVIDE_BY_4;
7681 	}
7682 
7683 	if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
7684 		dpll |= DPLL_DVO_2X_MODE;
7685 
7686 	if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7687 		 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7688 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7689 	else
7690 		dpll |= PLL_REF_INPUT_DREFCLK;
7691 
7692 	dpll |= DPLL_VCO_ENABLE;
7693 	crtc_state->dpll_hw_state.dpll = dpll;
7694 }
7695 
7696 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
7697 {
7698 	struct drm_device *dev = intel_crtc->base.dev;
7699 	struct drm_i915_private *dev_priv = dev->dev_private;
7700 	enum i915_pipe pipe = intel_crtc->pipe;
7701 	enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7702 	const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
7703 	uint32_t crtc_vtotal, crtc_vblank_end;
7704 	int vsyncshift = 0;
7705 
7706 	/* We need to be careful not to changed the adjusted mode, for otherwise
7707 	 * the hw state checker will get angry at the mismatch. */
7708 	crtc_vtotal = adjusted_mode->crtc_vtotal;
7709 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
7710 
7711 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
7712 		/* the chip adds 2 halflines automatically */
7713 		crtc_vtotal -= 1;
7714 		crtc_vblank_end -= 1;
7715 
7716 		if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7717 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7718 		else
7719 			vsyncshift = adjusted_mode->crtc_hsync_start -
7720 				adjusted_mode->crtc_htotal / 2;
7721 		if (vsyncshift < 0)
7722 			vsyncshift += adjusted_mode->crtc_htotal;
7723 	}
7724 
7725 	if (INTEL_INFO(dev)->gen > 3)
7726 		I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
7727 
7728 	I915_WRITE(HTOTAL(cpu_transcoder),
7729 		   (adjusted_mode->crtc_hdisplay - 1) |
7730 		   ((adjusted_mode->crtc_htotal - 1) << 16));
7731 	I915_WRITE(HBLANK(cpu_transcoder),
7732 		   (adjusted_mode->crtc_hblank_start - 1) |
7733 		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
7734 	I915_WRITE(HSYNC(cpu_transcoder),
7735 		   (adjusted_mode->crtc_hsync_start - 1) |
7736 		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
7737 
7738 	I915_WRITE(VTOTAL(cpu_transcoder),
7739 		   (adjusted_mode->crtc_vdisplay - 1) |
7740 		   ((crtc_vtotal - 1) << 16));
7741 	I915_WRITE(VBLANK(cpu_transcoder),
7742 		   (adjusted_mode->crtc_vblank_start - 1) |
7743 		   ((crtc_vblank_end - 1) << 16));
7744 	I915_WRITE(VSYNC(cpu_transcoder),
7745 		   (adjusted_mode->crtc_vsync_start - 1) |
7746 		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
7747 
7748 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7749 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7750 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7751 	 * bits. */
7752 	if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7753 	    (pipe == PIPE_B || pipe == PIPE_C))
7754 		I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7755 
7756 	/* pipesrc controls the size that is scaled from, which should
7757 	 * always be the user's requested size.
7758 	 */
7759 	I915_WRITE(PIPESRC(pipe),
7760 		   ((intel_crtc->config->pipe_src_w - 1) << 16) |
7761 		   (intel_crtc->config->pipe_src_h - 1));
7762 }
7763 
7764 static void intel_get_pipe_timings(struct intel_crtc *crtc,
7765 				   struct intel_crtc_state *pipe_config)
7766 {
7767 	struct drm_device *dev = crtc->base.dev;
7768 	struct drm_i915_private *dev_priv = dev->dev_private;
7769 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7770 	uint32_t tmp;
7771 
7772 	tmp = I915_READ(HTOTAL(cpu_transcoder));
7773 	pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7774 	pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
7775 	tmp = I915_READ(HBLANK(cpu_transcoder));
7776 	pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7777 	pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
7778 	tmp = I915_READ(HSYNC(cpu_transcoder));
7779 	pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7780 	pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
7781 
7782 	tmp = I915_READ(VTOTAL(cpu_transcoder));
7783 	pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7784 	pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
7785 	tmp = I915_READ(VBLANK(cpu_transcoder));
7786 	pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7787 	pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
7788 	tmp = I915_READ(VSYNC(cpu_transcoder));
7789 	pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7790 	pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
7791 
7792 	if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
7793 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7794 		pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7795 		pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
7796 	}
7797 
7798 	tmp = I915_READ(PIPESRC(crtc->pipe));
7799 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7800 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7801 
7802 	pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7803 	pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
7804 }
7805 
7806 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
7807 				 struct intel_crtc_state *pipe_config)
7808 {
7809 	mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7810 	mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7811 	mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7812 	mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
7813 
7814 	mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7815 	mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7816 	mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7817 	mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
7818 
7819 	mode->flags = pipe_config->base.adjusted_mode.flags;
7820 	mode->type = DRM_MODE_TYPE_DRIVER;
7821 
7822 	mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7823 	mode->flags |= pipe_config->base.adjusted_mode.flags;
7824 
7825 	mode->hsync = drm_mode_hsync(mode);
7826 	mode->vrefresh = drm_mode_vrefresh(mode);
7827 	drm_mode_set_name(mode);
7828 }
7829 
7830 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7831 {
7832 	struct drm_device *dev = intel_crtc->base.dev;
7833 	struct drm_i915_private *dev_priv = dev->dev_private;
7834 	uint32_t pipeconf;
7835 
7836 	pipeconf = 0;
7837 
7838 	if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7839 	    (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7840 		pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
7841 
7842 	if (intel_crtc->config->double_wide)
7843 		pipeconf |= PIPECONF_DOUBLE_WIDE;
7844 
7845 	/* only g4x and later have fancy bpc/dither controls */
7846 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7847 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
7848 		if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
7849 			pipeconf |= PIPECONF_DITHER_EN |
7850 				    PIPECONF_DITHER_TYPE_SP;
7851 
7852 		switch (intel_crtc->config->pipe_bpp) {
7853 		case 18:
7854 			pipeconf |= PIPECONF_6BPC;
7855 			break;
7856 		case 24:
7857 			pipeconf |= PIPECONF_8BPC;
7858 			break;
7859 		case 30:
7860 			pipeconf |= PIPECONF_10BPC;
7861 			break;
7862 		default:
7863 			/* Case prevented by intel_choose_pipe_bpp_dither. */
7864 			BUG();
7865 		}
7866 	}
7867 
7868 	if (HAS_PIPE_CXSR(dev)) {
7869 		if (intel_crtc->lowfreq_avail) {
7870 			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7871 			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7872 		} else {
7873 			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
7874 		}
7875 	}
7876 
7877 	if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
7878 		if (INTEL_INFO(dev)->gen < 4 ||
7879 		    intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7880 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7881 		else
7882 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7883 	} else
7884 		pipeconf |= PIPECONF_PROGRESSIVE;
7885 
7886 	if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
7887 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
7888 
7889 	I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7890 	POSTING_READ(PIPECONF(intel_crtc->pipe));
7891 }
7892 
7893 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7894 				   struct intel_crtc_state *crtc_state)
7895 {
7896 	struct drm_device *dev = crtc->base.dev;
7897 	struct drm_i915_private *dev_priv = dev->dev_private;
7898 	int refclk, num_connectors = 0;
7899 	intel_clock_t clock;
7900 	bool ok;
7901 	bool is_dsi = false;
7902 	struct intel_encoder *encoder;
7903 	const intel_limit_t *limit;
7904 	struct drm_atomic_state *state = crtc_state->base.state;
7905 	struct drm_connector *connector;
7906 	struct drm_connector_state *connector_state;
7907 	int i;
7908 
7909 	memset(&crtc_state->dpll_hw_state, 0,
7910 	       sizeof(crtc_state->dpll_hw_state));
7911 
7912 	for_each_connector_in_state(state, connector, connector_state, i) {
7913 		if (connector_state->crtc != &crtc->base)
7914 			continue;
7915 
7916 		encoder = to_intel_encoder(connector_state->best_encoder);
7917 
7918 		switch (encoder->type) {
7919 		case INTEL_OUTPUT_DSI:
7920 			is_dsi = true;
7921 			break;
7922 		default:
7923 			break;
7924 		}
7925 
7926 		num_connectors++;
7927 	}
7928 
7929 	if (is_dsi)
7930 		return 0;
7931 
7932 	if (!crtc_state->clock_set) {
7933 		refclk = i9xx_get_refclk(crtc_state, num_connectors);
7934 
7935 		/*
7936 		 * Returns a set of divisors for the desired target clock with
7937 		 * the given refclk, or FALSE.  The returned values represent
7938 		 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7939 		 * 2) / p1 / p2.
7940 		 */
7941 		limit = intel_limit(crtc_state, refclk);
7942 		ok = dev_priv->display.find_dpll(limit, crtc_state,
7943 						 crtc_state->port_clock,
7944 						 refclk, NULL, &clock);
7945 		if (!ok) {
7946 			DRM_ERROR("Couldn't find PLL settings for mode!\n");
7947 			return -EINVAL;
7948 		}
7949 
7950 		/* Compat-code for transition, will disappear. */
7951 		crtc_state->dpll.n = clock.n;
7952 		crtc_state->dpll.m1 = clock.m1;
7953 		crtc_state->dpll.m2 = clock.m2;
7954 		crtc_state->dpll.p1 = clock.p1;
7955 		crtc_state->dpll.p2 = clock.p2;
7956 	}
7957 
7958 	if (IS_GEN2(dev)) {
7959 		i8xx_compute_dpll(crtc, crtc_state, NULL,
7960 				  num_connectors);
7961 	} else if (IS_CHERRYVIEW(dev)) {
7962 		chv_compute_dpll(crtc, crtc_state);
7963 	} else if (IS_VALLEYVIEW(dev)) {
7964 		vlv_compute_dpll(crtc, crtc_state);
7965 	} else {
7966 		i9xx_compute_dpll(crtc, crtc_state, NULL,
7967 				  num_connectors);
7968 	}
7969 
7970 	return 0;
7971 }
7972 
7973 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
7974 				 struct intel_crtc_state *pipe_config)
7975 {
7976 	struct drm_device *dev = crtc->base.dev;
7977 	struct drm_i915_private *dev_priv = dev->dev_private;
7978 	uint32_t tmp;
7979 
7980 	if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7981 		return;
7982 
7983 	tmp = I915_READ(PFIT_CONTROL);
7984 	if (!(tmp & PFIT_ENABLE))
7985 		return;
7986 
7987 	/* Check whether the pfit is attached to our pipe. */
7988 	if (INTEL_INFO(dev)->gen < 4) {
7989 		if (crtc->pipe != PIPE_B)
7990 			return;
7991 	} else {
7992 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7993 			return;
7994 	}
7995 
7996 	pipe_config->gmch_pfit.control = tmp;
7997 	pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7998 	if (INTEL_INFO(dev)->gen < 5)
7999 		pipe_config->gmch_pfit.lvds_border_bits =
8000 			I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8001 }
8002 
8003 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
8004 			       struct intel_crtc_state *pipe_config)
8005 {
8006 	struct drm_device *dev = crtc->base.dev;
8007 	struct drm_i915_private *dev_priv = dev->dev_private;
8008 	int pipe = pipe_config->cpu_transcoder;
8009 	intel_clock_t clock;
8010 	u32 mdiv;
8011 	int refclk = 100000;
8012 
8013 	/* In case of MIPI DPLL will not even be used */
8014 	if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8015 		return;
8016 
8017 	mutex_lock(&dev_priv->sb_lock);
8018 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
8019 	mutex_unlock(&dev_priv->sb_lock);
8020 
8021 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8022 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
8023 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8024 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8025 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8026 
8027 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
8028 }
8029 
8030 static void
8031 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8032 			      struct intel_initial_plane_config *plane_config)
8033 {
8034 	struct drm_device *dev = crtc->base.dev;
8035 	struct drm_i915_private *dev_priv = dev->dev_private;
8036 	u32 val, base, offset;
8037 	int pipe = crtc->pipe, plane = crtc->plane;
8038 	int fourcc, pixel_format;
8039 	unsigned int aligned_height;
8040 	struct drm_framebuffer *fb;
8041 	struct intel_framebuffer *intel_fb;
8042 
8043 	val = I915_READ(DSPCNTR(plane));
8044 	if (!(val & DISPLAY_PLANE_ENABLE))
8045 		return;
8046 
8047 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8048 	if (!intel_fb) {
8049 		DRM_DEBUG_KMS("failed to alloc fb\n");
8050 		return;
8051 	}
8052 
8053 	fb = &intel_fb->base;
8054 
8055 	if (INTEL_INFO(dev)->gen >= 4) {
8056 		if (val & DISPPLANE_TILED) {
8057 			plane_config->tiling = I915_TILING_X;
8058 			fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8059 		}
8060 	}
8061 
8062 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
8063 	fourcc = i9xx_format_to_fourcc(pixel_format);
8064 	fb->pixel_format = fourcc;
8065 	fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8066 
8067 	if (INTEL_INFO(dev)->gen >= 4) {
8068 		if (plane_config->tiling)
8069 			offset = I915_READ(DSPTILEOFF(plane));
8070 		else
8071 			offset = I915_READ(DSPLINOFF(plane));
8072 		base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8073 	} else {
8074 		base = I915_READ(DSPADDR(plane));
8075 	}
8076 	plane_config->base = base;
8077 
8078 	val = I915_READ(PIPESRC(pipe));
8079 	fb->width = ((val >> 16) & 0xfff) + 1;
8080 	fb->height = ((val >> 0) & 0xfff) + 1;
8081 
8082 	val = I915_READ(DSPSTRIDE(pipe));
8083 	fb->pitches[0] = val & 0xffffffc0;
8084 
8085 	aligned_height = intel_fb_align_height(dev, fb->height,
8086 					       fb->pixel_format,
8087 					       fb->modifier[0]);
8088 
8089 	plane_config->size = fb->pitches[0] * aligned_height;
8090 
8091 	DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8092 		      pipe_name(pipe), plane, fb->width, fb->height,
8093 		      fb->bits_per_pixel, base, fb->pitches[0],
8094 		      plane_config->size);
8095 
8096 	plane_config->fb = intel_fb;
8097 }
8098 
8099 static void chv_crtc_clock_get(struct intel_crtc *crtc,
8100 			       struct intel_crtc_state *pipe_config)
8101 {
8102 	struct drm_device *dev = crtc->base.dev;
8103 	struct drm_i915_private *dev_priv = dev->dev_private;
8104 	int pipe = pipe_config->cpu_transcoder;
8105 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8106 	intel_clock_t clock;
8107 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
8108 	int refclk = 100000;
8109 
8110 	mutex_lock(&dev_priv->sb_lock);
8111 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8112 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8113 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8114 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
8115 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8116 	mutex_unlock(&dev_priv->sb_lock);
8117 
8118 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8119 	clock.m2 = (pll_dw0 & 0xff) << 22;
8120 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8121 		clock.m2 |= pll_dw2 & 0x3fffff;
8122 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8123 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8124 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8125 
8126 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
8127 }
8128 
8129 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
8130 				 struct intel_crtc_state *pipe_config)
8131 {
8132 	struct drm_device *dev = crtc->base.dev;
8133 	struct drm_i915_private *dev_priv = dev->dev_private;
8134 	uint32_t tmp;
8135 
8136 	if (!intel_display_power_is_enabled(dev_priv,
8137 					    POWER_DOMAIN_PIPE(crtc->pipe)))
8138 		return false;
8139 
8140 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8141 	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8142 
8143 	tmp = I915_READ(PIPECONF(crtc->pipe));
8144 	if (!(tmp & PIPECONF_ENABLE))
8145 		return false;
8146 
8147 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8148 		switch (tmp & PIPECONF_BPC_MASK) {
8149 		case PIPECONF_6BPC:
8150 			pipe_config->pipe_bpp = 18;
8151 			break;
8152 		case PIPECONF_8BPC:
8153 			pipe_config->pipe_bpp = 24;
8154 			break;
8155 		case PIPECONF_10BPC:
8156 			pipe_config->pipe_bpp = 30;
8157 			break;
8158 		default:
8159 			break;
8160 		}
8161 	}
8162 
8163 	if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8164 		pipe_config->limited_color_range = true;
8165 
8166 	if (INTEL_INFO(dev)->gen < 4)
8167 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8168 
8169 	intel_get_pipe_timings(crtc, pipe_config);
8170 
8171 	i9xx_get_pfit_config(crtc, pipe_config);
8172 
8173 	if (INTEL_INFO(dev)->gen >= 4) {
8174 		tmp = I915_READ(DPLL_MD(crtc->pipe));
8175 		pipe_config->pixel_multiplier =
8176 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8177 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8178 		pipe_config->dpll_hw_state.dpll_md = tmp;
8179 	} else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8180 		tmp = I915_READ(DPLL(crtc->pipe));
8181 		pipe_config->pixel_multiplier =
8182 			((tmp & SDVO_MULTIPLIER_MASK)
8183 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8184 	} else {
8185 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
8186 		 * port and will be fixed up in the encoder->get_config
8187 		 * function. */
8188 		pipe_config->pixel_multiplier = 1;
8189 	}
8190 	pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8191 	if (!IS_VALLEYVIEW(dev)) {
8192 		/*
8193 		 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8194 		 * on 830. Filter it out here so that we don't
8195 		 * report errors due to that.
8196 		 */
8197 		if (IS_I830(dev))
8198 			pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8199 
8200 		pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8201 		pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
8202 	} else {
8203 		/* Mask out read-only status bits. */
8204 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8205 						     DPLL_PORTC_READY_MASK |
8206 						     DPLL_PORTB_READY_MASK);
8207 	}
8208 
8209 	if (IS_CHERRYVIEW(dev))
8210 		chv_crtc_clock_get(crtc, pipe_config);
8211 	else if (IS_VALLEYVIEW(dev))
8212 		vlv_crtc_clock_get(crtc, pipe_config);
8213 	else
8214 		i9xx_crtc_clock_get(crtc, pipe_config);
8215 
8216 	/*
8217 	 * Normally the dotclock is filled in by the encoder .get_config()
8218 	 * but in case the pipe is enabled w/o any ports we need a sane
8219 	 * default.
8220 	 */
8221 	pipe_config->base.adjusted_mode.crtc_clock =
8222 		pipe_config->port_clock / pipe_config->pixel_multiplier;
8223 
8224 	return true;
8225 }
8226 
8227 static void ironlake_init_pch_refclk(struct drm_device *dev)
8228 {
8229 	struct drm_i915_private *dev_priv = dev->dev_private;
8230 	struct intel_encoder *encoder;
8231 	u32 val, final;
8232 	bool has_lvds = false;
8233 	bool has_cpu_edp = false;
8234 	bool has_panel = false;
8235 	bool has_ck505 = false;
8236 	bool can_ssc = false;
8237 
8238 	/* We need to take the global config into account */
8239 	for_each_intel_encoder(dev, encoder) {
8240 		switch (encoder->type) {
8241 		case INTEL_OUTPUT_LVDS:
8242 			has_panel = true;
8243 			has_lvds = true;
8244 			break;
8245 		case INTEL_OUTPUT_EDP:
8246 			has_panel = true;
8247 			if (enc_to_dig_port(&encoder->base)->port == PORT_A)
8248 				has_cpu_edp = true;
8249 			break;
8250 		default:
8251 			break;
8252 		}
8253 	}
8254 
8255 	if (HAS_PCH_IBX(dev)) {
8256 		has_ck505 = dev_priv->vbt.display_clock_mode;
8257 		can_ssc = has_ck505;
8258 	} else {
8259 		has_ck505 = false;
8260 		can_ssc = true;
8261 	}
8262 
8263 	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8264 		      has_panel, has_lvds, has_ck505);
8265 
8266 	/* Ironlake: try to setup display ref clock before DPLL
8267 	 * enabling. This is only under driver's control after
8268 	 * PCH B stepping, previous chipset stepping should be
8269 	 * ignoring this setting.
8270 	 */
8271 	val = I915_READ(PCH_DREF_CONTROL);
8272 
8273 	/* As we must carefully and slowly disable/enable each source in turn,
8274 	 * compute the final state we want first and check if we need to
8275 	 * make any changes at all.
8276 	 */
8277 	final = val;
8278 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
8279 	if (has_ck505)
8280 		final |= DREF_NONSPREAD_CK505_ENABLE;
8281 	else
8282 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
8283 
8284 	final &= ~DREF_SSC_SOURCE_MASK;
8285 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8286 	final &= ~DREF_SSC1_ENABLE;
8287 
8288 	if (has_panel) {
8289 		final |= DREF_SSC_SOURCE_ENABLE;
8290 
8291 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
8292 			final |= DREF_SSC1_ENABLE;
8293 
8294 		if (has_cpu_edp) {
8295 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
8296 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8297 			else
8298 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8299 		} else
8300 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8301 	} else {
8302 		final |= DREF_SSC_SOURCE_DISABLE;
8303 		final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8304 	}
8305 
8306 	if (final == val)
8307 		return;
8308 
8309 	/* Always enable nonspread source */
8310 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
8311 
8312 	if (has_ck505)
8313 		val |= DREF_NONSPREAD_CK505_ENABLE;
8314 	else
8315 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
8316 
8317 	if (has_panel) {
8318 		val &= ~DREF_SSC_SOURCE_MASK;
8319 		val |= DREF_SSC_SOURCE_ENABLE;
8320 
8321 		/* SSC must be turned on before enabling the CPU output  */
8322 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8323 			DRM_DEBUG_KMS("Using SSC on panel\n");
8324 			val |= DREF_SSC1_ENABLE;
8325 		} else
8326 			val &= ~DREF_SSC1_ENABLE;
8327 
8328 		/* Get SSC going before enabling the outputs */
8329 		I915_WRITE(PCH_DREF_CONTROL, val);
8330 		POSTING_READ(PCH_DREF_CONTROL);
8331 		udelay(200);
8332 
8333 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8334 
8335 		/* Enable CPU source on CPU attached eDP */
8336 		if (has_cpu_edp) {
8337 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8338 				DRM_DEBUG_KMS("Using SSC on eDP\n");
8339 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8340 			} else
8341 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8342 		} else
8343 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8344 
8345 		I915_WRITE(PCH_DREF_CONTROL, val);
8346 		POSTING_READ(PCH_DREF_CONTROL);
8347 		udelay(200);
8348 	} else {
8349 		DRM_DEBUG_KMS("Disabling SSC entirely\n");
8350 
8351 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8352 
8353 		/* Turn off CPU output */
8354 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8355 
8356 		I915_WRITE(PCH_DREF_CONTROL, val);
8357 		POSTING_READ(PCH_DREF_CONTROL);
8358 		udelay(200);
8359 
8360 		/* Turn off the SSC source */
8361 		val &= ~DREF_SSC_SOURCE_MASK;
8362 		val |= DREF_SSC_SOURCE_DISABLE;
8363 
8364 		/* Turn off SSC1 */
8365 		val &= ~DREF_SSC1_ENABLE;
8366 
8367 		I915_WRITE(PCH_DREF_CONTROL, val);
8368 		POSTING_READ(PCH_DREF_CONTROL);
8369 		udelay(200);
8370 	}
8371 
8372 	BUG_ON(val != final);
8373 }
8374 
8375 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
8376 {
8377 	uint32_t tmp;
8378 
8379 	tmp = I915_READ(SOUTH_CHICKEN2);
8380 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8381 	I915_WRITE(SOUTH_CHICKEN2, tmp);
8382 
8383 	if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8384 			       FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8385 		DRM_ERROR("FDI mPHY reset assert timeout\n");
8386 
8387 	tmp = I915_READ(SOUTH_CHICKEN2);
8388 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8389 	I915_WRITE(SOUTH_CHICKEN2, tmp);
8390 
8391 	if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8392 				FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8393 		DRM_ERROR("FDI mPHY reset de-assert timeout\n");
8394 }
8395 
8396 /* WaMPhyProgramming:hsw */
8397 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8398 {
8399 	uint32_t tmp;
8400 
8401 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8402 	tmp &= ~(0xFF << 24);
8403 	tmp |= (0x12 << 24);
8404 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8405 
8406 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8407 	tmp |= (1 << 11);
8408 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8409 
8410 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8411 	tmp |= (1 << 11);
8412 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8413 
8414 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8415 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8416 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8417 
8418 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8419 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8420 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8421 
8422 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8423 	tmp &= ~(7 << 13);
8424 	tmp |= (5 << 13);
8425 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
8426 
8427 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8428 	tmp &= ~(7 << 13);
8429 	tmp |= (5 << 13);
8430 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
8431 
8432 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8433 	tmp &= ~0xFF;
8434 	tmp |= 0x1C;
8435 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8436 
8437 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8438 	tmp &= ~0xFF;
8439 	tmp |= 0x1C;
8440 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8441 
8442 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8443 	tmp &= ~(0xFF << 16);
8444 	tmp |= (0x1C << 16);
8445 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8446 
8447 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8448 	tmp &= ~(0xFF << 16);
8449 	tmp |= (0x1C << 16);
8450 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8451 
8452 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8453 	tmp |= (1 << 27);
8454 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
8455 
8456 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8457 	tmp |= (1 << 27);
8458 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
8459 
8460 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8461 	tmp &= ~(0xF << 28);
8462 	tmp |= (4 << 28);
8463 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
8464 
8465 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8466 	tmp &= ~(0xF << 28);
8467 	tmp |= (4 << 28);
8468 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
8469 }
8470 
8471 /* Implements 3 different sequences from BSpec chapter "Display iCLK
8472  * Programming" based on the parameters passed:
8473  * - Sequence to enable CLKOUT_DP
8474  * - Sequence to enable CLKOUT_DP without spread
8475  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8476  */
8477 static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8478 				 bool with_fdi)
8479 {
8480 	struct drm_i915_private *dev_priv = dev->dev_private;
8481 	uint32_t reg, tmp;
8482 
8483 	if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8484 		with_spread = true;
8485 	if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
8486 		with_fdi = false;
8487 
8488 	mutex_lock(&dev_priv->sb_lock);
8489 
8490 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8491 	tmp &= ~SBI_SSCCTL_DISABLE;
8492 	tmp |= SBI_SSCCTL_PATHALT;
8493 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8494 
8495 	udelay(24);
8496 
8497 	if (with_spread) {
8498 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8499 		tmp &= ~SBI_SSCCTL_PATHALT;
8500 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8501 
8502 		if (with_fdi) {
8503 			lpt_reset_fdi_mphy(dev_priv);
8504 			lpt_program_fdi_mphy(dev_priv);
8505 		}
8506 	}
8507 
8508 	reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
8509 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8510 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8511 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8512 
8513 	mutex_unlock(&dev_priv->sb_lock);
8514 }
8515 
8516 /* Sequence to disable CLKOUT_DP */
8517 static void lpt_disable_clkout_dp(struct drm_device *dev)
8518 {
8519 	struct drm_i915_private *dev_priv = dev->dev_private;
8520 	uint32_t reg, tmp;
8521 
8522 	mutex_lock(&dev_priv->sb_lock);
8523 
8524 	reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
8525 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8526 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8527 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8528 
8529 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8530 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
8531 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
8532 			tmp |= SBI_SSCCTL_PATHALT;
8533 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8534 			udelay(32);
8535 		}
8536 		tmp |= SBI_SSCCTL_DISABLE;
8537 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8538 	}
8539 
8540 	mutex_unlock(&dev_priv->sb_lock);
8541 }
8542 
8543 static void lpt_init_pch_refclk(struct drm_device *dev)
8544 {
8545 	struct intel_encoder *encoder;
8546 	bool has_vga = false;
8547 
8548 	for_each_intel_encoder(dev, encoder) {
8549 		switch (encoder->type) {
8550 		case INTEL_OUTPUT_ANALOG:
8551 			has_vga = true;
8552 			break;
8553 		default:
8554 			break;
8555 		}
8556 	}
8557 
8558 	if (has_vga)
8559 		lpt_enable_clkout_dp(dev, true, true);
8560 	else
8561 		lpt_disable_clkout_dp(dev);
8562 }
8563 
8564 /*
8565  * Initialize reference clocks when the driver loads
8566  */
8567 void intel_init_pch_refclk(struct drm_device *dev)
8568 {
8569 	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8570 		ironlake_init_pch_refclk(dev);
8571 	else if (HAS_PCH_LPT(dev))
8572 		lpt_init_pch_refclk(dev);
8573 }
8574 
8575 static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
8576 {
8577 	struct drm_device *dev = crtc_state->base.crtc->dev;
8578 	struct drm_i915_private *dev_priv = dev->dev_private;
8579 	struct drm_atomic_state *state = crtc_state->base.state;
8580 	struct drm_connector *connector;
8581 	struct drm_connector_state *connector_state;
8582 	struct intel_encoder *encoder;
8583 	int num_connectors = 0, i;
8584 	bool is_lvds = false;
8585 
8586 	for_each_connector_in_state(state, connector, connector_state, i) {
8587 		if (connector_state->crtc != crtc_state->base.crtc)
8588 			continue;
8589 
8590 		encoder = to_intel_encoder(connector_state->best_encoder);
8591 
8592 		switch (encoder->type) {
8593 		case INTEL_OUTPUT_LVDS:
8594 			is_lvds = true;
8595 			break;
8596 		default:
8597 			break;
8598 		}
8599 		num_connectors++;
8600 	}
8601 
8602 	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
8603 		DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8604 			      dev_priv->vbt.lvds_ssc_freq);
8605 		return dev_priv->vbt.lvds_ssc_freq;
8606 	}
8607 
8608 	return 120000;
8609 }
8610 
8611 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
8612 {
8613 	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8614 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8615 	int pipe = intel_crtc->pipe;
8616 	uint32_t val;
8617 
8618 	val = 0;
8619 
8620 	switch (intel_crtc->config->pipe_bpp) {
8621 	case 18:
8622 		val |= PIPECONF_6BPC;
8623 		break;
8624 	case 24:
8625 		val |= PIPECONF_8BPC;
8626 		break;
8627 	case 30:
8628 		val |= PIPECONF_10BPC;
8629 		break;
8630 	case 36:
8631 		val |= PIPECONF_12BPC;
8632 		break;
8633 	default:
8634 		/* Case prevented by intel_choose_pipe_bpp_dither. */
8635 		BUG();
8636 	}
8637 
8638 	if (intel_crtc->config->dither)
8639 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8640 
8641 	if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
8642 		val |= PIPECONF_INTERLACED_ILK;
8643 	else
8644 		val |= PIPECONF_PROGRESSIVE;
8645 
8646 	if (intel_crtc->config->limited_color_range)
8647 		val |= PIPECONF_COLOR_RANGE_SELECT;
8648 
8649 	I915_WRITE(PIPECONF(pipe), val);
8650 	POSTING_READ(PIPECONF(pipe));
8651 }
8652 
8653 /*
8654  * Set up the pipe CSC unit.
8655  *
8656  * Currently only full range RGB to limited range RGB conversion
8657  * is supported, but eventually this should handle various
8658  * RGB<->YCbCr scenarios as well.
8659  */
8660 static void intel_set_pipe_csc(struct drm_crtc *crtc)
8661 {
8662 	struct drm_device *dev = crtc->dev;
8663 	struct drm_i915_private *dev_priv = dev->dev_private;
8664 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8665 	int pipe = intel_crtc->pipe;
8666 	uint16_t coeff = 0x7800; /* 1.0 */
8667 
8668 	/*
8669 	 * TODO: Check what kind of values actually come out of the pipe
8670 	 * with these coeff/postoff values and adjust to get the best
8671 	 * accuracy. Perhaps we even need to take the bpc value into
8672 	 * consideration.
8673 	 */
8674 
8675 	if (intel_crtc->config->limited_color_range)
8676 		coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8677 
8678 	/*
8679 	 * GY/GU and RY/RU should be the other way around according
8680 	 * to BSpec, but reality doesn't agree. Just set them up in
8681 	 * a way that results in the correct picture.
8682 	 */
8683 	I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8684 	I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8685 
8686 	I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8687 	I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8688 
8689 	I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8690 	I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8691 
8692 	I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8693 	I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8694 	I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8695 
8696 	if (INTEL_INFO(dev)->gen > 6) {
8697 		uint16_t postoff = 0;
8698 
8699 		if (intel_crtc->config->limited_color_range)
8700 			postoff = (16 * (1 << 12) / 255) & 0x1fff;
8701 
8702 		I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8703 		I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8704 		I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8705 
8706 		I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8707 	} else {
8708 		uint32_t mode = CSC_MODE_YUV_TO_RGB;
8709 
8710 		if (intel_crtc->config->limited_color_range)
8711 			mode |= CSC_BLACK_SCREEN_OFFSET;
8712 
8713 		I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8714 	}
8715 }
8716 
8717 static void haswell_set_pipeconf(struct drm_crtc *crtc)
8718 {
8719 	struct drm_device *dev = crtc->dev;
8720 	struct drm_i915_private *dev_priv = dev->dev_private;
8721 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8722 	enum i915_pipe pipe = intel_crtc->pipe;
8723 	enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8724 	uint32_t val;
8725 
8726 	val = 0;
8727 
8728 	if (IS_HASWELL(dev) && intel_crtc->config->dither)
8729 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8730 
8731 	if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
8732 		val |= PIPECONF_INTERLACED_ILK;
8733 	else
8734 		val |= PIPECONF_PROGRESSIVE;
8735 
8736 	I915_WRITE(PIPECONF(cpu_transcoder), val);
8737 	POSTING_READ(PIPECONF(cpu_transcoder));
8738 
8739 	I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8740 	POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
8741 
8742 	if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
8743 		val = 0;
8744 
8745 		switch (intel_crtc->config->pipe_bpp) {
8746 		case 18:
8747 			val |= PIPEMISC_DITHER_6_BPC;
8748 			break;
8749 		case 24:
8750 			val |= PIPEMISC_DITHER_8_BPC;
8751 			break;
8752 		case 30:
8753 			val |= PIPEMISC_DITHER_10_BPC;
8754 			break;
8755 		case 36:
8756 			val |= PIPEMISC_DITHER_12_BPC;
8757 			break;
8758 		default:
8759 			/* Case prevented by pipe_config_set_bpp. */
8760 			BUG();
8761 		}
8762 
8763 		if (intel_crtc->config->dither)
8764 			val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8765 
8766 		I915_WRITE(PIPEMISC(pipe), val);
8767 	}
8768 }
8769 
8770 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
8771 				    struct intel_crtc_state *crtc_state,
8772 				    intel_clock_t *clock,
8773 				    bool *has_reduced_clock,
8774 				    intel_clock_t *reduced_clock)
8775 {
8776 	struct drm_device *dev = crtc->dev;
8777 	struct drm_i915_private *dev_priv = dev->dev_private;
8778 	int refclk;
8779 	const intel_limit_t *limit;
8780 	bool ret;
8781 
8782 	refclk = ironlake_get_refclk(crtc_state);
8783 
8784 	/*
8785 	 * Returns a set of divisors for the desired target clock with the given
8786 	 * refclk, or FALSE.  The returned values represent the clock equation:
8787 	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8788 	 */
8789 	limit = intel_limit(crtc_state, refclk);
8790 	ret = dev_priv->display.find_dpll(limit, crtc_state,
8791 					  crtc_state->port_clock,
8792 					  refclk, NULL, clock);
8793 	if (!ret)
8794 		return false;
8795 
8796 	return true;
8797 }
8798 
8799 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8800 {
8801 	/*
8802 	 * Account for spread spectrum to avoid
8803 	 * oversubscribing the link. Max center spread
8804 	 * is 2.5%; use 5% for safety's sake.
8805 	 */
8806 	u32 bps = target_clock * bpp * 21 / 20;
8807 	return DIV_ROUND_UP(bps, link_bw * 8);
8808 }
8809 
8810 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
8811 {
8812 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
8813 }
8814 
8815 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8816 				      struct intel_crtc_state *crtc_state,
8817 				      u32 *fp,
8818 				      intel_clock_t *reduced_clock, u32 *fp2)
8819 {
8820 	struct drm_crtc *crtc = &intel_crtc->base;
8821 	struct drm_device *dev = crtc->dev;
8822 	struct drm_i915_private *dev_priv = dev->dev_private;
8823 	struct drm_atomic_state *state = crtc_state->base.state;
8824 	struct drm_connector *connector;
8825 	struct drm_connector_state *connector_state;
8826 	struct intel_encoder *encoder;
8827 	uint32_t dpll;
8828 	int factor, num_connectors = 0, i;
8829 	bool is_lvds = false, is_sdvo = false;
8830 
8831 	for_each_connector_in_state(state, connector, connector_state, i) {
8832 		if (connector_state->crtc != crtc_state->base.crtc)
8833 			continue;
8834 
8835 		encoder = to_intel_encoder(connector_state->best_encoder);
8836 
8837 		switch (encoder->type) {
8838 		case INTEL_OUTPUT_LVDS:
8839 			is_lvds = true;
8840 			break;
8841 		case INTEL_OUTPUT_SDVO:
8842 		case INTEL_OUTPUT_HDMI:
8843 			is_sdvo = true;
8844 			break;
8845 		default:
8846 			break;
8847 		}
8848 
8849 		num_connectors++;
8850 	}
8851 
8852 	/* Enable autotuning of the PLL clock (if permissible) */
8853 	factor = 21;
8854 	if (is_lvds) {
8855 		if ((intel_panel_use_ssc(dev_priv) &&
8856 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
8857 		    (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8858 			factor = 25;
8859 	} else if (crtc_state->sdvo_tv_clock)
8860 		factor = 20;
8861 
8862 	if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8863 		*fp |= FP_CB_TUNE;
8864 
8865 	if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8866 		*fp2 |= FP_CB_TUNE;
8867 
8868 	dpll = 0;
8869 
8870 	if (is_lvds)
8871 		dpll |= DPLLB_MODE_LVDS;
8872 	else
8873 		dpll |= DPLLB_MODE_DAC_SERIAL;
8874 
8875 	dpll |= (crtc_state->pixel_multiplier - 1)
8876 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
8877 
8878 	if (is_sdvo)
8879 		dpll |= DPLL_SDVO_HIGH_SPEED;
8880 	if (crtc_state->has_dp_encoder)
8881 		dpll |= DPLL_SDVO_HIGH_SPEED;
8882 
8883 	/* compute bitmask from p1 value */
8884 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8885 	/* also FPA1 */
8886 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8887 
8888 	switch (crtc_state->dpll.p2) {
8889 	case 5:
8890 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8891 		break;
8892 	case 7:
8893 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8894 		break;
8895 	case 10:
8896 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8897 		break;
8898 	case 14:
8899 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8900 		break;
8901 	}
8902 
8903 	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
8904 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8905 	else
8906 		dpll |= PLL_REF_INPUT_DREFCLK;
8907 
8908 	return dpll | DPLL_VCO_ENABLE;
8909 }
8910 
8911 static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8912 				       struct intel_crtc_state *crtc_state)
8913 {
8914 	struct drm_device *dev = crtc->base.dev;
8915 	intel_clock_t clock, reduced_clock;
8916 	u32 dpll = 0, fp = 0, fp2 = 0;
8917 	bool ok, has_reduced_clock = false;
8918 	bool is_lvds = false;
8919 	struct intel_shared_dpll *pll;
8920 
8921 	memset(&crtc_state->dpll_hw_state, 0,
8922 	       sizeof(crtc_state->dpll_hw_state));
8923 
8924 	is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
8925 
8926 	WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8927 	     "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8928 
8929 	ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
8930 				     &has_reduced_clock, &reduced_clock);
8931 	if (!ok && !crtc_state->clock_set) {
8932 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8933 		return -EINVAL;
8934 	}
8935 	/* Compat-code for transition, will disappear. */
8936 	if (!crtc_state->clock_set) {
8937 		crtc_state->dpll.n = clock.n;
8938 		crtc_state->dpll.m1 = clock.m1;
8939 		crtc_state->dpll.m2 = clock.m2;
8940 		crtc_state->dpll.p1 = clock.p1;
8941 		crtc_state->dpll.p2 = clock.p2;
8942 	}
8943 
8944 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8945 	if (crtc_state->has_pch_encoder) {
8946 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8947 		if (has_reduced_clock)
8948 			fp2 = i9xx_dpll_compute_fp(&reduced_clock);
8949 
8950 		dpll = ironlake_compute_dpll(crtc, crtc_state,
8951 					     &fp, &reduced_clock,
8952 					     has_reduced_clock ? &fp2 : NULL);
8953 
8954 		crtc_state->dpll_hw_state.dpll = dpll;
8955 		crtc_state->dpll_hw_state.fp0 = fp;
8956 		if (has_reduced_clock)
8957 			crtc_state->dpll_hw_state.fp1 = fp2;
8958 		else
8959 			crtc_state->dpll_hw_state.fp1 = fp;
8960 
8961 		pll = intel_get_shared_dpll(crtc, crtc_state);
8962 		if (pll == NULL) {
8963 			DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8964 					 pipe_name(crtc->pipe));
8965 			return -EINVAL;
8966 		}
8967 	}
8968 
8969 	if (is_lvds && has_reduced_clock)
8970 		crtc->lowfreq_avail = true;
8971 	else
8972 		crtc->lowfreq_avail = false;
8973 
8974 	return 0;
8975 }
8976 
8977 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8978 					 struct intel_link_m_n *m_n)
8979 {
8980 	struct drm_device *dev = crtc->base.dev;
8981 	struct drm_i915_private *dev_priv = dev->dev_private;
8982 	enum i915_pipe pipe = crtc->pipe;
8983 
8984 	m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8985 	m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8986 	m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8987 		& ~TU_SIZE_MASK;
8988 	m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8989 	m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8990 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8991 }
8992 
8993 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8994 					 enum transcoder transcoder,
8995 					 struct intel_link_m_n *m_n,
8996 					 struct intel_link_m_n *m2_n2)
8997 {
8998 	struct drm_device *dev = crtc->base.dev;
8999 	struct drm_i915_private *dev_priv = dev->dev_private;
9000 	enum i915_pipe pipe = crtc->pipe;
9001 
9002 	if (INTEL_INFO(dev)->gen >= 5) {
9003 		m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9004 		m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9005 		m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9006 			& ~TU_SIZE_MASK;
9007 		m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9008 		m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9009 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9010 		/* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9011 		 * gen < 8) and if DRRS is supported (to make sure the
9012 		 * registers are not unnecessarily read).
9013 		 */
9014 		if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
9015 			crtc->config->has_drrs) {
9016 			m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9017 			m2_n2->link_n =	I915_READ(PIPE_LINK_N2(transcoder));
9018 			m2_n2->gmch_m =	I915_READ(PIPE_DATA_M2(transcoder))
9019 					& ~TU_SIZE_MASK;
9020 			m2_n2->gmch_n =	I915_READ(PIPE_DATA_N2(transcoder));
9021 			m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9022 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9023 		}
9024 	} else {
9025 		m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9026 		m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9027 		m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9028 			& ~TU_SIZE_MASK;
9029 		m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9030 		m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9031 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9032 	}
9033 }
9034 
9035 void intel_dp_get_m_n(struct intel_crtc *crtc,
9036 		      struct intel_crtc_state *pipe_config)
9037 {
9038 	if (pipe_config->has_pch_encoder)
9039 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9040 	else
9041 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9042 					     &pipe_config->dp_m_n,
9043 					     &pipe_config->dp_m2_n2);
9044 }
9045 
9046 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
9047 					struct intel_crtc_state *pipe_config)
9048 {
9049 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9050 				     &pipe_config->fdi_m_n, NULL);
9051 }
9052 
9053 static void skylake_get_pfit_config(struct intel_crtc *crtc,
9054 				    struct intel_crtc_state *pipe_config)
9055 {
9056 	struct drm_device *dev = crtc->base.dev;
9057 	struct drm_i915_private *dev_priv = dev->dev_private;
9058 	struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9059 	uint32_t ps_ctrl = 0;
9060 	int id = -1;
9061 	int i;
9062 
9063 	/* find scaler attached to this pipe */
9064 	for (i = 0; i < crtc->num_scalers; i++) {
9065 		ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9066 		if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9067 			id = i;
9068 			pipe_config->pch_pfit.enabled = true;
9069 			pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9070 			pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9071 			break;
9072 		}
9073 	}
9074 
9075 	scaler_state->scaler_id = id;
9076 	if (id >= 0) {
9077 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9078 	} else {
9079 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
9080 	}
9081 }
9082 
9083 static void
9084 skylake_get_initial_plane_config(struct intel_crtc *crtc,
9085 				 struct intel_initial_plane_config *plane_config)
9086 {
9087 	struct drm_device *dev = crtc->base.dev;
9088 	struct drm_i915_private *dev_priv = dev->dev_private;
9089 	u32 val, base, offset, stride_mult, tiling;
9090 	int pipe = crtc->pipe;
9091 	int fourcc, pixel_format;
9092 	unsigned int aligned_height;
9093 	struct drm_framebuffer *fb;
9094 	struct intel_framebuffer *intel_fb;
9095 
9096 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9097 	if (!intel_fb) {
9098 		DRM_DEBUG_KMS("failed to alloc fb\n");
9099 		return;
9100 	}
9101 
9102 	fb = &intel_fb->base;
9103 
9104 	val = I915_READ(PLANE_CTL(pipe, 0));
9105 	if (!(val & PLANE_CTL_ENABLE))
9106 		goto error;
9107 
9108 	pixel_format = val & PLANE_CTL_FORMAT_MASK;
9109 	fourcc = skl_format_to_fourcc(pixel_format,
9110 				      val & PLANE_CTL_ORDER_RGBX,
9111 				      val & PLANE_CTL_ALPHA_MASK);
9112 	fb->pixel_format = fourcc;
9113 	fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9114 
9115 	tiling = val & PLANE_CTL_TILED_MASK;
9116 	switch (tiling) {
9117 	case PLANE_CTL_TILED_LINEAR:
9118 		fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9119 		break;
9120 	case PLANE_CTL_TILED_X:
9121 		plane_config->tiling = I915_TILING_X;
9122 		fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9123 		break;
9124 	case PLANE_CTL_TILED_Y:
9125 		fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9126 		break;
9127 	case PLANE_CTL_TILED_YF:
9128 		fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9129 		break;
9130 	default:
9131 		MISSING_CASE(tiling);
9132 		goto error;
9133 	}
9134 
9135 	base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9136 	plane_config->base = base;
9137 
9138 	offset = I915_READ(PLANE_OFFSET(pipe, 0));
9139 
9140 	val = I915_READ(PLANE_SIZE(pipe, 0));
9141 	fb->height = ((val >> 16) & 0xfff) + 1;
9142 	fb->width = ((val >> 0) & 0x1fff) + 1;
9143 
9144 	val = I915_READ(PLANE_STRIDE(pipe, 0));
9145 	stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9146 						fb->pixel_format);
9147 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
9148 
9149 	aligned_height = intel_fb_align_height(dev, fb->height,
9150 					       fb->pixel_format,
9151 					       fb->modifier[0]);
9152 
9153 	plane_config->size = fb->pitches[0] * aligned_height;
9154 
9155 	DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9156 		      pipe_name(pipe), fb->width, fb->height,
9157 		      fb->bits_per_pixel, base, fb->pitches[0],
9158 		      plane_config->size);
9159 
9160 	plane_config->fb = intel_fb;
9161 	return;
9162 
9163 error:
9164 	kfree(fb);
9165 }
9166 
9167 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
9168 				     struct intel_crtc_state *pipe_config)
9169 {
9170 	struct drm_device *dev = crtc->base.dev;
9171 	struct drm_i915_private *dev_priv = dev->dev_private;
9172 	uint32_t tmp;
9173 
9174 	tmp = I915_READ(PF_CTL(crtc->pipe));
9175 
9176 	if (tmp & PF_ENABLE) {
9177 		pipe_config->pch_pfit.enabled = true;
9178 		pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9179 		pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
9180 
9181 		/* We currently do not free assignements of panel fitters on
9182 		 * ivb/hsw (since we don't use the higher upscaling modes which
9183 		 * differentiates them) so just WARN about this case for now. */
9184 		if (IS_GEN7(dev)) {
9185 			WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9186 				PF_PIPE_SEL_IVB(crtc->pipe));
9187 		}
9188 	}
9189 }
9190 
9191 static void
9192 ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9193 				  struct intel_initial_plane_config *plane_config)
9194 {
9195 	struct drm_device *dev = crtc->base.dev;
9196 	struct drm_i915_private *dev_priv = dev->dev_private;
9197 	u32 val, base, offset;
9198 	int pipe = crtc->pipe;
9199 	int fourcc, pixel_format;
9200 	unsigned int aligned_height;
9201 	struct drm_framebuffer *fb;
9202 	struct intel_framebuffer *intel_fb;
9203 
9204 	val = I915_READ(DSPCNTR(pipe));
9205 	if (!(val & DISPLAY_PLANE_ENABLE))
9206 		return;
9207 
9208 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9209 	if (!intel_fb) {
9210 		DRM_DEBUG_KMS("failed to alloc fb\n");
9211 		return;
9212 	}
9213 
9214 	fb = &intel_fb->base;
9215 
9216 	if (INTEL_INFO(dev)->gen >= 4) {
9217 		if (val & DISPPLANE_TILED) {
9218 			plane_config->tiling = I915_TILING_X;
9219 			fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9220 		}
9221 	}
9222 
9223 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9224 	fourcc = i9xx_format_to_fourcc(pixel_format);
9225 	fb->pixel_format = fourcc;
9226 	fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9227 
9228 	base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
9229 	if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
9230 		offset = I915_READ(DSPOFFSET(pipe));
9231 	} else {
9232 		if (plane_config->tiling)
9233 			offset = I915_READ(DSPTILEOFF(pipe));
9234 		else
9235 			offset = I915_READ(DSPLINOFF(pipe));
9236 	}
9237 	plane_config->base = base;
9238 
9239 	val = I915_READ(PIPESRC(pipe));
9240 	fb->width = ((val >> 16) & 0xfff) + 1;
9241 	fb->height = ((val >> 0) & 0xfff) + 1;
9242 
9243 	val = I915_READ(DSPSTRIDE(pipe));
9244 	fb->pitches[0] = val & 0xffffffc0;
9245 
9246 	aligned_height = intel_fb_align_height(dev, fb->height,
9247 					       fb->pixel_format,
9248 					       fb->modifier[0]);
9249 
9250 	plane_config->size = fb->pitches[0] * aligned_height;
9251 
9252 	DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9253 		      pipe_name(pipe), fb->width, fb->height,
9254 		      fb->bits_per_pixel, base, fb->pitches[0],
9255 		      plane_config->size);
9256 
9257 	plane_config->fb = intel_fb;
9258 }
9259 
9260 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
9261 				     struct intel_crtc_state *pipe_config)
9262 {
9263 	struct drm_device *dev = crtc->base.dev;
9264 	struct drm_i915_private *dev_priv = dev->dev_private;
9265 	uint32_t tmp;
9266 
9267 	if (!intel_display_power_is_enabled(dev_priv,
9268 					    POWER_DOMAIN_PIPE(crtc->pipe)))
9269 		return false;
9270 
9271 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9272 	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9273 
9274 	tmp = I915_READ(PIPECONF(crtc->pipe));
9275 	if (!(tmp & PIPECONF_ENABLE))
9276 		return false;
9277 
9278 	switch (tmp & PIPECONF_BPC_MASK) {
9279 	case PIPECONF_6BPC:
9280 		pipe_config->pipe_bpp = 18;
9281 		break;
9282 	case PIPECONF_8BPC:
9283 		pipe_config->pipe_bpp = 24;
9284 		break;
9285 	case PIPECONF_10BPC:
9286 		pipe_config->pipe_bpp = 30;
9287 		break;
9288 	case PIPECONF_12BPC:
9289 		pipe_config->pipe_bpp = 36;
9290 		break;
9291 	default:
9292 		break;
9293 	}
9294 
9295 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9296 		pipe_config->limited_color_range = true;
9297 
9298 	if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
9299 		struct intel_shared_dpll *pll;
9300 
9301 		pipe_config->has_pch_encoder = true;
9302 
9303 		tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9304 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9305 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
9306 
9307 		ironlake_get_fdi_m_n_config(crtc, pipe_config);
9308 
9309 		if (HAS_PCH_IBX(dev_priv->dev)) {
9310 			pipe_config->shared_dpll =
9311 				(enum intel_dpll_id) crtc->pipe;
9312 		} else {
9313 			tmp = I915_READ(PCH_DPLL_SEL);
9314 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9315 				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9316 			else
9317 				pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9318 		}
9319 
9320 		pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9321 
9322 		WARN_ON(!pll->get_hw_state(dev_priv, pll,
9323 					   &pipe_config->dpll_hw_state));
9324 
9325 		tmp = pipe_config->dpll_hw_state.dpll;
9326 		pipe_config->pixel_multiplier =
9327 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9328 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
9329 
9330 		ironlake_pch_clock_get(crtc, pipe_config);
9331 	} else {
9332 		pipe_config->pixel_multiplier = 1;
9333 	}
9334 
9335 	intel_get_pipe_timings(crtc, pipe_config);
9336 
9337 	ironlake_get_pfit_config(crtc, pipe_config);
9338 
9339 	return true;
9340 }
9341 
9342 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9343 {
9344 	struct drm_device *dev = dev_priv->dev;
9345 	struct intel_crtc *crtc;
9346 
9347 	for_each_intel_crtc(dev, crtc)
9348 		I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
9349 		     pipe_name(crtc->pipe));
9350 
9351 	I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9352 	I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9353 	I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9354 	I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9355 	I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9356 	I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
9357 	     "CPU PWM1 enabled\n");
9358 	if (IS_HASWELL(dev))
9359 		I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
9360 		     "CPU PWM2 enabled\n");
9361 	I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
9362 	     "PCH PWM1 enabled\n");
9363 	I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
9364 	     "Utility pin enabled\n");
9365 	I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
9366 
9367 	/*
9368 	 * In theory we can still leave IRQs enabled, as long as only the HPD
9369 	 * interrupts remain enabled. We used to check for that, but since it's
9370 	 * gen-specific and since we only disable LCPLL after we fully disable
9371 	 * the interrupts, the check below should be enough.
9372 	 */
9373 	I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
9374 }
9375 
9376 static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9377 {
9378 	struct drm_device *dev = dev_priv->dev;
9379 
9380 	if (IS_HASWELL(dev))
9381 		return I915_READ(D_COMP_HSW);
9382 	else
9383 		return I915_READ(D_COMP_BDW);
9384 }
9385 
9386 static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9387 {
9388 	struct drm_device *dev = dev_priv->dev;
9389 
9390 	if (IS_HASWELL(dev)) {
9391 		mutex_lock(&dev_priv->rps.hw_lock);
9392 		if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9393 					    val))
9394 			DRM_ERROR("Failed to write to D_COMP\n");
9395 		mutex_unlock(&dev_priv->rps.hw_lock);
9396 	} else {
9397 		I915_WRITE(D_COMP_BDW, val);
9398 		POSTING_READ(D_COMP_BDW);
9399 	}
9400 }
9401 
9402 /*
9403  * This function implements pieces of two sequences from BSpec:
9404  * - Sequence for display software to disable LCPLL
9405  * - Sequence for display software to allow package C8+
9406  * The steps implemented here are just the steps that actually touch the LCPLL
9407  * register. Callers should take care of disabling all the display engine
9408  * functions, doing the mode unset, fixing interrupts, etc.
9409  */
9410 static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9411 			      bool switch_to_fclk, bool allow_power_down)
9412 {
9413 	uint32_t val;
9414 
9415 	assert_can_disable_lcpll(dev_priv);
9416 
9417 	val = I915_READ(LCPLL_CTL);
9418 
9419 	if (switch_to_fclk) {
9420 		val |= LCPLL_CD_SOURCE_FCLK;
9421 		I915_WRITE(LCPLL_CTL, val);
9422 
9423 		if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9424 				       LCPLL_CD_SOURCE_FCLK_DONE, 1))
9425 			DRM_ERROR("Switching to FCLK failed\n");
9426 
9427 		val = I915_READ(LCPLL_CTL);
9428 	}
9429 
9430 	val |= LCPLL_PLL_DISABLE;
9431 	I915_WRITE(LCPLL_CTL, val);
9432 	POSTING_READ(LCPLL_CTL);
9433 
9434 	if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9435 		DRM_ERROR("LCPLL still locked\n");
9436 
9437 	val = hsw_read_dcomp(dev_priv);
9438 	val |= D_COMP_COMP_DISABLE;
9439 	hsw_write_dcomp(dev_priv, val);
9440 	ndelay(100);
9441 
9442 	if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9443 		     1))
9444 		DRM_ERROR("D_COMP RCOMP still in progress\n");
9445 
9446 	if (allow_power_down) {
9447 		val = I915_READ(LCPLL_CTL);
9448 		val |= LCPLL_POWER_DOWN_ALLOW;
9449 		I915_WRITE(LCPLL_CTL, val);
9450 		POSTING_READ(LCPLL_CTL);
9451 	}
9452 }
9453 
9454 /*
9455  * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9456  * source.
9457  */
9458 static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
9459 {
9460 	uint32_t val;
9461 
9462 	val = I915_READ(LCPLL_CTL);
9463 
9464 	if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9465 		    LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9466 		return;
9467 
9468 	/*
9469 	 * Make sure we're not on PC8 state before disabling PC8, otherwise
9470 	 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
9471 	 */
9472 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
9473 
9474 	if (val & LCPLL_POWER_DOWN_ALLOW) {
9475 		val &= ~LCPLL_POWER_DOWN_ALLOW;
9476 		I915_WRITE(LCPLL_CTL, val);
9477 		POSTING_READ(LCPLL_CTL);
9478 	}
9479 
9480 	val = hsw_read_dcomp(dev_priv);
9481 	val |= D_COMP_COMP_FORCE;
9482 	val &= ~D_COMP_COMP_DISABLE;
9483 	hsw_write_dcomp(dev_priv, val);
9484 
9485 	val = I915_READ(LCPLL_CTL);
9486 	val &= ~LCPLL_PLL_DISABLE;
9487 	I915_WRITE(LCPLL_CTL, val);
9488 
9489 	if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9490 		DRM_ERROR("LCPLL not locked yet\n");
9491 
9492 	if (val & LCPLL_CD_SOURCE_FCLK) {
9493 		val = I915_READ(LCPLL_CTL);
9494 		val &= ~LCPLL_CD_SOURCE_FCLK;
9495 		I915_WRITE(LCPLL_CTL, val);
9496 
9497 		if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9498 					LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9499 			DRM_ERROR("Switching back to LCPLL failed\n");
9500 	}
9501 
9502 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
9503 	intel_update_cdclk(dev_priv->dev);
9504 }
9505 
9506 /*
9507  * Package states C8 and deeper are really deep PC states that can only be
9508  * reached when all the devices on the system allow it, so even if the graphics
9509  * device allows PC8+, it doesn't mean the system will actually get to these
9510  * states. Our driver only allows PC8+ when going into runtime PM.
9511  *
9512  * The requirements for PC8+ are that all the outputs are disabled, the power
9513  * well is disabled and most interrupts are disabled, and these are also
9514  * requirements for runtime PM. When these conditions are met, we manually do
9515  * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9516  * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9517  * hang the machine.
9518  *
9519  * When we really reach PC8 or deeper states (not just when we allow it) we lose
9520  * the state of some registers, so when we come back from PC8+ we need to
9521  * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9522  * need to take care of the registers kept by RC6. Notice that this happens even
9523  * if we don't put the device in PCI D3 state (which is what currently happens
9524  * because of the runtime PM support).
9525  *
9526  * For more, read "Display Sequences for Package C8" on the hardware
9527  * documentation.
9528  */
9529 void hsw_enable_pc8(struct drm_i915_private *dev_priv)
9530 {
9531 	struct drm_device *dev = dev_priv->dev;
9532 	uint32_t val;
9533 
9534 	DRM_DEBUG_KMS("Enabling package C8+\n");
9535 
9536 	if (HAS_PCH_LPT_LP(dev)) {
9537 		val = I915_READ(SOUTH_DSPCLK_GATE_D);
9538 		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9539 		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9540 	}
9541 
9542 	lpt_disable_clkout_dp(dev);
9543 	hsw_disable_lcpll(dev_priv, true, true);
9544 }
9545 
9546 void hsw_disable_pc8(struct drm_i915_private *dev_priv)
9547 {
9548 	struct drm_device *dev = dev_priv->dev;
9549 	uint32_t val;
9550 
9551 	DRM_DEBUG_KMS("Disabling package C8+\n");
9552 
9553 	hsw_restore_lcpll(dev_priv);
9554 	lpt_init_pch_refclk(dev);
9555 
9556 	if (HAS_PCH_LPT_LP(dev)) {
9557 		val = I915_READ(SOUTH_DSPCLK_GATE_D);
9558 		val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9559 		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9560 	}
9561 
9562 	intel_prepare_ddi(dev);
9563 }
9564 
9565 static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9566 {
9567 	struct drm_device *dev = old_state->dev;
9568 	unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
9569 
9570 	broxton_set_cdclk(dev, req_cdclk);
9571 }
9572 
9573 /* compute the max rate for new configuration */
9574 static int ilk_max_pixel_rate(struct drm_atomic_state *state)
9575 {
9576 	struct intel_crtc *intel_crtc;
9577 	struct intel_crtc_state *crtc_state;
9578 	int max_pixel_rate = 0;
9579 
9580 	for_each_intel_crtc(state->dev, intel_crtc) {
9581 		int pixel_rate;
9582 
9583 		crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9584 		if (IS_ERR(crtc_state))
9585 			return PTR_ERR(crtc_state);
9586 
9587 		if (!crtc_state->base.enable)
9588 			continue;
9589 
9590 		pixel_rate = ilk_pipe_pixel_rate(crtc_state);
9591 
9592 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9593 		if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
9594 			pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9595 
9596 		max_pixel_rate = max(max_pixel_rate, pixel_rate);
9597 	}
9598 
9599 	return max_pixel_rate;
9600 }
9601 
9602 static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9603 {
9604 	struct drm_i915_private *dev_priv = dev->dev_private;
9605 	uint32_t val, data;
9606 	int ret;
9607 
9608 	if (WARN((I915_READ(LCPLL_CTL) &
9609 		  (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9610 		   LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9611 		   LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9612 		   LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9613 		 "trying to change cdclk frequency with cdclk not enabled\n"))
9614 		return;
9615 
9616 	mutex_lock(&dev_priv->rps.hw_lock);
9617 	ret = sandybridge_pcode_write(dev_priv,
9618 				      BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9619 	mutex_unlock(&dev_priv->rps.hw_lock);
9620 	if (ret) {
9621 		DRM_ERROR("failed to inform pcode about cdclk change\n");
9622 		return;
9623 	}
9624 
9625 	val = I915_READ(LCPLL_CTL);
9626 	val |= LCPLL_CD_SOURCE_FCLK;
9627 	I915_WRITE(LCPLL_CTL, val);
9628 
9629 	if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9630 			       LCPLL_CD_SOURCE_FCLK_DONE, 1))
9631 		DRM_ERROR("Switching to FCLK failed\n");
9632 
9633 	val = I915_READ(LCPLL_CTL);
9634 	val &= ~LCPLL_CLK_FREQ_MASK;
9635 
9636 	switch (cdclk) {
9637 	case 450000:
9638 		val |= LCPLL_CLK_FREQ_450;
9639 		data = 0;
9640 		break;
9641 	case 540000:
9642 		val |= LCPLL_CLK_FREQ_54O_BDW;
9643 		data = 1;
9644 		break;
9645 	case 337500:
9646 		val |= LCPLL_CLK_FREQ_337_5_BDW;
9647 		data = 2;
9648 		break;
9649 	case 675000:
9650 		val |= LCPLL_CLK_FREQ_675_BDW;
9651 		data = 3;
9652 		break;
9653 	default:
9654 		WARN(1, "invalid cdclk frequency\n");
9655 		return;
9656 	}
9657 
9658 	I915_WRITE(LCPLL_CTL, val);
9659 
9660 	val = I915_READ(LCPLL_CTL);
9661 	val &= ~LCPLL_CD_SOURCE_FCLK;
9662 	I915_WRITE(LCPLL_CTL, val);
9663 
9664 	if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9665 				LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9666 		DRM_ERROR("Switching back to LCPLL failed\n");
9667 
9668 	mutex_lock(&dev_priv->rps.hw_lock);
9669 	sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9670 	mutex_unlock(&dev_priv->rps.hw_lock);
9671 
9672 	intel_update_cdclk(dev);
9673 
9674 	WARN(cdclk != dev_priv->cdclk_freq,
9675 	     "cdclk requested %d kHz but got %d kHz\n",
9676 	     cdclk, dev_priv->cdclk_freq);
9677 }
9678 
9679 static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
9680 {
9681 	struct drm_i915_private *dev_priv = to_i915(state->dev);
9682 	int max_pixclk = ilk_max_pixel_rate(state);
9683 	int cdclk;
9684 
9685 	/*
9686 	 * FIXME should also account for plane ratio
9687 	 * once 64bpp pixel formats are supported.
9688 	 */
9689 	if (max_pixclk > 540000)
9690 		cdclk = 675000;
9691 	else if (max_pixclk > 450000)
9692 		cdclk = 540000;
9693 	else if (max_pixclk > 337500)
9694 		cdclk = 450000;
9695 	else
9696 		cdclk = 337500;
9697 
9698 	/*
9699 	 * FIXME move the cdclk caclulation to
9700 	 * compute_config() so we can fail gracegully.
9701 	 */
9702 	if (cdclk > dev_priv->max_cdclk_freq) {
9703 		DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9704 			  cdclk, dev_priv->max_cdclk_freq);
9705 		cdclk = dev_priv->max_cdclk_freq;
9706 	}
9707 
9708 	to_intel_atomic_state(state)->cdclk = cdclk;
9709 
9710 	return 0;
9711 }
9712 
9713 static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
9714 {
9715 	struct drm_device *dev = old_state->dev;
9716 	unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
9717 
9718 	broadwell_set_cdclk(dev, req_cdclk);
9719 }
9720 
9721 static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9722 				      struct intel_crtc_state *crtc_state)
9723 {
9724 	if (!intel_ddi_pll_select(crtc, crtc_state))
9725 		return -EINVAL;
9726 
9727 	crtc->lowfreq_avail = false;
9728 
9729 	return 0;
9730 }
9731 
9732 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9733 				enum port port,
9734 				struct intel_crtc_state *pipe_config)
9735 {
9736 	switch (port) {
9737 	case PORT_A:
9738 		pipe_config->ddi_pll_sel = SKL_DPLL0;
9739 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9740 		break;
9741 	case PORT_B:
9742 		pipe_config->ddi_pll_sel = SKL_DPLL1;
9743 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9744 		break;
9745 	case PORT_C:
9746 		pipe_config->ddi_pll_sel = SKL_DPLL2;
9747 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9748 		break;
9749 	default:
9750 		DRM_ERROR("Incorrect port type\n");
9751 	}
9752 }
9753 
9754 static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9755 				enum port port,
9756 				struct intel_crtc_state *pipe_config)
9757 {
9758 	u32 temp, dpll_ctl1;
9759 
9760 	temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9761 	pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9762 
9763 	switch (pipe_config->ddi_pll_sel) {
9764 	case SKL_DPLL0:
9765 		/*
9766 		 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9767 		 * of the shared DPLL framework and thus needs to be read out
9768 		 * separately
9769 		 */
9770 		dpll_ctl1 = I915_READ(DPLL_CTRL1);
9771 		pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9772 		break;
9773 	case SKL_DPLL1:
9774 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9775 		break;
9776 	case SKL_DPLL2:
9777 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9778 		break;
9779 	case SKL_DPLL3:
9780 		pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9781 		break;
9782 	}
9783 }
9784 
9785 static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9786 				enum port port,
9787 				struct intel_crtc_state *pipe_config)
9788 {
9789 	pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9790 
9791 	switch (pipe_config->ddi_pll_sel) {
9792 	case PORT_CLK_SEL_WRPLL1:
9793 		pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9794 		break;
9795 	case PORT_CLK_SEL_WRPLL2:
9796 		pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9797 		break;
9798 	case PORT_CLK_SEL_SPLL:
9799 		pipe_config->shared_dpll = DPLL_ID_SPLL;
9800 	}
9801 }
9802 
9803 static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
9804 				       struct intel_crtc_state *pipe_config)
9805 {
9806 	struct drm_device *dev = crtc->base.dev;
9807 	struct drm_i915_private *dev_priv = dev->dev_private;
9808 	struct intel_shared_dpll *pll;
9809 	enum port port;
9810 	uint32_t tmp;
9811 
9812 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9813 
9814 	port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9815 
9816 	if (IS_SKYLAKE(dev))
9817 		skylake_get_ddi_pll(dev_priv, port, pipe_config);
9818 	else if (IS_BROXTON(dev))
9819 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
9820 	else
9821 		haswell_get_ddi_pll(dev_priv, port, pipe_config);
9822 
9823 	if (pipe_config->shared_dpll >= 0) {
9824 		pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9825 
9826 		WARN_ON(!pll->get_hw_state(dev_priv, pll,
9827 					   &pipe_config->dpll_hw_state));
9828 	}
9829 
9830 	/*
9831 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9832 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
9833 	 * the PCH transcoder is on.
9834 	 */
9835 	if (INTEL_INFO(dev)->gen < 9 &&
9836 	    (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
9837 		pipe_config->has_pch_encoder = true;
9838 
9839 		tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9840 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9841 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
9842 
9843 		ironlake_get_fdi_m_n_config(crtc, pipe_config);
9844 	}
9845 }
9846 
9847 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
9848 				    struct intel_crtc_state *pipe_config)
9849 {
9850 	struct drm_device *dev = crtc->base.dev;
9851 	struct drm_i915_private *dev_priv = dev->dev_private;
9852 	enum intel_display_power_domain pfit_domain;
9853 	uint32_t tmp;
9854 
9855 	if (!intel_display_power_is_enabled(dev_priv,
9856 					 POWER_DOMAIN_PIPE(crtc->pipe)))
9857 		return false;
9858 
9859 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9860 	pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9861 
9862 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9863 	if (tmp & TRANS_DDI_FUNC_ENABLE) {
9864 		enum i915_pipe trans_edp_pipe;
9865 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9866 		default:
9867 			WARN(1, "unknown pipe linked to edp transcoder\n");
9868 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
9869 		case TRANS_DDI_EDP_INPUT_A_ON:
9870 			trans_edp_pipe = PIPE_A;
9871 			break;
9872 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
9873 			trans_edp_pipe = PIPE_B;
9874 			break;
9875 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
9876 			trans_edp_pipe = PIPE_C;
9877 			break;
9878 		}
9879 
9880 		if (trans_edp_pipe == crtc->pipe)
9881 			pipe_config->cpu_transcoder = TRANSCODER_EDP;
9882 	}
9883 
9884 	if (!intel_display_power_is_enabled(dev_priv,
9885 			POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
9886 		return false;
9887 
9888 	tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9889 	if (!(tmp & PIPECONF_ENABLE))
9890 		return false;
9891 
9892 	haswell_get_ddi_port_state(crtc, pipe_config);
9893 
9894 	intel_get_pipe_timings(crtc, pipe_config);
9895 
9896 	if (INTEL_INFO(dev)->gen >= 9) {
9897 		skl_init_scalers(dev, crtc, pipe_config);
9898 	}
9899 
9900 	pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9901 
9902 	if (INTEL_INFO(dev)->gen >= 9) {
9903 		pipe_config->scaler_state.scaler_id = -1;
9904 		pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9905 	}
9906 
9907 	if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
9908 		if (INTEL_INFO(dev)->gen >= 9)
9909 			skylake_get_pfit_config(crtc, pipe_config);
9910 		else
9911 			ironlake_get_pfit_config(crtc, pipe_config);
9912 	}
9913 
9914 	if (IS_HASWELL(dev))
9915 		pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9916 			(I915_READ(IPS_CTL) & IPS_ENABLE);
9917 
9918 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9919 		pipe_config->pixel_multiplier =
9920 			I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9921 	} else {
9922 		pipe_config->pixel_multiplier = 1;
9923 	}
9924 
9925 	return true;
9926 }
9927 
9928 static void i845_update_cursor(struct drm_crtc *crtc, u32 base, bool on)
9929 {
9930 	struct drm_device *dev = crtc->dev;
9931 	struct drm_i915_private *dev_priv = dev->dev_private;
9932 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9933 	uint32_t cntl = 0, size = 0;
9934 
9935 	if (on) {
9936 		unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9937 		unsigned int height = intel_crtc->base.cursor->state->crtc_h;
9938 		unsigned int stride = roundup_pow_of_two(width) * 4;
9939 
9940 		switch (stride) {
9941 		default:
9942 			WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9943 				  width, stride);
9944 			stride = 256;
9945 			/* fallthrough */
9946 		case 256:
9947 		case 512:
9948 		case 1024:
9949 		case 2048:
9950 			break;
9951 		}
9952 
9953 		cntl |= CURSOR_ENABLE |
9954 			CURSOR_GAMMA_ENABLE |
9955 			CURSOR_FORMAT_ARGB |
9956 			CURSOR_STRIDE(stride);
9957 
9958 		size = (height << 12) | width;
9959 	}
9960 
9961 	if (intel_crtc->cursor_cntl != 0 &&
9962 	    (intel_crtc->cursor_base != base ||
9963 	     intel_crtc->cursor_size != size ||
9964 	     intel_crtc->cursor_cntl != cntl)) {
9965 		/* On these chipsets we can only modify the base/size/stride
9966 		 * whilst the cursor is disabled.
9967 		 */
9968 		I915_WRITE(CURCNTR(PIPE_A), 0);
9969 		POSTING_READ(CURCNTR(PIPE_A));
9970 		intel_crtc->cursor_cntl = 0;
9971 	}
9972 
9973 	if (intel_crtc->cursor_base != base) {
9974 		I915_WRITE(CURBASE(PIPE_A), base);
9975 		intel_crtc->cursor_base = base;
9976 	}
9977 
9978 	if (intel_crtc->cursor_size != size) {
9979 		I915_WRITE(CURSIZE, size);
9980 		intel_crtc->cursor_size = size;
9981 	}
9982 
9983 	if (intel_crtc->cursor_cntl != cntl) {
9984 		I915_WRITE(CURCNTR(PIPE_A), cntl);
9985 		POSTING_READ(CURCNTR(PIPE_A));
9986 		intel_crtc->cursor_cntl = cntl;
9987 	}
9988 }
9989 
9990 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base, bool on)
9991 {
9992 	struct drm_device *dev = crtc->dev;
9993 	struct drm_i915_private *dev_priv = dev->dev_private;
9994 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9995 	int pipe = intel_crtc->pipe;
9996 	uint32_t cntl = 0;
9997 
9998 	if (on) {
9999 		cntl = MCURSOR_GAMMA_ENABLE;
10000 		switch (intel_crtc->base.cursor->state->crtc_w) {
10001 			case 64:
10002 				cntl |= CURSOR_MODE_64_ARGB_AX;
10003 				break;
10004 			case 128:
10005 				cntl |= CURSOR_MODE_128_ARGB_AX;
10006 				break;
10007 			case 256:
10008 				cntl |= CURSOR_MODE_256_ARGB_AX;
10009 				break;
10010 			default:
10011 				MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
10012 				return;
10013 		}
10014 		cntl |= pipe << 28; /* Connect to correct pipe */
10015 
10016 		if (HAS_DDI(dev))
10017 			cntl |= CURSOR_PIPE_CSC_ENABLE;
10018 	}
10019 
10020 	if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
10021 		cntl |= CURSOR_ROTATE_180;
10022 
10023 	if (intel_crtc->cursor_cntl != cntl) {
10024 		I915_WRITE(CURCNTR(pipe), cntl);
10025 		POSTING_READ(CURCNTR(pipe));
10026 		intel_crtc->cursor_cntl = cntl;
10027 	}
10028 
10029 	/* and commit changes on next vblank */
10030 	I915_WRITE(CURBASE(pipe), base);
10031 	POSTING_READ(CURBASE(pipe));
10032 
10033 	intel_crtc->cursor_base = base;
10034 }
10035 
10036 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
10037 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10038 				     bool on)
10039 {
10040 	struct drm_device *dev = crtc->dev;
10041 	struct drm_i915_private *dev_priv = dev->dev_private;
10042 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10043 	int pipe = intel_crtc->pipe;
10044 	struct drm_plane_state *cursor_state = crtc->cursor->state;
10045 	int x = cursor_state->crtc_x;
10046 	int y = cursor_state->crtc_y;
10047 	u32 base = 0, pos = 0;
10048 
10049 	base = intel_crtc->cursor_addr;
10050 
10051 	if (x >= intel_crtc->config->pipe_src_w)
10052 		on = false;
10053 
10054 	if (y >= intel_crtc->config->pipe_src_h)
10055 		on = false;
10056 
10057 	if (x < 0) {
10058 		if (x + cursor_state->crtc_w <= 0)
10059 			on = false;
10060 
10061 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10062 		x = -x;
10063 	}
10064 	pos |= x << CURSOR_X_SHIFT;
10065 
10066 	if (y < 0) {
10067 		if (y + cursor_state->crtc_h <= 0)
10068 			on = false;
10069 
10070 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10071 		y = -y;
10072 	}
10073 	pos |= y << CURSOR_Y_SHIFT;
10074 
10075 	I915_WRITE(CURPOS(pipe), pos);
10076 
10077 	/* ILK+ do this automagically */
10078 	if (HAS_GMCH_DISPLAY(dev) &&
10079 	    crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
10080 		base += (cursor_state->crtc_h *
10081 			 cursor_state->crtc_w - 1) * 4;
10082 	}
10083 
10084 	if (IS_845G(dev) || IS_I865G(dev))
10085 		i845_update_cursor(crtc, base, on);
10086 	else
10087 		i9xx_update_cursor(crtc, base, on);
10088 }
10089 
10090 static bool cursor_size_ok(struct drm_device *dev,
10091 			   uint32_t width, uint32_t height)
10092 {
10093 	if (width == 0 || height == 0)
10094 		return false;
10095 
10096 	/*
10097 	 * 845g/865g are special in that they are only limited by
10098 	 * the width of their cursors, the height is arbitrary up to
10099 	 * the precision of the register. Everything else requires
10100 	 * square cursors, limited to a few power-of-two sizes.
10101 	 */
10102 	if (IS_845G(dev) || IS_I865G(dev)) {
10103 		if ((width & 63) != 0)
10104 			return false;
10105 
10106 		if (width > (IS_845G(dev) ? 64 : 512))
10107 			return false;
10108 
10109 		if (height > 1023)
10110 			return false;
10111 	} else {
10112 		switch (width | height) {
10113 		case 256:
10114 		case 128:
10115 			if (IS_GEN2(dev))
10116 				return false;
10117 		case 64:
10118 			break;
10119 		default:
10120 			return false;
10121 		}
10122 	}
10123 
10124 	return true;
10125 }
10126 
10127 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
10128 				 u16 *blue, uint32_t start, uint32_t size)
10129 {
10130 	int end = (start + size > 256) ? 256 : start + size, i;
10131 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10132 
10133 	for (i = start; i < end; i++) {
10134 		intel_crtc->lut_r[i] = red[i] >> 8;
10135 		intel_crtc->lut_g[i] = green[i] >> 8;
10136 		intel_crtc->lut_b[i] = blue[i] >> 8;
10137 	}
10138 
10139 	intel_crtc_load_lut(crtc);
10140 }
10141 
10142 /* VESA 640x480x72Hz mode to set on the pipe */
10143 static struct drm_display_mode load_detect_mode = {
10144 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10145 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10146 };
10147 
10148 struct drm_framebuffer *
10149 __intel_framebuffer_create(struct drm_device *dev,
10150 			   struct drm_mode_fb_cmd2 *mode_cmd,
10151 			   struct drm_i915_gem_object *obj)
10152 {
10153 	struct intel_framebuffer *intel_fb;
10154 	int ret;
10155 
10156 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10157 	if (!intel_fb) {
10158 		drm_gem_object_unreference(&obj->base);
10159 		return ERR_PTR(-ENOMEM);
10160 	}
10161 
10162 	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
10163 	if (ret)
10164 		goto err;
10165 
10166 	return &intel_fb->base;
10167 err:
10168 	drm_gem_object_unreference(&obj->base);
10169 	kfree(intel_fb);
10170 
10171 	return ERR_PTR(ret);
10172 }
10173 
10174 static struct drm_framebuffer *
10175 intel_framebuffer_create(struct drm_device *dev,
10176 			 struct drm_mode_fb_cmd2 *mode_cmd,
10177 			 struct drm_i915_gem_object *obj)
10178 {
10179 	struct drm_framebuffer *fb;
10180 	int ret;
10181 
10182 	ret = i915_mutex_lock_interruptible(dev);
10183 	if (ret)
10184 		return ERR_PTR(ret);
10185 	fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10186 	mutex_unlock(&dev->struct_mutex);
10187 
10188 	return fb;
10189 }
10190 
10191 static u32
10192 intel_framebuffer_pitch_for_width(int width, int bpp)
10193 {
10194 	u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10195 	return ALIGN(pitch, 64);
10196 }
10197 
10198 static u32
10199 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10200 {
10201 	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
10202 	return PAGE_ALIGN(pitch * mode->vdisplay);
10203 }
10204 
10205 static struct drm_framebuffer *
10206 intel_framebuffer_create_for_mode(struct drm_device *dev,
10207 				  struct drm_display_mode *mode,
10208 				  int depth, int bpp)
10209 {
10210 	struct drm_i915_gem_object *obj;
10211 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
10212 
10213 	obj = i915_gem_alloc_object(dev,
10214 				    intel_framebuffer_size_for_mode(mode, bpp));
10215 	if (obj == NULL)
10216 		return ERR_PTR(-ENOMEM);
10217 
10218 	mode_cmd.width = mode->hdisplay;
10219 	mode_cmd.height = mode->vdisplay;
10220 	mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10221 								bpp);
10222 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
10223 
10224 	return intel_framebuffer_create(dev, &mode_cmd, obj);
10225 }
10226 
10227 static struct drm_framebuffer *
10228 mode_fits_in_fbdev(struct drm_device *dev,
10229 		   struct drm_display_mode *mode)
10230 {
10231 #ifdef CONFIG_DRM_FBDEV_EMULATION
10232 	struct drm_i915_private *dev_priv = dev->dev_private;
10233 	struct drm_i915_gem_object *obj;
10234 	struct drm_framebuffer *fb;
10235 
10236 	if (!dev_priv->fbdev)
10237 		return NULL;
10238 
10239 	if (!dev_priv->fbdev->fb)
10240 		return NULL;
10241 
10242 	obj = dev_priv->fbdev->fb->obj;
10243 	BUG_ON(!obj);
10244 
10245 	fb = &dev_priv->fbdev->fb->base;
10246 	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10247 							       fb->bits_per_pixel))
10248 		return NULL;
10249 
10250 	if (obj->base.size < mode->vdisplay * fb->pitches[0])
10251 		return NULL;
10252 
10253 	return fb;
10254 #else
10255 	return NULL;
10256 #endif
10257 }
10258 
10259 static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10260 					   struct drm_crtc *crtc,
10261 					   struct drm_display_mode *mode,
10262 					   struct drm_framebuffer *fb,
10263 					   int x, int y)
10264 {
10265 	struct drm_plane_state *plane_state;
10266 	int hdisplay, vdisplay;
10267 	int ret;
10268 
10269 	plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10270 	if (IS_ERR(plane_state))
10271 		return PTR_ERR(plane_state);
10272 
10273 	if (mode)
10274 		drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10275 	else
10276 		hdisplay = vdisplay = 0;
10277 
10278 	ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10279 	if (ret)
10280 		return ret;
10281 	drm_atomic_set_fb_for_plane(plane_state, fb);
10282 	plane_state->crtc_x = 0;
10283 	plane_state->crtc_y = 0;
10284 	plane_state->crtc_w = hdisplay;
10285 	plane_state->crtc_h = vdisplay;
10286 	plane_state->src_x = x << 16;
10287 	plane_state->src_y = y << 16;
10288 	plane_state->src_w = hdisplay << 16;
10289 	plane_state->src_h = vdisplay << 16;
10290 
10291 	return 0;
10292 }
10293 
10294 bool intel_get_load_detect_pipe(struct drm_connector *connector,
10295 				struct drm_display_mode *mode,
10296 				struct intel_load_detect_pipe *old,
10297 				struct drm_modeset_acquire_ctx *ctx)
10298 {
10299 	struct intel_crtc *intel_crtc;
10300 	struct intel_encoder *intel_encoder =
10301 		intel_attached_encoder(connector);
10302 	struct drm_crtc *possible_crtc;
10303 	struct drm_encoder *encoder = &intel_encoder->base;
10304 	struct drm_crtc *crtc = NULL;
10305 	struct drm_device *dev = encoder->dev;
10306 	struct drm_framebuffer *fb;
10307 	struct drm_mode_config *config = &dev->mode_config;
10308 	struct drm_atomic_state *state = NULL;
10309 	struct drm_connector_state *connector_state;
10310 	struct intel_crtc_state *crtc_state;
10311 	int ret, i = -1;
10312 
10313 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
10314 		      connector->base.id, connector->name,
10315 		      encoder->base.id, encoder->name);
10316 
10317 retry:
10318 	ret = drm_modeset_lock(&config->connection_mutex, ctx);
10319 	if (ret)
10320 		goto fail;
10321 
10322 	/*
10323 	 * Algorithm gets a little messy:
10324 	 *
10325 	 *   - if the connector already has an assigned crtc, use it (but make
10326 	 *     sure it's on first)
10327 	 *
10328 	 *   - try to find the first unused crtc that can drive this connector,
10329 	 *     and use that if we find one
10330 	 */
10331 
10332 	/* See if we already have a CRTC for this connector */
10333 	if (encoder->crtc) {
10334 		crtc = encoder->crtc;
10335 
10336 		ret = drm_modeset_lock(&crtc->mutex, ctx);
10337 		if (ret)
10338 			goto fail;
10339 		ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10340 		if (ret)
10341 			goto fail;
10342 
10343 		old->dpms_mode = connector->dpms;
10344 		old->load_detect_temp = false;
10345 
10346 		/* Make sure the crtc and connector are running */
10347 		if (connector->dpms != DRM_MODE_DPMS_ON)
10348 			connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
10349 
10350 		return true;
10351 	}
10352 
10353 	/* Find an unused one (if possible) */
10354 	for_each_crtc(dev, possible_crtc) {
10355 		i++;
10356 		if (!(encoder->possible_crtcs & (1 << i)))
10357 			continue;
10358 		if (possible_crtc->state->enable)
10359 			continue;
10360 
10361 		crtc = possible_crtc;
10362 		break;
10363 	}
10364 
10365 	/*
10366 	 * If we didn't find an unused CRTC, don't use any.
10367 	 */
10368 	if (!crtc) {
10369 		DRM_DEBUG_KMS("no pipe available for load-detect\n");
10370 		goto fail;
10371 	}
10372 
10373 	ret = drm_modeset_lock(&crtc->mutex, ctx);
10374 	if (ret)
10375 		goto fail;
10376 	ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10377 	if (ret)
10378 		goto fail;
10379 
10380 	intel_crtc = to_intel_crtc(crtc);
10381 	old->dpms_mode = connector->dpms;
10382 	old->load_detect_temp = true;
10383 	old->release_fb = NULL;
10384 
10385 	state = drm_atomic_state_alloc(dev);
10386 	if (!state)
10387 		return false;
10388 
10389 	state->acquire_ctx = ctx;
10390 
10391 	connector_state = drm_atomic_get_connector_state(state, connector);
10392 	if (IS_ERR(connector_state)) {
10393 		ret = PTR_ERR(connector_state);
10394 		goto fail;
10395 	}
10396 
10397 	connector_state->crtc = crtc;
10398 	connector_state->best_encoder = &intel_encoder->base;
10399 
10400 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10401 	if (IS_ERR(crtc_state)) {
10402 		ret = PTR_ERR(crtc_state);
10403 		goto fail;
10404 	}
10405 
10406 	crtc_state->base.active = crtc_state->base.enable = true;
10407 
10408 	if (!mode)
10409 		mode = &load_detect_mode;
10410 
10411 	/* We need a framebuffer large enough to accommodate all accesses
10412 	 * that the plane may generate whilst we perform load detection.
10413 	 * We can not rely on the fbcon either being present (we get called
10414 	 * during its initialisation to detect all boot displays, or it may
10415 	 * not even exist) or that it is large enough to satisfy the
10416 	 * requested mode.
10417 	 */
10418 	fb = mode_fits_in_fbdev(dev, mode);
10419 	if (fb == NULL) {
10420 		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
10421 		fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10422 		old->release_fb = fb;
10423 	} else
10424 		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
10425 	if (IS_ERR(fb)) {
10426 		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
10427 		goto fail;
10428 	}
10429 
10430 	ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10431 	if (ret)
10432 		goto fail;
10433 
10434 	drm_mode_copy(&crtc_state->base.mode, mode);
10435 
10436 	if (drm_atomic_commit(state)) {
10437 		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
10438 		if (old->release_fb)
10439 			old->release_fb->funcs->destroy(old->release_fb);
10440 		goto fail;
10441 	}
10442 	crtc->primary->crtc = crtc;
10443 
10444 	/* let the connector get through one full cycle before testing */
10445 	intel_wait_for_vblank(dev, intel_crtc->pipe);
10446 	return true;
10447 
10448 fail:
10449 	drm_atomic_state_free(state);
10450 	state = NULL;
10451 
10452 	if (ret == -EDEADLK) {
10453 		drm_modeset_backoff(ctx);
10454 		goto retry;
10455 	}
10456 
10457 	return false;
10458 }
10459 
10460 void intel_release_load_detect_pipe(struct drm_connector *connector,
10461 				    struct intel_load_detect_pipe *old,
10462 				    struct drm_modeset_acquire_ctx *ctx)
10463 {
10464 	struct drm_device *dev = connector->dev;
10465 	struct intel_encoder *intel_encoder =
10466 		intel_attached_encoder(connector);
10467 	struct drm_encoder *encoder = &intel_encoder->base;
10468 	struct drm_crtc *crtc = encoder->crtc;
10469 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10470 	struct drm_atomic_state *state;
10471 	struct drm_connector_state *connector_state;
10472 	struct intel_crtc_state *crtc_state;
10473 	int ret;
10474 
10475 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
10476 		      connector->base.id, connector->name,
10477 		      encoder->base.id, encoder->name);
10478 
10479 	if (old->load_detect_temp) {
10480 		state = drm_atomic_state_alloc(dev);
10481 		if (!state)
10482 			goto fail;
10483 
10484 		state->acquire_ctx = ctx;
10485 
10486 		connector_state = drm_atomic_get_connector_state(state, connector);
10487 		if (IS_ERR(connector_state))
10488 			goto fail;
10489 
10490 		crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10491 		if (IS_ERR(crtc_state))
10492 			goto fail;
10493 
10494 		connector_state->best_encoder = NULL;
10495 		connector_state->crtc = NULL;
10496 
10497 		crtc_state->base.enable = crtc_state->base.active = false;
10498 
10499 		ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10500 						      0, 0);
10501 		if (ret)
10502 			goto fail;
10503 
10504 		ret = drm_atomic_commit(state);
10505 		if (ret)
10506 			goto fail;
10507 
10508 		if (old->release_fb) {
10509 			drm_framebuffer_unregister_private(old->release_fb);
10510 			drm_framebuffer_unreference(old->release_fb);
10511 		}
10512 
10513 		return;
10514 	}
10515 
10516 	/* Switch crtc and encoder back off if necessary */
10517 	if (old->dpms_mode != DRM_MODE_DPMS_ON)
10518 		connector->funcs->dpms(connector, old->dpms_mode);
10519 
10520 	return;
10521 fail:
10522 	DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10523 	drm_atomic_state_free(state);
10524 }
10525 
10526 static int i9xx_pll_refclk(struct drm_device *dev,
10527 			   const struct intel_crtc_state *pipe_config)
10528 {
10529 	struct drm_i915_private *dev_priv = dev->dev_private;
10530 	u32 dpll = pipe_config->dpll_hw_state.dpll;
10531 
10532 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
10533 		return dev_priv->vbt.lvds_ssc_freq;
10534 	else if (HAS_PCH_SPLIT(dev))
10535 		return 120000;
10536 	else if (!IS_GEN2(dev))
10537 		return 96000;
10538 	else
10539 		return 48000;
10540 }
10541 
10542 /* Returns the clock of the currently programmed mode of the given pipe. */
10543 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
10544 				struct intel_crtc_state *pipe_config)
10545 {
10546 	struct drm_device *dev = crtc->base.dev;
10547 	struct drm_i915_private *dev_priv = dev->dev_private;
10548 	int pipe = pipe_config->cpu_transcoder;
10549 	u32 dpll = pipe_config->dpll_hw_state.dpll;
10550 	u32 fp;
10551 	intel_clock_t clock;
10552 	int port_clock;
10553 	int refclk = i9xx_pll_refclk(dev, pipe_config);
10554 
10555 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
10556 		fp = pipe_config->dpll_hw_state.fp0;
10557 	else
10558 		fp = pipe_config->dpll_hw_state.fp1;
10559 
10560 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
10561 	if (IS_PINEVIEW(dev)) {
10562 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10563 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
10564 	} else {
10565 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10566 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10567 	}
10568 
10569 	if (!IS_GEN2(dev)) {
10570 		if (IS_PINEVIEW(dev))
10571 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10572 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
10573 		else
10574 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
10575 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
10576 
10577 		switch (dpll & DPLL_MODE_MASK) {
10578 		case DPLLB_MODE_DAC_SERIAL:
10579 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10580 				5 : 10;
10581 			break;
10582 		case DPLLB_MODE_LVDS:
10583 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10584 				7 : 14;
10585 			break;
10586 		default:
10587 			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
10588 				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
10589 			return;
10590 		}
10591 
10592 		if (IS_PINEVIEW(dev))
10593 			port_clock = pnv_calc_dpll_params(refclk, &clock);
10594 		else
10595 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
10596 	} else {
10597 		u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
10598 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
10599 
10600 		if (is_lvds) {
10601 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10602 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
10603 
10604 			if (lvds & LVDS_CLKB_POWER_UP)
10605 				clock.p2 = 7;
10606 			else
10607 				clock.p2 = 14;
10608 		} else {
10609 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
10610 				clock.p1 = 2;
10611 			else {
10612 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10613 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10614 			}
10615 			if (dpll & PLL_P2_DIVIDE_BY_4)
10616 				clock.p2 = 4;
10617 			else
10618 				clock.p2 = 2;
10619 		}
10620 
10621 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
10622 	}
10623 
10624 	/*
10625 	 * This value includes pixel_multiplier. We will use
10626 	 * port_clock to compute adjusted_mode.crtc_clock in the
10627 	 * encoder's get_config() function.
10628 	 */
10629 	pipe_config->port_clock = port_clock;
10630 }
10631 
10632 int intel_dotclock_calculate(int link_freq,
10633 			     const struct intel_link_m_n *m_n)
10634 {
10635 	/*
10636 	 * The calculation for the data clock is:
10637 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
10638 	 * But we want to avoid losing precison if possible, so:
10639 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
10640 	 *
10641 	 * and the link clock is simpler:
10642 	 * link_clock = (m * link_clock) / n
10643 	 */
10644 
10645 	if (!m_n->link_n)
10646 		return 0;
10647 
10648 	return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10649 }
10650 
10651 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
10652 				   struct intel_crtc_state *pipe_config)
10653 {
10654 	struct drm_device *dev = crtc->base.dev;
10655 
10656 	/* read out port_clock from the DPLL */
10657 	i9xx_crtc_clock_get(crtc, pipe_config);
10658 
10659 	/*
10660 	 * This value does not include pixel_multiplier.
10661 	 * We will check that port_clock and adjusted_mode.crtc_clock
10662 	 * agree once we know their relationship in the encoder's
10663 	 * get_config() function.
10664 	 */
10665 	pipe_config->base.adjusted_mode.crtc_clock =
10666 		intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10667 					 &pipe_config->fdi_m_n);
10668 }
10669 
10670 /** Returns the currently programmed mode of the given pipe. */
10671 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10672 					     struct drm_crtc *crtc)
10673 {
10674 	struct drm_i915_private *dev_priv = dev->dev_private;
10675 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10676 	enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
10677 	struct drm_display_mode *mode;
10678 	struct intel_crtc_state pipe_config;
10679 	int htot = I915_READ(HTOTAL(cpu_transcoder));
10680 	int hsync = I915_READ(HSYNC(cpu_transcoder));
10681 	int vtot = I915_READ(VTOTAL(cpu_transcoder));
10682 	int vsync = I915_READ(VSYNC(cpu_transcoder));
10683 	enum i915_pipe pipe = intel_crtc->pipe;
10684 
10685 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10686 	if (!mode)
10687 		return NULL;
10688 
10689 	/*
10690 	 * Construct a pipe_config sufficient for getting the clock info
10691 	 * back out of crtc_clock_get.
10692 	 *
10693 	 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10694 	 * to use a real value here instead.
10695 	 */
10696 	pipe_config.cpu_transcoder = (enum transcoder) pipe;
10697 	pipe_config.pixel_multiplier = 1;
10698 	pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10699 	pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10700 	pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10701 	i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10702 
10703 	mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
10704 	mode->hdisplay = (htot & 0xffff) + 1;
10705 	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10706 	mode->hsync_start = (hsync & 0xffff) + 1;
10707 	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10708 	mode->vdisplay = (vtot & 0xffff) + 1;
10709 	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10710 	mode->vsync_start = (vsync & 0xffff) + 1;
10711 	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10712 
10713 	drm_mode_set_name(mode);
10714 
10715 	return mode;
10716 }
10717 
10718 void intel_mark_busy(struct drm_device *dev)
10719 {
10720 	struct drm_i915_private *dev_priv = dev->dev_private;
10721 
10722 	if (dev_priv->mm.busy)
10723 		return;
10724 
10725 	intel_runtime_pm_get(dev_priv);
10726 	i915_update_gfx_val(dev_priv);
10727 	if (INTEL_INFO(dev)->gen >= 6)
10728 		gen6_rps_busy(dev_priv);
10729 	dev_priv->mm.busy = true;
10730 }
10731 
10732 void intel_mark_idle(struct drm_device *dev)
10733 {
10734 	struct drm_i915_private *dev_priv = dev->dev_private;
10735 
10736 	if (!dev_priv->mm.busy)
10737 		return;
10738 
10739 	dev_priv->mm.busy = false;
10740 
10741 	if (INTEL_INFO(dev)->gen >= 6)
10742 		gen6_rps_idle(dev->dev_private);
10743 
10744 	intel_runtime_pm_put(dev_priv);
10745 }
10746 
10747 static void intel_crtc_destroy(struct drm_crtc *crtc)
10748 {
10749 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10750 	struct drm_device *dev = crtc->dev;
10751 	struct intel_unpin_work *work;
10752 
10753 	spin_lock_irq(&dev->event_lock);
10754 	work = intel_crtc->unpin_work;
10755 	intel_crtc->unpin_work = NULL;
10756 	spin_unlock_irq(&dev->event_lock);
10757 
10758 	if (work) {
10759 		cancel_work_sync(&work->work);
10760 		kfree(work);
10761 	}
10762 
10763 	drm_crtc_cleanup(crtc);
10764 
10765 	kfree(intel_crtc);
10766 }
10767 
10768 static void intel_unpin_work_fn(struct work_struct *__work)
10769 {
10770 	struct intel_unpin_work *work =
10771 		container_of(__work, struct intel_unpin_work, work);
10772 	struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10773 	struct drm_device *dev = crtc->base.dev;
10774 	struct drm_plane *primary = crtc->base.primary;
10775 
10776 	mutex_lock(&dev->struct_mutex);
10777 	intel_unpin_fb_obj(work->old_fb, primary->state);
10778 	drm_gem_object_unreference(&work->pending_flip_obj->base);
10779 
10780 	if (work->flip_queued_req)
10781 		i915_gem_request_assign(&work->flip_queued_req, NULL);
10782 	mutex_unlock(&dev->struct_mutex);
10783 
10784 	intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
10785 	drm_framebuffer_unreference(work->old_fb);
10786 
10787 	BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10788 	atomic_dec(&crtc->unpin_work_count);
10789 
10790 	kfree(work);
10791 }
10792 
10793 static void do_intel_finish_page_flip(struct drm_device *dev,
10794 				      struct drm_crtc *crtc)
10795 {
10796 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10797 	struct intel_unpin_work *work;
10798 	unsigned long flags;
10799 
10800 	/* Ignore early vblank irqs */
10801 	if (intel_crtc == NULL)
10802 		return;
10803 
10804 	/*
10805 	 * This is called both by irq handlers and the reset code (to complete
10806 	 * lost pageflips) so needs the full irqsave spinlocks.
10807 	 */
10808 	spin_lock_irqsave(&dev->event_lock, flags);
10809 	work = intel_crtc->unpin_work;
10810 
10811 	/* Ensure we don't miss a work->pending update ... */
10812 	smp_rmb();
10813 
10814 	if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
10815 		spin_unlock_irqrestore(&dev->event_lock, flags);
10816 		return;
10817 	}
10818 
10819 	page_flip_completed(intel_crtc);
10820 
10821 	spin_unlock_irqrestore(&dev->event_lock, flags);
10822 }
10823 
10824 void intel_finish_page_flip(struct drm_device *dev, int pipe)
10825 {
10826 	struct drm_i915_private *dev_priv = dev->dev_private;
10827 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10828 
10829 	do_intel_finish_page_flip(dev, crtc);
10830 }
10831 
10832 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10833 {
10834 	struct drm_i915_private *dev_priv = dev->dev_private;
10835 	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10836 
10837 	do_intel_finish_page_flip(dev, crtc);
10838 }
10839 
10840 /* Is 'a' after or equal to 'b'? */
10841 static bool g4x_flip_count_after_eq(u32 a, u32 b)
10842 {
10843 	return !((a - b) & 0x80000000);
10844 }
10845 
10846 static bool page_flip_finished(struct intel_crtc *crtc)
10847 {
10848 	struct drm_device *dev = crtc->base.dev;
10849 	struct drm_i915_private *dev_priv = dev->dev_private;
10850 
10851 	if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10852 	    crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10853 		return true;
10854 
10855 	/*
10856 	 * The relevant registers doen't exist on pre-ctg.
10857 	 * As the flip done interrupt doesn't trigger for mmio
10858 	 * flips on gmch platforms, a flip count check isn't
10859 	 * really needed there. But since ctg has the registers,
10860 	 * include it in the check anyway.
10861 	 */
10862 	if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10863 		return true;
10864 
10865 	/*
10866 	 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10867 	 * used the same base address. In that case the mmio flip might
10868 	 * have completed, but the CS hasn't even executed the flip yet.
10869 	 *
10870 	 * A flip count check isn't enough as the CS might have updated
10871 	 * the base address just after start of vblank, but before we
10872 	 * managed to process the interrupt. This means we'd complete the
10873 	 * CS flip too soon.
10874 	 *
10875 	 * Combining both checks should get us a good enough result. It may
10876 	 * still happen that the CS flip has been executed, but has not
10877 	 * yet actually completed. But in case the base address is the same
10878 	 * anyway, we don't really care.
10879 	 */
10880 	return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10881 		crtc->unpin_work->gtt_offset &&
10882 		g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
10883 				    crtc->unpin_work->flip_count);
10884 }
10885 
10886 void intel_prepare_page_flip(struct drm_device *dev, int plane)
10887 {
10888 	struct drm_i915_private *dev_priv = dev->dev_private;
10889 	struct intel_crtc *intel_crtc =
10890 		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10891 	unsigned long flags;
10892 
10893 
10894 	/*
10895 	 * This is called both by irq handlers and the reset code (to complete
10896 	 * lost pageflips) so needs the full irqsave spinlocks.
10897 	 *
10898 	 * NB: An MMIO update of the plane base pointer will also
10899 	 * generate a page-flip completion irq, i.e. every modeset
10900 	 * is also accompanied by a spurious intel_prepare_page_flip().
10901 	 */
10902 	spin_lock_irqsave(&dev->event_lock, flags);
10903 	if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
10904 		atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
10905 	spin_unlock_irqrestore(&dev->event_lock, flags);
10906 }
10907 
10908 static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
10909 {
10910 	/* Ensure that the work item is consistent when activating it ... */
10911 	smp_wmb();
10912 	atomic_set(&work->pending, INTEL_FLIP_PENDING);
10913 	/* and that it is marked active as soon as the irq could fire. */
10914 	smp_wmb();
10915 }
10916 
10917 static int intel_gen2_queue_flip(struct drm_device *dev,
10918 				 struct drm_crtc *crtc,
10919 				 struct drm_framebuffer *fb,
10920 				 struct drm_i915_gem_object *obj,
10921 				 struct drm_i915_gem_request *req,
10922 				 uint32_t flags)
10923 {
10924 	struct intel_engine_cs *ring = req->ring;
10925 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10926 	u32 flip_mask;
10927 	int ret;
10928 
10929 	ret = intel_ring_begin(req, 6);
10930 	if (ret)
10931 		return ret;
10932 
10933 	/* Can't queue multiple flips, so wait for the previous
10934 	 * one to finish before executing the next.
10935 	 */
10936 	if (intel_crtc->plane)
10937 		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10938 	else
10939 		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
10940 	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10941 	intel_ring_emit(ring, MI_NOOP);
10942 	intel_ring_emit(ring, MI_DISPLAY_FLIP |
10943 			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10944 	intel_ring_emit(ring, fb->pitches[0]);
10945 	intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
10946 	intel_ring_emit(ring, 0); /* aux display base address, unused */
10947 
10948 	intel_mark_page_flip_active(intel_crtc->unpin_work);
10949 	return 0;
10950 }
10951 
10952 static int intel_gen3_queue_flip(struct drm_device *dev,
10953 				 struct drm_crtc *crtc,
10954 				 struct drm_framebuffer *fb,
10955 				 struct drm_i915_gem_object *obj,
10956 				 struct drm_i915_gem_request *req,
10957 				 uint32_t flags)
10958 {
10959 	struct intel_engine_cs *ring = req->ring;
10960 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10961 	u32 flip_mask;
10962 	int ret;
10963 
10964 	ret = intel_ring_begin(req, 6);
10965 	if (ret)
10966 		return ret;
10967 
10968 	if (intel_crtc->plane)
10969 		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10970 	else
10971 		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
10972 	intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10973 	intel_ring_emit(ring, MI_NOOP);
10974 	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10975 			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10976 	intel_ring_emit(ring, fb->pitches[0]);
10977 	intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
10978 	intel_ring_emit(ring, MI_NOOP);
10979 
10980 	intel_mark_page_flip_active(intel_crtc->unpin_work);
10981 	return 0;
10982 }
10983 
10984 static int intel_gen4_queue_flip(struct drm_device *dev,
10985 				 struct drm_crtc *crtc,
10986 				 struct drm_framebuffer *fb,
10987 				 struct drm_i915_gem_object *obj,
10988 				 struct drm_i915_gem_request *req,
10989 				 uint32_t flags)
10990 {
10991 	struct intel_engine_cs *ring = req->ring;
10992 	struct drm_i915_private *dev_priv = dev->dev_private;
10993 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10994 	uint32_t pf, pipesrc;
10995 	int ret;
10996 
10997 	ret = intel_ring_begin(req, 4);
10998 	if (ret)
10999 		return ret;
11000 
11001 	/* i965+ uses the linear or tiled offsets from the
11002 	 * Display Registers (which do not change across a page-flip)
11003 	 * so we need only reprogram the base address.
11004 	 */
11005 	intel_ring_emit(ring, MI_DISPLAY_FLIP |
11006 			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11007 	intel_ring_emit(ring, fb->pitches[0]);
11008 	intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
11009 			obj->tiling_mode);
11010 
11011 	/* XXX Enabling the panel-fitter across page-flip is so far
11012 	 * untested on non-native modes, so ignore it for now.
11013 	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11014 	 */
11015 	pf = 0;
11016 	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11017 	intel_ring_emit(ring, pf | pipesrc);
11018 
11019 	intel_mark_page_flip_active(intel_crtc->unpin_work);
11020 	return 0;
11021 }
11022 
11023 static int intel_gen6_queue_flip(struct drm_device *dev,
11024 				 struct drm_crtc *crtc,
11025 				 struct drm_framebuffer *fb,
11026 				 struct drm_i915_gem_object *obj,
11027 				 struct drm_i915_gem_request *req,
11028 				 uint32_t flags)
11029 {
11030 	struct intel_engine_cs *ring = req->ring;
11031 	struct drm_i915_private *dev_priv = dev->dev_private;
11032 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11033 	uint32_t pf, pipesrc;
11034 	int ret;
11035 
11036 	ret = intel_ring_begin(req, 4);
11037 	if (ret)
11038 		return ret;
11039 
11040 	intel_ring_emit(ring, MI_DISPLAY_FLIP |
11041 			MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11042 	intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
11043 	intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11044 
11045 	/* Contrary to the suggestions in the documentation,
11046 	 * "Enable Panel Fitter" does not seem to be required when page
11047 	 * flipping with a non-native mode, and worse causes a normal
11048 	 * modeset to fail.
11049 	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11050 	 */
11051 	pf = 0;
11052 	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11053 	intel_ring_emit(ring, pf | pipesrc);
11054 
11055 	intel_mark_page_flip_active(intel_crtc->unpin_work);
11056 	return 0;
11057 }
11058 
11059 static int intel_gen7_queue_flip(struct drm_device *dev,
11060 				 struct drm_crtc *crtc,
11061 				 struct drm_framebuffer *fb,
11062 				 struct drm_i915_gem_object *obj,
11063 				 struct drm_i915_gem_request *req,
11064 				 uint32_t flags)
11065 {
11066 	struct intel_engine_cs *ring = req->ring;
11067 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11068 	uint32_t plane_bit = 0;
11069 	int len, ret;
11070 
11071 	switch (intel_crtc->plane) {
11072 	case PLANE_A:
11073 		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11074 		break;
11075 	case PLANE_B:
11076 		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11077 		break;
11078 	case PLANE_C:
11079 		plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11080 		break;
11081 	default:
11082 		WARN_ONCE(1, "unknown plane in flip command\n");
11083 		return -ENODEV;
11084 	}
11085 
11086 	len = 4;
11087 	if (ring->id == RCS) {
11088 		len += 6;
11089 		/*
11090 		 * On Gen 8, SRM is now taking an extra dword to accommodate
11091 		 * 48bits addresses, and we need a NOOP for the batch size to
11092 		 * stay even.
11093 		 */
11094 		if (IS_GEN8(dev))
11095 			len += 2;
11096 	}
11097 
11098 	/*
11099 	 * BSpec MI_DISPLAY_FLIP for IVB:
11100 	 * "The full packet must be contained within the same cache line."
11101 	 *
11102 	 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11103 	 * cacheline, if we ever start emitting more commands before
11104 	 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11105 	 * then do the cacheline alignment, and finally emit the
11106 	 * MI_DISPLAY_FLIP.
11107 	 */
11108 	ret = intel_ring_cacheline_align(req);
11109 	if (ret)
11110 		return ret;
11111 
11112 	ret = intel_ring_begin(req, len);
11113 	if (ret)
11114 		return ret;
11115 
11116 	/* Unmask the flip-done completion message. Note that the bspec says that
11117 	 * we should do this for both the BCS and RCS, and that we must not unmask
11118 	 * more than one flip event at any time (or ensure that one flip message
11119 	 * can be sent by waiting for flip-done prior to queueing new flips).
11120 	 * Experimentation says that BCS works despite DERRMR masking all
11121 	 * flip-done completion events and that unmasking all planes at once
11122 	 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11123 	 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11124 	 */
11125 	if (ring->id == RCS) {
11126 		intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11127 		intel_ring_emit(ring, DERRMR);
11128 		intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11129 					DERRMR_PIPEB_PRI_FLIP_DONE |
11130 					DERRMR_PIPEC_PRI_FLIP_DONE));
11131 		if (IS_GEN8(dev))
11132 			intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
11133 					      MI_SRM_LRM_GLOBAL_GTT);
11134 		else
11135 			intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
11136 					      MI_SRM_LRM_GLOBAL_GTT);
11137 		intel_ring_emit(ring, DERRMR);
11138 		intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
11139 		if (IS_GEN8(dev)) {
11140 			intel_ring_emit(ring, 0);
11141 			intel_ring_emit(ring, MI_NOOP);
11142 		}
11143 	}
11144 
11145 	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
11146 	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
11147 	intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11148 	intel_ring_emit(ring, (MI_NOOP));
11149 
11150 	intel_mark_page_flip_active(intel_crtc->unpin_work);
11151 	return 0;
11152 }
11153 
11154 static bool use_mmio_flip(struct intel_engine_cs *ring,
11155 			  struct drm_i915_gem_object *obj)
11156 {
11157 	/*
11158 	 * This is not being used for older platforms, because
11159 	 * non-availability of flip done interrupt forces us to use
11160 	 * CS flips. Older platforms derive flip done using some clever
11161 	 * tricks involving the flip_pending status bits and vblank irqs.
11162 	 * So using MMIO flips there would disrupt this mechanism.
11163 	 */
11164 
11165 	if (ring == NULL)
11166 		return true;
11167 
11168 	if (INTEL_INFO(ring->dev)->gen < 5)
11169 		return false;
11170 
11171 	if (i915.use_mmio_flip < 0)
11172 		return false;
11173 	else if (i915.use_mmio_flip > 0)
11174 		return true;
11175 	else if (i915.enable_execlists)
11176 		return true;
11177 	else
11178 		return ring != i915_gem_request_get_ring(obj->last_write_req);
11179 }
11180 
11181 static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
11182 			     struct intel_unpin_work *work)
11183 {
11184 	struct drm_device *dev = intel_crtc->base.dev;
11185 	struct drm_i915_private *dev_priv = dev->dev_private;
11186 	struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11187 	const enum i915_pipe pipe = intel_crtc->pipe;
11188 	u32 ctl, stride;
11189 
11190 	ctl = I915_READ(PLANE_CTL(pipe, 0));
11191 	ctl &= ~PLANE_CTL_TILED_MASK;
11192 	switch (fb->modifier[0]) {
11193 	case DRM_FORMAT_MOD_NONE:
11194 		break;
11195 	case I915_FORMAT_MOD_X_TILED:
11196 		ctl |= PLANE_CTL_TILED_X;
11197 		break;
11198 	case I915_FORMAT_MOD_Y_TILED:
11199 		ctl |= PLANE_CTL_TILED_Y;
11200 		break;
11201 	case I915_FORMAT_MOD_Yf_TILED:
11202 		ctl |= PLANE_CTL_TILED_YF;
11203 		break;
11204 	default:
11205 		MISSING_CASE(fb->modifier[0]);
11206 	}
11207 
11208 	/*
11209 	 * The stride is either expressed as a multiple of 64 bytes chunks for
11210 	 * linear buffers or in number of tiles for tiled buffers.
11211 	 */
11212 	stride = fb->pitches[0] /
11213 		 intel_fb_stride_alignment(dev, fb->modifier[0],
11214 					   fb->pixel_format);
11215 
11216 	/*
11217 	 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11218 	 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11219 	 */
11220 	I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11221 	I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11222 
11223 	I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
11224 	POSTING_READ(PLANE_SURF(pipe, 0));
11225 }
11226 
11227 static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
11228 			     struct intel_unpin_work *work)
11229 {
11230 	struct drm_device *dev = intel_crtc->base.dev;
11231 	struct drm_i915_private *dev_priv = dev->dev_private;
11232 	struct intel_framebuffer *intel_fb =
11233 		to_intel_framebuffer(intel_crtc->base.primary->fb);
11234 	struct drm_i915_gem_object *obj = intel_fb->obj;
11235 	u32 dspcntr;
11236 	u32 reg;
11237 
11238 	reg = DSPCNTR(intel_crtc->plane);
11239 	dspcntr = I915_READ(reg);
11240 
11241 	if (obj->tiling_mode != I915_TILING_NONE)
11242 		dspcntr |= DISPPLANE_TILED;
11243 	else
11244 		dspcntr &= ~DISPPLANE_TILED;
11245 
11246 	I915_WRITE(reg, dspcntr);
11247 
11248 	I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
11249 	POSTING_READ(DSPSURF(intel_crtc->plane));
11250 }
11251 
11252 /*
11253  * XXX: This is the temporary way to update the plane registers until we get
11254  * around to using the usual plane update functions for MMIO flips
11255  */
11256 static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
11257 {
11258 	struct intel_crtc *crtc = mmio_flip->crtc;
11259 	struct intel_unpin_work *work;
11260 
11261 	spin_lock_irq(&crtc->base.dev->event_lock);
11262 	work = crtc->unpin_work;
11263 	spin_unlock_irq(&crtc->base.dev->event_lock);
11264 	if (work == NULL)
11265 		return;
11266 
11267 	intel_mark_page_flip_active(work);
11268 
11269 	intel_pipe_update_start(crtc);
11270 
11271 	if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
11272 		skl_do_mmio_flip(crtc, work);
11273 	else
11274 		/* use_mmio_flip() retricts MMIO flips to ilk+ */
11275 		ilk_do_mmio_flip(crtc, work);
11276 
11277 	intel_pipe_update_end(crtc);
11278 }
11279 
11280 static void intel_mmio_flip_work_func(struct work_struct *work)
11281 {
11282 	struct intel_mmio_flip *mmio_flip =
11283 		container_of(work, struct intel_mmio_flip, work);
11284 
11285 	if (mmio_flip->req) {
11286 		WARN_ON(__i915_wait_request(mmio_flip->req,
11287 					    mmio_flip->crtc->reset_counter,
11288 					    false, NULL,
11289 					    &mmio_flip->i915->rps.mmioflips));
11290 		i915_gem_request_unreference__unlocked(mmio_flip->req);
11291 	}
11292 
11293 	intel_do_mmio_flip(mmio_flip);
11294 	kfree(mmio_flip);
11295 }
11296 
11297 static int intel_queue_mmio_flip(struct drm_device *dev,
11298 				 struct drm_crtc *crtc,
11299 				 struct drm_framebuffer *fb,
11300 				 struct drm_i915_gem_object *obj,
11301 				 struct intel_engine_cs *ring,
11302 				 uint32_t flags)
11303 {
11304 	struct intel_mmio_flip *mmio_flip;
11305 
11306 	mmio_flip = kmalloc(sizeof(*mmio_flip), M_DRM, M_WAITOK);
11307 	if (mmio_flip == NULL)
11308 		return -ENOMEM;
11309 
11310 	mmio_flip->i915 = to_i915(dev);
11311 	mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
11312 	mmio_flip->crtc = to_intel_crtc(crtc);
11313 
11314 	INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11315 	schedule_work(&mmio_flip->work);
11316 
11317 	return 0;
11318 }
11319 
11320 static int intel_default_queue_flip(struct drm_device *dev,
11321 				    struct drm_crtc *crtc,
11322 				    struct drm_framebuffer *fb,
11323 				    struct drm_i915_gem_object *obj,
11324 				    struct drm_i915_gem_request *req,
11325 				    uint32_t flags)
11326 {
11327 	return -ENODEV;
11328 }
11329 
11330 static bool __intel_pageflip_stall_check(struct drm_device *dev,
11331 					 struct drm_crtc *crtc)
11332 {
11333 	struct drm_i915_private *dev_priv = dev->dev_private;
11334 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11335 	struct intel_unpin_work *work = intel_crtc->unpin_work;
11336 	u32 addr;
11337 
11338 	if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11339 		return true;
11340 
11341 	if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11342 		return false;
11343 
11344 	if (!work->enable_stall_check)
11345 		return false;
11346 
11347 	if (work->flip_ready_vblank == 0) {
11348 		if (work->flip_queued_req &&
11349 		    !i915_gem_request_completed(work->flip_queued_req, true))
11350 			return false;
11351 
11352 		work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
11353 	}
11354 
11355 	if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
11356 		return false;
11357 
11358 	/* Potential stall - if we see that the flip has happened,
11359 	 * assume a missed interrupt. */
11360 	if (INTEL_INFO(dev)->gen >= 4)
11361 		addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11362 	else
11363 		addr = I915_READ(DSPADDR(intel_crtc->plane));
11364 
11365 	/* There is a potential issue here with a false positive after a flip
11366 	 * to the same address. We could address this by checking for a
11367 	 * non-incrementing frame counter.
11368 	 */
11369 	return addr == work->gtt_offset;
11370 }
11371 
11372 void intel_check_page_flip(struct drm_device *dev, int pipe)
11373 {
11374 	struct drm_i915_private *dev_priv = dev->dev_private;
11375 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11376 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11377 	struct intel_unpin_work *work;
11378 
11379 //	WARN_ON(!in_interrupt());
11380 
11381 	if (crtc == NULL)
11382 		return;
11383 
11384 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
11385 	work = intel_crtc->unpin_work;
11386 	if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
11387 		WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
11388 			 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
11389 		page_flip_completed(intel_crtc);
11390 		work = NULL;
11391 	}
11392 	if (work != NULL &&
11393 	    drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11394 		intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
11395 	lockmgr(&dev->event_lock, LK_RELEASE);
11396 }
11397 
11398 static int intel_crtc_page_flip(struct drm_crtc *crtc,
11399 				struct drm_framebuffer *fb,
11400 				struct drm_pending_vblank_event *event,
11401 				uint32_t page_flip_flags)
11402 {
11403 	struct drm_device *dev = crtc->dev;
11404 	struct drm_i915_private *dev_priv = dev->dev_private;
11405 	struct drm_framebuffer *old_fb = crtc->primary->fb;
11406 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11407 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11408 	struct drm_plane *primary = crtc->primary;
11409 	enum i915_pipe pipe = intel_crtc->pipe;
11410 	struct intel_unpin_work *work;
11411 	struct intel_engine_cs *ring;
11412 	bool mmio_flip;
11413 	struct drm_i915_gem_request *request = NULL;
11414 	int ret;
11415 
11416 	/*
11417 	 * drm_mode_page_flip_ioctl() should already catch this, but double
11418 	 * check to be safe.  In the future we may enable pageflipping from
11419 	 * a disabled primary plane.
11420 	 */
11421 	if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11422 		return -EBUSY;
11423 
11424 	/* Can't change pixel format via MI display flips. */
11425 	if (fb->pixel_format != crtc->primary->fb->pixel_format)
11426 		return -EINVAL;
11427 
11428 	/*
11429 	 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11430 	 * Note that pitch changes could also affect these register.
11431 	 */
11432 	if (INTEL_INFO(dev)->gen > 3 &&
11433 	    (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11434 	     fb->pitches[0] != crtc->primary->fb->pitches[0]))
11435 		return -EINVAL;
11436 
11437 	if (i915_terminally_wedged(&dev_priv->gpu_error))
11438 		goto out_hang;
11439 
11440 	work = kzalloc(sizeof(*work), GFP_KERNEL);
11441 	if (work == NULL)
11442 		return -ENOMEM;
11443 
11444 	work->event = event;
11445 	work->crtc = crtc;
11446 	work->old_fb = old_fb;
11447 	INIT_WORK(&work->work, intel_unpin_work_fn);
11448 
11449 	ret = drm_crtc_vblank_get(crtc);
11450 	if (ret)
11451 		goto free_work;
11452 
11453 	/* We borrow the event spin lock for protecting unpin_work */
11454 	spin_lock_irq(&dev->event_lock);
11455 	if (intel_crtc->unpin_work) {
11456 		/* Before declaring the flip queue wedged, check if
11457 		 * the hardware completed the operation behind our backs.
11458 		 */
11459 		if (__intel_pageflip_stall_check(dev, crtc)) {
11460 			DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11461 			page_flip_completed(intel_crtc);
11462 		} else {
11463 			DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11464 			spin_unlock_irq(&dev->event_lock);
11465 
11466 			drm_crtc_vblank_put(crtc);
11467 			kfree(work);
11468 			return -EBUSY;
11469 		}
11470 	}
11471 	intel_crtc->unpin_work = work;
11472 	spin_unlock_irq(&dev->event_lock);
11473 
11474 	if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11475 		flush_workqueue(dev_priv->wq);
11476 
11477 	/* Reference the objects for the scheduled work. */
11478 	drm_framebuffer_reference(work->old_fb);
11479 	drm_gem_object_reference(&obj->base);
11480 
11481 	crtc->primary->fb = fb;
11482 	update_state_fb(crtc->primary);
11483 
11484 	work->pending_flip_obj = obj;
11485 
11486 	ret = i915_mutex_lock_interruptible(dev);
11487 	if (ret)
11488 		goto cleanup;
11489 
11490 	atomic_inc(&intel_crtc->unpin_work_count);
11491 	intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
11492 
11493 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11494 		work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
11495 
11496 	if (IS_VALLEYVIEW(dev)) {
11497 		ring = &dev_priv->ring[BCS];
11498 		if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11499 			/* vlv: DISPLAY_FLIP fails to change tiling */
11500 			ring = NULL;
11501 	} else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11502 		ring = &dev_priv->ring[BCS];
11503 	} else if (INTEL_INFO(dev)->gen >= 7) {
11504 		ring = i915_gem_request_get_ring(obj->last_write_req);
11505 		if (ring == NULL || ring->id != RCS)
11506 			ring = &dev_priv->ring[BCS];
11507 	} else {
11508 		ring = &dev_priv->ring[RCS];
11509 	}
11510 
11511 	mmio_flip = use_mmio_flip(ring, obj);
11512 
11513 	/* When using CS flips, we want to emit semaphores between rings.
11514 	 * However, when using mmio flips we will create a task to do the
11515 	 * synchronisation, so all we want here is to pin the framebuffer
11516 	 * into the display plane and skip any waits.
11517 	 */
11518 	ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
11519 					 crtc->primary->state,
11520 					 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
11521 	if (ret)
11522 		goto cleanup_pending;
11523 
11524 	work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
11525 						  obj, 0);
11526 	work->gtt_offset += intel_crtc->dspaddr_offset;
11527 
11528 	if (mmio_flip) {
11529 		ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11530 					    page_flip_flags);
11531 		if (ret)
11532 			goto cleanup_unpin;
11533 
11534 		i915_gem_request_assign(&work->flip_queued_req,
11535 					obj->last_write_req);
11536 	} else {
11537 		if (!request) {
11538 			ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11539 			if (ret)
11540 				goto cleanup_unpin;
11541 		}
11542 
11543 		ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
11544 						   page_flip_flags);
11545 		if (ret)
11546 			goto cleanup_unpin;
11547 
11548 		i915_gem_request_assign(&work->flip_queued_req, request);
11549 	}
11550 
11551 	if (request)
11552 		i915_add_request_no_flush(request);
11553 
11554 	work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
11555 	work->enable_stall_check = true;
11556 
11557 	i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
11558 			  to_intel_plane(primary)->frontbuffer_bit);
11559 	mutex_unlock(&dev->struct_mutex);
11560 
11561 	intel_fbc_disable_crtc(intel_crtc);
11562 	intel_frontbuffer_flip_prepare(dev,
11563 				       to_intel_plane(primary)->frontbuffer_bit);
11564 
11565 	trace_i915_flip_request(intel_crtc->plane, obj);
11566 
11567 	return 0;
11568 
11569 cleanup_unpin:
11570 	intel_unpin_fb_obj(fb, crtc->primary->state);
11571 cleanup_pending:
11572 	if (request)
11573 		i915_gem_request_cancel(request);
11574 	atomic_dec(&intel_crtc->unpin_work_count);
11575 	mutex_unlock(&dev->struct_mutex);
11576 cleanup:
11577 	crtc->primary->fb = old_fb;
11578 	update_state_fb(crtc->primary);
11579 
11580 	drm_gem_object_unreference_unlocked(&obj->base);
11581 	drm_framebuffer_unreference(work->old_fb);
11582 
11583 	spin_lock_irq(&dev->event_lock);
11584 	intel_crtc->unpin_work = NULL;
11585 	spin_unlock_irq(&dev->event_lock);
11586 
11587 	drm_crtc_vblank_put(crtc);
11588 free_work:
11589 	kfree(work);
11590 
11591 	if (ret == -EIO) {
11592 		struct drm_atomic_state *state;
11593 		struct drm_plane_state *plane_state;
11594 
11595 out_hang:
11596 		state = drm_atomic_state_alloc(dev);
11597 		if (!state)
11598 			return -ENOMEM;
11599 		state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11600 
11601 retry:
11602 		plane_state = drm_atomic_get_plane_state(state, primary);
11603 		ret = PTR_ERR_OR_ZERO(plane_state);
11604 		if (!ret) {
11605 			drm_atomic_set_fb_for_plane(plane_state, fb);
11606 
11607 			ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11608 			if (!ret)
11609 				ret = drm_atomic_commit(state);
11610 		}
11611 
11612 		if (ret == -EDEADLK) {
11613 			drm_modeset_backoff(state->acquire_ctx);
11614 			drm_atomic_state_clear(state);
11615 			goto retry;
11616 		}
11617 
11618 		if (ret)
11619 			drm_atomic_state_free(state);
11620 
11621 		if (ret == 0 && event) {
11622 			spin_lock_irq(&dev->event_lock);
11623 			drm_send_vblank_event(dev, pipe, event);
11624 			spin_unlock_irq(&dev->event_lock);
11625 		}
11626 	}
11627 	return ret;
11628 }
11629 
11630 
11631 /**
11632  * intel_wm_need_update - Check whether watermarks need updating
11633  * @plane: drm plane
11634  * @state: new plane state
11635  *
11636  * Check current plane state versus the new one to determine whether
11637  * watermarks need to be recalculated.
11638  *
11639  * Returns true or false.
11640  */
11641 static bool intel_wm_need_update(struct drm_plane *plane,
11642 				 struct drm_plane_state *state)
11643 {
11644 	/* Update watermarks on tiling changes. */
11645 	if (!plane->state->fb || !state->fb ||
11646 	    plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11647 	    plane->state->rotation != state->rotation)
11648 		return true;
11649 
11650 	if (plane->state->crtc_w != state->crtc_w)
11651 		return true;
11652 
11653 	return false;
11654 }
11655 
11656 int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11657 				    struct drm_plane_state *plane_state)
11658 {
11659 	struct drm_crtc *crtc = crtc_state->crtc;
11660 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11661 	struct drm_plane *plane = plane_state->plane;
11662 	struct drm_device *dev = crtc->dev;
11663 	struct drm_i915_private *dev_priv = dev->dev_private;
11664 	struct intel_plane_state *old_plane_state =
11665 		to_intel_plane_state(plane->state);
11666 	int idx = intel_crtc->base.base.id, ret;
11667 	int i = drm_plane_index(plane);
11668 	bool mode_changed = needs_modeset(crtc_state);
11669 	bool was_crtc_enabled = crtc->state->active;
11670 	bool is_crtc_enabled = crtc_state->active;
11671 
11672 	bool turn_off, turn_on, visible, was_visible;
11673 	struct drm_framebuffer *fb = plane_state->fb;
11674 
11675 	if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11676 	    plane->type != DRM_PLANE_TYPE_CURSOR) {
11677 		ret = skl_update_scaler_plane(
11678 			to_intel_crtc_state(crtc_state),
11679 			to_intel_plane_state(plane_state));
11680 		if (ret)
11681 			return ret;
11682 	}
11683 
11684 	/*
11685 	 * Disabling a plane is always okay; we just need to update
11686 	 * fb tracking in a special way since cleanup_fb() won't
11687 	 * get called by the plane helpers.
11688 	 */
11689 	if (old_plane_state->base.fb && !fb)
11690 		intel_crtc->atomic.disabled_planes |= 1 << i;
11691 
11692 	was_visible = old_plane_state->visible;
11693 	visible = to_intel_plane_state(plane_state)->visible;
11694 
11695 	if (!was_crtc_enabled && WARN_ON(was_visible))
11696 		was_visible = false;
11697 
11698 	if (!is_crtc_enabled && WARN_ON(visible))
11699 		visible = false;
11700 
11701 	if (!was_visible && !visible)
11702 		return 0;
11703 
11704 	turn_off = was_visible && (!visible || mode_changed);
11705 	turn_on = visible && (!was_visible || mode_changed);
11706 
11707 	DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11708 			 plane->base.id, fb ? fb->base.id : -1);
11709 
11710 	DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11711 			 plane->base.id, was_visible, visible,
11712 			 turn_off, turn_on, mode_changed);
11713 
11714 	if (turn_on) {
11715 		intel_crtc->atomic.update_wm_pre = true;
11716 		/* must disable cxsr around plane enable/disable */
11717 		if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11718 			intel_crtc->atomic.disable_cxsr = true;
11719 			/* to potentially re-enable cxsr */
11720 			intel_crtc->atomic.wait_vblank = true;
11721 			intel_crtc->atomic.update_wm_post = true;
11722 		}
11723 	} else if (turn_off) {
11724 		intel_crtc->atomic.update_wm_post = true;
11725 		/* must disable cxsr around plane enable/disable */
11726 		if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11727 			if (is_crtc_enabled)
11728 				intel_crtc->atomic.wait_vblank = true;
11729 			intel_crtc->atomic.disable_cxsr = true;
11730 		}
11731 	} else if (intel_wm_need_update(plane, plane_state)) {
11732 		intel_crtc->atomic.update_wm_pre = true;
11733 	}
11734 
11735 	if (visible || was_visible)
11736 		intel_crtc->atomic.fb_bits |=
11737 			to_intel_plane(plane)->frontbuffer_bit;
11738 
11739 	switch (plane->type) {
11740 	case DRM_PLANE_TYPE_PRIMARY:
11741 		intel_crtc->atomic.wait_for_flips = true;
11742 		intel_crtc->atomic.pre_disable_primary = turn_off;
11743 		intel_crtc->atomic.post_enable_primary = turn_on;
11744 
11745 		if (turn_off) {
11746 			/*
11747 			 * FIXME: Actually if we will still have any other
11748 			 * plane enabled on the pipe we could let IPS enabled
11749 			 * still, but for now lets consider that when we make
11750 			 * primary invisible by setting DSPCNTR to 0 on
11751 			 * update_primary_plane function IPS needs to be
11752 			 * disable.
11753 			 */
11754 			intel_crtc->atomic.disable_ips = true;
11755 
11756 			intel_crtc->atomic.disable_fbc = true;
11757 		}
11758 
11759 		/*
11760 		 * FBC does not work on some platforms for rotated
11761 		 * planes, so disable it when rotation is not 0 and
11762 		 * update it when rotation is set back to 0.
11763 		 *
11764 		 * FIXME: This is redundant with the fbc update done in
11765 		 * the primary plane enable function except that that
11766 		 * one is done too late. We eventually need to unify
11767 		 * this.
11768 		 */
11769 
11770 		if (visible &&
11771 		    INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11772 		    dev_priv->fbc.crtc == intel_crtc &&
11773 		    plane_state->rotation != BIT(DRM_ROTATE_0))
11774 			intel_crtc->atomic.disable_fbc = true;
11775 
11776 		/*
11777 		 * BDW signals flip done immediately if the plane
11778 		 * is disabled, even if the plane enable is already
11779 		 * armed to occur at the next vblank :(
11780 		 */
11781 		if (turn_on && IS_BROADWELL(dev))
11782 			intel_crtc->atomic.wait_vblank = true;
11783 
11784 		intel_crtc->atomic.update_fbc |= visible || mode_changed;
11785 		break;
11786 	case DRM_PLANE_TYPE_CURSOR:
11787 		break;
11788 	case DRM_PLANE_TYPE_OVERLAY:
11789 		if (turn_off && !mode_changed) {
11790 			intel_crtc->atomic.wait_vblank = true;
11791 			intel_crtc->atomic.update_sprite_watermarks |=
11792 				1 << i;
11793 		}
11794 	}
11795 	return 0;
11796 }
11797 
11798 static bool encoders_cloneable(const struct intel_encoder *a,
11799 			       const struct intel_encoder *b)
11800 {
11801 	/* masks could be asymmetric, so check both ways */
11802 	return a == b || (a->cloneable & (1 << b->type) &&
11803 			  b->cloneable & (1 << a->type));
11804 }
11805 
11806 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11807 					 struct intel_crtc *crtc,
11808 					 struct intel_encoder *encoder)
11809 {
11810 	struct intel_encoder *source_encoder;
11811 	struct drm_connector *connector;
11812 	struct drm_connector_state *connector_state;
11813 	int i;
11814 
11815 	for_each_connector_in_state(state, connector, connector_state, i) {
11816 		if (connector_state->crtc != &crtc->base)
11817 			continue;
11818 
11819 		source_encoder =
11820 			to_intel_encoder(connector_state->best_encoder);
11821 		if (!encoders_cloneable(encoder, source_encoder))
11822 			return false;
11823 	}
11824 
11825 	return true;
11826 }
11827 
11828 static bool check_encoder_cloning(struct drm_atomic_state *state,
11829 				  struct intel_crtc *crtc)
11830 {
11831 	struct intel_encoder *encoder;
11832 	struct drm_connector *connector;
11833 	struct drm_connector_state *connector_state;
11834 	int i;
11835 
11836 	for_each_connector_in_state(state, connector, connector_state, i) {
11837 		if (connector_state->crtc != &crtc->base)
11838 			continue;
11839 
11840 		encoder = to_intel_encoder(connector_state->best_encoder);
11841 		if (!check_single_encoder_cloning(state, crtc, encoder))
11842 			return false;
11843 	}
11844 
11845 	return true;
11846 }
11847 
11848 static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11849 				   struct drm_crtc_state *crtc_state)
11850 {
11851 	struct drm_device *dev = crtc->dev;
11852 	struct drm_i915_private *dev_priv = dev->dev_private;
11853 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11854 	struct intel_crtc_state *pipe_config =
11855 		to_intel_crtc_state(crtc_state);
11856 	struct drm_atomic_state *state = crtc_state->state;
11857 	int ret;
11858 	bool mode_changed = needs_modeset(crtc_state);
11859 
11860 	if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11861 		DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11862 		return -EINVAL;
11863 	}
11864 
11865 	if (mode_changed && !crtc_state->active)
11866 		intel_crtc->atomic.update_wm_post = true;
11867 
11868 	if (mode_changed && crtc_state->enable &&
11869 	    dev_priv->display.crtc_compute_clock &&
11870 	    !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11871 		ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11872 							   pipe_config);
11873 		if (ret)
11874 			return ret;
11875 	}
11876 
11877 	ret = 0;
11878 	if (INTEL_INFO(dev)->gen >= 9) {
11879 		if (mode_changed)
11880 			ret = skl_update_scaler_crtc(pipe_config);
11881 
11882 		if (!ret)
11883 			ret = intel_atomic_setup_scalers(dev, intel_crtc,
11884 							 pipe_config);
11885 	}
11886 
11887 	return ret;
11888 }
11889 
11890 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
11891 	.mode_set_base_atomic = intel_pipe_set_base_atomic,
11892 	.load_lut = intel_crtc_load_lut,
11893 	.atomic_begin = intel_begin_crtc_commit,
11894 	.atomic_flush = intel_finish_crtc_commit,
11895 	.atomic_check = intel_crtc_atomic_check,
11896 };
11897 
11898 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11899 {
11900 	struct intel_connector *connector;
11901 
11902 	for_each_intel_connector(dev, connector) {
11903 		if (connector->base.encoder) {
11904 			connector->base.state->best_encoder =
11905 				connector->base.encoder;
11906 			connector->base.state->crtc =
11907 				connector->base.encoder->crtc;
11908 		} else {
11909 			connector->base.state->best_encoder = NULL;
11910 			connector->base.state->crtc = NULL;
11911 		}
11912 	}
11913 }
11914 
11915 static void
11916 connected_sink_compute_bpp(struct intel_connector *connector,
11917 			   struct intel_crtc_state *pipe_config)
11918 {
11919 	int bpp = pipe_config->pipe_bpp;
11920 
11921 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11922 		connector->base.base.id,
11923 		connector->base.name);
11924 
11925 	/* Don't use an invalid EDID bpc value */
11926 	if (connector->base.display_info.bpc &&
11927 	    connector->base.display_info.bpc * 3 < bpp) {
11928 		DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11929 			      bpp, connector->base.display_info.bpc*3);
11930 		pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11931 	}
11932 
11933 	/* Clamp bpp to 8 on screens without EDID 1.4 */
11934 	if (connector->base.display_info.bpc == 0 && bpp > 24) {
11935 		DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11936 			      bpp);
11937 		pipe_config->pipe_bpp = 24;
11938 	}
11939 }
11940 
11941 static int
11942 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
11943 			  struct intel_crtc_state *pipe_config)
11944 {
11945 	struct drm_device *dev = crtc->base.dev;
11946 	struct drm_atomic_state *state;
11947 	struct drm_connector *connector;
11948 	struct drm_connector_state *connector_state;
11949 	int bpp, i;
11950 
11951 	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
11952 		bpp = 10*3;
11953 	else if (INTEL_INFO(dev)->gen >= 5)
11954 		bpp = 12*3;
11955 	else
11956 		bpp = 8*3;
11957 
11958 
11959 	pipe_config->pipe_bpp = bpp;
11960 
11961 	state = pipe_config->base.state;
11962 
11963 	/* Clamp display bpp to EDID value */
11964 	for_each_connector_in_state(state, connector, connector_state, i) {
11965 		if (connector_state->crtc != &crtc->base)
11966 			continue;
11967 
11968 		connected_sink_compute_bpp(to_intel_connector(connector),
11969 					   pipe_config);
11970 	}
11971 
11972 	return bpp;
11973 }
11974 
11975 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11976 {
11977 	DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11978 			"type: 0x%x flags: 0x%x\n",
11979 		mode->crtc_clock,
11980 		mode->crtc_hdisplay, mode->crtc_hsync_start,
11981 		mode->crtc_hsync_end, mode->crtc_htotal,
11982 		mode->crtc_vdisplay, mode->crtc_vsync_start,
11983 		mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11984 }
11985 
11986 static void intel_dump_pipe_config(struct intel_crtc *crtc,
11987 				   struct intel_crtc_state *pipe_config,
11988 				   const char *context)
11989 {
11990 	struct drm_device *dev = crtc->base.dev;
11991 	struct drm_plane *plane;
11992 	struct intel_plane *intel_plane;
11993 	struct intel_plane_state *state;
11994 	struct drm_framebuffer *fb;
11995 
11996 	DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11997 		      context, pipe_config, pipe_name(crtc->pipe));
11998 
11999 	DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12000 	DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12001 		      pipe_config->pipe_bpp, pipe_config->dither);
12002 	DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12003 		      pipe_config->has_pch_encoder,
12004 		      pipe_config->fdi_lanes,
12005 		      pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12006 		      pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12007 		      pipe_config->fdi_m_n.tu);
12008 	DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12009 		      pipe_config->has_dp_encoder,
12010 		      pipe_config->lane_count,
12011 		      pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12012 		      pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12013 		      pipe_config->dp_m_n.tu);
12014 
12015 	DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12016 		      pipe_config->has_dp_encoder,
12017 		      pipe_config->lane_count,
12018 		      pipe_config->dp_m2_n2.gmch_m,
12019 		      pipe_config->dp_m2_n2.gmch_n,
12020 		      pipe_config->dp_m2_n2.link_m,
12021 		      pipe_config->dp_m2_n2.link_n,
12022 		      pipe_config->dp_m2_n2.tu);
12023 
12024 	DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12025 		      pipe_config->has_audio,
12026 		      pipe_config->has_infoframe);
12027 
12028 	DRM_DEBUG_KMS("requested mode:\n");
12029 	drm_mode_debug_printmodeline(&pipe_config->base.mode);
12030 	DRM_DEBUG_KMS("adjusted mode:\n");
12031 	drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12032 	intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
12033 	DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
12034 	DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12035 		      pipe_config->pipe_src_w, pipe_config->pipe_src_h);
12036 	DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12037 		      crtc->num_scalers,
12038 		      pipe_config->scaler_state.scaler_users,
12039 		      pipe_config->scaler_state.scaler_id);
12040 	DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12041 		      pipe_config->gmch_pfit.control,
12042 		      pipe_config->gmch_pfit.pgm_ratios,
12043 		      pipe_config->gmch_pfit.lvds_border_bits);
12044 	DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12045 		      pipe_config->pch_pfit.pos,
12046 		      pipe_config->pch_pfit.size,
12047 		      pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
12048 	DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
12049 	DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
12050 
12051 	if (IS_BROXTON(dev)) {
12052 		DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
12053 			      "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12054 			      "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
12055 			      pipe_config->ddi_pll_sel,
12056 			      pipe_config->dpll_hw_state.ebb0,
12057 			      pipe_config->dpll_hw_state.ebb4,
12058 			      pipe_config->dpll_hw_state.pll0,
12059 			      pipe_config->dpll_hw_state.pll1,
12060 			      pipe_config->dpll_hw_state.pll2,
12061 			      pipe_config->dpll_hw_state.pll3,
12062 			      pipe_config->dpll_hw_state.pll6,
12063 			      pipe_config->dpll_hw_state.pll8,
12064 			      pipe_config->dpll_hw_state.pll9,
12065 			      pipe_config->dpll_hw_state.pll10,
12066 			      pipe_config->dpll_hw_state.pcsdw12);
12067 	} else if (IS_SKYLAKE(dev)) {
12068 		DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12069 			      "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12070 			      pipe_config->ddi_pll_sel,
12071 			      pipe_config->dpll_hw_state.ctrl1,
12072 			      pipe_config->dpll_hw_state.cfgcr1,
12073 			      pipe_config->dpll_hw_state.cfgcr2);
12074 	} else if (HAS_DDI(dev)) {
12075 		DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
12076 			      pipe_config->ddi_pll_sel,
12077 			      pipe_config->dpll_hw_state.wrpll,
12078 			      pipe_config->dpll_hw_state.spll);
12079 	} else {
12080 		DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12081 			      "fp0: 0x%x, fp1: 0x%x\n",
12082 			      pipe_config->dpll_hw_state.dpll,
12083 			      pipe_config->dpll_hw_state.dpll_md,
12084 			      pipe_config->dpll_hw_state.fp0,
12085 			      pipe_config->dpll_hw_state.fp1);
12086 	}
12087 
12088 	DRM_DEBUG_KMS("planes on this crtc\n");
12089 	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12090 		intel_plane = to_intel_plane(plane);
12091 		if (intel_plane->pipe != crtc->pipe)
12092 			continue;
12093 
12094 		state = to_intel_plane_state(plane->state);
12095 		fb = state->base.fb;
12096 		if (!fb) {
12097 			DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12098 				"disabled, scaler_id = %d\n",
12099 				plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12100 				plane->base.id, intel_plane->pipe,
12101 				(crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12102 				drm_plane_index(plane), state->scaler_id);
12103 			continue;
12104 		}
12105 
12106 		DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12107 			plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12108 			plane->base.id, intel_plane->pipe,
12109 			crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12110 			drm_plane_index(plane));
12111 		DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12112 			fb->base.id, fb->width, fb->height, fb->pixel_format);
12113 		DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12114 			state->scaler_id,
12115 			state->src.x1 >> 16, state->src.y1 >> 16,
12116 			drm_rect_width(&state->src) >> 16,
12117 			drm_rect_height(&state->src) >> 16,
12118 			state->dst.x1, state->dst.y1,
12119 			drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12120 	}
12121 }
12122 
12123 static bool check_digital_port_conflicts(struct drm_atomic_state *state)
12124 {
12125 	struct drm_device *dev = state->dev;
12126 	struct drm_connector *connector;
12127 	unsigned int used_ports = 0;
12128 
12129 	/*
12130 	 * Walk the connector list instead of the encoder
12131 	 * list to detect the problem on ddi platforms
12132 	 * where there's just one encoder per digital port.
12133 	 */
12134 	drm_for_each_connector(connector, dev) {
12135 		struct drm_connector_state *connector_state;
12136 		struct intel_encoder *encoder;
12137 
12138 		connector_state = drm_atomic_get_existing_connector_state(state, connector);
12139 		if (!connector_state)
12140 			connector_state = connector->state;
12141 
12142 		if (!connector_state->best_encoder)
12143 			continue;
12144 
12145 		encoder = to_intel_encoder(connector_state->best_encoder);
12146 
12147 		WARN_ON(!connector_state->crtc);
12148 
12149 		switch (encoder->type) {
12150 			unsigned int port_mask;
12151 		case INTEL_OUTPUT_UNKNOWN:
12152 			if (WARN_ON(!HAS_DDI(dev)))
12153 				break;
12154 		case INTEL_OUTPUT_DISPLAYPORT:
12155 		case INTEL_OUTPUT_HDMI:
12156 		case INTEL_OUTPUT_EDP:
12157 			port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12158 
12159 			/* the same port mustn't appear more than once */
12160 			if (used_ports & port_mask)
12161 				return false;
12162 
12163 			used_ports |= port_mask;
12164 		default:
12165 			break;
12166 		}
12167 	}
12168 
12169 	return true;
12170 }
12171 
12172 static void
12173 clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12174 {
12175 	struct drm_crtc_state tmp_state;
12176 	struct intel_crtc_scaler_state scaler_state;
12177 	struct intel_dpll_hw_state dpll_hw_state;
12178 	enum intel_dpll_id shared_dpll;
12179 	uint32_t ddi_pll_sel;
12180 	bool force_thru;
12181 
12182 	/* FIXME: before the switch to atomic started, a new pipe_config was
12183 	 * kzalloc'd. Code that depends on any field being zero should be
12184 	 * fixed, so that the crtc_state can be safely duplicated. For now,
12185 	 * only fields that are know to not cause problems are preserved. */
12186 
12187 	tmp_state = crtc_state->base;
12188 	scaler_state = crtc_state->scaler_state;
12189 	shared_dpll = crtc_state->shared_dpll;
12190 	dpll_hw_state = crtc_state->dpll_hw_state;
12191 	ddi_pll_sel = crtc_state->ddi_pll_sel;
12192 	force_thru = crtc_state->pch_pfit.force_thru;
12193 
12194 	memset(crtc_state, 0, sizeof *crtc_state);
12195 
12196 	crtc_state->base = tmp_state;
12197 	crtc_state->scaler_state = scaler_state;
12198 	crtc_state->shared_dpll = shared_dpll;
12199 	crtc_state->dpll_hw_state = dpll_hw_state;
12200 	crtc_state->ddi_pll_sel = ddi_pll_sel;
12201 	crtc_state->pch_pfit.force_thru = force_thru;
12202 }
12203 
12204 static int
12205 intel_modeset_pipe_config(struct drm_crtc *crtc,
12206 			  struct intel_crtc_state *pipe_config)
12207 {
12208 	struct drm_atomic_state *state = pipe_config->base.state;
12209 	struct intel_encoder *encoder;
12210 	struct drm_connector *connector;
12211 	struct drm_connector_state *connector_state;
12212 	int base_bpp, ret = -EINVAL;
12213 	int i;
12214 	bool retry = true;
12215 
12216 	clear_intel_crtc_state(pipe_config);
12217 
12218 	pipe_config->cpu_transcoder =
12219 		(enum transcoder) to_intel_crtc(crtc)->pipe;
12220 
12221 	/*
12222 	 * Sanitize sync polarity flags based on requested ones. If neither
12223 	 * positive or negative polarity is requested, treat this as meaning
12224 	 * negative polarity.
12225 	 */
12226 	if (!(pipe_config->base.adjusted_mode.flags &
12227 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
12228 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
12229 
12230 	if (!(pipe_config->base.adjusted_mode.flags &
12231 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
12232 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
12233 
12234 	base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12235 					     pipe_config);
12236 	if (base_bpp < 0)
12237 		goto fail;
12238 
12239 	/*
12240 	 * Determine the real pipe dimensions. Note that stereo modes can
12241 	 * increase the actual pipe size due to the frame doubling and
12242 	 * insertion of additional space for blanks between the frame. This
12243 	 * is stored in the crtc timings. We use the requested mode to do this
12244 	 * computation to clearly distinguish it from the adjusted mode, which
12245 	 * can be changed by the connectors in the below retry loop.
12246 	 */
12247 	drm_crtc_get_hv_timing(&pipe_config->base.mode,
12248 			       &pipe_config->pipe_src_w,
12249 			       &pipe_config->pipe_src_h);
12250 
12251 encoder_retry:
12252 	/* Ensure the port clock defaults are reset when retrying. */
12253 	pipe_config->port_clock = 0;
12254 	pipe_config->pixel_multiplier = 1;
12255 
12256 	/* Fill in default crtc timings, allow encoders to overwrite them. */
12257 	drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12258 			      CRTC_STEREO_DOUBLE);
12259 
12260 	/* Pass our mode to the connectors and the CRTC to give them a chance to
12261 	 * adjust it according to limitations or connector properties, and also
12262 	 * a chance to reject the mode entirely.
12263 	 */
12264 	for_each_connector_in_state(state, connector, connector_state, i) {
12265 		if (connector_state->crtc != crtc)
12266 			continue;
12267 
12268 		encoder = to_intel_encoder(connector_state->best_encoder);
12269 
12270 		if (!(encoder->compute_config(encoder, pipe_config))) {
12271 			DRM_DEBUG_KMS("Encoder config failure\n");
12272 			goto fail;
12273 		}
12274 	}
12275 
12276 	/* Set default port clock if not overwritten by the encoder. Needs to be
12277 	 * done afterwards in case the encoder adjusts the mode. */
12278 	if (!pipe_config->port_clock)
12279 		pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
12280 			* pipe_config->pixel_multiplier;
12281 
12282 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
12283 	if (ret < 0) {
12284 		DRM_DEBUG_KMS("CRTC fixup failed\n");
12285 		goto fail;
12286 	}
12287 
12288 	if (ret == RETRY) {
12289 		if (WARN(!retry, "loop in pipe configuration computation\n")) {
12290 			ret = -EINVAL;
12291 			goto fail;
12292 		}
12293 
12294 		DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12295 		retry = false;
12296 		goto encoder_retry;
12297 	}
12298 
12299 	/* Dithering seems to not pass-through bits correctly when it should, so
12300 	 * only enable it on 6bpc panels. */
12301 	pipe_config->dither = pipe_config->pipe_bpp == 6*3;
12302 	DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
12303 		      base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
12304 
12305 fail:
12306 	return ret;
12307 }
12308 
12309 static void
12310 intel_modeset_update_crtc_state(struct drm_atomic_state *state)
12311 {
12312 	struct drm_crtc *crtc;
12313 	struct drm_crtc_state *crtc_state;
12314 	int i;
12315 
12316 	/* Double check state. */
12317 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
12318 		to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
12319 
12320 		/* Update hwmode for vblank functions */
12321 		if (crtc->state->active)
12322 			crtc->hwmode = crtc->state->adjusted_mode;
12323 		else
12324 			crtc->hwmode.crtc_clock = 0;
12325 	}
12326 }
12327 
12328 static bool intel_fuzzy_clock_check(int clock1, int clock2)
12329 {
12330 	int diff;
12331 
12332 	if (clock1 == clock2)
12333 		return true;
12334 
12335 	if (!clock1 || !clock2)
12336 		return false;
12337 
12338 	diff = abs(clock1 - clock2);
12339 
12340 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12341 		return true;
12342 
12343 	return false;
12344 }
12345 
12346 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12347 	list_for_each_entry((intel_crtc), \
12348 			    &(dev)->mode_config.crtc_list, \
12349 			    base.head) \
12350 		if (mask & (1 <<(intel_crtc)->pipe))
12351 
12352 static bool
12353 intel_compare_m_n(unsigned int m, unsigned int n,
12354 		  unsigned int m2, unsigned int n2,
12355 		  bool exact)
12356 {
12357 	if (m == m2 && n == n2)
12358 		return true;
12359 
12360 	if (exact || !m || !n || !m2 || !n2)
12361 		return false;
12362 
12363 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12364 
12365 	if (m > m2) {
12366 		while (m > m2) {
12367 			m2 <<= 1;
12368 			n2 <<= 1;
12369 		}
12370 	} else if (m < m2) {
12371 		while (m < m2) {
12372 			m <<= 1;
12373 			n <<= 1;
12374 		}
12375 	}
12376 
12377 	return m == m2 && n == n2;
12378 }
12379 
12380 static bool
12381 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12382 		       struct intel_link_m_n *m2_n2,
12383 		       bool adjust)
12384 {
12385 	if (m_n->tu == m2_n2->tu &&
12386 	    intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12387 			      m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12388 	    intel_compare_m_n(m_n->link_m, m_n->link_n,
12389 			      m2_n2->link_m, m2_n2->link_n, !adjust)) {
12390 		if (adjust)
12391 			*m2_n2 = *m_n;
12392 
12393 		return true;
12394 	}
12395 
12396 	return false;
12397 }
12398 
12399 static bool
12400 intel_pipe_config_compare(struct drm_device *dev,
12401 			  struct intel_crtc_state *current_config,
12402 			  struct intel_crtc_state *pipe_config,
12403 			  bool adjust)
12404 {
12405 	bool ret = true;
12406 
12407 #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12408 	do { \
12409 		if (!adjust) \
12410 			DRM_ERROR(fmt, ##__VA_ARGS__); \
12411 		else \
12412 			DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12413 	} while (0)
12414 
12415 #define PIPE_CONF_CHECK_X(name)	\
12416 	if (current_config->name != pipe_config->name) { \
12417 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12418 			  "(expected 0x%08x, found 0x%08x)\n", \
12419 			  current_config->name, \
12420 			  pipe_config->name); \
12421 		ret = false; \
12422 	}
12423 
12424 #define PIPE_CONF_CHECK_I(name)	\
12425 	if (current_config->name != pipe_config->name) { \
12426 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12427 			  "(expected %i, found %i)\n", \
12428 			  current_config->name, \
12429 			  pipe_config->name); \
12430 		ret = false; \
12431 	}
12432 
12433 #define PIPE_CONF_CHECK_M_N(name) \
12434 	if (!intel_compare_link_m_n(&current_config->name, \
12435 				    &pipe_config->name,\
12436 				    adjust)) { \
12437 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12438 			  "(expected tu %i gmch %i/%i link %i/%i, " \
12439 			  "found tu %i, gmch %i/%i link %i/%i)\n", \
12440 			  current_config->name.tu, \
12441 			  current_config->name.gmch_m, \
12442 			  current_config->name.gmch_n, \
12443 			  current_config->name.link_m, \
12444 			  current_config->name.link_n, \
12445 			  pipe_config->name.tu, \
12446 			  pipe_config->name.gmch_m, \
12447 			  pipe_config->name.gmch_n, \
12448 			  pipe_config->name.link_m, \
12449 			  pipe_config->name.link_n); \
12450 		ret = false; \
12451 	}
12452 
12453 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12454 	if (!intel_compare_link_m_n(&current_config->name, \
12455 				    &pipe_config->name, adjust) && \
12456 	    !intel_compare_link_m_n(&current_config->alt_name, \
12457 				    &pipe_config->name, adjust)) { \
12458 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12459 			  "(expected tu %i gmch %i/%i link %i/%i, " \
12460 			  "or tu %i gmch %i/%i link %i/%i, " \
12461 			  "found tu %i, gmch %i/%i link %i/%i)\n", \
12462 			  current_config->name.tu, \
12463 			  current_config->name.gmch_m, \
12464 			  current_config->name.gmch_n, \
12465 			  current_config->name.link_m, \
12466 			  current_config->name.link_n, \
12467 			  current_config->alt_name.tu, \
12468 			  current_config->alt_name.gmch_m, \
12469 			  current_config->alt_name.gmch_n, \
12470 			  current_config->alt_name.link_m, \
12471 			  current_config->alt_name.link_n, \
12472 			  pipe_config->name.tu, \
12473 			  pipe_config->name.gmch_m, \
12474 			  pipe_config->name.gmch_n, \
12475 			  pipe_config->name.link_m, \
12476 			  pipe_config->name.link_n); \
12477 		ret = false; \
12478 	}
12479 
12480 /* This is required for BDW+ where there is only one set of registers for
12481  * switching between high and low RR.
12482  * This macro can be used whenever a comparison has to be made between one
12483  * hw state and multiple sw state variables.
12484  */
12485 #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12486 	if ((current_config->name != pipe_config->name) && \
12487 		(current_config->alt_name != pipe_config->name)) { \
12488 			INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12489 				  "(expected %i or %i, found %i)\n", \
12490 				  current_config->name, \
12491 				  current_config->alt_name, \
12492 				  pipe_config->name); \
12493 			ret = false; \
12494 	}
12495 
12496 #define PIPE_CONF_CHECK_FLAGS(name, mask)	\
12497 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
12498 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
12499 			  "(expected %i, found %i)\n", \
12500 			  current_config->name & (mask), \
12501 			  pipe_config->name & (mask)); \
12502 		ret = false; \
12503 	}
12504 
12505 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12506 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12507 		INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12508 			  "(expected %i, found %i)\n", \
12509 			  current_config->name, \
12510 			  pipe_config->name); \
12511 		ret = false; \
12512 	}
12513 
12514 #define PIPE_CONF_QUIRK(quirk)	\
12515 	((current_config->quirks | pipe_config->quirks) & (quirk))
12516 
12517 	PIPE_CONF_CHECK_I(cpu_transcoder);
12518 
12519 	PIPE_CONF_CHECK_I(has_pch_encoder);
12520 	PIPE_CONF_CHECK_I(fdi_lanes);
12521 	PIPE_CONF_CHECK_M_N(fdi_m_n);
12522 
12523 	PIPE_CONF_CHECK_I(has_dp_encoder);
12524 	PIPE_CONF_CHECK_I(lane_count);
12525 
12526 	if (INTEL_INFO(dev)->gen < 8) {
12527 		PIPE_CONF_CHECK_M_N(dp_m_n);
12528 
12529 		if (current_config->has_drrs)
12530 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
12531 	} else
12532 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
12533 
12534 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12535 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12536 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12537 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12538 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12539 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
12540 
12541 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12542 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12543 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12544 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12545 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12546 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
12547 
12548 	PIPE_CONF_CHECK_I(pixel_multiplier);
12549 	PIPE_CONF_CHECK_I(has_hdmi_sink);
12550 	if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12551 	    IS_VALLEYVIEW(dev))
12552 		PIPE_CONF_CHECK_I(limited_color_range);
12553 	PIPE_CONF_CHECK_I(has_infoframe);
12554 
12555 	PIPE_CONF_CHECK_I(has_audio);
12556 
12557 	PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12558 			      DRM_MODE_FLAG_INTERLACE);
12559 
12560 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
12561 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12562 				      DRM_MODE_FLAG_PHSYNC);
12563 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12564 				      DRM_MODE_FLAG_NHSYNC);
12565 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12566 				      DRM_MODE_FLAG_PVSYNC);
12567 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12568 				      DRM_MODE_FLAG_NVSYNC);
12569 	}
12570 
12571 	PIPE_CONF_CHECK_X(gmch_pfit.control);
12572 	/* pfit ratios are autocomputed by the hw on gen4+ */
12573 	if (INTEL_INFO(dev)->gen < 4)
12574 		PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12575 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
12576 
12577 	if (!adjust) {
12578 		PIPE_CONF_CHECK_I(pipe_src_w);
12579 		PIPE_CONF_CHECK_I(pipe_src_h);
12580 
12581 		PIPE_CONF_CHECK_I(pch_pfit.enabled);
12582 		if (current_config->pch_pfit.enabled) {
12583 			PIPE_CONF_CHECK_X(pch_pfit.pos);
12584 			PIPE_CONF_CHECK_X(pch_pfit.size);
12585 		}
12586 
12587 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12588 	}
12589 
12590 	/* BDW+ don't expose a synchronous way to read the state */
12591 	if (IS_HASWELL(dev))
12592 		PIPE_CONF_CHECK_I(ips_enabled);
12593 
12594 	PIPE_CONF_CHECK_I(double_wide);
12595 
12596 	PIPE_CONF_CHECK_X(ddi_pll_sel);
12597 
12598 	PIPE_CONF_CHECK_I(shared_dpll);
12599 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
12600 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
12601 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12602 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
12603 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
12604 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
12605 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12606 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12607 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
12608 
12609 	if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12610 		PIPE_CONF_CHECK_I(pipe_bpp);
12611 
12612 	PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
12613 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
12614 
12615 #undef PIPE_CONF_CHECK_X
12616 #undef PIPE_CONF_CHECK_I
12617 #undef PIPE_CONF_CHECK_I_ALT
12618 #undef PIPE_CONF_CHECK_FLAGS
12619 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
12620 #undef PIPE_CONF_QUIRK
12621 #undef INTEL_ERR_OR_DBG_KMS
12622 
12623 	return ret;
12624 }
12625 
12626 static void check_wm_state(struct drm_device *dev)
12627 {
12628 	struct drm_i915_private *dev_priv = dev->dev_private;
12629 	struct skl_ddb_allocation hw_ddb, *sw_ddb;
12630 	struct intel_crtc *intel_crtc;
12631 	int plane;
12632 
12633 	if (INTEL_INFO(dev)->gen < 9)
12634 		return;
12635 
12636 	skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12637 	sw_ddb = &dev_priv->wm.skl_hw.ddb;
12638 
12639 	for_each_intel_crtc(dev, intel_crtc) {
12640 		struct skl_ddb_entry *hw_entry, *sw_entry;
12641 		const enum i915_pipe pipe = intel_crtc->pipe;
12642 
12643 		if (!intel_crtc->active)
12644 			continue;
12645 
12646 		/* planes */
12647 		for_each_plane(dev_priv, pipe, plane) {
12648 			hw_entry = &hw_ddb.plane[pipe][plane];
12649 			sw_entry = &sw_ddb->plane[pipe][plane];
12650 
12651 			if (skl_ddb_entry_equal(hw_entry, sw_entry))
12652 				continue;
12653 
12654 			DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12655 				  "(expected (%u,%u), found (%u,%u))\n",
12656 				  pipe_name(pipe), plane + 1,
12657 				  sw_entry->start, sw_entry->end,
12658 				  hw_entry->start, hw_entry->end);
12659 		}
12660 
12661 		/* cursor */
12662 		hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
12663 		sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
12664 
12665 		if (skl_ddb_entry_equal(hw_entry, sw_entry))
12666 			continue;
12667 
12668 		DRM_ERROR("mismatch in DDB state pipe %c cursor "
12669 			  "(expected (%u,%u), found (%u,%u))\n",
12670 			  pipe_name(pipe),
12671 			  sw_entry->start, sw_entry->end,
12672 			  hw_entry->start, hw_entry->end);
12673 	}
12674 }
12675 
12676 static void
12677 check_connector_state(struct drm_device *dev,
12678 		      struct drm_atomic_state *old_state)
12679 {
12680 	struct drm_connector_state *old_conn_state;
12681 	struct drm_connector *connector;
12682 	int i;
12683 
12684 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12685 		struct drm_encoder *encoder = connector->encoder;
12686 		struct drm_connector_state *state = connector->state;
12687 
12688 		/* This also checks the encoder/connector hw state with the
12689 		 * ->get_hw_state callbacks. */
12690 		intel_connector_check_state(to_intel_connector(connector));
12691 
12692 		I915_STATE_WARN(state->best_encoder != encoder,
12693 		     "connector's atomic encoder doesn't match legacy encoder\n");
12694 	}
12695 }
12696 
12697 static void
12698 check_encoder_state(struct drm_device *dev)
12699 {
12700 	struct intel_encoder *encoder;
12701 	struct intel_connector *connector;
12702 
12703 	for_each_intel_encoder(dev, encoder) {
12704 		bool enabled = false;
12705 		enum i915_pipe pipe;
12706 
12707 		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12708 			      encoder->base.base.id,
12709 			      encoder->base.name);
12710 
12711 		for_each_intel_connector(dev, connector) {
12712 			if (connector->base.state->best_encoder != &encoder->base)
12713 				continue;
12714 			enabled = true;
12715 
12716 			I915_STATE_WARN(connector->base.state->crtc !=
12717 					encoder->base.crtc,
12718 			     "connector's crtc doesn't match encoder crtc\n");
12719 		}
12720 
12721 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
12722 		     "encoder's enabled state mismatch "
12723 		     "(expected %i, found %i)\n",
12724 		     !!encoder->base.crtc, enabled);
12725 
12726 		if (!encoder->base.crtc) {
12727 			bool active;
12728 
12729 			active = encoder->get_hw_state(encoder, &pipe);
12730 			I915_STATE_WARN(active,
12731 			     "encoder detached but still enabled on pipe %c.\n",
12732 			     pipe_name(pipe));
12733 		}
12734 	}
12735 }
12736 
12737 static void
12738 check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
12739 {
12740 	struct drm_i915_private *dev_priv = dev->dev_private;
12741 	struct intel_encoder *encoder;
12742 	struct drm_crtc_state *old_crtc_state;
12743 	struct drm_crtc *crtc;
12744 	int i;
12745 
12746 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12747 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12748 		struct intel_crtc_state *pipe_config, *sw_config;
12749 		bool active;
12750 
12751 		if (!needs_modeset(crtc->state) &&
12752 		    !to_intel_crtc_state(crtc->state)->update_pipe)
12753 			continue;
12754 
12755 		__drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12756 		pipe_config = to_intel_crtc_state(old_crtc_state);
12757 		memset(pipe_config, 0, sizeof(*pipe_config));
12758 		pipe_config->base.crtc = crtc;
12759 		pipe_config->base.state = old_state;
12760 
12761 		DRM_DEBUG_KMS("[CRTC:%d]\n",
12762 			      crtc->base.id);
12763 
12764 		active = dev_priv->display.get_pipe_config(intel_crtc,
12765 							   pipe_config);
12766 
12767 		/* hw state is inconsistent with the pipe quirk */
12768 		if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12769 		    (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12770 			active = crtc->state->active;
12771 
12772 		I915_STATE_WARN(crtc->state->active != active,
12773 		     "crtc active state doesn't match with hw state "
12774 		     "(expected %i, found %i)\n", crtc->state->active, active);
12775 
12776 		I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12777 		     "transitional active state does not match atomic hw state "
12778 		     "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12779 
12780 		for_each_encoder_on_crtc(dev, crtc, encoder) {
12781 			enum i915_pipe pipe;
12782 
12783 			active = encoder->get_hw_state(encoder, &pipe);
12784 			I915_STATE_WARN(active != crtc->state->active,
12785 				"[ENCODER:%i] active %i with crtc active %i\n",
12786 				encoder->base.base.id, active, crtc->state->active);
12787 
12788 			I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12789 					"Encoder connected to wrong pipe %c\n",
12790 					pipe_name(pipe));
12791 
12792 			if (active)
12793 				encoder->get_config(encoder, pipe_config);
12794 		}
12795 
12796 		if (!crtc->state->active)
12797 			continue;
12798 
12799 		sw_config = to_intel_crtc_state(crtc->state);
12800 		if (!intel_pipe_config_compare(dev, sw_config,
12801 					       pipe_config, false)) {
12802 			I915_STATE_WARN(1, "pipe state doesn't match!\n");
12803 			intel_dump_pipe_config(intel_crtc, pipe_config,
12804 					       "[hw state]");
12805 			intel_dump_pipe_config(intel_crtc, sw_config,
12806 					       "[sw state]");
12807 		}
12808 	}
12809 }
12810 
12811 static void
12812 check_shared_dpll_state(struct drm_device *dev)
12813 {
12814 	struct drm_i915_private *dev_priv = dev->dev_private;
12815 	struct intel_crtc *crtc;
12816 	struct intel_dpll_hw_state dpll_hw_state;
12817 	int i;
12818 
12819 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12820 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12821 		int enabled_crtcs = 0, active_crtcs = 0;
12822 		bool active;
12823 
12824 		memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12825 
12826 		DRM_DEBUG_KMS("%s\n", pll->name);
12827 
12828 		active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12829 
12830 		I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
12831 		     "more active pll users than references: %i vs %i\n",
12832 		     pll->active, hweight32(pll->config.crtc_mask));
12833 		I915_STATE_WARN(pll->active && !pll->on,
12834 		     "pll in active use but not on in sw tracking\n");
12835 		I915_STATE_WARN(pll->on && !pll->active,
12836 		     "pll in on but not on in use in sw tracking\n");
12837 		I915_STATE_WARN(pll->on != active,
12838 		     "pll on state mismatch (expected %i, found %i)\n",
12839 		     pll->on, active);
12840 
12841 		for_each_intel_crtc(dev, crtc) {
12842 			if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
12843 				enabled_crtcs++;
12844 			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12845 				active_crtcs++;
12846 		}
12847 		I915_STATE_WARN(pll->active != active_crtcs,
12848 		     "pll active crtcs mismatch (expected %i, found %i)\n",
12849 		     pll->active, active_crtcs);
12850 		I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
12851 		     "pll enabled crtcs mismatch (expected %i, found %i)\n",
12852 		     hweight32(pll->config.crtc_mask), enabled_crtcs);
12853 
12854 		I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
12855 				       sizeof(dpll_hw_state)),
12856 		     "pll hw state mismatch\n");
12857 	}
12858 }
12859 
12860 static void
12861 intel_modeset_check_state(struct drm_device *dev,
12862 			  struct drm_atomic_state *old_state)
12863 {
12864 	check_wm_state(dev);
12865 	check_connector_state(dev, old_state);
12866 	check_encoder_state(dev);
12867 	check_crtc_state(dev, old_state);
12868 	check_shared_dpll_state(dev);
12869 }
12870 
12871 void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
12872 				     int dotclock)
12873 {
12874 	/*
12875 	 * FDI already provided one idea for the dotclock.
12876 	 * Yell if the encoder disagrees.
12877 	 */
12878 	WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
12879 	     "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12880 	     pipe_config->base.adjusted_mode.crtc_clock, dotclock);
12881 }
12882 
12883 static void update_scanline_offset(struct intel_crtc *crtc)
12884 {
12885 	struct drm_device *dev = crtc->base.dev;
12886 
12887 	/*
12888 	 * The scanline counter increments at the leading edge of hsync.
12889 	 *
12890 	 * On most platforms it starts counting from vtotal-1 on the
12891 	 * first active line. That means the scanline counter value is
12892 	 * always one less than what we would expect. Ie. just after
12893 	 * start of vblank, which also occurs at start of hsync (on the
12894 	 * last active line), the scanline counter will read vblank_start-1.
12895 	 *
12896 	 * On gen2 the scanline counter starts counting from 1 instead
12897 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12898 	 * to keep the value positive), instead of adding one.
12899 	 *
12900 	 * On HSW+ the behaviour of the scanline counter depends on the output
12901 	 * type. For DP ports it behaves like most other platforms, but on HDMI
12902 	 * there's an extra 1 line difference. So we need to add two instead of
12903 	 * one to the value.
12904 	 */
12905 	if (IS_GEN2(dev)) {
12906 		const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
12907 		int vtotal;
12908 
12909 		vtotal = adjusted_mode->crtc_vtotal;
12910 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
12911 			vtotal /= 2;
12912 
12913 		crtc->scanline_offset = vtotal - 1;
12914 	} else if (HAS_DDI(dev) &&
12915 		   intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
12916 		crtc->scanline_offset = 2;
12917 	} else
12918 		crtc->scanline_offset = 1;
12919 }
12920 
12921 static void intel_modeset_clear_plls(struct drm_atomic_state *state)
12922 {
12923 	struct drm_device *dev = state->dev;
12924 	struct drm_i915_private *dev_priv = to_i915(dev);
12925 	struct intel_shared_dpll_config *shared_dpll = NULL;
12926 	struct intel_crtc *intel_crtc;
12927 	struct intel_crtc_state *intel_crtc_state;
12928 	struct drm_crtc *crtc;
12929 	struct drm_crtc_state *crtc_state;
12930 	int i;
12931 
12932 	if (!dev_priv->display.crtc_compute_clock)
12933 		return;
12934 
12935 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
12936 		int dpll;
12937 
12938 		intel_crtc = to_intel_crtc(crtc);
12939 		intel_crtc_state = to_intel_crtc_state(crtc_state);
12940 		dpll = intel_crtc_state->shared_dpll;
12941 
12942 		if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
12943 			continue;
12944 
12945 		intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
12946 
12947 		if (!shared_dpll)
12948 			shared_dpll = intel_atomic_get_shared_dpll_state(state);
12949 
12950 		shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
12951 	}
12952 }
12953 
12954 /*
12955  * This implements the workaround described in the "notes" section of the mode
12956  * set sequence documentation. When going from no pipes or single pipe to
12957  * multiple pipes, and planes are enabled after the pipe, we need to wait at
12958  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12959  */
12960 static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12961 {
12962 	struct drm_crtc_state *crtc_state;
12963 	struct intel_crtc *intel_crtc;
12964 	struct drm_crtc *crtc;
12965 	struct intel_crtc_state *first_crtc_state = NULL;
12966 	struct intel_crtc_state *other_crtc_state = NULL;
12967 	enum i915_pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12968 	int i;
12969 
12970 	/* look at all crtc's that are going to be enabled in during modeset */
12971 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
12972 		intel_crtc = to_intel_crtc(crtc);
12973 
12974 		if (!crtc_state->active || !needs_modeset(crtc_state))
12975 			continue;
12976 
12977 		if (first_crtc_state) {
12978 			other_crtc_state = to_intel_crtc_state(crtc_state);
12979 			break;
12980 		} else {
12981 			first_crtc_state = to_intel_crtc_state(crtc_state);
12982 			first_pipe = intel_crtc->pipe;
12983 		}
12984 	}
12985 
12986 	/* No workaround needed? */
12987 	if (!first_crtc_state)
12988 		return 0;
12989 
12990 	/* w/a possibly needed, check how many crtc's are already enabled. */
12991 	for_each_intel_crtc(state->dev, intel_crtc) {
12992 		struct intel_crtc_state *pipe_config;
12993 
12994 		pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12995 		if (IS_ERR(pipe_config))
12996 			return PTR_ERR(pipe_config);
12997 
12998 		pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12999 
13000 		if (!pipe_config->base.active ||
13001 		    needs_modeset(&pipe_config->base))
13002 			continue;
13003 
13004 		/* 2 or more enabled crtcs means no need for w/a */
13005 		if (enabled_pipe != INVALID_PIPE)
13006 			return 0;
13007 
13008 		enabled_pipe = intel_crtc->pipe;
13009 	}
13010 
13011 	if (enabled_pipe != INVALID_PIPE)
13012 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13013 	else if (other_crtc_state)
13014 		other_crtc_state->hsw_workaround_pipe = first_pipe;
13015 
13016 	return 0;
13017 }
13018 
13019 static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13020 {
13021 	struct drm_crtc *crtc;
13022 	struct drm_crtc_state *crtc_state;
13023 	int ret = 0;
13024 
13025 	/* add all active pipes to the state */
13026 	for_each_crtc(state->dev, crtc) {
13027 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
13028 		if (IS_ERR(crtc_state))
13029 			return PTR_ERR(crtc_state);
13030 
13031 		if (!crtc_state->active || needs_modeset(crtc_state))
13032 			continue;
13033 
13034 		crtc_state->mode_changed = true;
13035 
13036 		ret = drm_atomic_add_affected_connectors(state, crtc);
13037 		if (ret)
13038 			break;
13039 
13040 		ret = drm_atomic_add_affected_planes(state, crtc);
13041 		if (ret)
13042 			break;
13043 	}
13044 
13045 	return ret;
13046 }
13047 
13048 static int intel_modeset_checks(struct drm_atomic_state *state)
13049 {
13050 	struct drm_device *dev = state->dev;
13051 	struct drm_i915_private *dev_priv = dev->dev_private;
13052 	int ret;
13053 
13054 	if (!check_digital_port_conflicts(state)) {
13055 		DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13056 		return -EINVAL;
13057 	}
13058 
13059 	/*
13060 	 * See if the config requires any additional preparation, e.g.
13061 	 * to adjust global state with pipes off.  We need to do this
13062 	 * here so we can get the modeset_pipe updated config for the new
13063 	 * mode set on this crtc.  For other crtcs we need to use the
13064 	 * adjusted_mode bits in the crtc directly.
13065 	 */
13066 	if (dev_priv->display.modeset_calc_cdclk) {
13067 		unsigned int cdclk;
13068 
13069 		ret = dev_priv->display.modeset_calc_cdclk(state);
13070 
13071 		cdclk = to_intel_atomic_state(state)->cdclk;
13072 		if (!ret && cdclk != dev_priv->cdclk_freq)
13073 			ret = intel_modeset_all_pipes(state);
13074 
13075 		if (ret < 0)
13076 			return ret;
13077 	} else
13078 		to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
13079 
13080 	intel_modeset_clear_plls(state);
13081 
13082 	if (IS_HASWELL(dev))
13083 		return haswell_mode_set_planes_workaround(state);
13084 
13085 	return 0;
13086 }
13087 
13088 /**
13089  * intel_atomic_check - validate state object
13090  * @dev: drm device
13091  * @state: state to validate
13092  */
13093 static int intel_atomic_check(struct drm_device *dev,
13094 			      struct drm_atomic_state *state)
13095 {
13096 	struct drm_crtc *crtc;
13097 	struct drm_crtc_state *crtc_state;
13098 	int ret, i;
13099 	bool any_ms = false;
13100 
13101 	ret = drm_atomic_helper_check_modeset(dev, state);
13102 	if (ret)
13103 		return ret;
13104 
13105 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
13106 		struct intel_crtc_state *pipe_config =
13107 			to_intel_crtc_state(crtc_state);
13108 
13109 		memset(&to_intel_crtc(crtc)->atomic, 0,
13110 		       sizeof(struct intel_crtc_atomic_commit));
13111 
13112 		/* Catch I915_MODE_FLAG_INHERITED */
13113 		if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
13114 			crtc_state->mode_changed = true;
13115 
13116 		if (!crtc_state->enable) {
13117 			if (needs_modeset(crtc_state))
13118 				any_ms = true;
13119 			continue;
13120 		}
13121 
13122 		if (!needs_modeset(crtc_state))
13123 			continue;
13124 
13125 		/* FIXME: For only active_changed we shouldn't need to do any
13126 		 * state recomputation at all. */
13127 
13128 		ret = drm_atomic_add_affected_connectors(state, crtc);
13129 		if (ret)
13130 			return ret;
13131 
13132 		ret = intel_modeset_pipe_config(crtc, pipe_config);
13133 		if (ret)
13134 			return ret;
13135 
13136 		if (i915.fastboot &&
13137 		    intel_pipe_config_compare(state->dev,
13138 					to_intel_crtc_state(crtc->state),
13139 					pipe_config, true)) {
13140 			crtc_state->mode_changed = false;
13141 			to_intel_crtc_state(crtc_state)->update_pipe = true;
13142 		}
13143 
13144 		if (needs_modeset(crtc_state)) {
13145 			any_ms = true;
13146 
13147 			ret = drm_atomic_add_affected_planes(state, crtc);
13148 			if (ret)
13149 				return ret;
13150 		}
13151 
13152 		intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13153 				       needs_modeset(crtc_state) ?
13154 				       "[modeset]" : "[fastset]");
13155 	}
13156 
13157 	if (any_ms) {
13158 		ret = intel_modeset_checks(state);
13159 
13160 		if (ret)
13161 			return ret;
13162 	} else
13163 		to_intel_atomic_state(state)->cdclk =
13164 			to_i915(state->dev)->cdclk_freq;
13165 
13166 	return drm_atomic_helper_check_planes(state->dev, state);
13167 }
13168 
13169 /**
13170  * intel_atomic_commit - commit validated state object
13171  * @dev: DRM device
13172  * @state: the top-level driver state object
13173  * @async: asynchronous commit
13174  *
13175  * This function commits a top-level state object that has been validated
13176  * with drm_atomic_helper_check().
13177  *
13178  * FIXME:  Atomic modeset support for i915 is not yet complete.  At the moment
13179  * we can only handle plane-related operations and do not yet support
13180  * asynchronous commit.
13181  *
13182  * RETURNS
13183  * Zero for success or -errno.
13184  */
13185 static int intel_atomic_commit(struct drm_device *dev,
13186 			       struct drm_atomic_state *state,
13187 			       bool async)
13188 {
13189 	struct drm_i915_private *dev_priv = dev->dev_private;
13190 	struct drm_crtc *crtc;
13191 	struct drm_crtc_state *crtc_state;
13192 	int ret = 0;
13193 	int i;
13194 	bool any_ms = false;
13195 
13196 	if (async) {
13197 		DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13198 		return -EINVAL;
13199 	}
13200 
13201 	ret = drm_atomic_helper_prepare_planes(dev, state);
13202 	if (ret)
13203 		return ret;
13204 
13205 	drm_atomic_helper_swap_state(dev, state);
13206 
13207 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
13208 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13209 
13210 		if (!needs_modeset(crtc->state))
13211 			continue;
13212 
13213 		any_ms = true;
13214 		intel_pre_plane_update(intel_crtc);
13215 
13216 		if (crtc_state->active) {
13217 			intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13218 			dev_priv->display.crtc_disable(crtc);
13219 			intel_crtc->active = false;
13220 			intel_disable_shared_dpll(intel_crtc);
13221 		}
13222 	}
13223 
13224 	/* Only after disabling all output pipelines that will be changed can we
13225 	 * update the the output configuration. */
13226 	intel_modeset_update_crtc_state(state);
13227 
13228 	if (any_ms) {
13229 		intel_shared_dpll_commit(state);
13230 
13231 		drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
13232 		modeset_update_crtc_power_domains(state);
13233 	}
13234 
13235 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
13236 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
13237 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13238 		bool modeset = needs_modeset(crtc->state);
13239 		bool update_pipe = !modeset &&
13240 			to_intel_crtc_state(crtc->state)->update_pipe;
13241 		unsigned long put_domains = 0;
13242 
13243 		if (modeset && crtc->state->active) {
13244 			update_scanline_offset(to_intel_crtc(crtc));
13245 			dev_priv->display.crtc_enable(crtc);
13246 		}
13247 
13248 		if (update_pipe) {
13249 			put_domains = modeset_get_crtc_power_domains(crtc);
13250 
13251 			/* make sure intel_modeset_check_state runs */
13252 			any_ms = true;
13253 		}
13254 
13255 		if (!modeset)
13256 			intel_pre_plane_update(intel_crtc);
13257 
13258 		drm_atomic_helper_commit_planes_on_crtc(crtc_state);
13259 
13260 		if (put_domains)
13261 			modeset_put_power_domains(dev_priv, put_domains);
13262 
13263 		intel_post_plane_update(intel_crtc);
13264 	}
13265 
13266 	/* FIXME: add subpixel order */
13267 
13268 	drm_atomic_helper_wait_for_vblanks(dev, state);
13269 	drm_atomic_helper_cleanup_planes(dev, state);
13270 
13271 	if (any_ms)
13272 		intel_modeset_check_state(dev, state);
13273 
13274 	drm_atomic_state_free(state);
13275 
13276 	return 0;
13277 }
13278 
13279 void intel_crtc_restore_mode(struct drm_crtc *crtc)
13280 {
13281 	struct drm_device *dev = crtc->dev;
13282 	struct drm_atomic_state *state;
13283 	struct drm_crtc_state *crtc_state;
13284 	int ret;
13285 
13286 	state = drm_atomic_state_alloc(dev);
13287 	if (!state) {
13288 		DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
13289 			      crtc->base.id);
13290 		return;
13291 	}
13292 
13293 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
13294 
13295 retry:
13296 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
13297 	ret = PTR_ERR_OR_ZERO(crtc_state);
13298 	if (!ret) {
13299 		if (!crtc_state->active)
13300 			goto out;
13301 
13302 		crtc_state->mode_changed = true;
13303 		ret = drm_atomic_commit(state);
13304 	}
13305 
13306 	if (ret == -EDEADLK) {
13307 		drm_atomic_state_clear(state);
13308 		drm_modeset_backoff(state->acquire_ctx);
13309 		goto retry;
13310 	}
13311 
13312 	if (ret)
13313 out:
13314 		drm_atomic_state_free(state);
13315 }
13316 
13317 #undef for_each_intel_crtc_masked
13318 
13319 static const struct drm_crtc_funcs intel_crtc_funcs = {
13320 	.gamma_set = intel_crtc_gamma_set,
13321 	.set_config = drm_atomic_helper_set_config,
13322 	.destroy = intel_crtc_destroy,
13323 	.page_flip = intel_crtc_page_flip,
13324 	.atomic_duplicate_state = intel_crtc_duplicate_state,
13325 	.atomic_destroy_state = intel_crtc_destroy_state,
13326 };
13327 
13328 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13329 				      struct intel_shared_dpll *pll,
13330 				      struct intel_dpll_hw_state *hw_state)
13331 {
13332 	uint32_t val;
13333 
13334 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
13335 		return false;
13336 
13337 	val = I915_READ(PCH_DPLL(pll->id));
13338 	hw_state->dpll = val;
13339 	hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13340 	hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
13341 
13342 	return val & DPLL_VCO_ENABLE;
13343 }
13344 
13345 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13346 				  struct intel_shared_dpll *pll)
13347 {
13348 	I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13349 	I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
13350 }
13351 
13352 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13353 				struct intel_shared_dpll *pll)
13354 {
13355 	/* PCH refclock must be enabled first */
13356 	ibx_assert_pch_refclk_enabled(dev_priv);
13357 
13358 	I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
13359 
13360 	/* Wait for the clocks to stabilize. */
13361 	POSTING_READ(PCH_DPLL(pll->id));
13362 	udelay(150);
13363 
13364 	/* The pixel multiplier can only be updated once the
13365 	 * DPLL is enabled and the clocks are stable.
13366 	 *
13367 	 * So write it again.
13368 	 */
13369 	I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
13370 	POSTING_READ(PCH_DPLL(pll->id));
13371 	udelay(200);
13372 }
13373 
13374 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13375 				 struct intel_shared_dpll *pll)
13376 {
13377 	struct drm_device *dev = dev_priv->dev;
13378 	struct intel_crtc *crtc;
13379 
13380 	/* Make sure no transcoder isn't still depending on us. */
13381 	for_each_intel_crtc(dev, crtc) {
13382 		if (intel_crtc_to_shared_dpll(crtc) == pll)
13383 			assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13384 	}
13385 
13386 	I915_WRITE(PCH_DPLL(pll->id), 0);
13387 	POSTING_READ(PCH_DPLL(pll->id));
13388 	udelay(200);
13389 }
13390 
13391 static char *ibx_pch_dpll_names[] = {
13392 	"PCH DPLL A",
13393 	"PCH DPLL B",
13394 };
13395 
13396 static void ibx_pch_dpll_init(struct drm_device *dev)
13397 {
13398 	struct drm_i915_private *dev_priv = dev->dev_private;
13399 	int i;
13400 
13401 	dev_priv->num_shared_dpll = 2;
13402 
13403 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13404 		dev_priv->shared_dplls[i].id = i;
13405 		dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
13406 		dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
13407 		dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13408 		dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
13409 		dev_priv->shared_dplls[i].get_hw_state =
13410 			ibx_pch_dpll_get_hw_state;
13411 	}
13412 }
13413 
13414 static void intel_shared_dpll_init(struct drm_device *dev)
13415 {
13416 	struct drm_i915_private *dev_priv = dev->dev_private;
13417 
13418 	if (HAS_DDI(dev))
13419 		intel_ddi_pll_init(dev);
13420 	else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13421 		ibx_pch_dpll_init(dev);
13422 	else
13423 		dev_priv->num_shared_dpll = 0;
13424 
13425 	BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
13426 }
13427 
13428 /**
13429  * intel_prepare_plane_fb - Prepare fb for usage on plane
13430  * @plane: drm plane to prepare for
13431  * @fb: framebuffer to prepare for presentation
13432  *
13433  * Prepares a framebuffer for usage on a display plane.  Generally this
13434  * involves pinning the underlying object and updating the frontbuffer tracking
13435  * bits.  Some older platforms need special physical address handling for
13436  * cursor planes.
13437  *
13438  * Returns 0 on success, negative error code on failure.
13439  */
13440 int
13441 intel_prepare_plane_fb(struct drm_plane *plane,
13442 		       const struct drm_plane_state *new_state)
13443 {
13444 	struct drm_device *dev = plane->dev;
13445 	struct drm_framebuffer *fb = new_state->fb;
13446 	struct intel_plane *intel_plane = to_intel_plane(plane);
13447 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13448 	struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13449 	int ret = 0;
13450 
13451 	if (!obj)
13452 		return 0;
13453 
13454 	mutex_lock(&dev->struct_mutex);
13455 
13456 	if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13457 	    INTEL_INFO(dev)->cursor_needs_physical) {
13458 		int align = IS_I830(dev) ? 16 * 1024 : 256;
13459 		ret = i915_gem_object_attach_phys(obj, align);
13460 		if (ret)
13461 			DRM_DEBUG_KMS("failed to attach phys object\n");
13462 	} else {
13463 		ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
13464 	}
13465 
13466 	if (ret == 0)
13467 		i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
13468 
13469 	mutex_unlock(&dev->struct_mutex);
13470 
13471 	return ret;
13472 }
13473 
13474 /**
13475  * intel_cleanup_plane_fb - Cleans up an fb after plane use
13476  * @plane: drm plane to clean up for
13477  * @fb: old framebuffer that was on plane
13478  *
13479  * Cleans up a framebuffer that has just been removed from a plane.
13480  */
13481 void
13482 intel_cleanup_plane_fb(struct drm_plane *plane,
13483 		       const struct drm_plane_state *old_state)
13484 {
13485 	struct drm_device *dev = plane->dev;
13486 	struct drm_i915_gem_object *obj = intel_fb_obj(old_state->fb);
13487 
13488 	if (!obj)
13489 		return;
13490 
13491 	if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13492 	    !INTEL_INFO(dev)->cursor_needs_physical) {
13493 		mutex_lock(&dev->struct_mutex);
13494 		intel_unpin_fb_obj(old_state->fb, old_state);
13495 		mutex_unlock(&dev->struct_mutex);
13496 	}
13497 }
13498 
13499 int
13500 skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13501 {
13502 	int max_scale;
13503 	struct drm_device *dev;
13504 	struct drm_i915_private *dev_priv;
13505 	int crtc_clock, cdclk;
13506 
13507 	if (!intel_crtc || !crtc_state)
13508 		return DRM_PLANE_HELPER_NO_SCALING;
13509 
13510 	dev = intel_crtc->base.dev;
13511 	dev_priv = dev->dev_private;
13512 	crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13513 	cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
13514 
13515 	if (!crtc_clock || !cdclk)
13516 		return DRM_PLANE_HELPER_NO_SCALING;
13517 
13518 	/*
13519 	 * skl max scale is lower of:
13520 	 *    close to 3 but not 3, -1 is for that purpose
13521 	 *            or
13522 	 *    cdclk/crtc_clock
13523 	 */
13524 	max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13525 
13526 	return max_scale;
13527 }
13528 
13529 static int
13530 intel_check_primary_plane(struct drm_plane *plane,
13531 			  struct intel_crtc_state *crtc_state,
13532 			  struct intel_plane_state *state)
13533 {
13534 	struct drm_crtc *crtc = state->base.crtc;
13535 	struct drm_framebuffer *fb = state->base.fb;
13536 	int min_scale = DRM_PLANE_HELPER_NO_SCALING;
13537 	int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13538 	bool can_position = false;
13539 
13540 	/* use scaler when colorkey is not required */
13541 	if (INTEL_INFO(plane->dev)->gen >= 9 &&
13542 	    state->ckey.flags == I915_SET_COLORKEY_NONE) {
13543 		min_scale = 1;
13544 		max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
13545 		can_position = true;
13546 	}
13547 
13548 	return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13549 					     &state->dst, &state->clip,
13550 					     min_scale, max_scale,
13551 					     can_position, true,
13552 					     &state->visible);
13553 }
13554 
13555 static void
13556 intel_commit_primary_plane(struct drm_plane *plane,
13557 			   struct intel_plane_state *state)
13558 {
13559 	struct drm_crtc *crtc = state->base.crtc;
13560 	struct drm_framebuffer *fb = state->base.fb;
13561 	struct drm_device *dev = plane->dev;
13562 	struct drm_i915_private *dev_priv = dev->dev_private;
13563 	struct intel_crtc *intel_crtc;
13564 	struct drm_rect *src = &state->src;
13565 
13566 	crtc = crtc ? crtc : plane->crtc;
13567 	intel_crtc = to_intel_crtc(crtc);
13568 
13569 	plane->fb = fb;
13570 	crtc->x = src->x1 >> 16;
13571 	crtc->y = src->y1 >> 16;
13572 
13573 	if (!crtc->state->active)
13574 		return;
13575 
13576 	dev_priv->display.update_primary_plane(crtc, fb,
13577 					       state->src.x1 >> 16,
13578 					       state->src.y1 >> 16);
13579 }
13580 
13581 static void
13582 intel_disable_primary_plane(struct drm_plane *plane,
13583 			    struct drm_crtc *crtc)
13584 {
13585 	struct drm_device *dev = plane->dev;
13586 	struct drm_i915_private *dev_priv = dev->dev_private;
13587 
13588 	dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13589 }
13590 
13591 static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13592 				    struct drm_crtc_state *old_crtc_state)
13593 {
13594 	struct drm_device *dev = crtc->dev;
13595 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13596 	struct intel_crtc_state *old_intel_state =
13597 		to_intel_crtc_state(old_crtc_state);
13598 	bool modeset = needs_modeset(crtc->state);
13599 
13600 	if (intel_crtc->atomic.update_wm_pre)
13601 		intel_update_watermarks(crtc);
13602 
13603 	/* Perform vblank evasion around commit operation */
13604 	if (crtc->state->active)
13605 		intel_pipe_update_start(intel_crtc);
13606 
13607 	if (modeset)
13608 		return;
13609 
13610 	if (to_intel_crtc_state(crtc->state)->update_pipe)
13611 		intel_update_pipe_config(intel_crtc, old_intel_state);
13612 	else if (INTEL_INFO(dev)->gen >= 9)
13613 		skl_detach_scalers(intel_crtc);
13614 }
13615 
13616 static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13617 				     struct drm_crtc_state *old_crtc_state)
13618 {
13619 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13620 
13621 	if (crtc->state->active)
13622 		intel_pipe_update_end(intel_crtc);
13623 }
13624 
13625 /**
13626  * intel_plane_destroy - destroy a plane
13627  * @plane: plane to destroy
13628  *
13629  * Common destruction function for all types of planes (primary, cursor,
13630  * sprite).
13631  */
13632 void intel_plane_destroy(struct drm_plane *plane)
13633 {
13634 	struct intel_plane *intel_plane = to_intel_plane(plane);
13635 	drm_plane_cleanup(plane);
13636 	kfree(intel_plane);
13637 }
13638 
13639 const struct drm_plane_funcs intel_plane_funcs = {
13640 	.update_plane = drm_atomic_helper_update_plane,
13641 	.disable_plane = drm_atomic_helper_disable_plane,
13642 	.destroy = intel_plane_destroy,
13643 	.set_property = drm_atomic_helper_plane_set_property,
13644 	.atomic_get_property = intel_plane_atomic_get_property,
13645 	.atomic_set_property = intel_plane_atomic_set_property,
13646 	.atomic_duplicate_state = intel_plane_duplicate_state,
13647 	.atomic_destroy_state = intel_plane_destroy_state,
13648 
13649 };
13650 
13651 static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13652 						    int pipe)
13653 {
13654 	struct intel_plane *primary;
13655 	struct intel_plane_state *state;
13656 	const uint32_t *intel_primary_formats;
13657 	unsigned int num_formats;
13658 
13659 	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13660 	if (primary == NULL)
13661 		return NULL;
13662 
13663 	state = intel_create_plane_state(&primary->base);
13664 	if (!state) {
13665 		kfree(primary);
13666 		return NULL;
13667 	}
13668 	primary->base.state = &state->base;
13669 
13670 	primary->can_scale = false;
13671 	primary->max_downscale = 1;
13672 	if (INTEL_INFO(dev)->gen >= 9) {
13673 		primary->can_scale = true;
13674 		state->scaler_id = -1;
13675 	}
13676 	primary->pipe = pipe;
13677 	primary->plane = pipe;
13678 	primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
13679 	primary->check_plane = intel_check_primary_plane;
13680 	primary->commit_plane = intel_commit_primary_plane;
13681 	primary->disable_plane = intel_disable_primary_plane;
13682 	if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13683 		primary->plane = !pipe;
13684 
13685 	if (INTEL_INFO(dev)->gen >= 9) {
13686 		intel_primary_formats = skl_primary_formats;
13687 		num_formats = ARRAY_SIZE(skl_primary_formats);
13688 	} else if (INTEL_INFO(dev)->gen >= 4) {
13689 		intel_primary_formats = i965_primary_formats;
13690 		num_formats = ARRAY_SIZE(i965_primary_formats);
13691 	} else {
13692 		intel_primary_formats = i8xx_primary_formats;
13693 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
13694 	}
13695 
13696 	drm_universal_plane_init(dev, &primary->base, 0,
13697 				 &intel_plane_funcs,
13698 				 intel_primary_formats, num_formats,
13699 				 DRM_PLANE_TYPE_PRIMARY);
13700 
13701 	if (INTEL_INFO(dev)->gen >= 4)
13702 		intel_create_rotation_property(dev, primary);
13703 
13704 	drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13705 
13706 	return &primary->base;
13707 }
13708 
13709 void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13710 {
13711 	if (!dev->mode_config.rotation_property) {
13712 		unsigned long flags = BIT(DRM_ROTATE_0) |
13713 			BIT(DRM_ROTATE_180);
13714 
13715 		if (INTEL_INFO(dev)->gen >= 9)
13716 			flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13717 
13718 		dev->mode_config.rotation_property =
13719 			drm_mode_create_rotation_property(dev, flags);
13720 	}
13721 	if (dev->mode_config.rotation_property)
13722 		drm_object_attach_property(&plane->base.base,
13723 				dev->mode_config.rotation_property,
13724 				plane->base.state->rotation);
13725 }
13726 
13727 static int
13728 intel_check_cursor_plane(struct drm_plane *plane,
13729 			 struct intel_crtc_state *crtc_state,
13730 			 struct intel_plane_state *state)
13731 {
13732 	struct drm_crtc *crtc = crtc_state->base.crtc;
13733 	struct drm_framebuffer *fb = state->base.fb;
13734 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13735 	enum i915_pipe pipe = to_intel_plane(plane)->pipe;
13736 	unsigned stride;
13737 	int ret;
13738 
13739 	ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13740 					    &state->dst, &state->clip,
13741 					    DRM_PLANE_HELPER_NO_SCALING,
13742 					    DRM_PLANE_HELPER_NO_SCALING,
13743 					    true, true, &state->visible);
13744 	if (ret)
13745 		return ret;
13746 
13747 	/* if we want to turn off the cursor ignore width and height */
13748 	if (!obj)
13749 		return 0;
13750 
13751 	/* Check for which cursor types we support */
13752 	if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
13753 		DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13754 			  state->base.crtc_w, state->base.crtc_h);
13755 		return -EINVAL;
13756 	}
13757 
13758 	stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13759 	if (obj->base.size < stride * state->base.crtc_h) {
13760 		DRM_DEBUG_KMS("buffer is too small\n");
13761 		return -ENOMEM;
13762 	}
13763 
13764 	if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
13765 		DRM_DEBUG_KMS("cursor cannot be tiled\n");
13766 		return -EINVAL;
13767 	}
13768 
13769 	/*
13770 	 * There's something wrong with the cursor on CHV pipe C.
13771 	 * If it straddles the left edge of the screen then
13772 	 * moving it away from the edge or disabling it often
13773 	 * results in a pipe underrun, and often that can lead to
13774 	 * dead pipe (constant underrun reported, and it scans
13775 	 * out just a solid color). To recover from that, the
13776 	 * display power well must be turned off and on again.
13777 	 * Refuse the put the cursor into that compromised position.
13778 	 */
13779 	if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
13780 	    state->visible && state->base.crtc_x < 0) {
13781 		DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
13782 		return -EINVAL;
13783 	}
13784 
13785 	return 0;
13786 }
13787 
13788 static void
13789 intel_disable_cursor_plane(struct drm_plane *plane,
13790 			   struct drm_crtc *crtc)
13791 {
13792 	intel_crtc_update_cursor(crtc, false);
13793 }
13794 
13795 static void
13796 intel_commit_cursor_plane(struct drm_plane *plane,
13797 			  struct intel_plane_state *state)
13798 {
13799 	struct drm_crtc *crtc = state->base.crtc;
13800 	struct drm_device *dev = plane->dev;
13801 	struct intel_crtc *intel_crtc;
13802 	struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
13803 	uint32_t addr;
13804 
13805 	crtc = crtc ? crtc : plane->crtc;
13806 	intel_crtc = to_intel_crtc(crtc);
13807 
13808 	if (!obj)
13809 		addr = 0;
13810 	else if (!INTEL_INFO(dev)->cursor_needs_physical)
13811 		addr = i915_gem_obj_ggtt_offset(obj);
13812 	else
13813 		addr = obj->phys_handle->busaddr;
13814 
13815 	intel_crtc->cursor_addr = addr;
13816 
13817 	if (crtc->state->active)
13818 		intel_crtc_update_cursor(crtc, state->visible);
13819 }
13820 
13821 static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13822 						   int pipe)
13823 {
13824 	struct intel_plane *cursor;
13825 	struct intel_plane_state *state;
13826 
13827 	cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13828 	if (cursor == NULL)
13829 		return NULL;
13830 
13831 	state = intel_create_plane_state(&cursor->base);
13832 	if (!state) {
13833 		kfree(cursor);
13834 		return NULL;
13835 	}
13836 	cursor->base.state = &state->base;
13837 
13838 	cursor->can_scale = false;
13839 	cursor->max_downscale = 1;
13840 	cursor->pipe = pipe;
13841 	cursor->plane = pipe;
13842 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
13843 	cursor->check_plane = intel_check_cursor_plane;
13844 	cursor->commit_plane = intel_commit_cursor_plane;
13845 	cursor->disable_plane = intel_disable_cursor_plane;
13846 
13847 	drm_universal_plane_init(dev, &cursor->base, 0,
13848 				 &intel_plane_funcs,
13849 				 intel_cursor_formats,
13850 				 ARRAY_SIZE(intel_cursor_formats),
13851 				 DRM_PLANE_TYPE_CURSOR);
13852 
13853 	if (INTEL_INFO(dev)->gen >= 4) {
13854 		if (!dev->mode_config.rotation_property)
13855 			dev->mode_config.rotation_property =
13856 				drm_mode_create_rotation_property(dev,
13857 							BIT(DRM_ROTATE_0) |
13858 							BIT(DRM_ROTATE_180));
13859 		if (dev->mode_config.rotation_property)
13860 			drm_object_attach_property(&cursor->base.base,
13861 				dev->mode_config.rotation_property,
13862 				state->base.rotation);
13863 	}
13864 
13865 	if (INTEL_INFO(dev)->gen >=9)
13866 		state->scaler_id = -1;
13867 
13868 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13869 
13870 	return &cursor->base;
13871 }
13872 
13873 static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13874 	struct intel_crtc_state *crtc_state)
13875 {
13876 	int i;
13877 	struct intel_scaler *intel_scaler;
13878 	struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13879 
13880 	for (i = 0; i < intel_crtc->num_scalers; i++) {
13881 		intel_scaler = &scaler_state->scalers[i];
13882 		intel_scaler->in_use = 0;
13883 		intel_scaler->mode = PS_SCALER_MODE_DYN;
13884 	}
13885 
13886 	scaler_state->scaler_id = -1;
13887 }
13888 
13889 static void intel_crtc_init(struct drm_device *dev, int pipe)
13890 {
13891 	struct drm_i915_private *dev_priv = dev->dev_private;
13892 	struct intel_crtc *intel_crtc;
13893 	struct intel_crtc_state *crtc_state = NULL;
13894 	struct drm_plane *primary = NULL;
13895 	struct drm_plane *cursor = NULL;
13896 	int i, ret;
13897 
13898 	intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
13899 	if (intel_crtc == NULL)
13900 		return;
13901 
13902 	crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13903 	if (!crtc_state)
13904 		goto fail;
13905 	intel_crtc->config = crtc_state;
13906 	intel_crtc->base.state = &crtc_state->base;
13907 	crtc_state->base.crtc = &intel_crtc->base;
13908 
13909 	/* initialize shared scalers */
13910 	if (INTEL_INFO(dev)->gen >= 9) {
13911 		if (pipe == PIPE_C)
13912 			intel_crtc->num_scalers = 1;
13913 		else
13914 			intel_crtc->num_scalers = SKL_NUM_SCALERS;
13915 
13916 		skl_init_scalers(dev, intel_crtc, crtc_state);
13917 	}
13918 
13919 	primary = intel_primary_plane_create(dev, pipe);
13920 	if (!primary)
13921 		goto fail;
13922 
13923 	cursor = intel_cursor_plane_create(dev, pipe);
13924 	if (!cursor)
13925 		goto fail;
13926 
13927 	ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
13928 					cursor, &intel_crtc_funcs);
13929 	if (ret)
13930 		goto fail;
13931 
13932 	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
13933 	for (i = 0; i < 256; i++) {
13934 		intel_crtc->lut_r[i] = i;
13935 		intel_crtc->lut_g[i] = i;
13936 		intel_crtc->lut_b[i] = i;
13937 	}
13938 
13939 	/*
13940 	 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
13941 	 * is hooked to pipe B. Hence we want plane A feeding pipe B.
13942 	 */
13943 	intel_crtc->pipe = pipe;
13944 	intel_crtc->plane = pipe;
13945 	if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
13946 		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
13947 		intel_crtc->plane = !pipe;
13948 	}
13949 
13950 	intel_crtc->cursor_base = ~0;
13951 	intel_crtc->cursor_cntl = ~0;
13952 	intel_crtc->cursor_size = ~0;
13953 
13954 	intel_crtc->wm.cxsr_allowed = true;
13955 
13956 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13957 	       dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13958 	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13959 	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13960 
13961 	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
13962 
13963 	WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
13964 	return;
13965 
13966 fail:
13967 	if (primary)
13968 		drm_plane_cleanup(primary);
13969 	if (cursor)
13970 		drm_plane_cleanup(cursor);
13971 	kfree(crtc_state);
13972 	kfree(intel_crtc);
13973 }
13974 
13975 enum i915_pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13976 {
13977 	struct drm_encoder *encoder = connector->base.encoder;
13978 	struct drm_device *dev = connector->base.dev;
13979 
13980 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
13981 
13982 	if (!encoder || WARN_ON(!encoder->crtc))
13983 		return INVALID_PIPE;
13984 
13985 	return to_intel_crtc(encoder->crtc)->pipe;
13986 }
13987 
13988 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
13989 				struct drm_file *file)
13990 {
13991 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
13992 	struct drm_crtc *drmmode_crtc;
13993 	struct intel_crtc *crtc;
13994 
13995 	drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
13996 
13997 	if (!drmmode_crtc) {
13998 		DRM_ERROR("no such CRTC id\n");
13999 		return -ENOENT;
14000 	}
14001 
14002 	crtc = to_intel_crtc(drmmode_crtc);
14003 	pipe_from_crtc_id->pipe = crtc->pipe;
14004 
14005 	return 0;
14006 }
14007 
14008 static int intel_encoder_clones(struct intel_encoder *encoder)
14009 {
14010 	struct drm_device *dev = encoder->base.dev;
14011 	struct intel_encoder *source_encoder;
14012 	int index_mask = 0;
14013 	int entry = 0;
14014 
14015 	for_each_intel_encoder(dev, source_encoder) {
14016 		if (encoders_cloneable(encoder, source_encoder))
14017 			index_mask |= (1 << entry);
14018 
14019 		entry++;
14020 	}
14021 
14022 	return index_mask;
14023 }
14024 
14025 static bool has_edp_a(struct drm_device *dev)
14026 {
14027 	struct drm_i915_private *dev_priv = dev->dev_private;
14028 
14029 	if (!IS_MOBILE(dev))
14030 		return false;
14031 
14032 	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14033 		return false;
14034 
14035 	if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
14036 		return false;
14037 
14038 	return true;
14039 }
14040 
14041 static bool intel_crt_present(struct drm_device *dev)
14042 {
14043 	struct drm_i915_private *dev_priv = dev->dev_private;
14044 
14045 	if (INTEL_INFO(dev)->gen >= 9)
14046 		return false;
14047 
14048 	if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
14049 		return false;
14050 
14051 	if (IS_CHERRYVIEW(dev))
14052 		return false;
14053 
14054 	if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14055 		return false;
14056 
14057 	return true;
14058 }
14059 
14060 static void intel_setup_outputs(struct drm_device *dev)
14061 {
14062 	struct drm_i915_private *dev_priv = dev->dev_private;
14063 	struct intel_encoder *encoder;
14064 	bool dpd_is_edp = false;
14065 
14066 	intel_lvds_init(dev);
14067 
14068 	if (intel_crt_present(dev))
14069 		intel_crt_init(dev);
14070 
14071 	if (IS_BROXTON(dev)) {
14072 		/*
14073 		 * FIXME: Broxton doesn't support port detection via the
14074 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14075 		 * detect the ports.
14076 		 */
14077 		intel_ddi_init(dev, PORT_A);
14078 		intel_ddi_init(dev, PORT_B);
14079 		intel_ddi_init(dev, PORT_C);
14080 	} else if (HAS_DDI(dev)) {
14081 		int found;
14082 
14083 		/*
14084 		 * Haswell uses DDI functions to detect digital outputs.
14085 		 * On SKL pre-D0 the strap isn't connected, so we assume
14086 		 * it's there.
14087 		 */
14088 		found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
14089 		/* WaIgnoreDDIAStrap: skl */
14090 		if (found || IS_SKYLAKE(dev))
14091 			intel_ddi_init(dev, PORT_A);
14092 
14093 		/* DDI B, C and D detection is indicated by the SFUSE_STRAP
14094 		 * register */
14095 		found = I915_READ(SFUSE_STRAP);
14096 
14097 		if (found & SFUSE_STRAP_DDIB_DETECTED)
14098 			intel_ddi_init(dev, PORT_B);
14099 		if (found & SFUSE_STRAP_DDIC_DETECTED)
14100 			intel_ddi_init(dev, PORT_C);
14101 		if (found & SFUSE_STRAP_DDID_DETECTED)
14102 			intel_ddi_init(dev, PORT_D);
14103 		/*
14104 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
14105 		 */
14106 		if (IS_SKYLAKE(dev) &&
14107 		    (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
14108 		     dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
14109 		     dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
14110 			intel_ddi_init(dev, PORT_E);
14111 
14112 	} else if (HAS_PCH_SPLIT(dev)) {
14113 		int found;
14114 		dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
14115 
14116 		if (has_edp_a(dev))
14117 			intel_dp_init(dev, DP_A, PORT_A);
14118 
14119 		if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
14120 			/* PCH SDVOB multiplex with HDMIB */
14121 			found = intel_sdvo_init(dev, PCH_SDVOB, true);
14122 			if (!found)
14123 				intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
14124 			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
14125 				intel_dp_init(dev, PCH_DP_B, PORT_B);
14126 		}
14127 
14128 		if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
14129 			intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
14130 
14131 		if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
14132 			intel_hdmi_init(dev, PCH_HDMID, PORT_D);
14133 
14134 		if (I915_READ(PCH_DP_C) & DP_DETECTED)
14135 			intel_dp_init(dev, PCH_DP_C, PORT_C);
14136 
14137 		if (I915_READ(PCH_DP_D) & DP_DETECTED)
14138 			intel_dp_init(dev, PCH_DP_D, PORT_D);
14139 	} else if (IS_VALLEYVIEW(dev)) {
14140 		/*
14141 		 * The DP_DETECTED bit is the latched state of the DDC
14142 		 * SDA pin at boot. However since eDP doesn't require DDC
14143 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14144 		 * eDP ports may have been muxed to an alternate function.
14145 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
14146 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
14147 		 * detect eDP ports.
14148 		 */
14149 		if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
14150 		    !intel_dp_is_edp(dev, PORT_B))
14151 			intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
14152 		if (I915_READ(VLV_DP_B) & DP_DETECTED ||
14153 		    intel_dp_is_edp(dev, PORT_B))
14154 			intel_dp_init(dev, VLV_DP_B, PORT_B);
14155 
14156 		if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
14157 		    !intel_dp_is_edp(dev, PORT_C))
14158 			intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
14159 		if (I915_READ(VLV_DP_C) & DP_DETECTED ||
14160 		    intel_dp_is_edp(dev, PORT_C))
14161 			intel_dp_init(dev, VLV_DP_C, PORT_C);
14162 
14163 		if (IS_CHERRYVIEW(dev)) {
14164 			/* eDP not supported on port D, so don't check VBT */
14165 			if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
14166 				intel_hdmi_init(dev, CHV_HDMID, PORT_D);
14167 			if (I915_READ(CHV_DP_D) & DP_DETECTED)
14168 				intel_dp_init(dev, CHV_DP_D, PORT_D);
14169 		}
14170 
14171 		intel_dsi_init(dev);
14172 	} else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
14173 		bool found = false;
14174 
14175 		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
14176 			DRM_DEBUG_KMS("probing SDVOB\n");
14177 			found = intel_sdvo_init(dev, GEN3_SDVOB, true);
14178 			if (!found && IS_G4X(dev)) {
14179 				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
14180 				intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
14181 			}
14182 
14183 			if (!found && IS_G4X(dev))
14184 				intel_dp_init(dev, DP_B, PORT_B);
14185 		}
14186 
14187 		/* Before G4X SDVOC doesn't have its own detect register */
14188 
14189 		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
14190 			DRM_DEBUG_KMS("probing SDVOC\n");
14191 			found = intel_sdvo_init(dev, GEN3_SDVOC, false);
14192 		}
14193 
14194 		if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
14195 
14196 			if (IS_G4X(dev)) {
14197 				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
14198 				intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
14199 			}
14200 			if (IS_G4X(dev))
14201 				intel_dp_init(dev, DP_C, PORT_C);
14202 		}
14203 
14204 		if (IS_G4X(dev) &&
14205 		    (I915_READ(DP_D) & DP_DETECTED))
14206 			intel_dp_init(dev, DP_D, PORT_D);
14207 	} else if (IS_GEN2(dev))
14208 		intel_dvo_init(dev);
14209 
14210 	if (SUPPORTS_TV(dev))
14211 		intel_tv_init(dev);
14212 
14213 	intel_psr_init(dev);
14214 
14215 	for_each_intel_encoder(dev, encoder) {
14216 		encoder->base.possible_crtcs = encoder->crtc_mask;
14217 		encoder->base.possible_clones =
14218 			intel_encoder_clones(encoder);
14219 	}
14220 
14221 	intel_init_pch_refclk(dev);
14222 
14223 	drm_helper_move_panel_connectors_to_head(dev);
14224 }
14225 
14226 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14227 {
14228 	struct drm_device *dev = fb->dev;
14229 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14230 
14231 	drm_framebuffer_cleanup(fb);
14232 	mutex_lock(&dev->struct_mutex);
14233 	WARN_ON(!intel_fb->obj->framebuffer_references--);
14234 	drm_gem_object_unreference(&intel_fb->obj->base);
14235 	mutex_unlock(&dev->struct_mutex);
14236 	kfree(intel_fb);
14237 }
14238 
14239 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
14240 						struct drm_file *file,
14241 						unsigned int *handle)
14242 {
14243 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14244 	struct drm_i915_gem_object *obj = intel_fb->obj;
14245 
14246 	if (obj->userptr.mm) {
14247 		DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
14248 		return -EINVAL;
14249 	}
14250 
14251 	return drm_gem_handle_create(file, &obj->base, handle);
14252 }
14253 
14254 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14255 					struct drm_file *file,
14256 					unsigned flags, unsigned color,
14257 					struct drm_clip_rect *clips,
14258 					unsigned num_clips)
14259 {
14260 	struct drm_device *dev = fb->dev;
14261 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14262 	struct drm_i915_gem_object *obj = intel_fb->obj;
14263 
14264 	mutex_lock(&dev->struct_mutex);
14265 	intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
14266 	mutex_unlock(&dev->struct_mutex);
14267 
14268 	return 0;
14269 }
14270 
14271 static const struct drm_framebuffer_funcs intel_fb_funcs = {
14272 	.destroy = intel_user_framebuffer_destroy,
14273 	.create_handle = intel_user_framebuffer_create_handle,
14274 	.dirty = intel_user_framebuffer_dirty,
14275 };
14276 
14277 static
14278 u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14279 			 uint32_t pixel_format)
14280 {
14281 	u32 gen = INTEL_INFO(dev)->gen;
14282 
14283 	if (gen >= 9) {
14284 		/* "The stride in bytes must not exceed the of the size of 8K
14285 		 *  pixels and 32K bytes."
14286 		 */
14287 		 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14288 	} else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14289 		return 32*1024;
14290 	} else if (gen >= 4) {
14291 		if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14292 			return 16*1024;
14293 		else
14294 			return 32*1024;
14295 	} else if (gen >= 3) {
14296 		if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14297 			return 8*1024;
14298 		else
14299 			return 16*1024;
14300 	} else {
14301 		/* XXX DSPC is limited to 4k tiled */
14302 		return 8*1024;
14303 	}
14304 }
14305 
14306 static int intel_framebuffer_init(struct drm_device *dev,
14307 				  struct intel_framebuffer *intel_fb,
14308 				  struct drm_mode_fb_cmd2 *mode_cmd,
14309 				  struct drm_i915_gem_object *obj)
14310 {
14311 	unsigned int aligned_height;
14312 	int ret;
14313 	u32 pitch_limit, stride_alignment;
14314 
14315 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14316 
14317 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14318 		/* Enforce that fb modifier and tiling mode match, but only for
14319 		 * X-tiled. This is needed for FBC. */
14320 		if (!!(obj->tiling_mode == I915_TILING_X) !=
14321 		    !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14322 			DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14323 			return -EINVAL;
14324 		}
14325 	} else {
14326 		if (obj->tiling_mode == I915_TILING_X)
14327 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14328 		else if (obj->tiling_mode == I915_TILING_Y) {
14329 			DRM_DEBUG("No Y tiling for legacy addfb\n");
14330 			return -EINVAL;
14331 		}
14332 	}
14333 
14334 	/* Passed in modifier sanity checking. */
14335 	switch (mode_cmd->modifier[0]) {
14336 	case I915_FORMAT_MOD_Y_TILED:
14337 	case I915_FORMAT_MOD_Yf_TILED:
14338 		if (INTEL_INFO(dev)->gen < 9) {
14339 			DRM_DEBUG("Unsupported tiling 0x%lx!\n",
14340 				  mode_cmd->modifier[0]);
14341 			return -EINVAL;
14342 		}
14343 	case DRM_FORMAT_MOD_NONE:
14344 	case I915_FORMAT_MOD_X_TILED:
14345 		break;
14346 	default:
14347 		DRM_DEBUG("Unsupported fb modifier 0x%lx!\n",
14348 			  mode_cmd->modifier[0]);
14349 		return -EINVAL;
14350 	}
14351 
14352 	stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14353 						     mode_cmd->pixel_format);
14354 	if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14355 		DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14356 			  mode_cmd->pitches[0], stride_alignment);
14357 		return -EINVAL;
14358 	}
14359 
14360 	pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14361 					   mode_cmd->pixel_format);
14362 	if (mode_cmd->pitches[0] > pitch_limit) {
14363 		DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14364 			  mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
14365 			  "tiled" : "linear",
14366 			  mode_cmd->pitches[0], pitch_limit);
14367 		return -EINVAL;
14368 	}
14369 
14370 	if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
14371 	    mode_cmd->pitches[0] != obj->stride) {
14372 		DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14373 			  mode_cmd->pitches[0], obj->stride);
14374 		return -EINVAL;
14375 	}
14376 
14377 	/* Reject formats not supported by any plane early. */
14378 	switch (mode_cmd->pixel_format) {
14379 	case DRM_FORMAT_C8:
14380 	case DRM_FORMAT_RGB565:
14381 	case DRM_FORMAT_XRGB8888:
14382 	case DRM_FORMAT_ARGB8888:
14383 		break;
14384 	case DRM_FORMAT_XRGB1555:
14385 		if (INTEL_INFO(dev)->gen > 3) {
14386 			DRM_DEBUG("unsupported pixel format: %s\n",
14387 				  drm_get_format_name(mode_cmd->pixel_format));
14388 			return -EINVAL;
14389 		}
14390 		break;
14391 	case DRM_FORMAT_ABGR8888:
14392 		if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14393 			DRM_DEBUG("unsupported pixel format: %s\n",
14394 				  drm_get_format_name(mode_cmd->pixel_format));
14395 			return -EINVAL;
14396 		}
14397 		break;
14398 	case DRM_FORMAT_XBGR8888:
14399 	case DRM_FORMAT_XRGB2101010:
14400 	case DRM_FORMAT_XBGR2101010:
14401 		if (INTEL_INFO(dev)->gen < 4) {
14402 			DRM_DEBUG("unsupported pixel format: %s\n",
14403 				  drm_get_format_name(mode_cmd->pixel_format));
14404 			return -EINVAL;
14405 		}
14406 		break;
14407 	case DRM_FORMAT_ABGR2101010:
14408 		if (!IS_VALLEYVIEW(dev)) {
14409 			DRM_DEBUG("unsupported pixel format: %s\n",
14410 				  drm_get_format_name(mode_cmd->pixel_format));
14411 			return -EINVAL;
14412 		}
14413 		break;
14414 	case DRM_FORMAT_YUYV:
14415 	case DRM_FORMAT_UYVY:
14416 	case DRM_FORMAT_YVYU:
14417 	case DRM_FORMAT_VYUY:
14418 		if (INTEL_INFO(dev)->gen < 5) {
14419 			DRM_DEBUG("unsupported pixel format: %s\n",
14420 				  drm_get_format_name(mode_cmd->pixel_format));
14421 			return -EINVAL;
14422 		}
14423 		break;
14424 	default:
14425 		DRM_DEBUG("unsupported pixel format: %s\n",
14426 			  drm_get_format_name(mode_cmd->pixel_format));
14427 		return -EINVAL;
14428 	}
14429 
14430 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14431 	if (mode_cmd->offsets[0] != 0)
14432 		return -EINVAL;
14433 
14434 	aligned_height = intel_fb_align_height(dev, mode_cmd->height,
14435 					       mode_cmd->pixel_format,
14436 					       mode_cmd->modifier[0]);
14437 	/* FIXME drm helper for size checks (especially planar formats)? */
14438 	if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14439 		return -EINVAL;
14440 
14441 	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14442 	intel_fb->obj = obj;
14443 	intel_fb->obj->framebuffer_references++;
14444 
14445 	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14446 	if (ret) {
14447 		DRM_ERROR("framebuffer init failed %d\n", ret);
14448 		return ret;
14449 	}
14450 
14451 	return 0;
14452 }
14453 
14454 static struct drm_framebuffer *
14455 intel_user_framebuffer_create(struct drm_device *dev,
14456 			      struct drm_file *filp,
14457 			      struct drm_mode_fb_cmd2 *user_mode_cmd)
14458 {
14459 	struct drm_i915_gem_object *obj;
14460 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
14461 
14462 	obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14463 						mode_cmd.handles[0]));
14464 	if (&obj->base == NULL)
14465 		return ERR_PTR(-ENOENT);
14466 
14467 	return intel_framebuffer_create(dev, &mode_cmd, obj);
14468 }
14469 
14470 #ifndef CONFIG_DRM_FBDEV_EMULATION
14471 static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
14472 {
14473 }
14474 #endif
14475 
14476 static const struct drm_mode_config_funcs intel_mode_funcs = {
14477 	.fb_create = intel_user_framebuffer_create,
14478 	.output_poll_changed = intel_fbdev_output_poll_changed,
14479 	.atomic_check = intel_atomic_check,
14480 	.atomic_commit = intel_atomic_commit,
14481 	.atomic_state_alloc = intel_atomic_state_alloc,
14482 	.atomic_state_clear = intel_atomic_state_clear,
14483 };
14484 
14485 /* Set up chip specific display functions */
14486 static void intel_init_display(struct drm_device *dev)
14487 {
14488 	struct drm_i915_private *dev_priv = dev->dev_private;
14489 
14490 	if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14491 		dev_priv->display.find_dpll = g4x_find_best_dpll;
14492 	else if (IS_CHERRYVIEW(dev))
14493 		dev_priv->display.find_dpll = chv_find_best_dpll;
14494 	else if (IS_VALLEYVIEW(dev))
14495 		dev_priv->display.find_dpll = vlv_find_best_dpll;
14496 	else if (IS_PINEVIEW(dev))
14497 		dev_priv->display.find_dpll = pnv_find_best_dpll;
14498 	else
14499 		dev_priv->display.find_dpll = i9xx_find_best_dpll;
14500 
14501 	if (INTEL_INFO(dev)->gen >= 9) {
14502 		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
14503 		dev_priv->display.get_initial_plane_config =
14504 			skylake_get_initial_plane_config;
14505 		dev_priv->display.crtc_compute_clock =
14506 			haswell_crtc_compute_clock;
14507 		dev_priv->display.crtc_enable = haswell_crtc_enable;
14508 		dev_priv->display.crtc_disable = haswell_crtc_disable;
14509 		dev_priv->display.update_primary_plane =
14510 			skylake_update_primary_plane;
14511 	} else if (HAS_DDI(dev)) {
14512 		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
14513 		dev_priv->display.get_initial_plane_config =
14514 			ironlake_get_initial_plane_config;
14515 		dev_priv->display.crtc_compute_clock =
14516 			haswell_crtc_compute_clock;
14517 		dev_priv->display.crtc_enable = haswell_crtc_enable;
14518 		dev_priv->display.crtc_disable = haswell_crtc_disable;
14519 		dev_priv->display.update_primary_plane =
14520 			ironlake_update_primary_plane;
14521 	} else if (HAS_PCH_SPLIT(dev)) {
14522 		dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
14523 		dev_priv->display.get_initial_plane_config =
14524 			ironlake_get_initial_plane_config;
14525 		dev_priv->display.crtc_compute_clock =
14526 			ironlake_crtc_compute_clock;
14527 		dev_priv->display.crtc_enable = ironlake_crtc_enable;
14528 		dev_priv->display.crtc_disable = ironlake_crtc_disable;
14529 		dev_priv->display.update_primary_plane =
14530 			ironlake_update_primary_plane;
14531 	} else if (IS_VALLEYVIEW(dev)) {
14532 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14533 		dev_priv->display.get_initial_plane_config =
14534 			i9xx_get_initial_plane_config;
14535 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
14536 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
14537 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
14538 		dev_priv->display.update_primary_plane =
14539 			i9xx_update_primary_plane;
14540 	} else {
14541 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14542 		dev_priv->display.get_initial_plane_config =
14543 			i9xx_get_initial_plane_config;
14544 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
14545 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
14546 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
14547 		dev_priv->display.update_primary_plane =
14548 			i9xx_update_primary_plane;
14549 	}
14550 
14551 	/* Returns the core display clock speed */
14552 	if (IS_SKYLAKE(dev))
14553 		dev_priv->display.get_display_clock_speed =
14554 			skylake_get_display_clock_speed;
14555 	else if (IS_BROXTON(dev))
14556 		dev_priv->display.get_display_clock_speed =
14557 			broxton_get_display_clock_speed;
14558 	else if (IS_BROADWELL(dev))
14559 		dev_priv->display.get_display_clock_speed =
14560 			broadwell_get_display_clock_speed;
14561 	else if (IS_HASWELL(dev))
14562 		dev_priv->display.get_display_clock_speed =
14563 			haswell_get_display_clock_speed;
14564 	else if (IS_VALLEYVIEW(dev))
14565 		dev_priv->display.get_display_clock_speed =
14566 			valleyview_get_display_clock_speed;
14567 	else if (IS_GEN5(dev))
14568 		dev_priv->display.get_display_clock_speed =
14569 			ilk_get_display_clock_speed;
14570 	else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14571 		 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
14572 		dev_priv->display.get_display_clock_speed =
14573 			i945_get_display_clock_speed;
14574 	else if (IS_GM45(dev))
14575 		dev_priv->display.get_display_clock_speed =
14576 			gm45_get_display_clock_speed;
14577 	else if (IS_CRESTLINE(dev))
14578 		dev_priv->display.get_display_clock_speed =
14579 			i965gm_get_display_clock_speed;
14580 	else if (IS_PINEVIEW(dev))
14581 		dev_priv->display.get_display_clock_speed =
14582 			pnv_get_display_clock_speed;
14583 	else if (IS_G33(dev) || IS_G4X(dev))
14584 		dev_priv->display.get_display_clock_speed =
14585 			g33_get_display_clock_speed;
14586 	else if (IS_I915G(dev))
14587 		dev_priv->display.get_display_clock_speed =
14588 			i915_get_display_clock_speed;
14589 	else if (IS_I945GM(dev) || IS_845G(dev))
14590 		dev_priv->display.get_display_clock_speed =
14591 			i9xx_misc_get_display_clock_speed;
14592 	else if (IS_PINEVIEW(dev))
14593 		dev_priv->display.get_display_clock_speed =
14594 			pnv_get_display_clock_speed;
14595 	else if (IS_I915GM(dev))
14596 		dev_priv->display.get_display_clock_speed =
14597 			i915gm_get_display_clock_speed;
14598 	else if (IS_I865G(dev))
14599 		dev_priv->display.get_display_clock_speed =
14600 			i865_get_display_clock_speed;
14601 	else if (IS_I85X(dev))
14602 		dev_priv->display.get_display_clock_speed =
14603 			i85x_get_display_clock_speed;
14604 	else { /* 830 */
14605 		WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
14606 		dev_priv->display.get_display_clock_speed =
14607 			i830_get_display_clock_speed;
14608 	}
14609 
14610 	if (IS_GEN5(dev)) {
14611 		dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
14612 	} else if (IS_GEN6(dev)) {
14613 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
14614 	} else if (IS_IVYBRIDGE(dev)) {
14615 		/* FIXME: detect B0+ stepping and use auto training */
14616 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
14617 	} else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
14618 		dev_priv->display.fdi_link_train = hsw_fdi_link_train;
14619 		if (IS_BROADWELL(dev)) {
14620 			dev_priv->display.modeset_commit_cdclk =
14621 				broadwell_modeset_commit_cdclk;
14622 			dev_priv->display.modeset_calc_cdclk =
14623 				broadwell_modeset_calc_cdclk;
14624 		}
14625 	} else if (IS_VALLEYVIEW(dev)) {
14626 		dev_priv->display.modeset_commit_cdclk =
14627 			valleyview_modeset_commit_cdclk;
14628 		dev_priv->display.modeset_calc_cdclk =
14629 			valleyview_modeset_calc_cdclk;
14630 	} else if (IS_BROXTON(dev)) {
14631 		dev_priv->display.modeset_commit_cdclk =
14632 			broxton_modeset_commit_cdclk;
14633 		dev_priv->display.modeset_calc_cdclk =
14634 			broxton_modeset_calc_cdclk;
14635 	}
14636 
14637 	switch (INTEL_INFO(dev)->gen) {
14638 	case 2:
14639 		dev_priv->display.queue_flip = intel_gen2_queue_flip;
14640 		break;
14641 
14642 	case 3:
14643 		dev_priv->display.queue_flip = intel_gen3_queue_flip;
14644 		break;
14645 
14646 	case 4:
14647 	case 5:
14648 		dev_priv->display.queue_flip = intel_gen4_queue_flip;
14649 		break;
14650 
14651 	case 6:
14652 		dev_priv->display.queue_flip = intel_gen6_queue_flip;
14653 		break;
14654 	case 7:
14655 	case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14656 		dev_priv->display.queue_flip = intel_gen7_queue_flip;
14657 		break;
14658 	case 9:
14659 		/* Drop through - unsupported since execlist only. */
14660 	default:
14661 		/* Default just returns -ENODEV to indicate unsupported */
14662 		dev_priv->display.queue_flip = intel_default_queue_flip;
14663 	}
14664 
14665 	lockinit(&dev_priv->pps_mutex, "i915pm", 0, LK_CANRECURSE);
14666 }
14667 
14668 /*
14669  * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14670  * resume, or other times.  This quirk makes sure that's the case for
14671  * affected systems.
14672  */
14673 static void quirk_pipea_force(struct drm_device *dev)
14674 {
14675 	struct drm_i915_private *dev_priv = dev->dev_private;
14676 
14677 	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
14678 	DRM_INFO("applying pipe a force quirk\n");
14679 }
14680 
14681 static void quirk_pipeb_force(struct drm_device *dev)
14682 {
14683 	struct drm_i915_private *dev_priv = dev->dev_private;
14684 
14685 	dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14686 	DRM_INFO("applying pipe b force quirk\n");
14687 }
14688 
14689 /*
14690  * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14691  */
14692 static void quirk_ssc_force_disable(struct drm_device *dev)
14693 {
14694 	struct drm_i915_private *dev_priv = dev->dev_private;
14695 	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
14696 	DRM_INFO("applying lvds SSC disable quirk\n");
14697 }
14698 
14699 /*
14700  * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14701  * brightness value
14702  */
14703 static void quirk_invert_brightness(struct drm_device *dev)
14704 {
14705 	struct drm_i915_private *dev_priv = dev->dev_private;
14706 	dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
14707 	DRM_INFO("applying inverted panel brightness quirk\n");
14708 }
14709 
14710 /* Some VBT's incorrectly indicate no backlight is present */
14711 static void quirk_backlight_present(struct drm_device *dev)
14712 {
14713 	struct drm_i915_private *dev_priv = dev->dev_private;
14714 	dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14715 	DRM_INFO("applying backlight present quirk\n");
14716 }
14717 
14718 struct intel_quirk {
14719 	int device;
14720 	int subsystem_vendor;
14721 	int subsystem_device;
14722 	void (*hook)(struct drm_device *dev);
14723 };
14724 
14725 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14726 struct intel_dmi_quirk {
14727 	void (*hook)(struct drm_device *dev);
14728 	const struct dmi_system_id (*dmi_id_list)[];
14729 };
14730 
14731 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14732 {
14733 	DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14734 	return 1;
14735 }
14736 
14737 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14738 	{
14739 		.dmi_id_list = &(const struct dmi_system_id[]) {
14740 			{
14741 				.callback = intel_dmi_reverse_brightness,
14742 				.ident = "NCR Corporation",
14743 				.matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14744 					    DMI_MATCH(DMI_PRODUCT_NAME, ""),
14745 				},
14746 			},
14747 			{ }  /* terminating entry */
14748 		},
14749 		.hook = quirk_invert_brightness,
14750 	},
14751 };
14752 
14753 static struct intel_quirk intel_quirks[] = {
14754 	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14755 	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14756 
14757 	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14758 	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14759 
14760 	/* 830 needs to leave pipe A & dpll A up */
14761 	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14762 
14763 	/* 830 needs to leave pipe B & dpll B up */
14764 	{ 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14765 
14766 	/* Lenovo U160 cannot use SSC on LVDS */
14767 	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
14768 
14769 	/* Sony Vaio Y cannot use SSC on LVDS */
14770 	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
14771 
14772 	/* Acer Aspire 5734Z must invert backlight brightness */
14773 	{ 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14774 
14775 	/* Acer/eMachines G725 */
14776 	{ 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14777 
14778 	/* Acer/eMachines e725 */
14779 	{ 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14780 
14781 	/* Acer/Packard Bell NCL20 */
14782 	{ 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14783 
14784 	/* Acer Aspire 4736Z */
14785 	{ 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
14786 
14787 	/* Acer Aspire 5336 */
14788 	{ 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
14789 
14790 	/* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14791 	{ 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
14792 
14793 	/* Acer C720 Chromebook (Core i3 4005U) */
14794 	{ 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14795 
14796 	/* Apple Macbook 2,1 (Core 2 T7400) */
14797 	{ 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14798 
14799 	/* Apple Macbook 4,1 */
14800 	{ 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
14801 
14802 	/* Toshiba CB35 Chromebook (Celeron 2955U) */
14803 	{ 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
14804 
14805 	/* HP Chromebook 14 (Celeron 2955U) */
14806 	{ 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
14807 
14808 	/* Dell Chromebook 11 */
14809 	{ 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
14810 
14811 	/* Dell Chromebook 11 (2015 version) */
14812 	{ 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
14813 };
14814 
14815 static void intel_init_quirks(struct drm_device *dev)
14816 {
14817 	struct pci_dev *d = dev->pdev;
14818 	int i;
14819 
14820 	for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14821 		struct intel_quirk *q = &intel_quirks[i];
14822 
14823 		if (d->device == q->device &&
14824 		    (d->subsystem_vendor == q->subsystem_vendor ||
14825 		     q->subsystem_vendor == PCI_ANY_ID) &&
14826 		    (d->subsystem_device == q->subsystem_device ||
14827 		     q->subsystem_device == PCI_ANY_ID))
14828 			q->hook(dev);
14829 	}
14830 	for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14831 		if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14832 			intel_dmi_quirks[i].hook(dev);
14833 	}
14834 }
14835 
14836 /* Disable the VGA plane that we never use */
14837 static void i915_disable_vga(struct drm_device *dev)
14838 {
14839 	struct drm_i915_private *dev_priv = dev->dev_private;
14840 	u8 sr1;
14841 	u32 vga_reg = i915_vgacntrl_reg(dev);
14842 
14843 	/* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
14844 #if 0
14845 	vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
14846 #endif
14847 	outb(VGA_SR_INDEX, SR01);
14848 	sr1 = inb(VGA_SR_DATA);
14849 	outb(VGA_SR_DATA, sr1 | 1 << 5);
14850 #if 0
14851 	vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14852 #endif
14853 	udelay(300);
14854 
14855 	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
14856 	POSTING_READ(vga_reg);
14857 }
14858 
14859 void intel_modeset_init_hw(struct drm_device *dev)
14860 {
14861 	intel_update_cdclk(dev);
14862 	intel_prepare_ddi(dev);
14863 	intel_init_clock_gating(dev);
14864 	intel_enable_gt_powersave(dev);
14865 }
14866 
14867 void intel_modeset_init(struct drm_device *dev)
14868 {
14869 	struct drm_i915_private *dev_priv = dev->dev_private;
14870 	int sprite, ret;
14871 	enum i915_pipe pipe;
14872 	struct intel_crtc *crtc;
14873 
14874 	drm_mode_config_init(dev);
14875 
14876 	dev->mode_config.min_width = 0;
14877 	dev->mode_config.min_height = 0;
14878 
14879 	dev->mode_config.preferred_depth = 24;
14880 	dev->mode_config.prefer_shadow = 1;
14881 
14882 	dev->mode_config.allow_fb_modifiers = true;
14883 
14884 	dev->mode_config.funcs = &intel_mode_funcs;
14885 
14886 	intel_init_quirks(dev);
14887 
14888 	intel_init_pm(dev);
14889 
14890 	if (INTEL_INFO(dev)->num_pipes == 0)
14891 		return;
14892 
14893 	/*
14894 	 * There may be no VBT; and if the BIOS enabled SSC we can
14895 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
14896 	 * BIOS isn't using it, don't assume it will work even if the VBT
14897 	 * indicates as much.
14898 	 */
14899 	if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14900 		bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14901 					    DREF_SSC1_ENABLE);
14902 
14903 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14904 			DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14905 				     bios_lvds_use_ssc ? "en" : "dis",
14906 				     dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14907 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14908 		}
14909 	}
14910 
14911 	intel_init_display(dev);
14912 	intel_init_audio(dev);
14913 
14914 	if (IS_GEN2(dev)) {
14915 		dev->mode_config.max_width = 2048;
14916 		dev->mode_config.max_height = 2048;
14917 	} else if (IS_GEN3(dev)) {
14918 		dev->mode_config.max_width = 4096;
14919 		dev->mode_config.max_height = 4096;
14920 	} else {
14921 		dev->mode_config.max_width = 8192;
14922 		dev->mode_config.max_height = 8192;
14923 	}
14924 
14925 	if (IS_845G(dev) || IS_I865G(dev)) {
14926 		dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14927 		dev->mode_config.cursor_height = 1023;
14928 	} else if (IS_GEN2(dev)) {
14929 		dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14930 		dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14931 	} else {
14932 		dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14933 		dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14934 	}
14935 
14936 	dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
14937 
14938 	DRM_DEBUG_KMS("%d display pipe%s available.\n",
14939 		      INTEL_INFO(dev)->num_pipes,
14940 		      INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
14941 
14942 	for_each_pipe(dev_priv, pipe) {
14943 		intel_crtc_init(dev, pipe);
14944 		for_each_sprite(dev_priv, pipe, sprite) {
14945 			ret = intel_plane_init(dev, pipe, sprite);
14946 			if (ret)
14947 				DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
14948 					      pipe_name(pipe), sprite_name(pipe, sprite), ret);
14949 		}
14950 	}
14951 
14952 	intel_update_czclk(dev_priv);
14953 	intel_update_cdclk(dev);
14954 
14955 	intel_shared_dpll_init(dev);
14956 
14957 	/* Just disable it once at startup */
14958 	i915_disable_vga(dev);
14959 	intel_setup_outputs(dev);
14960 
14961 	/* Just in case the BIOS is doing something questionable. */
14962 	intel_fbc_disable(dev_priv);
14963 
14964 	drm_modeset_lock_all(dev);
14965 	intel_modeset_setup_hw_state(dev);
14966 	drm_modeset_unlock_all(dev);
14967 
14968 	for_each_intel_crtc(dev, crtc) {
14969 		struct intel_initial_plane_config plane_config = {};
14970 
14971 		if (!crtc->active)
14972 			continue;
14973 
14974 		/*
14975 		 * Note that reserving the BIOS fb up front prevents us
14976 		 * from stuffing other stolen allocations like the ring
14977 		 * on top.  This prevents some ugliness at boot time, and
14978 		 * can even allow for smooth boot transitions if the BIOS
14979 		 * fb is large enough for the active pipe configuration.
14980 		 */
14981 		dev_priv->display.get_initial_plane_config(crtc,
14982 							   &plane_config);
14983 
14984 		/*
14985 		 * If the fb is shared between multiple heads, we'll
14986 		 * just get the first one.
14987 		 */
14988 		intel_find_initial_plane_obj(crtc, &plane_config);
14989 	}
14990 }
14991 
14992 static void intel_enable_pipe_a(struct drm_device *dev)
14993 {
14994 	struct intel_connector *connector;
14995 	struct drm_connector *crt = NULL;
14996 	struct intel_load_detect_pipe load_detect_temp;
14997 	struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
14998 
14999 	/* We can't just switch on the pipe A, we need to set things up with a
15000 	 * proper mode and output configuration. As a gross hack, enable pipe A
15001 	 * by enabling the load detect pipe once. */
15002 	for_each_intel_connector(dev, connector) {
15003 		if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15004 			crt = &connector->base;
15005 			break;
15006 		}
15007 	}
15008 
15009 	if (!crt)
15010 		return;
15011 
15012 	if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
15013 		intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
15014 }
15015 
15016 static bool
15017 intel_check_plane_mapping(struct intel_crtc *crtc)
15018 {
15019 	struct drm_device *dev = crtc->base.dev;
15020 	struct drm_i915_private *dev_priv = dev->dev_private;
15021 	u32 val;
15022 
15023 	if (INTEL_INFO(dev)->num_pipes == 1)
15024 		return true;
15025 
15026 	val = I915_READ(DSPCNTR(!crtc->plane));
15027 
15028 	if ((val & DISPLAY_PLANE_ENABLE) &&
15029 	    (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15030 		return false;
15031 
15032 	return true;
15033 }
15034 
15035 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
15036 {
15037 	struct drm_device *dev = crtc->base.dev;
15038 	struct intel_encoder *encoder;
15039 
15040 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15041 		return true;
15042 
15043 	return false;
15044 }
15045 
15046 static void intel_sanitize_crtc(struct intel_crtc *crtc)
15047 {
15048 	struct drm_device *dev = crtc->base.dev;
15049 	struct drm_i915_private *dev_priv = dev->dev_private;
15050 	u32 reg;
15051 
15052 	/* Clear any frame start delays used for debugging left by the BIOS */
15053 	reg = PIPECONF(crtc->config->cpu_transcoder);
15054 	I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15055 
15056 	/* restore vblank interrupts to correct state */
15057 	drm_crtc_vblank_reset(&crtc->base);
15058 	if (crtc->active) {
15059 		struct intel_plane *plane;
15060 
15061 		drm_crtc_vblank_on(&crtc->base);
15062 
15063 		/* Disable everything but the primary plane */
15064 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
15065 			if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
15066 				continue;
15067 
15068 			plane->disable_plane(&plane->base, &crtc->base);
15069 		}
15070 	}
15071 
15072 	/* We need to sanitize the plane -> pipe mapping first because this will
15073 	 * disable the crtc (and hence change the state) if it is wrong. Note
15074 	 * that gen4+ has a fixed plane -> pipe mapping.  */
15075 	if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
15076 		bool plane;
15077 
15078 		DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15079 			      crtc->base.base.id);
15080 
15081 		/* Pipe has the wrong plane attached and the plane is active.
15082 		 * Temporarily change the plane mapping and disable everything
15083 		 * ...  */
15084 		plane = crtc->plane;
15085 		to_intel_plane_state(crtc->base.primary->state)->visible = true;
15086 		crtc->plane = !plane;
15087 		intel_crtc_disable_noatomic(&crtc->base);
15088 		crtc->plane = plane;
15089 	}
15090 
15091 	if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15092 	    crtc->pipe == PIPE_A && !crtc->active) {
15093 		/* BIOS forgot to enable pipe A, this mostly happens after
15094 		 * resume. Force-enable the pipe to fix this, the update_dpms
15095 		 * call below we restore the pipe to the right state, but leave
15096 		 * the required bits on. */
15097 		intel_enable_pipe_a(dev);
15098 	}
15099 
15100 	/* Adjust the state of the output pipe according to whether we
15101 	 * have active connectors/encoders. */
15102 	if (!intel_crtc_has_encoders(crtc))
15103 		intel_crtc_disable_noatomic(&crtc->base);
15104 
15105 	if (crtc->active != crtc->base.state->active) {
15106 		struct intel_encoder *encoder;
15107 
15108 		/* This can happen either due to bugs in the get_hw_state
15109 		 * functions or because of calls to intel_crtc_disable_noatomic,
15110 		 * or because the pipe is force-enabled due to the
15111 		 * pipe A quirk. */
15112 		DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15113 			      crtc->base.base.id,
15114 			      crtc->base.state->enable ? "enabled" : "disabled",
15115 			      crtc->active ? "enabled" : "disabled");
15116 
15117 		WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
15118 		crtc->base.state->active = crtc->active;
15119 		crtc->base.enabled = crtc->active;
15120 
15121 		/* Because we only establish the connector -> encoder ->
15122 		 * crtc links if something is active, this means the
15123 		 * crtc is now deactivated. Break the links. connector
15124 		 * -> encoder links are only establish when things are
15125 		 *  actually up, hence no need to break them. */
15126 		WARN_ON(crtc->active);
15127 
15128 		for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15129 			encoder->base.crtc = NULL;
15130 	}
15131 
15132 	if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
15133 		/*
15134 		 * We start out with underrun reporting disabled to avoid races.
15135 		 * For correct bookkeeping mark this on active crtcs.
15136 		 *
15137 		 * Also on gmch platforms we dont have any hardware bits to
15138 		 * disable the underrun reporting. Which means we need to start
15139 		 * out with underrun reporting disabled also on inactive pipes,
15140 		 * since otherwise we'll complain about the garbage we read when
15141 		 * e.g. coming up after runtime pm.
15142 		 *
15143 		 * No protection against concurrent access is required - at
15144 		 * worst a fifo underrun happens which also sets this to false.
15145 		 */
15146 		crtc->cpu_fifo_underrun_disabled = true;
15147 		crtc->pch_fifo_underrun_disabled = true;
15148 	}
15149 }
15150 
15151 static void intel_sanitize_encoder(struct intel_encoder *encoder)
15152 {
15153 	struct intel_connector *connector;
15154 	struct drm_device *dev = encoder->base.dev;
15155 	bool active = false;
15156 
15157 	/* We need to check both for a crtc link (meaning that the
15158 	 * encoder is active and trying to read from a pipe) and the
15159 	 * pipe itself being active. */
15160 	bool has_active_crtc = encoder->base.crtc &&
15161 		to_intel_crtc(encoder->base.crtc)->active;
15162 
15163 	for_each_intel_connector(dev, connector) {
15164 		if (connector->base.encoder != &encoder->base)
15165 			continue;
15166 
15167 		active = true;
15168 		break;
15169 	}
15170 
15171 	if (active && !has_active_crtc) {
15172 		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15173 			      encoder->base.base.id,
15174 			      encoder->base.name);
15175 
15176 		/* Connector is active, but has no active pipe. This is
15177 		 * fallout from our resume register restoring. Disable
15178 		 * the encoder manually again. */
15179 		if (encoder->base.crtc) {
15180 			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15181 				      encoder->base.base.id,
15182 				      encoder->base.name);
15183 			encoder->disable(encoder);
15184 			if (encoder->post_disable)
15185 				encoder->post_disable(encoder);
15186 		}
15187 		encoder->base.crtc = NULL;
15188 
15189 		/* Inconsistent output/port/pipe state happens presumably due to
15190 		 * a bug in one of the get_hw_state functions. Or someplace else
15191 		 * in our code, like the register restore mess on resume. Clamp
15192 		 * things to off as a safer default. */
15193 		for_each_intel_connector(dev, connector) {
15194 			if (connector->encoder != encoder)
15195 				continue;
15196 			connector->base.dpms = DRM_MODE_DPMS_OFF;
15197 			connector->base.encoder = NULL;
15198 		}
15199 	}
15200 	/* Enabled encoders without active connectors will be fixed in
15201 	 * the crtc fixup. */
15202 }
15203 
15204 void i915_redisable_vga_power_on(struct drm_device *dev)
15205 {
15206 	struct drm_i915_private *dev_priv = dev->dev_private;
15207 	u32 vga_reg = i915_vgacntrl_reg(dev);
15208 
15209 	if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15210 		DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15211 		i915_disable_vga(dev);
15212 	}
15213 }
15214 
15215 void i915_redisable_vga(struct drm_device *dev)
15216 {
15217 	struct drm_i915_private *dev_priv = dev->dev_private;
15218 
15219 	/* This function can be called both from intel_modeset_setup_hw_state or
15220 	 * at a very early point in our resume sequence, where the power well
15221 	 * structures are not yet restored. Since this function is at a very
15222 	 * paranoid "someone might have enabled VGA while we were not looking"
15223 	 * level, just check if the power well is enabled instead of trying to
15224 	 * follow the "don't touch the power well if we don't need it" policy
15225 	 * the rest of the driver uses. */
15226 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
15227 		return;
15228 
15229 	i915_redisable_vga_power_on(dev);
15230 }
15231 
15232 static bool primary_get_hw_state(struct intel_plane *plane)
15233 {
15234 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15235 
15236 	return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
15237 }
15238 
15239 /* FIXME read out full plane state for all planes */
15240 static void readout_plane_state(struct intel_crtc *crtc)
15241 {
15242 	struct drm_plane *primary = crtc->base.primary;
15243 	struct intel_plane_state *plane_state =
15244 		to_intel_plane_state(primary->state);
15245 
15246 	plane_state->visible =
15247 		primary_get_hw_state(to_intel_plane(primary));
15248 
15249 	if (plane_state->visible)
15250 		crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
15251 }
15252 
15253 static void intel_modeset_readout_hw_state(struct drm_device *dev)
15254 {
15255 	struct drm_i915_private *dev_priv = dev->dev_private;
15256 	enum i915_pipe pipe;
15257 	struct intel_crtc *crtc;
15258 	struct intel_encoder *encoder;
15259 	struct intel_connector *connector;
15260 	int i;
15261 
15262 	for_each_intel_crtc(dev, crtc) {
15263 		__drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
15264 		memset(crtc->config, 0, sizeof(*crtc->config));
15265 		crtc->config->base.crtc = &crtc->base;
15266 
15267 		crtc->active = dev_priv->display.get_pipe_config(crtc,
15268 								 crtc->config);
15269 
15270 		crtc->base.state->active = crtc->active;
15271 		crtc->base.enabled = crtc->active;
15272 
15273 		readout_plane_state(crtc);
15274 
15275 		DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15276 			      crtc->base.base.id,
15277 			      crtc->active ? "enabled" : "disabled");
15278 	}
15279 
15280 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15281 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15282 
15283 		pll->on = pll->get_hw_state(dev_priv, pll,
15284 					    &pll->config.hw_state);
15285 		pll->active = 0;
15286 		pll->config.crtc_mask = 0;
15287 		for_each_intel_crtc(dev, crtc) {
15288 			if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
15289 				pll->active++;
15290 				pll->config.crtc_mask |= 1 << crtc->pipe;
15291 			}
15292 		}
15293 
15294 		DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
15295 			      pll->name, pll->config.crtc_mask, pll->on);
15296 
15297 		if (pll->config.crtc_mask)
15298 			intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
15299 	}
15300 
15301 	for_each_intel_encoder(dev, encoder) {
15302 		pipe = 0;
15303 
15304 		if (encoder->get_hw_state(encoder, &pipe)) {
15305 			crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15306 			encoder->base.crtc = &crtc->base;
15307 			encoder->get_config(encoder, crtc->config);
15308 		} else {
15309 			encoder->base.crtc = NULL;
15310 		}
15311 
15312 		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
15313 			      encoder->base.base.id,
15314 			      encoder->base.name,
15315 			      encoder->base.crtc ? "enabled" : "disabled",
15316 			      pipe_name(pipe));
15317 	}
15318 
15319 	for_each_intel_connector(dev, connector) {
15320 		if (connector->get_hw_state(connector)) {
15321 			connector->base.dpms = DRM_MODE_DPMS_ON;
15322 			connector->base.encoder = &connector->encoder->base;
15323 		} else {
15324 			connector->base.dpms = DRM_MODE_DPMS_OFF;
15325 			connector->base.encoder = NULL;
15326 		}
15327 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15328 			      connector->base.base.id,
15329 			      connector->base.name,
15330 			      connector->base.encoder ? "enabled" : "disabled");
15331 	}
15332 
15333 	for_each_intel_crtc(dev, crtc) {
15334 		crtc->base.hwmode = crtc->config->base.adjusted_mode;
15335 
15336 		memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15337 		if (crtc->base.state->active) {
15338 			intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15339 			intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15340 			WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15341 
15342 			/*
15343 			 * The initial mode needs to be set in order to keep
15344 			 * the atomic core happy. It wants a valid mode if the
15345 			 * crtc's enabled, so we do the above call.
15346 			 *
15347 			 * At this point some state updated by the connectors
15348 			 * in their ->detect() callback has not run yet, so
15349 			 * no recalculation can be done yet.
15350 			 *
15351 			 * Even if we could do a recalculation and modeset
15352 			 * right now it would cause a double modeset if
15353 			 * fbdev or userspace chooses a different initial mode.
15354 			 *
15355 			 * If that happens, someone indicated they wanted a
15356 			 * mode change, which means it's safe to do a full
15357 			 * recalculation.
15358 			 */
15359 			crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
15360 
15361 			drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
15362 			update_scanline_offset(crtc);
15363 		}
15364 	}
15365 }
15366 
15367 /* Scan out the current hw modeset state,
15368  * and sanitizes it to the current state
15369  */
15370 static void
15371 intel_modeset_setup_hw_state(struct drm_device *dev)
15372 {
15373 	struct drm_i915_private *dev_priv = dev->dev_private;
15374 	enum i915_pipe pipe;
15375 	struct intel_crtc *crtc;
15376 	struct intel_encoder *encoder;
15377 	int i;
15378 
15379 	intel_modeset_readout_hw_state(dev);
15380 
15381 	/* HW state is read out, now we need to sanitize this mess. */
15382 	for_each_intel_encoder(dev, encoder) {
15383 		intel_sanitize_encoder(encoder);
15384 	}
15385 
15386 	for_each_pipe(dev_priv, pipe) {
15387 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15388 		intel_sanitize_crtc(crtc);
15389 		intel_dump_pipe_config(crtc, crtc->config,
15390 				       "[setup_hw_state]");
15391 	}
15392 
15393 	intel_modeset_update_connector_atomic_state(dev);
15394 
15395 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15396 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15397 
15398 		if (!pll->on || pll->active)
15399 			continue;
15400 
15401 		DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15402 
15403 		pll->disable(dev_priv, pll);
15404 		pll->on = false;
15405 	}
15406 
15407 	if (IS_VALLEYVIEW(dev))
15408 		vlv_wm_get_hw_state(dev);
15409 	else if (IS_GEN9(dev))
15410 		skl_wm_get_hw_state(dev);
15411 	else if (HAS_PCH_SPLIT(dev))
15412 		ilk_wm_get_hw_state(dev);
15413 
15414 	for_each_intel_crtc(dev, crtc) {
15415 		unsigned long put_domains;
15416 
15417 		put_domains = modeset_get_crtc_power_domains(&crtc->base);
15418 		if (WARN_ON(put_domains))
15419 			modeset_put_power_domains(dev_priv, put_domains);
15420 	}
15421 	intel_display_set_init_power(dev_priv, false);
15422 }
15423 
15424 void intel_display_resume(struct drm_device *dev)
15425 {
15426 	struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15427 	struct intel_connector *conn;
15428 	struct intel_plane *plane;
15429 	struct drm_crtc *crtc;
15430 	int ret;
15431 
15432 	if (!state)
15433 		return;
15434 
15435 	state->acquire_ctx = dev->mode_config.acquire_ctx;
15436 
15437 	/* preserve complete old state, including dpll */
15438 	intel_atomic_get_shared_dpll_state(state);
15439 
15440 	for_each_crtc(dev, crtc) {
15441 		struct drm_crtc_state *crtc_state =
15442 			drm_atomic_get_crtc_state(state, crtc);
15443 
15444 		ret = PTR_ERR_OR_ZERO(crtc_state);
15445 		if (ret)
15446 			goto err;
15447 
15448 		/* force a restore */
15449 		crtc_state->mode_changed = true;
15450 	}
15451 
15452 	for_each_intel_plane(dev, plane) {
15453 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15454 		if (ret)
15455 			goto err;
15456 	}
15457 
15458 	for_each_intel_connector(dev, conn) {
15459 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15460 		if (ret)
15461 			goto err;
15462 	}
15463 
15464 	intel_modeset_setup_hw_state(dev);
15465 
15466 	i915_redisable_vga(dev);
15467 	ret = drm_atomic_commit(state);
15468 	if (!ret)
15469 		return;
15470 
15471 err:
15472 	DRM_ERROR("Restoring old state failed with %i\n", ret);
15473 	drm_atomic_state_free(state);
15474 }
15475 
15476 void intel_modeset_gem_init(struct drm_device *dev)
15477 {
15478 	struct drm_crtc *c;
15479 	struct drm_i915_gem_object *obj;
15480 	int ret;
15481 
15482 	mutex_lock(&dev->struct_mutex);
15483 	intel_init_gt_powersave(dev);
15484 	mutex_unlock(&dev->struct_mutex);
15485 
15486 	intel_modeset_init_hw(dev);
15487 
15488 	intel_setup_overlay(dev);
15489 
15490 	/*
15491 	 * Make sure any fbs we allocated at startup are properly
15492 	 * pinned & fenced.  When we do the allocation it's too early
15493 	 * for this.
15494 	 */
15495 	for_each_crtc(dev, c) {
15496 		obj = intel_fb_obj(c->primary->fb);
15497 		if (obj == NULL)
15498 			continue;
15499 
15500 		mutex_lock(&dev->struct_mutex);
15501 		ret = intel_pin_and_fence_fb_obj(c->primary,
15502 						 c->primary->fb,
15503 						 c->primary->state,
15504 						 NULL, NULL);
15505 		mutex_unlock(&dev->struct_mutex);
15506 		if (ret) {
15507 			DRM_ERROR("failed to pin boot fb on pipe %d\n",
15508 				  to_intel_crtc(c)->pipe);
15509 			drm_framebuffer_unreference(c->primary->fb);
15510 			c->primary->fb = NULL;
15511 			c->primary->crtc = c->primary->state->crtc = NULL;
15512 			update_state_fb(c->primary);
15513 			c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
15514 		}
15515 	}
15516 
15517 	intel_backlight_register(dev);
15518 }
15519 
15520 void intel_connector_unregister(struct intel_connector *intel_connector)
15521 {
15522 	struct drm_connector *connector = &intel_connector->base;
15523 
15524 	intel_panel_destroy_backlight(connector);
15525 	drm_connector_unregister(connector);
15526 }
15527 
15528 void intel_modeset_cleanup(struct drm_device *dev)
15529 {
15530 	struct drm_i915_private *dev_priv = dev->dev_private;
15531 	struct drm_connector *connector;
15532 
15533 	intel_disable_gt_powersave(dev);
15534 
15535 	intel_backlight_unregister(dev);
15536 
15537 	/*
15538 	 * Interrupts and polling as the first thing to avoid creating havoc.
15539 	 * Too much stuff here (turning of connectors, ...) would
15540 	 * experience fancy races otherwise.
15541 	 */
15542 	intel_irq_uninstall(dev_priv);
15543 
15544 	/*
15545 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
15546 	 * poll handlers. Hence disable polling after hpd handling is shut down.
15547 	 */
15548 	drm_kms_helper_poll_fini(dev);
15549 
15550 	intel_unregister_dsm_handler();
15551 
15552 	intel_fbc_disable(dev_priv);
15553 
15554 	/* flush any delayed tasks or pending work */
15555 	flush_scheduled_work();
15556 
15557 	/* destroy the backlight and sysfs files before encoders/connectors */
15558 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
15559 		struct intel_connector *intel_connector;
15560 
15561 		intel_connector = to_intel_connector(connector);
15562 		intel_connector->unregister(intel_connector);
15563 	}
15564 
15565 	drm_mode_config_cleanup(dev);
15566 
15567 	intel_cleanup_overlay(dev);
15568 
15569 	mutex_lock(&dev->struct_mutex);
15570 	intel_cleanup_gt_powersave(dev);
15571 	mutex_unlock(&dev->struct_mutex);
15572 }
15573 
15574 /*
15575  * Return which encoder is currently attached for connector.
15576  */
15577 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
15578 {
15579 	return &intel_attached_encoder(connector)->base;
15580 }
15581 
15582 void intel_connector_attach_encoder(struct intel_connector *connector,
15583 				    struct intel_encoder *encoder)
15584 {
15585 	connector->encoder = encoder;
15586 	drm_mode_connector_attach_encoder(&connector->base,
15587 					  &encoder->base);
15588 }
15589 
15590 /*
15591  * set vga decode state - true == enable VGA decode
15592  */
15593 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15594 {
15595 	struct drm_i915_private *dev_priv = dev->dev_private;
15596 	unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
15597 	u16 gmch_ctrl;
15598 
15599 	if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15600 		DRM_ERROR("failed to read control word\n");
15601 		return -EIO;
15602 	}
15603 
15604 	if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15605 		return 0;
15606 
15607 	if (state)
15608 		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15609 	else
15610 		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
15611 
15612 	if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15613 		DRM_ERROR("failed to write control word\n");
15614 		return -EIO;
15615 	}
15616 
15617 	return 0;
15618 }
15619 
15620 #if 0
15621 struct intel_display_error_state {
15622 
15623 	u32 power_well_driver;
15624 
15625 	int num_transcoders;
15626 
15627 	struct intel_cursor_error_state {
15628 		u32 control;
15629 		u32 position;
15630 		u32 base;
15631 		u32 size;
15632 	} cursor[I915_MAX_PIPES];
15633 
15634 	struct intel_pipe_error_state {
15635 		bool power_domain_on;
15636 		u32 source;
15637 		u32 stat;
15638 	} pipe[I915_MAX_PIPES];
15639 
15640 	struct intel_plane_error_state {
15641 		u32 control;
15642 		u32 stride;
15643 		u32 size;
15644 		u32 pos;
15645 		u32 addr;
15646 		u32 surface;
15647 		u32 tile_offset;
15648 	} plane[I915_MAX_PIPES];
15649 
15650 	struct intel_transcoder_error_state {
15651 		bool power_domain_on;
15652 		enum transcoder cpu_transcoder;
15653 
15654 		u32 conf;
15655 
15656 		u32 htotal;
15657 		u32 hblank;
15658 		u32 hsync;
15659 		u32 vtotal;
15660 		u32 vblank;
15661 		u32 vsync;
15662 	} transcoder[4];
15663 };
15664 
15665 struct intel_display_error_state *
15666 intel_display_capture_error_state(struct drm_device *dev)
15667 {
15668 	struct drm_i915_private *dev_priv = dev->dev_private;
15669 	struct intel_display_error_state *error;
15670 	int transcoders[] = {
15671 		TRANSCODER_A,
15672 		TRANSCODER_B,
15673 		TRANSCODER_C,
15674 		TRANSCODER_EDP,
15675 	};
15676 	int i;
15677 
15678 	if (INTEL_INFO(dev)->num_pipes == 0)
15679 		return NULL;
15680 
15681 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
15682 	if (error == NULL)
15683 		return NULL;
15684 
15685 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
15686 		error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15687 
15688 	for_each_pipe(dev_priv, i) {
15689 		error->pipe[i].power_domain_on =
15690 			__intel_display_power_is_enabled(dev_priv,
15691 							 POWER_DOMAIN_PIPE(i));
15692 		if (!error->pipe[i].power_domain_on)
15693 			continue;
15694 
15695 		error->cursor[i].control = I915_READ(CURCNTR(i));
15696 		error->cursor[i].position = I915_READ(CURPOS(i));
15697 		error->cursor[i].base = I915_READ(CURBASE(i));
15698 
15699 		error->plane[i].control = I915_READ(DSPCNTR(i));
15700 		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
15701 		if (INTEL_INFO(dev)->gen <= 3) {
15702 			error->plane[i].size = I915_READ(DSPSIZE(i));
15703 			error->plane[i].pos = I915_READ(DSPPOS(i));
15704 		}
15705 		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15706 			error->plane[i].addr = I915_READ(DSPADDR(i));
15707 		if (INTEL_INFO(dev)->gen >= 4) {
15708 			error->plane[i].surface = I915_READ(DSPSURF(i));
15709 			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15710 		}
15711 
15712 		error->pipe[i].source = I915_READ(PIPESRC(i));
15713 
15714 		if (HAS_GMCH_DISPLAY(dev))
15715 			error->pipe[i].stat = I915_READ(PIPESTAT(i));
15716 	}
15717 
15718 	error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15719 	if (HAS_DDI(dev_priv->dev))
15720 		error->num_transcoders++; /* Account for eDP. */
15721 
15722 	for (i = 0; i < error->num_transcoders; i++) {
15723 		enum transcoder cpu_transcoder = transcoders[i];
15724 
15725 		error->transcoder[i].power_domain_on =
15726 			__intel_display_power_is_enabled(dev_priv,
15727 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
15728 		if (!error->transcoder[i].power_domain_on)
15729 			continue;
15730 
15731 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
15732 
15733 		error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15734 		error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15735 		error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15736 		error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15737 		error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15738 		error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15739 		error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
15740 	}
15741 
15742 	return error;
15743 }
15744 
15745 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15746 
15747 void
15748 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
15749 				struct drm_device *dev,
15750 				struct intel_display_error_state *error)
15751 {
15752 	struct drm_i915_private *dev_priv = dev->dev_private;
15753 	int i;
15754 
15755 	if (!error)
15756 		return;
15757 
15758 	err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
15759 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
15760 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
15761 			   error->power_well_driver);
15762 	for_each_pipe(dev_priv, i) {
15763 		err_printf(m, "Pipe [%d]:\n", i);
15764 		err_printf(m, "  Power: %s\n",
15765 			   error->pipe[i].power_domain_on ? "on" : "off");
15766 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
15767 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
15768 
15769 		err_printf(m, "Plane [%d]:\n", i);
15770 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
15771 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
15772 		if (INTEL_INFO(dev)->gen <= 3) {
15773 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
15774 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
15775 		}
15776 		if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15777 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
15778 		if (INTEL_INFO(dev)->gen >= 4) {
15779 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
15780 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
15781 		}
15782 
15783 		err_printf(m, "Cursor [%d]:\n", i);
15784 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
15785 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
15786 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
15787 	}
15788 
15789 	for (i = 0; i < error->num_transcoders; i++) {
15790 		err_printf(m, "CPU transcoder: %c\n",
15791 			   transcoder_name(error->transcoder[i].cpu_transcoder));
15792 		err_printf(m, "  Power: %s\n",
15793 			   error->transcoder[i].power_domain_on ? "on" : "off");
15794 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
15795 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
15796 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
15797 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
15798 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
15799 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
15800 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
15801 	}
15802 }
15803 #endif
15804 
15805 void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15806 {
15807 	struct intel_crtc *crtc;
15808 
15809 	for_each_intel_crtc(dev, crtc) {
15810 		struct intel_unpin_work *work;
15811 
15812 		spin_lock_irq(&dev->event_lock);
15813 
15814 		work = crtc->unpin_work;
15815 
15816 		if (work && work->event &&
15817 		    work->event->base.file_priv == file) {
15818 			kfree(work->event);
15819 			work->event = NULL;
15820 		}
15821 
15822 		spin_unlock_irq(&dev->event_lock);
15823 	}
15824 }
15825