1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/i2c.h>
28 #include <linux/input.h>
29 #include <linux/intel-iommu.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dma-resv.h>
33 #include <linux/slab.h>
34 #include <linux/vgaarb.h>
35 
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_atomic_uapi.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_plane_helper.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/drm_rect.h>
45 #include <drm/i915_drm.h>
46 
47 #include "display/intel_crt.h"
48 #include "display/intel_ddi.h"
49 #include "display/intel_dp.h"
50 #include "display/intel_dsi.h"
51 #include "display/intel_dvo.h"
52 #include "display/intel_gmbus.h"
53 #include "display/intel_hdmi.h"
54 #include "display/intel_lvds.h"
55 #include "display/intel_sdvo.h"
56 #include "display/intel_tv.h"
57 #include "display/intel_vdsc.h"
58 
59 #include "i915_drv.h"
60 #include "i915_trace.h"
61 #include "intel_acpi.h"
62 #include "intel_atomic.h"
63 #include "intel_atomic_plane.h"
64 #include "intel_bw.h"
65 #include "intel_cdclk.h"
66 #include "intel_color.h"
67 #include "intel_display_types.h"
68 #include "intel_fbc.h"
69 #include "intel_fbdev.h"
70 #include "intel_fifo_underrun.h"
71 #include "intel_frontbuffer.h"
72 #include "intel_hdcp.h"
73 #include "intel_hotplug.h"
74 #include "intel_overlay.h"
75 #include "intel_pipe_crc.h"
76 #include "intel_pm.h"
77 #include "intel_psr.h"
78 #include "intel_quirks.h"
79 #include "intel_sideband.h"
80 #include "intel_sprite.h"
81 #include "intel_tc.h"
82 
83 /* Primary plane formats for gen <= 3 */
84 static const u32 i8xx_primary_formats[] = {
85 	DRM_FORMAT_C8,
86 	DRM_FORMAT_RGB565,
87 	DRM_FORMAT_XRGB1555,
88 	DRM_FORMAT_XRGB8888,
89 };
90 
91 /* Primary plane formats for gen >= 4 */
92 static const u32 i965_primary_formats[] = {
93 	DRM_FORMAT_C8,
94 	DRM_FORMAT_RGB565,
95 	DRM_FORMAT_XRGB8888,
96 	DRM_FORMAT_XBGR8888,
97 	DRM_FORMAT_XRGB2101010,
98 	DRM_FORMAT_XBGR2101010,
99 };
100 
101 static const u64 i9xx_format_modifiers[] = {
102 	I915_FORMAT_MOD_X_TILED,
103 	DRM_FORMAT_MOD_LINEAR,
104 	DRM_FORMAT_MOD_INVALID
105 };
106 
107 /* Cursor formats */
108 static const u32 intel_cursor_formats[] = {
109 	DRM_FORMAT_ARGB8888,
110 };
111 
112 static const u64 cursor_format_modifiers[] = {
113 	DRM_FORMAT_MOD_LINEAR,
114 	DRM_FORMAT_MOD_INVALID
115 };
116 
117 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
118 				struct intel_crtc_state *pipe_config);
119 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
120 				   struct intel_crtc_state *pipe_config);
121 
122 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
123 				  struct drm_i915_gem_object *obj,
124 				  struct drm_mode_fb_cmd2 *mode_cmd);
125 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
126 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
127 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
128 					 const struct intel_link_m_n *m_n,
129 					 const struct intel_link_m_n *m2_n2);
130 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
131 static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state);
132 static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state);
133 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
134 static void vlv_prepare_pll(struct intel_crtc *crtc,
135 			    const struct intel_crtc_state *pipe_config);
136 static void chv_prepare_pll(struct intel_crtc *crtc,
137 			    const struct intel_crtc_state *pipe_config);
138 static void intel_begin_crtc_commit(struct intel_atomic_state *, struct intel_crtc *);
139 static void intel_finish_crtc_commit(struct intel_atomic_state *, struct intel_crtc *);
140 static void intel_crtc_init_scalers(struct intel_crtc *crtc,
141 				    struct intel_crtc_state *crtc_state);
142 static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state);
143 static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state);
144 static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state);
145 static void intel_modeset_setup_hw_state(struct drm_device *dev,
146 					 struct drm_modeset_acquire_ctx *ctx);
147 static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
148 
149 struct intel_limit {
150 	struct {
151 		int min, max;
152 	} dot, vco, n, m, m1, m2, p, p1;
153 
154 	struct {
155 		int dot_limit;
156 		int p2_slow, p2_fast;
157 	} p2;
158 };
159 
160 /* returns HPLL frequency in kHz */
161 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
162 {
163 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
164 
165 	/* Obtain SKU information */
166 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
167 		CCK_FUSE_HPLL_FREQ_MASK;
168 
169 	return vco_freq[hpll_freq] * 1000;
170 }
171 
172 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
173 		      const char *name, u32 reg, int ref_freq)
174 {
175 	u32 val;
176 	int divider;
177 
178 	val = vlv_cck_read(dev_priv, reg);
179 	divider = val & CCK_FREQUENCY_VALUES;
180 
181 	WARN((val & CCK_FREQUENCY_STATUS) !=
182 	     (divider << CCK_FREQUENCY_STATUS_SHIFT),
183 	     "%s change in progress\n", name);
184 
185 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
186 }
187 
188 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
189 			   const char *name, u32 reg)
190 {
191 	int hpll;
192 
193 	vlv_cck_get(dev_priv);
194 
195 	if (dev_priv->hpll_freq == 0)
196 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
197 
198 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
199 
200 	vlv_cck_put(dev_priv);
201 
202 	return hpll;
203 }
204 
205 static void intel_update_czclk(struct drm_i915_private *dev_priv)
206 {
207 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
208 		return;
209 
210 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
211 						      CCK_CZ_CLOCK_CONTROL);
212 
213 	DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
214 }
215 
216 static inline u32 /* units of 100MHz */
217 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
218 		    const struct intel_crtc_state *pipe_config)
219 {
220 	if (HAS_DDI(dev_priv))
221 		return pipe_config->port_clock; /* SPLL */
222 	else
223 		return dev_priv->fdi_pll_freq;
224 }
225 
226 static const struct intel_limit intel_limits_i8xx_dac = {
227 	.dot = { .min = 25000, .max = 350000 },
228 	.vco = { .min = 908000, .max = 1512000 },
229 	.n = { .min = 2, .max = 16 },
230 	.m = { .min = 96, .max = 140 },
231 	.m1 = { .min = 18, .max = 26 },
232 	.m2 = { .min = 6, .max = 16 },
233 	.p = { .min = 4, .max = 128 },
234 	.p1 = { .min = 2, .max = 33 },
235 	.p2 = { .dot_limit = 165000,
236 		.p2_slow = 4, .p2_fast = 2 },
237 };
238 
239 static const struct intel_limit intel_limits_i8xx_dvo = {
240 	.dot = { .min = 25000, .max = 350000 },
241 	.vco = { .min = 908000, .max = 1512000 },
242 	.n = { .min = 2, .max = 16 },
243 	.m = { .min = 96, .max = 140 },
244 	.m1 = { .min = 18, .max = 26 },
245 	.m2 = { .min = 6, .max = 16 },
246 	.p = { .min = 4, .max = 128 },
247 	.p1 = { .min = 2, .max = 33 },
248 	.p2 = { .dot_limit = 165000,
249 		.p2_slow = 4, .p2_fast = 4 },
250 };
251 
252 static const struct intel_limit intel_limits_i8xx_lvds = {
253 	.dot = { .min = 25000, .max = 350000 },
254 	.vco = { .min = 908000, .max = 1512000 },
255 	.n = { .min = 2, .max = 16 },
256 	.m = { .min = 96, .max = 140 },
257 	.m1 = { .min = 18, .max = 26 },
258 	.m2 = { .min = 6, .max = 16 },
259 	.p = { .min = 4, .max = 128 },
260 	.p1 = { .min = 1, .max = 6 },
261 	.p2 = { .dot_limit = 165000,
262 		.p2_slow = 14, .p2_fast = 7 },
263 };
264 
265 static const struct intel_limit intel_limits_i9xx_sdvo = {
266 	.dot = { .min = 20000, .max = 400000 },
267 	.vco = { .min = 1400000, .max = 2800000 },
268 	.n = { .min = 1, .max = 6 },
269 	.m = { .min = 70, .max = 120 },
270 	.m1 = { .min = 8, .max = 18 },
271 	.m2 = { .min = 3, .max = 7 },
272 	.p = { .min = 5, .max = 80 },
273 	.p1 = { .min = 1, .max = 8 },
274 	.p2 = { .dot_limit = 200000,
275 		.p2_slow = 10, .p2_fast = 5 },
276 };
277 
278 static const struct intel_limit intel_limits_i9xx_lvds = {
279 	.dot = { .min = 20000, .max = 400000 },
280 	.vco = { .min = 1400000, .max = 2800000 },
281 	.n = { .min = 1, .max = 6 },
282 	.m = { .min = 70, .max = 120 },
283 	.m1 = { .min = 8, .max = 18 },
284 	.m2 = { .min = 3, .max = 7 },
285 	.p = { .min = 7, .max = 98 },
286 	.p1 = { .min = 1, .max = 8 },
287 	.p2 = { .dot_limit = 112000,
288 		.p2_slow = 14, .p2_fast = 7 },
289 };
290 
291 
292 static const struct intel_limit intel_limits_g4x_sdvo = {
293 	.dot = { .min = 25000, .max = 270000 },
294 	.vco = { .min = 1750000, .max = 3500000},
295 	.n = { .min = 1, .max = 4 },
296 	.m = { .min = 104, .max = 138 },
297 	.m1 = { .min = 17, .max = 23 },
298 	.m2 = { .min = 5, .max = 11 },
299 	.p = { .min = 10, .max = 30 },
300 	.p1 = { .min = 1, .max = 3},
301 	.p2 = { .dot_limit = 270000,
302 		.p2_slow = 10,
303 		.p2_fast = 10
304 	},
305 };
306 
307 static const struct intel_limit intel_limits_g4x_hdmi = {
308 	.dot = { .min = 22000, .max = 400000 },
309 	.vco = { .min = 1750000, .max = 3500000},
310 	.n = { .min = 1, .max = 4 },
311 	.m = { .min = 104, .max = 138 },
312 	.m1 = { .min = 16, .max = 23 },
313 	.m2 = { .min = 5, .max = 11 },
314 	.p = { .min = 5, .max = 80 },
315 	.p1 = { .min = 1, .max = 8},
316 	.p2 = { .dot_limit = 165000,
317 		.p2_slow = 10, .p2_fast = 5 },
318 };
319 
320 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
321 	.dot = { .min = 20000, .max = 115000 },
322 	.vco = { .min = 1750000, .max = 3500000 },
323 	.n = { .min = 1, .max = 3 },
324 	.m = { .min = 104, .max = 138 },
325 	.m1 = { .min = 17, .max = 23 },
326 	.m2 = { .min = 5, .max = 11 },
327 	.p = { .min = 28, .max = 112 },
328 	.p1 = { .min = 2, .max = 8 },
329 	.p2 = { .dot_limit = 0,
330 		.p2_slow = 14, .p2_fast = 14
331 	},
332 };
333 
334 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
335 	.dot = { .min = 80000, .max = 224000 },
336 	.vco = { .min = 1750000, .max = 3500000 },
337 	.n = { .min = 1, .max = 3 },
338 	.m = { .min = 104, .max = 138 },
339 	.m1 = { .min = 17, .max = 23 },
340 	.m2 = { .min = 5, .max = 11 },
341 	.p = { .min = 14, .max = 42 },
342 	.p1 = { .min = 2, .max = 6 },
343 	.p2 = { .dot_limit = 0,
344 		.p2_slow = 7, .p2_fast = 7
345 	},
346 };
347 
348 static const struct intel_limit intel_limits_pineview_sdvo = {
349 	.dot = { .min = 20000, .max = 400000},
350 	.vco = { .min = 1700000, .max = 3500000 },
351 	/* Pineview's Ncounter is a ring counter */
352 	.n = { .min = 3, .max = 6 },
353 	.m = { .min = 2, .max = 256 },
354 	/* Pineview only has one combined m divider, which we treat as m2. */
355 	.m1 = { .min = 0, .max = 0 },
356 	.m2 = { .min = 0, .max = 254 },
357 	.p = { .min = 5, .max = 80 },
358 	.p1 = { .min = 1, .max = 8 },
359 	.p2 = { .dot_limit = 200000,
360 		.p2_slow = 10, .p2_fast = 5 },
361 };
362 
363 static const struct intel_limit intel_limits_pineview_lvds = {
364 	.dot = { .min = 20000, .max = 400000 },
365 	.vco = { .min = 1700000, .max = 3500000 },
366 	.n = { .min = 3, .max = 6 },
367 	.m = { .min = 2, .max = 256 },
368 	.m1 = { .min = 0, .max = 0 },
369 	.m2 = { .min = 0, .max = 254 },
370 	.p = { .min = 7, .max = 112 },
371 	.p1 = { .min = 1, .max = 8 },
372 	.p2 = { .dot_limit = 112000,
373 		.p2_slow = 14, .p2_fast = 14 },
374 };
375 
376 /* Ironlake / Sandybridge
377  *
378  * We calculate clock using (register_value + 2) for N/M1/M2, so here
379  * the range value for them is (actual_value - 2).
380  */
381 static const struct intel_limit intel_limits_ironlake_dac = {
382 	.dot = { .min = 25000, .max = 350000 },
383 	.vco = { .min = 1760000, .max = 3510000 },
384 	.n = { .min = 1, .max = 5 },
385 	.m = { .min = 79, .max = 127 },
386 	.m1 = { .min = 12, .max = 22 },
387 	.m2 = { .min = 5, .max = 9 },
388 	.p = { .min = 5, .max = 80 },
389 	.p1 = { .min = 1, .max = 8 },
390 	.p2 = { .dot_limit = 225000,
391 		.p2_slow = 10, .p2_fast = 5 },
392 };
393 
394 static const struct intel_limit intel_limits_ironlake_single_lvds = {
395 	.dot = { .min = 25000, .max = 350000 },
396 	.vco = { .min = 1760000, .max = 3510000 },
397 	.n = { .min = 1, .max = 3 },
398 	.m = { .min = 79, .max = 118 },
399 	.m1 = { .min = 12, .max = 22 },
400 	.m2 = { .min = 5, .max = 9 },
401 	.p = { .min = 28, .max = 112 },
402 	.p1 = { .min = 2, .max = 8 },
403 	.p2 = { .dot_limit = 225000,
404 		.p2_slow = 14, .p2_fast = 14 },
405 };
406 
407 static const struct intel_limit intel_limits_ironlake_dual_lvds = {
408 	.dot = { .min = 25000, .max = 350000 },
409 	.vco = { .min = 1760000, .max = 3510000 },
410 	.n = { .min = 1, .max = 3 },
411 	.m = { .min = 79, .max = 127 },
412 	.m1 = { .min = 12, .max = 22 },
413 	.m2 = { .min = 5, .max = 9 },
414 	.p = { .min = 14, .max = 56 },
415 	.p1 = { .min = 2, .max = 8 },
416 	.p2 = { .dot_limit = 225000,
417 		.p2_slow = 7, .p2_fast = 7 },
418 };
419 
420 /* LVDS 100mhz refclk limits. */
421 static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
422 	.dot = { .min = 25000, .max = 350000 },
423 	.vco = { .min = 1760000, .max = 3510000 },
424 	.n = { .min = 1, .max = 2 },
425 	.m = { .min = 79, .max = 126 },
426 	.m1 = { .min = 12, .max = 22 },
427 	.m2 = { .min = 5, .max = 9 },
428 	.p = { .min = 28, .max = 112 },
429 	.p1 = { .min = 2, .max = 8 },
430 	.p2 = { .dot_limit = 225000,
431 		.p2_slow = 14, .p2_fast = 14 },
432 };
433 
434 static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
435 	.dot = { .min = 25000, .max = 350000 },
436 	.vco = { .min = 1760000, .max = 3510000 },
437 	.n = { .min = 1, .max = 3 },
438 	.m = { .min = 79, .max = 126 },
439 	.m1 = { .min = 12, .max = 22 },
440 	.m2 = { .min = 5, .max = 9 },
441 	.p = { .min = 14, .max = 42 },
442 	.p1 = { .min = 2, .max = 6 },
443 	.p2 = { .dot_limit = 225000,
444 		.p2_slow = 7, .p2_fast = 7 },
445 };
446 
447 static const struct intel_limit intel_limits_vlv = {
448 	 /*
449 	  * These are the data rate limits (measured in fast clocks)
450 	  * since those are the strictest limits we have. The fast
451 	  * clock and actual rate limits are more relaxed, so checking
452 	  * them would make no difference.
453 	  */
454 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
455 	.vco = { .min = 4000000, .max = 6000000 },
456 	.n = { .min = 1, .max = 7 },
457 	.m1 = { .min = 2, .max = 3 },
458 	.m2 = { .min = 11, .max = 156 },
459 	.p1 = { .min = 2, .max = 3 },
460 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
461 };
462 
463 static const struct intel_limit intel_limits_chv = {
464 	/*
465 	 * These are the data rate limits (measured in fast clocks)
466 	 * since those are the strictest limits we have.  The fast
467 	 * clock and actual rate limits are more relaxed, so checking
468 	 * them would make no difference.
469 	 */
470 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
471 	.vco = { .min = 4800000, .max = 6480000 },
472 	.n = { .min = 1, .max = 1 },
473 	.m1 = { .min = 2, .max = 2 },
474 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
475 	.p1 = { .min = 2, .max = 4 },
476 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
477 };
478 
479 static const struct intel_limit intel_limits_bxt = {
480 	/* FIXME: find real dot limits */
481 	.dot = { .min = 0, .max = INT_MAX },
482 	.vco = { .min = 4800000, .max = 6700000 },
483 	.n = { .min = 1, .max = 1 },
484 	.m1 = { .min = 2, .max = 2 },
485 	/* FIXME: find real m2 limits */
486 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
487 	.p1 = { .min = 2, .max = 4 },
488 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
489 };
490 
491 /* WA Display #0827: Gen9:all */
492 static void
493 skl_wa_827(struct drm_i915_private *dev_priv, int pipe, bool enable)
494 {
495 	if (enable)
496 		I915_WRITE(CLKGATE_DIS_PSL(pipe),
497 			   I915_READ(CLKGATE_DIS_PSL(pipe)) |
498 			   DUPS1_GATING_DIS | DUPS2_GATING_DIS);
499 	else
500 		I915_WRITE(CLKGATE_DIS_PSL(pipe),
501 			   I915_READ(CLKGATE_DIS_PSL(pipe)) &
502 			   ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
503 }
504 
505 /* Wa_2006604312:icl */
506 static void
507 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
508 		       bool enable)
509 {
510 	if (enable)
511 		I915_WRITE(CLKGATE_DIS_PSL(pipe),
512 			   I915_READ(CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
513 	else
514 		I915_WRITE(CLKGATE_DIS_PSL(pipe),
515 			   I915_READ(CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
516 }
517 
518 static bool
519 needs_modeset(const struct intel_crtc_state *state)
520 {
521 	return drm_atomic_crtc_needs_modeset(&state->base);
522 }
523 
524 /*
525  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
526  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
527  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
528  * The helpers' return value is the rate of the clock that is fed to the
529  * display engine's pipe which can be the above fast dot clock rate or a
530  * divided-down version of it.
531  */
532 /* m1 is reserved as 0 in Pineview, n is a ring counter */
533 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
534 {
535 	clock->m = clock->m2 + 2;
536 	clock->p = clock->p1 * clock->p2;
537 	if (WARN_ON(clock->n == 0 || clock->p == 0))
538 		return 0;
539 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
540 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
541 
542 	return clock->dot;
543 }
544 
545 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
546 {
547 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
548 }
549 
550 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
551 {
552 	clock->m = i9xx_dpll_compute_m(clock);
553 	clock->p = clock->p1 * clock->p2;
554 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
555 		return 0;
556 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
557 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
558 
559 	return clock->dot;
560 }
561 
562 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
563 {
564 	clock->m = clock->m1 * clock->m2;
565 	clock->p = clock->p1 * clock->p2;
566 	if (WARN_ON(clock->n == 0 || clock->p == 0))
567 		return 0;
568 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
569 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
570 
571 	return clock->dot / 5;
572 }
573 
574 int chv_calc_dpll_params(int refclk, struct dpll *clock)
575 {
576 	clock->m = clock->m1 * clock->m2;
577 	clock->p = clock->p1 * clock->p2;
578 	if (WARN_ON(clock->n == 0 || clock->p == 0))
579 		return 0;
580 	clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
581 					   clock->n << 22);
582 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
583 
584 	return clock->dot / 5;
585 }
586 
587 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
588 
589 /*
590  * Returns whether the given set of divisors are valid for a given refclk with
591  * the given connectors.
592  */
593 static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
594 			       const struct intel_limit *limit,
595 			       const struct dpll *clock)
596 {
597 	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
598 		INTELPllInvalid("n out of range\n");
599 	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
600 		INTELPllInvalid("p1 out of range\n");
601 	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
602 		INTELPllInvalid("m2 out of range\n");
603 	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
604 		INTELPllInvalid("m1 out of range\n");
605 
606 	if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
607 	    !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
608 		if (clock->m1 <= clock->m2)
609 			INTELPllInvalid("m1 <= m2\n");
610 
611 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
612 	    !IS_GEN9_LP(dev_priv)) {
613 		if (clock->p < limit->p.min || limit->p.max < clock->p)
614 			INTELPllInvalid("p out of range\n");
615 		if (clock->m < limit->m.min || limit->m.max < clock->m)
616 			INTELPllInvalid("m out of range\n");
617 	}
618 
619 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
620 		INTELPllInvalid("vco out of range\n");
621 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
622 	 * connector, etc., rather than just a single range.
623 	 */
624 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
625 		INTELPllInvalid("dot out of range\n");
626 
627 	return true;
628 }
629 
630 static int
631 i9xx_select_p2_div(const struct intel_limit *limit,
632 		   const struct intel_crtc_state *crtc_state,
633 		   int target)
634 {
635 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
636 
637 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
638 		/*
639 		 * For LVDS just rely on its current settings for dual-channel.
640 		 * We haven't figured out how to reliably set up different
641 		 * single/dual channel state, if we even can.
642 		 */
643 		if (intel_is_dual_link_lvds(dev_priv))
644 			return limit->p2.p2_fast;
645 		else
646 			return limit->p2.p2_slow;
647 	} else {
648 		if (target < limit->p2.dot_limit)
649 			return limit->p2.p2_slow;
650 		else
651 			return limit->p2.p2_fast;
652 	}
653 }
654 
655 /*
656  * Returns a set of divisors for the desired target clock with the given
657  * refclk, or FALSE.  The returned values represent the clock equation:
658  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
659  *
660  * Target and reference clocks are specified in kHz.
661  *
662  * If match_clock is provided, then best_clock P divider must match the P
663  * divider from @match_clock used for LVDS downclocking.
664  */
665 static bool
666 i9xx_find_best_dpll(const struct intel_limit *limit,
667 		    struct intel_crtc_state *crtc_state,
668 		    int target, int refclk, struct dpll *match_clock,
669 		    struct dpll *best_clock)
670 {
671 	struct drm_device *dev = crtc_state->base.crtc->dev;
672 	struct dpll clock;
673 	int err = target;
674 
675 	memset(best_clock, 0, sizeof(*best_clock));
676 
677 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
678 
679 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
680 	     clock.m1++) {
681 		for (clock.m2 = limit->m2.min;
682 		     clock.m2 <= limit->m2.max; clock.m2++) {
683 			if (clock.m2 >= clock.m1)
684 				break;
685 			for (clock.n = limit->n.min;
686 			     clock.n <= limit->n.max; clock.n++) {
687 				for (clock.p1 = limit->p1.min;
688 					clock.p1 <= limit->p1.max; clock.p1++) {
689 					int this_err;
690 
691 					i9xx_calc_dpll_params(refclk, &clock);
692 					if (!intel_PLL_is_valid(to_i915(dev),
693 								limit,
694 								&clock))
695 						continue;
696 					if (match_clock &&
697 					    clock.p != match_clock->p)
698 						continue;
699 
700 					this_err = abs(clock.dot - target);
701 					if (this_err < err) {
702 						*best_clock = clock;
703 						err = this_err;
704 					}
705 				}
706 			}
707 		}
708 	}
709 
710 	return (err != target);
711 }
712 
713 /*
714  * Returns a set of divisors for the desired target clock with the given
715  * refclk, or FALSE.  The returned values represent the clock equation:
716  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
717  *
718  * Target and reference clocks are specified in kHz.
719  *
720  * If match_clock is provided, then best_clock P divider must match the P
721  * divider from @match_clock used for LVDS downclocking.
722  */
723 static bool
724 pnv_find_best_dpll(const struct intel_limit *limit,
725 		   struct intel_crtc_state *crtc_state,
726 		   int target, int refclk, struct dpll *match_clock,
727 		   struct dpll *best_clock)
728 {
729 	struct drm_device *dev = crtc_state->base.crtc->dev;
730 	struct dpll clock;
731 	int err = target;
732 
733 	memset(best_clock, 0, sizeof(*best_clock));
734 
735 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
736 
737 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
738 	     clock.m1++) {
739 		for (clock.m2 = limit->m2.min;
740 		     clock.m2 <= limit->m2.max; clock.m2++) {
741 			for (clock.n = limit->n.min;
742 			     clock.n <= limit->n.max; clock.n++) {
743 				for (clock.p1 = limit->p1.min;
744 					clock.p1 <= limit->p1.max; clock.p1++) {
745 					int this_err;
746 
747 					pnv_calc_dpll_params(refclk, &clock);
748 					if (!intel_PLL_is_valid(to_i915(dev),
749 								limit,
750 								&clock))
751 						continue;
752 					if (match_clock &&
753 					    clock.p != match_clock->p)
754 						continue;
755 
756 					this_err = abs(clock.dot - target);
757 					if (this_err < err) {
758 						*best_clock = clock;
759 						err = this_err;
760 					}
761 				}
762 			}
763 		}
764 	}
765 
766 	return (err != target);
767 }
768 
769 /*
770  * Returns a set of divisors for the desired target clock with the given
771  * refclk, or FALSE.  The returned values represent the clock equation:
772  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
773  *
774  * Target and reference clocks are specified in kHz.
775  *
776  * If match_clock is provided, then best_clock P divider must match the P
777  * divider from @match_clock used for LVDS downclocking.
778  */
779 static bool
780 g4x_find_best_dpll(const struct intel_limit *limit,
781 		   struct intel_crtc_state *crtc_state,
782 		   int target, int refclk, struct dpll *match_clock,
783 		   struct dpll *best_clock)
784 {
785 	struct drm_device *dev = crtc_state->base.crtc->dev;
786 	struct dpll clock;
787 	int max_n;
788 	bool found = false;
789 	/* approximately equals target * 0.00585 */
790 	int err_most = (target >> 8) + (target >> 9);
791 
792 	memset(best_clock, 0, sizeof(*best_clock));
793 
794 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
795 
796 	max_n = limit->n.max;
797 	/* based on hardware requirement, prefer smaller n to precision */
798 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
799 		/* based on hardware requirement, prefere larger m1,m2 */
800 		for (clock.m1 = limit->m1.max;
801 		     clock.m1 >= limit->m1.min; clock.m1--) {
802 			for (clock.m2 = limit->m2.max;
803 			     clock.m2 >= limit->m2.min; clock.m2--) {
804 				for (clock.p1 = limit->p1.max;
805 				     clock.p1 >= limit->p1.min; clock.p1--) {
806 					int this_err;
807 
808 					i9xx_calc_dpll_params(refclk, &clock);
809 					if (!intel_PLL_is_valid(to_i915(dev),
810 								limit,
811 								&clock))
812 						continue;
813 
814 					this_err = abs(clock.dot - target);
815 					if (this_err < err_most) {
816 						*best_clock = clock;
817 						err_most = this_err;
818 						max_n = clock.n;
819 						found = true;
820 					}
821 				}
822 			}
823 		}
824 	}
825 	return found;
826 }
827 
828 /*
829  * Check if the calculated PLL configuration is more optimal compared to the
830  * best configuration and error found so far. Return the calculated error.
831  */
832 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
833 			       const struct dpll *calculated_clock,
834 			       const struct dpll *best_clock,
835 			       unsigned int best_error_ppm,
836 			       unsigned int *error_ppm)
837 {
838 	/*
839 	 * For CHV ignore the error and consider only the P value.
840 	 * Prefer a bigger P value based on HW requirements.
841 	 */
842 	if (IS_CHERRYVIEW(to_i915(dev))) {
843 		*error_ppm = 0;
844 
845 		return calculated_clock->p > best_clock->p;
846 	}
847 
848 	if (WARN_ON_ONCE(!target_freq))
849 		return false;
850 
851 	*error_ppm = div_u64(1000000ULL *
852 				abs(target_freq - calculated_clock->dot),
853 			     target_freq);
854 	/*
855 	 * Prefer a better P value over a better (smaller) error if the error
856 	 * is small. Ensure this preference for future configurations too by
857 	 * setting the error to 0.
858 	 */
859 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
860 		*error_ppm = 0;
861 
862 		return true;
863 	}
864 
865 	return *error_ppm + 10 < best_error_ppm;
866 }
867 
868 /*
869  * Returns a set of divisors for the desired target clock with the given
870  * refclk, or FALSE.  The returned values represent the clock equation:
871  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
872  */
873 static bool
874 vlv_find_best_dpll(const struct intel_limit *limit,
875 		   struct intel_crtc_state *crtc_state,
876 		   int target, int refclk, struct dpll *match_clock,
877 		   struct dpll *best_clock)
878 {
879 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
880 	struct drm_device *dev = crtc->base.dev;
881 	struct dpll clock;
882 	unsigned int bestppm = 1000000;
883 	/* min update 19.2 MHz */
884 	int max_n = min(limit->n.max, refclk / 19200);
885 	bool found = false;
886 
887 	target *= 5; /* fast clock */
888 
889 	memset(best_clock, 0, sizeof(*best_clock));
890 
891 	/* based on hardware requirement, prefer smaller n to precision */
892 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
893 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
894 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
895 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
896 				clock.p = clock.p1 * clock.p2;
897 				/* based on hardware requirement, prefer bigger m1,m2 values */
898 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
899 					unsigned int ppm;
900 
901 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
902 								     refclk * clock.m1);
903 
904 					vlv_calc_dpll_params(refclk, &clock);
905 
906 					if (!intel_PLL_is_valid(to_i915(dev),
907 								limit,
908 								&clock))
909 						continue;
910 
911 					if (!vlv_PLL_is_optimal(dev, target,
912 								&clock,
913 								best_clock,
914 								bestppm, &ppm))
915 						continue;
916 
917 					*best_clock = clock;
918 					bestppm = ppm;
919 					found = true;
920 				}
921 			}
922 		}
923 	}
924 
925 	return found;
926 }
927 
928 /*
929  * Returns a set of divisors for the desired target clock with the given
930  * refclk, or FALSE.  The returned values represent the clock equation:
931  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
932  */
933 static bool
934 chv_find_best_dpll(const struct intel_limit *limit,
935 		   struct intel_crtc_state *crtc_state,
936 		   int target, int refclk, struct dpll *match_clock,
937 		   struct dpll *best_clock)
938 {
939 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
940 	struct drm_device *dev = crtc->base.dev;
941 	unsigned int best_error_ppm;
942 	struct dpll clock;
943 	u64 m2;
944 	int found = false;
945 
946 	memset(best_clock, 0, sizeof(*best_clock));
947 	best_error_ppm = 1000000;
948 
949 	/*
950 	 * Based on hardware doc, the n always set to 1, and m1 always
951 	 * set to 2.  If requires to support 200Mhz refclk, we need to
952 	 * revisit this because n may not 1 anymore.
953 	 */
954 	clock.n = 1, clock.m1 = 2;
955 	target *= 5;	/* fast clock */
956 
957 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
958 		for (clock.p2 = limit->p2.p2_fast;
959 				clock.p2 >= limit->p2.p2_slow;
960 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
961 			unsigned int error_ppm;
962 
963 			clock.p = clock.p1 * clock.p2;
964 
965 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
966 						   refclk * clock.m1);
967 
968 			if (m2 > INT_MAX/clock.m1)
969 				continue;
970 
971 			clock.m2 = m2;
972 
973 			chv_calc_dpll_params(refclk, &clock);
974 
975 			if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
976 				continue;
977 
978 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
979 						best_error_ppm, &error_ppm))
980 				continue;
981 
982 			*best_clock = clock;
983 			best_error_ppm = error_ppm;
984 			found = true;
985 		}
986 	}
987 
988 	return found;
989 }
990 
991 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
992 			struct dpll *best_clock)
993 {
994 	int refclk = 100000;
995 	const struct intel_limit *limit = &intel_limits_bxt;
996 
997 	return chv_find_best_dpll(limit, crtc_state,
998 				  crtc_state->port_clock, refclk,
999 				  NULL, best_clock);
1000 }
1001 
1002 bool intel_crtc_active(struct intel_crtc *crtc)
1003 {
1004 	/* Be paranoid as we can arrive here with only partial
1005 	 * state retrieved from the hardware during setup.
1006 	 *
1007 	 * We can ditch the adjusted_mode.crtc_clock check as soon
1008 	 * as Haswell has gained clock readout/fastboot support.
1009 	 *
1010 	 * We can ditch the crtc->primary->state->fb check as soon as we can
1011 	 * properly reconstruct framebuffers.
1012 	 *
1013 	 * FIXME: The intel_crtc->active here should be switched to
1014 	 * crtc->state->active once we have proper CRTC states wired up
1015 	 * for atomic.
1016 	 */
1017 	return crtc->active && crtc->base.primary->state->fb &&
1018 		crtc->config->base.adjusted_mode.crtc_clock;
1019 }
1020 
1021 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1022 					     enum pipe pipe)
1023 {
1024 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
1025 
1026 	return crtc->config->cpu_transcoder;
1027 }
1028 
1029 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1030 				    enum pipe pipe)
1031 {
1032 	i915_reg_t reg = PIPEDSL(pipe);
1033 	u32 line1, line2;
1034 	u32 line_mask;
1035 
1036 	if (IS_GEN(dev_priv, 2))
1037 		line_mask = DSL_LINEMASK_GEN2;
1038 	else
1039 		line_mask = DSL_LINEMASK_GEN3;
1040 
1041 	line1 = I915_READ(reg) & line_mask;
1042 	msleep(5);
1043 	line2 = I915_READ(reg) & line_mask;
1044 
1045 	return line1 != line2;
1046 }
1047 
1048 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1049 {
1050 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1051 	enum pipe pipe = crtc->pipe;
1052 
1053 	/* Wait for the display line to settle/start moving */
1054 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1055 		DRM_ERROR("pipe %c scanline %s wait timed out\n",
1056 			  pipe_name(pipe), onoff(state));
1057 }
1058 
1059 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1060 {
1061 	wait_for_pipe_scanline_moving(crtc, false);
1062 }
1063 
1064 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1065 {
1066 	wait_for_pipe_scanline_moving(crtc, true);
1067 }
1068 
1069 static void
1070 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1071 {
1072 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
1073 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1074 
1075 	if (INTEL_GEN(dev_priv) >= 4) {
1076 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1077 		i915_reg_t reg = PIPECONF(cpu_transcoder);
1078 
1079 		/* Wait for the Pipe State to go off */
1080 		if (intel_de_wait_for_clear(dev_priv, reg,
1081 					    I965_PIPECONF_ACTIVE, 100))
1082 			WARN(1, "pipe_off wait timed out\n");
1083 	} else {
1084 		intel_wait_for_pipe_scanline_stopped(crtc);
1085 	}
1086 }
1087 
1088 /* Only for pre-ILK configs */
1089 void assert_pll(struct drm_i915_private *dev_priv,
1090 		enum pipe pipe, bool state)
1091 {
1092 	u32 val;
1093 	bool cur_state;
1094 
1095 	val = I915_READ(DPLL(pipe));
1096 	cur_state = !!(val & DPLL_VCO_ENABLE);
1097 	I915_STATE_WARN(cur_state != state,
1098 	     "PLL state assertion failure (expected %s, current %s)\n",
1099 			onoff(state), onoff(cur_state));
1100 }
1101 
1102 /* XXX: the dsi pll is shared between MIPI DSI ports */
1103 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1104 {
1105 	u32 val;
1106 	bool cur_state;
1107 
1108 	vlv_cck_get(dev_priv);
1109 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1110 	vlv_cck_put(dev_priv);
1111 
1112 	cur_state = val & DSI_PLL_VCO_EN;
1113 	I915_STATE_WARN(cur_state != state,
1114 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1115 			onoff(state), onoff(cur_state));
1116 }
1117 
1118 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 			  enum pipe pipe, bool state)
1120 {
1121 	bool cur_state;
1122 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1123 								      pipe);
1124 
1125 	if (HAS_DDI(dev_priv)) {
1126 		/* DDI does not have a specific FDI_TX register */
1127 		u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1128 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1129 	} else {
1130 		u32 val = I915_READ(FDI_TX_CTL(pipe));
1131 		cur_state = !!(val & FDI_TX_ENABLE);
1132 	}
1133 	I915_STATE_WARN(cur_state != state,
1134 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1135 			onoff(state), onoff(cur_state));
1136 }
1137 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1138 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1139 
1140 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1141 			  enum pipe pipe, bool state)
1142 {
1143 	u32 val;
1144 	bool cur_state;
1145 
1146 	val = I915_READ(FDI_RX_CTL(pipe));
1147 	cur_state = !!(val & FDI_RX_ENABLE);
1148 	I915_STATE_WARN(cur_state != state,
1149 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1150 			onoff(state), onoff(cur_state));
1151 }
1152 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1153 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1154 
1155 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1156 				      enum pipe pipe)
1157 {
1158 	u32 val;
1159 
1160 	/* ILK FDI PLL is always enabled */
1161 	if (IS_GEN(dev_priv, 5))
1162 		return;
1163 
1164 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1165 	if (HAS_DDI(dev_priv))
1166 		return;
1167 
1168 	val = I915_READ(FDI_TX_CTL(pipe));
1169 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1170 }
1171 
1172 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1173 		       enum pipe pipe, bool state)
1174 {
1175 	u32 val;
1176 	bool cur_state;
1177 
1178 	val = I915_READ(FDI_RX_CTL(pipe));
1179 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1180 	I915_STATE_WARN(cur_state != state,
1181 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1182 			onoff(state), onoff(cur_state));
1183 }
1184 
1185 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1186 {
1187 	i915_reg_t pp_reg;
1188 	u32 val;
1189 	enum pipe panel_pipe = INVALID_PIPE;
1190 	bool locked = true;
1191 
1192 	if (WARN_ON(HAS_DDI(dev_priv)))
1193 		return;
1194 
1195 	if (HAS_PCH_SPLIT(dev_priv)) {
1196 		u32 port_sel;
1197 
1198 		pp_reg = PP_CONTROL(0);
1199 		port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1200 
1201 		switch (port_sel) {
1202 		case PANEL_PORT_SELECT_LVDS:
1203 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1204 			break;
1205 		case PANEL_PORT_SELECT_DPA:
1206 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1207 			break;
1208 		case PANEL_PORT_SELECT_DPC:
1209 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1210 			break;
1211 		case PANEL_PORT_SELECT_DPD:
1212 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1213 			break;
1214 		default:
1215 			MISSING_CASE(port_sel);
1216 			break;
1217 		}
1218 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1219 		/* presumably write lock depends on pipe, not port select */
1220 		pp_reg = PP_CONTROL(pipe);
1221 		panel_pipe = pipe;
1222 	} else {
1223 		u32 port_sel;
1224 
1225 		pp_reg = PP_CONTROL(0);
1226 		port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1227 
1228 		WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
1229 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1230 	}
1231 
1232 	val = I915_READ(pp_reg);
1233 	if (!(val & PANEL_POWER_ON) ||
1234 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1235 		locked = false;
1236 
1237 	I915_STATE_WARN(panel_pipe == pipe && locked,
1238 	     "panel assertion failure, pipe %c regs locked\n",
1239 	     pipe_name(pipe));
1240 }
1241 
1242 void assert_pipe(struct drm_i915_private *dev_priv,
1243 		 enum pipe pipe, bool state)
1244 {
1245 	bool cur_state;
1246 	enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1247 								      pipe);
1248 	enum intel_display_power_domain power_domain;
1249 	intel_wakeref_t wakeref;
1250 
1251 	/* we keep both pipes enabled on 830 */
1252 	if (IS_I830(dev_priv))
1253 		state = true;
1254 
1255 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1256 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1257 	if (wakeref) {
1258 		u32 val = I915_READ(PIPECONF(cpu_transcoder));
1259 		cur_state = !!(val & PIPECONF_ENABLE);
1260 
1261 		intel_display_power_put(dev_priv, power_domain, wakeref);
1262 	} else {
1263 		cur_state = false;
1264 	}
1265 
1266 	I915_STATE_WARN(cur_state != state,
1267 	     "pipe %c assertion failure (expected %s, current %s)\n",
1268 			pipe_name(pipe), onoff(state), onoff(cur_state));
1269 }
1270 
1271 static void assert_plane(struct intel_plane *plane, bool state)
1272 {
1273 	enum pipe pipe;
1274 	bool cur_state;
1275 
1276 	cur_state = plane->get_hw_state(plane, &pipe);
1277 
1278 	I915_STATE_WARN(cur_state != state,
1279 			"%s assertion failure (expected %s, current %s)\n",
1280 			plane->base.name, onoff(state), onoff(cur_state));
1281 }
1282 
1283 #define assert_plane_enabled(p) assert_plane(p, true)
1284 #define assert_plane_disabled(p) assert_plane(p, false)
1285 
1286 static void assert_planes_disabled(struct intel_crtc *crtc)
1287 {
1288 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1289 	struct intel_plane *plane;
1290 
1291 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1292 		assert_plane_disabled(plane);
1293 }
1294 
1295 static void assert_vblank_disabled(struct drm_crtc *crtc)
1296 {
1297 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1298 		drm_crtc_vblank_put(crtc);
1299 }
1300 
1301 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1302 				    enum pipe pipe)
1303 {
1304 	u32 val;
1305 	bool enabled;
1306 
1307 	val = I915_READ(PCH_TRANSCONF(pipe));
1308 	enabled = !!(val & TRANS_ENABLE);
1309 	I915_STATE_WARN(enabled,
1310 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1311 	     pipe_name(pipe));
1312 }
1313 
1314 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1315 				   enum pipe pipe, enum port port,
1316 				   i915_reg_t dp_reg)
1317 {
1318 	enum pipe port_pipe;
1319 	bool state;
1320 
1321 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1322 
1323 	I915_STATE_WARN(state && port_pipe == pipe,
1324 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
1325 			port_name(port), pipe_name(pipe));
1326 
1327 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1328 			"IBX PCH DP %c still using transcoder B\n",
1329 			port_name(port));
1330 }
1331 
1332 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1333 				     enum pipe pipe, enum port port,
1334 				     i915_reg_t hdmi_reg)
1335 {
1336 	enum pipe port_pipe;
1337 	bool state;
1338 
1339 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1340 
1341 	I915_STATE_WARN(state && port_pipe == pipe,
1342 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1343 			port_name(port), pipe_name(pipe));
1344 
1345 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1346 			"IBX PCH HDMI %c still using transcoder B\n",
1347 			port_name(port));
1348 }
1349 
1350 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1351 				      enum pipe pipe)
1352 {
1353 	enum pipe port_pipe;
1354 
1355 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1356 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1357 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1358 
1359 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1360 			port_pipe == pipe,
1361 			"PCH VGA enabled on transcoder %c, should be disabled\n",
1362 			pipe_name(pipe));
1363 
1364 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1365 			port_pipe == pipe,
1366 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
1367 			pipe_name(pipe));
1368 
1369 	/* PCH SDVOB multiplex with HDMIB */
1370 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1371 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1372 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1373 }
1374 
1375 static void _vlv_enable_pll(struct intel_crtc *crtc,
1376 			    const struct intel_crtc_state *pipe_config)
1377 {
1378 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1379 	enum pipe pipe = crtc->pipe;
1380 
1381 	I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1382 	POSTING_READ(DPLL(pipe));
1383 	udelay(150);
1384 
1385 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1386 		DRM_ERROR("DPLL %d failed to lock\n", pipe);
1387 }
1388 
1389 static void vlv_enable_pll(struct intel_crtc *crtc,
1390 			   const struct intel_crtc_state *pipe_config)
1391 {
1392 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1393 	enum pipe pipe = crtc->pipe;
1394 
1395 	assert_pipe_disabled(dev_priv, pipe);
1396 
1397 	/* PLL is protected by panel, make sure we can write it */
1398 	assert_panel_unlocked(dev_priv, pipe);
1399 
1400 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1401 		_vlv_enable_pll(crtc, pipe_config);
1402 
1403 	I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1404 	POSTING_READ(DPLL_MD(pipe));
1405 }
1406 
1407 
1408 static void _chv_enable_pll(struct intel_crtc *crtc,
1409 			    const struct intel_crtc_state *pipe_config)
1410 {
1411 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1412 	enum pipe pipe = crtc->pipe;
1413 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1414 	u32 tmp;
1415 
1416 	vlv_dpio_get(dev_priv);
1417 
1418 	/* Enable back the 10bit clock to display controller */
1419 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1420 	tmp |= DPIO_DCLKP_EN;
1421 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1422 
1423 	vlv_dpio_put(dev_priv);
1424 
1425 	/*
1426 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1427 	 */
1428 	udelay(1);
1429 
1430 	/* Enable PLL */
1431 	I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1432 
1433 	/* Check PLL is locked */
1434 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1435 		DRM_ERROR("PLL %d failed to lock\n", pipe);
1436 }
1437 
1438 static void chv_enable_pll(struct intel_crtc *crtc,
1439 			   const struct intel_crtc_state *pipe_config)
1440 {
1441 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1442 	enum pipe pipe = crtc->pipe;
1443 
1444 	assert_pipe_disabled(dev_priv, pipe);
1445 
1446 	/* PLL is protected by panel, make sure we can write it */
1447 	assert_panel_unlocked(dev_priv, pipe);
1448 
1449 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1450 		_chv_enable_pll(crtc, pipe_config);
1451 
1452 	if (pipe != PIPE_A) {
1453 		/*
1454 		 * WaPixelRepeatModeFixForC0:chv
1455 		 *
1456 		 * DPLLCMD is AWOL. Use chicken bits to propagate
1457 		 * the value from DPLLBMD to either pipe B or C.
1458 		 */
1459 		I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1460 		I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1461 		I915_WRITE(CBR4_VLV, 0);
1462 		dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1463 
1464 		/*
1465 		 * DPLLB VGA mode also seems to cause problems.
1466 		 * We should always have it disabled.
1467 		 */
1468 		WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1469 	} else {
1470 		I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1471 		POSTING_READ(DPLL_MD(pipe));
1472 	}
1473 }
1474 
1475 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1476 {
1477 	if (IS_I830(dev_priv))
1478 		return false;
1479 
1480 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1481 }
1482 
1483 static void i9xx_enable_pll(struct intel_crtc *crtc,
1484 			    const struct intel_crtc_state *crtc_state)
1485 {
1486 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1487 	i915_reg_t reg = DPLL(crtc->pipe);
1488 	u32 dpll = crtc_state->dpll_hw_state.dpll;
1489 	int i;
1490 
1491 	assert_pipe_disabled(dev_priv, crtc->pipe);
1492 
1493 	/* PLL is protected by panel, make sure we can write it */
1494 	if (i9xx_has_pps(dev_priv))
1495 		assert_panel_unlocked(dev_priv, crtc->pipe);
1496 
1497 	/*
1498 	 * Apparently we need to have VGA mode enabled prior to changing
1499 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1500 	 * dividers, even though the register value does change.
1501 	 */
1502 	I915_WRITE(reg, dpll & ~DPLL_VGA_MODE_DIS);
1503 	I915_WRITE(reg, dpll);
1504 
1505 	/* Wait for the clocks to stabilize. */
1506 	POSTING_READ(reg);
1507 	udelay(150);
1508 
1509 	if (INTEL_GEN(dev_priv) >= 4) {
1510 		I915_WRITE(DPLL_MD(crtc->pipe),
1511 			   crtc_state->dpll_hw_state.dpll_md);
1512 	} else {
1513 		/* The pixel multiplier can only be updated once the
1514 		 * DPLL is enabled and the clocks are stable.
1515 		 *
1516 		 * So write it again.
1517 		 */
1518 		I915_WRITE(reg, dpll);
1519 	}
1520 
1521 	/* We do this three times for luck */
1522 	for (i = 0; i < 3; i++) {
1523 		I915_WRITE(reg, dpll);
1524 		POSTING_READ(reg);
1525 		udelay(150); /* wait for warmup */
1526 	}
1527 }
1528 
1529 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1530 {
1531 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1532 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1533 	enum pipe pipe = crtc->pipe;
1534 
1535 	/* Don't disable pipe or pipe PLLs if needed */
1536 	if (IS_I830(dev_priv))
1537 		return;
1538 
1539 	/* Make sure the pipe isn't still relying on us */
1540 	assert_pipe_disabled(dev_priv, pipe);
1541 
1542 	I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
1543 	POSTING_READ(DPLL(pipe));
1544 }
1545 
1546 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1547 {
1548 	u32 val;
1549 
1550 	/* Make sure the pipe isn't still relying on us */
1551 	assert_pipe_disabled(dev_priv, pipe);
1552 
1553 	val = DPLL_INTEGRATED_REF_CLK_VLV |
1554 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1555 	if (pipe != PIPE_A)
1556 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1557 
1558 	I915_WRITE(DPLL(pipe), val);
1559 	POSTING_READ(DPLL(pipe));
1560 }
1561 
1562 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1563 {
1564 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1565 	u32 val;
1566 
1567 	/* Make sure the pipe isn't still relying on us */
1568 	assert_pipe_disabled(dev_priv, pipe);
1569 
1570 	val = DPLL_SSC_REF_CLK_CHV |
1571 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1572 	if (pipe != PIPE_A)
1573 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1574 
1575 	I915_WRITE(DPLL(pipe), val);
1576 	POSTING_READ(DPLL(pipe));
1577 
1578 	vlv_dpio_get(dev_priv);
1579 
1580 	/* Disable 10bit clock to display controller */
1581 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1582 	val &= ~DPIO_DCLKP_EN;
1583 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1584 
1585 	vlv_dpio_put(dev_priv);
1586 }
1587 
1588 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1589 			 struct intel_digital_port *dport,
1590 			 unsigned int expected_mask)
1591 {
1592 	u32 port_mask;
1593 	i915_reg_t dpll_reg;
1594 
1595 	switch (dport->base.port) {
1596 	case PORT_B:
1597 		port_mask = DPLL_PORTB_READY_MASK;
1598 		dpll_reg = DPLL(0);
1599 		break;
1600 	case PORT_C:
1601 		port_mask = DPLL_PORTC_READY_MASK;
1602 		dpll_reg = DPLL(0);
1603 		expected_mask <<= 4;
1604 		break;
1605 	case PORT_D:
1606 		port_mask = DPLL_PORTD_READY_MASK;
1607 		dpll_reg = DPIO_PHY_STATUS;
1608 		break;
1609 	default:
1610 		BUG();
1611 	}
1612 
1613 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
1614 				       port_mask, expected_mask, 1000))
1615 		WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1616 		     port_name(dport->base.port),
1617 		     I915_READ(dpll_reg) & port_mask, expected_mask);
1618 }
1619 
1620 static void ironlake_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1621 {
1622 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1623 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1624 	enum pipe pipe = crtc->pipe;
1625 	i915_reg_t reg;
1626 	u32 val, pipeconf_val;
1627 
1628 	/* Make sure PCH DPLL is enabled */
1629 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1630 
1631 	/* FDI must be feeding us bits for PCH ports */
1632 	assert_fdi_tx_enabled(dev_priv, pipe);
1633 	assert_fdi_rx_enabled(dev_priv, pipe);
1634 
1635 	if (HAS_PCH_CPT(dev_priv)) {
1636 		/* Workaround: Set the timing override bit before enabling the
1637 		 * pch transcoder. */
1638 		reg = TRANS_CHICKEN2(pipe);
1639 		val = I915_READ(reg);
1640 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1641 		I915_WRITE(reg, val);
1642 	}
1643 
1644 	reg = PCH_TRANSCONF(pipe);
1645 	val = I915_READ(reg);
1646 	pipeconf_val = I915_READ(PIPECONF(pipe));
1647 
1648 	if (HAS_PCH_IBX(dev_priv)) {
1649 		/*
1650 		 * Make the BPC in transcoder be consistent with
1651 		 * that in pipeconf reg. For HDMI we must use 8bpc
1652 		 * here for both 8bpc and 12bpc.
1653 		 */
1654 		val &= ~PIPECONF_BPC_MASK;
1655 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1656 			val |= PIPECONF_8BPC;
1657 		else
1658 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1659 	}
1660 
1661 	val &= ~TRANS_INTERLACE_MASK;
1662 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1663 		if (HAS_PCH_IBX(dev_priv) &&
1664 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1665 			val |= TRANS_LEGACY_INTERLACED_ILK;
1666 		else
1667 			val |= TRANS_INTERLACED;
1668 	} else {
1669 		val |= TRANS_PROGRESSIVE;
1670 	}
1671 
1672 	I915_WRITE(reg, val | TRANS_ENABLE);
1673 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1674 		DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1675 }
1676 
1677 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1678 				      enum transcoder cpu_transcoder)
1679 {
1680 	u32 val, pipeconf_val;
1681 
1682 	/* FDI must be feeding us bits for PCH ports */
1683 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1684 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
1685 
1686 	/* Workaround: set timing override bit. */
1687 	val = I915_READ(TRANS_CHICKEN2(PIPE_A));
1688 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1689 	I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
1690 
1691 	val = TRANS_ENABLE;
1692 	pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
1693 
1694 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1695 	    PIPECONF_INTERLACED_ILK)
1696 		val |= TRANS_INTERLACED;
1697 	else
1698 		val |= TRANS_PROGRESSIVE;
1699 
1700 	I915_WRITE(LPT_TRANSCONF, val);
1701 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1702 				  TRANS_STATE_ENABLE, 100))
1703 		DRM_ERROR("Failed to enable PCH transcoder\n");
1704 }
1705 
1706 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1707 					    enum pipe pipe)
1708 {
1709 	i915_reg_t reg;
1710 	u32 val;
1711 
1712 	/* FDI relies on the transcoder */
1713 	assert_fdi_tx_disabled(dev_priv, pipe);
1714 	assert_fdi_rx_disabled(dev_priv, pipe);
1715 
1716 	/* Ports must be off as well */
1717 	assert_pch_ports_disabled(dev_priv, pipe);
1718 
1719 	reg = PCH_TRANSCONF(pipe);
1720 	val = I915_READ(reg);
1721 	val &= ~TRANS_ENABLE;
1722 	I915_WRITE(reg, val);
1723 	/* wait for PCH transcoder off, transcoder state */
1724 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1725 		DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
1726 
1727 	if (HAS_PCH_CPT(dev_priv)) {
1728 		/* Workaround: Clear the timing override chicken bit again. */
1729 		reg = TRANS_CHICKEN2(pipe);
1730 		val = I915_READ(reg);
1731 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1732 		I915_WRITE(reg, val);
1733 	}
1734 }
1735 
1736 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1737 {
1738 	u32 val;
1739 
1740 	val = I915_READ(LPT_TRANSCONF);
1741 	val &= ~TRANS_ENABLE;
1742 	I915_WRITE(LPT_TRANSCONF, val);
1743 	/* wait for PCH transcoder off, transcoder state */
1744 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1745 				    TRANS_STATE_ENABLE, 50))
1746 		DRM_ERROR("Failed to disable PCH transcoder\n");
1747 
1748 	/* Workaround: clear timing override bit. */
1749 	val = I915_READ(TRANS_CHICKEN2(PIPE_A));
1750 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1751 	I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
1752 }
1753 
1754 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1755 {
1756 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1757 
1758 	if (HAS_PCH_LPT(dev_priv))
1759 		return PIPE_A;
1760 	else
1761 		return crtc->pipe;
1762 }
1763 
1764 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1765 {
1766 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1767 
1768 	/*
1769 	 * On i965gm the hardware frame counter reads
1770 	 * zero when the TV encoder is enabled :(
1771 	 */
1772 	if (IS_I965GM(dev_priv) &&
1773 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1774 		return 0;
1775 
1776 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1777 		return 0xffffffff; /* full 32 bit counter */
1778 	else if (INTEL_GEN(dev_priv) >= 3)
1779 		return 0xffffff; /* only 24 bits of frame count */
1780 	else
1781 		return 0; /* Gen2 doesn't have a hardware frame counter */
1782 }
1783 
1784 static void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1785 {
1786 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1787 
1788 	drm_crtc_set_max_vblank_count(&crtc->base,
1789 				      intel_crtc_max_vblank_count(crtc_state));
1790 	drm_crtc_vblank_on(&crtc->base);
1791 }
1792 
1793 static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1794 {
1795 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1796 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1797 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1798 	enum pipe pipe = crtc->pipe;
1799 	i915_reg_t reg;
1800 	u32 val;
1801 
1802 	DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1803 
1804 	assert_planes_disabled(crtc);
1805 
1806 	/*
1807 	 * A pipe without a PLL won't actually be able to drive bits from
1808 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1809 	 * need the check.
1810 	 */
1811 	if (HAS_GMCH(dev_priv)) {
1812 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1813 			assert_dsi_pll_enabled(dev_priv);
1814 		else
1815 			assert_pll_enabled(dev_priv, pipe);
1816 	} else {
1817 		if (new_crtc_state->has_pch_encoder) {
1818 			/* if driving the PCH, we need FDI enabled */
1819 			assert_fdi_rx_pll_enabled(dev_priv,
1820 						  intel_crtc_pch_transcoder(crtc));
1821 			assert_fdi_tx_pll_enabled(dev_priv,
1822 						  (enum pipe) cpu_transcoder);
1823 		}
1824 		/* FIXME: assert CPU port conditions for SNB+ */
1825 	}
1826 
1827 	trace_intel_pipe_enable(crtc);
1828 
1829 	reg = PIPECONF(cpu_transcoder);
1830 	val = I915_READ(reg);
1831 	if (val & PIPECONF_ENABLE) {
1832 		/* we keep both pipes enabled on 830 */
1833 		WARN_ON(!IS_I830(dev_priv));
1834 		return;
1835 	}
1836 
1837 	I915_WRITE(reg, val | PIPECONF_ENABLE);
1838 	POSTING_READ(reg);
1839 
1840 	/*
1841 	 * Until the pipe starts PIPEDSL reads will return a stale value,
1842 	 * which causes an apparent vblank timestamp jump when PIPEDSL
1843 	 * resets to its proper value. That also messes up the frame count
1844 	 * when it's derived from the timestamps. So let's wait for the
1845 	 * pipe to start properly before we call drm_crtc_vblank_on()
1846 	 */
1847 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1848 		intel_wait_for_pipe_scanline_moving(crtc);
1849 }
1850 
1851 static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1852 {
1853 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
1854 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1855 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1856 	enum pipe pipe = crtc->pipe;
1857 	i915_reg_t reg;
1858 	u32 val;
1859 
1860 	DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
1861 
1862 	/*
1863 	 * Make sure planes won't keep trying to pump pixels to us,
1864 	 * or we might hang the display.
1865 	 */
1866 	assert_planes_disabled(crtc);
1867 
1868 	trace_intel_pipe_disable(crtc);
1869 
1870 	reg = PIPECONF(cpu_transcoder);
1871 	val = I915_READ(reg);
1872 	if ((val & PIPECONF_ENABLE) == 0)
1873 		return;
1874 
1875 	/*
1876 	 * Double wide has implications for planes
1877 	 * so best keep it disabled when not needed.
1878 	 */
1879 	if (old_crtc_state->double_wide)
1880 		val &= ~PIPECONF_DOUBLE_WIDE;
1881 
1882 	/* Don't disable pipe or pipe PLLs if needed */
1883 	if (!IS_I830(dev_priv))
1884 		val &= ~PIPECONF_ENABLE;
1885 
1886 	I915_WRITE(reg, val);
1887 	if ((val & PIPECONF_ENABLE) == 0)
1888 		intel_wait_for_pipe_off(old_crtc_state);
1889 }
1890 
1891 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1892 {
1893 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1894 }
1895 
1896 static unsigned int
1897 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
1898 {
1899 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1900 	unsigned int cpp = fb->format->cpp[color_plane];
1901 
1902 	switch (fb->modifier) {
1903 	case DRM_FORMAT_MOD_LINEAR:
1904 		return intel_tile_size(dev_priv);
1905 	case I915_FORMAT_MOD_X_TILED:
1906 		if (IS_GEN(dev_priv, 2))
1907 			return 128;
1908 		else
1909 			return 512;
1910 	case I915_FORMAT_MOD_Y_TILED_CCS:
1911 		if (color_plane == 1)
1912 			return 128;
1913 		/* fall through */
1914 	case I915_FORMAT_MOD_Y_TILED:
1915 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
1916 			return 128;
1917 		else
1918 			return 512;
1919 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1920 		if (color_plane == 1)
1921 			return 128;
1922 		/* fall through */
1923 	case I915_FORMAT_MOD_Yf_TILED:
1924 		switch (cpp) {
1925 		case 1:
1926 			return 64;
1927 		case 2:
1928 		case 4:
1929 			return 128;
1930 		case 8:
1931 		case 16:
1932 			return 256;
1933 		default:
1934 			MISSING_CASE(cpp);
1935 			return cpp;
1936 		}
1937 		break;
1938 	default:
1939 		MISSING_CASE(fb->modifier);
1940 		return cpp;
1941 	}
1942 }
1943 
1944 static unsigned int
1945 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
1946 {
1947 	return intel_tile_size(to_i915(fb->dev)) /
1948 		intel_tile_width_bytes(fb, color_plane);
1949 }
1950 
1951 /* Return the tile dimensions in pixel units */
1952 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
1953 			    unsigned int *tile_width,
1954 			    unsigned int *tile_height)
1955 {
1956 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
1957 	unsigned int cpp = fb->format->cpp[color_plane];
1958 
1959 	*tile_width = tile_width_bytes / cpp;
1960 	*tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
1961 }
1962 
1963 unsigned int
1964 intel_fb_align_height(const struct drm_framebuffer *fb,
1965 		      int color_plane, unsigned int height)
1966 {
1967 	unsigned int tile_height = intel_tile_height(fb, color_plane);
1968 
1969 	return ALIGN(height, tile_height);
1970 }
1971 
1972 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1973 {
1974 	unsigned int size = 0;
1975 	int i;
1976 
1977 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1978 		size += rot_info->plane[i].width * rot_info->plane[i].height;
1979 
1980 	return size;
1981 }
1982 
1983 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
1984 {
1985 	unsigned int size = 0;
1986 	int i;
1987 
1988 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
1989 		size += rem_info->plane[i].width * rem_info->plane[i].height;
1990 
1991 	return size;
1992 }
1993 
1994 static void
1995 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
1996 			const struct drm_framebuffer *fb,
1997 			unsigned int rotation)
1998 {
1999 	view->type = I915_GGTT_VIEW_NORMAL;
2000 	if (drm_rotation_90_or_270(rotation)) {
2001 		view->type = I915_GGTT_VIEW_ROTATED;
2002 		view->rotated = to_intel_framebuffer(fb)->rot_info;
2003 	}
2004 }
2005 
2006 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2007 {
2008 	if (IS_I830(dev_priv))
2009 		return 16 * 1024;
2010 	else if (IS_I85X(dev_priv))
2011 		return 256;
2012 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2013 		return 32;
2014 	else
2015 		return 4 * 1024;
2016 }
2017 
2018 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2019 {
2020 	if (INTEL_GEN(dev_priv) >= 9)
2021 		return 256 * 1024;
2022 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2023 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2024 		return 128 * 1024;
2025 	else if (INTEL_GEN(dev_priv) >= 4)
2026 		return 4 * 1024;
2027 	else
2028 		return 0;
2029 }
2030 
2031 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2032 					 int color_plane)
2033 {
2034 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2035 
2036 	/* AUX_DIST needs only 4K alignment */
2037 	if (color_plane == 1)
2038 		return 4096;
2039 
2040 	switch (fb->modifier) {
2041 	case DRM_FORMAT_MOD_LINEAR:
2042 		return intel_linear_alignment(dev_priv);
2043 	case I915_FORMAT_MOD_X_TILED:
2044 		if (INTEL_GEN(dev_priv) >= 9)
2045 			return 256 * 1024;
2046 		return 0;
2047 	case I915_FORMAT_MOD_Y_TILED_CCS:
2048 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2049 	case I915_FORMAT_MOD_Y_TILED:
2050 	case I915_FORMAT_MOD_Yf_TILED:
2051 		return 1 * 1024 * 1024;
2052 	default:
2053 		MISSING_CASE(fb->modifier);
2054 		return 0;
2055 	}
2056 }
2057 
2058 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2059 {
2060 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2061 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2062 
2063 	return INTEL_GEN(dev_priv) < 4 ||
2064 		(plane->has_fbc &&
2065 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2066 }
2067 
2068 struct i915_vma *
2069 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2070 			   const struct i915_ggtt_view *view,
2071 			   bool uses_fence,
2072 			   unsigned long *out_flags)
2073 {
2074 	struct drm_device *dev = fb->dev;
2075 	struct drm_i915_private *dev_priv = to_i915(dev);
2076 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2077 	intel_wakeref_t wakeref;
2078 	struct i915_vma *vma;
2079 	unsigned int pinctl;
2080 	u32 alignment;
2081 
2082 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2083 
2084 	alignment = intel_surf_alignment(fb, 0);
2085 
2086 	/* Note that the w/a also requires 64 PTE of padding following the
2087 	 * bo. We currently fill all unused PTE with the shadow page and so
2088 	 * we should always have valid PTE following the scanout preventing
2089 	 * the VT-d warning.
2090 	 */
2091 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2092 		alignment = 256 * 1024;
2093 
2094 	/*
2095 	 * Global gtt pte registers are special registers which actually forward
2096 	 * writes to a chunk of system memory. Which means that there is no risk
2097 	 * that the register values disappear as soon as we call
2098 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2099 	 * pin/unpin/fence and not more.
2100 	 */
2101 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2102 	i915_gem_object_lock(obj);
2103 
2104 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2105 
2106 	pinctl = 0;
2107 
2108 	/* Valleyview is definitely limited to scanning out the first
2109 	 * 512MiB. Lets presume this behaviour was inherited from the
2110 	 * g4x display engine and that all earlier gen are similarly
2111 	 * limited. Testing suggests that it is a little more
2112 	 * complicated than this. For example, Cherryview appears quite
2113 	 * happy to scanout from anywhere within its global aperture.
2114 	 */
2115 	if (HAS_GMCH(dev_priv))
2116 		pinctl |= PIN_MAPPABLE;
2117 
2118 	vma = i915_gem_object_pin_to_display_plane(obj,
2119 						   alignment, view, pinctl);
2120 	if (IS_ERR(vma))
2121 		goto err;
2122 
2123 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2124 		int ret;
2125 
2126 		/* Install a fence for tiled scan-out. Pre-i965 always needs a
2127 		 * fence, whereas 965+ only requires a fence if using
2128 		 * framebuffer compression.  For simplicity, we always, when
2129 		 * possible, install a fence as the cost is not that onerous.
2130 		 *
2131 		 * If we fail to fence the tiled scanout, then either the
2132 		 * modeset will reject the change (which is highly unlikely as
2133 		 * the affected systems, all but one, do not have unmappable
2134 		 * space) or we will not be able to enable full powersaving
2135 		 * techniques (also likely not to apply due to various limits
2136 		 * FBC and the like impose on the size of the buffer, which
2137 		 * presumably we violated anyway with this unmappable buffer).
2138 		 * Anyway, it is presumably better to stumble onwards with
2139 		 * something and try to run the system in a "less than optimal"
2140 		 * mode that matches the user configuration.
2141 		 */
2142 		ret = i915_vma_pin_fence(vma);
2143 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2144 			i915_gem_object_unpin_from_display_plane(vma);
2145 			vma = ERR_PTR(ret);
2146 			goto err;
2147 		}
2148 
2149 		if (ret == 0 && vma->fence)
2150 			*out_flags |= PLANE_HAS_FENCE;
2151 	}
2152 
2153 	i915_vma_get(vma);
2154 err:
2155 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2156 
2157 	i915_gem_object_unlock(obj);
2158 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2159 	return vma;
2160 }
2161 
2162 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2163 {
2164 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
2165 
2166 	i915_gem_object_lock(vma->obj);
2167 	if (flags & PLANE_HAS_FENCE)
2168 		i915_vma_unpin_fence(vma);
2169 	i915_gem_object_unpin_from_display_plane(vma);
2170 	i915_gem_object_unlock(vma->obj);
2171 
2172 	i915_vma_put(vma);
2173 }
2174 
2175 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2176 			  unsigned int rotation)
2177 {
2178 	if (drm_rotation_90_or_270(rotation))
2179 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2180 	else
2181 		return fb->pitches[color_plane];
2182 }
2183 
2184 /*
2185  * Convert the x/y offsets into a linear offset.
2186  * Only valid with 0/180 degree rotation, which is fine since linear
2187  * offset is only used with linear buffers on pre-hsw and tiled buffers
2188  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2189  */
2190 u32 intel_fb_xy_to_linear(int x, int y,
2191 			  const struct intel_plane_state *state,
2192 			  int color_plane)
2193 {
2194 	const struct drm_framebuffer *fb = state->base.fb;
2195 	unsigned int cpp = fb->format->cpp[color_plane];
2196 	unsigned int pitch = state->color_plane[color_plane].stride;
2197 
2198 	return y * pitch + x * cpp;
2199 }
2200 
2201 /*
2202  * Add the x/y offsets derived from fb->offsets[] to the user
2203  * specified plane src x/y offsets. The resulting x/y offsets
2204  * specify the start of scanout from the beginning of the gtt mapping.
2205  */
2206 void intel_add_fb_offsets(int *x, int *y,
2207 			  const struct intel_plane_state *state,
2208 			  int color_plane)
2209 
2210 {
2211 	*x += state->color_plane[color_plane].x;
2212 	*y += state->color_plane[color_plane].y;
2213 }
2214 
2215 static u32 intel_adjust_tile_offset(int *x, int *y,
2216 				    unsigned int tile_width,
2217 				    unsigned int tile_height,
2218 				    unsigned int tile_size,
2219 				    unsigned int pitch_tiles,
2220 				    u32 old_offset,
2221 				    u32 new_offset)
2222 {
2223 	unsigned int pitch_pixels = pitch_tiles * tile_width;
2224 	unsigned int tiles;
2225 
2226 	WARN_ON(old_offset & (tile_size - 1));
2227 	WARN_ON(new_offset & (tile_size - 1));
2228 	WARN_ON(new_offset > old_offset);
2229 
2230 	tiles = (old_offset - new_offset) / tile_size;
2231 
2232 	*y += tiles / pitch_tiles * tile_height;
2233 	*x += tiles % pitch_tiles * tile_width;
2234 
2235 	/* minimize x in case it got needlessly big */
2236 	*y += *x / pitch_pixels * tile_height;
2237 	*x %= pitch_pixels;
2238 
2239 	return new_offset;
2240 }
2241 
2242 static bool is_surface_linear(u64 modifier, int color_plane)
2243 {
2244 	return modifier == DRM_FORMAT_MOD_LINEAR;
2245 }
2246 
2247 static u32 intel_adjust_aligned_offset(int *x, int *y,
2248 				       const struct drm_framebuffer *fb,
2249 				       int color_plane,
2250 				       unsigned int rotation,
2251 				       unsigned int pitch,
2252 				       u32 old_offset, u32 new_offset)
2253 {
2254 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2255 	unsigned int cpp = fb->format->cpp[color_plane];
2256 
2257 	WARN_ON(new_offset > old_offset);
2258 
2259 	if (!is_surface_linear(fb->modifier, color_plane)) {
2260 		unsigned int tile_size, tile_width, tile_height;
2261 		unsigned int pitch_tiles;
2262 
2263 		tile_size = intel_tile_size(dev_priv);
2264 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2265 
2266 		if (drm_rotation_90_or_270(rotation)) {
2267 			pitch_tiles = pitch / tile_height;
2268 			swap(tile_width, tile_height);
2269 		} else {
2270 			pitch_tiles = pitch / (tile_width * cpp);
2271 		}
2272 
2273 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2274 					 tile_size, pitch_tiles,
2275 					 old_offset, new_offset);
2276 	} else {
2277 		old_offset += *y * pitch + *x * cpp;
2278 
2279 		*y = (old_offset - new_offset) / pitch;
2280 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
2281 	}
2282 
2283 	return new_offset;
2284 }
2285 
2286 /*
2287  * Adjust the tile offset by moving the difference into
2288  * the x/y offsets.
2289  */
2290 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2291 					     const struct intel_plane_state *state,
2292 					     int color_plane,
2293 					     u32 old_offset, u32 new_offset)
2294 {
2295 	return intel_adjust_aligned_offset(x, y, state->base.fb, color_plane,
2296 					   state->base.rotation,
2297 					   state->color_plane[color_plane].stride,
2298 					   old_offset, new_offset);
2299 }
2300 
2301 /*
2302  * Computes the aligned offset to the base tile and adjusts
2303  * x, y. bytes per pixel is assumed to be a power-of-two.
2304  *
2305  * In the 90/270 rotated case, x and y are assumed
2306  * to be already rotated to match the rotated GTT view, and
2307  * pitch is the tile_height aligned framebuffer height.
2308  *
2309  * This function is used when computing the derived information
2310  * under intel_framebuffer, so using any of that information
2311  * here is not allowed. Anything under drm_framebuffer can be
2312  * used. This is why the user has to pass in the pitch since it
2313  * is specified in the rotated orientation.
2314  */
2315 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2316 					int *x, int *y,
2317 					const struct drm_framebuffer *fb,
2318 					int color_plane,
2319 					unsigned int pitch,
2320 					unsigned int rotation,
2321 					u32 alignment)
2322 {
2323 	unsigned int cpp = fb->format->cpp[color_plane];
2324 	u32 offset, offset_aligned;
2325 
2326 	if (alignment)
2327 		alignment--;
2328 
2329 	if (!is_surface_linear(fb->modifier, color_plane)) {
2330 		unsigned int tile_size, tile_width, tile_height;
2331 		unsigned int tile_rows, tiles, pitch_tiles;
2332 
2333 		tile_size = intel_tile_size(dev_priv);
2334 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2335 
2336 		if (drm_rotation_90_or_270(rotation)) {
2337 			pitch_tiles = pitch / tile_height;
2338 			swap(tile_width, tile_height);
2339 		} else {
2340 			pitch_tiles = pitch / (tile_width * cpp);
2341 		}
2342 
2343 		tile_rows = *y / tile_height;
2344 		*y %= tile_height;
2345 
2346 		tiles = *x / tile_width;
2347 		*x %= tile_width;
2348 
2349 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2350 		offset_aligned = offset & ~alignment;
2351 
2352 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2353 					 tile_size, pitch_tiles,
2354 					 offset, offset_aligned);
2355 	} else {
2356 		offset = *y * pitch + *x * cpp;
2357 		offset_aligned = offset & ~alignment;
2358 
2359 		*y = (offset & alignment) / pitch;
2360 		*x = ((offset & alignment) - *y * pitch) / cpp;
2361 	}
2362 
2363 	return offset_aligned;
2364 }
2365 
2366 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2367 					      const struct intel_plane_state *state,
2368 					      int color_plane)
2369 {
2370 	struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
2371 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2372 	const struct drm_framebuffer *fb = state->base.fb;
2373 	unsigned int rotation = state->base.rotation;
2374 	int pitch = state->color_plane[color_plane].stride;
2375 	u32 alignment;
2376 
2377 	if (intel_plane->id == PLANE_CURSOR)
2378 		alignment = intel_cursor_alignment(dev_priv);
2379 	else
2380 		alignment = intel_surf_alignment(fb, color_plane);
2381 
2382 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2383 					    pitch, rotation, alignment);
2384 }
2385 
2386 /* Convert the fb->offset[] into x/y offsets */
2387 static int intel_fb_offset_to_xy(int *x, int *y,
2388 				 const struct drm_framebuffer *fb,
2389 				 int color_plane)
2390 {
2391 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2392 	unsigned int height;
2393 
2394 	if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
2395 	    fb->offsets[color_plane] % intel_tile_size(dev_priv)) {
2396 		DRM_DEBUG_KMS("Misaligned offset 0x%08x for color plane %d\n",
2397 			      fb->offsets[color_plane], color_plane);
2398 		return -EINVAL;
2399 	}
2400 
2401 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2402 	height = ALIGN(height, intel_tile_height(fb, color_plane));
2403 
2404 	/* Catch potential overflows early */
2405 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2406 			    fb->offsets[color_plane])) {
2407 		DRM_DEBUG_KMS("Bad offset 0x%08x or pitch %d for color plane %d\n",
2408 			      fb->offsets[color_plane], fb->pitches[color_plane],
2409 			      color_plane);
2410 		return -ERANGE;
2411 	}
2412 
2413 	*x = 0;
2414 	*y = 0;
2415 
2416 	intel_adjust_aligned_offset(x, y,
2417 				    fb, color_plane, DRM_MODE_ROTATE_0,
2418 				    fb->pitches[color_plane],
2419 				    fb->offsets[color_plane], 0);
2420 
2421 	return 0;
2422 }
2423 
2424 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2425 {
2426 	switch (fb_modifier) {
2427 	case I915_FORMAT_MOD_X_TILED:
2428 		return I915_TILING_X;
2429 	case I915_FORMAT_MOD_Y_TILED:
2430 	case I915_FORMAT_MOD_Y_TILED_CCS:
2431 		return I915_TILING_Y;
2432 	default:
2433 		return I915_TILING_NONE;
2434 	}
2435 }
2436 
2437 /*
2438  * From the Sky Lake PRM:
2439  * "The Color Control Surface (CCS) contains the compression status of
2440  *  the cache-line pairs. The compression state of the cache-line pair
2441  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2442  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2443  *  cache-line-pairs. CCS is always Y tiled."
2444  *
2445  * Since cache line pairs refers to horizontally adjacent cache lines,
2446  * each cache line in the CCS corresponds to an area of 32x16 cache
2447  * lines on the main surface. Since each pixel is 4 bytes, this gives
2448  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2449  * main surface.
2450  */
2451 static const struct drm_format_info ccs_formats[] = {
2452 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2453 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2454 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2455 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2456 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2457 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2458 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2459 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2460 };
2461 
2462 static const struct drm_format_info *
2463 lookup_format_info(const struct drm_format_info formats[],
2464 		   int num_formats, u32 format)
2465 {
2466 	int i;
2467 
2468 	for (i = 0; i < num_formats; i++) {
2469 		if (formats[i].format == format)
2470 			return &formats[i];
2471 	}
2472 
2473 	return NULL;
2474 }
2475 
2476 static const struct drm_format_info *
2477 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2478 {
2479 	switch (cmd->modifier[0]) {
2480 	case I915_FORMAT_MOD_Y_TILED_CCS:
2481 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2482 		return lookup_format_info(ccs_formats,
2483 					  ARRAY_SIZE(ccs_formats),
2484 					  cmd->pixel_format);
2485 	default:
2486 		return NULL;
2487 	}
2488 }
2489 
2490 bool is_ccs_modifier(u64 modifier)
2491 {
2492 	return modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2493 	       modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2494 }
2495 
2496 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2497 			      u32 pixel_format, u64 modifier)
2498 {
2499 	struct intel_crtc *crtc;
2500 	struct intel_plane *plane;
2501 
2502 	/*
2503 	 * We assume the primary plane for pipe A has
2504 	 * the highest stride limits of them all.
2505 	 */
2506 	crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
2507 	plane = to_intel_plane(crtc->base.primary);
2508 
2509 	return plane->max_stride(plane, pixel_format, modifier,
2510 				 DRM_MODE_ROTATE_0);
2511 }
2512 
2513 static
2514 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2515 			u32 pixel_format, u64 modifier)
2516 {
2517 	/*
2518 	 * Arbitrary limit for gen4+ chosen to match the
2519 	 * render engine max stride.
2520 	 *
2521 	 * The new CCS hash mode makes remapping impossible
2522 	 */
2523 	if (!is_ccs_modifier(modifier)) {
2524 		if (INTEL_GEN(dev_priv) >= 7)
2525 			return 256*1024;
2526 		else if (INTEL_GEN(dev_priv) >= 4)
2527 			return 128*1024;
2528 	}
2529 
2530 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2531 }
2532 
2533 static u32
2534 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2535 {
2536 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2537 
2538 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2539 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2540 							   fb->format->format,
2541 							   fb->modifier);
2542 
2543 		/*
2544 		 * To make remapping with linear generally feasible
2545 		 * we need the stride to be page aligned.
2546 		 */
2547 		if (fb->pitches[color_plane] > max_stride)
2548 			return intel_tile_size(dev_priv);
2549 		else
2550 			return 64;
2551 	} else {
2552 		return intel_tile_width_bytes(fb, color_plane);
2553 	}
2554 }
2555 
2556 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2557 {
2558 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2559 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2560 	const struct drm_framebuffer *fb = plane_state->base.fb;
2561 	int i;
2562 
2563 	/* We don't want to deal with remapping with cursors */
2564 	if (plane->id == PLANE_CURSOR)
2565 		return false;
2566 
2567 	/*
2568 	 * The display engine limits already match/exceed the
2569 	 * render engine limits, so not much point in remapping.
2570 	 * Would also need to deal with the fence POT alignment
2571 	 * and gen2 2KiB GTT tile size.
2572 	 */
2573 	if (INTEL_GEN(dev_priv) < 4)
2574 		return false;
2575 
2576 	/*
2577 	 * The new CCS hash mode isn't compatible with remapping as
2578 	 * the virtual address of the pages affects the compressed data.
2579 	 */
2580 	if (is_ccs_modifier(fb->modifier))
2581 		return false;
2582 
2583 	/* Linear needs a page aligned stride for remapping */
2584 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2585 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
2586 
2587 		for (i = 0; i < fb->format->num_planes; i++) {
2588 			if (fb->pitches[i] & alignment)
2589 				return false;
2590 		}
2591 	}
2592 
2593 	return true;
2594 }
2595 
2596 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2597 {
2598 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
2599 	const struct drm_framebuffer *fb = plane_state->base.fb;
2600 	unsigned int rotation = plane_state->base.rotation;
2601 	u32 stride, max_stride;
2602 
2603 	/*
2604 	 * No remapping for invisible planes since we don't have
2605 	 * an actual source viewport to remap.
2606 	 */
2607 	if (!plane_state->base.visible)
2608 		return false;
2609 
2610 	if (!intel_plane_can_remap(plane_state))
2611 		return false;
2612 
2613 	/*
2614 	 * FIXME: aux plane limits on gen9+ are
2615 	 * unclear in Bspec, for now no checking.
2616 	 */
2617 	stride = intel_fb_pitch(fb, 0, rotation);
2618 	max_stride = plane->max_stride(plane, fb->format->format,
2619 				       fb->modifier, rotation);
2620 
2621 	return stride > max_stride;
2622 }
2623 
2624 static int
2625 intel_fill_fb_info(struct drm_i915_private *dev_priv,
2626 		   struct drm_framebuffer *fb)
2627 {
2628 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2629 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2630 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2631 	u32 gtt_offset_rotated = 0;
2632 	unsigned int max_size = 0;
2633 	int i, num_planes = fb->format->num_planes;
2634 	unsigned int tile_size = intel_tile_size(dev_priv);
2635 
2636 	for (i = 0; i < num_planes; i++) {
2637 		unsigned int width, height;
2638 		unsigned int cpp, size;
2639 		u32 offset;
2640 		int x, y;
2641 		int ret;
2642 
2643 		cpp = fb->format->cpp[i];
2644 		width = drm_framebuffer_plane_width(fb->width, fb, i);
2645 		height = drm_framebuffer_plane_height(fb->height, fb, i);
2646 
2647 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
2648 		if (ret) {
2649 			DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2650 				      i, fb->offsets[i]);
2651 			return ret;
2652 		}
2653 
2654 		if (is_ccs_modifier(fb->modifier) && i == 1) {
2655 			int hsub = fb->format->hsub;
2656 			int vsub = fb->format->vsub;
2657 			int tile_width, tile_height;
2658 			int main_x, main_y;
2659 			int ccs_x, ccs_y;
2660 
2661 			intel_tile_dims(fb, i, &tile_width, &tile_height);
2662 			tile_width *= hsub;
2663 			tile_height *= vsub;
2664 
2665 			ccs_x = (x * hsub) % tile_width;
2666 			ccs_y = (y * vsub) % tile_height;
2667 			main_x = intel_fb->normal[0].x % tile_width;
2668 			main_y = intel_fb->normal[0].y % tile_height;
2669 
2670 			/*
2671 			 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2672 			 * x/y offsets must match between CCS and the main surface.
2673 			 */
2674 			if (main_x != ccs_x || main_y != ccs_y) {
2675 				DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2676 					      main_x, main_y,
2677 					      ccs_x, ccs_y,
2678 					      intel_fb->normal[0].x,
2679 					      intel_fb->normal[0].y,
2680 					      x, y);
2681 				return -EINVAL;
2682 			}
2683 		}
2684 
2685 		/*
2686 		 * The fence (if used) is aligned to the start of the object
2687 		 * so having the framebuffer wrap around across the edge of the
2688 		 * fenced region doesn't really work. We have no API to configure
2689 		 * the fence start offset within the object (nor could we probably
2690 		 * on gen2/3). So it's just easier if we just require that the
2691 		 * fb layout agrees with the fence layout. We already check that the
2692 		 * fb stride matches the fence stride elsewhere.
2693 		 */
2694 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
2695 		    (x + width) * cpp > fb->pitches[i]) {
2696 			DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
2697 				      i, fb->offsets[i]);
2698 			return -EINVAL;
2699 		}
2700 
2701 		/*
2702 		 * First pixel of the framebuffer from
2703 		 * the start of the normal gtt mapping.
2704 		 */
2705 		intel_fb->normal[i].x = x;
2706 		intel_fb->normal[i].y = y;
2707 
2708 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
2709 						      fb->pitches[i],
2710 						      DRM_MODE_ROTATE_0,
2711 						      tile_size);
2712 		offset /= tile_size;
2713 
2714 		if (!is_surface_linear(fb->modifier, i)) {
2715 			unsigned int tile_width, tile_height;
2716 			unsigned int pitch_tiles;
2717 			struct drm_rect r;
2718 
2719 			intel_tile_dims(fb, i, &tile_width, &tile_height);
2720 
2721 			rot_info->plane[i].offset = offset;
2722 			rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2723 			rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2724 			rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2725 
2726 			intel_fb->rotated[i].pitch =
2727 				rot_info->plane[i].height * tile_height;
2728 
2729 			/* how many tiles does this plane need */
2730 			size = rot_info->plane[i].stride * rot_info->plane[i].height;
2731 			/*
2732 			 * If the plane isn't horizontally tile aligned,
2733 			 * we need one more tile.
2734 			 */
2735 			if (x != 0)
2736 				size++;
2737 
2738 			/* rotate the x/y offsets to match the GTT view */
2739 			r.x1 = x;
2740 			r.y1 = y;
2741 			r.x2 = x + width;
2742 			r.y2 = y + height;
2743 			drm_rect_rotate(&r,
2744 					rot_info->plane[i].width * tile_width,
2745 					rot_info->plane[i].height * tile_height,
2746 					DRM_MODE_ROTATE_270);
2747 			x = r.x1;
2748 			y = r.y1;
2749 
2750 			/* rotate the tile dimensions to match the GTT view */
2751 			pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2752 			swap(tile_width, tile_height);
2753 
2754 			/*
2755 			 * We only keep the x/y offsets, so push all of the
2756 			 * gtt offset into the x/y offsets.
2757 			 */
2758 			intel_adjust_tile_offset(&x, &y,
2759 						 tile_width, tile_height,
2760 						 tile_size, pitch_tiles,
2761 						 gtt_offset_rotated * tile_size, 0);
2762 
2763 			gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2764 
2765 			/*
2766 			 * First pixel of the framebuffer from
2767 			 * the start of the rotated gtt mapping.
2768 			 */
2769 			intel_fb->rotated[i].x = x;
2770 			intel_fb->rotated[i].y = y;
2771 		} else {
2772 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2773 					    x * cpp, tile_size);
2774 		}
2775 
2776 		/* how many tiles in total needed in the bo */
2777 		max_size = max(max_size, offset + size);
2778 	}
2779 
2780 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2781 		DRM_DEBUG_KMS("fb too big for bo (need %llu bytes, have %zu bytes)\n",
2782 			      mul_u32_u32(max_size, tile_size), obj->base.size);
2783 		return -EINVAL;
2784 	}
2785 
2786 	return 0;
2787 }
2788 
2789 static void
2790 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
2791 {
2792 	struct drm_i915_private *dev_priv =
2793 		to_i915(plane_state->base.plane->dev);
2794 	struct drm_framebuffer *fb = plane_state->base.fb;
2795 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2796 	struct intel_rotation_info *info = &plane_state->view.rotated;
2797 	unsigned int rotation = plane_state->base.rotation;
2798 	int i, num_planes = fb->format->num_planes;
2799 	unsigned int tile_size = intel_tile_size(dev_priv);
2800 	unsigned int src_x, src_y;
2801 	unsigned int src_w, src_h;
2802 	u32 gtt_offset = 0;
2803 
2804 	memset(&plane_state->view, 0, sizeof(plane_state->view));
2805 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
2806 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
2807 
2808 	src_x = plane_state->base.src.x1 >> 16;
2809 	src_y = plane_state->base.src.y1 >> 16;
2810 	src_w = drm_rect_width(&plane_state->base.src) >> 16;
2811 	src_h = drm_rect_height(&plane_state->base.src) >> 16;
2812 
2813 	WARN_ON(is_ccs_modifier(fb->modifier));
2814 
2815 	/* Make src coordinates relative to the viewport */
2816 	drm_rect_translate(&plane_state->base.src,
2817 			   -(src_x << 16), -(src_y << 16));
2818 
2819 	/* Rotate src coordinates to match rotated GTT view */
2820 	if (drm_rotation_90_or_270(rotation))
2821 		drm_rect_rotate(&plane_state->base.src,
2822 				src_w << 16, src_h << 16,
2823 				DRM_MODE_ROTATE_270);
2824 
2825 	for (i = 0; i < num_planes; i++) {
2826 		unsigned int hsub = i ? fb->format->hsub : 1;
2827 		unsigned int vsub = i ? fb->format->vsub : 1;
2828 		unsigned int cpp = fb->format->cpp[i];
2829 		unsigned int tile_width, tile_height;
2830 		unsigned int width, height;
2831 		unsigned int pitch_tiles;
2832 		unsigned int x, y;
2833 		u32 offset;
2834 
2835 		intel_tile_dims(fb, i, &tile_width, &tile_height);
2836 
2837 		x = src_x / hsub;
2838 		y = src_y / vsub;
2839 		width = src_w / hsub;
2840 		height = src_h / vsub;
2841 
2842 		/*
2843 		 * First pixel of the src viewport from the
2844 		 * start of the normal gtt mapping.
2845 		 */
2846 		x += intel_fb->normal[i].x;
2847 		y += intel_fb->normal[i].y;
2848 
2849 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
2850 						      fb, i, fb->pitches[i],
2851 						      DRM_MODE_ROTATE_0, tile_size);
2852 		offset /= tile_size;
2853 
2854 		info->plane[i].offset = offset;
2855 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
2856 						     tile_width * cpp);
2857 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2858 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2859 
2860 		if (drm_rotation_90_or_270(rotation)) {
2861 			struct drm_rect r;
2862 
2863 			/* rotate the x/y offsets to match the GTT view */
2864 			r.x1 = x;
2865 			r.y1 = y;
2866 			r.x2 = x + width;
2867 			r.y2 = y + height;
2868 			drm_rect_rotate(&r,
2869 					info->plane[i].width * tile_width,
2870 					info->plane[i].height * tile_height,
2871 					DRM_MODE_ROTATE_270);
2872 			x = r.x1;
2873 			y = r.y1;
2874 
2875 			pitch_tiles = info->plane[i].height;
2876 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
2877 
2878 			/* rotate the tile dimensions to match the GTT view */
2879 			swap(tile_width, tile_height);
2880 		} else {
2881 			pitch_tiles = info->plane[i].width;
2882 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
2883 		}
2884 
2885 		/*
2886 		 * We only keep the x/y offsets, so push all of the
2887 		 * gtt offset into the x/y offsets.
2888 		 */
2889 		intel_adjust_tile_offset(&x, &y,
2890 					 tile_width, tile_height,
2891 					 tile_size, pitch_tiles,
2892 					 gtt_offset * tile_size, 0);
2893 
2894 		gtt_offset += info->plane[i].width * info->plane[i].height;
2895 
2896 		plane_state->color_plane[i].offset = 0;
2897 		plane_state->color_plane[i].x = x;
2898 		plane_state->color_plane[i].y = y;
2899 	}
2900 }
2901 
2902 static int
2903 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
2904 {
2905 	const struct intel_framebuffer *fb =
2906 		to_intel_framebuffer(plane_state->base.fb);
2907 	unsigned int rotation = plane_state->base.rotation;
2908 	int i, num_planes;
2909 
2910 	if (!fb)
2911 		return 0;
2912 
2913 	num_planes = fb->base.format->num_planes;
2914 
2915 	if (intel_plane_needs_remap(plane_state)) {
2916 		intel_plane_remap_gtt(plane_state);
2917 
2918 		/*
2919 		 * Sometimes even remapping can't overcome
2920 		 * the stride limitations :( Can happen with
2921 		 * big plane sizes and suitably misaligned
2922 		 * offsets.
2923 		 */
2924 		return intel_plane_check_stride(plane_state);
2925 	}
2926 
2927 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
2928 
2929 	for (i = 0; i < num_planes; i++) {
2930 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
2931 		plane_state->color_plane[i].offset = 0;
2932 
2933 		if (drm_rotation_90_or_270(rotation)) {
2934 			plane_state->color_plane[i].x = fb->rotated[i].x;
2935 			plane_state->color_plane[i].y = fb->rotated[i].y;
2936 		} else {
2937 			plane_state->color_plane[i].x = fb->normal[i].x;
2938 			plane_state->color_plane[i].y = fb->normal[i].y;
2939 		}
2940 	}
2941 
2942 	/* Rotate src coordinates to match rotated GTT view */
2943 	if (drm_rotation_90_or_270(rotation))
2944 		drm_rect_rotate(&plane_state->base.src,
2945 				fb->base.width << 16, fb->base.height << 16,
2946 				DRM_MODE_ROTATE_270);
2947 
2948 	return intel_plane_check_stride(plane_state);
2949 }
2950 
2951 static int i9xx_format_to_fourcc(int format)
2952 {
2953 	switch (format) {
2954 	case DISPPLANE_8BPP:
2955 		return DRM_FORMAT_C8;
2956 	case DISPPLANE_BGRX555:
2957 		return DRM_FORMAT_XRGB1555;
2958 	case DISPPLANE_BGRX565:
2959 		return DRM_FORMAT_RGB565;
2960 	default:
2961 	case DISPPLANE_BGRX888:
2962 		return DRM_FORMAT_XRGB8888;
2963 	case DISPPLANE_RGBX888:
2964 		return DRM_FORMAT_XBGR8888;
2965 	case DISPPLANE_BGRX101010:
2966 		return DRM_FORMAT_XRGB2101010;
2967 	case DISPPLANE_RGBX101010:
2968 		return DRM_FORMAT_XBGR2101010;
2969 	}
2970 }
2971 
2972 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2973 {
2974 	switch (format) {
2975 	case PLANE_CTL_FORMAT_RGB_565:
2976 		return DRM_FORMAT_RGB565;
2977 	case PLANE_CTL_FORMAT_NV12:
2978 		return DRM_FORMAT_NV12;
2979 	case PLANE_CTL_FORMAT_P010:
2980 		return DRM_FORMAT_P010;
2981 	case PLANE_CTL_FORMAT_P012:
2982 		return DRM_FORMAT_P012;
2983 	case PLANE_CTL_FORMAT_P016:
2984 		return DRM_FORMAT_P016;
2985 	case PLANE_CTL_FORMAT_Y210:
2986 		return DRM_FORMAT_Y210;
2987 	case PLANE_CTL_FORMAT_Y212:
2988 		return DRM_FORMAT_Y212;
2989 	case PLANE_CTL_FORMAT_Y216:
2990 		return DRM_FORMAT_Y216;
2991 	case PLANE_CTL_FORMAT_Y410:
2992 		return DRM_FORMAT_XVYU2101010;
2993 	case PLANE_CTL_FORMAT_Y412:
2994 		return DRM_FORMAT_XVYU12_16161616;
2995 	case PLANE_CTL_FORMAT_Y416:
2996 		return DRM_FORMAT_XVYU16161616;
2997 	default:
2998 	case PLANE_CTL_FORMAT_XRGB_8888:
2999 		if (rgb_order) {
3000 			if (alpha)
3001 				return DRM_FORMAT_ABGR8888;
3002 			else
3003 				return DRM_FORMAT_XBGR8888;
3004 		} else {
3005 			if (alpha)
3006 				return DRM_FORMAT_ARGB8888;
3007 			else
3008 				return DRM_FORMAT_XRGB8888;
3009 		}
3010 	case PLANE_CTL_FORMAT_XRGB_2101010:
3011 		if (rgb_order)
3012 			return DRM_FORMAT_XBGR2101010;
3013 		else
3014 			return DRM_FORMAT_XRGB2101010;
3015 	case PLANE_CTL_FORMAT_XRGB_16161616F:
3016 		if (rgb_order) {
3017 			if (alpha)
3018 				return DRM_FORMAT_ABGR16161616F;
3019 			else
3020 				return DRM_FORMAT_XBGR16161616F;
3021 		} else {
3022 			if (alpha)
3023 				return DRM_FORMAT_ARGB16161616F;
3024 			else
3025 				return DRM_FORMAT_XRGB16161616F;
3026 		}
3027 	}
3028 }
3029 
3030 static bool
3031 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3032 			      struct intel_initial_plane_config *plane_config)
3033 {
3034 	struct drm_device *dev = crtc->base.dev;
3035 	struct drm_i915_private *dev_priv = to_i915(dev);
3036 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3037 	struct drm_framebuffer *fb = &plane_config->fb->base;
3038 	u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
3039 	u32 size_aligned = round_up(plane_config->base + plane_config->size,
3040 				    PAGE_SIZE);
3041 	struct drm_i915_gem_object *obj;
3042 	bool ret = false;
3043 
3044 	size_aligned -= base_aligned;
3045 
3046 	if (plane_config->size == 0)
3047 		return false;
3048 
3049 	/* If the FB is too big, just don't use it since fbdev is not very
3050 	 * important and we should probably use that space with FBC or other
3051 	 * features. */
3052 	if (size_aligned * 2 > dev_priv->stolen_usable_size)
3053 		return false;
3054 
3055 	switch (fb->modifier) {
3056 	case DRM_FORMAT_MOD_LINEAR:
3057 	case I915_FORMAT_MOD_X_TILED:
3058 	case I915_FORMAT_MOD_Y_TILED:
3059 		break;
3060 	default:
3061 		DRM_DEBUG_DRIVER("Unsupported modifier for initial FB: 0x%llx\n",
3062 				 fb->modifier);
3063 		return false;
3064 	}
3065 
3066 	mutex_lock(&dev->struct_mutex);
3067 	obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
3068 							     base_aligned,
3069 							     base_aligned,
3070 							     size_aligned);
3071 	mutex_unlock(&dev->struct_mutex);
3072 	if (!obj)
3073 		return false;
3074 
3075 	switch (plane_config->tiling) {
3076 	case I915_TILING_NONE:
3077 		break;
3078 	case I915_TILING_X:
3079 	case I915_TILING_Y:
3080 		obj->tiling_and_stride = fb->pitches[0] | plane_config->tiling;
3081 		break;
3082 	default:
3083 		MISSING_CASE(plane_config->tiling);
3084 		goto out;
3085 	}
3086 
3087 	mode_cmd.pixel_format = fb->format->format;
3088 	mode_cmd.width = fb->width;
3089 	mode_cmd.height = fb->height;
3090 	mode_cmd.pitches[0] = fb->pitches[0];
3091 	mode_cmd.modifier[0] = fb->modifier;
3092 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3093 
3094 	if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
3095 		DRM_DEBUG_KMS("intel fb init failed\n");
3096 		goto out;
3097 	}
3098 
3099 
3100 	DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
3101 	ret = true;
3102 out:
3103 	i915_gem_object_put(obj);
3104 	return ret;
3105 }
3106 
3107 static void
3108 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3109 			struct intel_plane_state *plane_state,
3110 			bool visible)
3111 {
3112 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
3113 
3114 	plane_state->base.visible = visible;
3115 
3116 	if (visible)
3117 		crtc_state->base.plane_mask |= drm_plane_mask(&plane->base);
3118 	else
3119 		crtc_state->base.plane_mask &= ~drm_plane_mask(&plane->base);
3120 }
3121 
3122 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3123 {
3124 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3125 	struct drm_plane *plane;
3126 
3127 	/*
3128 	 * Active_planes aliases if multiple "primary" or cursor planes
3129 	 * have been used on the same (or wrong) pipe. plane_mask uses
3130 	 * unique ids, hence we can use that to reconstruct active_planes.
3131 	 */
3132 	crtc_state->active_planes = 0;
3133 
3134 	drm_for_each_plane_mask(plane, &dev_priv->drm,
3135 				crtc_state->base.plane_mask)
3136 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3137 }
3138 
3139 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3140 					 struct intel_plane *plane)
3141 {
3142 	struct intel_crtc_state *crtc_state =
3143 		to_intel_crtc_state(crtc->base.state);
3144 	struct intel_plane_state *plane_state =
3145 		to_intel_plane_state(plane->base.state);
3146 
3147 	DRM_DEBUG_KMS("Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3148 		      plane->base.base.id, plane->base.name,
3149 		      crtc->base.base.id, crtc->base.name);
3150 
3151 	intel_set_plane_visible(crtc_state, plane_state, false);
3152 	fixup_active_planes(crtc_state);
3153 	crtc_state->data_rate[plane->id] = 0;
3154 
3155 	if (plane->id == PLANE_PRIMARY)
3156 		intel_pre_disable_primary_noatomic(&crtc->base);
3157 
3158 	intel_disable_plane(plane, crtc_state);
3159 }
3160 
3161 static struct intel_frontbuffer *
3162 to_intel_frontbuffer(struct drm_framebuffer *fb)
3163 {
3164 	return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3165 }
3166 
3167 static void
3168 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3169 			     struct intel_initial_plane_config *plane_config)
3170 {
3171 	struct drm_device *dev = intel_crtc->base.dev;
3172 	struct drm_i915_private *dev_priv = to_i915(dev);
3173 	struct drm_crtc *c;
3174 	struct drm_plane *primary = intel_crtc->base.primary;
3175 	struct drm_plane_state *plane_state = primary->state;
3176 	struct intel_plane *intel_plane = to_intel_plane(primary);
3177 	struct intel_plane_state *intel_state =
3178 		to_intel_plane_state(plane_state);
3179 	struct drm_framebuffer *fb;
3180 
3181 	if (!plane_config->fb)
3182 		return;
3183 
3184 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3185 		fb = &plane_config->fb->base;
3186 		goto valid_fb;
3187 	}
3188 
3189 	kfree(plane_config->fb);
3190 
3191 	/*
3192 	 * Failed to alloc the obj, check to see if we should share
3193 	 * an fb with another CRTC instead
3194 	 */
3195 	for_each_crtc(dev, c) {
3196 		struct intel_plane_state *state;
3197 
3198 		if (c == &intel_crtc->base)
3199 			continue;
3200 
3201 		if (!to_intel_crtc(c)->active)
3202 			continue;
3203 
3204 		state = to_intel_plane_state(c->primary->state);
3205 		if (!state->vma)
3206 			continue;
3207 
3208 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
3209 			fb = state->base.fb;
3210 			drm_framebuffer_get(fb);
3211 			goto valid_fb;
3212 		}
3213 	}
3214 
3215 	/*
3216 	 * We've failed to reconstruct the BIOS FB.  Current display state
3217 	 * indicates that the primary plane is visible, but has a NULL FB,
3218 	 * which will lead to problems later if we don't fix it up.  The
3219 	 * simplest solution is to just disable the primary plane now and
3220 	 * pretend the BIOS never had it enabled.
3221 	 */
3222 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
3223 
3224 	return;
3225 
3226 valid_fb:
3227 	intel_state->base.rotation = plane_config->rotation;
3228 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
3229 				intel_state->base.rotation);
3230 	intel_state->color_plane[0].stride =
3231 		intel_fb_pitch(fb, 0, intel_state->base.rotation);
3232 
3233 	mutex_lock(&dev->struct_mutex);
3234 	intel_state->vma =
3235 		intel_pin_and_fence_fb_obj(fb,
3236 					   &intel_state->view,
3237 					   intel_plane_uses_fence(intel_state),
3238 					   &intel_state->flags);
3239 	mutex_unlock(&dev->struct_mutex);
3240 	if (IS_ERR(intel_state->vma)) {
3241 		DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
3242 			  intel_crtc->pipe, PTR_ERR(intel_state->vma));
3243 
3244 		intel_state->vma = NULL;
3245 		drm_framebuffer_put(fb);
3246 		return;
3247 	}
3248 
3249 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3250 
3251 	plane_state->src_x = 0;
3252 	plane_state->src_y = 0;
3253 	plane_state->src_w = fb->width << 16;
3254 	plane_state->src_h = fb->height << 16;
3255 
3256 	plane_state->crtc_x = 0;
3257 	plane_state->crtc_y = 0;
3258 	plane_state->crtc_w = fb->width;
3259 	plane_state->crtc_h = fb->height;
3260 
3261 	intel_state->base.src = drm_plane_state_src(plane_state);
3262 	intel_state->base.dst = drm_plane_state_dest(plane_state);
3263 
3264 	if (plane_config->tiling)
3265 		dev_priv->preserve_bios_swizzle = true;
3266 
3267 	plane_state->fb = fb;
3268 	plane_state->crtc = &intel_crtc->base;
3269 
3270 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3271 		  &to_intel_frontbuffer(fb)->bits);
3272 }
3273 
3274 static int skl_max_plane_width(const struct drm_framebuffer *fb,
3275 			       int color_plane,
3276 			       unsigned int rotation)
3277 {
3278 	int cpp = fb->format->cpp[color_plane];
3279 
3280 	switch (fb->modifier) {
3281 	case DRM_FORMAT_MOD_LINEAR:
3282 	case I915_FORMAT_MOD_X_TILED:
3283 		return 4096;
3284 	case I915_FORMAT_MOD_Y_TILED_CCS:
3285 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3286 		/* FIXME AUX plane? */
3287 	case I915_FORMAT_MOD_Y_TILED:
3288 	case I915_FORMAT_MOD_Yf_TILED:
3289 		if (cpp == 8)
3290 			return 2048;
3291 		else
3292 			return 4096;
3293 	default:
3294 		MISSING_CASE(fb->modifier);
3295 		return 2048;
3296 	}
3297 }
3298 
3299 static int glk_max_plane_width(const struct drm_framebuffer *fb,
3300 			       int color_plane,
3301 			       unsigned int rotation)
3302 {
3303 	int cpp = fb->format->cpp[color_plane];
3304 
3305 	switch (fb->modifier) {
3306 	case DRM_FORMAT_MOD_LINEAR:
3307 	case I915_FORMAT_MOD_X_TILED:
3308 		if (cpp == 8)
3309 			return 4096;
3310 		else
3311 			return 5120;
3312 	case I915_FORMAT_MOD_Y_TILED_CCS:
3313 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3314 		/* FIXME AUX plane? */
3315 	case I915_FORMAT_MOD_Y_TILED:
3316 	case I915_FORMAT_MOD_Yf_TILED:
3317 		if (cpp == 8)
3318 			return 2048;
3319 		else
3320 			return 5120;
3321 	default:
3322 		MISSING_CASE(fb->modifier);
3323 		return 2048;
3324 	}
3325 }
3326 
3327 static int icl_max_plane_width(const struct drm_framebuffer *fb,
3328 			       int color_plane,
3329 			       unsigned int rotation)
3330 {
3331 	return 5120;
3332 }
3333 
3334 static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3335 					   int main_x, int main_y, u32 main_offset)
3336 {
3337 	const struct drm_framebuffer *fb = plane_state->base.fb;
3338 	int hsub = fb->format->hsub;
3339 	int vsub = fb->format->vsub;
3340 	int aux_x = plane_state->color_plane[1].x;
3341 	int aux_y = plane_state->color_plane[1].y;
3342 	u32 aux_offset = plane_state->color_plane[1].offset;
3343 	u32 alignment = intel_surf_alignment(fb, 1);
3344 
3345 	while (aux_offset >= main_offset && aux_y <= main_y) {
3346 		int x, y;
3347 
3348 		if (aux_x == main_x && aux_y == main_y)
3349 			break;
3350 
3351 		if (aux_offset == 0)
3352 			break;
3353 
3354 		x = aux_x / hsub;
3355 		y = aux_y / vsub;
3356 		aux_offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 1,
3357 							       aux_offset, aux_offset - alignment);
3358 		aux_x = x * hsub + aux_x % hsub;
3359 		aux_y = y * vsub + aux_y % vsub;
3360 	}
3361 
3362 	if (aux_x != main_x || aux_y != main_y)
3363 		return false;
3364 
3365 	plane_state->color_plane[1].offset = aux_offset;
3366 	plane_state->color_plane[1].x = aux_x;
3367 	plane_state->color_plane[1].y = aux_y;
3368 
3369 	return true;
3370 }
3371 
3372 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3373 {
3374 	struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
3375 	const struct drm_framebuffer *fb = plane_state->base.fb;
3376 	unsigned int rotation = plane_state->base.rotation;
3377 	int x = plane_state->base.src.x1 >> 16;
3378 	int y = plane_state->base.src.y1 >> 16;
3379 	int w = drm_rect_width(&plane_state->base.src) >> 16;
3380 	int h = drm_rect_height(&plane_state->base.src) >> 16;
3381 	int max_width;
3382 	int max_height = 4096;
3383 	u32 alignment, offset, aux_offset = plane_state->color_plane[1].offset;
3384 
3385 	if (INTEL_GEN(dev_priv) >= 11)
3386 		max_width = icl_max_plane_width(fb, 0, rotation);
3387 	else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
3388 		max_width = glk_max_plane_width(fb, 0, rotation);
3389 	else
3390 		max_width = skl_max_plane_width(fb, 0, rotation);
3391 
3392 	if (w > max_width || h > max_height) {
3393 		DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3394 			      w, h, max_width, max_height);
3395 		return -EINVAL;
3396 	}
3397 
3398 	intel_add_fb_offsets(&x, &y, plane_state, 0);
3399 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3400 	alignment = intel_surf_alignment(fb, 0);
3401 
3402 	/*
3403 	 * AUX surface offset is specified as the distance from the
3404 	 * main surface offset, and it must be non-negative. Make
3405 	 * sure that is what we will get.
3406 	 */
3407 	if (offset > aux_offset)
3408 		offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3409 							   offset, aux_offset & ~(alignment - 1));
3410 
3411 	/*
3412 	 * When using an X-tiled surface, the plane blows up
3413 	 * if the x offset + width exceed the stride.
3414 	 *
3415 	 * TODO: linear and Y-tiled seem fine, Yf untested,
3416 	 */
3417 	if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3418 		int cpp = fb->format->cpp[0];
3419 
3420 		while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3421 			if (offset == 0) {
3422 				DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
3423 				return -EINVAL;
3424 			}
3425 
3426 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3427 								   offset, offset - alignment);
3428 		}
3429 	}
3430 
3431 	/*
3432 	 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3433 	 * they match with the main surface x/y offsets.
3434 	 */
3435 	if (is_ccs_modifier(fb->modifier)) {
3436 		while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
3437 			if (offset == 0)
3438 				break;
3439 
3440 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3441 								   offset, offset - alignment);
3442 		}
3443 
3444 		if (x != plane_state->color_plane[1].x || y != plane_state->color_plane[1].y) {
3445 			DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
3446 			return -EINVAL;
3447 		}
3448 	}
3449 
3450 	plane_state->color_plane[0].offset = offset;
3451 	plane_state->color_plane[0].x = x;
3452 	plane_state->color_plane[0].y = y;
3453 
3454 	/*
3455 	 * Put the final coordinates back so that the src
3456 	 * coordinate checks will see the right values.
3457 	 */
3458 	drm_rect_translate(&plane_state->base.src,
3459 			   (x << 16) - plane_state->base.src.x1,
3460 			   (y << 16) - plane_state->base.src.y1);
3461 
3462 	return 0;
3463 }
3464 
3465 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3466 {
3467 	const struct drm_framebuffer *fb = plane_state->base.fb;
3468 	unsigned int rotation = plane_state->base.rotation;
3469 	int max_width = skl_max_plane_width(fb, 1, rotation);
3470 	int max_height = 4096;
3471 	int x = plane_state->base.src.x1 >> 17;
3472 	int y = plane_state->base.src.y1 >> 17;
3473 	int w = drm_rect_width(&plane_state->base.src) >> 17;
3474 	int h = drm_rect_height(&plane_state->base.src) >> 17;
3475 	u32 offset;
3476 
3477 	intel_add_fb_offsets(&x, &y, plane_state, 1);
3478 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
3479 
3480 	/* FIXME not quite sure how/if these apply to the chroma plane */
3481 	if (w > max_width || h > max_height) {
3482 		DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
3483 			      w, h, max_width, max_height);
3484 		return -EINVAL;
3485 	}
3486 
3487 	plane_state->color_plane[1].offset = offset;
3488 	plane_state->color_plane[1].x = x;
3489 	plane_state->color_plane[1].y = y;
3490 
3491 	return 0;
3492 }
3493 
3494 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3495 {
3496 	const struct drm_framebuffer *fb = plane_state->base.fb;
3497 	int src_x = plane_state->base.src.x1 >> 16;
3498 	int src_y = plane_state->base.src.y1 >> 16;
3499 	int hsub = fb->format->hsub;
3500 	int vsub = fb->format->vsub;
3501 	int x = src_x / hsub;
3502 	int y = src_y / vsub;
3503 	u32 offset;
3504 
3505 	intel_add_fb_offsets(&x, &y, plane_state, 1);
3506 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 1);
3507 
3508 	plane_state->color_plane[1].offset = offset;
3509 	plane_state->color_plane[1].x = x * hsub + src_x % hsub;
3510 	plane_state->color_plane[1].y = y * vsub + src_y % vsub;
3511 
3512 	return 0;
3513 }
3514 
3515 int skl_check_plane_surface(struct intel_plane_state *plane_state)
3516 {
3517 	const struct drm_framebuffer *fb = plane_state->base.fb;
3518 	int ret;
3519 
3520 	ret = intel_plane_compute_gtt(plane_state);
3521 	if (ret)
3522 		return ret;
3523 
3524 	if (!plane_state->base.visible)
3525 		return 0;
3526 
3527 	/*
3528 	 * Handle the AUX surface first since
3529 	 * the main surface setup depends on it.
3530 	 */
3531 	if (is_planar_yuv_format(fb->format->format)) {
3532 		ret = skl_check_nv12_aux_surface(plane_state);
3533 		if (ret)
3534 			return ret;
3535 	} else if (is_ccs_modifier(fb->modifier)) {
3536 		ret = skl_check_ccs_aux_surface(plane_state);
3537 		if (ret)
3538 			return ret;
3539 	} else {
3540 		plane_state->color_plane[1].offset = ~0xfff;
3541 		plane_state->color_plane[1].x = 0;
3542 		plane_state->color_plane[1].y = 0;
3543 	}
3544 
3545 	ret = skl_check_main_surface(plane_state);
3546 	if (ret)
3547 		return ret;
3548 
3549 	return 0;
3550 }
3551 
3552 unsigned int
3553 i9xx_plane_max_stride(struct intel_plane *plane,
3554 		      u32 pixel_format, u64 modifier,
3555 		      unsigned int rotation)
3556 {
3557 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3558 
3559 	if (!HAS_GMCH(dev_priv)) {
3560 		return 32*1024;
3561 	} else if (INTEL_GEN(dev_priv) >= 4) {
3562 		if (modifier == I915_FORMAT_MOD_X_TILED)
3563 			return 16*1024;
3564 		else
3565 			return 32*1024;
3566 	} else if (INTEL_GEN(dev_priv) >= 3) {
3567 		if (modifier == I915_FORMAT_MOD_X_TILED)
3568 			return 8*1024;
3569 		else
3570 			return 16*1024;
3571 	} else {
3572 		if (plane->i9xx_plane == PLANE_C)
3573 			return 4*1024;
3574 		else
3575 			return 8*1024;
3576 	}
3577 }
3578 
3579 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
3580 {
3581 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3582 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3583 	u32 dspcntr = 0;
3584 
3585 	if (crtc_state->gamma_enable)
3586 		dspcntr |= DISPPLANE_GAMMA_ENABLE;
3587 
3588 	if (crtc_state->csc_enable)
3589 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
3590 
3591 	if (INTEL_GEN(dev_priv) < 5)
3592 		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
3593 
3594 	return dspcntr;
3595 }
3596 
3597 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
3598 			  const struct intel_plane_state *plane_state)
3599 {
3600 	struct drm_i915_private *dev_priv =
3601 		to_i915(plane_state->base.plane->dev);
3602 	const struct drm_framebuffer *fb = plane_state->base.fb;
3603 	unsigned int rotation = plane_state->base.rotation;
3604 	u32 dspcntr;
3605 
3606 	dspcntr = DISPLAY_PLANE_ENABLE;
3607 
3608 	if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
3609 	    IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
3610 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3611 
3612 	switch (fb->format->format) {
3613 	case DRM_FORMAT_C8:
3614 		dspcntr |= DISPPLANE_8BPP;
3615 		break;
3616 	case DRM_FORMAT_XRGB1555:
3617 		dspcntr |= DISPPLANE_BGRX555;
3618 		break;
3619 	case DRM_FORMAT_RGB565:
3620 		dspcntr |= DISPPLANE_BGRX565;
3621 		break;
3622 	case DRM_FORMAT_XRGB8888:
3623 		dspcntr |= DISPPLANE_BGRX888;
3624 		break;
3625 	case DRM_FORMAT_XBGR8888:
3626 		dspcntr |= DISPPLANE_RGBX888;
3627 		break;
3628 	case DRM_FORMAT_XRGB2101010:
3629 		dspcntr |= DISPPLANE_BGRX101010;
3630 		break;
3631 	case DRM_FORMAT_XBGR2101010:
3632 		dspcntr |= DISPPLANE_RGBX101010;
3633 		break;
3634 	default:
3635 		MISSING_CASE(fb->format->format);
3636 		return 0;
3637 	}
3638 
3639 	if (INTEL_GEN(dev_priv) >= 4 &&
3640 	    fb->modifier == I915_FORMAT_MOD_X_TILED)
3641 		dspcntr |= DISPPLANE_TILED;
3642 
3643 	if (rotation & DRM_MODE_ROTATE_180)
3644 		dspcntr |= DISPPLANE_ROTATE_180;
3645 
3646 	if (rotation & DRM_MODE_REFLECT_X)
3647 		dspcntr |= DISPPLANE_MIRROR;
3648 
3649 	return dspcntr;
3650 }
3651 
3652 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
3653 {
3654 	struct drm_i915_private *dev_priv =
3655 		to_i915(plane_state->base.plane->dev);
3656 	int src_x, src_y;
3657 	u32 offset;
3658 	int ret;
3659 
3660 	ret = intel_plane_compute_gtt(plane_state);
3661 	if (ret)
3662 		return ret;
3663 
3664 	if (!plane_state->base.visible)
3665 		return 0;
3666 
3667 	src_x = plane_state->base.src.x1 >> 16;
3668 	src_y = plane_state->base.src.y1 >> 16;
3669 
3670 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
3671 
3672 	if (INTEL_GEN(dev_priv) >= 4)
3673 		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
3674 							    plane_state, 0);
3675 	else
3676 		offset = 0;
3677 
3678 	/*
3679 	 * Put the final coordinates back so that the src
3680 	 * coordinate checks will see the right values.
3681 	 */
3682 	drm_rect_translate(&plane_state->base.src,
3683 			   (src_x << 16) - plane_state->base.src.x1,
3684 			   (src_y << 16) - plane_state->base.src.y1);
3685 
3686 	/* HSW/BDW do this automagically in hardware */
3687 	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
3688 		unsigned int rotation = plane_state->base.rotation;
3689 		int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3690 		int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3691 
3692 		if (rotation & DRM_MODE_ROTATE_180) {
3693 			src_x += src_w - 1;
3694 			src_y += src_h - 1;
3695 		} else if (rotation & DRM_MODE_REFLECT_X) {
3696 			src_x += src_w - 1;
3697 		}
3698 	}
3699 
3700 	plane_state->color_plane[0].offset = offset;
3701 	plane_state->color_plane[0].x = src_x;
3702 	plane_state->color_plane[0].y = src_y;
3703 
3704 	return 0;
3705 }
3706 
3707 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
3708 {
3709 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3710 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
3711 
3712 	if (IS_CHERRYVIEW(dev_priv))
3713 		return i9xx_plane == PLANE_B;
3714 	else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
3715 		return false;
3716 	else if (IS_GEN(dev_priv, 4))
3717 		return i9xx_plane == PLANE_C;
3718 	else
3719 		return i9xx_plane == PLANE_B ||
3720 			i9xx_plane == PLANE_C;
3721 }
3722 
3723 static int
3724 i9xx_plane_check(struct intel_crtc_state *crtc_state,
3725 		 struct intel_plane_state *plane_state)
3726 {
3727 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
3728 	int ret;
3729 
3730 	ret = chv_plane_check_rotation(plane_state);
3731 	if (ret)
3732 		return ret;
3733 
3734 	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
3735 						  &crtc_state->base,
3736 						  DRM_PLANE_HELPER_NO_SCALING,
3737 						  DRM_PLANE_HELPER_NO_SCALING,
3738 						  i9xx_plane_has_windowing(plane),
3739 						  true);
3740 	if (ret)
3741 		return ret;
3742 
3743 	ret = i9xx_check_plane_surface(plane_state);
3744 	if (ret)
3745 		return ret;
3746 
3747 	if (!plane_state->base.visible)
3748 		return 0;
3749 
3750 	ret = intel_plane_check_src_coordinates(plane_state);
3751 	if (ret)
3752 		return ret;
3753 
3754 	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
3755 
3756 	return 0;
3757 }
3758 
3759 static void i9xx_update_plane(struct intel_plane *plane,
3760 			      const struct intel_crtc_state *crtc_state,
3761 			      const struct intel_plane_state *plane_state)
3762 {
3763 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3764 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
3765 	u32 linear_offset;
3766 	int x = plane_state->color_plane[0].x;
3767 	int y = plane_state->color_plane[0].y;
3768 	int crtc_x = plane_state->base.dst.x1;
3769 	int crtc_y = plane_state->base.dst.y1;
3770 	int crtc_w = drm_rect_width(&plane_state->base.dst);
3771 	int crtc_h = drm_rect_height(&plane_state->base.dst);
3772 	unsigned long irqflags;
3773 	u32 dspaddr_offset;
3774 	u32 dspcntr;
3775 
3776 	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
3777 
3778 	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
3779 
3780 	if (INTEL_GEN(dev_priv) >= 4)
3781 		dspaddr_offset = plane_state->color_plane[0].offset;
3782 	else
3783 		dspaddr_offset = linear_offset;
3784 
3785 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3786 
3787 	I915_WRITE_FW(DSPSTRIDE(i9xx_plane), plane_state->color_plane[0].stride);
3788 
3789 	if (INTEL_GEN(dev_priv) < 4) {
3790 		/*
3791 		 * PLANE_A doesn't actually have a full window
3792 		 * generator but let's assume we still need to
3793 		 * program whatever is there.
3794 		 */
3795 		I915_WRITE_FW(DSPPOS(i9xx_plane), (crtc_y << 16) | crtc_x);
3796 		I915_WRITE_FW(DSPSIZE(i9xx_plane),
3797 			      ((crtc_h - 1) << 16) | (crtc_w - 1));
3798 	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
3799 		I915_WRITE_FW(PRIMPOS(i9xx_plane), (crtc_y << 16) | crtc_x);
3800 		I915_WRITE_FW(PRIMSIZE(i9xx_plane),
3801 			      ((crtc_h - 1) << 16) | (crtc_w - 1));
3802 		I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
3803 	}
3804 
3805 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3806 		I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
3807 	} else if (INTEL_GEN(dev_priv) >= 4) {
3808 		I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
3809 		I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
3810 	}
3811 
3812 	/*
3813 	 * The control register self-arms if the plane was previously
3814 	 * disabled. Try to make the plane enable atomic by writing
3815 	 * the control register just before the surface register.
3816 	 */
3817 	I915_WRITE_FW(DSPCNTR(i9xx_plane), dspcntr);
3818 	if (INTEL_GEN(dev_priv) >= 4)
3819 		I915_WRITE_FW(DSPSURF(i9xx_plane),
3820 			      intel_plane_ggtt_offset(plane_state) +
3821 			      dspaddr_offset);
3822 	else
3823 		I915_WRITE_FW(DSPADDR(i9xx_plane),
3824 			      intel_plane_ggtt_offset(plane_state) +
3825 			      dspaddr_offset);
3826 
3827 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
3828 }
3829 
3830 static void i9xx_disable_plane(struct intel_plane *plane,
3831 			       const struct intel_crtc_state *crtc_state)
3832 {
3833 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3834 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
3835 	unsigned long irqflags;
3836 	u32 dspcntr;
3837 
3838 	/*
3839 	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
3840 	 * enable on ilk+ affect the pipe bottom color as
3841 	 * well, so we must configure them even if the plane
3842 	 * is disabled.
3843 	 *
3844 	 * On pre-g4x there is no way to gamma correct the
3845 	 * pipe bottom color but we'll keep on doing this
3846 	 * anyway so that the crtc state readout works correctly.
3847 	 */
3848 	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
3849 
3850 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
3851 
3852 	I915_WRITE_FW(DSPCNTR(i9xx_plane), dspcntr);
3853 	if (INTEL_GEN(dev_priv) >= 4)
3854 		I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
3855 	else
3856 		I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
3857 
3858 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
3859 }
3860 
3861 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
3862 				    enum pipe *pipe)
3863 {
3864 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3865 	enum intel_display_power_domain power_domain;
3866 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
3867 	intel_wakeref_t wakeref;
3868 	bool ret;
3869 	u32 val;
3870 
3871 	/*
3872 	 * Not 100% correct for planes that can move between pipes,
3873 	 * but that's only the case for gen2-4 which don't have any
3874 	 * display power wells.
3875 	 */
3876 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
3877 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
3878 	if (!wakeref)
3879 		return false;
3880 
3881 	val = I915_READ(DSPCNTR(i9xx_plane));
3882 
3883 	ret = val & DISPLAY_PLANE_ENABLE;
3884 
3885 	if (INTEL_GEN(dev_priv) >= 5)
3886 		*pipe = plane->pipe;
3887 	else
3888 		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
3889 			DISPPLANE_SEL_PIPE_SHIFT;
3890 
3891 	intel_display_power_put(dev_priv, power_domain, wakeref);
3892 
3893 	return ret;
3894 }
3895 
3896 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3897 {
3898 	struct drm_device *dev = intel_crtc->base.dev;
3899 	struct drm_i915_private *dev_priv = to_i915(dev);
3900 
3901 	I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3902 	I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3903 	I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
3904 }
3905 
3906 /*
3907  * This function detaches (aka. unbinds) unused scalers in hardware
3908  */
3909 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
3910 {
3911 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3912 	const struct intel_crtc_scaler_state *scaler_state =
3913 		&crtc_state->scaler_state;
3914 	int i;
3915 
3916 	/* loop through and disable scalers that aren't in use */
3917 	for (i = 0; i < intel_crtc->num_scalers; i++) {
3918 		if (!scaler_state->scalers[i].in_use)
3919 			skl_detach_scaler(intel_crtc, i);
3920 	}
3921 }
3922 
3923 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
3924 					  int color_plane, unsigned int rotation)
3925 {
3926 	/*
3927 	 * The stride is either expressed as a multiple of 64 bytes chunks for
3928 	 * linear buffers or in number of tiles for tiled buffers.
3929 	 */
3930 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
3931 		return 64;
3932 	else if (drm_rotation_90_or_270(rotation))
3933 		return intel_tile_height(fb, color_plane);
3934 	else
3935 		return intel_tile_width_bytes(fb, color_plane);
3936 }
3937 
3938 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
3939 		     int color_plane)
3940 {
3941 	const struct drm_framebuffer *fb = plane_state->base.fb;
3942 	unsigned int rotation = plane_state->base.rotation;
3943 	u32 stride = plane_state->color_plane[color_plane].stride;
3944 
3945 	if (color_plane >= fb->format->num_planes)
3946 		return 0;
3947 
3948 	return stride / skl_plane_stride_mult(fb, color_plane, rotation);
3949 }
3950 
3951 static u32 skl_plane_ctl_format(u32 pixel_format)
3952 {
3953 	switch (pixel_format) {
3954 	case DRM_FORMAT_C8:
3955 		return PLANE_CTL_FORMAT_INDEXED;
3956 	case DRM_FORMAT_RGB565:
3957 		return PLANE_CTL_FORMAT_RGB_565;
3958 	case DRM_FORMAT_XBGR8888:
3959 	case DRM_FORMAT_ABGR8888:
3960 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
3961 	case DRM_FORMAT_XRGB8888:
3962 	case DRM_FORMAT_ARGB8888:
3963 		return PLANE_CTL_FORMAT_XRGB_8888;
3964 	case DRM_FORMAT_XBGR2101010:
3965 		return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
3966 	case DRM_FORMAT_XRGB2101010:
3967 		return PLANE_CTL_FORMAT_XRGB_2101010;
3968 	case DRM_FORMAT_XBGR16161616F:
3969 	case DRM_FORMAT_ABGR16161616F:
3970 		return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
3971 	case DRM_FORMAT_XRGB16161616F:
3972 	case DRM_FORMAT_ARGB16161616F:
3973 		return PLANE_CTL_FORMAT_XRGB_16161616F;
3974 	case DRM_FORMAT_YUYV:
3975 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
3976 	case DRM_FORMAT_YVYU:
3977 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
3978 	case DRM_FORMAT_UYVY:
3979 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3980 	case DRM_FORMAT_VYUY:
3981 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
3982 	case DRM_FORMAT_NV12:
3983 		return PLANE_CTL_FORMAT_NV12;
3984 	case DRM_FORMAT_P010:
3985 		return PLANE_CTL_FORMAT_P010;
3986 	case DRM_FORMAT_P012:
3987 		return PLANE_CTL_FORMAT_P012;
3988 	case DRM_FORMAT_P016:
3989 		return PLANE_CTL_FORMAT_P016;
3990 	case DRM_FORMAT_Y210:
3991 		return PLANE_CTL_FORMAT_Y210;
3992 	case DRM_FORMAT_Y212:
3993 		return PLANE_CTL_FORMAT_Y212;
3994 	case DRM_FORMAT_Y216:
3995 		return PLANE_CTL_FORMAT_Y216;
3996 	case DRM_FORMAT_XVYU2101010:
3997 		return PLANE_CTL_FORMAT_Y410;
3998 	case DRM_FORMAT_XVYU12_16161616:
3999 		return PLANE_CTL_FORMAT_Y412;
4000 	case DRM_FORMAT_XVYU16161616:
4001 		return PLANE_CTL_FORMAT_Y416;
4002 	default:
4003 		MISSING_CASE(pixel_format);
4004 	}
4005 
4006 	return 0;
4007 }
4008 
4009 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4010 {
4011 	if (!plane_state->base.fb->format->has_alpha)
4012 		return PLANE_CTL_ALPHA_DISABLE;
4013 
4014 	switch (plane_state->base.pixel_blend_mode) {
4015 	case DRM_MODE_BLEND_PIXEL_NONE:
4016 		return PLANE_CTL_ALPHA_DISABLE;
4017 	case DRM_MODE_BLEND_PREMULTI:
4018 		return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4019 	case DRM_MODE_BLEND_COVERAGE:
4020 		return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4021 	default:
4022 		MISSING_CASE(plane_state->base.pixel_blend_mode);
4023 		return PLANE_CTL_ALPHA_DISABLE;
4024 	}
4025 }
4026 
4027 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4028 {
4029 	if (!plane_state->base.fb->format->has_alpha)
4030 		return PLANE_COLOR_ALPHA_DISABLE;
4031 
4032 	switch (plane_state->base.pixel_blend_mode) {
4033 	case DRM_MODE_BLEND_PIXEL_NONE:
4034 		return PLANE_COLOR_ALPHA_DISABLE;
4035 	case DRM_MODE_BLEND_PREMULTI:
4036 		return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4037 	case DRM_MODE_BLEND_COVERAGE:
4038 		return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4039 	default:
4040 		MISSING_CASE(plane_state->base.pixel_blend_mode);
4041 		return PLANE_COLOR_ALPHA_DISABLE;
4042 	}
4043 }
4044 
4045 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4046 {
4047 	switch (fb_modifier) {
4048 	case DRM_FORMAT_MOD_LINEAR:
4049 		break;
4050 	case I915_FORMAT_MOD_X_TILED:
4051 		return PLANE_CTL_TILED_X;
4052 	case I915_FORMAT_MOD_Y_TILED:
4053 		return PLANE_CTL_TILED_Y;
4054 	case I915_FORMAT_MOD_Y_TILED_CCS:
4055 		return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4056 	case I915_FORMAT_MOD_Yf_TILED:
4057 		return PLANE_CTL_TILED_YF;
4058 	case I915_FORMAT_MOD_Yf_TILED_CCS:
4059 		return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4060 	default:
4061 		MISSING_CASE(fb_modifier);
4062 	}
4063 
4064 	return 0;
4065 }
4066 
4067 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4068 {
4069 	switch (rotate) {
4070 	case DRM_MODE_ROTATE_0:
4071 		break;
4072 	/*
4073 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4074 	 * while i915 HW rotation is clockwise, thats why this swapping.
4075 	 */
4076 	case DRM_MODE_ROTATE_90:
4077 		return PLANE_CTL_ROTATE_270;
4078 	case DRM_MODE_ROTATE_180:
4079 		return PLANE_CTL_ROTATE_180;
4080 	case DRM_MODE_ROTATE_270:
4081 		return PLANE_CTL_ROTATE_90;
4082 	default:
4083 		MISSING_CASE(rotate);
4084 	}
4085 
4086 	return 0;
4087 }
4088 
4089 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4090 {
4091 	switch (reflect) {
4092 	case 0:
4093 		break;
4094 	case DRM_MODE_REFLECT_X:
4095 		return PLANE_CTL_FLIP_HORIZONTAL;
4096 	case DRM_MODE_REFLECT_Y:
4097 	default:
4098 		MISSING_CASE(reflect);
4099 	}
4100 
4101 	return 0;
4102 }
4103 
4104 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4105 {
4106 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
4107 	u32 plane_ctl = 0;
4108 
4109 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4110 		return plane_ctl;
4111 
4112 	if (crtc_state->gamma_enable)
4113 		plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4114 
4115 	if (crtc_state->csc_enable)
4116 		plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4117 
4118 	return plane_ctl;
4119 }
4120 
4121 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4122 		  const struct intel_plane_state *plane_state)
4123 {
4124 	struct drm_i915_private *dev_priv =
4125 		to_i915(plane_state->base.plane->dev);
4126 	const struct drm_framebuffer *fb = plane_state->base.fb;
4127 	unsigned int rotation = plane_state->base.rotation;
4128 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4129 	u32 plane_ctl;
4130 
4131 	plane_ctl = PLANE_CTL_ENABLE;
4132 
4133 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4134 		plane_ctl |= skl_plane_ctl_alpha(plane_state);
4135 		plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4136 
4137 		if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
4138 			plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4139 
4140 		if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4141 			plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4142 	}
4143 
4144 	plane_ctl |= skl_plane_ctl_format(fb->format->format);
4145 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4146 	plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4147 
4148 	if (INTEL_GEN(dev_priv) >= 10)
4149 		plane_ctl |= cnl_plane_ctl_flip(rotation &
4150 						DRM_MODE_REFLECT_MASK);
4151 
4152 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
4153 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4154 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
4155 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4156 
4157 	return plane_ctl;
4158 }
4159 
4160 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4161 {
4162 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
4163 	u32 plane_color_ctl = 0;
4164 
4165 	if (INTEL_GEN(dev_priv) >= 11)
4166 		return plane_color_ctl;
4167 
4168 	if (crtc_state->gamma_enable)
4169 		plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4170 
4171 	if (crtc_state->csc_enable)
4172 		plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4173 
4174 	return plane_color_ctl;
4175 }
4176 
4177 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4178 			const struct intel_plane_state *plane_state)
4179 {
4180 	struct drm_i915_private *dev_priv =
4181 		to_i915(plane_state->base.plane->dev);
4182 	const struct drm_framebuffer *fb = plane_state->base.fb;
4183 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
4184 	u32 plane_color_ctl = 0;
4185 
4186 	plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4187 	plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4188 
4189 	if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4190 		if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
4191 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4192 		else
4193 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
4194 
4195 		if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4196 			plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4197 	} else if (fb->format->is_yuv) {
4198 		plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4199 	}
4200 
4201 	return plane_color_ctl;
4202 }
4203 
4204 static int
4205 __intel_display_resume(struct drm_device *dev,
4206 		       struct drm_atomic_state *state,
4207 		       struct drm_modeset_acquire_ctx *ctx)
4208 {
4209 	struct drm_crtc_state *crtc_state;
4210 	struct drm_crtc *crtc;
4211 	int i, ret;
4212 
4213 	intel_modeset_setup_hw_state(dev, ctx);
4214 	i915_redisable_vga(to_i915(dev));
4215 
4216 	if (!state)
4217 		return 0;
4218 
4219 	/*
4220 	 * We've duplicated the state, pointers to the old state are invalid.
4221 	 *
4222 	 * Don't attempt to use the old state until we commit the duplicated state.
4223 	 */
4224 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4225 		/*
4226 		 * Force recalculation even if we restore
4227 		 * current state. With fast modeset this may not result
4228 		 * in a modeset when the state is compatible.
4229 		 */
4230 		crtc_state->mode_changed = true;
4231 	}
4232 
4233 	/* ignore any reset values/BIOS leftovers in the WM registers */
4234 	if (!HAS_GMCH(to_i915(dev)))
4235 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
4236 
4237 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4238 
4239 	WARN_ON(ret == -EDEADLK);
4240 	return ret;
4241 }
4242 
4243 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4244 {
4245 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4246 		intel_has_gpu_reset(dev_priv));
4247 }
4248 
4249 void intel_prepare_reset(struct drm_i915_private *dev_priv)
4250 {
4251 	struct drm_device *dev = &dev_priv->drm;
4252 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4253 	struct drm_atomic_state *state;
4254 	int ret;
4255 
4256 	/* reset doesn't touch the display */
4257 	if (!i915_modparams.force_reset_modeset_test &&
4258 	    !gpu_reset_clobbers_display(dev_priv))
4259 		return;
4260 
4261 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
4262 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4263 	smp_mb__after_atomic();
4264 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4265 
4266 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4267 		DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
4268 		intel_gt_set_wedged(&dev_priv->gt);
4269 	}
4270 
4271 	/*
4272 	 * Need mode_config.mutex so that we don't
4273 	 * trample ongoing ->detect() and whatnot.
4274 	 */
4275 	mutex_lock(&dev->mode_config.mutex);
4276 	drm_modeset_acquire_init(ctx, 0);
4277 	while (1) {
4278 		ret = drm_modeset_lock_all_ctx(dev, ctx);
4279 		if (ret != -EDEADLK)
4280 			break;
4281 
4282 		drm_modeset_backoff(ctx);
4283 	}
4284 	/*
4285 	 * Disabling the crtcs gracefully seems nicer. Also the
4286 	 * g33 docs say we should at least disable all the planes.
4287 	 */
4288 	state = drm_atomic_helper_duplicate_state(dev, ctx);
4289 	if (IS_ERR(state)) {
4290 		ret = PTR_ERR(state);
4291 		DRM_ERROR("Duplicating state failed with %i\n", ret);
4292 		return;
4293 	}
4294 
4295 	ret = drm_atomic_helper_disable_all(dev, ctx);
4296 	if (ret) {
4297 		DRM_ERROR("Suspending crtc's failed with %i\n", ret);
4298 		drm_atomic_state_put(state);
4299 		return;
4300 	}
4301 
4302 	dev_priv->modeset_restore_state = state;
4303 	state->acquire_ctx = ctx;
4304 }
4305 
4306 void intel_finish_reset(struct drm_i915_private *dev_priv)
4307 {
4308 	struct drm_device *dev = &dev_priv->drm;
4309 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4310 	struct drm_atomic_state *state;
4311 	int ret;
4312 
4313 	/* reset doesn't touch the display */
4314 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
4315 		return;
4316 
4317 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
4318 	if (!state)
4319 		goto unlock;
4320 
4321 	/* reset doesn't touch the display */
4322 	if (!gpu_reset_clobbers_display(dev_priv)) {
4323 		/* for testing only restore the display */
4324 		ret = __intel_display_resume(dev, state, ctx);
4325 		if (ret)
4326 			DRM_ERROR("Restoring old state failed with %i\n", ret);
4327 	} else {
4328 		/*
4329 		 * The display has been reset as well,
4330 		 * so need a full re-initialization.
4331 		 */
4332 		intel_pps_unlock_regs_wa(dev_priv);
4333 		intel_modeset_init_hw(dev);
4334 		intel_init_clock_gating(dev_priv);
4335 
4336 		spin_lock_irq(&dev_priv->irq_lock);
4337 		if (dev_priv->display.hpd_irq_setup)
4338 			dev_priv->display.hpd_irq_setup(dev_priv);
4339 		spin_unlock_irq(&dev_priv->irq_lock);
4340 
4341 		ret = __intel_display_resume(dev, state, ctx);
4342 		if (ret)
4343 			DRM_ERROR("Restoring old state failed with %i\n", ret);
4344 
4345 		intel_hpd_init(dev_priv);
4346 	}
4347 
4348 	drm_atomic_state_put(state);
4349 unlock:
4350 	drm_modeset_drop_locks(ctx);
4351 	drm_modeset_acquire_fini(ctx);
4352 	mutex_unlock(&dev->mode_config.mutex);
4353 
4354 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4355 }
4356 
4357 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4358 {
4359 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4360 	enum pipe pipe = crtc->pipe;
4361 	u32 tmp;
4362 
4363 	tmp = I915_READ(PIPE_CHICKEN(pipe));
4364 
4365 	/*
4366 	 * Display WA #1153: icl
4367 	 * enable hardware to bypass the alpha math
4368 	 * and rounding for per-pixel values 00 and 0xff
4369 	 */
4370 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
4371 	/*
4372 	 * Display WA # 1605353570: icl
4373 	 * Set the pixel rounding bit to 1 for allowing
4374 	 * passthrough of Frame buffer pixels unmodified
4375 	 * across pipe
4376 	 */
4377 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
4378 	I915_WRITE(PIPE_CHICKEN(pipe), tmp);
4379 }
4380 
4381 static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
4382 				     const struct intel_crtc_state *new_crtc_state)
4383 {
4384 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
4385 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4386 
4387 	/* drm_atomic_helper_update_legacy_modeset_state might not be called. */
4388 	crtc->base.mode = new_crtc_state->base.mode;
4389 
4390 	/*
4391 	 * Update pipe size and adjust fitter if needed: the reason for this is
4392 	 * that in compute_mode_changes we check the native mode (not the pfit
4393 	 * mode) to see if we can flip rather than do a full mode set. In the
4394 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
4395 	 * pfit state, we'll end up with a big fb scanned out into the wrong
4396 	 * sized surface.
4397 	 */
4398 
4399 	I915_WRITE(PIPESRC(crtc->pipe),
4400 		   ((new_crtc_state->pipe_src_w - 1) << 16) |
4401 		   (new_crtc_state->pipe_src_h - 1));
4402 
4403 	/* on skylake this is done by detaching scalers */
4404 	if (INTEL_GEN(dev_priv) >= 9) {
4405 		skl_detach_scalers(new_crtc_state);
4406 
4407 		if (new_crtc_state->pch_pfit.enabled)
4408 			skylake_pfit_enable(new_crtc_state);
4409 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4410 		if (new_crtc_state->pch_pfit.enabled)
4411 			ironlake_pfit_enable(new_crtc_state);
4412 		else if (old_crtc_state->pch_pfit.enabled)
4413 			ironlake_pfit_disable(old_crtc_state);
4414 	}
4415 
4416 	if (INTEL_GEN(dev_priv) >= 11)
4417 		icl_set_pipe_chicken(crtc);
4418 }
4419 
4420 static void intel_fdi_normal_train(struct intel_crtc *crtc)
4421 {
4422 	struct drm_device *dev = crtc->base.dev;
4423 	struct drm_i915_private *dev_priv = to_i915(dev);
4424 	int pipe = crtc->pipe;
4425 	i915_reg_t reg;
4426 	u32 temp;
4427 
4428 	/* enable normal train */
4429 	reg = FDI_TX_CTL(pipe);
4430 	temp = I915_READ(reg);
4431 	if (IS_IVYBRIDGE(dev_priv)) {
4432 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4433 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
4434 	} else {
4435 		temp &= ~FDI_LINK_TRAIN_NONE;
4436 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
4437 	}
4438 	I915_WRITE(reg, temp);
4439 
4440 	reg = FDI_RX_CTL(pipe);
4441 	temp = I915_READ(reg);
4442 	if (HAS_PCH_CPT(dev_priv)) {
4443 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4444 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
4445 	} else {
4446 		temp &= ~FDI_LINK_TRAIN_NONE;
4447 		temp |= FDI_LINK_TRAIN_NONE;
4448 	}
4449 	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
4450 
4451 	/* wait one idle pattern time */
4452 	POSTING_READ(reg);
4453 	udelay(1000);
4454 
4455 	/* IVB wants error correction enabled */
4456 	if (IS_IVYBRIDGE(dev_priv))
4457 		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
4458 			   FDI_FE_ERRC_ENABLE);
4459 }
4460 
4461 /* The FDI link training functions for ILK/Ibexpeak. */
4462 static void ironlake_fdi_link_train(struct intel_crtc *crtc,
4463 				    const struct intel_crtc_state *crtc_state)
4464 {
4465 	struct drm_device *dev = crtc->base.dev;
4466 	struct drm_i915_private *dev_priv = to_i915(dev);
4467 	int pipe = crtc->pipe;
4468 	i915_reg_t reg;
4469 	u32 temp, tries;
4470 
4471 	/* FDI needs bits from pipe first */
4472 	assert_pipe_enabled(dev_priv, pipe);
4473 
4474 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4475 	   for train result */
4476 	reg = FDI_RX_IMR(pipe);
4477 	temp = I915_READ(reg);
4478 	temp &= ~FDI_RX_SYMBOL_LOCK;
4479 	temp &= ~FDI_RX_BIT_LOCK;
4480 	I915_WRITE(reg, temp);
4481 	I915_READ(reg);
4482 	udelay(150);
4483 
4484 	/* enable CPU FDI TX and PCH FDI RX */
4485 	reg = FDI_TX_CTL(pipe);
4486 	temp = I915_READ(reg);
4487 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
4488 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
4489 	temp &= ~FDI_LINK_TRAIN_NONE;
4490 	temp |= FDI_LINK_TRAIN_PATTERN_1;
4491 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
4492 
4493 	reg = FDI_RX_CTL(pipe);
4494 	temp = I915_READ(reg);
4495 	temp &= ~FDI_LINK_TRAIN_NONE;
4496 	temp |= FDI_LINK_TRAIN_PATTERN_1;
4497 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
4498 
4499 	POSTING_READ(reg);
4500 	udelay(150);
4501 
4502 	/* Ironlake workaround, enable clock pointer after FDI enable*/
4503 	I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
4504 	I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
4505 		   FDI_RX_PHASE_SYNC_POINTER_EN);
4506 
4507 	reg = FDI_RX_IIR(pipe);
4508 	for (tries = 0; tries < 5; tries++) {
4509 		temp = I915_READ(reg);
4510 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4511 
4512 		if ((temp & FDI_RX_BIT_LOCK)) {
4513 			DRM_DEBUG_KMS("FDI train 1 done.\n");
4514 			I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4515 			break;
4516 		}
4517 	}
4518 	if (tries == 5)
4519 		DRM_ERROR("FDI train 1 fail!\n");
4520 
4521 	/* Train 2 */
4522 	reg = FDI_TX_CTL(pipe);
4523 	temp = I915_READ(reg);
4524 	temp &= ~FDI_LINK_TRAIN_NONE;
4525 	temp |= FDI_LINK_TRAIN_PATTERN_2;
4526 	I915_WRITE(reg, temp);
4527 
4528 	reg = FDI_RX_CTL(pipe);
4529 	temp = I915_READ(reg);
4530 	temp &= ~FDI_LINK_TRAIN_NONE;
4531 	temp |= FDI_LINK_TRAIN_PATTERN_2;
4532 	I915_WRITE(reg, temp);
4533 
4534 	POSTING_READ(reg);
4535 	udelay(150);
4536 
4537 	reg = FDI_RX_IIR(pipe);
4538 	for (tries = 0; tries < 5; tries++) {
4539 		temp = I915_READ(reg);
4540 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4541 
4542 		if (temp & FDI_RX_SYMBOL_LOCK) {
4543 			I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4544 			DRM_DEBUG_KMS("FDI train 2 done.\n");
4545 			break;
4546 		}
4547 	}
4548 	if (tries == 5)
4549 		DRM_ERROR("FDI train 2 fail!\n");
4550 
4551 	DRM_DEBUG_KMS("FDI train done\n");
4552 
4553 }
4554 
4555 static const int snb_b_fdi_train_param[] = {
4556 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
4557 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
4558 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
4559 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
4560 };
4561 
4562 /* The FDI link training functions for SNB/Cougarpoint. */
4563 static void gen6_fdi_link_train(struct intel_crtc *crtc,
4564 				const struct intel_crtc_state *crtc_state)
4565 {
4566 	struct drm_device *dev = crtc->base.dev;
4567 	struct drm_i915_private *dev_priv = to_i915(dev);
4568 	int pipe = crtc->pipe;
4569 	i915_reg_t reg;
4570 	u32 temp, i, retry;
4571 
4572 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4573 	   for train result */
4574 	reg = FDI_RX_IMR(pipe);
4575 	temp = I915_READ(reg);
4576 	temp &= ~FDI_RX_SYMBOL_LOCK;
4577 	temp &= ~FDI_RX_BIT_LOCK;
4578 	I915_WRITE(reg, temp);
4579 
4580 	POSTING_READ(reg);
4581 	udelay(150);
4582 
4583 	/* enable CPU FDI TX and PCH FDI RX */
4584 	reg = FDI_TX_CTL(pipe);
4585 	temp = I915_READ(reg);
4586 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
4587 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
4588 	temp &= ~FDI_LINK_TRAIN_NONE;
4589 	temp |= FDI_LINK_TRAIN_PATTERN_1;
4590 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4591 	/* SNB-B */
4592 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4593 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
4594 
4595 	I915_WRITE(FDI_RX_MISC(pipe),
4596 		   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4597 
4598 	reg = FDI_RX_CTL(pipe);
4599 	temp = I915_READ(reg);
4600 	if (HAS_PCH_CPT(dev_priv)) {
4601 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4602 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4603 	} else {
4604 		temp &= ~FDI_LINK_TRAIN_NONE;
4605 		temp |= FDI_LINK_TRAIN_PATTERN_1;
4606 	}
4607 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
4608 
4609 	POSTING_READ(reg);
4610 	udelay(150);
4611 
4612 	for (i = 0; i < 4; i++) {
4613 		reg = FDI_TX_CTL(pipe);
4614 		temp = I915_READ(reg);
4615 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4616 		temp |= snb_b_fdi_train_param[i];
4617 		I915_WRITE(reg, temp);
4618 
4619 		POSTING_READ(reg);
4620 		udelay(500);
4621 
4622 		for (retry = 0; retry < 5; retry++) {
4623 			reg = FDI_RX_IIR(pipe);
4624 			temp = I915_READ(reg);
4625 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4626 			if (temp & FDI_RX_BIT_LOCK) {
4627 				I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4628 				DRM_DEBUG_KMS("FDI train 1 done.\n");
4629 				break;
4630 			}
4631 			udelay(50);
4632 		}
4633 		if (retry < 5)
4634 			break;
4635 	}
4636 	if (i == 4)
4637 		DRM_ERROR("FDI train 1 fail!\n");
4638 
4639 	/* Train 2 */
4640 	reg = FDI_TX_CTL(pipe);
4641 	temp = I915_READ(reg);
4642 	temp &= ~FDI_LINK_TRAIN_NONE;
4643 	temp |= FDI_LINK_TRAIN_PATTERN_2;
4644 	if (IS_GEN(dev_priv, 6)) {
4645 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4646 		/* SNB-B */
4647 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
4648 	}
4649 	I915_WRITE(reg, temp);
4650 
4651 	reg = FDI_RX_CTL(pipe);
4652 	temp = I915_READ(reg);
4653 	if (HAS_PCH_CPT(dev_priv)) {
4654 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4655 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4656 	} else {
4657 		temp &= ~FDI_LINK_TRAIN_NONE;
4658 		temp |= FDI_LINK_TRAIN_PATTERN_2;
4659 	}
4660 	I915_WRITE(reg, temp);
4661 
4662 	POSTING_READ(reg);
4663 	udelay(150);
4664 
4665 	for (i = 0; i < 4; i++) {
4666 		reg = FDI_TX_CTL(pipe);
4667 		temp = I915_READ(reg);
4668 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4669 		temp |= snb_b_fdi_train_param[i];
4670 		I915_WRITE(reg, temp);
4671 
4672 		POSTING_READ(reg);
4673 		udelay(500);
4674 
4675 		for (retry = 0; retry < 5; retry++) {
4676 			reg = FDI_RX_IIR(pipe);
4677 			temp = I915_READ(reg);
4678 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4679 			if (temp & FDI_RX_SYMBOL_LOCK) {
4680 				I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4681 				DRM_DEBUG_KMS("FDI train 2 done.\n");
4682 				break;
4683 			}
4684 			udelay(50);
4685 		}
4686 		if (retry < 5)
4687 			break;
4688 	}
4689 	if (i == 4)
4690 		DRM_ERROR("FDI train 2 fail!\n");
4691 
4692 	DRM_DEBUG_KMS("FDI train done.\n");
4693 }
4694 
4695 /* Manual link training for Ivy Bridge A0 parts */
4696 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
4697 				      const struct intel_crtc_state *crtc_state)
4698 {
4699 	struct drm_device *dev = crtc->base.dev;
4700 	struct drm_i915_private *dev_priv = to_i915(dev);
4701 	int pipe = crtc->pipe;
4702 	i915_reg_t reg;
4703 	u32 temp, i, j;
4704 
4705 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4706 	   for train result */
4707 	reg = FDI_RX_IMR(pipe);
4708 	temp = I915_READ(reg);
4709 	temp &= ~FDI_RX_SYMBOL_LOCK;
4710 	temp &= ~FDI_RX_BIT_LOCK;
4711 	I915_WRITE(reg, temp);
4712 
4713 	POSTING_READ(reg);
4714 	udelay(150);
4715 
4716 	DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4717 		      I915_READ(FDI_RX_IIR(pipe)));
4718 
4719 	/* Try each vswing and preemphasis setting twice before moving on */
4720 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4721 		/* disable first in case we need to retry */
4722 		reg = FDI_TX_CTL(pipe);
4723 		temp = I915_READ(reg);
4724 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4725 		temp &= ~FDI_TX_ENABLE;
4726 		I915_WRITE(reg, temp);
4727 
4728 		reg = FDI_RX_CTL(pipe);
4729 		temp = I915_READ(reg);
4730 		temp &= ~FDI_LINK_TRAIN_AUTO;
4731 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4732 		temp &= ~FDI_RX_ENABLE;
4733 		I915_WRITE(reg, temp);
4734 
4735 		/* enable CPU FDI TX and PCH FDI RX */
4736 		reg = FDI_TX_CTL(pipe);
4737 		temp = I915_READ(reg);
4738 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
4739 		temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
4740 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
4741 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
4742 		temp |= snb_b_fdi_train_param[j/2];
4743 		temp |= FDI_COMPOSITE_SYNC;
4744 		I915_WRITE(reg, temp | FDI_TX_ENABLE);
4745 
4746 		I915_WRITE(FDI_RX_MISC(pipe),
4747 			   FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
4748 
4749 		reg = FDI_RX_CTL(pipe);
4750 		temp = I915_READ(reg);
4751 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4752 		temp |= FDI_COMPOSITE_SYNC;
4753 		I915_WRITE(reg, temp | FDI_RX_ENABLE);
4754 
4755 		POSTING_READ(reg);
4756 		udelay(1); /* should be 0.5us */
4757 
4758 		for (i = 0; i < 4; i++) {
4759 			reg = FDI_RX_IIR(pipe);
4760 			temp = I915_READ(reg);
4761 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4762 
4763 			if (temp & FDI_RX_BIT_LOCK ||
4764 			    (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4765 				I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4766 				DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4767 					      i);
4768 				break;
4769 			}
4770 			udelay(1); /* should be 0.5us */
4771 		}
4772 		if (i == 4) {
4773 			DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4774 			continue;
4775 		}
4776 
4777 		/* Train 2 */
4778 		reg = FDI_TX_CTL(pipe);
4779 		temp = I915_READ(reg);
4780 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4781 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4782 		I915_WRITE(reg, temp);
4783 
4784 		reg = FDI_RX_CTL(pipe);
4785 		temp = I915_READ(reg);
4786 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4787 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
4788 		I915_WRITE(reg, temp);
4789 
4790 		POSTING_READ(reg);
4791 		udelay(2); /* should be 1.5us */
4792 
4793 		for (i = 0; i < 4; i++) {
4794 			reg = FDI_RX_IIR(pipe);
4795 			temp = I915_READ(reg);
4796 			DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
4797 
4798 			if (temp & FDI_RX_SYMBOL_LOCK ||
4799 			    (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4800 				I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4801 				DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4802 					      i);
4803 				goto train_done;
4804 			}
4805 			udelay(2); /* should be 1.5us */
4806 		}
4807 		if (i == 4)
4808 			DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
4809 	}
4810 
4811 train_done:
4812 	DRM_DEBUG_KMS("FDI train done.\n");
4813 }
4814 
4815 static void ironlake_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
4816 {
4817 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
4818 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
4819 	int pipe = intel_crtc->pipe;
4820 	i915_reg_t reg;
4821 	u32 temp;
4822 
4823 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
4824 	reg = FDI_RX_CTL(pipe);
4825 	temp = I915_READ(reg);
4826 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
4827 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
4828 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
4829 	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4830 
4831 	POSTING_READ(reg);
4832 	udelay(200);
4833 
4834 	/* Switch from Rawclk to PCDclk */
4835 	temp = I915_READ(reg);
4836 	I915_WRITE(reg, temp | FDI_PCDCLK);
4837 
4838 	POSTING_READ(reg);
4839 	udelay(200);
4840 
4841 	/* Enable CPU FDI TX PLL, always on for Ironlake */
4842 	reg = FDI_TX_CTL(pipe);
4843 	temp = I915_READ(reg);
4844 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4845 		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
4846 
4847 		POSTING_READ(reg);
4848 		udelay(100);
4849 	}
4850 }
4851 
4852 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4853 {
4854 	struct drm_device *dev = intel_crtc->base.dev;
4855 	struct drm_i915_private *dev_priv = to_i915(dev);
4856 	int pipe = intel_crtc->pipe;
4857 	i915_reg_t reg;
4858 	u32 temp;
4859 
4860 	/* Switch from PCDclk to Rawclk */
4861 	reg = FDI_RX_CTL(pipe);
4862 	temp = I915_READ(reg);
4863 	I915_WRITE(reg, temp & ~FDI_PCDCLK);
4864 
4865 	/* Disable CPU FDI TX PLL */
4866 	reg = FDI_TX_CTL(pipe);
4867 	temp = I915_READ(reg);
4868 	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4869 
4870 	POSTING_READ(reg);
4871 	udelay(100);
4872 
4873 	reg = FDI_RX_CTL(pipe);
4874 	temp = I915_READ(reg);
4875 	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4876 
4877 	/* Wait for the clocks to turn off. */
4878 	POSTING_READ(reg);
4879 	udelay(100);
4880 }
4881 
4882 static void ironlake_fdi_disable(struct drm_crtc *crtc)
4883 {
4884 	struct drm_device *dev = crtc->dev;
4885 	struct drm_i915_private *dev_priv = to_i915(dev);
4886 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4887 	int pipe = intel_crtc->pipe;
4888 	i915_reg_t reg;
4889 	u32 temp;
4890 
4891 	/* disable CPU FDI tx and PCH FDI rx */
4892 	reg = FDI_TX_CTL(pipe);
4893 	temp = I915_READ(reg);
4894 	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4895 	POSTING_READ(reg);
4896 
4897 	reg = FDI_RX_CTL(pipe);
4898 	temp = I915_READ(reg);
4899 	temp &= ~(0x7 << 16);
4900 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
4901 	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4902 
4903 	POSTING_READ(reg);
4904 	udelay(100);
4905 
4906 	/* Ironlake workaround, disable clock pointer after downing FDI */
4907 	if (HAS_PCH_IBX(dev_priv))
4908 		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
4909 
4910 	/* still set train pattern 1 */
4911 	reg = FDI_TX_CTL(pipe);
4912 	temp = I915_READ(reg);
4913 	temp &= ~FDI_LINK_TRAIN_NONE;
4914 	temp |= FDI_LINK_TRAIN_PATTERN_1;
4915 	I915_WRITE(reg, temp);
4916 
4917 	reg = FDI_RX_CTL(pipe);
4918 	temp = I915_READ(reg);
4919 	if (HAS_PCH_CPT(dev_priv)) {
4920 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4921 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4922 	} else {
4923 		temp &= ~FDI_LINK_TRAIN_NONE;
4924 		temp |= FDI_LINK_TRAIN_PATTERN_1;
4925 	}
4926 	/* BPC in FDI rx is consistent with that in PIPECONF */
4927 	temp &= ~(0x07 << 16);
4928 	temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
4929 	I915_WRITE(reg, temp);
4930 
4931 	POSTING_READ(reg);
4932 	udelay(100);
4933 }
4934 
4935 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
4936 {
4937 	struct drm_crtc *crtc;
4938 	bool cleanup_done;
4939 
4940 	drm_for_each_crtc(crtc, &dev_priv->drm) {
4941 		struct drm_crtc_commit *commit;
4942 		spin_lock(&crtc->commit_lock);
4943 		commit = list_first_entry_or_null(&crtc->commit_list,
4944 						  struct drm_crtc_commit, commit_entry);
4945 		cleanup_done = commit ?
4946 			try_wait_for_completion(&commit->cleanup_done) : true;
4947 		spin_unlock(&crtc->commit_lock);
4948 
4949 		if (cleanup_done)
4950 			continue;
4951 
4952 		drm_crtc_wait_one_vblank(crtc);
4953 
4954 		return true;
4955 	}
4956 
4957 	return false;
4958 }
4959 
4960 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
4961 {
4962 	u32 temp;
4963 
4964 	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4965 
4966 	mutex_lock(&dev_priv->sb_lock);
4967 
4968 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4969 	temp |= SBI_SSCCTL_DISABLE;
4970 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4971 
4972 	mutex_unlock(&dev_priv->sb_lock);
4973 }
4974 
4975 /* Program iCLKIP clock to the desired frequency */
4976 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
4977 {
4978 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
4979 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4980 	int clock = crtc_state->base.adjusted_mode.crtc_clock;
4981 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
4982 	u32 temp;
4983 
4984 	lpt_disable_iclkip(dev_priv);
4985 
4986 	/* The iCLK virtual clock root frequency is in MHz,
4987 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
4988 	 * divisors, it is necessary to divide one by another, so we
4989 	 * convert the virtual clock precision to KHz here for higher
4990 	 * precision.
4991 	 */
4992 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
4993 		u32 iclk_virtual_root_freq = 172800 * 1000;
4994 		u32 iclk_pi_range = 64;
4995 		u32 desired_divisor;
4996 
4997 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4998 						    clock << auxdiv);
4999 		divsel = (desired_divisor / iclk_pi_range) - 2;
5000 		phaseinc = desired_divisor % iclk_pi_range;
5001 
5002 		/*
5003 		 * Near 20MHz is a corner case which is
5004 		 * out of range for the 7-bit divisor
5005 		 */
5006 		if (divsel <= 0x7f)
5007 			break;
5008 	}
5009 
5010 	/* This should not happen with any sane values */
5011 	WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5012 		~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5013 	WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
5014 		~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5015 
5016 	DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5017 			clock,
5018 			auxdiv,
5019 			divsel,
5020 			phasedir,
5021 			phaseinc);
5022 
5023 	mutex_lock(&dev_priv->sb_lock);
5024 
5025 	/* Program SSCDIVINTPHASE6 */
5026 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5027 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5028 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5029 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5030 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5031 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5032 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5033 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5034 
5035 	/* Program SSCAUXDIV */
5036 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5037 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5038 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5039 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5040 
5041 	/* Enable modulator and associated divider */
5042 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5043 	temp &= ~SBI_SSCCTL_DISABLE;
5044 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5045 
5046 	mutex_unlock(&dev_priv->sb_lock);
5047 
5048 	/* Wait for initialization time */
5049 	udelay(24);
5050 
5051 	I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5052 }
5053 
5054 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5055 {
5056 	u32 divsel, phaseinc, auxdiv;
5057 	u32 iclk_virtual_root_freq = 172800 * 1000;
5058 	u32 iclk_pi_range = 64;
5059 	u32 desired_divisor;
5060 	u32 temp;
5061 
5062 	if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5063 		return 0;
5064 
5065 	mutex_lock(&dev_priv->sb_lock);
5066 
5067 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5068 	if (temp & SBI_SSCCTL_DISABLE) {
5069 		mutex_unlock(&dev_priv->sb_lock);
5070 		return 0;
5071 	}
5072 
5073 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5074 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5075 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5076 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5077 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5078 
5079 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5080 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5081 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5082 
5083 	mutex_unlock(&dev_priv->sb_lock);
5084 
5085 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5086 
5087 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5088 				 desired_divisor << auxdiv);
5089 }
5090 
5091 static void ironlake_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5092 						enum pipe pch_transcoder)
5093 {
5094 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5095 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5096 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5097 
5098 	I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
5099 		   I915_READ(HTOTAL(cpu_transcoder)));
5100 	I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
5101 		   I915_READ(HBLANK(cpu_transcoder)));
5102 	I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
5103 		   I915_READ(HSYNC(cpu_transcoder)));
5104 
5105 	I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
5106 		   I915_READ(VTOTAL(cpu_transcoder)));
5107 	I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
5108 		   I915_READ(VBLANK(cpu_transcoder)));
5109 	I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
5110 		   I915_READ(VSYNC(cpu_transcoder)));
5111 	I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5112 		   I915_READ(VSYNCSHIFT(cpu_transcoder)));
5113 }
5114 
5115 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5116 {
5117 	u32 temp;
5118 
5119 	temp = I915_READ(SOUTH_CHICKEN1);
5120 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5121 		return;
5122 
5123 	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5124 	WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5125 
5126 	temp &= ~FDI_BC_BIFURCATION_SELECT;
5127 	if (enable)
5128 		temp |= FDI_BC_BIFURCATION_SELECT;
5129 
5130 	DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
5131 	I915_WRITE(SOUTH_CHICKEN1, temp);
5132 	POSTING_READ(SOUTH_CHICKEN1);
5133 }
5134 
5135 static void ivybridge_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5136 {
5137 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5138 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5139 
5140 	switch (crtc->pipe) {
5141 	case PIPE_A:
5142 		break;
5143 	case PIPE_B:
5144 		if (crtc_state->fdi_lanes > 2)
5145 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
5146 		else
5147 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
5148 
5149 		break;
5150 	case PIPE_C:
5151 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
5152 
5153 		break;
5154 	default:
5155 		BUG();
5156 	}
5157 }
5158 
5159 /*
5160  * Finds the encoder associated with the given CRTC. This can only be
5161  * used when we know that the CRTC isn't feeding multiple encoders!
5162  */
5163 static struct intel_encoder *
5164 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5165 			   const struct intel_crtc_state *crtc_state)
5166 {
5167 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5168 	const struct drm_connector_state *connector_state;
5169 	const struct drm_connector *connector;
5170 	struct intel_encoder *encoder = NULL;
5171 	int num_encoders = 0;
5172 	int i;
5173 
5174 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5175 		if (connector_state->crtc != &crtc->base)
5176 			continue;
5177 
5178 		encoder = to_intel_encoder(connector_state->best_encoder);
5179 		num_encoders++;
5180 	}
5181 
5182 	WARN(num_encoders != 1, "%d encoders for pipe %c\n",
5183 	     num_encoders, pipe_name(crtc->pipe));
5184 
5185 	return encoder;
5186 }
5187 
5188 /*
5189  * Enable PCH resources required for PCH ports:
5190  *   - PCH PLLs
5191  *   - FDI training & RX/TX
5192  *   - update transcoder timings
5193  *   - DP transcoding bits
5194  *   - transcoder
5195  */
5196 static void ironlake_pch_enable(const struct intel_atomic_state *state,
5197 				const struct intel_crtc_state *crtc_state)
5198 {
5199 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5200 	struct drm_device *dev = crtc->base.dev;
5201 	struct drm_i915_private *dev_priv = to_i915(dev);
5202 	int pipe = crtc->pipe;
5203 	u32 temp;
5204 
5205 	assert_pch_transcoder_disabled(dev_priv, pipe);
5206 
5207 	if (IS_IVYBRIDGE(dev_priv))
5208 		ivybridge_update_fdi_bc_bifurcation(crtc_state);
5209 
5210 	/* Write the TU size bits before fdi link training, so that error
5211 	 * detection works. */
5212 	I915_WRITE(FDI_RX_TUSIZE1(pipe),
5213 		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5214 
5215 	/* For PCH output, training FDI link */
5216 	dev_priv->display.fdi_link_train(crtc, crtc_state);
5217 
5218 	/* We need to program the right clock selection before writing the pixel
5219 	 * mutliplier into the DPLL. */
5220 	if (HAS_PCH_CPT(dev_priv)) {
5221 		u32 sel;
5222 
5223 		temp = I915_READ(PCH_DPLL_SEL);
5224 		temp |= TRANS_DPLL_ENABLE(pipe);
5225 		sel = TRANS_DPLLB_SEL(pipe);
5226 		if (crtc_state->shared_dpll ==
5227 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5228 			temp |= sel;
5229 		else
5230 			temp &= ~sel;
5231 		I915_WRITE(PCH_DPLL_SEL, temp);
5232 	}
5233 
5234 	/* XXX: pch pll's can be enabled any time before we enable the PCH
5235 	 * transcoder, and we actually should do this to not upset any PCH
5236 	 * transcoder that already use the clock when we share it.
5237 	 *
5238 	 * Note that enable_shared_dpll tries to do the right thing, but
5239 	 * get_shared_dpll unconditionally resets the pll - we need that to have
5240 	 * the right LVDS enable sequence. */
5241 	intel_enable_shared_dpll(crtc_state);
5242 
5243 	/* set transcoder timing, panel must allow it */
5244 	assert_panel_unlocked(dev_priv, pipe);
5245 	ironlake_pch_transcoder_set_timings(crtc_state, pipe);
5246 
5247 	intel_fdi_normal_train(crtc);
5248 
5249 	/* For PCH DP, enable TRANS_DP_CTL */
5250 	if (HAS_PCH_CPT(dev_priv) &&
5251 	    intel_crtc_has_dp_encoder(crtc_state)) {
5252 		const struct drm_display_mode *adjusted_mode =
5253 			&crtc_state->base.adjusted_mode;
5254 		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5255 		i915_reg_t reg = TRANS_DP_CTL(pipe);
5256 		enum port port;
5257 
5258 		temp = I915_READ(reg);
5259 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
5260 			  TRANS_DP_SYNC_MASK |
5261 			  TRANS_DP_BPC_MASK);
5262 		temp |= TRANS_DP_OUTPUT_ENABLE;
5263 		temp |= bpc << 9; /* same format but at 11:9 */
5264 
5265 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5266 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5267 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5268 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5269 
5270 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5271 		WARN_ON(port < PORT_B || port > PORT_D);
5272 		temp |= TRANS_DP_PORT_SEL(port);
5273 
5274 		I915_WRITE(reg, temp);
5275 	}
5276 
5277 	ironlake_enable_pch_transcoder(crtc_state);
5278 }
5279 
5280 static void lpt_pch_enable(const struct intel_atomic_state *state,
5281 			   const struct intel_crtc_state *crtc_state)
5282 {
5283 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5284 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5285 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5286 
5287 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5288 
5289 	lpt_program_iclkip(crtc_state);
5290 
5291 	/* Set transcoder timing. */
5292 	ironlake_pch_transcoder_set_timings(crtc_state, PIPE_A);
5293 
5294 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5295 }
5296 
5297 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
5298 {
5299 	struct drm_i915_private *dev_priv = to_i915(dev);
5300 	i915_reg_t dslreg = PIPEDSL(pipe);
5301 	u32 temp;
5302 
5303 	temp = I915_READ(dslreg);
5304 	udelay(500);
5305 	if (wait_for(I915_READ(dslreg) != temp, 5)) {
5306 		if (wait_for(I915_READ(dslreg) != temp, 5))
5307 			DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
5308 	}
5309 }
5310 
5311 /*
5312  * The hardware phase 0.0 refers to the center of the pixel.
5313  * We want to start from the top/left edge which is phase
5314  * -0.5. That matches how the hardware calculates the scaling
5315  * factors (from top-left of the first pixel to bottom-right
5316  * of the last pixel, as opposed to the pixel centers).
5317  *
5318  * For 4:2:0 subsampled chroma planes we obviously have to
5319  * adjust that so that the chroma sample position lands in
5320  * the right spot.
5321  *
5322  * Note that for packed YCbCr 4:2:2 formats there is no way to
5323  * control chroma siting. The hardware simply replicates the
5324  * chroma samples for both of the luma samples, and thus we don't
5325  * actually get the expected MPEG2 chroma siting convention :(
5326  * The same behaviour is observed on pre-SKL platforms as well.
5327  *
5328  * Theory behind the formula (note that we ignore sub-pixel
5329  * source coordinates):
5330  * s = source sample position
5331  * d = destination sample position
5332  *
5333  * Downscaling 4:1:
5334  * -0.5
5335  * | 0.0
5336  * | |     1.5 (initial phase)
5337  * | |     |
5338  * v v     v
5339  * | s | s | s | s |
5340  * |       d       |
5341  *
5342  * Upscaling 1:4:
5343  * -0.5
5344  * | -0.375 (initial phase)
5345  * | |     0.0
5346  * | |     |
5347  * v v     v
5348  * |       s       |
5349  * | d | d | d | d |
5350  */
5351 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
5352 {
5353 	int phase = -0x8000;
5354 	u16 trip = 0;
5355 
5356 	if (chroma_cosited)
5357 		phase += (sub - 1) * 0x8000 / sub;
5358 
5359 	phase += scale / (2 * sub);
5360 
5361 	/*
5362 	 * Hardware initial phase limited to [-0.5:1.5].
5363 	 * Since the max hardware scale factor is 3.0, we
5364 	 * should never actually excdeed 1.0 here.
5365 	 */
5366 	WARN_ON(phase < -0x8000 || phase > 0x18000);
5367 
5368 	if (phase < 0)
5369 		phase = 0x10000 + phase;
5370 	else
5371 		trip = PS_PHASE_TRIP;
5372 
5373 	return ((phase >> 2) & PS_PHASE_MASK) | trip;
5374 }
5375 
5376 #define SKL_MIN_SRC_W 8
5377 #define SKL_MAX_SRC_W 4096
5378 #define SKL_MIN_SRC_H 8
5379 #define SKL_MAX_SRC_H 4096
5380 #define SKL_MIN_DST_W 8
5381 #define SKL_MAX_DST_W 4096
5382 #define SKL_MIN_DST_H 8
5383 #define SKL_MAX_DST_H 4096
5384 #define ICL_MAX_SRC_W 5120
5385 #define ICL_MAX_SRC_H 4096
5386 #define ICL_MAX_DST_W 5120
5387 #define ICL_MAX_DST_H 4096
5388 #define SKL_MIN_YUV_420_SRC_W 16
5389 #define SKL_MIN_YUV_420_SRC_H 16
5390 
5391 static int
5392 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
5393 		  unsigned int scaler_user, int *scaler_id,
5394 		  int src_w, int src_h, int dst_w, int dst_h,
5395 		  const struct drm_format_info *format, bool need_scaler)
5396 {
5397 	struct intel_crtc_scaler_state *scaler_state =
5398 		&crtc_state->scaler_state;
5399 	struct intel_crtc *intel_crtc =
5400 		to_intel_crtc(crtc_state->base.crtc);
5401 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5402 	const struct drm_display_mode *adjusted_mode =
5403 		&crtc_state->base.adjusted_mode;
5404 
5405 	/*
5406 	 * Src coordinates are already rotated by 270 degrees for
5407 	 * the 90/270 degree plane rotation cases (to match the
5408 	 * GTT mapping), hence no need to account for rotation here.
5409 	 */
5410 	if (src_w != dst_w || src_h != dst_h)
5411 		need_scaler = true;
5412 
5413 	/*
5414 	 * Scaling/fitting not supported in IF-ID mode in GEN9+
5415 	 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
5416 	 * Once NV12 is enabled, handle it here while allocating scaler
5417 	 * for NV12.
5418 	 */
5419 	if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
5420 	    need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5421 		DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
5422 		return -EINVAL;
5423 	}
5424 
5425 	/*
5426 	 * if plane is being disabled or scaler is no more required or force detach
5427 	 *  - free scaler binded to this plane/crtc
5428 	 *  - in order to do this, update crtc->scaler_usage
5429 	 *
5430 	 * Here scaler state in crtc_state is set free so that
5431 	 * scaler can be assigned to other user. Actual register
5432 	 * update to free the scaler is done in plane/panel-fit programming.
5433 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
5434 	 */
5435 	if (force_detach || !need_scaler) {
5436 		if (*scaler_id >= 0) {
5437 			scaler_state->scaler_users &= ~(1 << scaler_user);
5438 			scaler_state->scalers[*scaler_id].in_use = 0;
5439 
5440 			DRM_DEBUG_KMS("scaler_user index %u.%u: "
5441 				"Staged freeing scaler id %d scaler_users = 0x%x\n",
5442 				intel_crtc->pipe, scaler_user, *scaler_id,
5443 				scaler_state->scaler_users);
5444 			*scaler_id = -1;
5445 		}
5446 		return 0;
5447 	}
5448 
5449 	if (format && is_planar_yuv_format(format->format) &&
5450 	    (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
5451 		DRM_DEBUG_KMS("Planar YUV: src dimensions not met\n");
5452 		return -EINVAL;
5453 	}
5454 
5455 	/* range checks */
5456 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
5457 	    dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
5458 	    (INTEL_GEN(dev_priv) >= 11 &&
5459 	     (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
5460 	      dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
5461 	    (INTEL_GEN(dev_priv) < 11 &&
5462 	     (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
5463 	      dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H)))	{
5464 		DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
5465 			"size is out of scaler range\n",
5466 			intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
5467 		return -EINVAL;
5468 	}
5469 
5470 	/* mark this plane as a scaler user in crtc_state */
5471 	scaler_state->scaler_users |= (1 << scaler_user);
5472 	DRM_DEBUG_KMS("scaler_user index %u.%u: "
5473 		"staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
5474 		intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
5475 		scaler_state->scaler_users);
5476 
5477 	return 0;
5478 }
5479 
5480 /**
5481  * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
5482  *
5483  * @state: crtc's scaler state
5484  *
5485  * Return
5486  *     0 - scaler_usage updated successfully
5487  *    error - requested scaling cannot be supported or other error condition
5488  */
5489 int skl_update_scaler_crtc(struct intel_crtc_state *state)
5490 {
5491 	const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
5492 	bool need_scaler = false;
5493 
5494 	if (state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
5495 		need_scaler = true;
5496 
5497 	return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
5498 				 &state->scaler_state.scaler_id,
5499 				 state->pipe_src_w, state->pipe_src_h,
5500 				 adjusted_mode->crtc_hdisplay,
5501 				 adjusted_mode->crtc_vdisplay, NULL, need_scaler);
5502 }
5503 
5504 /**
5505  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
5506  * @crtc_state: crtc's scaler state
5507  * @plane_state: atomic plane state to update
5508  *
5509  * Return
5510  *     0 - scaler_usage updated successfully
5511  *    error - requested scaling cannot be supported or other error condition
5512  */
5513 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
5514 				   struct intel_plane_state *plane_state)
5515 {
5516 	struct intel_plane *intel_plane =
5517 		to_intel_plane(plane_state->base.plane);
5518 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
5519 	struct drm_framebuffer *fb = plane_state->base.fb;
5520 	int ret;
5521 	bool force_detach = !fb || !plane_state->base.visible;
5522 	bool need_scaler = false;
5523 
5524 	/* Pre-gen11 and SDR planes always need a scaler for planar formats. */
5525 	if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
5526 	    fb && is_planar_yuv_format(fb->format->format))
5527 		need_scaler = true;
5528 
5529 	ret = skl_update_scaler(crtc_state, force_detach,
5530 				drm_plane_index(&intel_plane->base),
5531 				&plane_state->scaler_id,
5532 				drm_rect_width(&plane_state->base.src) >> 16,
5533 				drm_rect_height(&plane_state->base.src) >> 16,
5534 				drm_rect_width(&plane_state->base.dst),
5535 				drm_rect_height(&plane_state->base.dst),
5536 				fb ? fb->format : NULL, need_scaler);
5537 
5538 	if (ret || plane_state->scaler_id < 0)
5539 		return ret;
5540 
5541 	/* check colorkey */
5542 	if (plane_state->ckey.flags) {
5543 		DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
5544 			      intel_plane->base.base.id,
5545 			      intel_plane->base.name);
5546 		return -EINVAL;
5547 	}
5548 
5549 	/* Check src format */
5550 	switch (fb->format->format) {
5551 	case DRM_FORMAT_RGB565:
5552 	case DRM_FORMAT_XBGR8888:
5553 	case DRM_FORMAT_XRGB8888:
5554 	case DRM_FORMAT_ABGR8888:
5555 	case DRM_FORMAT_ARGB8888:
5556 	case DRM_FORMAT_XRGB2101010:
5557 	case DRM_FORMAT_XBGR2101010:
5558 	case DRM_FORMAT_XBGR16161616F:
5559 	case DRM_FORMAT_ABGR16161616F:
5560 	case DRM_FORMAT_XRGB16161616F:
5561 	case DRM_FORMAT_ARGB16161616F:
5562 	case DRM_FORMAT_YUYV:
5563 	case DRM_FORMAT_YVYU:
5564 	case DRM_FORMAT_UYVY:
5565 	case DRM_FORMAT_VYUY:
5566 	case DRM_FORMAT_NV12:
5567 	case DRM_FORMAT_P010:
5568 	case DRM_FORMAT_P012:
5569 	case DRM_FORMAT_P016:
5570 	case DRM_FORMAT_Y210:
5571 	case DRM_FORMAT_Y212:
5572 	case DRM_FORMAT_Y216:
5573 	case DRM_FORMAT_XVYU2101010:
5574 	case DRM_FORMAT_XVYU12_16161616:
5575 	case DRM_FORMAT_XVYU16161616:
5576 		break;
5577 	default:
5578 		DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
5579 			      intel_plane->base.base.id, intel_plane->base.name,
5580 			      fb->base.id, fb->format->format);
5581 		return -EINVAL;
5582 	}
5583 
5584 	return 0;
5585 }
5586 
5587 static void skylake_scaler_disable(struct intel_crtc *crtc)
5588 {
5589 	int i;
5590 
5591 	for (i = 0; i < crtc->num_scalers; i++)
5592 		skl_detach_scaler(crtc, i);
5593 }
5594 
5595 static void skylake_pfit_enable(const struct intel_crtc_state *crtc_state)
5596 {
5597 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5598 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5599 	enum pipe pipe = crtc->pipe;
5600 	const struct intel_crtc_scaler_state *scaler_state =
5601 		&crtc_state->scaler_state;
5602 
5603 	if (crtc_state->pch_pfit.enabled) {
5604 		u16 uv_rgb_hphase, uv_rgb_vphase;
5605 		int pfit_w, pfit_h, hscale, vscale;
5606 		int id;
5607 
5608 		if (WARN_ON(crtc_state->scaler_state.scaler_id < 0))
5609 			return;
5610 
5611 		pfit_w = (crtc_state->pch_pfit.size >> 16) & 0xFFFF;
5612 		pfit_h = crtc_state->pch_pfit.size & 0xFFFF;
5613 
5614 		hscale = (crtc_state->pipe_src_w << 16) / pfit_w;
5615 		vscale = (crtc_state->pipe_src_h << 16) / pfit_h;
5616 
5617 		uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
5618 		uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
5619 
5620 		id = scaler_state->scaler_id;
5621 		I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
5622 			PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
5623 		I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
5624 			      PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
5625 		I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
5626 			      PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
5627 		I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc_state->pch_pfit.pos);
5628 		I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc_state->pch_pfit.size);
5629 	}
5630 }
5631 
5632 static void ironlake_pfit_enable(const struct intel_crtc_state *crtc_state)
5633 {
5634 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5635 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5636 	int pipe = crtc->pipe;
5637 
5638 	if (crtc_state->pch_pfit.enabled) {
5639 		/* Force use of hard-coded filter coefficients
5640 		 * as some pre-programmed values are broken,
5641 		 * e.g. x201.
5642 		 */
5643 		if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
5644 			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
5645 						 PF_PIPE_SEL_IVB(pipe));
5646 		else
5647 			I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
5648 		I915_WRITE(PF_WIN_POS(pipe), crtc_state->pch_pfit.pos);
5649 		I915_WRITE(PF_WIN_SZ(pipe), crtc_state->pch_pfit.size);
5650 	}
5651 }
5652 
5653 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
5654 {
5655 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5656 	struct drm_device *dev = crtc->base.dev;
5657 	struct drm_i915_private *dev_priv = to_i915(dev);
5658 
5659 	if (!crtc_state->ips_enabled)
5660 		return;
5661 
5662 	/*
5663 	 * We can only enable IPS after we enable a plane and wait for a vblank
5664 	 * This function is called from post_plane_update, which is run after
5665 	 * a vblank wait.
5666 	 */
5667 	WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
5668 
5669 	if (IS_BROADWELL(dev_priv)) {
5670 		WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
5671 						IPS_ENABLE | IPS_PCODE_CONTROL));
5672 		/* Quoting Art Runyan: "its not safe to expect any particular
5673 		 * value in IPS_CTL bit 31 after enabling IPS through the
5674 		 * mailbox." Moreover, the mailbox may return a bogus state,
5675 		 * so we need to just enable it and continue on.
5676 		 */
5677 	} else {
5678 		I915_WRITE(IPS_CTL, IPS_ENABLE);
5679 		/* The bit only becomes 1 in the next vblank, so this wait here
5680 		 * is essentially intel_wait_for_vblank. If we don't have this
5681 		 * and don't wait for vblanks until the end of crtc_enable, then
5682 		 * the HW state readout code will complain that the expected
5683 		 * IPS_CTL value is not the one we read. */
5684 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
5685 			DRM_ERROR("Timed out waiting for IPS enable\n");
5686 	}
5687 }
5688 
5689 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
5690 {
5691 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
5692 	struct drm_device *dev = crtc->base.dev;
5693 	struct drm_i915_private *dev_priv = to_i915(dev);
5694 
5695 	if (!crtc_state->ips_enabled)
5696 		return;
5697 
5698 	if (IS_BROADWELL(dev_priv)) {
5699 		WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
5700 		/*
5701 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
5702 		 * 42ms timeout value leads to occasional timeouts so use 100ms
5703 		 * instead.
5704 		 */
5705 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
5706 			DRM_ERROR("Timed out waiting for IPS disable\n");
5707 	} else {
5708 		I915_WRITE(IPS_CTL, 0);
5709 		POSTING_READ(IPS_CTL);
5710 	}
5711 
5712 	/* We need to wait for a vblank before we can disable the plane. */
5713 	intel_wait_for_vblank(dev_priv, crtc->pipe);
5714 }
5715 
5716 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
5717 {
5718 	if (intel_crtc->overlay) {
5719 		struct drm_device *dev = intel_crtc->base.dev;
5720 
5721 		mutex_lock(&dev->struct_mutex);
5722 		(void) intel_overlay_switch_off(intel_crtc->overlay);
5723 		mutex_unlock(&dev->struct_mutex);
5724 	}
5725 
5726 	/* Let userspace switch the overlay on again. In most cases userspace
5727 	 * has to recompute where to put it anyway.
5728 	 */
5729 }
5730 
5731 /**
5732  * intel_post_enable_primary - Perform operations after enabling primary plane
5733  * @crtc: the CRTC whose primary plane was just enabled
5734  * @new_crtc_state: the enabling state
5735  *
5736  * Performs potentially sleeping operations that must be done after the primary
5737  * plane is enabled, such as updating FBC and IPS.  Note that this may be
5738  * called due to an explicit primary plane update, or due to an implicit
5739  * re-enable that is caused when a sprite plane is updated to no longer
5740  * completely hide the primary plane.
5741  */
5742 static void
5743 intel_post_enable_primary(struct drm_crtc *crtc,
5744 			  const struct intel_crtc_state *new_crtc_state)
5745 {
5746 	struct drm_device *dev = crtc->dev;
5747 	struct drm_i915_private *dev_priv = to_i915(dev);
5748 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5749 	int pipe = intel_crtc->pipe;
5750 
5751 	/*
5752 	 * Gen2 reports pipe underruns whenever all planes are disabled.
5753 	 * So don't enable underrun reporting before at least some planes
5754 	 * are enabled.
5755 	 * FIXME: Need to fix the logic to work when we turn off all planes
5756 	 * but leave the pipe running.
5757 	 */
5758 	if (IS_GEN(dev_priv, 2))
5759 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5760 
5761 	/* Underruns don't always raise interrupts, so check manually. */
5762 	intel_check_cpu_fifo_underruns(dev_priv);
5763 	intel_check_pch_fifo_underruns(dev_priv);
5764 }
5765 
5766 /* FIXME get rid of this and use pre_plane_update */
5767 static void
5768 intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5769 {
5770 	struct drm_device *dev = crtc->dev;
5771 	struct drm_i915_private *dev_priv = to_i915(dev);
5772 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5773 	int pipe = intel_crtc->pipe;
5774 
5775 	/*
5776 	 * Gen2 reports pipe underruns whenever all planes are disabled.
5777 	 * So disable underrun reporting before all the planes get disabled.
5778 	 */
5779 	if (IS_GEN(dev_priv, 2))
5780 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5781 
5782 	hsw_disable_ips(to_intel_crtc_state(crtc->state));
5783 
5784 	/*
5785 	 * Vblank time updates from the shadow to live plane control register
5786 	 * are blocked if the memory self-refresh mode is active at that
5787 	 * moment. So to make sure the plane gets truly disabled, disable
5788 	 * first the self-refresh mode. The self-refresh enable bit in turn
5789 	 * will be checked/applied by the HW only at the next frame start
5790 	 * event which is after the vblank start event, so we need to have a
5791 	 * wait-for-vblank between disabling the plane and the pipe.
5792 	 */
5793 	if (HAS_GMCH(dev_priv) &&
5794 	    intel_set_memory_cxsr(dev_priv, false))
5795 		intel_wait_for_vblank(dev_priv, pipe);
5796 }
5797 
5798 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
5799 				       const struct intel_crtc_state *new_crtc_state)
5800 {
5801 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5802 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5803 
5804 	if (!old_crtc_state->ips_enabled)
5805 		return false;
5806 
5807 	if (needs_modeset(new_crtc_state))
5808 		return true;
5809 
5810 	/*
5811 	 * Workaround : Do not read or write the pipe palette/gamma data while
5812 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
5813 	 *
5814 	 * Disable IPS before we program the LUT.
5815 	 */
5816 	if (IS_HASWELL(dev_priv) &&
5817 	    (new_crtc_state->base.color_mgmt_changed ||
5818 	     new_crtc_state->update_pipe) &&
5819 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
5820 		return true;
5821 
5822 	return !new_crtc_state->ips_enabled;
5823 }
5824 
5825 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
5826 				       const struct intel_crtc_state *new_crtc_state)
5827 {
5828 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
5829 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5830 
5831 	if (!new_crtc_state->ips_enabled)
5832 		return false;
5833 
5834 	if (needs_modeset(new_crtc_state))
5835 		return true;
5836 
5837 	/*
5838 	 * Workaround : Do not read or write the pipe palette/gamma data while
5839 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
5840 	 *
5841 	 * Re-enable IPS after the LUT has been programmed.
5842 	 */
5843 	if (IS_HASWELL(dev_priv) &&
5844 	    (new_crtc_state->base.color_mgmt_changed ||
5845 	     new_crtc_state->update_pipe) &&
5846 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
5847 		return true;
5848 
5849 	/*
5850 	 * We can't read out IPS on broadwell, assume the worst and
5851 	 * forcibly enable IPS on the first fastset.
5852 	 */
5853 	if (new_crtc_state->update_pipe &&
5854 	    old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
5855 		return true;
5856 
5857 	return !old_crtc_state->ips_enabled;
5858 }
5859 
5860 static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
5861 			  const struct intel_crtc_state *crtc_state)
5862 {
5863 	if (!crtc_state->nv12_planes)
5864 		return false;
5865 
5866 	/* WA Display #0827: Gen9:all */
5867 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
5868 		return true;
5869 
5870 	return false;
5871 }
5872 
5873 static bool needs_scalerclk_wa(struct drm_i915_private *dev_priv,
5874 			       const struct intel_crtc_state *crtc_state)
5875 {
5876 	/* Wa_2006604312:icl */
5877 	if (crtc_state->scaler_state.scaler_users > 0 && IS_ICELAKE(dev_priv))
5878 		return true;
5879 
5880 	return false;
5881 }
5882 
5883 static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5884 {
5885 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5886 	struct drm_device *dev = crtc->base.dev;
5887 	struct drm_i915_private *dev_priv = to_i915(dev);
5888 	struct drm_atomic_state *state = old_crtc_state->base.state;
5889 	struct intel_crtc_state *pipe_config =
5890 		intel_atomic_get_new_crtc_state(to_intel_atomic_state(state),
5891 						crtc);
5892 	struct drm_plane *primary = crtc->base.primary;
5893 	struct drm_plane_state *old_primary_state =
5894 		drm_atomic_get_old_plane_state(state, primary);
5895 
5896 	intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5897 
5898 	if (pipe_config->update_wm_post && pipe_config->base.active)
5899 		intel_update_watermarks(crtc);
5900 
5901 	if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
5902 		hsw_enable_ips(pipe_config);
5903 
5904 	if (old_primary_state) {
5905 		struct drm_plane_state *new_primary_state =
5906 			drm_atomic_get_new_plane_state(state, primary);
5907 
5908 		intel_fbc_post_update(crtc);
5909 
5910 		if (new_primary_state->visible &&
5911 		    (needs_modeset(pipe_config) ||
5912 		     !old_primary_state->visible))
5913 			intel_post_enable_primary(&crtc->base, pipe_config);
5914 	}
5915 
5916 	if (needs_nv12_wa(dev_priv, old_crtc_state) &&
5917 	    !needs_nv12_wa(dev_priv, pipe_config))
5918 		skl_wa_827(dev_priv, crtc->pipe, false);
5919 
5920 	if (needs_scalerclk_wa(dev_priv, old_crtc_state) &&
5921 	    !needs_scalerclk_wa(dev_priv, pipe_config))
5922 		icl_wa_scalerclkgating(dev_priv, crtc->pipe, false);
5923 }
5924 
5925 static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
5926 				   struct intel_crtc_state *pipe_config)
5927 {
5928 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5929 	struct drm_device *dev = crtc->base.dev;
5930 	struct drm_i915_private *dev_priv = to_i915(dev);
5931 	struct drm_atomic_state *state = old_crtc_state->base.state;
5932 	struct drm_plane *primary = crtc->base.primary;
5933 	struct drm_plane_state *old_primary_state =
5934 		drm_atomic_get_old_plane_state(state, primary);
5935 	bool modeset = needs_modeset(pipe_config);
5936 	struct intel_atomic_state *intel_state =
5937 		to_intel_atomic_state(state);
5938 
5939 	if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
5940 		hsw_disable_ips(old_crtc_state);
5941 
5942 	if (old_primary_state) {
5943 		struct intel_plane_state *new_primary_state =
5944 			intel_atomic_get_new_plane_state(intel_state,
5945 							 to_intel_plane(primary));
5946 
5947 		intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
5948 		/*
5949 		 * Gen2 reports pipe underruns whenever all planes are disabled.
5950 		 * So disable underrun reporting before all the planes get disabled.
5951 		 */
5952 		if (IS_GEN(dev_priv, 2) && old_primary_state->visible &&
5953 		    (modeset || !new_primary_state->base.visible))
5954 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
5955 	}
5956 
5957 	/* Display WA 827 */
5958 	if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
5959 	    needs_nv12_wa(dev_priv, pipe_config))
5960 		skl_wa_827(dev_priv, crtc->pipe, true);
5961 
5962 	/* Wa_2006604312:icl */
5963 	if (!needs_scalerclk_wa(dev_priv, old_crtc_state) &&
5964 	    needs_scalerclk_wa(dev_priv, pipe_config))
5965 		icl_wa_scalerclkgating(dev_priv, crtc->pipe, true);
5966 
5967 	/*
5968 	 * Vblank time updates from the shadow to live plane control register
5969 	 * are blocked if the memory self-refresh mode is active at that
5970 	 * moment. So to make sure the plane gets truly disabled, disable
5971 	 * first the self-refresh mode. The self-refresh enable bit in turn
5972 	 * will be checked/applied by the HW only at the next frame start
5973 	 * event which is after the vblank start event, so we need to have a
5974 	 * wait-for-vblank between disabling the plane and the pipe.
5975 	 */
5976 	if (HAS_GMCH(dev_priv) && old_crtc_state->base.active &&
5977 	    pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
5978 		intel_wait_for_vblank(dev_priv, crtc->pipe);
5979 
5980 	/*
5981 	 * IVB workaround: must disable low power watermarks for at least
5982 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
5983 	 * when scaling is disabled.
5984 	 *
5985 	 * WaCxSRDisabledForSpriteScaling:ivb
5986 	 */
5987 	if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev) &&
5988 	    old_crtc_state->base.active)
5989 		intel_wait_for_vblank(dev_priv, crtc->pipe);
5990 
5991 	/*
5992 	 * If we're doing a modeset, we're done.  No need to do any pre-vblank
5993 	 * watermark programming here.
5994 	 */
5995 	if (needs_modeset(pipe_config))
5996 		return;
5997 
5998 	/*
5999 	 * For platforms that support atomic watermarks, program the
6000 	 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6001 	 * will be the intermediate values that are safe for both pre- and
6002 	 * post- vblank; when vblank happens, the 'active' values will be set
6003 	 * to the final 'target' values and we'll do this again to get the
6004 	 * optimal watermarks.  For gen9+ platforms, the values we program here
6005 	 * will be the final target values which will get automatically latched
6006 	 * at vblank time; no further programming will be necessary.
6007 	 *
6008 	 * If a platform hasn't been transitioned to atomic watermarks yet,
6009 	 * we'll continue to update watermarks the old way, if flags tell
6010 	 * us to.
6011 	 */
6012 	if (dev_priv->display.initial_watermarks != NULL)
6013 		dev_priv->display.initial_watermarks(intel_state,
6014 						     pipe_config);
6015 	else if (pipe_config->update_wm_pre)
6016 		intel_update_watermarks(crtc);
6017 }
6018 
6019 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6020 				      struct intel_crtc *crtc)
6021 {
6022 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6023 	const struct intel_crtc_state *new_crtc_state =
6024 		intel_atomic_get_new_crtc_state(state, crtc);
6025 	unsigned int update_mask = new_crtc_state->update_planes;
6026 	const struct intel_plane_state *old_plane_state;
6027 	struct intel_plane *plane;
6028 	unsigned fb_bits = 0;
6029 	int i;
6030 
6031 	intel_crtc_dpms_overlay_disable(crtc);
6032 
6033 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6034 		if (crtc->pipe != plane->pipe ||
6035 		    !(update_mask & BIT(plane->id)))
6036 			continue;
6037 
6038 		intel_disable_plane(plane, new_crtc_state);
6039 
6040 		if (old_plane_state->base.visible)
6041 			fb_bits |= plane->frontbuffer_bit;
6042 	}
6043 
6044 	intel_frontbuffer_flip(dev_priv, fb_bits);
6045 }
6046 
6047 /*
6048  * intel_connector_primary_encoder - get the primary encoder for a connector
6049  * @connector: connector for which to return the encoder
6050  *
6051  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6052  * all connectors to their encoder, except for DP-MST connectors which have
6053  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6054  * pointed to by as many DP-MST connectors as there are pipes.
6055  */
6056 static struct intel_encoder *
6057 intel_connector_primary_encoder(struct intel_connector *connector)
6058 {
6059 	struct intel_encoder *encoder;
6060 
6061 	if (connector->mst_port)
6062 		return &dp_to_dig_port(connector->mst_port)->base;
6063 
6064 	encoder = intel_attached_encoder(&connector->base);
6065 	WARN_ON(!encoder);
6066 
6067 	return encoder;
6068 }
6069 
6070 static bool
6071 intel_connector_needs_modeset(struct intel_atomic_state *state,
6072 			      const struct drm_connector_state *old_conn_state,
6073 			      const struct drm_connector_state *new_conn_state)
6074 {
6075 	struct intel_crtc *old_crtc = old_conn_state->crtc ?
6076 				      to_intel_crtc(old_conn_state->crtc) : NULL;
6077 	struct intel_crtc *new_crtc = new_conn_state->crtc ?
6078 				      to_intel_crtc(new_conn_state->crtc) : NULL;
6079 
6080 	return new_crtc != old_crtc ||
6081 	       (new_crtc &&
6082 		needs_modeset(intel_atomic_get_new_crtc_state(state, new_crtc)));
6083 }
6084 
6085 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6086 {
6087 	struct drm_connector_state *old_conn_state;
6088 	struct drm_connector_state *new_conn_state;
6089 	struct drm_connector *conn;
6090 	int i;
6091 
6092 	for_each_oldnew_connector_in_state(&state->base, conn,
6093 					   old_conn_state, new_conn_state, i) {
6094 		struct intel_encoder *encoder;
6095 		struct intel_crtc *crtc;
6096 
6097 		if (!intel_connector_needs_modeset(state,
6098 						   old_conn_state,
6099 						   new_conn_state))
6100 			continue;
6101 
6102 		encoder = intel_connector_primary_encoder(to_intel_connector(conn));
6103 		if (!encoder->update_prepare)
6104 			continue;
6105 
6106 		crtc = new_conn_state->crtc ?
6107 			to_intel_crtc(new_conn_state->crtc) : NULL;
6108 		encoder->update_prepare(state, encoder, crtc);
6109 	}
6110 }
6111 
6112 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6113 {
6114 	struct drm_connector_state *old_conn_state;
6115 	struct drm_connector_state *new_conn_state;
6116 	struct drm_connector *conn;
6117 	int i;
6118 
6119 	for_each_oldnew_connector_in_state(&state->base, conn,
6120 					   old_conn_state, new_conn_state, i) {
6121 		struct intel_encoder *encoder;
6122 		struct intel_crtc *crtc;
6123 
6124 		if (!intel_connector_needs_modeset(state,
6125 						   old_conn_state,
6126 						   new_conn_state))
6127 			continue;
6128 
6129 		encoder = intel_connector_primary_encoder(to_intel_connector(conn));
6130 		if (!encoder->update_complete)
6131 			continue;
6132 
6133 		crtc = new_conn_state->crtc ?
6134 			to_intel_crtc(new_conn_state->crtc) : NULL;
6135 		encoder->update_complete(state, encoder, crtc);
6136 	}
6137 }
6138 
6139 static void intel_encoders_pre_pll_enable(struct intel_crtc *crtc,
6140 					  struct intel_crtc_state *crtc_state,
6141 					  struct intel_atomic_state *state)
6142 {
6143 	struct drm_connector_state *conn_state;
6144 	struct drm_connector *conn;
6145 	int i;
6146 
6147 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6148 		struct intel_encoder *encoder =
6149 			to_intel_encoder(conn_state->best_encoder);
6150 
6151 		if (conn_state->crtc != &crtc->base)
6152 			continue;
6153 
6154 		if (encoder->pre_pll_enable)
6155 			encoder->pre_pll_enable(encoder, crtc_state, conn_state);
6156 	}
6157 }
6158 
6159 static void intel_encoders_pre_enable(struct intel_crtc *crtc,
6160 				      struct intel_crtc_state *crtc_state,
6161 				      struct intel_atomic_state *state)
6162 {
6163 	struct drm_connector_state *conn_state;
6164 	struct drm_connector *conn;
6165 	int i;
6166 
6167 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6168 		struct intel_encoder *encoder =
6169 			to_intel_encoder(conn_state->best_encoder);
6170 
6171 		if (conn_state->crtc != &crtc->base)
6172 			continue;
6173 
6174 		if (encoder->pre_enable)
6175 			encoder->pre_enable(encoder, crtc_state, conn_state);
6176 	}
6177 }
6178 
6179 static void intel_encoders_enable(struct intel_crtc *crtc,
6180 				  struct intel_crtc_state *crtc_state,
6181 				  struct intel_atomic_state *state)
6182 {
6183 	struct drm_connector_state *conn_state;
6184 	struct drm_connector *conn;
6185 	int i;
6186 
6187 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6188 		struct intel_encoder *encoder =
6189 			to_intel_encoder(conn_state->best_encoder);
6190 
6191 		if (conn_state->crtc != &crtc->base)
6192 			continue;
6193 
6194 		if (encoder->enable)
6195 			encoder->enable(encoder, crtc_state, conn_state);
6196 		intel_opregion_notify_encoder(encoder, true);
6197 	}
6198 }
6199 
6200 static void intel_encoders_disable(struct intel_crtc *crtc,
6201 				   struct intel_crtc_state *old_crtc_state,
6202 				   struct intel_atomic_state *state)
6203 {
6204 	struct drm_connector_state *old_conn_state;
6205 	struct drm_connector *conn;
6206 	int i;
6207 
6208 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6209 		struct intel_encoder *encoder =
6210 			to_intel_encoder(old_conn_state->best_encoder);
6211 
6212 		if (old_conn_state->crtc != &crtc->base)
6213 			continue;
6214 
6215 		intel_opregion_notify_encoder(encoder, false);
6216 		if (encoder->disable)
6217 			encoder->disable(encoder, old_crtc_state, old_conn_state);
6218 	}
6219 }
6220 
6221 static void intel_encoders_post_disable(struct intel_crtc *crtc,
6222 					struct intel_crtc_state *old_crtc_state,
6223 					struct intel_atomic_state *state)
6224 {
6225 	struct drm_connector_state *old_conn_state;
6226 	struct drm_connector *conn;
6227 	int i;
6228 
6229 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6230 		struct intel_encoder *encoder =
6231 			to_intel_encoder(old_conn_state->best_encoder);
6232 
6233 		if (old_conn_state->crtc != &crtc->base)
6234 			continue;
6235 
6236 		if (encoder->post_disable)
6237 			encoder->post_disable(encoder, old_crtc_state, old_conn_state);
6238 	}
6239 }
6240 
6241 static void intel_encoders_post_pll_disable(struct intel_crtc *crtc,
6242 					    struct intel_crtc_state *old_crtc_state,
6243 					    struct intel_atomic_state *state)
6244 {
6245 	struct drm_connector_state *old_conn_state;
6246 	struct drm_connector *conn;
6247 	int i;
6248 
6249 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6250 		struct intel_encoder *encoder =
6251 			to_intel_encoder(old_conn_state->best_encoder);
6252 
6253 		if (old_conn_state->crtc != &crtc->base)
6254 			continue;
6255 
6256 		if (encoder->post_pll_disable)
6257 			encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
6258 	}
6259 }
6260 
6261 static void intel_encoders_update_pipe(struct intel_crtc *crtc,
6262 				       struct intel_crtc_state *crtc_state,
6263 				       struct intel_atomic_state *state)
6264 {
6265 	struct drm_connector_state *conn_state;
6266 	struct drm_connector *conn;
6267 	int i;
6268 
6269 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6270 		struct intel_encoder *encoder =
6271 			to_intel_encoder(conn_state->best_encoder);
6272 
6273 		if (conn_state->crtc != &crtc->base)
6274 			continue;
6275 
6276 		if (encoder->update_pipe)
6277 			encoder->update_pipe(encoder, crtc_state, conn_state);
6278 	}
6279 }
6280 
6281 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6282 {
6283 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6284 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6285 
6286 	plane->disable_plane(plane, crtc_state);
6287 }
6288 
6289 static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
6290 				 struct intel_atomic_state *state)
6291 {
6292 	struct drm_crtc *crtc = pipe_config->base.crtc;
6293 	struct drm_device *dev = crtc->dev;
6294 	struct drm_i915_private *dev_priv = to_i915(dev);
6295 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6296 	int pipe = intel_crtc->pipe;
6297 
6298 	if (WARN_ON(intel_crtc->active))
6299 		return;
6300 
6301 	/*
6302 	 * Sometimes spurious CPU pipe underruns happen during FDI
6303 	 * training, at least with VGA+HDMI cloning. Suppress them.
6304 	 *
6305 	 * On ILK we get an occasional spurious CPU pipe underruns
6306 	 * between eDP port A enable and vdd enable. Also PCH port
6307 	 * enable seems to result in the occasional CPU pipe underrun.
6308 	 *
6309 	 * Spurious PCH underruns also occur during PCH enabling.
6310 	 */
6311 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6312 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
6313 
6314 	if (pipe_config->has_pch_encoder)
6315 		intel_prepare_shared_dpll(pipe_config);
6316 
6317 	if (intel_crtc_has_dp_encoder(pipe_config))
6318 		intel_dp_set_m_n(pipe_config, M1_N1);
6319 
6320 	intel_set_pipe_timings(pipe_config);
6321 	intel_set_pipe_src_size(pipe_config);
6322 
6323 	if (pipe_config->has_pch_encoder) {
6324 		intel_cpu_transcoder_set_m_n(pipe_config,
6325 					     &pipe_config->fdi_m_n, NULL);
6326 	}
6327 
6328 	ironlake_set_pipeconf(pipe_config);
6329 
6330 	intel_crtc->active = true;
6331 
6332 	intel_encoders_pre_enable(intel_crtc, pipe_config, state);
6333 
6334 	if (pipe_config->has_pch_encoder) {
6335 		/* Note: FDI PLL enabling _must_ be done before we enable the
6336 		 * cpu pipes, hence this is separate from all the other fdi/pch
6337 		 * enabling. */
6338 		ironlake_fdi_pll_enable(pipe_config);
6339 	} else {
6340 		assert_fdi_tx_disabled(dev_priv, pipe);
6341 		assert_fdi_rx_disabled(dev_priv, pipe);
6342 	}
6343 
6344 	ironlake_pfit_enable(pipe_config);
6345 
6346 	/*
6347 	 * On ILK+ LUT must be loaded before the pipe is running but with
6348 	 * clocks enabled
6349 	 */
6350 	intel_color_load_luts(pipe_config);
6351 	intel_color_commit(pipe_config);
6352 	/* update DSPCNTR to configure gamma for pipe bottom color */
6353 	intel_disable_primary_plane(pipe_config);
6354 
6355 	if (dev_priv->display.initial_watermarks != NULL)
6356 		dev_priv->display.initial_watermarks(state, pipe_config);
6357 	intel_enable_pipe(pipe_config);
6358 
6359 	if (pipe_config->has_pch_encoder)
6360 		ironlake_pch_enable(state, pipe_config);
6361 
6362 	assert_vblank_disabled(crtc);
6363 	intel_crtc_vblank_on(pipe_config);
6364 
6365 	intel_encoders_enable(intel_crtc, pipe_config, state);
6366 
6367 	if (HAS_PCH_CPT(dev_priv))
6368 		cpt_verify_modeset(dev, intel_crtc->pipe);
6369 
6370 	/*
6371 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
6372 	 * And a second vblank wait is needed at least on ILK with
6373 	 * some interlaced HDMI modes. Let's do the double wait always
6374 	 * in case there are more corner cases we don't know about.
6375 	 */
6376 	if (pipe_config->has_pch_encoder) {
6377 		intel_wait_for_vblank(dev_priv, pipe);
6378 		intel_wait_for_vblank(dev_priv, pipe);
6379 	}
6380 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6381 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6382 }
6383 
6384 /* IPS only exists on ULT machines and is tied to pipe A. */
6385 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
6386 {
6387 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
6388 }
6389 
6390 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
6391 					    enum pipe pipe, bool apply)
6392 {
6393 	u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
6394 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
6395 
6396 	if (apply)
6397 		val |= mask;
6398 	else
6399 		val &= ~mask;
6400 
6401 	I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
6402 }
6403 
6404 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
6405 {
6406 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6407 	enum pipe pipe = crtc->pipe;
6408 	u32 val;
6409 
6410 	val = MBUS_DBOX_A_CREDIT(2);
6411 
6412 	if (INTEL_GEN(dev_priv) >= 12) {
6413 		val |= MBUS_DBOX_BW_CREDIT(2);
6414 		val |= MBUS_DBOX_B_CREDIT(12);
6415 	} else {
6416 		val |= MBUS_DBOX_BW_CREDIT(1);
6417 		val |= MBUS_DBOX_B_CREDIT(8);
6418 	}
6419 
6420 	I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
6421 }
6422 
6423 static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
6424 				struct intel_atomic_state *state)
6425 {
6426 	struct drm_crtc *crtc = pipe_config->base.crtc;
6427 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6428 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6429 	int pipe = intel_crtc->pipe, hsw_workaround_pipe;
6430 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6431 	bool psl_clkgate_wa;
6432 
6433 	if (WARN_ON(intel_crtc->active))
6434 		return;
6435 
6436 	intel_encoders_pre_pll_enable(intel_crtc, pipe_config, state);
6437 
6438 	if (pipe_config->shared_dpll)
6439 		intel_enable_shared_dpll(pipe_config);
6440 
6441 	intel_encoders_pre_enable(intel_crtc, pipe_config, state);
6442 
6443 	if (intel_crtc_has_dp_encoder(pipe_config))
6444 		intel_dp_set_m_n(pipe_config, M1_N1);
6445 
6446 	if (!transcoder_is_dsi(cpu_transcoder))
6447 		intel_set_pipe_timings(pipe_config);
6448 
6449 	intel_set_pipe_src_size(pipe_config);
6450 
6451 	if (cpu_transcoder != TRANSCODER_EDP &&
6452 	    !transcoder_is_dsi(cpu_transcoder)) {
6453 		I915_WRITE(PIPE_MULT(cpu_transcoder),
6454 			   pipe_config->pixel_multiplier - 1);
6455 	}
6456 
6457 	if (pipe_config->has_pch_encoder) {
6458 		intel_cpu_transcoder_set_m_n(pipe_config,
6459 					     &pipe_config->fdi_m_n, NULL);
6460 	}
6461 
6462 	if (!transcoder_is_dsi(cpu_transcoder))
6463 		haswell_set_pipeconf(pipe_config);
6464 
6465 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
6466 		bdw_set_pipemisc(pipe_config);
6467 
6468 	intel_crtc->active = true;
6469 
6470 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
6471 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
6472 			 pipe_config->pch_pfit.enabled;
6473 	if (psl_clkgate_wa)
6474 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
6475 
6476 	if (INTEL_GEN(dev_priv) >= 9)
6477 		skylake_pfit_enable(pipe_config);
6478 	else
6479 		ironlake_pfit_enable(pipe_config);
6480 
6481 	/*
6482 	 * On ILK+ LUT must be loaded before the pipe is running but with
6483 	 * clocks enabled
6484 	 */
6485 	intel_color_load_luts(pipe_config);
6486 	intel_color_commit(pipe_config);
6487 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
6488 	if (INTEL_GEN(dev_priv) < 9)
6489 		intel_disable_primary_plane(pipe_config);
6490 
6491 	if (INTEL_GEN(dev_priv) >= 11)
6492 		icl_set_pipe_chicken(intel_crtc);
6493 
6494 	intel_ddi_set_pipe_settings(pipe_config);
6495 	if (!transcoder_is_dsi(cpu_transcoder))
6496 		intel_ddi_enable_transcoder_func(pipe_config);
6497 
6498 	if (dev_priv->display.initial_watermarks != NULL)
6499 		dev_priv->display.initial_watermarks(state, pipe_config);
6500 
6501 	if (INTEL_GEN(dev_priv) >= 11)
6502 		icl_pipe_mbus_enable(intel_crtc);
6503 
6504 	/* XXX: Do the pipe assertions at the right place for BXT DSI. */
6505 	if (!transcoder_is_dsi(cpu_transcoder))
6506 		intel_enable_pipe(pipe_config);
6507 
6508 	if (pipe_config->has_pch_encoder)
6509 		lpt_pch_enable(state, pipe_config);
6510 
6511 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DP_MST))
6512 		intel_ddi_set_vc_payload_alloc(pipe_config, true);
6513 
6514 	assert_vblank_disabled(crtc);
6515 	intel_crtc_vblank_on(pipe_config);
6516 
6517 	intel_encoders_enable(intel_crtc, pipe_config, state);
6518 
6519 	if (psl_clkgate_wa) {
6520 		intel_wait_for_vblank(dev_priv, pipe);
6521 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
6522 	}
6523 
6524 	/* If we change the relative order between pipe/planes enabling, we need
6525 	 * to change the workaround. */
6526 	hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
6527 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
6528 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
6529 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
6530 	}
6531 }
6532 
6533 static void ironlake_pfit_disable(const struct intel_crtc_state *old_crtc_state)
6534 {
6535 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6536 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6537 	enum pipe pipe = crtc->pipe;
6538 
6539 	/* To avoid upsetting the power well on haswell only disable the pfit if
6540 	 * it's in use. The hw state code will make sure we get this right. */
6541 	if (old_crtc_state->pch_pfit.enabled) {
6542 		I915_WRITE(PF_CTL(pipe), 0);
6543 		I915_WRITE(PF_WIN_POS(pipe), 0);
6544 		I915_WRITE(PF_WIN_SZ(pipe), 0);
6545 	}
6546 }
6547 
6548 static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
6549 				  struct intel_atomic_state *state)
6550 {
6551 	struct drm_crtc *crtc = old_crtc_state->base.crtc;
6552 	struct drm_device *dev = crtc->dev;
6553 	struct drm_i915_private *dev_priv = to_i915(dev);
6554 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6555 	int pipe = intel_crtc->pipe;
6556 
6557 	/*
6558 	 * Sometimes spurious CPU pipe underruns happen when the
6559 	 * pipe is already disabled, but FDI RX/TX is still enabled.
6560 	 * Happens at least with VGA+HDMI cloning. Suppress them.
6561 	 */
6562 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6563 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
6564 
6565 	intel_encoders_disable(intel_crtc, old_crtc_state, state);
6566 
6567 	drm_crtc_vblank_off(crtc);
6568 	assert_vblank_disabled(crtc);
6569 
6570 	intel_disable_pipe(old_crtc_state);
6571 
6572 	ironlake_pfit_disable(old_crtc_state);
6573 
6574 	if (old_crtc_state->has_pch_encoder)
6575 		ironlake_fdi_disable(crtc);
6576 
6577 	intel_encoders_post_disable(intel_crtc, old_crtc_state, state);
6578 
6579 	if (old_crtc_state->has_pch_encoder) {
6580 		ironlake_disable_pch_transcoder(dev_priv, pipe);
6581 
6582 		if (HAS_PCH_CPT(dev_priv)) {
6583 			i915_reg_t reg;
6584 			u32 temp;
6585 
6586 			/* disable TRANS_DP_CTL */
6587 			reg = TRANS_DP_CTL(pipe);
6588 			temp = I915_READ(reg);
6589 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
6590 				  TRANS_DP_PORT_SEL_MASK);
6591 			temp |= TRANS_DP_PORT_SEL_NONE;
6592 			I915_WRITE(reg, temp);
6593 
6594 			/* disable DPLL_SEL */
6595 			temp = I915_READ(PCH_DPLL_SEL);
6596 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
6597 			I915_WRITE(PCH_DPLL_SEL, temp);
6598 		}
6599 
6600 		ironlake_fdi_pll_disable(intel_crtc);
6601 	}
6602 
6603 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6604 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6605 }
6606 
6607 static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
6608 				 struct intel_atomic_state *state)
6609 {
6610 	struct drm_crtc *crtc = old_crtc_state->base.crtc;
6611 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6612 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6613 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
6614 
6615 	intel_encoders_disable(intel_crtc, old_crtc_state, state);
6616 
6617 	drm_crtc_vblank_off(crtc);
6618 	assert_vblank_disabled(crtc);
6619 
6620 	/* XXX: Do the pipe assertions at the right place for BXT DSI. */
6621 	if (!transcoder_is_dsi(cpu_transcoder))
6622 		intel_disable_pipe(old_crtc_state);
6623 
6624 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
6625 		intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
6626 
6627 	if (!transcoder_is_dsi(cpu_transcoder))
6628 		intel_ddi_disable_transcoder_func(old_crtc_state);
6629 
6630 	intel_dsc_disable(old_crtc_state);
6631 
6632 	if (INTEL_GEN(dev_priv) >= 9)
6633 		skylake_scaler_disable(intel_crtc);
6634 	else
6635 		ironlake_pfit_disable(old_crtc_state);
6636 
6637 	intel_encoders_post_disable(intel_crtc, old_crtc_state, state);
6638 
6639 	intel_encoders_post_pll_disable(intel_crtc, old_crtc_state, state);
6640 }
6641 
6642 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
6643 {
6644 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6645 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6646 
6647 	if (!crtc_state->gmch_pfit.control)
6648 		return;
6649 
6650 	/*
6651 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
6652 	 * according to register description and PRM.
6653 	 */
6654 	WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
6655 	assert_pipe_disabled(dev_priv, crtc->pipe);
6656 
6657 	I915_WRITE(PFIT_PGM_RATIOS, crtc_state->gmch_pfit.pgm_ratios);
6658 	I915_WRITE(PFIT_CONTROL, crtc_state->gmch_pfit.control);
6659 
6660 	/* Border color in case we don't scale up to the full screen. Black by
6661 	 * default, change to something else for debugging. */
6662 	I915_WRITE(BCLRPAT(crtc->pipe), 0);
6663 }
6664 
6665 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
6666 {
6667 	if (phy == PHY_NONE)
6668 		return false;
6669 
6670 	if (IS_ELKHARTLAKE(dev_priv))
6671 		return phy <= PHY_C;
6672 
6673 	if (INTEL_GEN(dev_priv) >= 11)
6674 		return phy <= PHY_B;
6675 
6676 	return false;
6677 }
6678 
6679 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
6680 {
6681 	if (INTEL_GEN(dev_priv) >= 12)
6682 		return phy >= PHY_D && phy <= PHY_I;
6683 
6684 	if (INTEL_GEN(dev_priv) >= 11 && !IS_ELKHARTLAKE(dev_priv))
6685 		return phy >= PHY_C && phy <= PHY_F;
6686 
6687 	return false;
6688 }
6689 
6690 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
6691 {
6692 	if (IS_ELKHARTLAKE(i915) && port == PORT_D)
6693 		return PHY_A;
6694 
6695 	return (enum phy)port;
6696 }
6697 
6698 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
6699 {
6700 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
6701 		return PORT_TC_NONE;
6702 
6703 	if (INTEL_GEN(dev_priv) >= 12)
6704 		return port - PORT_D;
6705 
6706 	return port - PORT_C;
6707 }
6708 
6709 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
6710 {
6711 	switch (port) {
6712 	case PORT_A:
6713 		return POWER_DOMAIN_PORT_DDI_A_LANES;
6714 	case PORT_B:
6715 		return POWER_DOMAIN_PORT_DDI_B_LANES;
6716 	case PORT_C:
6717 		return POWER_DOMAIN_PORT_DDI_C_LANES;
6718 	case PORT_D:
6719 		return POWER_DOMAIN_PORT_DDI_D_LANES;
6720 	case PORT_E:
6721 		return POWER_DOMAIN_PORT_DDI_E_LANES;
6722 	case PORT_F:
6723 		return POWER_DOMAIN_PORT_DDI_F_LANES;
6724 	default:
6725 		MISSING_CASE(port);
6726 		return POWER_DOMAIN_PORT_OTHER;
6727 	}
6728 }
6729 
6730 enum intel_display_power_domain
6731 intel_aux_power_domain(struct intel_digital_port *dig_port)
6732 {
6733 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
6734 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
6735 
6736 	if (intel_phy_is_tc(dev_priv, phy) &&
6737 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
6738 		switch (dig_port->aux_ch) {
6739 		case AUX_CH_C:
6740 			return POWER_DOMAIN_AUX_TBT1;
6741 		case AUX_CH_D:
6742 			return POWER_DOMAIN_AUX_TBT2;
6743 		case AUX_CH_E:
6744 			return POWER_DOMAIN_AUX_TBT3;
6745 		case AUX_CH_F:
6746 			return POWER_DOMAIN_AUX_TBT4;
6747 		default:
6748 			MISSING_CASE(dig_port->aux_ch);
6749 			return POWER_DOMAIN_AUX_TBT1;
6750 		}
6751 	}
6752 
6753 	switch (dig_port->aux_ch) {
6754 	case AUX_CH_A:
6755 		return POWER_DOMAIN_AUX_A;
6756 	case AUX_CH_B:
6757 		return POWER_DOMAIN_AUX_B;
6758 	case AUX_CH_C:
6759 		return POWER_DOMAIN_AUX_C;
6760 	case AUX_CH_D:
6761 		return POWER_DOMAIN_AUX_D;
6762 	case AUX_CH_E:
6763 		return POWER_DOMAIN_AUX_E;
6764 	case AUX_CH_F:
6765 		return POWER_DOMAIN_AUX_F;
6766 	default:
6767 		MISSING_CASE(dig_port->aux_ch);
6768 		return POWER_DOMAIN_AUX_A;
6769 	}
6770 }
6771 
6772 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
6773 {
6774 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6775 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6776 	struct drm_encoder *encoder;
6777 	enum pipe pipe = crtc->pipe;
6778 	u64 mask;
6779 	enum transcoder transcoder = crtc_state->cpu_transcoder;
6780 
6781 	if (!crtc_state->base.active)
6782 		return 0;
6783 
6784 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
6785 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
6786 	if (crtc_state->pch_pfit.enabled ||
6787 	    crtc_state->pch_pfit.force_thru)
6788 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
6789 
6790 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
6791 				  crtc_state->base.encoder_mask) {
6792 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
6793 
6794 		mask |= BIT_ULL(intel_encoder->power_domain);
6795 	}
6796 
6797 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
6798 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
6799 
6800 	if (crtc_state->shared_dpll)
6801 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
6802 
6803 	return mask;
6804 }
6805 
6806 static u64
6807 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
6808 {
6809 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6810 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6811 	enum intel_display_power_domain domain;
6812 	u64 domains, new_domains, old_domains;
6813 
6814 	old_domains = crtc->enabled_power_domains;
6815 	crtc->enabled_power_domains = new_domains =
6816 		get_crtc_power_domains(crtc_state);
6817 
6818 	domains = new_domains & ~old_domains;
6819 
6820 	for_each_power_domain(domain, domains)
6821 		intel_display_power_get(dev_priv, domain);
6822 
6823 	return old_domains & ~new_domains;
6824 }
6825 
6826 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
6827 				      u64 domains)
6828 {
6829 	enum intel_display_power_domain domain;
6830 
6831 	for_each_power_domain(domain, domains)
6832 		intel_display_power_put_unchecked(dev_priv, domain);
6833 }
6834 
6835 static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6836 				   struct intel_atomic_state *state)
6837 {
6838 	struct drm_crtc *crtc = pipe_config->base.crtc;
6839 	struct drm_device *dev = crtc->dev;
6840 	struct drm_i915_private *dev_priv = to_i915(dev);
6841 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6842 	int pipe = intel_crtc->pipe;
6843 
6844 	if (WARN_ON(intel_crtc->active))
6845 		return;
6846 
6847 	if (intel_crtc_has_dp_encoder(pipe_config))
6848 		intel_dp_set_m_n(pipe_config, M1_N1);
6849 
6850 	intel_set_pipe_timings(pipe_config);
6851 	intel_set_pipe_src_size(pipe_config);
6852 
6853 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
6854 		I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6855 		I915_WRITE(CHV_CANVAS(pipe), 0);
6856 	}
6857 
6858 	i9xx_set_pipeconf(pipe_config);
6859 
6860 	intel_crtc->active = true;
6861 
6862 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6863 
6864 	intel_encoders_pre_pll_enable(intel_crtc, pipe_config, state);
6865 
6866 	if (IS_CHERRYVIEW(dev_priv)) {
6867 		chv_prepare_pll(intel_crtc, pipe_config);
6868 		chv_enable_pll(intel_crtc, pipe_config);
6869 	} else {
6870 		vlv_prepare_pll(intel_crtc, pipe_config);
6871 		vlv_enable_pll(intel_crtc, pipe_config);
6872 	}
6873 
6874 	intel_encoders_pre_enable(intel_crtc, pipe_config, state);
6875 
6876 	i9xx_pfit_enable(pipe_config);
6877 
6878 	intel_color_load_luts(pipe_config);
6879 	intel_color_commit(pipe_config);
6880 	/* update DSPCNTR to configure gamma for pipe bottom color */
6881 	intel_disable_primary_plane(pipe_config);
6882 
6883 	dev_priv->display.initial_watermarks(state, pipe_config);
6884 	intel_enable_pipe(pipe_config);
6885 
6886 	assert_vblank_disabled(crtc);
6887 	intel_crtc_vblank_on(pipe_config);
6888 
6889 	intel_encoders_enable(intel_crtc, pipe_config, state);
6890 }
6891 
6892 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
6893 {
6894 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
6895 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6896 
6897 	I915_WRITE(FP0(crtc->pipe), crtc_state->dpll_hw_state.fp0);
6898 	I915_WRITE(FP1(crtc->pipe), crtc_state->dpll_hw_state.fp1);
6899 }
6900 
6901 static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6902 			     struct intel_atomic_state *state)
6903 {
6904 	struct drm_crtc *crtc = pipe_config->base.crtc;
6905 	struct drm_device *dev = crtc->dev;
6906 	struct drm_i915_private *dev_priv = to_i915(dev);
6907 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6908 	enum pipe pipe = intel_crtc->pipe;
6909 
6910 	if (WARN_ON(intel_crtc->active))
6911 		return;
6912 
6913 	i9xx_set_pll_dividers(pipe_config);
6914 
6915 	if (intel_crtc_has_dp_encoder(pipe_config))
6916 		intel_dp_set_m_n(pipe_config, M1_N1);
6917 
6918 	intel_set_pipe_timings(pipe_config);
6919 	intel_set_pipe_src_size(pipe_config);
6920 
6921 	i9xx_set_pipeconf(pipe_config);
6922 
6923 	intel_crtc->active = true;
6924 
6925 	if (!IS_GEN(dev_priv, 2))
6926 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6927 
6928 	intel_encoders_pre_enable(intel_crtc, pipe_config, state);
6929 
6930 	i9xx_enable_pll(intel_crtc, pipe_config);
6931 
6932 	i9xx_pfit_enable(pipe_config);
6933 
6934 	intel_color_load_luts(pipe_config);
6935 	intel_color_commit(pipe_config);
6936 	/* update DSPCNTR to configure gamma for pipe bottom color */
6937 	intel_disable_primary_plane(pipe_config);
6938 
6939 	if (dev_priv->display.initial_watermarks != NULL)
6940 		dev_priv->display.initial_watermarks(state,
6941 						     pipe_config);
6942 	else
6943 		intel_update_watermarks(intel_crtc);
6944 	intel_enable_pipe(pipe_config);
6945 
6946 	assert_vblank_disabled(crtc);
6947 	intel_crtc_vblank_on(pipe_config);
6948 
6949 	intel_encoders_enable(intel_crtc, pipe_config, state);
6950 }
6951 
6952 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
6953 {
6954 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
6955 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6956 
6957 	if (!old_crtc_state->gmch_pfit.control)
6958 		return;
6959 
6960 	assert_pipe_disabled(dev_priv, crtc->pipe);
6961 
6962 	DRM_DEBUG_KMS("disabling pfit, current: 0x%08x\n",
6963 		      I915_READ(PFIT_CONTROL));
6964 	I915_WRITE(PFIT_CONTROL, 0);
6965 }
6966 
6967 static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6968 			      struct intel_atomic_state *state)
6969 {
6970 	struct drm_crtc *crtc = old_crtc_state->base.crtc;
6971 	struct drm_device *dev = crtc->dev;
6972 	struct drm_i915_private *dev_priv = to_i915(dev);
6973 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6974 	int pipe = intel_crtc->pipe;
6975 
6976 	/*
6977 	 * On gen2 planes are double buffered but the pipe isn't, so we must
6978 	 * wait for planes to fully turn off before disabling the pipe.
6979 	 */
6980 	if (IS_GEN(dev_priv, 2))
6981 		intel_wait_for_vblank(dev_priv, pipe);
6982 
6983 	intel_encoders_disable(intel_crtc, old_crtc_state, state);
6984 
6985 	drm_crtc_vblank_off(crtc);
6986 	assert_vblank_disabled(crtc);
6987 
6988 	intel_disable_pipe(old_crtc_state);
6989 
6990 	i9xx_pfit_disable(old_crtc_state);
6991 
6992 	intel_encoders_post_disable(intel_crtc, old_crtc_state, state);
6993 
6994 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
6995 		if (IS_CHERRYVIEW(dev_priv))
6996 			chv_disable_pll(dev_priv, pipe);
6997 		else if (IS_VALLEYVIEW(dev_priv))
6998 			vlv_disable_pll(dev_priv, pipe);
6999 		else
7000 			i9xx_disable_pll(old_crtc_state);
7001 	}
7002 
7003 	intel_encoders_post_pll_disable(intel_crtc, old_crtc_state, state);
7004 
7005 	if (!IS_GEN(dev_priv, 2))
7006 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7007 
7008 	if (!dev_priv->display.initial_watermarks)
7009 		intel_update_watermarks(intel_crtc);
7010 
7011 	/* clock the pipe down to 640x480@60 to potentially save power */
7012 	if (IS_I830(dev_priv))
7013 		i830_enable_pipe(dev_priv, pipe);
7014 }
7015 
7016 static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
7017 					struct drm_modeset_acquire_ctx *ctx)
7018 {
7019 	struct intel_encoder *encoder;
7020 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7021 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
7022 	struct intel_bw_state *bw_state =
7023 		to_intel_bw_state(dev_priv->bw_obj.state);
7024 	enum intel_display_power_domain domain;
7025 	struct intel_plane *plane;
7026 	u64 domains;
7027 	struct drm_atomic_state *state;
7028 	struct intel_crtc_state *crtc_state;
7029 	int ret;
7030 
7031 	if (!intel_crtc->active)
7032 		return;
7033 
7034 	for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
7035 		const struct intel_plane_state *plane_state =
7036 			to_intel_plane_state(plane->base.state);
7037 
7038 		if (plane_state->base.visible)
7039 			intel_plane_disable_noatomic(intel_crtc, plane);
7040 	}
7041 
7042 	state = drm_atomic_state_alloc(crtc->dev);
7043 	if (!state) {
7044 		DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
7045 			      crtc->base.id, crtc->name);
7046 		return;
7047 	}
7048 
7049 	state->acquire_ctx = ctx;
7050 
7051 	/* Everything's already locked, -EDEADLK can't happen. */
7052 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
7053 	ret = drm_atomic_add_affected_connectors(state, crtc);
7054 
7055 	WARN_ON(IS_ERR(crtc_state) || ret);
7056 
7057 	dev_priv->display.crtc_disable(crtc_state, to_intel_atomic_state(state));
7058 
7059 	drm_atomic_state_put(state);
7060 
7061 	DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7062 		      crtc->base.id, crtc->name);
7063 
7064 	WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
7065 	crtc->state->active = false;
7066 	intel_crtc->active = false;
7067 	crtc->enabled = false;
7068 	crtc->state->connector_mask = 0;
7069 	crtc->state->encoder_mask = 0;
7070 
7071 	for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
7072 		encoder->base.crtc = NULL;
7073 
7074 	intel_fbc_disable(intel_crtc);
7075 	intel_update_watermarks(intel_crtc);
7076 	intel_disable_shared_dpll(to_intel_crtc_state(crtc->state));
7077 
7078 	domains = intel_crtc->enabled_power_domains;
7079 	for_each_power_domain(domain, domains)
7080 		intel_display_power_put_unchecked(dev_priv, domain);
7081 	intel_crtc->enabled_power_domains = 0;
7082 
7083 	dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
7084 	dev_priv->min_cdclk[intel_crtc->pipe] = 0;
7085 	dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
7086 
7087 	bw_state->data_rate[intel_crtc->pipe] = 0;
7088 	bw_state->num_active_planes[intel_crtc->pipe] = 0;
7089 }
7090 
7091 /*
7092  * turn all crtc's off, but do not adjust state
7093  * This has to be paired with a call to intel_modeset_setup_hw_state.
7094  */
7095 int intel_display_suspend(struct drm_device *dev)
7096 {
7097 	struct drm_i915_private *dev_priv = to_i915(dev);
7098 	struct drm_atomic_state *state;
7099 	int ret;
7100 
7101 	state = drm_atomic_helper_suspend(dev);
7102 	ret = PTR_ERR_OR_ZERO(state);
7103 	if (ret)
7104 		DRM_ERROR("Suspending crtc's failed with %i\n", ret);
7105 	else
7106 		dev_priv->modeset_restore_state = state;
7107 	return ret;
7108 }
7109 
7110 void intel_encoder_destroy(struct drm_encoder *encoder)
7111 {
7112 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7113 
7114 	drm_encoder_cleanup(encoder);
7115 	kfree(intel_encoder);
7116 }
7117 
7118 /* Cross check the actual hw state with our own modeset state tracking (and it's
7119  * internal consistency). */
7120 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7121 					 struct drm_connector_state *conn_state)
7122 {
7123 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7124 
7125 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
7126 		      connector->base.base.id,
7127 		      connector->base.name);
7128 
7129 	if (connector->get_hw_state(connector)) {
7130 		struct intel_encoder *encoder = connector->encoder;
7131 
7132 		I915_STATE_WARN(!crtc_state,
7133 			 "connector enabled without attached crtc\n");
7134 
7135 		if (!crtc_state)
7136 			return;
7137 
7138 		I915_STATE_WARN(!crtc_state->base.active,
7139 		      "connector is active, but attached crtc isn't\n");
7140 
7141 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7142 			return;
7143 
7144 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7145 			"atomic encoder doesn't match attached encoder\n");
7146 
7147 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7148 			"attached encoder crtc differs from connector crtc\n");
7149 	} else {
7150 		I915_STATE_WARN(crtc_state && crtc_state->base.active,
7151 			"attached crtc is active, but connector isn't\n");
7152 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7153 			"best encoder set without crtc!\n");
7154 	}
7155 }
7156 
7157 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7158 {
7159 	if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7160 		return crtc_state->fdi_lanes;
7161 
7162 	return 0;
7163 }
7164 
7165 static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7166 				     struct intel_crtc_state *pipe_config)
7167 {
7168 	struct drm_i915_private *dev_priv = to_i915(dev);
7169 	struct drm_atomic_state *state = pipe_config->base.state;
7170 	struct intel_crtc *other_crtc;
7171 	struct intel_crtc_state *other_crtc_state;
7172 
7173 	DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7174 		      pipe_name(pipe), pipe_config->fdi_lanes);
7175 	if (pipe_config->fdi_lanes > 4) {
7176 		DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7177 			      pipe_name(pipe), pipe_config->fdi_lanes);
7178 		return -EINVAL;
7179 	}
7180 
7181 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7182 		if (pipe_config->fdi_lanes > 2) {
7183 			DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7184 				      pipe_config->fdi_lanes);
7185 			return -EINVAL;
7186 		} else {
7187 			return 0;
7188 		}
7189 	}
7190 
7191 	if (INTEL_INFO(dev_priv)->num_pipes == 2)
7192 		return 0;
7193 
7194 	/* Ivybridge 3 pipe is really complicated */
7195 	switch (pipe) {
7196 	case PIPE_A:
7197 		return 0;
7198 	case PIPE_B:
7199 		if (pipe_config->fdi_lanes <= 2)
7200 			return 0;
7201 
7202 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
7203 		other_crtc_state =
7204 			intel_atomic_get_crtc_state(state, other_crtc);
7205 		if (IS_ERR(other_crtc_state))
7206 			return PTR_ERR(other_crtc_state);
7207 
7208 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
7209 			DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7210 				      pipe_name(pipe), pipe_config->fdi_lanes);
7211 			return -EINVAL;
7212 		}
7213 		return 0;
7214 	case PIPE_C:
7215 		if (pipe_config->fdi_lanes > 2) {
7216 			DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7217 				      pipe_name(pipe), pipe_config->fdi_lanes);
7218 			return -EINVAL;
7219 		}
7220 
7221 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
7222 		other_crtc_state =
7223 			intel_atomic_get_crtc_state(state, other_crtc);
7224 		if (IS_ERR(other_crtc_state))
7225 			return PTR_ERR(other_crtc_state);
7226 
7227 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
7228 			DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
7229 			return -EINVAL;
7230 		}
7231 		return 0;
7232 	default:
7233 		BUG();
7234 	}
7235 }
7236 
7237 #define RETRY 1
7238 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
7239 				       struct intel_crtc_state *pipe_config)
7240 {
7241 	struct drm_device *dev = intel_crtc->base.dev;
7242 	const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
7243 	int lane, link_bw, fdi_dotclock, ret;
7244 	bool needs_recompute = false;
7245 
7246 retry:
7247 	/* FDI is a binary signal running at ~2.7GHz, encoding
7248 	 * each output octet as 10 bits. The actual frequency
7249 	 * is stored as a divider into a 100MHz clock, and the
7250 	 * mode pixel clock is stored in units of 1KHz.
7251 	 * Hence the bw of each lane in terms of the mode signal
7252 	 * is:
7253 	 */
7254 	link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
7255 
7256 	fdi_dotclock = adjusted_mode->crtc_clock;
7257 
7258 	lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
7259 					   pipe_config->pipe_bpp);
7260 
7261 	pipe_config->fdi_lanes = lane;
7262 
7263 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
7264 			       link_bw, &pipe_config->fdi_m_n, false, false);
7265 
7266 	ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
7267 	if (ret == -EDEADLK)
7268 		return ret;
7269 
7270 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
7271 		pipe_config->pipe_bpp -= 2*3;
7272 		DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7273 			      pipe_config->pipe_bpp);
7274 		needs_recompute = true;
7275 		pipe_config->bw_constrained = true;
7276 
7277 		goto retry;
7278 	}
7279 
7280 	if (needs_recompute)
7281 		return RETRY;
7282 
7283 	return ret;
7284 }
7285 
7286 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
7287 {
7288 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7289 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7290 
7291 	/* IPS only exists on ULT machines and is tied to pipe A. */
7292 	if (!hsw_crtc_supports_ips(crtc))
7293 		return false;
7294 
7295 	if (!i915_modparams.enable_ips)
7296 		return false;
7297 
7298 	if (crtc_state->pipe_bpp > 24)
7299 		return false;
7300 
7301 	/*
7302 	 * We compare against max which means we must take
7303 	 * the increased cdclk requirement into account when
7304 	 * calculating the new cdclk.
7305 	 *
7306 	 * Should measure whether using a lower cdclk w/o IPS
7307 	 */
7308 	if (IS_BROADWELL(dev_priv) &&
7309 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
7310 		return false;
7311 
7312 	return true;
7313 }
7314 
7315 static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7316 {
7317 	struct drm_i915_private *dev_priv =
7318 		to_i915(crtc_state->base.crtc->dev);
7319 	struct intel_atomic_state *intel_state =
7320 		to_intel_atomic_state(crtc_state->base.state);
7321 
7322 	if (!hsw_crtc_state_ips_capable(crtc_state))
7323 		return false;
7324 
7325 	/*
7326 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
7327 	 * enabled and disabled dynamically based on package C states,
7328 	 * user space can't make reliable use of the CRCs, so let's just
7329 	 * completely disable it.
7330 	 */
7331 	if (crtc_state->crc_enabled)
7332 		return false;
7333 
7334 	/* IPS should be fine as long as at least one plane is enabled. */
7335 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
7336 		return false;
7337 
7338 	/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
7339 	if (IS_BROADWELL(dev_priv) &&
7340 	    crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
7341 		return false;
7342 
7343 	return true;
7344 }
7345 
7346 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7347 {
7348 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7349 
7350 	/* GDG double wide on either pipe, otherwise pipe A only */
7351 	return INTEL_GEN(dev_priv) < 4 &&
7352 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7353 }
7354 
7355 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
7356 {
7357 	u32 pixel_rate;
7358 
7359 	pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
7360 
7361 	/*
7362 	 * We only use IF-ID interlacing. If we ever use
7363 	 * PF-ID we'll need to adjust the pixel_rate here.
7364 	 */
7365 
7366 	if (pipe_config->pch_pfit.enabled) {
7367 		u64 pipe_w, pipe_h, pfit_w, pfit_h;
7368 		u32 pfit_size = pipe_config->pch_pfit.size;
7369 
7370 		pipe_w = pipe_config->pipe_src_w;
7371 		pipe_h = pipe_config->pipe_src_h;
7372 
7373 		pfit_w = (pfit_size >> 16) & 0xFFFF;
7374 		pfit_h = pfit_size & 0xFFFF;
7375 		if (pipe_w < pfit_w)
7376 			pipe_w = pfit_w;
7377 		if (pipe_h < pfit_h)
7378 			pipe_h = pfit_h;
7379 
7380 		if (WARN_ON(!pfit_w || !pfit_h))
7381 			return pixel_rate;
7382 
7383 		pixel_rate = div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
7384 				     pfit_w * pfit_h);
7385 	}
7386 
7387 	return pixel_rate;
7388 }
7389 
7390 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
7391 {
7392 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
7393 
7394 	if (HAS_GMCH(dev_priv))
7395 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
7396 		crtc_state->pixel_rate =
7397 			crtc_state->base.adjusted_mode.crtc_clock;
7398 	else
7399 		crtc_state->pixel_rate =
7400 			ilk_pipe_pixel_rate(crtc_state);
7401 }
7402 
7403 static int intel_crtc_compute_config(struct intel_crtc *crtc,
7404 				     struct intel_crtc_state *pipe_config)
7405 {
7406 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7407 	const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
7408 	int clock_limit = dev_priv->max_dotclk_freq;
7409 
7410 	if (INTEL_GEN(dev_priv) < 4) {
7411 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
7412 
7413 		/*
7414 		 * Enable double wide mode when the dot clock
7415 		 * is > 90% of the (display) core speed.
7416 		 */
7417 		if (intel_crtc_supports_double_wide(crtc) &&
7418 		    adjusted_mode->crtc_clock > clock_limit) {
7419 			clock_limit = dev_priv->max_dotclk_freq;
7420 			pipe_config->double_wide = true;
7421 		}
7422 	}
7423 
7424 	if (adjusted_mode->crtc_clock > clock_limit) {
7425 		DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7426 			      adjusted_mode->crtc_clock, clock_limit,
7427 			      yesno(pipe_config->double_wide));
7428 		return -EINVAL;
7429 	}
7430 
7431 	if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
7432 	     pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
7433 	     pipe_config->base.ctm) {
7434 		/*
7435 		 * There is only one pipe CSC unit per pipe, and we need that
7436 		 * for output conversion from RGB->YCBCR. So if CTM is already
7437 		 * applied we can't support YCBCR420 output.
7438 		 */
7439 		DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
7440 		return -EINVAL;
7441 	}
7442 
7443 	/*
7444 	 * Pipe horizontal size must be even in:
7445 	 * - DVO ganged mode
7446 	 * - LVDS dual channel mode
7447 	 * - Double wide pipe
7448 	 */
7449 	if (pipe_config->pipe_src_w & 1) {
7450 		if (pipe_config->double_wide) {
7451 			DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
7452 			return -EINVAL;
7453 		}
7454 
7455 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
7456 		    intel_is_dual_link_lvds(dev_priv)) {
7457 			DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
7458 			return -EINVAL;
7459 		}
7460 	}
7461 
7462 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
7463 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
7464 	 */
7465 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
7466 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
7467 		return -EINVAL;
7468 
7469 	intel_crtc_compute_pixel_rate(pipe_config);
7470 
7471 	if (pipe_config->has_pch_encoder)
7472 		return ironlake_fdi_compute_config(crtc, pipe_config);
7473 
7474 	return 0;
7475 }
7476 
7477 static void
7478 intel_reduce_m_n_ratio(u32 *num, u32 *den)
7479 {
7480 	while (*num > DATA_LINK_M_N_MASK ||
7481 	       *den > DATA_LINK_M_N_MASK) {
7482 		*num >>= 1;
7483 		*den >>= 1;
7484 	}
7485 }
7486 
7487 static void compute_m_n(unsigned int m, unsigned int n,
7488 			u32 *ret_m, u32 *ret_n,
7489 			bool constant_n)
7490 {
7491 	/*
7492 	 * Several DP dongles in particular seem to be fussy about
7493 	 * too large link M/N values. Give N value as 0x8000 that
7494 	 * should be acceptable by specific devices. 0x8000 is the
7495 	 * specified fixed N value for asynchronous clock mode,
7496 	 * which the devices expect also in synchronous clock mode.
7497 	 */
7498 	if (constant_n)
7499 		*ret_n = 0x8000;
7500 	else
7501 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7502 
7503 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
7504 	intel_reduce_m_n_ratio(ret_m, ret_n);
7505 }
7506 
7507 void
7508 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
7509 		       int pixel_clock, int link_clock,
7510 		       struct intel_link_m_n *m_n,
7511 		       bool constant_n, bool fec_enable)
7512 {
7513 	u32 data_clock = bits_per_pixel * pixel_clock;
7514 
7515 	if (fec_enable)
7516 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
7517 
7518 	m_n->tu = 64;
7519 	compute_m_n(data_clock,
7520 		    link_clock * nlanes * 8,
7521 		    &m_n->gmch_m, &m_n->gmch_n,
7522 		    constant_n);
7523 
7524 	compute_m_n(pixel_clock, link_clock,
7525 		    &m_n->link_m, &m_n->link_n,
7526 		    constant_n);
7527 }
7528 
7529 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7530 {
7531 	if (i915_modparams.panel_use_ssc >= 0)
7532 		return i915_modparams.panel_use_ssc != 0;
7533 	return dev_priv->vbt.lvds_use_ssc
7534 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
7535 }
7536 
7537 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
7538 {
7539 	return (1 << dpll->n) << 16 | dpll->m2;
7540 }
7541 
7542 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
7543 {
7544 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
7545 }
7546 
7547 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
7548 				     struct intel_crtc_state *crtc_state,
7549 				     struct dpll *reduced_clock)
7550 {
7551 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7552 	u32 fp, fp2 = 0;
7553 
7554 	if (IS_PINEVIEW(dev_priv)) {
7555 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
7556 		if (reduced_clock)
7557 			fp2 = pnv_dpll_compute_fp(reduced_clock);
7558 	} else {
7559 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
7560 		if (reduced_clock)
7561 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
7562 	}
7563 
7564 	crtc_state->dpll_hw_state.fp0 = fp;
7565 
7566 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7567 	    reduced_clock) {
7568 		crtc_state->dpll_hw_state.fp1 = fp2;
7569 	} else {
7570 		crtc_state->dpll_hw_state.fp1 = fp;
7571 	}
7572 }
7573 
7574 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7575 		pipe)
7576 {
7577 	u32 reg_val;
7578 
7579 	/*
7580 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7581 	 * and set it to a reasonable value instead.
7582 	 */
7583 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7584 	reg_val &= 0xffffff00;
7585 	reg_val |= 0x00000030;
7586 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7587 
7588 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7589 	reg_val &= 0x00ffffff;
7590 	reg_val |= 0x8c000000;
7591 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7592 
7593 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7594 	reg_val &= 0xffffff00;
7595 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7596 
7597 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7598 	reg_val &= 0x00ffffff;
7599 	reg_val |= 0xb0000000;
7600 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7601 }
7602 
7603 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
7604 					 const struct intel_link_m_n *m_n)
7605 {
7606 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7607 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7608 	enum pipe pipe = crtc->pipe;
7609 
7610 	I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7611 	I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7612 	I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7613 	I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
7614 }
7615 
7616 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
7617 				 enum transcoder transcoder)
7618 {
7619 	if (IS_HASWELL(dev_priv))
7620 		return transcoder == TRANSCODER_EDP;
7621 
7622 	/*
7623 	 * Strictly speaking some registers are available before
7624 	 * gen7, but we only support DRRS on gen7+
7625 	 */
7626 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
7627 }
7628 
7629 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
7630 					 const struct intel_link_m_n *m_n,
7631 					 const struct intel_link_m_n *m2_n2)
7632 {
7633 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
7634 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7635 	enum pipe pipe = crtc->pipe;
7636 	enum transcoder transcoder = crtc_state->cpu_transcoder;
7637 
7638 	if (INTEL_GEN(dev_priv) >= 5) {
7639 		I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7640 		I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7641 		I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7642 		I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
7643 		/*
7644 		 *  M2_N2 registers are set only if DRRS is supported
7645 		 * (to make sure the registers are not unnecessarily accessed).
7646 		 */
7647 		if (m2_n2 && crtc_state->has_drrs &&
7648 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
7649 			I915_WRITE(PIPE_DATA_M2(transcoder),
7650 					TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7651 			I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7652 			I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7653 			I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7654 		}
7655 	} else {
7656 		I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7657 		I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7658 		I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7659 		I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
7660 	}
7661 }
7662 
7663 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
7664 {
7665 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7666 
7667 	if (m_n == M1_N1) {
7668 		dp_m_n = &crtc_state->dp_m_n;
7669 		dp_m2_n2 = &crtc_state->dp_m2_n2;
7670 	} else if (m_n == M2_N2) {
7671 
7672 		/*
7673 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
7674 		 * needs to be programmed into M1_N1.
7675 		 */
7676 		dp_m_n = &crtc_state->dp_m2_n2;
7677 	} else {
7678 		DRM_ERROR("Unsupported divider value\n");
7679 		return;
7680 	}
7681 
7682 	if (crtc_state->has_pch_encoder)
7683 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
7684 	else
7685 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
7686 }
7687 
7688 static void vlv_compute_dpll(struct intel_crtc *crtc,
7689 			     struct intel_crtc_state *pipe_config)
7690 {
7691 	pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
7692 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
7693 	if (crtc->pipe != PIPE_A)
7694 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7695 
7696 	/* DPLL not used with DSI, but still need the rest set up */
7697 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
7698 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7699 			DPLL_EXT_BUFFER_ENABLE_VLV;
7700 
7701 	pipe_config->dpll_hw_state.dpll_md =
7702 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7703 }
7704 
7705 static void chv_compute_dpll(struct intel_crtc *crtc,
7706 			     struct intel_crtc_state *pipe_config)
7707 {
7708 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7709 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
7710 	if (crtc->pipe != PIPE_A)
7711 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7712 
7713 	/* DPLL not used with DSI, but still need the rest set up */
7714 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
7715 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7716 
7717 	pipe_config->dpll_hw_state.dpll_md =
7718 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7719 }
7720 
7721 static void vlv_prepare_pll(struct intel_crtc *crtc,
7722 			    const struct intel_crtc_state *pipe_config)
7723 {
7724 	struct drm_device *dev = crtc->base.dev;
7725 	struct drm_i915_private *dev_priv = to_i915(dev);
7726 	enum pipe pipe = crtc->pipe;
7727 	u32 mdiv;
7728 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
7729 	u32 coreclk, reg_val;
7730 
7731 	/* Enable Refclk */
7732 	I915_WRITE(DPLL(pipe),
7733 		   pipe_config->dpll_hw_state.dpll &
7734 		   ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7735 
7736 	/* No need to actually set up the DPLL with DSI */
7737 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7738 		return;
7739 
7740 	vlv_dpio_get(dev_priv);
7741 
7742 	bestn = pipe_config->dpll.n;
7743 	bestm1 = pipe_config->dpll.m1;
7744 	bestm2 = pipe_config->dpll.m2;
7745 	bestp1 = pipe_config->dpll.p1;
7746 	bestp2 = pipe_config->dpll.p2;
7747 
7748 	/* See eDP HDMI DPIO driver vbios notes doc */
7749 
7750 	/* PLL B needs special handling */
7751 	if (pipe == PIPE_B)
7752 		vlv_pllb_recal_opamp(dev_priv, pipe);
7753 
7754 	/* Set up Tx target for periodic Rcomp update */
7755 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
7756 
7757 	/* Disable target IRef on PLL */
7758 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
7759 	reg_val &= 0x00ffffff;
7760 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
7761 
7762 	/* Disable fast lock */
7763 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
7764 
7765 	/* Set idtafcrecal before PLL is enabled */
7766 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7767 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7768 	mdiv |= ((bestn << DPIO_N_SHIFT));
7769 	mdiv |= (1 << DPIO_K_SHIFT);
7770 
7771 	/*
7772 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7773 	 * but we don't support that).
7774 	 * Note: don't use the DAC post divider as it seems unstable.
7775 	 */
7776 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
7777 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7778 
7779 	mdiv |= DPIO_ENABLE_CALIBRATION;
7780 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7781 
7782 	/* Set HBR and RBR LPF coefficients */
7783 	if (pipe_config->port_clock == 162000 ||
7784 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
7785 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
7786 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7787 				 0x009f0003);
7788 	else
7789 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7790 				 0x00d0000f);
7791 
7792 	if (intel_crtc_has_dp_encoder(pipe_config)) {
7793 		/* Use SSC source */
7794 		if (pipe == PIPE_A)
7795 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7796 					 0x0df40000);
7797 		else
7798 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7799 					 0x0df70000);
7800 	} else { /* HDMI or VGA */
7801 		/* Use bend source */
7802 		if (pipe == PIPE_A)
7803 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7804 					 0x0df70000);
7805 		else
7806 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7807 					 0x0df40000);
7808 	}
7809 
7810 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
7811 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
7812 	if (intel_crtc_has_dp_encoder(pipe_config))
7813 		coreclk |= 0x01000000;
7814 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
7815 
7816 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
7817 
7818 	vlv_dpio_put(dev_priv);
7819 }
7820 
7821 static void chv_prepare_pll(struct intel_crtc *crtc,
7822 			    const struct intel_crtc_state *pipe_config)
7823 {
7824 	struct drm_device *dev = crtc->base.dev;
7825 	struct drm_i915_private *dev_priv = to_i915(dev);
7826 	enum pipe pipe = crtc->pipe;
7827 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
7828 	u32 loopfilter, tribuf_calcntr;
7829 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
7830 	u32 dpio_val;
7831 	int vco;
7832 
7833 	/* Enable Refclk and SSC */
7834 	I915_WRITE(DPLL(pipe),
7835 		   pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7836 
7837 	/* No need to actually set up the DPLL with DSI */
7838 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7839 		return;
7840 
7841 	bestn = pipe_config->dpll.n;
7842 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7843 	bestm1 = pipe_config->dpll.m1;
7844 	bestm2 = pipe_config->dpll.m2 >> 22;
7845 	bestp1 = pipe_config->dpll.p1;
7846 	bestp2 = pipe_config->dpll.p2;
7847 	vco = pipe_config->dpll.vco;
7848 	dpio_val = 0;
7849 	loopfilter = 0;
7850 
7851 	vlv_dpio_get(dev_priv);
7852 
7853 	/* p1 and p2 divider */
7854 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7855 			5 << DPIO_CHV_S1_DIV_SHIFT |
7856 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7857 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7858 			1 << DPIO_CHV_K_DIV_SHIFT);
7859 
7860 	/* Feedback post-divider - m2 */
7861 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7862 
7863 	/* Feedback refclk divider - n and m1 */
7864 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7865 			DPIO_CHV_M1_DIV_BY_2 |
7866 			1 << DPIO_CHV_N_DIV_SHIFT);
7867 
7868 	/* M2 fraction division */
7869 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
7870 
7871 	/* M2 fraction division enable */
7872 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7873 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7874 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7875 	if (bestm2_frac)
7876 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7877 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
7878 
7879 	/* Program digital lock detect threshold */
7880 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7881 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7882 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7883 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7884 	if (!bestm2_frac)
7885 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7886 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7887 
7888 	/* Loop filter */
7889 	if (vco == 5400000) {
7890 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7891 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7892 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7893 		tribuf_calcntr = 0x9;
7894 	} else if (vco <= 6200000) {
7895 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7896 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7897 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7898 		tribuf_calcntr = 0x9;
7899 	} else if (vco <= 6480000) {
7900 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7901 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7902 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7903 		tribuf_calcntr = 0x8;
7904 	} else {
7905 		/* Not supported. Apply the same limits as in the max case */
7906 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7907 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7908 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7909 		tribuf_calcntr = 0;
7910 	}
7911 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7912 
7913 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
7914 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7915 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7916 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7917 
7918 	/* AFC Recal */
7919 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7920 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7921 			DPIO_AFC_RECAL);
7922 
7923 	vlv_dpio_put(dev_priv);
7924 }
7925 
7926 /**
7927  * vlv_force_pll_on - forcibly enable just the PLL
7928  * @dev_priv: i915 private structure
7929  * @pipe: pipe PLL to enable
7930  * @dpll: PLL configuration
7931  *
7932  * Enable the PLL for @pipe using the supplied @dpll config. To be used
7933  * in cases where we need the PLL enabled even when @pipe is not going to
7934  * be enabled.
7935  */
7936 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
7937 		     const struct dpll *dpll)
7938 {
7939 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
7940 	struct intel_crtc_state *pipe_config;
7941 
7942 	pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7943 	if (!pipe_config)
7944 		return -ENOMEM;
7945 
7946 	pipe_config->base.crtc = &crtc->base;
7947 	pipe_config->pixel_multiplier = 1;
7948 	pipe_config->dpll = *dpll;
7949 
7950 	if (IS_CHERRYVIEW(dev_priv)) {
7951 		chv_compute_dpll(crtc, pipe_config);
7952 		chv_prepare_pll(crtc, pipe_config);
7953 		chv_enable_pll(crtc, pipe_config);
7954 	} else {
7955 		vlv_compute_dpll(crtc, pipe_config);
7956 		vlv_prepare_pll(crtc, pipe_config);
7957 		vlv_enable_pll(crtc, pipe_config);
7958 	}
7959 
7960 	kfree(pipe_config);
7961 
7962 	return 0;
7963 }
7964 
7965 /**
7966  * vlv_force_pll_off - forcibly disable just the PLL
7967  * @dev_priv: i915 private structure
7968  * @pipe: pipe PLL to disable
7969  *
7970  * Disable the PLL for @pipe. To be used in cases where we need
7971  * the PLL enabled even when @pipe is not going to be enabled.
7972  */
7973 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
7974 {
7975 	if (IS_CHERRYVIEW(dev_priv))
7976 		chv_disable_pll(dev_priv, pipe);
7977 	else
7978 		vlv_disable_pll(dev_priv, pipe);
7979 }
7980 
7981 static void i9xx_compute_dpll(struct intel_crtc *crtc,
7982 			      struct intel_crtc_state *crtc_state,
7983 			      struct dpll *reduced_clock)
7984 {
7985 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7986 	u32 dpll;
7987 	struct dpll *clock = &crtc_state->dpll;
7988 
7989 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
7990 
7991 	dpll = DPLL_VGA_MODE_DIS;
7992 
7993 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
7994 		dpll |= DPLLB_MODE_LVDS;
7995 	else
7996 		dpll |= DPLLB_MODE_DAC_SERIAL;
7997 
7998 	if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
7999 	    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8000 		dpll |= (crtc_state->pixel_multiplier - 1)
8001 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
8002 	}
8003 
8004 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8005 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8006 		dpll |= DPLL_SDVO_HIGH_SPEED;
8007 
8008 	if (intel_crtc_has_dp_encoder(crtc_state))
8009 		dpll |= DPLL_SDVO_HIGH_SPEED;
8010 
8011 	/* compute bitmask from p1 value */
8012 	if (IS_PINEVIEW(dev_priv))
8013 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8014 	else {
8015 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8016 		if (IS_G4X(dev_priv) && reduced_clock)
8017 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8018 	}
8019 	switch (clock->p2) {
8020 	case 5:
8021 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8022 		break;
8023 	case 7:
8024 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8025 		break;
8026 	case 10:
8027 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8028 		break;
8029 	case 14:
8030 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8031 		break;
8032 	}
8033 	if (INTEL_GEN(dev_priv) >= 4)
8034 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8035 
8036 	if (crtc_state->sdvo_tv_clock)
8037 		dpll |= PLL_REF_INPUT_TVCLKINBC;
8038 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8039 		 intel_panel_use_ssc(dev_priv))
8040 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8041 	else
8042 		dpll |= PLL_REF_INPUT_DREFCLK;
8043 
8044 	dpll |= DPLL_VCO_ENABLE;
8045 	crtc_state->dpll_hw_state.dpll = dpll;
8046 
8047 	if (INTEL_GEN(dev_priv) >= 4) {
8048 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8049 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
8050 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
8051 	}
8052 }
8053 
8054 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8055 			      struct intel_crtc_state *crtc_state,
8056 			      struct dpll *reduced_clock)
8057 {
8058 	struct drm_device *dev = crtc->base.dev;
8059 	struct drm_i915_private *dev_priv = to_i915(dev);
8060 	u32 dpll;
8061 	struct dpll *clock = &crtc_state->dpll;
8062 
8063 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8064 
8065 	dpll = DPLL_VGA_MODE_DIS;
8066 
8067 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8068 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8069 	} else {
8070 		if (clock->p1 == 2)
8071 			dpll |= PLL_P1_DIVIDE_BY_TWO;
8072 		else
8073 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8074 		if (clock->p2 == 4)
8075 			dpll |= PLL_P2_DIVIDE_BY_4;
8076 	}
8077 
8078 	/*
8079 	 * Bspec:
8080 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8081 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8082 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8083 	 *  Enable) must be set to “1” in both the DPLL A Control Register
8084 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8085 	 *
8086 	 * For simplicity We simply keep both bits always enabled in
8087 	 * both DPLLS. The spec says we should disable the DVO 2X clock
8088 	 * when not needed, but this seems to work fine in practice.
8089 	 */
8090 	if (IS_I830(dev_priv) ||
8091 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8092 		dpll |= DPLL_DVO_2X_MODE;
8093 
8094 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8095 	    intel_panel_use_ssc(dev_priv))
8096 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8097 	else
8098 		dpll |= PLL_REF_INPUT_DREFCLK;
8099 
8100 	dpll |= DPLL_VCO_ENABLE;
8101 	crtc_state->dpll_hw_state.dpll = dpll;
8102 }
8103 
8104 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
8105 {
8106 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8107 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8108 	enum pipe pipe = crtc->pipe;
8109 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8110 	const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
8111 	u32 crtc_vtotal, crtc_vblank_end;
8112 	int vsyncshift = 0;
8113 
8114 	/* We need to be careful not to changed the adjusted mode, for otherwise
8115 	 * the hw state checker will get angry at the mismatch. */
8116 	crtc_vtotal = adjusted_mode->crtc_vtotal;
8117 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8118 
8119 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8120 		/* the chip adds 2 halflines automatically */
8121 		crtc_vtotal -= 1;
8122 		crtc_vblank_end -= 1;
8123 
8124 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8125 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8126 		else
8127 			vsyncshift = adjusted_mode->crtc_hsync_start -
8128 				adjusted_mode->crtc_htotal / 2;
8129 		if (vsyncshift < 0)
8130 			vsyncshift += adjusted_mode->crtc_htotal;
8131 	}
8132 
8133 	if (INTEL_GEN(dev_priv) > 3)
8134 		I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
8135 
8136 	I915_WRITE(HTOTAL(cpu_transcoder),
8137 		   (adjusted_mode->crtc_hdisplay - 1) |
8138 		   ((adjusted_mode->crtc_htotal - 1) << 16));
8139 	I915_WRITE(HBLANK(cpu_transcoder),
8140 		   (adjusted_mode->crtc_hblank_start - 1) |
8141 		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
8142 	I915_WRITE(HSYNC(cpu_transcoder),
8143 		   (adjusted_mode->crtc_hsync_start - 1) |
8144 		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
8145 
8146 	I915_WRITE(VTOTAL(cpu_transcoder),
8147 		   (adjusted_mode->crtc_vdisplay - 1) |
8148 		   ((crtc_vtotal - 1) << 16));
8149 	I915_WRITE(VBLANK(cpu_transcoder),
8150 		   (adjusted_mode->crtc_vblank_start - 1) |
8151 		   ((crtc_vblank_end - 1) << 16));
8152 	I915_WRITE(VSYNC(cpu_transcoder),
8153 		   (adjusted_mode->crtc_vsync_start - 1) |
8154 		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
8155 
8156 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8157 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8158 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8159 	 * bits. */
8160 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
8161 	    (pipe == PIPE_B || pipe == PIPE_C))
8162 		I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8163 
8164 }
8165 
8166 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
8167 {
8168 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8169 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8170 	enum pipe pipe = crtc->pipe;
8171 
8172 	/* pipesrc controls the size that is scaled from, which should
8173 	 * always be the user's requested size.
8174 	 */
8175 	I915_WRITE(PIPESRC(pipe),
8176 		   ((crtc_state->pipe_src_w - 1) << 16) |
8177 		   (crtc_state->pipe_src_h - 1));
8178 }
8179 
8180 static void intel_get_pipe_timings(struct intel_crtc *crtc,
8181 				   struct intel_crtc_state *pipe_config)
8182 {
8183 	struct drm_device *dev = crtc->base.dev;
8184 	struct drm_i915_private *dev_priv = to_i915(dev);
8185 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8186 	u32 tmp;
8187 
8188 	tmp = I915_READ(HTOTAL(cpu_transcoder));
8189 	pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8190 	pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
8191 
8192 	if (!transcoder_is_dsi(cpu_transcoder)) {
8193 		tmp = I915_READ(HBLANK(cpu_transcoder));
8194 		pipe_config->base.adjusted_mode.crtc_hblank_start =
8195 							(tmp & 0xffff) + 1;
8196 		pipe_config->base.adjusted_mode.crtc_hblank_end =
8197 						((tmp >> 16) & 0xffff) + 1;
8198 	}
8199 	tmp = I915_READ(HSYNC(cpu_transcoder));
8200 	pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8201 	pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
8202 
8203 	tmp = I915_READ(VTOTAL(cpu_transcoder));
8204 	pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8205 	pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
8206 
8207 	if (!transcoder_is_dsi(cpu_transcoder)) {
8208 		tmp = I915_READ(VBLANK(cpu_transcoder));
8209 		pipe_config->base.adjusted_mode.crtc_vblank_start =
8210 							(tmp & 0xffff) + 1;
8211 		pipe_config->base.adjusted_mode.crtc_vblank_end =
8212 						((tmp >> 16) & 0xffff) + 1;
8213 	}
8214 	tmp = I915_READ(VSYNC(cpu_transcoder));
8215 	pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8216 	pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
8217 
8218 	if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
8219 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8220 		pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8221 		pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
8222 	}
8223 }
8224 
8225 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8226 				    struct intel_crtc_state *pipe_config)
8227 {
8228 	struct drm_device *dev = crtc->base.dev;
8229 	struct drm_i915_private *dev_priv = to_i915(dev);
8230 	u32 tmp;
8231 
8232 	tmp = I915_READ(PIPESRC(crtc->pipe));
8233 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8234 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8235 
8236 	pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8237 	pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
8238 }
8239 
8240 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
8241 				 struct intel_crtc_state *pipe_config)
8242 {
8243 	mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8244 	mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8245 	mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8246 	mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
8247 
8248 	mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8249 	mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8250 	mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8251 	mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
8252 
8253 	mode->flags = pipe_config->base.adjusted_mode.flags;
8254 	mode->type = DRM_MODE_TYPE_DRIVER;
8255 
8256 	mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8257 
8258 	mode->hsync = drm_mode_hsync(mode);
8259 	mode->vrefresh = drm_mode_vrefresh(mode);
8260 	drm_mode_set_name(mode);
8261 }
8262 
8263 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
8264 {
8265 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8266 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8267 	u32 pipeconf;
8268 
8269 	pipeconf = 0;
8270 
8271 	/* we keep both pipes enabled on 830 */
8272 	if (IS_I830(dev_priv))
8273 		pipeconf |= I915_READ(PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
8274 
8275 	if (crtc_state->double_wide)
8276 		pipeconf |= PIPECONF_DOUBLE_WIDE;
8277 
8278 	/* only g4x and later have fancy bpc/dither controls */
8279 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8280 	    IS_CHERRYVIEW(dev_priv)) {
8281 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
8282 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
8283 			pipeconf |= PIPECONF_DITHER_EN |
8284 				    PIPECONF_DITHER_TYPE_SP;
8285 
8286 		switch (crtc_state->pipe_bpp) {
8287 		case 18:
8288 			pipeconf |= PIPECONF_6BPC;
8289 			break;
8290 		case 24:
8291 			pipeconf |= PIPECONF_8BPC;
8292 			break;
8293 		case 30:
8294 			pipeconf |= PIPECONF_10BPC;
8295 			break;
8296 		default:
8297 			/* Case prevented by intel_choose_pipe_bpp_dither. */
8298 			BUG();
8299 		}
8300 	}
8301 
8302 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
8303 		if (INTEL_GEN(dev_priv) < 4 ||
8304 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8305 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8306 		else
8307 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8308 	} else {
8309 		pipeconf |= PIPECONF_PROGRESSIVE;
8310 	}
8311 
8312 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
8313 	     crtc_state->limited_color_range)
8314 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
8315 
8316 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
8317 
8318 	I915_WRITE(PIPECONF(crtc->pipe), pipeconf);
8319 	POSTING_READ(PIPECONF(crtc->pipe));
8320 }
8321 
8322 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8323 				   struct intel_crtc_state *crtc_state)
8324 {
8325 	struct drm_device *dev = crtc->base.dev;
8326 	struct drm_i915_private *dev_priv = to_i915(dev);
8327 	const struct intel_limit *limit;
8328 	int refclk = 48000;
8329 
8330 	memset(&crtc_state->dpll_hw_state, 0,
8331 	       sizeof(crtc_state->dpll_hw_state));
8332 
8333 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8334 		if (intel_panel_use_ssc(dev_priv)) {
8335 			refclk = dev_priv->vbt.lvds_ssc_freq;
8336 			DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8337 		}
8338 
8339 		limit = &intel_limits_i8xx_lvds;
8340 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
8341 		limit = &intel_limits_i8xx_dvo;
8342 	} else {
8343 		limit = &intel_limits_i8xx_dac;
8344 	}
8345 
8346 	if (!crtc_state->clock_set &&
8347 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8348 				 refclk, NULL, &crtc_state->dpll)) {
8349 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8350 		return -EINVAL;
8351 	}
8352 
8353 	i8xx_compute_dpll(crtc, crtc_state, NULL);
8354 
8355 	return 0;
8356 }
8357 
8358 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8359 				  struct intel_crtc_state *crtc_state)
8360 {
8361 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8362 	const struct intel_limit *limit;
8363 	int refclk = 96000;
8364 
8365 	memset(&crtc_state->dpll_hw_state, 0,
8366 	       sizeof(crtc_state->dpll_hw_state));
8367 
8368 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8369 		if (intel_panel_use_ssc(dev_priv)) {
8370 			refclk = dev_priv->vbt.lvds_ssc_freq;
8371 			DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8372 		}
8373 
8374 		if (intel_is_dual_link_lvds(dev_priv))
8375 			limit = &intel_limits_g4x_dual_channel_lvds;
8376 		else
8377 			limit = &intel_limits_g4x_single_channel_lvds;
8378 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8379 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
8380 		limit = &intel_limits_g4x_hdmi;
8381 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
8382 		limit = &intel_limits_g4x_sdvo;
8383 	} else {
8384 		/* The option is for other outputs */
8385 		limit = &intel_limits_i9xx_sdvo;
8386 	}
8387 
8388 	if (!crtc_state->clock_set &&
8389 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8390 				refclk, NULL, &crtc_state->dpll)) {
8391 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8392 		return -EINVAL;
8393 	}
8394 
8395 	i9xx_compute_dpll(crtc, crtc_state, NULL);
8396 
8397 	return 0;
8398 }
8399 
8400 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8401 				  struct intel_crtc_state *crtc_state)
8402 {
8403 	struct drm_device *dev = crtc->base.dev;
8404 	struct drm_i915_private *dev_priv = to_i915(dev);
8405 	const struct intel_limit *limit;
8406 	int refclk = 96000;
8407 
8408 	memset(&crtc_state->dpll_hw_state, 0,
8409 	       sizeof(crtc_state->dpll_hw_state));
8410 
8411 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8412 		if (intel_panel_use_ssc(dev_priv)) {
8413 			refclk = dev_priv->vbt.lvds_ssc_freq;
8414 			DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8415 		}
8416 
8417 		limit = &intel_limits_pineview_lvds;
8418 	} else {
8419 		limit = &intel_limits_pineview_sdvo;
8420 	}
8421 
8422 	if (!crtc_state->clock_set &&
8423 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8424 				refclk, NULL, &crtc_state->dpll)) {
8425 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8426 		return -EINVAL;
8427 	}
8428 
8429 	i9xx_compute_dpll(crtc, crtc_state, NULL);
8430 
8431 	return 0;
8432 }
8433 
8434 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8435 				   struct intel_crtc_state *crtc_state)
8436 {
8437 	struct drm_device *dev = crtc->base.dev;
8438 	struct drm_i915_private *dev_priv = to_i915(dev);
8439 	const struct intel_limit *limit;
8440 	int refclk = 96000;
8441 
8442 	memset(&crtc_state->dpll_hw_state, 0,
8443 	       sizeof(crtc_state->dpll_hw_state));
8444 
8445 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8446 		if (intel_panel_use_ssc(dev_priv)) {
8447 			refclk = dev_priv->vbt.lvds_ssc_freq;
8448 			DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8449 		}
8450 
8451 		limit = &intel_limits_i9xx_lvds;
8452 	} else {
8453 		limit = &intel_limits_i9xx_sdvo;
8454 	}
8455 
8456 	if (!crtc_state->clock_set &&
8457 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8458 				 refclk, NULL, &crtc_state->dpll)) {
8459 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8460 		return -EINVAL;
8461 	}
8462 
8463 	i9xx_compute_dpll(crtc, crtc_state, NULL);
8464 
8465 	return 0;
8466 }
8467 
8468 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8469 				  struct intel_crtc_state *crtc_state)
8470 {
8471 	int refclk = 100000;
8472 	const struct intel_limit *limit = &intel_limits_chv;
8473 
8474 	memset(&crtc_state->dpll_hw_state, 0,
8475 	       sizeof(crtc_state->dpll_hw_state));
8476 
8477 	if (!crtc_state->clock_set &&
8478 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8479 				refclk, NULL, &crtc_state->dpll)) {
8480 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8481 		return -EINVAL;
8482 	}
8483 
8484 	chv_compute_dpll(crtc, crtc_state);
8485 
8486 	return 0;
8487 }
8488 
8489 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8490 				  struct intel_crtc_state *crtc_state)
8491 {
8492 	int refclk = 100000;
8493 	const struct intel_limit *limit = &intel_limits_vlv;
8494 
8495 	memset(&crtc_state->dpll_hw_state, 0,
8496 	       sizeof(crtc_state->dpll_hw_state));
8497 
8498 	if (!crtc_state->clock_set &&
8499 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8500 				refclk, NULL, &crtc_state->dpll)) {
8501 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
8502 		return -EINVAL;
8503 	}
8504 
8505 	vlv_compute_dpll(crtc, crtc_state);
8506 
8507 	return 0;
8508 }
8509 
8510 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
8511 {
8512 	if (IS_I830(dev_priv))
8513 		return false;
8514 
8515 	return INTEL_GEN(dev_priv) >= 4 ||
8516 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
8517 }
8518 
8519 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
8520 				 struct intel_crtc_state *pipe_config)
8521 {
8522 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8523 	u32 tmp;
8524 
8525 	if (!i9xx_has_pfit(dev_priv))
8526 		return;
8527 
8528 	tmp = I915_READ(PFIT_CONTROL);
8529 	if (!(tmp & PFIT_ENABLE))
8530 		return;
8531 
8532 	/* Check whether the pfit is attached to our pipe. */
8533 	if (INTEL_GEN(dev_priv) < 4) {
8534 		if (crtc->pipe != PIPE_B)
8535 			return;
8536 	} else {
8537 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8538 			return;
8539 	}
8540 
8541 	pipe_config->gmch_pfit.control = tmp;
8542 	pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8543 }
8544 
8545 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
8546 			       struct intel_crtc_state *pipe_config)
8547 {
8548 	struct drm_device *dev = crtc->base.dev;
8549 	struct drm_i915_private *dev_priv = to_i915(dev);
8550 	int pipe = pipe_config->cpu_transcoder;
8551 	struct dpll clock;
8552 	u32 mdiv;
8553 	int refclk = 100000;
8554 
8555 	/* In case of DSI, DPLL will not be used */
8556 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8557 		return;
8558 
8559 	vlv_dpio_get(dev_priv);
8560 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
8561 	vlv_dpio_put(dev_priv);
8562 
8563 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8564 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
8565 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8566 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8567 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8568 
8569 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
8570 }
8571 
8572 static void
8573 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8574 			      struct intel_initial_plane_config *plane_config)
8575 {
8576 	struct drm_device *dev = crtc->base.dev;
8577 	struct drm_i915_private *dev_priv = to_i915(dev);
8578 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8579 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
8580 	enum pipe pipe;
8581 	u32 val, base, offset;
8582 	int fourcc, pixel_format;
8583 	unsigned int aligned_height;
8584 	struct drm_framebuffer *fb;
8585 	struct intel_framebuffer *intel_fb;
8586 
8587 	if (!plane->get_hw_state(plane, &pipe))
8588 		return;
8589 
8590 	WARN_ON(pipe != crtc->pipe);
8591 
8592 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8593 	if (!intel_fb) {
8594 		DRM_DEBUG_KMS("failed to alloc fb\n");
8595 		return;
8596 	}
8597 
8598 	fb = &intel_fb->base;
8599 
8600 	fb->dev = dev;
8601 
8602 	val = I915_READ(DSPCNTR(i9xx_plane));
8603 
8604 	if (INTEL_GEN(dev_priv) >= 4) {
8605 		if (val & DISPPLANE_TILED) {
8606 			plane_config->tiling = I915_TILING_X;
8607 			fb->modifier = I915_FORMAT_MOD_X_TILED;
8608 		}
8609 
8610 		if (val & DISPPLANE_ROTATE_180)
8611 			plane_config->rotation = DRM_MODE_ROTATE_180;
8612 	}
8613 
8614 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
8615 	    val & DISPPLANE_MIRROR)
8616 		plane_config->rotation |= DRM_MODE_REFLECT_X;
8617 
8618 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
8619 	fourcc = i9xx_format_to_fourcc(pixel_format);
8620 	fb->format = drm_format_info(fourcc);
8621 
8622 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
8623 		offset = I915_READ(DSPOFFSET(i9xx_plane));
8624 		base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
8625 	} else if (INTEL_GEN(dev_priv) >= 4) {
8626 		if (plane_config->tiling)
8627 			offset = I915_READ(DSPTILEOFF(i9xx_plane));
8628 		else
8629 			offset = I915_READ(DSPLINOFF(i9xx_plane));
8630 		base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
8631 	} else {
8632 		base = I915_READ(DSPADDR(i9xx_plane));
8633 	}
8634 	plane_config->base = base;
8635 
8636 	val = I915_READ(PIPESRC(pipe));
8637 	fb->width = ((val >> 16) & 0xfff) + 1;
8638 	fb->height = ((val >> 0) & 0xfff) + 1;
8639 
8640 	val = I915_READ(DSPSTRIDE(i9xx_plane));
8641 	fb->pitches[0] = val & 0xffffffc0;
8642 
8643 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
8644 
8645 	plane_config->size = fb->pitches[0] * aligned_height;
8646 
8647 	DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8648 		      crtc->base.name, plane->base.name, fb->width, fb->height,
8649 		      fb->format->cpp[0] * 8, base, fb->pitches[0],
8650 		      plane_config->size);
8651 
8652 	plane_config->fb = intel_fb;
8653 }
8654 
8655 static void chv_crtc_clock_get(struct intel_crtc *crtc,
8656 			       struct intel_crtc_state *pipe_config)
8657 {
8658 	struct drm_device *dev = crtc->base.dev;
8659 	struct drm_i915_private *dev_priv = to_i915(dev);
8660 	int pipe = pipe_config->cpu_transcoder;
8661 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8662 	struct dpll clock;
8663 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
8664 	int refclk = 100000;
8665 
8666 	/* In case of DSI, DPLL will not be used */
8667 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8668 		return;
8669 
8670 	vlv_dpio_get(dev_priv);
8671 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8672 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8673 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8674 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
8675 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8676 	vlv_dpio_put(dev_priv);
8677 
8678 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8679 	clock.m2 = (pll_dw0 & 0xff) << 22;
8680 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8681 		clock.m2 |= pll_dw2 & 0x3fffff;
8682 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8683 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8684 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8685 
8686 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
8687 }
8688 
8689 static void intel_get_crtc_ycbcr_config(struct intel_crtc *crtc,
8690 					struct intel_crtc_state *pipe_config)
8691 {
8692 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8693 	enum intel_output_format output = INTEL_OUTPUT_FORMAT_RGB;
8694 
8695 	pipe_config->lspcon_downsampling = false;
8696 
8697 	if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
8698 		u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
8699 
8700 		if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
8701 			bool ycbcr420_enabled = tmp & PIPEMISC_YUV420_ENABLE;
8702 			bool blend = tmp & PIPEMISC_YUV420_MODE_FULL_BLEND;
8703 
8704 			if (ycbcr420_enabled) {
8705 				/* We support 4:2:0 in full blend mode only */
8706 				if (!blend)
8707 					output = INTEL_OUTPUT_FORMAT_INVALID;
8708 				else if (!(IS_GEMINILAKE(dev_priv) ||
8709 					   INTEL_GEN(dev_priv) >= 10))
8710 					output = INTEL_OUTPUT_FORMAT_INVALID;
8711 				else
8712 					output = INTEL_OUTPUT_FORMAT_YCBCR420;
8713 			} else {
8714 				/*
8715 				 * Currently there is no interface defined to
8716 				 * check user preference between RGB/YCBCR444
8717 				 * or YCBCR420. So the only possible case for
8718 				 * YCBCR444 usage is driving YCBCR420 output
8719 				 * with LSPCON, when pipe is configured for
8720 				 * YCBCR444 output and LSPCON takes care of
8721 				 * downsampling it.
8722 				 */
8723 				pipe_config->lspcon_downsampling = true;
8724 				output = INTEL_OUTPUT_FORMAT_YCBCR444;
8725 			}
8726 		}
8727 	}
8728 
8729 	pipe_config->output_format = output;
8730 }
8731 
8732 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
8733 {
8734 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
8735 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
8736 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8737 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
8738 	u32 tmp;
8739 
8740 	tmp = I915_READ(DSPCNTR(i9xx_plane));
8741 
8742 	if (tmp & DISPPLANE_GAMMA_ENABLE)
8743 		crtc_state->gamma_enable = true;
8744 
8745 	if (!HAS_GMCH(dev_priv) &&
8746 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
8747 		crtc_state->csc_enable = true;
8748 }
8749 
8750 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
8751 				 struct intel_crtc_state *pipe_config)
8752 {
8753 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8754 	enum intel_display_power_domain power_domain;
8755 	intel_wakeref_t wakeref;
8756 	u32 tmp;
8757 	bool ret;
8758 
8759 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8760 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
8761 	if (!wakeref)
8762 		return false;
8763 
8764 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
8765 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8766 	pipe_config->shared_dpll = NULL;
8767 
8768 	ret = false;
8769 
8770 	tmp = I915_READ(PIPECONF(crtc->pipe));
8771 	if (!(tmp & PIPECONF_ENABLE))
8772 		goto out;
8773 
8774 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8775 	    IS_CHERRYVIEW(dev_priv)) {
8776 		switch (tmp & PIPECONF_BPC_MASK) {
8777 		case PIPECONF_6BPC:
8778 			pipe_config->pipe_bpp = 18;
8779 			break;
8780 		case PIPECONF_8BPC:
8781 			pipe_config->pipe_bpp = 24;
8782 			break;
8783 		case PIPECONF_10BPC:
8784 			pipe_config->pipe_bpp = 30;
8785 			break;
8786 		default:
8787 			break;
8788 		}
8789 	}
8790 
8791 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
8792 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
8793 		pipe_config->limited_color_range = true;
8794 
8795 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
8796 		PIPECONF_GAMMA_MODE_SHIFT;
8797 
8798 	if (IS_CHERRYVIEW(dev_priv))
8799 		pipe_config->cgm_mode = I915_READ(CGM_PIPE_MODE(crtc->pipe));
8800 
8801 	i9xx_get_pipe_color_config(pipe_config);
8802 	intel_color_get_config(pipe_config);
8803 
8804 	if (INTEL_GEN(dev_priv) < 4)
8805 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8806 
8807 	intel_get_pipe_timings(crtc, pipe_config);
8808 	intel_get_pipe_src_size(crtc, pipe_config);
8809 
8810 	i9xx_get_pfit_config(crtc, pipe_config);
8811 
8812 	if (INTEL_GEN(dev_priv) >= 4) {
8813 		/* No way to read it out on pipes B and C */
8814 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
8815 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
8816 		else
8817 			tmp = I915_READ(DPLL_MD(crtc->pipe));
8818 		pipe_config->pixel_multiplier =
8819 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8820 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8821 		pipe_config->dpll_hw_state.dpll_md = tmp;
8822 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8823 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8824 		tmp = I915_READ(DPLL(crtc->pipe));
8825 		pipe_config->pixel_multiplier =
8826 			((tmp & SDVO_MULTIPLIER_MASK)
8827 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8828 	} else {
8829 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
8830 		 * port and will be fixed up in the encoder->get_config
8831 		 * function. */
8832 		pipe_config->pixel_multiplier = 1;
8833 	}
8834 	pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8835 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
8836 		pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8837 		pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
8838 	} else {
8839 		/* Mask out read-only status bits. */
8840 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8841 						     DPLL_PORTC_READY_MASK |
8842 						     DPLL_PORTB_READY_MASK);
8843 	}
8844 
8845 	if (IS_CHERRYVIEW(dev_priv))
8846 		chv_crtc_clock_get(crtc, pipe_config);
8847 	else if (IS_VALLEYVIEW(dev_priv))
8848 		vlv_crtc_clock_get(crtc, pipe_config);
8849 	else
8850 		i9xx_crtc_clock_get(crtc, pipe_config);
8851 
8852 	/*
8853 	 * Normally the dotclock is filled in by the encoder .get_config()
8854 	 * but in case the pipe is enabled w/o any ports we need a sane
8855 	 * default.
8856 	 */
8857 	pipe_config->base.adjusted_mode.crtc_clock =
8858 		pipe_config->port_clock / pipe_config->pixel_multiplier;
8859 
8860 	ret = true;
8861 
8862 out:
8863 	intel_display_power_put(dev_priv, power_domain, wakeref);
8864 
8865 	return ret;
8866 }
8867 
8868 static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
8869 {
8870 	struct intel_encoder *encoder;
8871 	int i;
8872 	u32 val, final;
8873 	bool has_lvds = false;
8874 	bool has_cpu_edp = false;
8875 	bool has_panel = false;
8876 	bool has_ck505 = false;
8877 	bool can_ssc = false;
8878 	bool using_ssc_source = false;
8879 
8880 	/* We need to take the global config into account */
8881 	for_each_intel_encoder(&dev_priv->drm, encoder) {
8882 		switch (encoder->type) {
8883 		case INTEL_OUTPUT_LVDS:
8884 			has_panel = true;
8885 			has_lvds = true;
8886 			break;
8887 		case INTEL_OUTPUT_EDP:
8888 			has_panel = true;
8889 			if (encoder->port == PORT_A)
8890 				has_cpu_edp = true;
8891 			break;
8892 		default:
8893 			break;
8894 		}
8895 	}
8896 
8897 	if (HAS_PCH_IBX(dev_priv)) {
8898 		has_ck505 = dev_priv->vbt.display_clock_mode;
8899 		can_ssc = has_ck505;
8900 	} else {
8901 		has_ck505 = false;
8902 		can_ssc = true;
8903 	}
8904 
8905 	/* Check if any DPLLs are using the SSC source */
8906 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8907 		u32 temp = I915_READ(PCH_DPLL(i));
8908 
8909 		if (!(temp & DPLL_VCO_ENABLE))
8910 			continue;
8911 
8912 		if ((temp & PLL_REF_INPUT_MASK) ==
8913 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
8914 			using_ssc_source = true;
8915 			break;
8916 		}
8917 	}
8918 
8919 	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
8920 		      has_panel, has_lvds, has_ck505, using_ssc_source);
8921 
8922 	/* Ironlake: try to setup display ref clock before DPLL
8923 	 * enabling. This is only under driver's control after
8924 	 * PCH B stepping, previous chipset stepping should be
8925 	 * ignoring this setting.
8926 	 */
8927 	val = I915_READ(PCH_DREF_CONTROL);
8928 
8929 	/* As we must carefully and slowly disable/enable each source in turn,
8930 	 * compute the final state we want first and check if we need to
8931 	 * make any changes at all.
8932 	 */
8933 	final = val;
8934 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
8935 	if (has_ck505)
8936 		final |= DREF_NONSPREAD_CK505_ENABLE;
8937 	else
8938 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
8939 
8940 	final &= ~DREF_SSC_SOURCE_MASK;
8941 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8942 	final &= ~DREF_SSC1_ENABLE;
8943 
8944 	if (has_panel) {
8945 		final |= DREF_SSC_SOURCE_ENABLE;
8946 
8947 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
8948 			final |= DREF_SSC1_ENABLE;
8949 
8950 		if (has_cpu_edp) {
8951 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
8952 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8953 			else
8954 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8955 		} else
8956 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8957 	} else if (using_ssc_source) {
8958 		final |= DREF_SSC_SOURCE_ENABLE;
8959 		final |= DREF_SSC1_ENABLE;
8960 	}
8961 
8962 	if (final == val)
8963 		return;
8964 
8965 	/* Always enable nonspread source */
8966 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
8967 
8968 	if (has_ck505)
8969 		val |= DREF_NONSPREAD_CK505_ENABLE;
8970 	else
8971 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
8972 
8973 	if (has_panel) {
8974 		val &= ~DREF_SSC_SOURCE_MASK;
8975 		val |= DREF_SSC_SOURCE_ENABLE;
8976 
8977 		/* SSC must be turned on before enabling the CPU output  */
8978 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8979 			DRM_DEBUG_KMS("Using SSC on panel\n");
8980 			val |= DREF_SSC1_ENABLE;
8981 		} else
8982 			val &= ~DREF_SSC1_ENABLE;
8983 
8984 		/* Get SSC going before enabling the outputs */
8985 		I915_WRITE(PCH_DREF_CONTROL, val);
8986 		POSTING_READ(PCH_DREF_CONTROL);
8987 		udelay(200);
8988 
8989 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8990 
8991 		/* Enable CPU source on CPU attached eDP */
8992 		if (has_cpu_edp) {
8993 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8994 				DRM_DEBUG_KMS("Using SSC on eDP\n");
8995 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8996 			} else
8997 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8998 		} else
8999 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9000 
9001 		I915_WRITE(PCH_DREF_CONTROL, val);
9002 		POSTING_READ(PCH_DREF_CONTROL);
9003 		udelay(200);
9004 	} else {
9005 		DRM_DEBUG_KMS("Disabling CPU source output\n");
9006 
9007 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9008 
9009 		/* Turn off CPU output */
9010 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9011 
9012 		I915_WRITE(PCH_DREF_CONTROL, val);
9013 		POSTING_READ(PCH_DREF_CONTROL);
9014 		udelay(200);
9015 
9016 		if (!using_ssc_source) {
9017 			DRM_DEBUG_KMS("Disabling SSC source\n");
9018 
9019 			/* Turn off the SSC source */
9020 			val &= ~DREF_SSC_SOURCE_MASK;
9021 			val |= DREF_SSC_SOURCE_DISABLE;
9022 
9023 			/* Turn off SSC1 */
9024 			val &= ~DREF_SSC1_ENABLE;
9025 
9026 			I915_WRITE(PCH_DREF_CONTROL, val);
9027 			POSTING_READ(PCH_DREF_CONTROL);
9028 			udelay(200);
9029 		}
9030 	}
9031 
9032 	BUG_ON(val != final);
9033 }
9034 
9035 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9036 {
9037 	u32 tmp;
9038 
9039 	tmp = I915_READ(SOUTH_CHICKEN2);
9040 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9041 	I915_WRITE(SOUTH_CHICKEN2, tmp);
9042 
9043 	if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9044 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9045 		DRM_ERROR("FDI mPHY reset assert timeout\n");
9046 
9047 	tmp = I915_READ(SOUTH_CHICKEN2);
9048 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9049 	I915_WRITE(SOUTH_CHICKEN2, tmp);
9050 
9051 	if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9052 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9053 		DRM_ERROR("FDI mPHY reset de-assert timeout\n");
9054 }
9055 
9056 /* WaMPhyProgramming:hsw */
9057 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9058 {
9059 	u32 tmp;
9060 
9061 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9062 	tmp &= ~(0xFF << 24);
9063 	tmp |= (0x12 << 24);
9064 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9065 
9066 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9067 	tmp |= (1 << 11);
9068 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9069 
9070 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9071 	tmp |= (1 << 11);
9072 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9073 
9074 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9075 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9076 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9077 
9078 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9079 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9080 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9081 
9082 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9083 	tmp &= ~(7 << 13);
9084 	tmp |= (5 << 13);
9085 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9086 
9087 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9088 	tmp &= ~(7 << 13);
9089 	tmp |= (5 << 13);
9090 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9091 
9092 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9093 	tmp &= ~0xFF;
9094 	tmp |= 0x1C;
9095 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9096 
9097 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9098 	tmp &= ~0xFF;
9099 	tmp |= 0x1C;
9100 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9101 
9102 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9103 	tmp &= ~(0xFF << 16);
9104 	tmp |= (0x1C << 16);
9105 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9106 
9107 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9108 	tmp &= ~(0xFF << 16);
9109 	tmp |= (0x1C << 16);
9110 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9111 
9112 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9113 	tmp |= (1 << 27);
9114 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9115 
9116 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9117 	tmp |= (1 << 27);
9118 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
9119 
9120 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9121 	tmp &= ~(0xF << 28);
9122 	tmp |= (4 << 28);
9123 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
9124 
9125 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9126 	tmp &= ~(0xF << 28);
9127 	tmp |= (4 << 28);
9128 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
9129 }
9130 
9131 /* Implements 3 different sequences from BSpec chapter "Display iCLK
9132  * Programming" based on the parameters passed:
9133  * - Sequence to enable CLKOUT_DP
9134  * - Sequence to enable CLKOUT_DP without spread
9135  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9136  */
9137 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9138 				 bool with_spread, bool with_fdi)
9139 {
9140 	u32 reg, tmp;
9141 
9142 	if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9143 		with_spread = true;
9144 	if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9145 	    with_fdi, "LP PCH doesn't have FDI\n"))
9146 		with_fdi = false;
9147 
9148 	mutex_lock(&dev_priv->sb_lock);
9149 
9150 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9151 	tmp &= ~SBI_SSCCTL_DISABLE;
9152 	tmp |= SBI_SSCCTL_PATHALT;
9153 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9154 
9155 	udelay(24);
9156 
9157 	if (with_spread) {
9158 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9159 		tmp &= ~SBI_SSCCTL_PATHALT;
9160 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9161 
9162 		if (with_fdi) {
9163 			lpt_reset_fdi_mphy(dev_priv);
9164 			lpt_program_fdi_mphy(dev_priv);
9165 		}
9166 	}
9167 
9168 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9169 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9170 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9171 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9172 
9173 	mutex_unlock(&dev_priv->sb_lock);
9174 }
9175 
9176 /* Sequence to disable CLKOUT_DP */
9177 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
9178 {
9179 	u32 reg, tmp;
9180 
9181 	mutex_lock(&dev_priv->sb_lock);
9182 
9183 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9184 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9185 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9186 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9187 
9188 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9189 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
9190 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
9191 			tmp |= SBI_SSCCTL_PATHALT;
9192 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9193 			udelay(32);
9194 		}
9195 		tmp |= SBI_SSCCTL_DISABLE;
9196 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9197 	}
9198 
9199 	mutex_unlock(&dev_priv->sb_lock);
9200 }
9201 
9202 #define BEND_IDX(steps) ((50 + (steps)) / 5)
9203 
9204 static const u16 sscdivintphase[] = {
9205 	[BEND_IDX( 50)] = 0x3B23,
9206 	[BEND_IDX( 45)] = 0x3B23,
9207 	[BEND_IDX( 40)] = 0x3C23,
9208 	[BEND_IDX( 35)] = 0x3C23,
9209 	[BEND_IDX( 30)] = 0x3D23,
9210 	[BEND_IDX( 25)] = 0x3D23,
9211 	[BEND_IDX( 20)] = 0x3E23,
9212 	[BEND_IDX( 15)] = 0x3E23,
9213 	[BEND_IDX( 10)] = 0x3F23,
9214 	[BEND_IDX(  5)] = 0x3F23,
9215 	[BEND_IDX(  0)] = 0x0025,
9216 	[BEND_IDX( -5)] = 0x0025,
9217 	[BEND_IDX(-10)] = 0x0125,
9218 	[BEND_IDX(-15)] = 0x0125,
9219 	[BEND_IDX(-20)] = 0x0225,
9220 	[BEND_IDX(-25)] = 0x0225,
9221 	[BEND_IDX(-30)] = 0x0325,
9222 	[BEND_IDX(-35)] = 0x0325,
9223 	[BEND_IDX(-40)] = 0x0425,
9224 	[BEND_IDX(-45)] = 0x0425,
9225 	[BEND_IDX(-50)] = 0x0525,
9226 };
9227 
9228 /*
9229  * Bend CLKOUT_DP
9230  * steps -50 to 50 inclusive, in steps of 5
9231  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9232  * change in clock period = -(steps / 10) * 5.787 ps
9233  */
9234 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9235 {
9236 	u32 tmp;
9237 	int idx = BEND_IDX(steps);
9238 
9239 	if (WARN_ON(steps % 5 != 0))
9240 		return;
9241 
9242 	if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9243 		return;
9244 
9245 	mutex_lock(&dev_priv->sb_lock);
9246 
9247 	if (steps % 10 != 0)
9248 		tmp = 0xAAAAAAAB;
9249 	else
9250 		tmp = 0x00000000;
9251 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9252 
9253 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9254 	tmp &= 0xffff0000;
9255 	tmp |= sscdivintphase[idx];
9256 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9257 
9258 	mutex_unlock(&dev_priv->sb_lock);
9259 }
9260 
9261 #undef BEND_IDX
9262 
9263 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
9264 {
9265 	u32 fuse_strap = I915_READ(FUSE_STRAP);
9266 	u32 ctl = I915_READ(SPLL_CTL);
9267 
9268 	if ((ctl & SPLL_PLL_ENABLE) == 0)
9269 		return false;
9270 
9271 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
9272 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9273 		return true;
9274 
9275 	if (IS_BROADWELL(dev_priv) &&
9276 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
9277 		return true;
9278 
9279 	return false;
9280 }
9281 
9282 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
9283 			       enum intel_dpll_id id)
9284 {
9285 	u32 fuse_strap = I915_READ(FUSE_STRAP);
9286 	u32 ctl = I915_READ(WRPLL_CTL(id));
9287 
9288 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
9289 		return false;
9290 
9291 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
9292 		return true;
9293 
9294 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
9295 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
9296 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9297 		return true;
9298 
9299 	return false;
9300 }
9301 
9302 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
9303 {
9304 	struct intel_encoder *encoder;
9305 	bool pch_ssc_in_use = false;
9306 	bool has_fdi = false;
9307 
9308 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9309 		switch (encoder->type) {
9310 		case INTEL_OUTPUT_ANALOG:
9311 			has_fdi = true;
9312 			break;
9313 		default:
9314 			break;
9315 		}
9316 	}
9317 
9318 	/*
9319 	 * The BIOS may have decided to use the PCH SSC
9320 	 * reference so we must not disable it until the
9321 	 * relevant PLLs have stopped relying on it. We'll
9322 	 * just leave the PCH SSC reference enabled in case
9323 	 * any active PLL is using it. It will get disabled
9324 	 * after runtime suspend if we don't have FDI.
9325 	 *
9326 	 * TODO: Move the whole reference clock handling
9327 	 * to the modeset sequence proper so that we can
9328 	 * actually enable/disable/reconfigure these things
9329 	 * safely. To do that we need to introduce a real
9330 	 * clock hierarchy. That would also allow us to do
9331 	 * clock bending finally.
9332 	 */
9333 	if (spll_uses_pch_ssc(dev_priv)) {
9334 		DRM_DEBUG_KMS("SPLL using PCH SSC\n");
9335 		pch_ssc_in_use = true;
9336 	}
9337 
9338 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
9339 		DRM_DEBUG_KMS("WRPLL1 using PCH SSC\n");
9340 		pch_ssc_in_use = true;
9341 	}
9342 
9343 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
9344 		DRM_DEBUG_KMS("WRPLL2 using PCH SSC\n");
9345 		pch_ssc_in_use = true;
9346 	}
9347 
9348 	if (pch_ssc_in_use)
9349 		return;
9350 
9351 	if (has_fdi) {
9352 		lpt_bend_clkout_dp(dev_priv, 0);
9353 		lpt_enable_clkout_dp(dev_priv, true, true);
9354 	} else {
9355 		lpt_disable_clkout_dp(dev_priv);
9356 	}
9357 }
9358 
9359 /*
9360  * Initialize reference clocks when the driver loads
9361  */
9362 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
9363 {
9364 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
9365 		ironlake_init_pch_refclk(dev_priv);
9366 	else if (HAS_PCH_LPT(dev_priv))
9367 		lpt_init_pch_refclk(dev_priv);
9368 }
9369 
9370 static void ironlake_set_pipeconf(const struct intel_crtc_state *crtc_state)
9371 {
9372 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
9373 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9374 	enum pipe pipe = crtc->pipe;
9375 	u32 val;
9376 
9377 	val = 0;
9378 
9379 	switch (crtc_state->pipe_bpp) {
9380 	case 18:
9381 		val |= PIPECONF_6BPC;
9382 		break;
9383 	case 24:
9384 		val |= PIPECONF_8BPC;
9385 		break;
9386 	case 30:
9387 		val |= PIPECONF_10BPC;
9388 		break;
9389 	case 36:
9390 		val |= PIPECONF_12BPC;
9391 		break;
9392 	default:
9393 		/* Case prevented by intel_choose_pipe_bpp_dither. */
9394 		BUG();
9395 	}
9396 
9397 	if (crtc_state->dither)
9398 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9399 
9400 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
9401 		val |= PIPECONF_INTERLACED_ILK;
9402 	else
9403 		val |= PIPECONF_PROGRESSIVE;
9404 
9405 	if (crtc_state->limited_color_range)
9406 		val |= PIPECONF_COLOR_RANGE_SELECT;
9407 
9408 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
9409 
9410 	I915_WRITE(PIPECONF(pipe), val);
9411 	POSTING_READ(PIPECONF(pipe));
9412 }
9413 
9414 static void haswell_set_pipeconf(const struct intel_crtc_state *crtc_state)
9415 {
9416 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
9417 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9418 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
9419 	u32 val = 0;
9420 
9421 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
9422 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9423 
9424 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
9425 		val |= PIPECONF_INTERLACED_ILK;
9426 	else
9427 		val |= PIPECONF_PROGRESSIVE;
9428 
9429 	I915_WRITE(PIPECONF(cpu_transcoder), val);
9430 	POSTING_READ(PIPECONF(cpu_transcoder));
9431 }
9432 
9433 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
9434 {
9435 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
9436 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9437 	u32 val = 0;
9438 
9439 	switch (crtc_state->pipe_bpp) {
9440 	case 18:
9441 		val |= PIPEMISC_DITHER_6_BPC;
9442 		break;
9443 	case 24:
9444 		val |= PIPEMISC_DITHER_8_BPC;
9445 		break;
9446 	case 30:
9447 		val |= PIPEMISC_DITHER_10_BPC;
9448 		break;
9449 	case 36:
9450 		val |= PIPEMISC_DITHER_12_BPC;
9451 		break;
9452 	default:
9453 		MISSING_CASE(crtc_state->pipe_bpp);
9454 		break;
9455 	}
9456 
9457 	if (crtc_state->dither)
9458 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9459 
9460 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
9461 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
9462 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
9463 
9464 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
9465 		val |= PIPEMISC_YUV420_ENABLE |
9466 			PIPEMISC_YUV420_MODE_FULL_BLEND;
9467 
9468 	if (INTEL_GEN(dev_priv) >= 11 &&
9469 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
9470 					   BIT(PLANE_CURSOR))) == 0)
9471 		val |= PIPEMISC_HDR_MODE_PRECISION;
9472 
9473 	I915_WRITE(PIPEMISC(crtc->pipe), val);
9474 }
9475 
9476 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
9477 {
9478 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9479 	u32 tmp;
9480 
9481 	tmp = I915_READ(PIPEMISC(crtc->pipe));
9482 
9483 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
9484 	case PIPEMISC_DITHER_6_BPC:
9485 		return 18;
9486 	case PIPEMISC_DITHER_8_BPC:
9487 		return 24;
9488 	case PIPEMISC_DITHER_10_BPC:
9489 		return 30;
9490 	case PIPEMISC_DITHER_12_BPC:
9491 		return 36;
9492 	default:
9493 		MISSING_CASE(tmp);
9494 		return 0;
9495 	}
9496 }
9497 
9498 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9499 {
9500 	/*
9501 	 * Account for spread spectrum to avoid
9502 	 * oversubscribing the link. Max center spread
9503 	 * is 2.5%; use 5% for safety's sake.
9504 	 */
9505 	u32 bps = target_clock * bpp * 21 / 20;
9506 	return DIV_ROUND_UP(bps, link_bw * 8);
9507 }
9508 
9509 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
9510 {
9511 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
9512 }
9513 
9514 static void ironlake_compute_dpll(struct intel_crtc *crtc,
9515 				  struct intel_crtc_state *crtc_state,
9516 				  struct dpll *reduced_clock)
9517 {
9518 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9519 	u32 dpll, fp, fp2;
9520 	int factor;
9521 
9522 	/* Enable autotuning of the PLL clock (if permissible) */
9523 	factor = 21;
9524 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9525 		if ((intel_panel_use_ssc(dev_priv) &&
9526 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
9527 		    (HAS_PCH_IBX(dev_priv) &&
9528 		     intel_is_dual_link_lvds(dev_priv)))
9529 			factor = 25;
9530 	} else if (crtc_state->sdvo_tv_clock) {
9531 		factor = 20;
9532 	}
9533 
9534 	fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9535 
9536 	if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
9537 		fp |= FP_CB_TUNE;
9538 
9539 	if (reduced_clock) {
9540 		fp2 = i9xx_dpll_compute_fp(reduced_clock);
9541 
9542 		if (reduced_clock->m < factor * reduced_clock->n)
9543 			fp2 |= FP_CB_TUNE;
9544 	} else {
9545 		fp2 = fp;
9546 	}
9547 
9548 	dpll = 0;
9549 
9550 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
9551 		dpll |= DPLLB_MODE_LVDS;
9552 	else
9553 		dpll |= DPLLB_MODE_DAC_SERIAL;
9554 
9555 	dpll |= (crtc_state->pixel_multiplier - 1)
9556 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
9557 
9558 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9559 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
9560 		dpll |= DPLL_SDVO_HIGH_SPEED;
9561 
9562 	if (intel_crtc_has_dp_encoder(crtc_state))
9563 		dpll |= DPLL_SDVO_HIGH_SPEED;
9564 
9565 	/*
9566 	 * The high speed IO clock is only really required for
9567 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9568 	 * possible to share the DPLL between CRT and HDMI. Enabling
9569 	 * the clock needlessly does no real harm, except use up a
9570 	 * bit of power potentially.
9571 	 *
9572 	 * We'll limit this to IVB with 3 pipes, since it has only two
9573 	 * DPLLs and so DPLL sharing is the only way to get three pipes
9574 	 * driving PCH ports at the same time. On SNB we could do this,
9575 	 * and potentially avoid enabling the second DPLL, but it's not
9576 	 * clear if it''s a win or loss power wise. No point in doing
9577 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9578 	 */
9579 	if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9580 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9581 		dpll |= DPLL_SDVO_HIGH_SPEED;
9582 
9583 	/* compute bitmask from p1 value */
9584 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9585 	/* also FPA1 */
9586 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
9587 
9588 	switch (crtc_state->dpll.p2) {
9589 	case 5:
9590 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9591 		break;
9592 	case 7:
9593 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9594 		break;
9595 	case 10:
9596 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9597 		break;
9598 	case 14:
9599 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9600 		break;
9601 	}
9602 
9603 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9604 	    intel_panel_use_ssc(dev_priv))
9605 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
9606 	else
9607 		dpll |= PLL_REF_INPUT_DREFCLK;
9608 
9609 	dpll |= DPLL_VCO_ENABLE;
9610 
9611 	crtc_state->dpll_hw_state.dpll = dpll;
9612 	crtc_state->dpll_hw_state.fp0 = fp;
9613 	crtc_state->dpll_hw_state.fp1 = fp2;
9614 }
9615 
9616 static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9617 				       struct intel_crtc_state *crtc_state)
9618 {
9619 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9620 	struct intel_atomic_state *state =
9621 		to_intel_atomic_state(crtc_state->base.state);
9622 	const struct intel_limit *limit;
9623 	int refclk = 120000;
9624 
9625 	memset(&crtc_state->dpll_hw_state, 0,
9626 	       sizeof(crtc_state->dpll_hw_state));
9627 
9628 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9629 	if (!crtc_state->has_pch_encoder)
9630 		return 0;
9631 
9632 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9633 		if (intel_panel_use_ssc(dev_priv)) {
9634 			DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9635 				      dev_priv->vbt.lvds_ssc_freq);
9636 			refclk = dev_priv->vbt.lvds_ssc_freq;
9637 		}
9638 
9639 		if (intel_is_dual_link_lvds(dev_priv)) {
9640 			if (refclk == 100000)
9641 				limit = &intel_limits_ironlake_dual_lvds_100m;
9642 			else
9643 				limit = &intel_limits_ironlake_dual_lvds;
9644 		} else {
9645 			if (refclk == 100000)
9646 				limit = &intel_limits_ironlake_single_lvds_100m;
9647 			else
9648 				limit = &intel_limits_ironlake_single_lvds;
9649 		}
9650 	} else {
9651 		limit = &intel_limits_ironlake_dac;
9652 	}
9653 
9654 	if (!crtc_state->clock_set &&
9655 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9656 				refclk, NULL, &crtc_state->dpll)) {
9657 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
9658 		return -EINVAL;
9659 	}
9660 
9661 	ironlake_compute_dpll(crtc, crtc_state, NULL);
9662 
9663 	if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
9664 		DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
9665 			      pipe_name(crtc->pipe));
9666 		return -EINVAL;
9667 	}
9668 
9669 	return 0;
9670 }
9671 
9672 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9673 					 struct intel_link_m_n *m_n)
9674 {
9675 	struct drm_device *dev = crtc->base.dev;
9676 	struct drm_i915_private *dev_priv = to_i915(dev);
9677 	enum pipe pipe = crtc->pipe;
9678 
9679 	m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9680 	m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9681 	m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9682 		& ~TU_SIZE_MASK;
9683 	m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9684 	m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9685 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9686 }
9687 
9688 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9689 					 enum transcoder transcoder,
9690 					 struct intel_link_m_n *m_n,
9691 					 struct intel_link_m_n *m2_n2)
9692 {
9693 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9694 	enum pipe pipe = crtc->pipe;
9695 
9696 	if (INTEL_GEN(dev_priv) >= 5) {
9697 		m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9698 		m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9699 		m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9700 			& ~TU_SIZE_MASK;
9701 		m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9702 		m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9703 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9704 
9705 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
9706 			m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9707 			m2_n2->link_n =	I915_READ(PIPE_LINK_N2(transcoder));
9708 			m2_n2->gmch_m =	I915_READ(PIPE_DATA_M2(transcoder))
9709 					& ~TU_SIZE_MASK;
9710 			m2_n2->gmch_n =	I915_READ(PIPE_DATA_N2(transcoder));
9711 			m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9712 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9713 		}
9714 	} else {
9715 		m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9716 		m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9717 		m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9718 			& ~TU_SIZE_MASK;
9719 		m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9720 		m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9721 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9722 	}
9723 }
9724 
9725 void intel_dp_get_m_n(struct intel_crtc *crtc,
9726 		      struct intel_crtc_state *pipe_config)
9727 {
9728 	if (pipe_config->has_pch_encoder)
9729 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9730 	else
9731 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9732 					     &pipe_config->dp_m_n,
9733 					     &pipe_config->dp_m2_n2);
9734 }
9735 
9736 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
9737 					struct intel_crtc_state *pipe_config)
9738 {
9739 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9740 				     &pipe_config->fdi_m_n, NULL);
9741 }
9742 
9743 static void skylake_get_pfit_config(struct intel_crtc *crtc,
9744 				    struct intel_crtc_state *pipe_config)
9745 {
9746 	struct drm_device *dev = crtc->base.dev;
9747 	struct drm_i915_private *dev_priv = to_i915(dev);
9748 	struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9749 	u32 ps_ctrl = 0;
9750 	int id = -1;
9751 	int i;
9752 
9753 	/* find scaler attached to this pipe */
9754 	for (i = 0; i < crtc->num_scalers; i++) {
9755 		ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9756 		if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9757 			id = i;
9758 			pipe_config->pch_pfit.enabled = true;
9759 			pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9760 			pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9761 			scaler_state->scalers[i].in_use = true;
9762 			break;
9763 		}
9764 	}
9765 
9766 	scaler_state->scaler_id = id;
9767 	if (id >= 0) {
9768 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9769 	} else {
9770 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
9771 	}
9772 }
9773 
9774 static void
9775 skylake_get_initial_plane_config(struct intel_crtc *crtc,
9776 				 struct intel_initial_plane_config *plane_config)
9777 {
9778 	struct drm_device *dev = crtc->base.dev;
9779 	struct drm_i915_private *dev_priv = to_i915(dev);
9780 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9781 	enum plane_id plane_id = plane->id;
9782 	enum pipe pipe;
9783 	u32 val, base, offset, stride_mult, tiling, alpha;
9784 	int fourcc, pixel_format;
9785 	unsigned int aligned_height;
9786 	struct drm_framebuffer *fb;
9787 	struct intel_framebuffer *intel_fb;
9788 
9789 	if (!plane->get_hw_state(plane, &pipe))
9790 		return;
9791 
9792 	WARN_ON(pipe != crtc->pipe);
9793 
9794 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9795 	if (!intel_fb) {
9796 		DRM_DEBUG_KMS("failed to alloc fb\n");
9797 		return;
9798 	}
9799 
9800 	fb = &intel_fb->base;
9801 
9802 	fb->dev = dev;
9803 
9804 	val = I915_READ(PLANE_CTL(pipe, plane_id));
9805 
9806 	if (INTEL_GEN(dev_priv) >= 11)
9807 		pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
9808 	else
9809 		pixel_format = val & PLANE_CTL_FORMAT_MASK;
9810 
9811 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
9812 		alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
9813 		alpha &= PLANE_COLOR_ALPHA_MASK;
9814 	} else {
9815 		alpha = val & PLANE_CTL_ALPHA_MASK;
9816 	}
9817 
9818 	fourcc = skl_format_to_fourcc(pixel_format,
9819 				      val & PLANE_CTL_ORDER_RGBX, alpha);
9820 	fb->format = drm_format_info(fourcc);
9821 
9822 	tiling = val & PLANE_CTL_TILED_MASK;
9823 	switch (tiling) {
9824 	case PLANE_CTL_TILED_LINEAR:
9825 		fb->modifier = DRM_FORMAT_MOD_LINEAR;
9826 		break;
9827 	case PLANE_CTL_TILED_X:
9828 		plane_config->tiling = I915_TILING_X;
9829 		fb->modifier = I915_FORMAT_MOD_X_TILED;
9830 		break;
9831 	case PLANE_CTL_TILED_Y:
9832 		plane_config->tiling = I915_TILING_Y;
9833 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
9834 			fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
9835 		else
9836 			fb->modifier = I915_FORMAT_MOD_Y_TILED;
9837 		break;
9838 	case PLANE_CTL_TILED_YF:
9839 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
9840 			fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
9841 		else
9842 			fb->modifier = I915_FORMAT_MOD_Yf_TILED;
9843 		break;
9844 	default:
9845 		MISSING_CASE(tiling);
9846 		goto error;
9847 	}
9848 
9849 	/*
9850 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
9851 	 * while i915 HW rotation is clockwise, thats why this swapping.
9852 	 */
9853 	switch (val & PLANE_CTL_ROTATE_MASK) {
9854 	case PLANE_CTL_ROTATE_0:
9855 		plane_config->rotation = DRM_MODE_ROTATE_0;
9856 		break;
9857 	case PLANE_CTL_ROTATE_90:
9858 		plane_config->rotation = DRM_MODE_ROTATE_270;
9859 		break;
9860 	case PLANE_CTL_ROTATE_180:
9861 		plane_config->rotation = DRM_MODE_ROTATE_180;
9862 		break;
9863 	case PLANE_CTL_ROTATE_270:
9864 		plane_config->rotation = DRM_MODE_ROTATE_90;
9865 		break;
9866 	}
9867 
9868 	if (INTEL_GEN(dev_priv) >= 10 &&
9869 	    val & PLANE_CTL_FLIP_HORIZONTAL)
9870 		plane_config->rotation |= DRM_MODE_REFLECT_X;
9871 
9872 	base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
9873 	plane_config->base = base;
9874 
9875 	offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
9876 
9877 	val = I915_READ(PLANE_SIZE(pipe, plane_id));
9878 	fb->height = ((val >> 16) & 0xfff) + 1;
9879 	fb->width = ((val >> 0) & 0x1fff) + 1;
9880 
9881 	val = I915_READ(PLANE_STRIDE(pipe, plane_id));
9882 	stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
9883 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
9884 
9885 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
9886 
9887 	plane_config->size = fb->pitches[0] * aligned_height;
9888 
9889 	DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9890 		      crtc->base.name, plane->base.name, fb->width, fb->height,
9891 		      fb->format->cpp[0] * 8, base, fb->pitches[0],
9892 		      plane_config->size);
9893 
9894 	plane_config->fb = intel_fb;
9895 	return;
9896 
9897 error:
9898 	kfree(intel_fb);
9899 }
9900 
9901 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
9902 				     struct intel_crtc_state *pipe_config)
9903 {
9904 	struct drm_device *dev = crtc->base.dev;
9905 	struct drm_i915_private *dev_priv = to_i915(dev);
9906 	u32 tmp;
9907 
9908 	tmp = I915_READ(PF_CTL(crtc->pipe));
9909 
9910 	if (tmp & PF_ENABLE) {
9911 		pipe_config->pch_pfit.enabled = true;
9912 		pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9913 		pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
9914 
9915 		/* We currently do not free assignements of panel fitters on
9916 		 * ivb/hsw (since we don't use the higher upscaling modes which
9917 		 * differentiates them) so just WARN about this case for now. */
9918 		if (IS_GEN(dev_priv, 7)) {
9919 			WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9920 				PF_PIPE_SEL_IVB(crtc->pipe));
9921 		}
9922 	}
9923 }
9924 
9925 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
9926 				     struct intel_crtc_state *pipe_config)
9927 {
9928 	struct drm_device *dev = crtc->base.dev;
9929 	struct drm_i915_private *dev_priv = to_i915(dev);
9930 	enum intel_display_power_domain power_domain;
9931 	intel_wakeref_t wakeref;
9932 	u32 tmp;
9933 	bool ret;
9934 
9935 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9936 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9937 	if (!wakeref)
9938 		return false;
9939 
9940 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9941 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9942 	pipe_config->shared_dpll = NULL;
9943 
9944 	ret = false;
9945 	tmp = I915_READ(PIPECONF(crtc->pipe));
9946 	if (!(tmp & PIPECONF_ENABLE))
9947 		goto out;
9948 
9949 	switch (tmp & PIPECONF_BPC_MASK) {
9950 	case PIPECONF_6BPC:
9951 		pipe_config->pipe_bpp = 18;
9952 		break;
9953 	case PIPECONF_8BPC:
9954 		pipe_config->pipe_bpp = 24;
9955 		break;
9956 	case PIPECONF_10BPC:
9957 		pipe_config->pipe_bpp = 30;
9958 		break;
9959 	case PIPECONF_12BPC:
9960 		pipe_config->pipe_bpp = 36;
9961 		break;
9962 	default:
9963 		break;
9964 	}
9965 
9966 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9967 		pipe_config->limited_color_range = true;
9968 
9969 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
9970 		PIPECONF_GAMMA_MODE_SHIFT;
9971 
9972 	pipe_config->csc_mode = I915_READ(PIPE_CSC_MODE(crtc->pipe));
9973 
9974 	i9xx_get_pipe_color_config(pipe_config);
9975 	intel_color_get_config(pipe_config);
9976 
9977 	if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
9978 		struct intel_shared_dpll *pll;
9979 		enum intel_dpll_id pll_id;
9980 
9981 		pipe_config->has_pch_encoder = true;
9982 
9983 		tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9984 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9985 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
9986 
9987 		ironlake_get_fdi_m_n_config(crtc, pipe_config);
9988 
9989 		if (HAS_PCH_IBX(dev_priv)) {
9990 			/*
9991 			 * The pipe->pch transcoder and pch transcoder->pll
9992 			 * mapping is fixed.
9993 			 */
9994 			pll_id = (enum intel_dpll_id) crtc->pipe;
9995 		} else {
9996 			tmp = I915_READ(PCH_DPLL_SEL);
9997 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9998 				pll_id = DPLL_ID_PCH_PLL_B;
9999 			else
10000 				pll_id= DPLL_ID_PCH_PLL_A;
10001 		}
10002 
10003 		pipe_config->shared_dpll =
10004 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
10005 		pll = pipe_config->shared_dpll;
10006 
10007 		WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
10008 						&pipe_config->dpll_hw_state));
10009 
10010 		tmp = pipe_config->dpll_hw_state.dpll;
10011 		pipe_config->pixel_multiplier =
10012 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10013 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10014 
10015 		ironlake_pch_clock_get(crtc, pipe_config);
10016 	} else {
10017 		pipe_config->pixel_multiplier = 1;
10018 	}
10019 
10020 	intel_get_pipe_timings(crtc, pipe_config);
10021 	intel_get_pipe_src_size(crtc, pipe_config);
10022 
10023 	ironlake_get_pfit_config(crtc, pipe_config);
10024 
10025 	ret = true;
10026 
10027 out:
10028 	intel_display_power_put(dev_priv, power_domain, wakeref);
10029 
10030 	return ret;
10031 }
10032 static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10033 				      struct intel_crtc_state *crtc_state)
10034 {
10035 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10036 	struct intel_atomic_state *state =
10037 		to_intel_atomic_state(crtc_state->base.state);
10038 
10039 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10040 	    INTEL_GEN(dev_priv) >= 11) {
10041 		struct intel_encoder *encoder =
10042 			intel_get_crtc_new_encoder(state, crtc_state);
10043 
10044 		if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10045 			DRM_DEBUG_KMS("failed to find PLL for pipe %c\n",
10046 				      pipe_name(crtc->pipe));
10047 			return -EINVAL;
10048 		}
10049 	}
10050 
10051 	return 0;
10052 }
10053 
10054 static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
10055 				   enum port port,
10056 				   struct intel_crtc_state *pipe_config)
10057 {
10058 	enum intel_dpll_id id;
10059 	u32 temp;
10060 
10061 	temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
10062 	id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
10063 
10064 	if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
10065 		return;
10066 
10067 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10068 }
10069 
10070 static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
10071 				enum port port,
10072 				struct intel_crtc_state *pipe_config)
10073 {
10074 	enum phy phy = intel_port_to_phy(dev_priv, port);
10075 	enum icl_port_dpll_id port_dpll_id;
10076 	enum intel_dpll_id id;
10077 	u32 temp;
10078 
10079 	if (intel_phy_is_combo(dev_priv, phy)) {
10080 		temp = I915_READ(ICL_DPCLKA_CFGCR0) &
10081 			ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10082 		id = temp >> ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10083 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10084 	} else if (intel_phy_is_tc(dev_priv, phy)) {
10085 		u32 clk_sel = I915_READ(DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
10086 
10087 		if (clk_sel == DDI_CLK_SEL_MG) {
10088 			id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
10089 								    port));
10090 			port_dpll_id = ICL_PORT_DPLL_MG_PHY;
10091 		} else {
10092 			WARN_ON(clk_sel < DDI_CLK_SEL_TBT_162);
10093 			id = DPLL_ID_ICL_TBTPLL;
10094 			port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10095 		}
10096 	} else {
10097 		WARN(1, "Invalid port %x\n", port);
10098 		return;
10099 	}
10100 
10101 	pipe_config->icl_port_dplls[port_dpll_id].pll =
10102 		intel_get_shared_dpll_by_id(dev_priv, id);
10103 
10104 	icl_set_active_port_dpll(pipe_config, port_dpll_id);
10105 }
10106 
10107 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10108 				enum port port,
10109 				struct intel_crtc_state *pipe_config)
10110 {
10111 	enum intel_dpll_id id;
10112 
10113 	switch (port) {
10114 	case PORT_A:
10115 		id = DPLL_ID_SKL_DPLL0;
10116 		break;
10117 	case PORT_B:
10118 		id = DPLL_ID_SKL_DPLL1;
10119 		break;
10120 	case PORT_C:
10121 		id = DPLL_ID_SKL_DPLL2;
10122 		break;
10123 	default:
10124 		DRM_ERROR("Incorrect port type\n");
10125 		return;
10126 	}
10127 
10128 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10129 }
10130 
10131 static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10132 				enum port port,
10133 				struct intel_crtc_state *pipe_config)
10134 {
10135 	enum intel_dpll_id id;
10136 	u32 temp;
10137 
10138 	temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10139 	id = temp >> (port * 3 + 1);
10140 
10141 	if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
10142 		return;
10143 
10144 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10145 }
10146 
10147 static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10148 				enum port port,
10149 				struct intel_crtc_state *pipe_config)
10150 {
10151 	enum intel_dpll_id id;
10152 	u32 ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
10153 
10154 	switch (ddi_pll_sel) {
10155 	case PORT_CLK_SEL_WRPLL1:
10156 		id = DPLL_ID_WRPLL1;
10157 		break;
10158 	case PORT_CLK_SEL_WRPLL2:
10159 		id = DPLL_ID_WRPLL2;
10160 		break;
10161 	case PORT_CLK_SEL_SPLL:
10162 		id = DPLL_ID_SPLL;
10163 		break;
10164 	case PORT_CLK_SEL_LCPLL_810:
10165 		id = DPLL_ID_LCPLL_810;
10166 		break;
10167 	case PORT_CLK_SEL_LCPLL_1350:
10168 		id = DPLL_ID_LCPLL_1350;
10169 		break;
10170 	case PORT_CLK_SEL_LCPLL_2700:
10171 		id = DPLL_ID_LCPLL_2700;
10172 		break;
10173 	default:
10174 		MISSING_CASE(ddi_pll_sel);
10175 		/* fall through */
10176 	case PORT_CLK_SEL_NONE:
10177 		return;
10178 	}
10179 
10180 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10181 }
10182 
10183 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10184 				     struct intel_crtc_state *pipe_config,
10185 				     u64 *power_domain_mask,
10186 				     intel_wakeref_t *wakerefs)
10187 {
10188 	struct drm_device *dev = crtc->base.dev;
10189 	struct drm_i915_private *dev_priv = to_i915(dev);
10190 	enum intel_display_power_domain power_domain;
10191 	unsigned long panel_transcoder_mask = 0;
10192 	unsigned long enabled_panel_transcoders = 0;
10193 	enum transcoder panel_transcoder;
10194 	intel_wakeref_t wf;
10195 	u32 tmp;
10196 
10197 	if (INTEL_GEN(dev_priv) >= 11)
10198 		panel_transcoder_mask |=
10199 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
10200 
10201 	if (HAS_TRANSCODER_EDP(dev_priv))
10202 		panel_transcoder_mask |= BIT(TRANSCODER_EDP);
10203 
10204 	/*
10205 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
10206 	 * and DSI transcoders handled below.
10207 	 */
10208 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10209 
10210 	/*
10211 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10212 	 * consistency and less surprising code; it's in always on power).
10213 	 */
10214 	for_each_set_bit(panel_transcoder,
10215 			 &panel_transcoder_mask,
10216 			 ARRAY_SIZE(INTEL_INFO(dev_priv)->trans_offsets)) {
10217 		bool force_thru = false;
10218 		enum pipe trans_pipe;
10219 
10220 		tmp = I915_READ(TRANS_DDI_FUNC_CTL(panel_transcoder));
10221 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
10222 			continue;
10223 
10224 		/*
10225 		 * Log all enabled ones, only use the first one.
10226 		 *
10227 		 * FIXME: This won't work for two separate DSI displays.
10228 		 */
10229 		enabled_panel_transcoders |= BIT(panel_transcoder);
10230 		if (enabled_panel_transcoders != BIT(panel_transcoder))
10231 			continue;
10232 
10233 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10234 		default:
10235 			WARN(1, "unknown pipe linked to transcoder %s\n",
10236 			     transcoder_name(panel_transcoder));
10237 			/* fall through */
10238 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
10239 			force_thru = true;
10240 			/* fall through */
10241 		case TRANS_DDI_EDP_INPUT_A_ON:
10242 			trans_pipe = PIPE_A;
10243 			break;
10244 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
10245 			trans_pipe = PIPE_B;
10246 			break;
10247 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
10248 			trans_pipe = PIPE_C;
10249 			break;
10250 		}
10251 
10252 		if (trans_pipe == crtc->pipe) {
10253 			pipe_config->cpu_transcoder = panel_transcoder;
10254 			pipe_config->pch_pfit.force_thru = force_thru;
10255 		}
10256 	}
10257 
10258 	/*
10259 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
10260 	 */
10261 	WARN_ON((enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
10262 		enabled_panel_transcoders != BIT(TRANSCODER_EDP));
10263 
10264 	power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10265 	WARN_ON(*power_domain_mask & BIT_ULL(power_domain));
10266 
10267 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10268 	if (!wf)
10269 		return false;
10270 
10271 	wakerefs[power_domain] = wf;
10272 	*power_domain_mask |= BIT_ULL(power_domain);
10273 
10274 	tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10275 
10276 	return tmp & PIPECONF_ENABLE;
10277 }
10278 
10279 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10280 					 struct intel_crtc_state *pipe_config,
10281 					 u64 *power_domain_mask,
10282 					 intel_wakeref_t *wakerefs)
10283 {
10284 	struct drm_device *dev = crtc->base.dev;
10285 	struct drm_i915_private *dev_priv = to_i915(dev);
10286 	enum intel_display_power_domain power_domain;
10287 	enum transcoder cpu_transcoder;
10288 	intel_wakeref_t wf;
10289 	enum port port;
10290 	u32 tmp;
10291 
10292 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10293 		if (port == PORT_A)
10294 			cpu_transcoder = TRANSCODER_DSI_A;
10295 		else
10296 			cpu_transcoder = TRANSCODER_DSI_C;
10297 
10298 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10299 		WARN_ON(*power_domain_mask & BIT_ULL(power_domain));
10300 
10301 		wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10302 		if (!wf)
10303 			continue;
10304 
10305 		wakerefs[power_domain] = wf;
10306 		*power_domain_mask |= BIT_ULL(power_domain);
10307 
10308 		/*
10309 		 * The PLL needs to be enabled with a valid divider
10310 		 * configuration, otherwise accessing DSI registers will hang
10311 		 * the machine. See BSpec North Display Engine
10312 		 * registers/MIPI[BXT]. We can break out here early, since we
10313 		 * need the same DSI PLL to be enabled for both DSI ports.
10314 		 */
10315 		if (!bxt_dsi_pll_is_enabled(dev_priv))
10316 			break;
10317 
10318 		/* XXX: this works for video mode only */
10319 		tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10320 		if (!(tmp & DPI_ENABLE))
10321 			continue;
10322 
10323 		tmp = I915_READ(MIPI_CTRL(port));
10324 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10325 			continue;
10326 
10327 		pipe_config->cpu_transcoder = cpu_transcoder;
10328 		break;
10329 	}
10330 
10331 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
10332 }
10333 
10334 static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
10335 				       struct intel_crtc_state *pipe_config)
10336 {
10337 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10338 	struct intel_shared_dpll *pll;
10339 	enum port port;
10340 	u32 tmp;
10341 
10342 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10343 
10344 	if (INTEL_GEN(dev_priv) >= 12)
10345 		port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
10346 	else
10347 		port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
10348 
10349 	if (INTEL_GEN(dev_priv) >= 11)
10350 		icelake_get_ddi_pll(dev_priv, port, pipe_config);
10351 	else if (IS_CANNONLAKE(dev_priv))
10352 		cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
10353 	else if (IS_GEN9_BC(dev_priv))
10354 		skylake_get_ddi_pll(dev_priv, port, pipe_config);
10355 	else if (IS_GEN9_LP(dev_priv))
10356 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
10357 	else
10358 		haswell_get_ddi_pll(dev_priv, port, pipe_config);
10359 
10360 	pll = pipe_config->shared_dpll;
10361 	if (pll) {
10362 		WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
10363 						&pipe_config->dpll_hw_state));
10364 	}
10365 
10366 	/*
10367 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10368 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
10369 	 * the PCH transcoder is on.
10370 	 */
10371 	if (INTEL_GEN(dev_priv) < 9 &&
10372 	    (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
10373 		pipe_config->has_pch_encoder = true;
10374 
10375 		tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10376 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10377 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
10378 
10379 		ironlake_get_fdi_m_n_config(crtc, pipe_config);
10380 	}
10381 }
10382 
10383 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
10384 				    struct intel_crtc_state *pipe_config)
10385 {
10386 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10387 	intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
10388 	enum intel_display_power_domain power_domain;
10389 	u64 power_domain_mask;
10390 	bool active;
10391 
10392 	intel_crtc_init_scalers(crtc, pipe_config);
10393 
10394 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10395 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10396 	if (!wf)
10397 		return false;
10398 
10399 	wakerefs[power_domain] = wf;
10400 	power_domain_mask = BIT_ULL(power_domain);
10401 
10402 	pipe_config->shared_dpll = NULL;
10403 
10404 	active = hsw_get_transcoder_state(crtc, pipe_config,
10405 					  &power_domain_mask, wakerefs);
10406 
10407 	if (IS_GEN9_LP(dev_priv) &&
10408 	    bxt_get_dsi_transcoder_state(crtc, pipe_config,
10409 					 &power_domain_mask, wakerefs)) {
10410 		WARN_ON(active);
10411 		active = true;
10412 	}
10413 
10414 	if (!active)
10415 		goto out;
10416 
10417 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
10418 	    INTEL_GEN(dev_priv) >= 11) {
10419 		haswell_get_ddi_port_state(crtc, pipe_config);
10420 		intel_get_pipe_timings(crtc, pipe_config);
10421 	}
10422 
10423 	intel_get_pipe_src_size(crtc, pipe_config);
10424 	intel_get_crtc_ycbcr_config(crtc, pipe_config);
10425 
10426 	pipe_config->gamma_mode = I915_READ(GAMMA_MODE(crtc->pipe));
10427 
10428 	pipe_config->csc_mode = I915_READ(PIPE_CSC_MODE(crtc->pipe));
10429 
10430 	if (INTEL_GEN(dev_priv) >= 9) {
10431 		u32 tmp = I915_READ(SKL_BOTTOM_COLOR(crtc->pipe));
10432 
10433 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
10434 			pipe_config->gamma_enable = true;
10435 
10436 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
10437 			pipe_config->csc_enable = true;
10438 	} else {
10439 		i9xx_get_pipe_color_config(pipe_config);
10440 	}
10441 
10442 	intel_color_get_config(pipe_config);
10443 
10444 	power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10445 	WARN_ON(power_domain_mask & BIT_ULL(power_domain));
10446 
10447 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10448 	if (wf) {
10449 		wakerefs[power_domain] = wf;
10450 		power_domain_mask |= BIT_ULL(power_domain);
10451 
10452 		if (INTEL_GEN(dev_priv) >= 9)
10453 			skylake_get_pfit_config(crtc, pipe_config);
10454 		else
10455 			ironlake_get_pfit_config(crtc, pipe_config);
10456 	}
10457 
10458 	if (hsw_crtc_supports_ips(crtc)) {
10459 		if (IS_HASWELL(dev_priv))
10460 			pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
10461 		else {
10462 			/*
10463 			 * We cannot readout IPS state on broadwell, set to
10464 			 * true so we can set it to a defined state on first
10465 			 * commit.
10466 			 */
10467 			pipe_config->ips_enabled = true;
10468 		}
10469 	}
10470 
10471 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10472 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
10473 		pipe_config->pixel_multiplier =
10474 			I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10475 	} else {
10476 		pipe_config->pixel_multiplier = 1;
10477 	}
10478 
10479 out:
10480 	for_each_power_domain(power_domain, power_domain_mask)
10481 		intel_display_power_put(dev_priv,
10482 					power_domain, wakerefs[power_domain]);
10483 
10484 	return active;
10485 }
10486 
10487 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
10488 {
10489 	struct drm_i915_private *dev_priv =
10490 		to_i915(plane_state->base.plane->dev);
10491 	const struct drm_framebuffer *fb = plane_state->base.fb;
10492 	const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10493 	u32 base;
10494 
10495 	if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
10496 		base = obj->phys_handle->busaddr;
10497 	else
10498 		base = intel_plane_ggtt_offset(plane_state);
10499 
10500 	base += plane_state->color_plane[0].offset;
10501 
10502 	/* ILK+ do this automagically */
10503 	if (HAS_GMCH(dev_priv) &&
10504 	    plane_state->base.rotation & DRM_MODE_ROTATE_180)
10505 		base += (plane_state->base.crtc_h *
10506 			 plane_state->base.crtc_w - 1) * fb->format->cpp[0];
10507 
10508 	return base;
10509 }
10510 
10511 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
10512 {
10513 	int x = plane_state->base.crtc_x;
10514 	int y = plane_state->base.crtc_y;
10515 	u32 pos = 0;
10516 
10517 	if (x < 0) {
10518 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10519 		x = -x;
10520 	}
10521 	pos |= x << CURSOR_X_SHIFT;
10522 
10523 	if (y < 0) {
10524 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10525 		y = -y;
10526 	}
10527 	pos |= y << CURSOR_Y_SHIFT;
10528 
10529 	return pos;
10530 }
10531 
10532 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
10533 {
10534 	const struct drm_mode_config *config =
10535 		&plane_state->base.plane->dev->mode_config;
10536 	int width = plane_state->base.crtc_w;
10537 	int height = plane_state->base.crtc_h;
10538 
10539 	return width > 0 && width <= config->cursor_width &&
10540 		height > 0 && height <= config->cursor_height;
10541 }
10542 
10543 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
10544 {
10545 	int src_x, src_y;
10546 	u32 offset;
10547 	int ret;
10548 
10549 	ret = intel_plane_compute_gtt(plane_state);
10550 	if (ret)
10551 		return ret;
10552 
10553 	if (!plane_state->base.visible)
10554 		return 0;
10555 
10556 	src_x = plane_state->base.src_x >> 16;
10557 	src_y = plane_state->base.src_y >> 16;
10558 
10559 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
10560 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
10561 						    plane_state, 0);
10562 
10563 	if (src_x != 0 || src_y != 0) {
10564 		DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
10565 		return -EINVAL;
10566 	}
10567 
10568 	plane_state->color_plane[0].offset = offset;
10569 
10570 	return 0;
10571 }
10572 
10573 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
10574 			      struct intel_plane_state *plane_state)
10575 {
10576 	const struct drm_framebuffer *fb = plane_state->base.fb;
10577 	int ret;
10578 
10579 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
10580 		DRM_DEBUG_KMS("cursor cannot be tiled\n");
10581 		return -EINVAL;
10582 	}
10583 
10584 	ret = drm_atomic_helper_check_plane_state(&plane_state->base,
10585 						  &crtc_state->base,
10586 						  DRM_PLANE_HELPER_NO_SCALING,
10587 						  DRM_PLANE_HELPER_NO_SCALING,
10588 						  true, true);
10589 	if (ret)
10590 		return ret;
10591 
10592 	ret = intel_cursor_check_surface(plane_state);
10593 	if (ret)
10594 		return ret;
10595 
10596 	if (!plane_state->base.visible)
10597 		return 0;
10598 
10599 	ret = intel_plane_check_src_coordinates(plane_state);
10600 	if (ret)
10601 		return ret;
10602 
10603 	return 0;
10604 }
10605 
10606 static unsigned int
10607 i845_cursor_max_stride(struct intel_plane *plane,
10608 		       u32 pixel_format, u64 modifier,
10609 		       unsigned int rotation)
10610 {
10611 	return 2048;
10612 }
10613 
10614 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
10615 {
10616 	u32 cntl = 0;
10617 
10618 	if (crtc_state->gamma_enable)
10619 		cntl |= CURSOR_GAMMA_ENABLE;
10620 
10621 	return cntl;
10622 }
10623 
10624 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
10625 			   const struct intel_plane_state *plane_state)
10626 {
10627 	return CURSOR_ENABLE |
10628 		CURSOR_FORMAT_ARGB |
10629 		CURSOR_STRIDE(plane_state->color_plane[0].stride);
10630 }
10631 
10632 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
10633 {
10634 	int width = plane_state->base.crtc_w;
10635 
10636 	/*
10637 	 * 845g/865g are only limited by the width of their cursors,
10638 	 * the height is arbitrary up to the precision of the register.
10639 	 */
10640 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
10641 }
10642 
10643 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
10644 			     struct intel_plane_state *plane_state)
10645 {
10646 	const struct drm_framebuffer *fb = plane_state->base.fb;
10647 	int ret;
10648 
10649 	ret = intel_check_cursor(crtc_state, plane_state);
10650 	if (ret)
10651 		return ret;
10652 
10653 	/* if we want to turn off the cursor ignore width and height */
10654 	if (!fb)
10655 		return 0;
10656 
10657 	/* Check for which cursor types we support */
10658 	if (!i845_cursor_size_ok(plane_state)) {
10659 		DRM_DEBUG("Cursor dimension %dx%d not supported\n",
10660 			  plane_state->base.crtc_w,
10661 			  plane_state->base.crtc_h);
10662 		return -EINVAL;
10663 	}
10664 
10665 	WARN_ON(plane_state->base.visible &&
10666 		plane_state->color_plane[0].stride != fb->pitches[0]);
10667 
10668 	switch (fb->pitches[0]) {
10669 	case 256:
10670 	case 512:
10671 	case 1024:
10672 	case 2048:
10673 		break;
10674 	default:
10675 		DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
10676 			      fb->pitches[0]);
10677 		return -EINVAL;
10678 	}
10679 
10680 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
10681 
10682 	return 0;
10683 }
10684 
10685 static void i845_update_cursor(struct intel_plane *plane,
10686 			       const struct intel_crtc_state *crtc_state,
10687 			       const struct intel_plane_state *plane_state)
10688 {
10689 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10690 	u32 cntl = 0, base = 0, pos = 0, size = 0;
10691 	unsigned long irqflags;
10692 
10693 	if (plane_state && plane_state->base.visible) {
10694 		unsigned int width = plane_state->base.crtc_w;
10695 		unsigned int height = plane_state->base.crtc_h;
10696 
10697 		cntl = plane_state->ctl |
10698 			i845_cursor_ctl_crtc(crtc_state);
10699 
10700 		size = (height << 12) | width;
10701 
10702 		base = intel_cursor_base(plane_state);
10703 		pos = intel_cursor_position(plane_state);
10704 	}
10705 
10706 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10707 
10708 	/* On these chipsets we can only modify the base/size/stride
10709 	 * whilst the cursor is disabled.
10710 	 */
10711 	if (plane->cursor.base != base ||
10712 	    plane->cursor.size != size ||
10713 	    plane->cursor.cntl != cntl) {
10714 		I915_WRITE_FW(CURCNTR(PIPE_A), 0);
10715 		I915_WRITE_FW(CURBASE(PIPE_A), base);
10716 		I915_WRITE_FW(CURSIZE, size);
10717 		I915_WRITE_FW(CURPOS(PIPE_A), pos);
10718 		I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
10719 
10720 		plane->cursor.base = base;
10721 		plane->cursor.size = size;
10722 		plane->cursor.cntl = cntl;
10723 	} else {
10724 		I915_WRITE_FW(CURPOS(PIPE_A), pos);
10725 	}
10726 
10727 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
10728 }
10729 
10730 static void i845_disable_cursor(struct intel_plane *plane,
10731 				const struct intel_crtc_state *crtc_state)
10732 {
10733 	i845_update_cursor(plane, crtc_state, NULL);
10734 }
10735 
10736 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
10737 				     enum pipe *pipe)
10738 {
10739 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10740 	enum intel_display_power_domain power_domain;
10741 	intel_wakeref_t wakeref;
10742 	bool ret;
10743 
10744 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
10745 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10746 	if (!wakeref)
10747 		return false;
10748 
10749 	ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
10750 
10751 	*pipe = PIPE_A;
10752 
10753 	intel_display_power_put(dev_priv, power_domain, wakeref);
10754 
10755 	return ret;
10756 }
10757 
10758 static unsigned int
10759 i9xx_cursor_max_stride(struct intel_plane *plane,
10760 		       u32 pixel_format, u64 modifier,
10761 		       unsigned int rotation)
10762 {
10763 	return plane->base.dev->mode_config.cursor_width * 4;
10764 }
10765 
10766 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
10767 {
10768 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
10769 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10770 	u32 cntl = 0;
10771 
10772 	if (INTEL_GEN(dev_priv) >= 11)
10773 		return cntl;
10774 
10775 	if (crtc_state->gamma_enable)
10776 		cntl = MCURSOR_GAMMA_ENABLE;
10777 
10778 	if (crtc_state->csc_enable)
10779 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
10780 
10781 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
10782 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
10783 
10784 	return cntl;
10785 }
10786 
10787 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
10788 			   const struct intel_plane_state *plane_state)
10789 {
10790 	struct drm_i915_private *dev_priv =
10791 		to_i915(plane_state->base.plane->dev);
10792 	u32 cntl = 0;
10793 
10794 	if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
10795 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
10796 
10797 	switch (plane_state->base.crtc_w) {
10798 	case 64:
10799 		cntl |= MCURSOR_MODE_64_ARGB_AX;
10800 		break;
10801 	case 128:
10802 		cntl |= MCURSOR_MODE_128_ARGB_AX;
10803 		break;
10804 	case 256:
10805 		cntl |= MCURSOR_MODE_256_ARGB_AX;
10806 		break;
10807 	default:
10808 		MISSING_CASE(plane_state->base.crtc_w);
10809 		return 0;
10810 	}
10811 
10812 	if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
10813 		cntl |= MCURSOR_ROTATE_180;
10814 
10815 	return cntl;
10816 }
10817 
10818 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
10819 {
10820 	struct drm_i915_private *dev_priv =
10821 		to_i915(plane_state->base.plane->dev);
10822 	int width = plane_state->base.crtc_w;
10823 	int height = plane_state->base.crtc_h;
10824 
10825 	if (!intel_cursor_size_ok(plane_state))
10826 		return false;
10827 
10828 	/* Cursor width is limited to a few power-of-two sizes */
10829 	switch (width) {
10830 	case 256:
10831 	case 128:
10832 	case 64:
10833 		break;
10834 	default:
10835 		return false;
10836 	}
10837 
10838 	/*
10839 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
10840 	 * height from 8 lines up to the cursor width, when the
10841 	 * cursor is not rotated. Everything else requires square
10842 	 * cursors.
10843 	 */
10844 	if (HAS_CUR_FBC(dev_priv) &&
10845 	    plane_state->base.rotation & DRM_MODE_ROTATE_0) {
10846 		if (height < 8 || height > width)
10847 			return false;
10848 	} else {
10849 		if (height != width)
10850 			return false;
10851 	}
10852 
10853 	return true;
10854 }
10855 
10856 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
10857 			     struct intel_plane_state *plane_state)
10858 {
10859 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
10860 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10861 	const struct drm_framebuffer *fb = plane_state->base.fb;
10862 	enum pipe pipe = plane->pipe;
10863 	int ret;
10864 
10865 	ret = intel_check_cursor(crtc_state, plane_state);
10866 	if (ret)
10867 		return ret;
10868 
10869 	/* if we want to turn off the cursor ignore width and height */
10870 	if (!fb)
10871 		return 0;
10872 
10873 	/* Check for which cursor types we support */
10874 	if (!i9xx_cursor_size_ok(plane_state)) {
10875 		DRM_DEBUG("Cursor dimension %dx%d not supported\n",
10876 			  plane_state->base.crtc_w,
10877 			  plane_state->base.crtc_h);
10878 		return -EINVAL;
10879 	}
10880 
10881 	WARN_ON(plane_state->base.visible &&
10882 		plane_state->color_plane[0].stride != fb->pitches[0]);
10883 
10884 	if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
10885 		DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
10886 			      fb->pitches[0], plane_state->base.crtc_w);
10887 		return -EINVAL;
10888 	}
10889 
10890 	/*
10891 	 * There's something wrong with the cursor on CHV pipe C.
10892 	 * If it straddles the left edge of the screen then
10893 	 * moving it away from the edge or disabling it often
10894 	 * results in a pipe underrun, and often that can lead to
10895 	 * dead pipe (constant underrun reported, and it scans
10896 	 * out just a solid color). To recover from that, the
10897 	 * display power well must be turned off and on again.
10898 	 * Refuse the put the cursor into that compromised position.
10899 	 */
10900 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
10901 	    plane_state->base.visible && plane_state->base.crtc_x < 0) {
10902 		DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
10903 		return -EINVAL;
10904 	}
10905 
10906 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
10907 
10908 	return 0;
10909 }
10910 
10911 static void i9xx_update_cursor(struct intel_plane *plane,
10912 			       const struct intel_crtc_state *crtc_state,
10913 			       const struct intel_plane_state *plane_state)
10914 {
10915 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10916 	enum pipe pipe = plane->pipe;
10917 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
10918 	unsigned long irqflags;
10919 
10920 	if (plane_state && plane_state->base.visible) {
10921 		cntl = plane_state->ctl |
10922 			i9xx_cursor_ctl_crtc(crtc_state);
10923 
10924 		if (plane_state->base.crtc_h != plane_state->base.crtc_w)
10925 			fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
10926 
10927 		base = intel_cursor_base(plane_state);
10928 		pos = intel_cursor_position(plane_state);
10929 	}
10930 
10931 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
10932 
10933 	/*
10934 	 * On some platforms writing CURCNTR first will also
10935 	 * cause CURPOS to be armed by the CURBASE write.
10936 	 * Without the CURCNTR write the CURPOS write would
10937 	 * arm itself. Thus we always update CURCNTR before
10938 	 * CURPOS.
10939 	 *
10940 	 * On other platforms CURPOS always requires the
10941 	 * CURBASE write to arm the update. Additonally
10942 	 * a write to any of the cursor register will cancel
10943 	 * an already armed cursor update. Thus leaving out
10944 	 * the CURBASE write after CURPOS could lead to a
10945 	 * cursor that doesn't appear to move, or even change
10946 	 * shape. Thus we always write CURBASE.
10947 	 *
10948 	 * The other registers are armed by by the CURBASE write
10949 	 * except when the plane is getting enabled at which time
10950 	 * the CURCNTR write arms the update.
10951 	 */
10952 
10953 	if (INTEL_GEN(dev_priv) >= 9)
10954 		skl_write_cursor_wm(plane, crtc_state);
10955 
10956 	if (plane->cursor.base != base ||
10957 	    plane->cursor.size != fbc_ctl ||
10958 	    plane->cursor.cntl != cntl) {
10959 		if (HAS_CUR_FBC(dev_priv))
10960 			I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
10961 		I915_WRITE_FW(CURCNTR(pipe), cntl);
10962 		I915_WRITE_FW(CURPOS(pipe), pos);
10963 		I915_WRITE_FW(CURBASE(pipe), base);
10964 
10965 		plane->cursor.base = base;
10966 		plane->cursor.size = fbc_ctl;
10967 		plane->cursor.cntl = cntl;
10968 	} else {
10969 		I915_WRITE_FW(CURPOS(pipe), pos);
10970 		I915_WRITE_FW(CURBASE(pipe), base);
10971 	}
10972 
10973 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
10974 }
10975 
10976 static void i9xx_disable_cursor(struct intel_plane *plane,
10977 				const struct intel_crtc_state *crtc_state)
10978 {
10979 	i9xx_update_cursor(plane, crtc_state, NULL);
10980 }
10981 
10982 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
10983 				     enum pipe *pipe)
10984 {
10985 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10986 	enum intel_display_power_domain power_domain;
10987 	intel_wakeref_t wakeref;
10988 	bool ret;
10989 	u32 val;
10990 
10991 	/*
10992 	 * Not 100% correct for planes that can move between pipes,
10993 	 * but that's only the case for gen2-3 which don't have any
10994 	 * display power wells.
10995 	 */
10996 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
10997 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10998 	if (!wakeref)
10999 		return false;
11000 
11001 	val = I915_READ(CURCNTR(plane->pipe));
11002 
11003 	ret = val & MCURSOR_MODE;
11004 
11005 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
11006 		*pipe = plane->pipe;
11007 	else
11008 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
11009 			MCURSOR_PIPE_SELECT_SHIFT;
11010 
11011 	intel_display_power_put(dev_priv, power_domain, wakeref);
11012 
11013 	return ret;
11014 }
11015 
11016 /* VESA 640x480x72Hz mode to set on the pipe */
11017 static const struct drm_display_mode load_detect_mode = {
11018 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11019 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11020 };
11021 
11022 struct drm_framebuffer *
11023 intel_framebuffer_create(struct drm_i915_gem_object *obj,
11024 			 struct drm_mode_fb_cmd2 *mode_cmd)
11025 {
11026 	struct intel_framebuffer *intel_fb;
11027 	int ret;
11028 
11029 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
11030 	if (!intel_fb)
11031 		return ERR_PTR(-ENOMEM);
11032 
11033 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
11034 	if (ret)
11035 		goto err;
11036 
11037 	return &intel_fb->base;
11038 
11039 err:
11040 	kfree(intel_fb);
11041 	return ERR_PTR(ret);
11042 }
11043 
11044 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
11045 					struct drm_crtc *crtc)
11046 {
11047 	struct drm_plane *plane;
11048 	struct drm_plane_state *plane_state;
11049 	int ret, i;
11050 
11051 	ret = drm_atomic_add_affected_planes(state, crtc);
11052 	if (ret)
11053 		return ret;
11054 
11055 	for_each_new_plane_in_state(state, plane, plane_state, i) {
11056 		if (plane_state->crtc != crtc)
11057 			continue;
11058 
11059 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
11060 		if (ret)
11061 			return ret;
11062 
11063 		drm_atomic_set_fb_for_plane(plane_state, NULL);
11064 	}
11065 
11066 	return 0;
11067 }
11068 
11069 int intel_get_load_detect_pipe(struct drm_connector *connector,
11070 			       const struct drm_display_mode *mode,
11071 			       struct intel_load_detect_pipe *old,
11072 			       struct drm_modeset_acquire_ctx *ctx)
11073 {
11074 	struct intel_crtc *intel_crtc;
11075 	struct intel_encoder *intel_encoder =
11076 		intel_attached_encoder(connector);
11077 	struct drm_crtc *possible_crtc;
11078 	struct drm_encoder *encoder = &intel_encoder->base;
11079 	struct drm_crtc *crtc = NULL;
11080 	struct drm_device *dev = encoder->dev;
11081 	struct drm_i915_private *dev_priv = to_i915(dev);
11082 	struct drm_mode_config *config = &dev->mode_config;
11083 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
11084 	struct drm_connector_state *connector_state;
11085 	struct intel_crtc_state *crtc_state;
11086 	int ret, i = -1;
11087 
11088 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
11089 		      connector->base.id, connector->name,
11090 		      encoder->base.id, encoder->name);
11091 
11092 	old->restore_state = NULL;
11093 
11094 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
11095 
11096 	/*
11097 	 * Algorithm gets a little messy:
11098 	 *
11099 	 *   - if the connector already has an assigned crtc, use it (but make
11100 	 *     sure it's on first)
11101 	 *
11102 	 *   - try to find the first unused crtc that can drive this connector,
11103 	 *     and use that if we find one
11104 	 */
11105 
11106 	/* See if we already have a CRTC for this connector */
11107 	if (connector->state->crtc) {
11108 		crtc = connector->state->crtc;
11109 
11110 		ret = drm_modeset_lock(&crtc->mutex, ctx);
11111 		if (ret)
11112 			goto fail;
11113 
11114 		/* Make sure the crtc and connector are running */
11115 		goto found;
11116 	}
11117 
11118 	/* Find an unused one (if possible) */
11119 	for_each_crtc(dev, possible_crtc) {
11120 		i++;
11121 		if (!(encoder->possible_crtcs & (1 << i)))
11122 			continue;
11123 
11124 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11125 		if (ret)
11126 			goto fail;
11127 
11128 		if (possible_crtc->state->enable) {
11129 			drm_modeset_unlock(&possible_crtc->mutex);
11130 			continue;
11131 		}
11132 
11133 		crtc = possible_crtc;
11134 		break;
11135 	}
11136 
11137 	/*
11138 	 * If we didn't find an unused CRTC, don't use any.
11139 	 */
11140 	if (!crtc) {
11141 		DRM_DEBUG_KMS("no pipe available for load-detect\n");
11142 		ret = -ENODEV;
11143 		goto fail;
11144 	}
11145 
11146 found:
11147 	intel_crtc = to_intel_crtc(crtc);
11148 
11149 	state = drm_atomic_state_alloc(dev);
11150 	restore_state = drm_atomic_state_alloc(dev);
11151 	if (!state || !restore_state) {
11152 		ret = -ENOMEM;
11153 		goto fail;
11154 	}
11155 
11156 	state->acquire_ctx = ctx;
11157 	restore_state->acquire_ctx = ctx;
11158 
11159 	connector_state = drm_atomic_get_connector_state(state, connector);
11160 	if (IS_ERR(connector_state)) {
11161 		ret = PTR_ERR(connector_state);
11162 		goto fail;
11163 	}
11164 
11165 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11166 	if (ret)
11167 		goto fail;
11168 
11169 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11170 	if (IS_ERR(crtc_state)) {
11171 		ret = PTR_ERR(crtc_state);
11172 		goto fail;
11173 	}
11174 
11175 	crtc_state->base.active = crtc_state->base.enable = true;
11176 
11177 	if (!mode)
11178 		mode = &load_detect_mode;
11179 
11180 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11181 	if (ret)
11182 		goto fail;
11183 
11184 	ret = intel_modeset_disable_planes(state, crtc);
11185 	if (ret)
11186 		goto fail;
11187 
11188 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11189 	if (!ret)
11190 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11191 	if (!ret)
11192 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
11193 	if (ret) {
11194 		DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11195 		goto fail;
11196 	}
11197 
11198 	ret = drm_atomic_commit(state);
11199 	if (ret) {
11200 		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
11201 		goto fail;
11202 	}
11203 
11204 	old->restore_state = restore_state;
11205 	drm_atomic_state_put(state);
11206 
11207 	/* let the connector get through one full cycle before testing */
11208 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
11209 	return true;
11210 
11211 fail:
11212 	if (state) {
11213 		drm_atomic_state_put(state);
11214 		state = NULL;
11215 	}
11216 	if (restore_state) {
11217 		drm_atomic_state_put(restore_state);
11218 		restore_state = NULL;
11219 	}
11220 
11221 	if (ret == -EDEADLK)
11222 		return ret;
11223 
11224 	return false;
11225 }
11226 
11227 void intel_release_load_detect_pipe(struct drm_connector *connector,
11228 				    struct intel_load_detect_pipe *old,
11229 				    struct drm_modeset_acquire_ctx *ctx)
11230 {
11231 	struct intel_encoder *intel_encoder =
11232 		intel_attached_encoder(connector);
11233 	struct drm_encoder *encoder = &intel_encoder->base;
11234 	struct drm_atomic_state *state = old->restore_state;
11235 	int ret;
11236 
11237 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
11238 		      connector->base.id, connector->name,
11239 		      encoder->base.id, encoder->name);
11240 
11241 	if (!state)
11242 		return;
11243 
11244 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
11245 	if (ret)
11246 		DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
11247 	drm_atomic_state_put(state);
11248 }
11249 
11250 static int i9xx_pll_refclk(struct drm_device *dev,
11251 			   const struct intel_crtc_state *pipe_config)
11252 {
11253 	struct drm_i915_private *dev_priv = to_i915(dev);
11254 	u32 dpll = pipe_config->dpll_hw_state.dpll;
11255 
11256 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
11257 		return dev_priv->vbt.lvds_ssc_freq;
11258 	else if (HAS_PCH_SPLIT(dev_priv))
11259 		return 120000;
11260 	else if (!IS_GEN(dev_priv, 2))
11261 		return 96000;
11262 	else
11263 		return 48000;
11264 }
11265 
11266 /* Returns the clock of the currently programmed mode of the given pipe. */
11267 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
11268 				struct intel_crtc_state *pipe_config)
11269 {
11270 	struct drm_device *dev = crtc->base.dev;
11271 	struct drm_i915_private *dev_priv = to_i915(dev);
11272 	int pipe = pipe_config->cpu_transcoder;
11273 	u32 dpll = pipe_config->dpll_hw_state.dpll;
11274 	u32 fp;
11275 	struct dpll clock;
11276 	int port_clock;
11277 	int refclk = i9xx_pll_refclk(dev, pipe_config);
11278 
11279 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
11280 		fp = pipe_config->dpll_hw_state.fp0;
11281 	else
11282 		fp = pipe_config->dpll_hw_state.fp1;
11283 
11284 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
11285 	if (IS_PINEVIEW(dev_priv)) {
11286 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11287 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
11288 	} else {
11289 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11290 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11291 	}
11292 
11293 	if (!IS_GEN(dev_priv, 2)) {
11294 		if (IS_PINEVIEW(dev_priv))
11295 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11296 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
11297 		else
11298 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
11299 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
11300 
11301 		switch (dpll & DPLL_MODE_MASK) {
11302 		case DPLLB_MODE_DAC_SERIAL:
11303 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11304 				5 : 10;
11305 			break;
11306 		case DPLLB_MODE_LVDS:
11307 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11308 				7 : 14;
11309 			break;
11310 		default:
11311 			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
11312 				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
11313 			return;
11314 		}
11315 
11316 		if (IS_PINEVIEW(dev_priv))
11317 			port_clock = pnv_calc_dpll_params(refclk, &clock);
11318 		else
11319 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
11320 	} else {
11321 		u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
11322 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
11323 
11324 		if (is_lvds) {
11325 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11326 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
11327 
11328 			if (lvds & LVDS_CLKB_POWER_UP)
11329 				clock.p2 = 7;
11330 			else
11331 				clock.p2 = 14;
11332 		} else {
11333 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
11334 				clock.p1 = 2;
11335 			else {
11336 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11337 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11338 			}
11339 			if (dpll & PLL_P2_DIVIDE_BY_4)
11340 				clock.p2 = 4;
11341 			else
11342 				clock.p2 = 2;
11343 		}
11344 
11345 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
11346 	}
11347 
11348 	/*
11349 	 * This value includes pixel_multiplier. We will use
11350 	 * port_clock to compute adjusted_mode.crtc_clock in the
11351 	 * encoder's get_config() function.
11352 	 */
11353 	pipe_config->port_clock = port_clock;
11354 }
11355 
11356 int intel_dotclock_calculate(int link_freq,
11357 			     const struct intel_link_m_n *m_n)
11358 {
11359 	/*
11360 	 * The calculation for the data clock is:
11361 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
11362 	 * But we want to avoid losing precison if possible, so:
11363 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
11364 	 *
11365 	 * and the link clock is simpler:
11366 	 * link_clock = (m * link_clock) / n
11367 	 */
11368 
11369 	if (!m_n->link_n)
11370 		return 0;
11371 
11372 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
11373 }
11374 
11375 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
11376 				   struct intel_crtc_state *pipe_config)
11377 {
11378 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11379 
11380 	/* read out port_clock from the DPLL */
11381 	i9xx_crtc_clock_get(crtc, pipe_config);
11382 
11383 	/*
11384 	 * In case there is an active pipe without active ports,
11385 	 * we may need some idea for the dotclock anyway.
11386 	 * Calculate one based on the FDI configuration.
11387 	 */
11388 	pipe_config->base.adjusted_mode.crtc_clock =
11389 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
11390 					 &pipe_config->fdi_m_n);
11391 }
11392 
11393 /* Returns the currently programmed mode of the given encoder. */
11394 struct drm_display_mode *
11395 intel_encoder_current_mode(struct intel_encoder *encoder)
11396 {
11397 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
11398 	struct intel_crtc_state *crtc_state;
11399 	struct drm_display_mode *mode;
11400 	struct intel_crtc *crtc;
11401 	enum pipe pipe;
11402 
11403 	if (!encoder->get_hw_state(encoder, &pipe))
11404 		return NULL;
11405 
11406 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11407 
11408 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11409 	if (!mode)
11410 		return NULL;
11411 
11412 	crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
11413 	if (!crtc_state) {
11414 		kfree(mode);
11415 		return NULL;
11416 	}
11417 
11418 	crtc_state->base.crtc = &crtc->base;
11419 
11420 	if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
11421 		kfree(crtc_state);
11422 		kfree(mode);
11423 		return NULL;
11424 	}
11425 
11426 	encoder->get_config(encoder, crtc_state);
11427 
11428 	intel_mode_from_pipe_config(mode, crtc_state);
11429 
11430 	kfree(crtc_state);
11431 
11432 	return mode;
11433 }
11434 
11435 static void intel_crtc_destroy(struct drm_crtc *crtc)
11436 {
11437 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11438 
11439 	drm_crtc_cleanup(crtc);
11440 	kfree(intel_crtc);
11441 }
11442 
11443 /**
11444  * intel_wm_need_update - Check whether watermarks need updating
11445  * @cur: current plane state
11446  * @new: new plane state
11447  *
11448  * Check current plane state versus the new one to determine whether
11449  * watermarks need to be recalculated.
11450  *
11451  * Returns true or false.
11452  */
11453 static bool intel_wm_need_update(const struct intel_plane_state *cur,
11454 				 struct intel_plane_state *new)
11455 {
11456 	/* Update watermarks on tiling or size changes. */
11457 	if (new->base.visible != cur->base.visible)
11458 		return true;
11459 
11460 	if (!cur->base.fb || !new->base.fb)
11461 		return false;
11462 
11463 	if (cur->base.fb->modifier != new->base.fb->modifier ||
11464 	    cur->base.rotation != new->base.rotation ||
11465 	    drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
11466 	    drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
11467 	    drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
11468 	    drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
11469 		return true;
11470 
11471 	return false;
11472 }
11473 
11474 static bool needs_scaling(const struct intel_plane_state *state)
11475 {
11476 	int src_w = drm_rect_width(&state->base.src) >> 16;
11477 	int src_h = drm_rect_height(&state->base.src) >> 16;
11478 	int dst_w = drm_rect_width(&state->base.dst);
11479 	int dst_h = drm_rect_height(&state->base.dst);
11480 
11481 	return (src_w != dst_w || src_h != dst_h);
11482 }
11483 
11484 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
11485 				    struct intel_crtc_state *crtc_state,
11486 				    const struct intel_plane_state *old_plane_state,
11487 				    struct intel_plane_state *plane_state)
11488 {
11489 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
11490 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
11491 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11492 	bool mode_changed = needs_modeset(crtc_state);
11493 	bool was_crtc_enabled = old_crtc_state->base.active;
11494 	bool is_crtc_enabled = crtc_state->base.active;
11495 	bool turn_off, turn_on, visible, was_visible;
11496 	struct drm_framebuffer *fb = plane_state->base.fb;
11497 	int ret;
11498 
11499 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
11500 		ret = skl_update_scaler_plane(crtc_state, plane_state);
11501 		if (ret)
11502 			return ret;
11503 	}
11504 
11505 	was_visible = old_plane_state->base.visible;
11506 	visible = plane_state->base.visible;
11507 
11508 	if (!was_crtc_enabled && WARN_ON(was_visible))
11509 		was_visible = false;
11510 
11511 	/*
11512 	 * Visibility is calculated as if the crtc was on, but
11513 	 * after scaler setup everything depends on it being off
11514 	 * when the crtc isn't active.
11515 	 *
11516 	 * FIXME this is wrong for watermarks. Watermarks should also
11517 	 * be computed as if the pipe would be active. Perhaps move
11518 	 * per-plane wm computation to the .check_plane() hook, and
11519 	 * only combine the results from all planes in the current place?
11520 	 */
11521 	if (!is_crtc_enabled) {
11522 		plane_state->base.visible = visible = false;
11523 		crtc_state->active_planes &= ~BIT(plane->id);
11524 		crtc_state->data_rate[plane->id] = 0;
11525 	}
11526 
11527 	if (!was_visible && !visible)
11528 		return 0;
11529 
11530 	if (fb != old_plane_state->base.fb)
11531 		crtc_state->fb_changed = true;
11532 
11533 	turn_off = was_visible && (!visible || mode_changed);
11534 	turn_on = visible && (!was_visible || mode_changed);
11535 
11536 	DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
11537 			 crtc->base.base.id, crtc->base.name,
11538 			 plane->base.base.id, plane->base.name,
11539 			 fb ? fb->base.id : -1);
11540 
11541 	DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
11542 			 plane->base.base.id, plane->base.name,
11543 			 was_visible, visible,
11544 			 turn_off, turn_on, mode_changed);
11545 
11546 	if (turn_on) {
11547 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11548 			crtc_state->update_wm_pre = true;
11549 
11550 		/* must disable cxsr around plane enable/disable */
11551 		if (plane->id != PLANE_CURSOR)
11552 			crtc_state->disable_cxsr = true;
11553 	} else if (turn_off) {
11554 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11555 			crtc_state->update_wm_post = true;
11556 
11557 		/* must disable cxsr around plane enable/disable */
11558 		if (plane->id != PLANE_CURSOR)
11559 			crtc_state->disable_cxsr = true;
11560 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
11561 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
11562 			/* FIXME bollocks */
11563 			crtc_state->update_wm_pre = true;
11564 			crtc_state->update_wm_post = true;
11565 		}
11566 	}
11567 
11568 	if (visible || was_visible)
11569 		crtc_state->fb_bits |= plane->frontbuffer_bit;
11570 
11571 	/*
11572 	 * ILK/SNB DVSACNTR/Sprite Enable
11573 	 * IVB SPR_CTL/Sprite Enable
11574 	 * "When in Self Refresh Big FIFO mode, a write to enable the
11575 	 *  plane will be internally buffered and delayed while Big FIFO
11576 	 *  mode is exiting."
11577 	 *
11578 	 * Which means that enabling the sprite can take an extra frame
11579 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
11580 	 * down to LP0 and wait for vblank in order to make sure the
11581 	 * sprite gets enabled on the next vblank after the register write.
11582 	 * Doing otherwise would risk enabling the sprite one frame after
11583 	 * we've already signalled flip completion. We can resume LP1+
11584 	 * once the sprite has been enabled.
11585 	 *
11586 	 *
11587 	 * WaCxSRDisabledForSpriteScaling:ivb
11588 	 * IVB SPR_SCALE/Scaling Enable
11589 	 * "Low Power watermarks must be disabled for at least one
11590 	 *  frame before enabling sprite scaling, and kept disabled
11591 	 *  until sprite scaling is disabled."
11592 	 *
11593 	 * ILK/SNB DVSASCALE/Scaling Enable
11594 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
11595 	 *  masked off while Big FIFO mode is exiting."
11596 	 *
11597 	 * Despite the w/a only being listed for IVB we assume that
11598 	 * the ILK/SNB note has similar ramifications, hence we apply
11599 	 * the w/a on all three platforms.
11600 	 *
11601 	 * With experimental results seems this is needed also for primary
11602 	 * plane, not only sprite plane.
11603 	 */
11604 	if (plane->id != PLANE_CURSOR &&
11605 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
11606 	     IS_IVYBRIDGE(dev_priv)) &&
11607 	    (turn_on || (!needs_scaling(old_plane_state) &&
11608 			 needs_scaling(plane_state))))
11609 		crtc_state->disable_lp_wm = true;
11610 
11611 	return 0;
11612 }
11613 
11614 static bool encoders_cloneable(const struct intel_encoder *a,
11615 			       const struct intel_encoder *b)
11616 {
11617 	/* masks could be asymmetric, so check both ways */
11618 	return a == b || (a->cloneable & (1 << b->type) &&
11619 			  b->cloneable & (1 << a->type));
11620 }
11621 
11622 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11623 					 struct intel_crtc *crtc,
11624 					 struct intel_encoder *encoder)
11625 {
11626 	struct intel_encoder *source_encoder;
11627 	struct drm_connector *connector;
11628 	struct drm_connector_state *connector_state;
11629 	int i;
11630 
11631 	for_each_new_connector_in_state(state, connector, connector_state, i) {
11632 		if (connector_state->crtc != &crtc->base)
11633 			continue;
11634 
11635 		source_encoder =
11636 			to_intel_encoder(connector_state->best_encoder);
11637 		if (!encoders_cloneable(encoder, source_encoder))
11638 			return false;
11639 	}
11640 
11641 	return true;
11642 }
11643 
11644 static int icl_add_linked_planes(struct intel_atomic_state *state)
11645 {
11646 	struct intel_plane *plane, *linked;
11647 	struct intel_plane_state *plane_state, *linked_plane_state;
11648 	int i;
11649 
11650 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
11651 		linked = plane_state->linked_plane;
11652 
11653 		if (!linked)
11654 			continue;
11655 
11656 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
11657 		if (IS_ERR(linked_plane_state))
11658 			return PTR_ERR(linked_plane_state);
11659 
11660 		WARN_ON(linked_plane_state->linked_plane != plane);
11661 		WARN_ON(linked_plane_state->slave == plane_state->slave);
11662 	}
11663 
11664 	return 0;
11665 }
11666 
11667 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
11668 {
11669 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
11670 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11671 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->base.state);
11672 	struct intel_plane *plane, *linked;
11673 	struct intel_plane_state *plane_state;
11674 	int i;
11675 
11676 	if (INTEL_GEN(dev_priv) < 11)
11677 		return 0;
11678 
11679 	/*
11680 	 * Destroy all old plane links and make the slave plane invisible
11681 	 * in the crtc_state->active_planes mask.
11682 	 */
11683 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
11684 		if (plane->pipe != crtc->pipe || !plane_state->linked_plane)
11685 			continue;
11686 
11687 		plane_state->linked_plane = NULL;
11688 		if (plane_state->slave && !plane_state->base.visible) {
11689 			crtc_state->active_planes &= ~BIT(plane->id);
11690 			crtc_state->update_planes |= BIT(plane->id);
11691 		}
11692 
11693 		plane_state->slave = false;
11694 	}
11695 
11696 	if (!crtc_state->nv12_planes)
11697 		return 0;
11698 
11699 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
11700 		struct intel_plane_state *linked_state = NULL;
11701 
11702 		if (plane->pipe != crtc->pipe ||
11703 		    !(crtc_state->nv12_planes & BIT(plane->id)))
11704 			continue;
11705 
11706 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
11707 			if (!icl_is_nv12_y_plane(linked->id))
11708 				continue;
11709 
11710 			if (crtc_state->active_planes & BIT(linked->id))
11711 				continue;
11712 
11713 			linked_state = intel_atomic_get_plane_state(state, linked);
11714 			if (IS_ERR(linked_state))
11715 				return PTR_ERR(linked_state);
11716 
11717 			break;
11718 		}
11719 
11720 		if (!linked_state) {
11721 			DRM_DEBUG_KMS("Need %d free Y planes for planar YUV\n",
11722 				      hweight8(crtc_state->nv12_planes));
11723 
11724 			return -EINVAL;
11725 		}
11726 
11727 		plane_state->linked_plane = linked;
11728 
11729 		linked_state->slave = true;
11730 		linked_state->linked_plane = plane;
11731 		crtc_state->active_planes |= BIT(linked->id);
11732 		crtc_state->update_planes |= BIT(linked->id);
11733 		DRM_DEBUG_KMS("Using %s as Y plane for %s\n", linked->base.name, plane->base.name);
11734 	}
11735 
11736 	return 0;
11737 }
11738 
11739 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
11740 {
11741 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
11742 	struct intel_atomic_state *state =
11743 		to_intel_atomic_state(new_crtc_state->base.state);
11744 	const struct intel_crtc_state *old_crtc_state =
11745 		intel_atomic_get_old_crtc_state(state, crtc);
11746 
11747 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
11748 }
11749 
11750 static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11751 				   struct drm_crtc_state *crtc_state)
11752 {
11753 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
11754 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11755 	struct intel_crtc_state *pipe_config =
11756 		to_intel_crtc_state(crtc_state);
11757 	int ret;
11758 	bool mode_changed = needs_modeset(pipe_config);
11759 
11760 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
11761 	    mode_changed && !crtc_state->active)
11762 		pipe_config->update_wm_post = true;
11763 
11764 	if (mode_changed && crtc_state->enable &&
11765 	    dev_priv->display.crtc_compute_clock &&
11766 	    !WARN_ON(pipe_config->shared_dpll)) {
11767 		ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11768 							   pipe_config);
11769 		if (ret)
11770 			return ret;
11771 	}
11772 
11773 	/*
11774 	 * May need to update pipe gamma enable bits
11775 	 * when C8 planes are getting enabled/disabled.
11776 	 */
11777 	if (c8_planes_changed(pipe_config))
11778 		crtc_state->color_mgmt_changed = true;
11779 
11780 	if (mode_changed || pipe_config->update_pipe ||
11781 	    crtc_state->color_mgmt_changed) {
11782 		ret = intel_color_check(pipe_config);
11783 		if (ret)
11784 			return ret;
11785 	}
11786 
11787 	ret = 0;
11788 	if (dev_priv->display.compute_pipe_wm) {
11789 		ret = dev_priv->display.compute_pipe_wm(pipe_config);
11790 		if (ret) {
11791 			DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
11792 			return ret;
11793 		}
11794 	}
11795 
11796 	if (dev_priv->display.compute_intermediate_wm) {
11797 		if (WARN_ON(!dev_priv->display.compute_pipe_wm))
11798 			return 0;
11799 
11800 		/*
11801 		 * Calculate 'intermediate' watermarks that satisfy both the
11802 		 * old state and the new state.  We can program these
11803 		 * immediately.
11804 		 */
11805 		ret = dev_priv->display.compute_intermediate_wm(pipe_config);
11806 		if (ret) {
11807 			DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
11808 			return ret;
11809 		}
11810 	}
11811 
11812 	if (INTEL_GEN(dev_priv) >= 9) {
11813 		if (mode_changed || pipe_config->update_pipe)
11814 			ret = skl_update_scaler_crtc(pipe_config);
11815 
11816 		if (!ret)
11817 			ret = icl_check_nv12_planes(pipe_config);
11818 		if (!ret)
11819 			ret = skl_check_pipe_max_pixel_rate(intel_crtc,
11820 							    pipe_config);
11821 		if (!ret)
11822 			ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
11823 							 pipe_config);
11824 	}
11825 
11826 	if (HAS_IPS(dev_priv))
11827 		pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
11828 
11829 	return ret;
11830 }
11831 
11832 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
11833 	.atomic_check = intel_crtc_atomic_check,
11834 };
11835 
11836 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11837 {
11838 	struct intel_connector *connector;
11839 	struct drm_connector_list_iter conn_iter;
11840 
11841 	drm_connector_list_iter_begin(dev, &conn_iter);
11842 	for_each_intel_connector_iter(connector, &conn_iter) {
11843 		if (connector->base.state->crtc)
11844 			drm_connector_put(&connector->base);
11845 
11846 		if (connector->base.encoder) {
11847 			connector->base.state->best_encoder =
11848 				connector->base.encoder;
11849 			connector->base.state->crtc =
11850 				connector->base.encoder->crtc;
11851 
11852 			drm_connector_get(&connector->base);
11853 		} else {
11854 			connector->base.state->best_encoder = NULL;
11855 			connector->base.state->crtc = NULL;
11856 		}
11857 	}
11858 	drm_connector_list_iter_end(&conn_iter);
11859 }
11860 
11861 static int
11862 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
11863 		      struct intel_crtc_state *pipe_config)
11864 {
11865 	struct drm_connector *connector = conn_state->connector;
11866 	const struct drm_display_info *info = &connector->display_info;
11867 	int bpp;
11868 
11869 	switch (conn_state->max_bpc) {
11870 	case 6 ... 7:
11871 		bpp = 6 * 3;
11872 		break;
11873 	case 8 ... 9:
11874 		bpp = 8 * 3;
11875 		break;
11876 	case 10 ... 11:
11877 		bpp = 10 * 3;
11878 		break;
11879 	case 12:
11880 		bpp = 12 * 3;
11881 		break;
11882 	default:
11883 		return -EINVAL;
11884 	}
11885 
11886 	if (bpp < pipe_config->pipe_bpp) {
11887 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
11888 			      "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
11889 			      connector->base.id, connector->name,
11890 			      bpp, 3 * info->bpc, 3 * conn_state->max_requested_bpc,
11891 			      pipe_config->pipe_bpp);
11892 
11893 		pipe_config->pipe_bpp = bpp;
11894 	}
11895 
11896 	return 0;
11897 }
11898 
11899 static int
11900 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
11901 			  struct intel_crtc_state *pipe_config)
11902 {
11903 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11904 	struct drm_atomic_state *state = pipe_config->base.state;
11905 	struct drm_connector *connector;
11906 	struct drm_connector_state *connector_state;
11907 	int bpp, i;
11908 
11909 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
11910 	    IS_CHERRYVIEW(dev_priv)))
11911 		bpp = 10*3;
11912 	else if (INTEL_GEN(dev_priv) >= 5)
11913 		bpp = 12*3;
11914 	else
11915 		bpp = 8*3;
11916 
11917 	pipe_config->pipe_bpp = bpp;
11918 
11919 	/* Clamp display bpp to connector max bpp */
11920 	for_each_new_connector_in_state(state, connector, connector_state, i) {
11921 		int ret;
11922 
11923 		if (connector_state->crtc != &crtc->base)
11924 			continue;
11925 
11926 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
11927 		if (ret)
11928 			return ret;
11929 	}
11930 
11931 	return 0;
11932 }
11933 
11934 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11935 {
11936 	DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11937 		      "type: 0x%x flags: 0x%x\n",
11938 		      mode->crtc_clock,
11939 		      mode->crtc_hdisplay, mode->crtc_hsync_start,
11940 		      mode->crtc_hsync_end, mode->crtc_htotal,
11941 		      mode->crtc_vdisplay, mode->crtc_vsync_start,
11942 		      mode->crtc_vsync_end, mode->crtc_vtotal,
11943 		      mode->type, mode->flags);
11944 }
11945 
11946 static inline void
11947 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
11948 		      const char *id, unsigned int lane_count,
11949 		      const struct intel_link_m_n *m_n)
11950 {
11951 	DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11952 		      id, lane_count,
11953 		      m_n->gmch_m, m_n->gmch_n,
11954 		      m_n->link_m, m_n->link_n, m_n->tu);
11955 }
11956 
11957 static void
11958 intel_dump_infoframe(struct drm_i915_private *dev_priv,
11959 		     const union hdmi_infoframe *frame)
11960 {
11961 	if ((drm_debug & DRM_UT_KMS) == 0)
11962 		return;
11963 
11964 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
11965 }
11966 
11967 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
11968 
11969 static const char * const output_type_str[] = {
11970 	OUTPUT_TYPE(UNUSED),
11971 	OUTPUT_TYPE(ANALOG),
11972 	OUTPUT_TYPE(DVO),
11973 	OUTPUT_TYPE(SDVO),
11974 	OUTPUT_TYPE(LVDS),
11975 	OUTPUT_TYPE(TVOUT),
11976 	OUTPUT_TYPE(HDMI),
11977 	OUTPUT_TYPE(DP),
11978 	OUTPUT_TYPE(EDP),
11979 	OUTPUT_TYPE(DSI),
11980 	OUTPUT_TYPE(DDI),
11981 	OUTPUT_TYPE(DP_MST),
11982 };
11983 
11984 #undef OUTPUT_TYPE
11985 
11986 static void snprintf_output_types(char *buf, size_t len,
11987 				  unsigned int output_types)
11988 {
11989 	char *str = buf;
11990 	int i;
11991 
11992 	str[0] = '\0';
11993 
11994 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
11995 		int r;
11996 
11997 		if ((output_types & BIT(i)) == 0)
11998 			continue;
11999 
12000 		r = snprintf(str, len, "%s%s",
12001 			     str != buf ? "," : "", output_type_str[i]);
12002 		if (r >= len)
12003 			break;
12004 		str += r;
12005 		len -= r;
12006 
12007 		output_types &= ~BIT(i);
12008 	}
12009 
12010 	WARN_ON_ONCE(output_types != 0);
12011 }
12012 
12013 static const char * const output_format_str[] = {
12014 	[INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
12015 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
12016 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
12017 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
12018 };
12019 
12020 static const char *output_formats(enum intel_output_format format)
12021 {
12022 	if (format >= ARRAY_SIZE(output_format_str))
12023 		format = INTEL_OUTPUT_FORMAT_INVALID;
12024 	return output_format_str[format];
12025 }
12026 
12027 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
12028 {
12029 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
12030 	const struct drm_framebuffer *fb = plane_state->base.fb;
12031 	struct drm_format_name_buf format_name;
12032 
12033 	if (!fb) {
12034 		DRM_DEBUG_KMS("[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
12035 			      plane->base.base.id, plane->base.name,
12036 			      yesno(plane_state->base.visible));
12037 		return;
12038 	}
12039 
12040 	DRM_DEBUG_KMS("[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s, visible: %s\n",
12041 		      plane->base.base.id, plane->base.name,
12042 		      fb->base.id, fb->width, fb->height,
12043 		      drm_get_format_name(fb->format->format, &format_name),
12044 		      yesno(plane_state->base.visible));
12045 	DRM_DEBUG_KMS("\trotation: 0x%x, scaler: %d\n",
12046 		      plane_state->base.rotation, plane_state->scaler_id);
12047 	if (plane_state->base.visible)
12048 		DRM_DEBUG_KMS("\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
12049 			      DRM_RECT_FP_ARG(&plane_state->base.src),
12050 			      DRM_RECT_ARG(&plane_state->base.dst));
12051 }
12052 
12053 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
12054 				   struct intel_atomic_state *state,
12055 				   const char *context)
12056 {
12057 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
12058 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12059 	const struct intel_plane_state *plane_state;
12060 	struct intel_plane *plane;
12061 	char buf[64];
12062 	int i;
12063 
12064 	DRM_DEBUG_KMS("[CRTC:%d:%s] enable: %s %s\n",
12065 		      crtc->base.base.id, crtc->base.name,
12066 		      yesno(pipe_config->base.enable), context);
12067 
12068 	if (!pipe_config->base.enable)
12069 		goto dump_planes;
12070 
12071 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
12072 	DRM_DEBUG_KMS("active: %s, output_types: %s (0x%x), output format: %s\n",
12073 		      yesno(pipe_config->base.active),
12074 		      buf, pipe_config->output_types,
12075 		      output_formats(pipe_config->output_format));
12076 
12077 	DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12078 		      transcoder_name(pipe_config->cpu_transcoder),
12079 		      pipe_config->pipe_bpp, pipe_config->dither);
12080 
12081 	if (pipe_config->has_pch_encoder)
12082 		intel_dump_m_n_config(pipe_config, "fdi",
12083 				      pipe_config->fdi_lanes,
12084 				      &pipe_config->fdi_m_n);
12085 
12086 	if (intel_crtc_has_dp_encoder(pipe_config)) {
12087 		intel_dump_m_n_config(pipe_config, "dp m_n",
12088 				pipe_config->lane_count, &pipe_config->dp_m_n);
12089 		if (pipe_config->has_drrs)
12090 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
12091 					      pipe_config->lane_count,
12092 					      &pipe_config->dp_m2_n2);
12093 	}
12094 
12095 	DRM_DEBUG_KMS("audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
12096 		      pipe_config->has_audio, pipe_config->has_infoframe,
12097 		      pipe_config->infoframes.enable);
12098 
12099 	if (pipe_config->infoframes.enable &
12100 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
12101 		DRM_DEBUG_KMS("GCP: 0x%x\n", pipe_config->infoframes.gcp);
12102 	if (pipe_config->infoframes.enable &
12103 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
12104 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
12105 	if (pipe_config->infoframes.enable &
12106 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
12107 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
12108 	if (pipe_config->infoframes.enable &
12109 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
12110 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
12111 
12112 	DRM_DEBUG_KMS("requested mode:\n");
12113 	drm_mode_debug_printmodeline(&pipe_config->base.mode);
12114 	DRM_DEBUG_KMS("adjusted mode:\n");
12115 	drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12116 	intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
12117 	DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
12118 		      pipe_config->port_clock,
12119 		      pipe_config->pipe_src_w, pipe_config->pipe_src_h,
12120 		      pipe_config->pixel_rate);
12121 
12122 	if (INTEL_GEN(dev_priv) >= 9)
12123 		DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12124 			      crtc->num_scalers,
12125 			      pipe_config->scaler_state.scaler_users,
12126 		              pipe_config->scaler_state.scaler_id);
12127 
12128 	if (HAS_GMCH(dev_priv))
12129 		DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12130 			      pipe_config->gmch_pfit.control,
12131 			      pipe_config->gmch_pfit.pgm_ratios,
12132 			      pipe_config->gmch_pfit.lvds_border_bits);
12133 	else
12134 		DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s, force thru: %s\n",
12135 			      pipe_config->pch_pfit.pos,
12136 			      pipe_config->pch_pfit.size,
12137 			      enableddisabled(pipe_config->pch_pfit.enabled),
12138 			      yesno(pipe_config->pch_pfit.force_thru));
12139 
12140 	DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12141 		      pipe_config->ips_enabled, pipe_config->double_wide);
12142 
12143 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
12144 
12145 dump_planes:
12146 	if (!state)
12147 		return;
12148 
12149 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12150 		if (plane->pipe == crtc->pipe)
12151 			intel_dump_plane_state(plane_state);
12152 	}
12153 }
12154 
12155 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
12156 {
12157 	struct drm_device *dev = state->base.dev;
12158 	struct drm_connector *connector;
12159 	struct drm_connector_list_iter conn_iter;
12160 	unsigned int used_ports = 0;
12161 	unsigned int used_mst_ports = 0;
12162 	bool ret = true;
12163 
12164 	/*
12165 	 * Walk the connector list instead of the encoder
12166 	 * list to detect the problem on ddi platforms
12167 	 * where there's just one encoder per digital port.
12168 	 */
12169 	drm_connector_list_iter_begin(dev, &conn_iter);
12170 	drm_for_each_connector_iter(connector, &conn_iter) {
12171 		struct drm_connector_state *connector_state;
12172 		struct intel_encoder *encoder;
12173 
12174 		connector_state =
12175 			drm_atomic_get_new_connector_state(&state->base,
12176 							   connector);
12177 		if (!connector_state)
12178 			connector_state = connector->state;
12179 
12180 		if (!connector_state->best_encoder)
12181 			continue;
12182 
12183 		encoder = to_intel_encoder(connector_state->best_encoder);
12184 
12185 		WARN_ON(!connector_state->crtc);
12186 
12187 		switch (encoder->type) {
12188 			unsigned int port_mask;
12189 		case INTEL_OUTPUT_DDI:
12190 			if (WARN_ON(!HAS_DDI(to_i915(dev))))
12191 				break;
12192 			/* else, fall through */
12193 		case INTEL_OUTPUT_DP:
12194 		case INTEL_OUTPUT_HDMI:
12195 		case INTEL_OUTPUT_EDP:
12196 			port_mask = 1 << encoder->port;
12197 
12198 			/* the same port mustn't appear more than once */
12199 			if (used_ports & port_mask)
12200 				ret = false;
12201 
12202 			used_ports |= port_mask;
12203 			break;
12204 		case INTEL_OUTPUT_DP_MST:
12205 			used_mst_ports |=
12206 				1 << encoder->port;
12207 			break;
12208 		default:
12209 			break;
12210 		}
12211 	}
12212 	drm_connector_list_iter_end(&conn_iter);
12213 
12214 	/* can't mix MST and SST/HDMI on the same port */
12215 	if (used_ports & used_mst_ports)
12216 		return false;
12217 
12218 	return ret;
12219 }
12220 
12221 static int
12222 clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12223 {
12224 	struct drm_i915_private *dev_priv =
12225 		to_i915(crtc_state->base.crtc->dev);
12226 	struct intel_crtc_state *saved_state;
12227 
12228 	saved_state = kzalloc(sizeof(*saved_state), GFP_KERNEL);
12229 	if (!saved_state)
12230 		return -ENOMEM;
12231 
12232 	/* FIXME: before the switch to atomic started, a new pipe_config was
12233 	 * kzalloc'd. Code that depends on any field being zero should be
12234 	 * fixed, so that the crtc_state can be safely duplicated. For now,
12235 	 * only fields that are know to not cause problems are preserved. */
12236 
12237 	saved_state->scaler_state = crtc_state->scaler_state;
12238 	saved_state->shared_dpll = crtc_state->shared_dpll;
12239 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
12240 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
12241 	       sizeof(saved_state->icl_port_dplls));
12242 	saved_state->crc_enabled = crtc_state->crc_enabled;
12243 	if (IS_G4X(dev_priv) ||
12244 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
12245 		saved_state->wm = crtc_state->wm;
12246 
12247 	/* Keep base drm_crtc_state intact, only clear our extended struct */
12248 	BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
12249 	memcpy(&crtc_state->base + 1, &saved_state->base + 1,
12250 	       sizeof(*crtc_state) - sizeof(crtc_state->base));
12251 
12252 	kfree(saved_state);
12253 	return 0;
12254 }
12255 
12256 static int
12257 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
12258 {
12259 	struct drm_crtc *crtc = pipe_config->base.crtc;
12260 	struct drm_atomic_state *state = pipe_config->base.state;
12261 	struct intel_encoder *encoder;
12262 	struct drm_connector *connector;
12263 	struct drm_connector_state *connector_state;
12264 	int base_bpp, ret;
12265 	int i;
12266 	bool retry = true;
12267 
12268 	ret = clear_intel_crtc_state(pipe_config);
12269 	if (ret)
12270 		return ret;
12271 
12272 	pipe_config->cpu_transcoder =
12273 		(enum transcoder) to_intel_crtc(crtc)->pipe;
12274 
12275 	/*
12276 	 * Sanitize sync polarity flags based on requested ones. If neither
12277 	 * positive or negative polarity is requested, treat this as meaning
12278 	 * negative polarity.
12279 	 */
12280 	if (!(pipe_config->base.adjusted_mode.flags &
12281 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
12282 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
12283 
12284 	if (!(pipe_config->base.adjusted_mode.flags &
12285 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
12286 		pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
12287 
12288 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12289 					pipe_config);
12290 	if (ret)
12291 		return ret;
12292 
12293 	base_bpp = pipe_config->pipe_bpp;
12294 
12295 	/*
12296 	 * Determine the real pipe dimensions. Note that stereo modes can
12297 	 * increase the actual pipe size due to the frame doubling and
12298 	 * insertion of additional space for blanks between the frame. This
12299 	 * is stored in the crtc timings. We use the requested mode to do this
12300 	 * computation to clearly distinguish it from the adjusted mode, which
12301 	 * can be changed by the connectors in the below retry loop.
12302 	 */
12303 	drm_mode_get_hv_timing(&pipe_config->base.mode,
12304 			       &pipe_config->pipe_src_w,
12305 			       &pipe_config->pipe_src_h);
12306 
12307 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12308 		if (connector_state->crtc != crtc)
12309 			continue;
12310 
12311 		encoder = to_intel_encoder(connector_state->best_encoder);
12312 
12313 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
12314 			DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12315 			return -EINVAL;
12316 		}
12317 
12318 		/*
12319 		 * Determine output_types before calling the .compute_config()
12320 		 * hooks so that the hooks can use this information safely.
12321 		 */
12322 		if (encoder->compute_output_type)
12323 			pipe_config->output_types |=
12324 				BIT(encoder->compute_output_type(encoder, pipe_config,
12325 								 connector_state));
12326 		else
12327 			pipe_config->output_types |= BIT(encoder->type);
12328 	}
12329 
12330 encoder_retry:
12331 	/* Ensure the port clock defaults are reset when retrying. */
12332 	pipe_config->port_clock = 0;
12333 	pipe_config->pixel_multiplier = 1;
12334 
12335 	/* Fill in default crtc timings, allow encoders to overwrite them. */
12336 	drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12337 			      CRTC_STEREO_DOUBLE);
12338 
12339 	/* Pass our mode to the connectors and the CRTC to give them a chance to
12340 	 * adjust it according to limitations or connector properties, and also
12341 	 * a chance to reject the mode entirely.
12342 	 */
12343 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12344 		if (connector_state->crtc != crtc)
12345 			continue;
12346 
12347 		encoder = to_intel_encoder(connector_state->best_encoder);
12348 		ret = encoder->compute_config(encoder, pipe_config,
12349 					      connector_state);
12350 		if (ret < 0) {
12351 			if (ret != -EDEADLK)
12352 				DRM_DEBUG_KMS("Encoder config failure: %d\n",
12353 					      ret);
12354 			return ret;
12355 		}
12356 	}
12357 
12358 	/* Set default port clock if not overwritten by the encoder. Needs to be
12359 	 * done afterwards in case the encoder adjusts the mode. */
12360 	if (!pipe_config->port_clock)
12361 		pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
12362 			* pipe_config->pixel_multiplier;
12363 
12364 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
12365 	if (ret == -EDEADLK)
12366 		return ret;
12367 	if (ret < 0) {
12368 		DRM_DEBUG_KMS("CRTC fixup failed\n");
12369 		return ret;
12370 	}
12371 
12372 	if (ret == RETRY) {
12373 		if (WARN(!retry, "loop in pipe configuration computation\n"))
12374 			return -EINVAL;
12375 
12376 		DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12377 		retry = false;
12378 		goto encoder_retry;
12379 	}
12380 
12381 	/* Dithering seems to not pass-through bits correctly when it should, so
12382 	 * only enable it on 6bpc panels and when its not a compliance
12383 	 * test requesting 6bpc video pattern.
12384 	 */
12385 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
12386 		!pipe_config->dither_force_disable;
12387 	DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
12388 		      base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
12389 
12390 	return 0;
12391 }
12392 
12393 bool intel_fuzzy_clock_check(int clock1, int clock2)
12394 {
12395 	int diff;
12396 
12397 	if (clock1 == clock2)
12398 		return true;
12399 
12400 	if (!clock1 || !clock2)
12401 		return false;
12402 
12403 	diff = abs(clock1 - clock2);
12404 
12405 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12406 		return true;
12407 
12408 	return false;
12409 }
12410 
12411 static bool
12412 intel_compare_m_n(unsigned int m, unsigned int n,
12413 		  unsigned int m2, unsigned int n2,
12414 		  bool exact)
12415 {
12416 	if (m == m2 && n == n2)
12417 		return true;
12418 
12419 	if (exact || !m || !n || !m2 || !n2)
12420 		return false;
12421 
12422 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12423 
12424 	if (n > n2) {
12425 		while (n > n2) {
12426 			m2 <<= 1;
12427 			n2 <<= 1;
12428 		}
12429 	} else if (n < n2) {
12430 		while (n < n2) {
12431 			m <<= 1;
12432 			n <<= 1;
12433 		}
12434 	}
12435 
12436 	if (n != n2)
12437 		return false;
12438 
12439 	return intel_fuzzy_clock_check(m, m2);
12440 }
12441 
12442 static bool
12443 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12444 		       const struct intel_link_m_n *m2_n2,
12445 		       bool exact)
12446 {
12447 	return m_n->tu == m2_n2->tu &&
12448 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12449 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
12450 		intel_compare_m_n(m_n->link_m, m_n->link_n,
12451 				  m2_n2->link_m, m2_n2->link_n, exact);
12452 }
12453 
12454 static bool
12455 intel_compare_infoframe(const union hdmi_infoframe *a,
12456 			const union hdmi_infoframe *b)
12457 {
12458 	return memcmp(a, b, sizeof(*a)) == 0;
12459 }
12460 
12461 static void
12462 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
12463 			       bool fastset, const char *name,
12464 			       const union hdmi_infoframe *a,
12465 			       const union hdmi_infoframe *b)
12466 {
12467 	if (fastset) {
12468 		if ((drm_debug & DRM_UT_KMS) == 0)
12469 			return;
12470 
12471 		drm_dbg(DRM_UT_KMS, "fastset mismatch in %s infoframe", name);
12472 		drm_dbg(DRM_UT_KMS, "expected:");
12473 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
12474 		drm_dbg(DRM_UT_KMS, "found");
12475 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
12476 	} else {
12477 		drm_err("mismatch in %s infoframe", name);
12478 		drm_err("expected:");
12479 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
12480 		drm_err("found");
12481 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
12482 	}
12483 }
12484 
12485 static void __printf(3, 4)
12486 pipe_config_mismatch(bool fastset, const char *name, const char *format, ...)
12487 {
12488 	struct va_format vaf;
12489 	va_list args;
12490 
12491 	va_start(args, format);
12492 	vaf.fmt = format;
12493 	vaf.va = &args;
12494 
12495 	if (fastset)
12496 		drm_dbg(DRM_UT_KMS, "fastset mismatch in %s %pV", name, &vaf);
12497 	else
12498 		drm_err("mismatch in %s %pV", name, &vaf);
12499 
12500 	va_end(args);
12501 }
12502 
12503 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
12504 {
12505 	if (i915_modparams.fastboot != -1)
12506 		return i915_modparams.fastboot;
12507 
12508 	/* Enable fastboot by default on Skylake and newer */
12509 	if (INTEL_GEN(dev_priv) >= 9)
12510 		return true;
12511 
12512 	/* Enable fastboot by default on VLV and CHV */
12513 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
12514 		return true;
12515 
12516 	/* Disabled by default on all others */
12517 	return false;
12518 }
12519 
12520 static bool
12521 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
12522 			  const struct intel_crtc_state *pipe_config,
12523 			  bool fastset)
12524 {
12525 	struct drm_i915_private *dev_priv = to_i915(current_config->base.crtc->dev);
12526 	bool ret = true;
12527 	bool fixup_inherited = fastset &&
12528 		(current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
12529 		!(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
12530 
12531 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
12532 		DRM_DEBUG_KMS("initial modeset and fastboot not set\n");
12533 		ret = false;
12534 	}
12535 
12536 #define PIPE_CONF_CHECK_X(name) do { \
12537 	if (current_config->name != pipe_config->name) { \
12538 		pipe_config_mismatch(fastset, __stringify(name), \
12539 				     "(expected 0x%08x, found 0x%08x)\n", \
12540 				     current_config->name, \
12541 				     pipe_config->name); \
12542 		ret = false; \
12543 	} \
12544 } while (0)
12545 
12546 #define PIPE_CONF_CHECK_I(name) do { \
12547 	if (current_config->name != pipe_config->name) { \
12548 		pipe_config_mismatch(fastset, __stringify(name), \
12549 				     "(expected %i, found %i)\n", \
12550 				     current_config->name, \
12551 				     pipe_config->name); \
12552 		ret = false; \
12553 	} \
12554 } while (0)
12555 
12556 #define PIPE_CONF_CHECK_BOOL(name) do { \
12557 	if (current_config->name != pipe_config->name) { \
12558 		pipe_config_mismatch(fastset, __stringify(name), \
12559 				     "(expected %s, found %s)\n", \
12560 				     yesno(current_config->name), \
12561 				     yesno(pipe_config->name)); \
12562 		ret = false; \
12563 	} \
12564 } while (0)
12565 
12566 /*
12567  * Checks state where we only read out the enabling, but not the entire
12568  * state itself (like full infoframes or ELD for audio). These states
12569  * require a full modeset on bootup to fix up.
12570  */
12571 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
12572 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
12573 		PIPE_CONF_CHECK_BOOL(name); \
12574 	} else { \
12575 		pipe_config_mismatch(fastset, __stringify(name), \
12576 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
12577 				     yesno(current_config->name), \
12578 				     yesno(pipe_config->name)); \
12579 		ret = false; \
12580 	} \
12581 } while (0)
12582 
12583 #define PIPE_CONF_CHECK_P(name) do { \
12584 	if (current_config->name != pipe_config->name) { \
12585 		pipe_config_mismatch(fastset, __stringify(name), \
12586 				     "(expected %p, found %p)\n", \
12587 				     current_config->name, \
12588 				     pipe_config->name); \
12589 		ret = false; \
12590 	} \
12591 } while (0)
12592 
12593 #define PIPE_CONF_CHECK_M_N(name) do { \
12594 	if (!intel_compare_link_m_n(&current_config->name, \
12595 				    &pipe_config->name,\
12596 				    !fastset)) { \
12597 		pipe_config_mismatch(fastset, __stringify(name), \
12598 				     "(expected tu %i gmch %i/%i link %i/%i, " \
12599 				     "found tu %i, gmch %i/%i link %i/%i)\n", \
12600 				     current_config->name.tu, \
12601 				     current_config->name.gmch_m, \
12602 				     current_config->name.gmch_n, \
12603 				     current_config->name.link_m, \
12604 				     current_config->name.link_n, \
12605 				     pipe_config->name.tu, \
12606 				     pipe_config->name.gmch_m, \
12607 				     pipe_config->name.gmch_n, \
12608 				     pipe_config->name.link_m, \
12609 				     pipe_config->name.link_n); \
12610 		ret = false; \
12611 	} \
12612 } while (0)
12613 
12614 /* This is required for BDW+ where there is only one set of registers for
12615  * switching between high and low RR.
12616  * This macro can be used whenever a comparison has to be made between one
12617  * hw state and multiple sw state variables.
12618  */
12619 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
12620 	if (!intel_compare_link_m_n(&current_config->name, \
12621 				    &pipe_config->name, !fastset) && \
12622 	    !intel_compare_link_m_n(&current_config->alt_name, \
12623 				    &pipe_config->name, !fastset)) { \
12624 		pipe_config_mismatch(fastset, __stringify(name), \
12625 				     "(expected tu %i gmch %i/%i link %i/%i, " \
12626 				     "or tu %i gmch %i/%i link %i/%i, " \
12627 				     "found tu %i, gmch %i/%i link %i/%i)\n", \
12628 				     current_config->name.tu, \
12629 				     current_config->name.gmch_m, \
12630 				     current_config->name.gmch_n, \
12631 				     current_config->name.link_m, \
12632 				     current_config->name.link_n, \
12633 				     current_config->alt_name.tu, \
12634 				     current_config->alt_name.gmch_m, \
12635 				     current_config->alt_name.gmch_n, \
12636 				     current_config->alt_name.link_m, \
12637 				     current_config->alt_name.link_n, \
12638 				     pipe_config->name.tu, \
12639 				     pipe_config->name.gmch_m, \
12640 				     pipe_config->name.gmch_n, \
12641 				     pipe_config->name.link_m, \
12642 				     pipe_config->name.link_n); \
12643 		ret = false; \
12644 	} \
12645 } while (0)
12646 
12647 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
12648 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
12649 		pipe_config_mismatch(fastset, __stringify(name), \
12650 				     "(%x) (expected %i, found %i)\n", \
12651 				     (mask), \
12652 				     current_config->name & (mask), \
12653 				     pipe_config->name & (mask)); \
12654 		ret = false; \
12655 	} \
12656 } while (0)
12657 
12658 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
12659 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12660 		pipe_config_mismatch(fastset, __stringify(name), \
12661 				     "(expected %i, found %i)\n", \
12662 				     current_config->name, \
12663 				     pipe_config->name); \
12664 		ret = false; \
12665 	} \
12666 } while (0)
12667 
12668 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
12669 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
12670 				     &pipe_config->infoframes.name)) { \
12671 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
12672 					       &current_config->infoframes.name, \
12673 					       &pipe_config->infoframes.name); \
12674 		ret = false; \
12675 	} \
12676 } while (0)
12677 
12678 #define PIPE_CONF_QUIRK(quirk) \
12679 	((current_config->quirks | pipe_config->quirks) & (quirk))
12680 
12681 	PIPE_CONF_CHECK_I(cpu_transcoder);
12682 
12683 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
12684 	PIPE_CONF_CHECK_I(fdi_lanes);
12685 	PIPE_CONF_CHECK_M_N(fdi_m_n);
12686 
12687 	PIPE_CONF_CHECK_I(lane_count);
12688 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
12689 
12690 	if (INTEL_GEN(dev_priv) < 8) {
12691 		PIPE_CONF_CHECK_M_N(dp_m_n);
12692 
12693 		if (current_config->has_drrs)
12694 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
12695 	} else
12696 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
12697 
12698 	PIPE_CONF_CHECK_X(output_types);
12699 
12700 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12701 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12702 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12703 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12704 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12705 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
12706 
12707 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12708 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12709 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12710 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12711 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12712 	PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
12713 
12714 	PIPE_CONF_CHECK_I(pixel_multiplier);
12715 	PIPE_CONF_CHECK_I(output_format);
12716 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
12717 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
12718 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
12719 		PIPE_CONF_CHECK_BOOL(limited_color_range);
12720 
12721 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
12722 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
12723 	PIPE_CONF_CHECK_BOOL(has_infoframe);
12724 
12725 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
12726 
12727 	PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12728 			      DRM_MODE_FLAG_INTERLACE);
12729 
12730 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
12731 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12732 				      DRM_MODE_FLAG_PHSYNC);
12733 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12734 				      DRM_MODE_FLAG_NHSYNC);
12735 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12736 				      DRM_MODE_FLAG_PVSYNC);
12737 		PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12738 				      DRM_MODE_FLAG_NVSYNC);
12739 	}
12740 
12741 	PIPE_CONF_CHECK_X(gmch_pfit.control);
12742 	/* pfit ratios are autocomputed by the hw on gen4+ */
12743 	if (INTEL_GEN(dev_priv) < 4)
12744 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
12745 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
12746 
12747 	/*
12748 	 * Changing the EDP transcoder input mux
12749 	 * (A_ONOFF vs. A_ON) requires a full modeset.
12750 	 */
12751 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
12752 
12753 	if (!fastset) {
12754 		PIPE_CONF_CHECK_I(pipe_src_w);
12755 		PIPE_CONF_CHECK_I(pipe_src_h);
12756 
12757 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
12758 		if (current_config->pch_pfit.enabled) {
12759 			PIPE_CONF_CHECK_X(pch_pfit.pos);
12760 			PIPE_CONF_CHECK_X(pch_pfit.size);
12761 		}
12762 
12763 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12764 		PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
12765 
12766 		PIPE_CONF_CHECK_X(gamma_mode);
12767 		if (IS_CHERRYVIEW(dev_priv))
12768 			PIPE_CONF_CHECK_X(cgm_mode);
12769 		else
12770 			PIPE_CONF_CHECK_X(csc_mode);
12771 		PIPE_CONF_CHECK_BOOL(gamma_enable);
12772 		PIPE_CONF_CHECK_BOOL(csc_enable);
12773 	}
12774 
12775 	PIPE_CONF_CHECK_BOOL(double_wide);
12776 
12777 	PIPE_CONF_CHECK_P(shared_dpll);
12778 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
12779 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
12780 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12781 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
12782 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
12783 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
12784 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12785 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12786 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
12787 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
12788 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
12789 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
12790 	PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
12791 	PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
12792 	PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
12793 	PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
12794 	PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
12795 	PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
12796 	PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
12797 	PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
12798 	PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
12799 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
12800 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
12801 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
12802 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
12803 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
12804 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
12805 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
12806 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
12807 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
12808 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
12809 
12810 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
12811 	PIPE_CONF_CHECK_X(dsi_pll.div);
12812 
12813 	if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
12814 		PIPE_CONF_CHECK_I(pipe_bpp);
12815 
12816 	PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
12817 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
12818 
12819 	PIPE_CONF_CHECK_I(min_voltage_level);
12820 
12821 	PIPE_CONF_CHECK_X(infoframes.enable);
12822 	PIPE_CONF_CHECK_X(infoframes.gcp);
12823 	PIPE_CONF_CHECK_INFOFRAME(avi);
12824 	PIPE_CONF_CHECK_INFOFRAME(spd);
12825 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
12826 	PIPE_CONF_CHECK_INFOFRAME(drm);
12827 
12828 #undef PIPE_CONF_CHECK_X
12829 #undef PIPE_CONF_CHECK_I
12830 #undef PIPE_CONF_CHECK_BOOL
12831 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
12832 #undef PIPE_CONF_CHECK_P
12833 #undef PIPE_CONF_CHECK_FLAGS
12834 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
12835 #undef PIPE_CONF_QUIRK
12836 
12837 	return ret;
12838 }
12839 
12840 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
12841 					   const struct intel_crtc_state *pipe_config)
12842 {
12843 	if (pipe_config->has_pch_encoder) {
12844 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12845 							    &pipe_config->fdi_m_n);
12846 		int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
12847 
12848 		/*
12849 		 * FDI already provided one idea for the dotclock.
12850 		 * Yell if the encoder disagrees.
12851 		 */
12852 		WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
12853 		     "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12854 		     fdi_dotclock, dotclock);
12855 	}
12856 }
12857 
12858 static void verify_wm_state(struct intel_crtc *crtc,
12859 			    struct intel_crtc_state *new_crtc_state)
12860 {
12861 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12862 	struct skl_hw_state {
12863 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
12864 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
12865 		struct skl_ddb_allocation ddb;
12866 		struct skl_pipe_wm wm;
12867 	} *hw;
12868 	struct skl_ddb_allocation *sw_ddb;
12869 	struct skl_pipe_wm *sw_wm;
12870 	struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
12871 	const enum pipe pipe = crtc->pipe;
12872 	int plane, level, max_level = ilk_wm_max_level(dev_priv);
12873 
12874 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->base.active)
12875 		return;
12876 
12877 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
12878 	if (!hw)
12879 		return;
12880 
12881 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
12882 	sw_wm = &new_crtc_state->wm.skl.optimal;
12883 
12884 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
12885 
12886 	skl_ddb_get_hw_state(dev_priv, &hw->ddb);
12887 	sw_ddb = &dev_priv->wm.skl_hw.ddb;
12888 
12889 	if (INTEL_GEN(dev_priv) >= 11 &&
12890 	    hw->ddb.enabled_slices != sw_ddb->enabled_slices)
12891 		DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
12892 			  sw_ddb->enabled_slices,
12893 			  hw->ddb.enabled_slices);
12894 
12895 	/* planes */
12896 	for_each_universal_plane(dev_priv, pipe, plane) {
12897 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
12898 
12899 		hw_plane_wm = &hw->wm.planes[plane];
12900 		sw_plane_wm = &sw_wm->planes[plane];
12901 
12902 		/* Watermarks */
12903 		for (level = 0; level <= max_level; level++) {
12904 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12905 						&sw_plane_wm->wm[level]))
12906 				continue;
12907 
12908 			DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12909 				  pipe_name(pipe), plane + 1, level,
12910 				  sw_plane_wm->wm[level].plane_en,
12911 				  sw_plane_wm->wm[level].plane_res_b,
12912 				  sw_plane_wm->wm[level].plane_res_l,
12913 				  hw_plane_wm->wm[level].plane_en,
12914 				  hw_plane_wm->wm[level].plane_res_b,
12915 				  hw_plane_wm->wm[level].plane_res_l);
12916 		}
12917 
12918 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12919 					 &sw_plane_wm->trans_wm)) {
12920 			DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12921 				  pipe_name(pipe), plane + 1,
12922 				  sw_plane_wm->trans_wm.plane_en,
12923 				  sw_plane_wm->trans_wm.plane_res_b,
12924 				  sw_plane_wm->trans_wm.plane_res_l,
12925 				  hw_plane_wm->trans_wm.plane_en,
12926 				  hw_plane_wm->trans_wm.plane_res_b,
12927 				  hw_plane_wm->trans_wm.plane_res_l);
12928 		}
12929 
12930 		/* DDB */
12931 		hw_ddb_entry = &hw->ddb_y[plane];
12932 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
12933 
12934 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
12935 			DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
12936 				  pipe_name(pipe), plane + 1,
12937 				  sw_ddb_entry->start, sw_ddb_entry->end,
12938 				  hw_ddb_entry->start, hw_ddb_entry->end);
12939 		}
12940 	}
12941 
12942 	/*
12943 	 * cursor
12944 	 * If the cursor plane isn't active, we may not have updated it's ddb
12945 	 * allocation. In that case since the ddb allocation will be updated
12946 	 * once the plane becomes visible, we can skip this check
12947 	 */
12948 	if (1) {
12949 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
12950 
12951 		hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
12952 		sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
12953 
12954 		/* Watermarks */
12955 		for (level = 0; level <= max_level; level++) {
12956 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
12957 						&sw_plane_wm->wm[level]))
12958 				continue;
12959 
12960 			DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12961 				  pipe_name(pipe), level,
12962 				  sw_plane_wm->wm[level].plane_en,
12963 				  sw_plane_wm->wm[level].plane_res_b,
12964 				  sw_plane_wm->wm[level].plane_res_l,
12965 				  hw_plane_wm->wm[level].plane_en,
12966 				  hw_plane_wm->wm[level].plane_res_b,
12967 				  hw_plane_wm->wm[level].plane_res_l);
12968 		}
12969 
12970 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
12971 					 &sw_plane_wm->trans_wm)) {
12972 			DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
12973 				  pipe_name(pipe),
12974 				  sw_plane_wm->trans_wm.plane_en,
12975 				  sw_plane_wm->trans_wm.plane_res_b,
12976 				  sw_plane_wm->trans_wm.plane_res_l,
12977 				  hw_plane_wm->trans_wm.plane_en,
12978 				  hw_plane_wm->trans_wm.plane_res_b,
12979 				  hw_plane_wm->trans_wm.plane_res_l);
12980 		}
12981 
12982 		/* DDB */
12983 		hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
12984 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
12985 
12986 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
12987 			DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
12988 				  pipe_name(pipe),
12989 				  sw_ddb_entry->start, sw_ddb_entry->end,
12990 				  hw_ddb_entry->start, hw_ddb_entry->end);
12991 		}
12992 	}
12993 
12994 	kfree(hw);
12995 }
12996 
12997 static void
12998 verify_connector_state(struct intel_atomic_state *state,
12999 		       struct intel_crtc *crtc)
13000 {
13001 	struct drm_connector *connector;
13002 	struct drm_connector_state *new_conn_state;
13003 	int i;
13004 
13005 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
13006 		struct drm_encoder *encoder = connector->encoder;
13007 		struct intel_crtc_state *crtc_state = NULL;
13008 
13009 		if (new_conn_state->crtc != &crtc->base)
13010 			continue;
13011 
13012 		if (crtc)
13013 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
13014 
13015 		intel_connector_verify_state(crtc_state, new_conn_state);
13016 
13017 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
13018 		     "connector's atomic encoder doesn't match legacy encoder\n");
13019 	}
13020 }
13021 
13022 static void
13023 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
13024 {
13025 	struct intel_encoder *encoder;
13026 	struct drm_connector *connector;
13027 	struct drm_connector_state *old_conn_state, *new_conn_state;
13028 	int i;
13029 
13030 	for_each_intel_encoder(&dev_priv->drm, encoder) {
13031 		bool enabled = false, found = false;
13032 		enum pipe pipe;
13033 
13034 		DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13035 			      encoder->base.base.id,
13036 			      encoder->base.name);
13037 
13038 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
13039 						   new_conn_state, i) {
13040 			if (old_conn_state->best_encoder == &encoder->base)
13041 				found = true;
13042 
13043 			if (new_conn_state->best_encoder != &encoder->base)
13044 				continue;
13045 			found = enabled = true;
13046 
13047 			I915_STATE_WARN(new_conn_state->crtc !=
13048 					encoder->base.crtc,
13049 			     "connector's crtc doesn't match encoder crtc\n");
13050 		}
13051 
13052 		if (!found)
13053 			continue;
13054 
13055 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
13056 		     "encoder's enabled state mismatch "
13057 		     "(expected %i, found %i)\n",
13058 		     !!encoder->base.crtc, enabled);
13059 
13060 		if (!encoder->base.crtc) {
13061 			bool active;
13062 
13063 			active = encoder->get_hw_state(encoder, &pipe);
13064 			I915_STATE_WARN(active,
13065 			     "encoder detached but still enabled on pipe %c.\n",
13066 			     pipe_name(pipe));
13067 		}
13068 	}
13069 }
13070 
13071 static void
13072 verify_crtc_state(struct intel_crtc *crtc,
13073 		  struct intel_crtc_state *old_crtc_state,
13074 		  struct intel_crtc_state *new_crtc_state)
13075 {
13076 	struct drm_device *dev = crtc->base.dev;
13077 	struct drm_i915_private *dev_priv = to_i915(dev);
13078 	struct intel_encoder *encoder;
13079 	struct intel_crtc_state *pipe_config;
13080 	struct drm_atomic_state *state;
13081 	bool active;
13082 
13083 	state = old_crtc_state->base.state;
13084 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->base);
13085 	pipe_config = old_crtc_state;
13086 	memset(pipe_config, 0, sizeof(*pipe_config));
13087 	pipe_config->base.crtc = &crtc->base;
13088 	pipe_config->base.state = state;
13089 
13090 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.base.id, crtc->base.name);
13091 
13092 	active = dev_priv->display.get_pipe_config(crtc, pipe_config);
13093 
13094 	/* we keep both pipes enabled on 830 */
13095 	if (IS_I830(dev_priv))
13096 		active = new_crtc_state->base.active;
13097 
13098 	I915_STATE_WARN(new_crtc_state->base.active != active,
13099 	     "crtc active state doesn't match with hw state "
13100 	     "(expected %i, found %i)\n", new_crtc_state->base.active, active);
13101 
13102 	I915_STATE_WARN(crtc->active != new_crtc_state->base.active,
13103 	     "transitional active state does not match atomic hw state "
13104 	     "(expected %i, found %i)\n", new_crtc_state->base.active, crtc->active);
13105 
13106 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13107 		enum pipe pipe;
13108 
13109 		active = encoder->get_hw_state(encoder, &pipe);
13110 		I915_STATE_WARN(active != new_crtc_state->base.active,
13111 			"[ENCODER:%i] active %i with crtc active %i\n",
13112 			encoder->base.base.id, active, new_crtc_state->base.active);
13113 
13114 		I915_STATE_WARN(active && crtc->pipe != pipe,
13115 				"Encoder connected to wrong pipe %c\n",
13116 				pipe_name(pipe));
13117 
13118 		if (active)
13119 			encoder->get_config(encoder, pipe_config);
13120 	}
13121 
13122 	intel_crtc_compute_pixel_rate(pipe_config);
13123 
13124 	if (!new_crtc_state->base.active)
13125 		return;
13126 
13127 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
13128 
13129 	if (!intel_pipe_config_compare(new_crtc_state,
13130 				       pipe_config, false)) {
13131 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
13132 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
13133 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
13134 	}
13135 }
13136 
13137 static void
13138 intel_verify_planes(struct intel_atomic_state *state)
13139 {
13140 	struct intel_plane *plane;
13141 	const struct intel_plane_state *plane_state;
13142 	int i;
13143 
13144 	for_each_new_intel_plane_in_state(state, plane,
13145 					  plane_state, i)
13146 		assert_plane(plane, plane_state->slave ||
13147 			     plane_state->base.visible);
13148 }
13149 
13150 static void
13151 verify_single_dpll_state(struct drm_i915_private *dev_priv,
13152 			 struct intel_shared_dpll *pll,
13153 			 struct intel_crtc *crtc,
13154 			 struct intel_crtc_state *new_crtc_state)
13155 {
13156 	struct intel_dpll_hw_state dpll_hw_state;
13157 	unsigned int crtc_mask;
13158 	bool active;
13159 
13160 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
13161 
13162 	DRM_DEBUG_KMS("%s\n", pll->info->name);
13163 
13164 	active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
13165 
13166 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
13167 		I915_STATE_WARN(!pll->on && pll->active_mask,
13168 		     "pll in active use but not on in sw tracking\n");
13169 		I915_STATE_WARN(pll->on && !pll->active_mask,
13170 		     "pll is on but not used by any active crtc\n");
13171 		I915_STATE_WARN(pll->on != active,
13172 		     "pll on state mismatch (expected %i, found %i)\n",
13173 		     pll->on, active);
13174 	}
13175 
13176 	if (!crtc) {
13177 		I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
13178 				"more active pll users than references: %x vs %x\n",
13179 				pll->active_mask, pll->state.crtc_mask);
13180 
13181 		return;
13182 	}
13183 
13184 	crtc_mask = drm_crtc_mask(&crtc->base);
13185 
13186 	if (new_crtc_state->base.active)
13187 		I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13188 				"pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13189 				pipe_name(drm_crtc_index(&crtc->base)), pll->active_mask);
13190 	else
13191 		I915_STATE_WARN(pll->active_mask & crtc_mask,
13192 				"pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13193 				pipe_name(drm_crtc_index(&crtc->base)), pll->active_mask);
13194 
13195 	I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
13196 			"pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13197 			crtc_mask, pll->state.crtc_mask);
13198 
13199 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
13200 					  &dpll_hw_state,
13201 					  sizeof(dpll_hw_state)),
13202 			"pll hw state mismatch\n");
13203 }
13204 
13205 static void
13206 verify_shared_dpll_state(struct intel_crtc *crtc,
13207 			 struct intel_crtc_state *old_crtc_state,
13208 			 struct intel_crtc_state *new_crtc_state)
13209 {
13210 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13211 
13212 	if (new_crtc_state->shared_dpll)
13213 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
13214 
13215 	if (old_crtc_state->shared_dpll &&
13216 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
13217 		unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
13218 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
13219 
13220 		I915_STATE_WARN(pll->active_mask & crtc_mask,
13221 				"pll active mismatch (didn't expect pipe %c in active mask)\n",
13222 				pipe_name(drm_crtc_index(&crtc->base)));
13223 		I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
13224 				"pll enabled crtcs mismatch (found %x in enabled mask)\n",
13225 				pipe_name(drm_crtc_index(&crtc->base)));
13226 	}
13227 }
13228 
13229 static void
13230 intel_modeset_verify_crtc(struct intel_crtc *crtc,
13231 			  struct intel_atomic_state *state,
13232 			  struct intel_crtc_state *old_crtc_state,
13233 			  struct intel_crtc_state *new_crtc_state)
13234 {
13235 	if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
13236 		return;
13237 
13238 	verify_wm_state(crtc, new_crtc_state);
13239 	verify_connector_state(state, crtc);
13240 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
13241 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
13242 }
13243 
13244 static void
13245 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
13246 {
13247 	int i;
13248 
13249 	for (i = 0; i < dev_priv->num_shared_dpll; i++)
13250 		verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
13251 }
13252 
13253 static void
13254 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
13255 			      struct intel_atomic_state *state)
13256 {
13257 	verify_encoder_state(dev_priv, state);
13258 	verify_connector_state(state, NULL);
13259 	verify_disabled_dpll_state(dev_priv);
13260 }
13261 
13262 static void update_scanline_offset(const struct intel_crtc_state *crtc_state)
13263 {
13264 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
13265 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13266 
13267 	/*
13268 	 * The scanline counter increments at the leading edge of hsync.
13269 	 *
13270 	 * On most platforms it starts counting from vtotal-1 on the
13271 	 * first active line. That means the scanline counter value is
13272 	 * always one less than what we would expect. Ie. just after
13273 	 * start of vblank, which also occurs at start of hsync (on the
13274 	 * last active line), the scanline counter will read vblank_start-1.
13275 	 *
13276 	 * On gen2 the scanline counter starts counting from 1 instead
13277 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13278 	 * to keep the value positive), instead of adding one.
13279 	 *
13280 	 * On HSW+ the behaviour of the scanline counter depends on the output
13281 	 * type. For DP ports it behaves like most other platforms, but on HDMI
13282 	 * there's an extra 1 line difference. So we need to add two instead of
13283 	 * one to the value.
13284 	 *
13285 	 * On VLV/CHV DSI the scanline counter would appear to increment
13286 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
13287 	 * that means we can't tell whether we're in vblank or not while
13288 	 * we're on that particular line. We must still set scanline_offset
13289 	 * to 1 so that the vblank timestamps come out correct when we query
13290 	 * the scanline counter from within the vblank interrupt handler.
13291 	 * However if queried just before the start of vblank we'll get an
13292 	 * answer that's slightly in the future.
13293 	 */
13294 	if (IS_GEN(dev_priv, 2)) {
13295 		const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
13296 		int vtotal;
13297 
13298 		vtotal = adjusted_mode->crtc_vtotal;
13299 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
13300 			vtotal /= 2;
13301 
13302 		crtc->scanline_offset = vtotal - 1;
13303 	} else if (HAS_DDI(dev_priv) &&
13304 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
13305 		crtc->scanline_offset = 2;
13306 	} else
13307 		crtc->scanline_offset = 1;
13308 }
13309 
13310 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
13311 {
13312 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
13313 	struct intel_crtc_state *new_crtc_state;
13314 	struct intel_crtc *crtc;
13315 	int i;
13316 
13317 	if (!dev_priv->display.crtc_compute_clock)
13318 		return;
13319 
13320 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
13321 		if (!needs_modeset(new_crtc_state))
13322 			continue;
13323 
13324 		intel_release_shared_dplls(state, crtc);
13325 	}
13326 }
13327 
13328 /*
13329  * This implements the workaround described in the "notes" section of the mode
13330  * set sequence documentation. When going from no pipes or single pipe to
13331  * multiple pipes, and planes are enabled after the pipe, we need to wait at
13332  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13333  */
13334 static int haswell_mode_set_planes_workaround(struct intel_atomic_state *state)
13335 {
13336 	struct intel_crtc_state *crtc_state;
13337 	struct intel_crtc *crtc;
13338 	struct intel_crtc_state *first_crtc_state = NULL;
13339 	struct intel_crtc_state *other_crtc_state = NULL;
13340 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13341 	int i;
13342 
13343 	/* look at all crtc's that are going to be enabled in during modeset */
13344 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
13345 		if (!crtc_state->base.active ||
13346 		    !needs_modeset(crtc_state))
13347 			continue;
13348 
13349 		if (first_crtc_state) {
13350 			other_crtc_state = crtc_state;
13351 			break;
13352 		} else {
13353 			first_crtc_state = crtc_state;
13354 			first_pipe = crtc->pipe;
13355 		}
13356 	}
13357 
13358 	/* No workaround needed? */
13359 	if (!first_crtc_state)
13360 		return 0;
13361 
13362 	/* w/a possibly needed, check how many crtc's are already enabled. */
13363 	for_each_intel_crtc(state->base.dev, crtc) {
13364 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
13365 		if (IS_ERR(crtc_state))
13366 			return PTR_ERR(crtc_state);
13367 
13368 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
13369 
13370 		if (!crtc_state->base.active ||
13371 		    needs_modeset(crtc_state))
13372 			continue;
13373 
13374 		/* 2 or more enabled crtcs means no need for w/a */
13375 		if (enabled_pipe != INVALID_PIPE)
13376 			return 0;
13377 
13378 		enabled_pipe = crtc->pipe;
13379 	}
13380 
13381 	if (enabled_pipe != INVALID_PIPE)
13382 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13383 	else if (other_crtc_state)
13384 		other_crtc_state->hsw_workaround_pipe = first_pipe;
13385 
13386 	return 0;
13387 }
13388 
13389 static int intel_lock_all_pipes(struct intel_atomic_state *state)
13390 {
13391 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
13392 	struct intel_crtc *crtc;
13393 
13394 	/* Add all pipes to the state */
13395 	for_each_intel_crtc(&dev_priv->drm, crtc) {
13396 		struct intel_crtc_state *crtc_state;
13397 
13398 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
13399 		if (IS_ERR(crtc_state))
13400 			return PTR_ERR(crtc_state);
13401 	}
13402 
13403 	return 0;
13404 }
13405 
13406 static int intel_modeset_all_pipes(struct intel_atomic_state *state)
13407 {
13408 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
13409 	struct intel_crtc *crtc;
13410 
13411 	/*
13412 	 * Add all pipes to the state, and force
13413 	 * a modeset on all the active ones.
13414 	 */
13415 	for_each_intel_crtc(&dev_priv->drm, crtc) {
13416 		struct intel_crtc_state *crtc_state;
13417 		int ret;
13418 
13419 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
13420 		if (IS_ERR(crtc_state))
13421 			return PTR_ERR(crtc_state);
13422 
13423 		if (!crtc_state->base.active || needs_modeset(crtc_state))
13424 			continue;
13425 
13426 		crtc_state->base.mode_changed = true;
13427 
13428 		ret = drm_atomic_add_affected_connectors(&state->base,
13429 							 &crtc->base);
13430 		if (ret)
13431 			return ret;
13432 
13433 		ret = drm_atomic_add_affected_planes(&state->base,
13434 						     &crtc->base);
13435 		if (ret)
13436 			return ret;
13437 	}
13438 
13439 	return 0;
13440 }
13441 
13442 static int intel_modeset_checks(struct intel_atomic_state *state)
13443 {
13444 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
13445 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
13446 	struct intel_crtc *crtc;
13447 	int ret = 0, i;
13448 
13449 	if (!check_digital_port_conflicts(state)) {
13450 		DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13451 		return -EINVAL;
13452 	}
13453 
13454 	/* keep the current setting */
13455 	if (!state->cdclk.force_min_cdclk_changed)
13456 		state->cdclk.force_min_cdclk = dev_priv->cdclk.force_min_cdclk;
13457 
13458 	state->modeset = true;
13459 	state->active_crtcs = dev_priv->active_crtcs;
13460 	state->cdclk.logical = dev_priv->cdclk.logical;
13461 	state->cdclk.actual = dev_priv->cdclk.actual;
13462 	state->cdclk.pipe = INVALID_PIPE;
13463 
13464 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
13465 					    new_crtc_state, i) {
13466 		if (new_crtc_state->base.active)
13467 			state->active_crtcs |= 1 << i;
13468 		else
13469 			state->active_crtcs &= ~(1 << i);
13470 
13471 		if (old_crtc_state->base.active != new_crtc_state->base.active)
13472 			state->active_pipe_changes |= drm_crtc_mask(&crtc->base);
13473 	}
13474 
13475 	/*
13476 	 * See if the config requires any additional preparation, e.g.
13477 	 * to adjust global state with pipes off.  We need to do this
13478 	 * here so we can get the modeset_pipe updated config for the new
13479 	 * mode set on this crtc.  For other crtcs we need to use the
13480 	 * adjusted_mode bits in the crtc directly.
13481 	 */
13482 	if (dev_priv->display.modeset_calc_cdclk) {
13483 		enum pipe pipe;
13484 
13485 		ret = dev_priv->display.modeset_calc_cdclk(state);
13486 		if (ret < 0)
13487 			return ret;
13488 
13489 		/*
13490 		 * Writes to dev_priv->cdclk.logical must protected by
13491 		 * holding all the crtc locks, even if we don't end up
13492 		 * touching the hardware
13493 		 */
13494 		if (intel_cdclk_changed(&dev_priv->cdclk.logical,
13495 					&state->cdclk.logical)) {
13496 			ret = intel_lock_all_pipes(state);
13497 			if (ret < 0)
13498 				return ret;
13499 		}
13500 
13501 		if (is_power_of_2(state->active_crtcs)) {
13502 			struct intel_crtc *crtc;
13503 			struct intel_crtc_state *crtc_state;
13504 
13505 			pipe = ilog2(state->active_crtcs);
13506 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
13507 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
13508 			if (crtc_state && needs_modeset(crtc_state))
13509 				pipe = INVALID_PIPE;
13510 		} else {
13511 			pipe = INVALID_PIPE;
13512 		}
13513 
13514 		/* All pipes must be switched off while we change the cdclk. */
13515 		if (pipe != INVALID_PIPE &&
13516 		    intel_cdclk_needs_cd2x_update(dev_priv,
13517 						  &dev_priv->cdclk.actual,
13518 						  &state->cdclk.actual)) {
13519 			ret = intel_lock_all_pipes(state);
13520 			if (ret < 0)
13521 				return ret;
13522 
13523 			state->cdclk.pipe = pipe;
13524 		} else if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
13525 						     &state->cdclk.actual)) {
13526 			ret = intel_modeset_all_pipes(state);
13527 			if (ret < 0)
13528 				return ret;
13529 
13530 			state->cdclk.pipe = INVALID_PIPE;
13531 		}
13532 
13533 		DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
13534 			      state->cdclk.logical.cdclk,
13535 			      state->cdclk.actual.cdclk);
13536 		DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
13537 			      state->cdclk.logical.voltage_level,
13538 			      state->cdclk.actual.voltage_level);
13539 	}
13540 
13541 	intel_modeset_clear_plls(state);
13542 
13543 	if (IS_HASWELL(dev_priv))
13544 		return haswell_mode_set_planes_workaround(state);
13545 
13546 	return 0;
13547 }
13548 
13549 /*
13550  * Handle calculation of various watermark data at the end of the atomic check
13551  * phase.  The code here should be run after the per-crtc and per-plane 'check'
13552  * handlers to ensure that all derived state has been updated.
13553  */
13554 static int calc_watermark_data(struct intel_atomic_state *state)
13555 {
13556 	struct drm_device *dev = state->base.dev;
13557 	struct drm_i915_private *dev_priv = to_i915(dev);
13558 
13559 	/* Is there platform-specific watermark information to calculate? */
13560 	if (dev_priv->display.compute_global_watermarks)
13561 		return dev_priv->display.compute_global_watermarks(state);
13562 
13563 	return 0;
13564 }
13565 
13566 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
13567 				     struct intel_crtc_state *new_crtc_state)
13568 {
13569 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
13570 		return;
13571 
13572 	new_crtc_state->base.mode_changed = false;
13573 	new_crtc_state->update_pipe = true;
13574 
13575 	/*
13576 	 * If we're not doing the full modeset we want to
13577 	 * keep the current M/N values as they may be
13578 	 * sufficiently different to the computed values
13579 	 * to cause problems.
13580 	 *
13581 	 * FIXME: should really copy more fuzzy state here
13582 	 */
13583 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
13584 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
13585 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
13586 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
13587 }
13588 
13589 /**
13590  * intel_atomic_check - validate state object
13591  * @dev: drm device
13592  * @_state: state to validate
13593  */
13594 static int intel_atomic_check(struct drm_device *dev,
13595 			      struct drm_atomic_state *_state)
13596 {
13597 	struct drm_i915_private *dev_priv = to_i915(dev);
13598 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
13599 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
13600 	struct intel_crtc *crtc;
13601 	int ret, i;
13602 	bool any_ms = state->cdclk.force_min_cdclk_changed;
13603 
13604 	/* Catch I915_MODE_FLAG_INHERITED */
13605 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
13606 					    new_crtc_state, i) {
13607 		if (new_crtc_state->base.mode.private_flags !=
13608 		    old_crtc_state->base.mode.private_flags)
13609 			new_crtc_state->base.mode_changed = true;
13610 	}
13611 
13612 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
13613 	if (ret)
13614 		goto fail;
13615 
13616 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
13617 					    new_crtc_state, i) {
13618 		if (!needs_modeset(new_crtc_state))
13619 			continue;
13620 
13621 		if (!new_crtc_state->base.enable) {
13622 			any_ms = true;
13623 			continue;
13624 		}
13625 
13626 		ret = intel_modeset_pipe_config(new_crtc_state);
13627 		if (ret)
13628 			goto fail;
13629 
13630 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
13631 
13632 		if (needs_modeset(new_crtc_state))
13633 			any_ms = true;
13634 	}
13635 
13636 	ret = drm_dp_mst_atomic_check(&state->base);
13637 	if (ret)
13638 		goto fail;
13639 
13640 	if (any_ms) {
13641 		ret = intel_modeset_checks(state);
13642 		if (ret)
13643 			goto fail;
13644 	} else {
13645 		state->cdclk.logical = dev_priv->cdclk.logical;
13646 	}
13647 
13648 	ret = icl_add_linked_planes(state);
13649 	if (ret)
13650 		goto fail;
13651 
13652 	ret = drm_atomic_helper_check_planes(dev, &state->base);
13653 	if (ret)
13654 		goto fail;
13655 
13656 	intel_fbc_choose_crtc(dev_priv, state);
13657 	ret = calc_watermark_data(state);
13658 	if (ret)
13659 		goto fail;
13660 
13661 	ret = intel_bw_atomic_check(state);
13662 	if (ret)
13663 		goto fail;
13664 
13665 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
13666 					    new_crtc_state, i) {
13667 		if (!needs_modeset(new_crtc_state) &&
13668 		    !new_crtc_state->update_pipe)
13669 			continue;
13670 
13671 		intel_dump_pipe_config(new_crtc_state, state,
13672 				       needs_modeset(new_crtc_state) ?
13673 				       "[modeset]" : "[fastset]");
13674 	}
13675 
13676 	return 0;
13677 
13678  fail:
13679 	if (ret == -EDEADLK)
13680 		return ret;
13681 
13682 	/*
13683 	 * FIXME would probably be nice to know which crtc specifically
13684 	 * caused the failure, in cases where we can pinpoint it.
13685 	 */
13686 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
13687 					    new_crtc_state, i)
13688 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
13689 
13690 	return ret;
13691 }
13692 
13693 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
13694 {
13695 	return drm_atomic_helper_prepare_planes(state->base.dev,
13696 						&state->base);
13697 }
13698 
13699 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
13700 {
13701 	struct drm_device *dev = crtc->base.dev;
13702 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
13703 
13704 	if (!vblank->max_vblank_count)
13705 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
13706 
13707 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
13708 }
13709 
13710 static void intel_update_crtc(struct intel_crtc *crtc,
13711 			      struct intel_atomic_state *state,
13712 			      struct intel_crtc_state *old_crtc_state,
13713 			      struct intel_crtc_state *new_crtc_state)
13714 {
13715 	struct drm_device *dev = state->base.dev;
13716 	struct drm_i915_private *dev_priv = to_i915(dev);
13717 	bool modeset = needs_modeset(new_crtc_state);
13718 	struct intel_plane_state *new_plane_state =
13719 		intel_atomic_get_new_plane_state(state,
13720 						 to_intel_plane(crtc->base.primary));
13721 
13722 	if (modeset) {
13723 		update_scanline_offset(new_crtc_state);
13724 		dev_priv->display.crtc_enable(new_crtc_state, state);
13725 
13726 		/* vblanks work again, re-enable pipe CRC. */
13727 		intel_crtc_enable_pipe_crc(crtc);
13728 	} else {
13729 		intel_pre_plane_update(old_crtc_state, new_crtc_state);
13730 
13731 		if (new_crtc_state->update_pipe)
13732 			intel_encoders_update_pipe(crtc, new_crtc_state, state);
13733 	}
13734 
13735 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
13736 		intel_fbc_disable(crtc);
13737 	else if (new_plane_state)
13738 		intel_fbc_enable(crtc, new_crtc_state, new_plane_state);
13739 
13740 	intel_begin_crtc_commit(state, crtc);
13741 
13742 	if (INTEL_GEN(dev_priv) >= 9)
13743 		skl_update_planes_on_crtc(state, crtc);
13744 	else
13745 		i9xx_update_planes_on_crtc(state, crtc);
13746 
13747 	intel_finish_crtc_commit(state, crtc);
13748 }
13749 
13750 static void intel_update_crtcs(struct intel_atomic_state *state)
13751 {
13752 	struct intel_crtc *crtc;
13753 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
13754 	int i;
13755 
13756 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
13757 		if (!new_crtc_state->base.active)
13758 			continue;
13759 
13760 		intel_update_crtc(crtc, state, old_crtc_state,
13761 				  new_crtc_state);
13762 	}
13763 }
13764 
13765 static void skl_update_crtcs(struct intel_atomic_state *state)
13766 {
13767 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
13768 	struct intel_crtc *crtc;
13769 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
13770 	unsigned int updated = 0;
13771 	bool progress;
13772 	enum pipe pipe;
13773 	int i;
13774 	u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
13775 	u8 required_slices = state->wm_results.ddb.enabled_slices;
13776 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
13777 
13778 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
13779 		/* ignore allocations for crtc's that have been turned off. */
13780 		if (new_crtc_state->base.active)
13781 			entries[i] = old_crtc_state->wm.skl.ddb;
13782 
13783 	/* If 2nd DBuf slice required, enable it here */
13784 	if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
13785 		icl_dbuf_slices_update(dev_priv, required_slices);
13786 
13787 	/*
13788 	 * Whenever the number of active pipes changes, we need to make sure we
13789 	 * update the pipes in the right order so that their ddb allocations
13790 	 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
13791 	 * cause pipe underruns and other bad stuff.
13792 	 */
13793 	do {
13794 		progress = false;
13795 
13796 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
13797 			bool vbl_wait = false;
13798 			unsigned int cmask = drm_crtc_mask(&crtc->base);
13799 
13800 			pipe = crtc->pipe;
13801 
13802 			if (updated & cmask || !new_crtc_state->base.active)
13803 				continue;
13804 
13805 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
13806 							entries,
13807 							INTEL_INFO(dev_priv)->num_pipes, i))
13808 				continue;
13809 
13810 			updated |= cmask;
13811 			entries[i] = new_crtc_state->wm.skl.ddb;
13812 
13813 			/*
13814 			 * If this is an already active pipe, it's DDB changed,
13815 			 * and this isn't the last pipe that needs updating
13816 			 * then we need to wait for a vblank to pass for the
13817 			 * new ddb allocation to take effect.
13818 			 */
13819 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
13820 						 &old_crtc_state->wm.skl.ddb) &&
13821 			    !new_crtc_state->base.active_changed &&
13822 			    state->wm_results.dirty_pipes != updated)
13823 				vbl_wait = true;
13824 
13825 			intel_update_crtc(crtc, state, old_crtc_state,
13826 					  new_crtc_state);
13827 
13828 			if (vbl_wait)
13829 				intel_wait_for_vblank(dev_priv, pipe);
13830 
13831 			progress = true;
13832 		}
13833 	} while (progress);
13834 
13835 	/* If 2nd DBuf slice is no more required disable it */
13836 	if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
13837 		icl_dbuf_slices_update(dev_priv, required_slices);
13838 }
13839 
13840 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
13841 {
13842 	struct intel_atomic_state *state, *next;
13843 	struct llist_node *freed;
13844 
13845 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
13846 	llist_for_each_entry_safe(state, next, freed, freed)
13847 		drm_atomic_state_put(&state->base);
13848 }
13849 
13850 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
13851 {
13852 	struct drm_i915_private *dev_priv =
13853 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
13854 
13855 	intel_atomic_helper_free_state(dev_priv);
13856 }
13857 
13858 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
13859 {
13860 	struct wait_queue_entry wait_fence, wait_reset;
13861 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
13862 
13863 	init_wait_entry(&wait_fence, 0);
13864 	init_wait_entry(&wait_reset, 0);
13865 	for (;;) {
13866 		prepare_to_wait(&intel_state->commit_ready.wait,
13867 				&wait_fence, TASK_UNINTERRUPTIBLE);
13868 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
13869 					      I915_RESET_MODESET),
13870 				&wait_reset, TASK_UNINTERRUPTIBLE);
13871 
13872 
13873 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
13874 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
13875 			break;
13876 
13877 		schedule();
13878 	}
13879 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
13880 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
13881 				  I915_RESET_MODESET),
13882 		    &wait_reset);
13883 }
13884 
13885 static void intel_atomic_cleanup_work(struct work_struct *work)
13886 {
13887 	struct drm_atomic_state *state =
13888 		container_of(work, struct drm_atomic_state, commit_work);
13889 	struct drm_i915_private *i915 = to_i915(state->dev);
13890 
13891 	drm_atomic_helper_cleanup_planes(&i915->drm, state);
13892 	drm_atomic_helper_commit_cleanup_done(state);
13893 	drm_atomic_state_put(state);
13894 
13895 	intel_atomic_helper_free_state(i915);
13896 }
13897 
13898 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
13899 {
13900 	struct drm_device *dev = state->base.dev;
13901 	struct drm_i915_private *dev_priv = to_i915(dev);
13902 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
13903 	struct intel_crtc *crtc;
13904 	u64 put_domains[I915_MAX_PIPES] = {};
13905 	intel_wakeref_t wakeref = 0;
13906 	int i;
13907 
13908 	intel_atomic_commit_fence_wait(state);
13909 
13910 	drm_atomic_helper_wait_for_dependencies(&state->base);
13911 
13912 	if (state->modeset)
13913 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
13914 
13915 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
13916 		if (needs_modeset(new_crtc_state) ||
13917 		    new_crtc_state->update_pipe) {
13918 
13919 			put_domains[crtc->pipe] =
13920 				modeset_get_crtc_power_domains(new_crtc_state);
13921 		}
13922 
13923 		if (!needs_modeset(new_crtc_state))
13924 			continue;
13925 
13926 		intel_pre_plane_update(old_crtc_state, new_crtc_state);
13927 
13928 		if (old_crtc_state->base.active) {
13929 			intel_crtc_disable_planes(state, crtc);
13930 
13931 			/*
13932 			 * We need to disable pipe CRC before disabling the pipe,
13933 			 * or we race against vblank off.
13934 			 */
13935 			intel_crtc_disable_pipe_crc(crtc);
13936 
13937 			dev_priv->display.crtc_disable(old_crtc_state, state);
13938 			crtc->active = false;
13939 			intel_fbc_disable(crtc);
13940 			intel_disable_shared_dpll(old_crtc_state);
13941 
13942 			/*
13943 			 * Underruns don't always raise
13944 			 * interrupts, so check manually.
13945 			 */
13946 			intel_check_cpu_fifo_underruns(dev_priv);
13947 			intel_check_pch_fifo_underruns(dev_priv);
13948 
13949 			/* FIXME unify this for all platforms */
13950 			if (!new_crtc_state->base.active &&
13951 			    !HAS_GMCH(dev_priv) &&
13952 			    dev_priv->display.initial_watermarks)
13953 				dev_priv->display.initial_watermarks(state,
13954 								     new_crtc_state);
13955 		}
13956 	}
13957 
13958 	/* FIXME: Eventually get rid of our crtc->config pointer */
13959 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
13960 		crtc->config = new_crtc_state;
13961 
13962 	if (state->modeset) {
13963 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
13964 
13965 		intel_set_cdclk_pre_plane_update(dev_priv,
13966 						 &state->cdclk.actual,
13967 						 &dev_priv->cdclk.actual,
13968 						 state->cdclk.pipe);
13969 
13970 		/*
13971 		 * SKL workaround: bspec recommends we disable the SAGV when we
13972 		 * have more then one pipe enabled
13973 		 */
13974 		if (!intel_can_enable_sagv(state))
13975 			intel_disable_sagv(dev_priv);
13976 
13977 		intel_modeset_verify_disabled(dev_priv, state);
13978 	}
13979 
13980 	/* Complete the events for pipes that have now been disabled */
13981 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
13982 		bool modeset = needs_modeset(new_crtc_state);
13983 
13984 		/* Complete events for now disable pipes here. */
13985 		if (modeset && !new_crtc_state->base.active && new_crtc_state->base.event) {
13986 			spin_lock_irq(&dev->event_lock);
13987 			drm_crtc_send_vblank_event(&crtc->base, new_crtc_state->base.event);
13988 			spin_unlock_irq(&dev->event_lock);
13989 
13990 			new_crtc_state->base.event = NULL;
13991 		}
13992 	}
13993 
13994 	if (state->modeset)
13995 		intel_encoders_update_prepare(state);
13996 
13997 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
13998 	dev_priv->display.update_crtcs(state);
13999 
14000 	if (state->modeset) {
14001 		intel_encoders_update_complete(state);
14002 
14003 		intel_set_cdclk_post_plane_update(dev_priv,
14004 						  &state->cdclk.actual,
14005 						  &dev_priv->cdclk.actual,
14006 						  state->cdclk.pipe);
14007 	}
14008 
14009 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14010 	 * already, but still need the state for the delayed optimization. To
14011 	 * fix this:
14012 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14013 	 * - schedule that vblank worker _before_ calling hw_done
14014 	 * - at the start of commit_tail, cancel it _synchrously
14015 	 * - switch over to the vblank wait helper in the core after that since
14016 	 *   we don't need out special handling any more.
14017 	 */
14018 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
14019 
14020 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14021 		if (new_crtc_state->base.active &&
14022 		    !needs_modeset(new_crtc_state) &&
14023 		    (new_crtc_state->base.color_mgmt_changed ||
14024 		     new_crtc_state->update_pipe))
14025 			intel_color_load_luts(new_crtc_state);
14026 	}
14027 
14028 	/*
14029 	 * Now that the vblank has passed, we can go ahead and program the
14030 	 * optimal watermarks on platforms that need two-step watermark
14031 	 * programming.
14032 	 *
14033 	 * TODO: Move this (and other cleanup) to an async worker eventually.
14034 	 */
14035 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14036 		if (dev_priv->display.optimize_watermarks)
14037 			dev_priv->display.optimize_watermarks(state,
14038 							      new_crtc_state);
14039 	}
14040 
14041 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
14042 		intel_post_plane_update(old_crtc_state);
14043 
14044 		if (put_domains[i])
14045 			modeset_put_power_domains(dev_priv, put_domains[i]);
14046 
14047 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
14048 	}
14049 
14050 	if (state->modeset)
14051 		intel_verify_planes(state);
14052 
14053 	if (state->modeset && intel_can_enable_sagv(state))
14054 		intel_enable_sagv(dev_priv);
14055 
14056 	drm_atomic_helper_commit_hw_done(&state->base);
14057 
14058 	if (state->modeset) {
14059 		/* As one of the primary mmio accessors, KMS has a high
14060 		 * likelihood of triggering bugs in unclaimed access. After we
14061 		 * finish modesetting, see if an error has been flagged, and if
14062 		 * so enable debugging for the next modeset - and hope we catch
14063 		 * the culprit.
14064 		 */
14065 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
14066 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
14067 	}
14068 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
14069 
14070 	/*
14071 	 * Defer the cleanup of the old state to a separate worker to not
14072 	 * impede the current task (userspace for blocking modesets) that
14073 	 * are executed inline. For out-of-line asynchronous modesets/flips,
14074 	 * deferring to a new worker seems overkill, but we would place a
14075 	 * schedule point (cond_resched()) here anyway to keep latencies
14076 	 * down.
14077 	 */
14078 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
14079 	queue_work(system_highpri_wq, &state->base.commit_work);
14080 }
14081 
14082 static void intel_atomic_commit_work(struct work_struct *work)
14083 {
14084 	struct intel_atomic_state *state =
14085 		container_of(work, struct intel_atomic_state, base.commit_work);
14086 
14087 	intel_atomic_commit_tail(state);
14088 }
14089 
14090 static int __i915_sw_fence_call
14091 intel_atomic_commit_ready(struct i915_sw_fence *fence,
14092 			  enum i915_sw_fence_notify notify)
14093 {
14094 	struct intel_atomic_state *state =
14095 		container_of(fence, struct intel_atomic_state, commit_ready);
14096 
14097 	switch (notify) {
14098 	case FENCE_COMPLETE:
14099 		/* we do blocking waits in the worker, nothing to do here */
14100 		break;
14101 	case FENCE_FREE:
14102 		{
14103 			struct intel_atomic_helper *helper =
14104 				&to_i915(state->base.dev)->atomic_helper;
14105 
14106 			if (llist_add(&state->freed, &helper->free_list))
14107 				schedule_work(&helper->free_work);
14108 			break;
14109 		}
14110 	}
14111 
14112 	return NOTIFY_DONE;
14113 }
14114 
14115 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
14116 {
14117 	struct intel_plane_state *old_plane_state, *new_plane_state;
14118 	struct intel_plane *plane;
14119 	int i;
14120 
14121 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
14122 					     new_plane_state, i)
14123 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->base.fb),
14124 					to_intel_frontbuffer(new_plane_state->base.fb),
14125 					plane->frontbuffer_bit);
14126 }
14127 
14128 static int intel_atomic_commit(struct drm_device *dev,
14129 			       struct drm_atomic_state *_state,
14130 			       bool nonblock)
14131 {
14132 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
14133 	struct drm_i915_private *dev_priv = to_i915(dev);
14134 	int ret = 0;
14135 
14136 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
14137 
14138 	drm_atomic_state_get(&state->base);
14139 	i915_sw_fence_init(&state->commit_ready,
14140 			   intel_atomic_commit_ready);
14141 
14142 	/*
14143 	 * The intel_legacy_cursor_update() fast path takes care
14144 	 * of avoiding the vblank waits for simple cursor
14145 	 * movement and flips. For cursor on/off and size changes,
14146 	 * we want to perform the vblank waits so that watermark
14147 	 * updates happen during the correct frames. Gen9+ have
14148 	 * double buffered watermarks and so shouldn't need this.
14149 	 *
14150 	 * Unset state->legacy_cursor_update before the call to
14151 	 * drm_atomic_helper_setup_commit() because otherwise
14152 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
14153 	 * we get FIFO underruns because we didn't wait
14154 	 * for vblank.
14155 	 *
14156 	 * FIXME doing watermarks and fb cleanup from a vblank worker
14157 	 * (assuming we had any) would solve these problems.
14158 	 */
14159 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
14160 		struct intel_crtc_state *new_crtc_state;
14161 		struct intel_crtc *crtc;
14162 		int i;
14163 
14164 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
14165 			if (new_crtc_state->wm.need_postvbl_update ||
14166 			    new_crtc_state->update_wm_post)
14167 				state->base.legacy_cursor_update = false;
14168 	}
14169 
14170 	ret = intel_atomic_prepare_commit(state);
14171 	if (ret) {
14172 		DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
14173 		i915_sw_fence_commit(&state->commit_ready);
14174 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
14175 		return ret;
14176 	}
14177 
14178 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
14179 	if (!ret)
14180 		ret = drm_atomic_helper_swap_state(&state->base, true);
14181 
14182 	if (ret) {
14183 		i915_sw_fence_commit(&state->commit_ready);
14184 
14185 		drm_atomic_helper_cleanup_planes(dev, &state->base);
14186 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
14187 		return ret;
14188 	}
14189 	dev_priv->wm.distrust_bios_wm = false;
14190 	intel_shared_dpll_swap_state(state);
14191 	intel_atomic_track_fbs(state);
14192 
14193 	if (state->modeset) {
14194 		memcpy(dev_priv->min_cdclk, state->min_cdclk,
14195 		       sizeof(state->min_cdclk));
14196 		memcpy(dev_priv->min_voltage_level, state->min_voltage_level,
14197 		       sizeof(state->min_voltage_level));
14198 		dev_priv->active_crtcs = state->active_crtcs;
14199 		dev_priv->cdclk.force_min_cdclk = state->cdclk.force_min_cdclk;
14200 
14201 		intel_cdclk_swap_state(state);
14202 	}
14203 
14204 	drm_atomic_state_get(&state->base);
14205 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
14206 
14207 	i915_sw_fence_commit(&state->commit_ready);
14208 	if (nonblock && state->modeset) {
14209 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
14210 	} else if (nonblock) {
14211 		queue_work(system_unbound_wq, &state->base.commit_work);
14212 	} else {
14213 		if (state->modeset)
14214 			flush_workqueue(dev_priv->modeset_wq);
14215 		intel_atomic_commit_tail(state);
14216 	}
14217 
14218 	return 0;
14219 }
14220 
14221 struct wait_rps_boost {
14222 	struct wait_queue_entry wait;
14223 
14224 	struct drm_crtc *crtc;
14225 	struct i915_request *request;
14226 };
14227 
14228 static int do_rps_boost(struct wait_queue_entry *_wait,
14229 			unsigned mode, int sync, void *key)
14230 {
14231 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
14232 	struct i915_request *rq = wait->request;
14233 
14234 	/*
14235 	 * If we missed the vblank, but the request is already running it
14236 	 * is reasonable to assume that it will complete before the next
14237 	 * vblank without our intervention, so leave RPS alone.
14238 	 */
14239 	if (!i915_request_started(rq))
14240 		gen6_rps_boost(rq);
14241 	i915_request_put(rq);
14242 
14243 	drm_crtc_vblank_put(wait->crtc);
14244 
14245 	list_del(&wait->wait.entry);
14246 	kfree(wait);
14247 	return 1;
14248 }
14249 
14250 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
14251 				       struct dma_fence *fence)
14252 {
14253 	struct wait_rps_boost *wait;
14254 
14255 	if (!dma_fence_is_i915(fence))
14256 		return;
14257 
14258 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
14259 		return;
14260 
14261 	if (drm_crtc_vblank_get(crtc))
14262 		return;
14263 
14264 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
14265 	if (!wait) {
14266 		drm_crtc_vblank_put(crtc);
14267 		return;
14268 	}
14269 
14270 	wait->request = to_request(dma_fence_get(fence));
14271 	wait->crtc = crtc;
14272 
14273 	wait->wait.func = do_rps_boost;
14274 	wait->wait.flags = 0;
14275 
14276 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
14277 }
14278 
14279 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
14280 {
14281 	struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
14282 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
14283 	struct drm_framebuffer *fb = plane_state->base.fb;
14284 	struct i915_vma *vma;
14285 
14286 	if (plane->id == PLANE_CURSOR &&
14287 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
14288 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
14289 		const int align = intel_cursor_alignment(dev_priv);
14290 		int err;
14291 
14292 		err = i915_gem_object_attach_phys(obj, align);
14293 		if (err)
14294 			return err;
14295 	}
14296 
14297 	vma = intel_pin_and_fence_fb_obj(fb,
14298 					 &plane_state->view,
14299 					 intel_plane_uses_fence(plane_state),
14300 					 &plane_state->flags);
14301 	if (IS_ERR(vma))
14302 		return PTR_ERR(vma);
14303 
14304 	plane_state->vma = vma;
14305 
14306 	return 0;
14307 }
14308 
14309 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
14310 {
14311 	struct i915_vma *vma;
14312 
14313 	vma = fetch_and_zero(&old_plane_state->vma);
14314 	if (vma)
14315 		intel_unpin_fb_vma(vma, old_plane_state->flags);
14316 }
14317 
14318 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
14319 {
14320 	struct i915_sched_attr attr = {
14321 		.priority = I915_PRIORITY_DISPLAY,
14322 	};
14323 
14324 	i915_gem_object_wait_priority(obj, 0, &attr);
14325 }
14326 
14327 /**
14328  * intel_prepare_plane_fb - Prepare fb for usage on plane
14329  * @plane: drm plane to prepare for
14330  * @new_state: the plane state being prepared
14331  *
14332  * Prepares a framebuffer for usage on a display plane.  Generally this
14333  * involves pinning the underlying object and updating the frontbuffer tracking
14334  * bits.  Some older platforms need special physical address handling for
14335  * cursor planes.
14336  *
14337  * Must be called with struct_mutex held.
14338  *
14339  * Returns 0 on success, negative error code on failure.
14340  */
14341 int
14342 intel_prepare_plane_fb(struct drm_plane *plane,
14343 		       struct drm_plane_state *new_state)
14344 {
14345 	struct intel_atomic_state *intel_state =
14346 		to_intel_atomic_state(new_state->state);
14347 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
14348 	struct drm_framebuffer *fb = new_state->fb;
14349 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
14350 	struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
14351 	int ret;
14352 
14353 	if (old_obj) {
14354 		struct intel_crtc_state *crtc_state =
14355 			intel_atomic_get_new_crtc_state(intel_state,
14356 							to_intel_crtc(plane->state->crtc));
14357 
14358 		/* Big Hammer, we also need to ensure that any pending
14359 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14360 		 * current scanout is retired before unpinning the old
14361 		 * framebuffer. Note that we rely on userspace rendering
14362 		 * into the buffer attached to the pipe they are waiting
14363 		 * on. If not, userspace generates a GPU hang with IPEHR
14364 		 * point to the MI_WAIT_FOR_EVENT.
14365 		 *
14366 		 * This should only fail upon a hung GPU, in which case we
14367 		 * can safely continue.
14368 		 */
14369 		if (needs_modeset(crtc_state)) {
14370 			ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14371 							      old_obj->base.resv, NULL,
14372 							      false, 0,
14373 							      GFP_KERNEL);
14374 			if (ret < 0)
14375 				return ret;
14376 		}
14377 	}
14378 
14379 	if (new_state->fence) { /* explicit fencing */
14380 		ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14381 						    new_state->fence,
14382 						    I915_FENCE_TIMEOUT,
14383 						    GFP_KERNEL);
14384 		if (ret < 0)
14385 			return ret;
14386 	}
14387 
14388 	if (!obj)
14389 		return 0;
14390 
14391 	ret = i915_gem_object_pin_pages(obj);
14392 	if (ret)
14393 		return ret;
14394 
14395 	ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
14396 	if (ret) {
14397 		i915_gem_object_unpin_pages(obj);
14398 		return ret;
14399 	}
14400 
14401 	ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
14402 
14403 	mutex_unlock(&dev_priv->drm.struct_mutex);
14404 	i915_gem_object_unpin_pages(obj);
14405 	if (ret)
14406 		return ret;
14407 
14408 	fb_obj_bump_render_priority(obj);
14409 	intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_DIRTYFB);
14410 
14411 	if (!new_state->fence) { /* implicit fencing */
14412 		struct dma_fence *fence;
14413 
14414 		ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14415 						      obj->base.resv, NULL,
14416 						      false, I915_FENCE_TIMEOUT,
14417 						      GFP_KERNEL);
14418 		if (ret < 0)
14419 			return ret;
14420 
14421 		fence = dma_resv_get_excl_rcu(obj->base.resv);
14422 		if (fence) {
14423 			add_rps_boost_after_vblank(new_state->crtc, fence);
14424 			dma_fence_put(fence);
14425 		}
14426 	} else {
14427 		add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
14428 	}
14429 
14430 	/*
14431 	 * We declare pageflips to be interactive and so merit a small bias
14432 	 * towards upclocking to deliver the frame on time. By only changing
14433 	 * the RPS thresholds to sample more regularly and aim for higher
14434 	 * clocks we can hopefully deliver low power workloads (like kodi)
14435 	 * that are not quite steady state without resorting to forcing
14436 	 * maximum clocks following a vblank miss (see do_rps_boost()).
14437 	 */
14438 	if (!intel_state->rps_interactive) {
14439 		intel_rps_mark_interactive(dev_priv, true);
14440 		intel_state->rps_interactive = true;
14441 	}
14442 
14443 	return 0;
14444 }
14445 
14446 /**
14447  * intel_cleanup_plane_fb - Cleans up an fb after plane use
14448  * @plane: drm plane to clean up for
14449  * @old_state: the state from the previous modeset
14450  *
14451  * Cleans up a framebuffer that has just been removed from a plane.
14452  *
14453  * Must be called with struct_mutex held.
14454  */
14455 void
14456 intel_cleanup_plane_fb(struct drm_plane *plane,
14457 		       struct drm_plane_state *old_state)
14458 {
14459 	struct intel_atomic_state *intel_state =
14460 		to_intel_atomic_state(old_state->state);
14461 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
14462 
14463 	if (intel_state->rps_interactive) {
14464 		intel_rps_mark_interactive(dev_priv, false);
14465 		intel_state->rps_interactive = false;
14466 	}
14467 
14468 	/* Should only be called after a successful intel_prepare_plane_fb()! */
14469 	mutex_lock(&dev_priv->drm.struct_mutex);
14470 	intel_plane_unpin_fb(to_intel_plane_state(old_state));
14471 	mutex_unlock(&dev_priv->drm.struct_mutex);
14472 }
14473 
14474 int
14475 skl_max_scale(const struct intel_crtc_state *crtc_state,
14476 	      u32 pixel_format)
14477 {
14478 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
14479 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14480 	int max_scale, mult;
14481 	int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
14482 
14483 	if (!crtc_state->base.enable)
14484 		return DRM_PLANE_HELPER_NO_SCALING;
14485 
14486 	crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
14487 	max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
14488 
14489 	if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
14490 		max_dotclk *= 2;
14491 
14492 	if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
14493 		return DRM_PLANE_HELPER_NO_SCALING;
14494 
14495 	/*
14496 	 * skl max scale is lower of:
14497 	 *    close to 3 but not 3, -1 is for that purpose
14498 	 *            or
14499 	 *    cdclk/crtc_clock
14500 	 */
14501 	mult = is_planar_yuv_format(pixel_format) ? 2 : 3;
14502 	tmpclk1 = (1 << 16) * mult - 1;
14503 	tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
14504 	max_scale = min(tmpclk1, tmpclk2);
14505 
14506 	return max_scale;
14507 }
14508 
14509 static void intel_begin_crtc_commit(struct intel_atomic_state *state,
14510 				    struct intel_crtc *crtc)
14511 {
14512 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14513 	struct intel_crtc_state *old_crtc_state =
14514 		intel_atomic_get_old_crtc_state(state, crtc);
14515 	struct intel_crtc_state *new_crtc_state =
14516 		intel_atomic_get_new_crtc_state(state, crtc);
14517 	bool modeset = needs_modeset(new_crtc_state);
14518 
14519 	/* Perform vblank evasion around commit operation */
14520 	intel_pipe_update_start(new_crtc_state);
14521 
14522 	if (modeset)
14523 		goto out;
14524 
14525 	if (new_crtc_state->base.color_mgmt_changed ||
14526 	    new_crtc_state->update_pipe)
14527 		intel_color_commit(new_crtc_state);
14528 
14529 	if (new_crtc_state->update_pipe)
14530 		intel_update_pipe_config(old_crtc_state, new_crtc_state);
14531 	else if (INTEL_GEN(dev_priv) >= 9)
14532 		skl_detach_scalers(new_crtc_state);
14533 
14534 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
14535 		bdw_set_pipemisc(new_crtc_state);
14536 
14537 out:
14538 	if (dev_priv->display.atomic_update_watermarks)
14539 		dev_priv->display.atomic_update_watermarks(state,
14540 							   new_crtc_state);
14541 }
14542 
14543 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
14544 				  struct intel_crtc_state *crtc_state)
14545 {
14546 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14547 
14548 	if (!IS_GEN(dev_priv, 2))
14549 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
14550 
14551 	if (crtc_state->has_pch_encoder) {
14552 		enum pipe pch_transcoder =
14553 			intel_crtc_pch_transcoder(crtc);
14554 
14555 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
14556 	}
14557 }
14558 
14559 static void intel_finish_crtc_commit(struct intel_atomic_state *state,
14560 				     struct intel_crtc *crtc)
14561 {
14562 	struct intel_crtc_state *old_crtc_state =
14563 		intel_atomic_get_old_crtc_state(state, crtc);
14564 	struct intel_crtc_state *new_crtc_state =
14565 		intel_atomic_get_new_crtc_state(state, crtc);
14566 
14567 	intel_pipe_update_end(new_crtc_state);
14568 
14569 	if (new_crtc_state->update_pipe &&
14570 	    !needs_modeset(new_crtc_state) &&
14571 	    old_crtc_state->base.mode.private_flags & I915_MODE_FLAG_INHERITED)
14572 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
14573 }
14574 
14575 /**
14576  * intel_plane_destroy - destroy a plane
14577  * @plane: plane to destroy
14578  *
14579  * Common destruction function for all types of planes (primary, cursor,
14580  * sprite).
14581  */
14582 void intel_plane_destroy(struct drm_plane *plane)
14583 {
14584 	drm_plane_cleanup(plane);
14585 	kfree(to_intel_plane(plane));
14586 }
14587 
14588 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
14589 					    u32 format, u64 modifier)
14590 {
14591 	switch (modifier) {
14592 	case DRM_FORMAT_MOD_LINEAR:
14593 	case I915_FORMAT_MOD_X_TILED:
14594 		break;
14595 	default:
14596 		return false;
14597 	}
14598 
14599 	switch (format) {
14600 	case DRM_FORMAT_C8:
14601 	case DRM_FORMAT_RGB565:
14602 	case DRM_FORMAT_XRGB1555:
14603 	case DRM_FORMAT_XRGB8888:
14604 		return modifier == DRM_FORMAT_MOD_LINEAR ||
14605 			modifier == I915_FORMAT_MOD_X_TILED;
14606 	default:
14607 		return false;
14608 	}
14609 }
14610 
14611 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
14612 					    u32 format, u64 modifier)
14613 {
14614 	switch (modifier) {
14615 	case DRM_FORMAT_MOD_LINEAR:
14616 	case I915_FORMAT_MOD_X_TILED:
14617 		break;
14618 	default:
14619 		return false;
14620 	}
14621 
14622 	switch (format) {
14623 	case DRM_FORMAT_C8:
14624 	case DRM_FORMAT_RGB565:
14625 	case DRM_FORMAT_XRGB8888:
14626 	case DRM_FORMAT_XBGR8888:
14627 	case DRM_FORMAT_XRGB2101010:
14628 	case DRM_FORMAT_XBGR2101010:
14629 		return modifier == DRM_FORMAT_MOD_LINEAR ||
14630 			modifier == I915_FORMAT_MOD_X_TILED;
14631 	default:
14632 		return false;
14633 	}
14634 }
14635 
14636 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
14637 					      u32 format, u64 modifier)
14638 {
14639 	return modifier == DRM_FORMAT_MOD_LINEAR &&
14640 		format == DRM_FORMAT_ARGB8888;
14641 }
14642 
14643 static const struct drm_plane_funcs i965_plane_funcs = {
14644 	.update_plane = drm_atomic_helper_update_plane,
14645 	.disable_plane = drm_atomic_helper_disable_plane,
14646 	.destroy = intel_plane_destroy,
14647 	.atomic_duplicate_state = intel_plane_duplicate_state,
14648 	.atomic_destroy_state = intel_plane_destroy_state,
14649 	.format_mod_supported = i965_plane_format_mod_supported,
14650 };
14651 
14652 static const struct drm_plane_funcs i8xx_plane_funcs = {
14653 	.update_plane = drm_atomic_helper_update_plane,
14654 	.disable_plane = drm_atomic_helper_disable_plane,
14655 	.destroy = intel_plane_destroy,
14656 	.atomic_duplicate_state = intel_plane_duplicate_state,
14657 	.atomic_destroy_state = intel_plane_destroy_state,
14658 	.format_mod_supported = i8xx_plane_format_mod_supported,
14659 };
14660 
14661 static int
14662 intel_legacy_cursor_update(struct drm_plane *plane,
14663 			   struct drm_crtc *crtc,
14664 			   struct drm_framebuffer *fb,
14665 			   int crtc_x, int crtc_y,
14666 			   unsigned int crtc_w, unsigned int crtc_h,
14667 			   u32 src_x, u32 src_y,
14668 			   u32 src_w, u32 src_h,
14669 			   struct drm_modeset_acquire_ctx *ctx)
14670 {
14671 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
14672 	struct drm_plane_state *old_plane_state, *new_plane_state;
14673 	struct intel_plane *intel_plane = to_intel_plane(plane);
14674 	struct intel_crtc_state *crtc_state =
14675 		to_intel_crtc_state(crtc->state);
14676 	struct intel_crtc_state *new_crtc_state;
14677 	int ret;
14678 
14679 	/*
14680 	 * When crtc is inactive or there is a modeset pending,
14681 	 * wait for it to complete in the slowpath
14682 	 */
14683 	if (!crtc_state->base.active || needs_modeset(crtc_state) ||
14684 	    crtc_state->update_pipe)
14685 		goto slow;
14686 
14687 	old_plane_state = plane->state;
14688 	/*
14689 	 * Don't do an async update if there is an outstanding commit modifying
14690 	 * the plane.  This prevents our async update's changes from getting
14691 	 * overridden by a previous synchronous update's state.
14692 	 */
14693 	if (old_plane_state->commit &&
14694 	    !try_wait_for_completion(&old_plane_state->commit->hw_done))
14695 		goto slow;
14696 
14697 	/*
14698 	 * If any parameters change that may affect watermarks,
14699 	 * take the slowpath. Only changing fb or position should be
14700 	 * in the fastpath.
14701 	 */
14702 	if (old_plane_state->crtc != crtc ||
14703 	    old_plane_state->src_w != src_w ||
14704 	    old_plane_state->src_h != src_h ||
14705 	    old_plane_state->crtc_w != crtc_w ||
14706 	    old_plane_state->crtc_h != crtc_h ||
14707 	    !old_plane_state->fb != !fb)
14708 		goto slow;
14709 
14710 	new_plane_state = intel_plane_duplicate_state(plane);
14711 	if (!new_plane_state)
14712 		return -ENOMEM;
14713 
14714 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(crtc));
14715 	if (!new_crtc_state) {
14716 		ret = -ENOMEM;
14717 		goto out_free;
14718 	}
14719 
14720 	drm_atomic_set_fb_for_plane(new_plane_state, fb);
14721 
14722 	new_plane_state->src_x = src_x;
14723 	new_plane_state->src_y = src_y;
14724 	new_plane_state->src_w = src_w;
14725 	new_plane_state->src_h = src_h;
14726 	new_plane_state->crtc_x = crtc_x;
14727 	new_plane_state->crtc_y = crtc_y;
14728 	new_plane_state->crtc_w = crtc_w;
14729 	new_plane_state->crtc_h = crtc_h;
14730 
14731 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
14732 						  to_intel_plane_state(old_plane_state),
14733 						  to_intel_plane_state(new_plane_state));
14734 	if (ret)
14735 		goto out_free;
14736 
14737 	ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
14738 	if (ret)
14739 		goto out_free;
14740 
14741 	ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
14742 	if (ret)
14743 		goto out_unlock;
14744 
14745 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_FLIP);
14746 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->fb),
14747 				to_intel_frontbuffer(fb),
14748 				intel_plane->frontbuffer_bit);
14749 
14750 	/* Swap plane state */
14751 	plane->state = new_plane_state;
14752 
14753 	/*
14754 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
14755 	 * page flip that's running simultaneously. If we swap crtc_state and
14756 	 * destroy the old state, we will cause a use-after-free there.
14757 	 *
14758 	 * Only update active_planes, which is needed for our internal
14759 	 * bookkeeping. Either value will do the right thing when updating
14760 	 * planes atomically. If the cursor was part of the atomic update then
14761 	 * we would have taken the slowpath.
14762 	 */
14763 	crtc_state->active_planes = new_crtc_state->active_planes;
14764 
14765 	if (plane->state->visible)
14766 		intel_update_plane(intel_plane, crtc_state,
14767 				   to_intel_plane_state(plane->state));
14768 	else
14769 		intel_disable_plane(intel_plane, crtc_state);
14770 
14771 	intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
14772 
14773 out_unlock:
14774 	mutex_unlock(&dev_priv->drm.struct_mutex);
14775 out_free:
14776 	if (new_crtc_state)
14777 		intel_crtc_destroy_state(crtc, &new_crtc_state->base);
14778 	if (ret)
14779 		intel_plane_destroy_state(plane, new_plane_state);
14780 	else
14781 		intel_plane_destroy_state(plane, old_plane_state);
14782 	return ret;
14783 
14784 slow:
14785 	return drm_atomic_helper_update_plane(plane, crtc, fb,
14786 					      crtc_x, crtc_y, crtc_w, crtc_h,
14787 					      src_x, src_y, src_w, src_h, ctx);
14788 }
14789 
14790 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
14791 	.update_plane = intel_legacy_cursor_update,
14792 	.disable_plane = drm_atomic_helper_disable_plane,
14793 	.destroy = intel_plane_destroy,
14794 	.atomic_duplicate_state = intel_plane_duplicate_state,
14795 	.atomic_destroy_state = intel_plane_destroy_state,
14796 	.format_mod_supported = intel_cursor_format_mod_supported,
14797 };
14798 
14799 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
14800 			       enum i9xx_plane_id i9xx_plane)
14801 {
14802 	if (!HAS_FBC(dev_priv))
14803 		return false;
14804 
14805 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
14806 		return i9xx_plane == PLANE_A; /* tied to pipe A */
14807 	else if (IS_IVYBRIDGE(dev_priv))
14808 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
14809 			i9xx_plane == PLANE_C;
14810 	else if (INTEL_GEN(dev_priv) >= 4)
14811 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
14812 	else
14813 		return i9xx_plane == PLANE_A;
14814 }
14815 
14816 static struct intel_plane *
14817 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
14818 {
14819 	struct intel_plane *plane;
14820 	const struct drm_plane_funcs *plane_funcs;
14821 	unsigned int supported_rotations;
14822 	unsigned int possible_crtcs;
14823 	const u64 *modifiers;
14824 	const u32 *formats;
14825 	int num_formats;
14826 	int ret;
14827 
14828 	if (INTEL_GEN(dev_priv) >= 9)
14829 		return skl_universal_plane_create(dev_priv, pipe,
14830 						  PLANE_PRIMARY);
14831 
14832 	plane = intel_plane_alloc();
14833 	if (IS_ERR(plane))
14834 		return plane;
14835 
14836 	plane->pipe = pipe;
14837 	/*
14838 	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
14839 	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
14840 	 */
14841 	if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
14842 		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
14843 	else
14844 		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
14845 	plane->id = PLANE_PRIMARY;
14846 	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
14847 
14848 	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
14849 	if (plane->has_fbc) {
14850 		struct intel_fbc *fbc = &dev_priv->fbc;
14851 
14852 		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
14853 	}
14854 
14855 	if (INTEL_GEN(dev_priv) >= 4) {
14856 		formats = i965_primary_formats;
14857 		num_formats = ARRAY_SIZE(i965_primary_formats);
14858 		modifiers = i9xx_format_modifiers;
14859 
14860 		plane->max_stride = i9xx_plane_max_stride;
14861 		plane->update_plane = i9xx_update_plane;
14862 		plane->disable_plane = i9xx_disable_plane;
14863 		plane->get_hw_state = i9xx_plane_get_hw_state;
14864 		plane->check_plane = i9xx_plane_check;
14865 
14866 		plane_funcs = &i965_plane_funcs;
14867 	} else {
14868 		formats = i8xx_primary_formats;
14869 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
14870 		modifiers = i9xx_format_modifiers;
14871 
14872 		plane->max_stride = i9xx_plane_max_stride;
14873 		plane->update_plane = i9xx_update_plane;
14874 		plane->disable_plane = i9xx_disable_plane;
14875 		plane->get_hw_state = i9xx_plane_get_hw_state;
14876 		plane->check_plane = i9xx_plane_check;
14877 
14878 		plane_funcs = &i8xx_plane_funcs;
14879 	}
14880 
14881 	possible_crtcs = BIT(pipe);
14882 
14883 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
14884 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
14885 					       possible_crtcs, plane_funcs,
14886 					       formats, num_formats, modifiers,
14887 					       DRM_PLANE_TYPE_PRIMARY,
14888 					       "primary %c", pipe_name(pipe));
14889 	else
14890 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
14891 					       possible_crtcs, plane_funcs,
14892 					       formats, num_formats, modifiers,
14893 					       DRM_PLANE_TYPE_PRIMARY,
14894 					       "plane %c",
14895 					       plane_name(plane->i9xx_plane));
14896 	if (ret)
14897 		goto fail;
14898 
14899 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
14900 		supported_rotations =
14901 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
14902 			DRM_MODE_REFLECT_X;
14903 	} else if (INTEL_GEN(dev_priv) >= 4) {
14904 		supported_rotations =
14905 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
14906 	} else {
14907 		supported_rotations = DRM_MODE_ROTATE_0;
14908 	}
14909 
14910 	if (INTEL_GEN(dev_priv) >= 4)
14911 		drm_plane_create_rotation_property(&plane->base,
14912 						   DRM_MODE_ROTATE_0,
14913 						   supported_rotations);
14914 
14915 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
14916 
14917 	return plane;
14918 
14919 fail:
14920 	intel_plane_free(plane);
14921 
14922 	return ERR_PTR(ret);
14923 }
14924 
14925 static struct intel_plane *
14926 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
14927 			  enum pipe pipe)
14928 {
14929 	unsigned int possible_crtcs;
14930 	struct intel_plane *cursor;
14931 	int ret;
14932 
14933 	cursor = intel_plane_alloc();
14934 	if (IS_ERR(cursor))
14935 		return cursor;
14936 
14937 	cursor->pipe = pipe;
14938 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
14939 	cursor->id = PLANE_CURSOR;
14940 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
14941 
14942 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
14943 		cursor->max_stride = i845_cursor_max_stride;
14944 		cursor->update_plane = i845_update_cursor;
14945 		cursor->disable_plane = i845_disable_cursor;
14946 		cursor->get_hw_state = i845_cursor_get_hw_state;
14947 		cursor->check_plane = i845_check_cursor;
14948 	} else {
14949 		cursor->max_stride = i9xx_cursor_max_stride;
14950 		cursor->update_plane = i9xx_update_cursor;
14951 		cursor->disable_plane = i9xx_disable_cursor;
14952 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
14953 		cursor->check_plane = i9xx_check_cursor;
14954 	}
14955 
14956 	cursor->cursor.base = ~0;
14957 	cursor->cursor.cntl = ~0;
14958 
14959 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
14960 		cursor->cursor.size = ~0;
14961 
14962 	possible_crtcs = BIT(pipe);
14963 
14964 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
14965 				       possible_crtcs, &intel_cursor_plane_funcs,
14966 				       intel_cursor_formats,
14967 				       ARRAY_SIZE(intel_cursor_formats),
14968 				       cursor_format_modifiers,
14969 				       DRM_PLANE_TYPE_CURSOR,
14970 				       "cursor %c", pipe_name(pipe));
14971 	if (ret)
14972 		goto fail;
14973 
14974 	if (INTEL_GEN(dev_priv) >= 4)
14975 		drm_plane_create_rotation_property(&cursor->base,
14976 						   DRM_MODE_ROTATE_0,
14977 						   DRM_MODE_ROTATE_0 |
14978 						   DRM_MODE_ROTATE_180);
14979 
14980 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14981 
14982 	return cursor;
14983 
14984 fail:
14985 	intel_plane_free(cursor);
14986 
14987 	return ERR_PTR(ret);
14988 }
14989 
14990 static void intel_crtc_init_scalers(struct intel_crtc *crtc,
14991 				    struct intel_crtc_state *crtc_state)
14992 {
14993 	struct intel_crtc_scaler_state *scaler_state =
14994 		&crtc_state->scaler_state;
14995 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14996 	int i;
14997 
14998 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[crtc->pipe];
14999 	if (!crtc->num_scalers)
15000 		return;
15001 
15002 	for (i = 0; i < crtc->num_scalers; i++) {
15003 		struct intel_scaler *scaler = &scaler_state->scalers[i];
15004 
15005 		scaler->in_use = 0;
15006 		scaler->mode = 0;
15007 	}
15008 
15009 	scaler_state->scaler_id = -1;
15010 }
15011 
15012 #define INTEL_CRTC_FUNCS \
15013 	.gamma_set = drm_atomic_helper_legacy_gamma_set, \
15014 	.set_config = drm_atomic_helper_set_config, \
15015 	.destroy = intel_crtc_destroy, \
15016 	.page_flip = drm_atomic_helper_page_flip, \
15017 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
15018 	.atomic_destroy_state = intel_crtc_destroy_state, \
15019 	.set_crc_source = intel_crtc_set_crc_source, \
15020 	.verify_crc_source = intel_crtc_verify_crc_source, \
15021 	.get_crc_sources = intel_crtc_get_crc_sources
15022 
15023 static const struct drm_crtc_funcs bdw_crtc_funcs = {
15024 	INTEL_CRTC_FUNCS,
15025 
15026 	.get_vblank_counter = g4x_get_vblank_counter,
15027 	.enable_vblank = bdw_enable_vblank,
15028 	.disable_vblank = bdw_disable_vblank,
15029 };
15030 
15031 static const struct drm_crtc_funcs ilk_crtc_funcs = {
15032 	INTEL_CRTC_FUNCS,
15033 
15034 	.get_vblank_counter = g4x_get_vblank_counter,
15035 	.enable_vblank = ilk_enable_vblank,
15036 	.disable_vblank = ilk_disable_vblank,
15037 };
15038 
15039 static const struct drm_crtc_funcs g4x_crtc_funcs = {
15040 	INTEL_CRTC_FUNCS,
15041 
15042 	.get_vblank_counter = g4x_get_vblank_counter,
15043 	.enable_vblank = i965_enable_vblank,
15044 	.disable_vblank = i965_disable_vblank,
15045 };
15046 
15047 static const struct drm_crtc_funcs i965_crtc_funcs = {
15048 	INTEL_CRTC_FUNCS,
15049 
15050 	.get_vblank_counter = i915_get_vblank_counter,
15051 	.enable_vblank = i965_enable_vblank,
15052 	.disable_vblank = i965_disable_vblank,
15053 };
15054 
15055 static const struct drm_crtc_funcs i945gm_crtc_funcs = {
15056 	INTEL_CRTC_FUNCS,
15057 
15058 	.get_vblank_counter = i915_get_vblank_counter,
15059 	.enable_vblank = i945gm_enable_vblank,
15060 	.disable_vblank = i945gm_disable_vblank,
15061 };
15062 
15063 static const struct drm_crtc_funcs i915_crtc_funcs = {
15064 	INTEL_CRTC_FUNCS,
15065 
15066 	.get_vblank_counter = i915_get_vblank_counter,
15067 	.enable_vblank = i8xx_enable_vblank,
15068 	.disable_vblank = i8xx_disable_vblank,
15069 };
15070 
15071 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
15072 	INTEL_CRTC_FUNCS,
15073 
15074 	/* no hw vblank counter */
15075 	.enable_vblank = i8xx_enable_vblank,
15076 	.disable_vblank = i8xx_disable_vblank,
15077 };
15078 
15079 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
15080 {
15081 	const struct drm_crtc_funcs *funcs;
15082 	struct intel_crtc *intel_crtc;
15083 	struct intel_crtc_state *crtc_state = NULL;
15084 	struct intel_plane *primary = NULL;
15085 	struct intel_plane *cursor = NULL;
15086 	int sprite, ret;
15087 
15088 	intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
15089 	if (!intel_crtc)
15090 		return -ENOMEM;
15091 
15092 	crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
15093 	if (!crtc_state) {
15094 		ret = -ENOMEM;
15095 		goto fail;
15096 	}
15097 	__drm_atomic_helper_crtc_reset(&intel_crtc->base, &crtc_state->base);
15098 	intel_crtc->config = crtc_state;
15099 
15100 	primary = intel_primary_plane_create(dev_priv, pipe);
15101 	if (IS_ERR(primary)) {
15102 		ret = PTR_ERR(primary);
15103 		goto fail;
15104 	}
15105 	intel_crtc->plane_ids_mask |= BIT(primary->id);
15106 
15107 	for_each_sprite(dev_priv, pipe, sprite) {
15108 		struct intel_plane *plane;
15109 
15110 		plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
15111 		if (IS_ERR(plane)) {
15112 			ret = PTR_ERR(plane);
15113 			goto fail;
15114 		}
15115 		intel_crtc->plane_ids_mask |= BIT(plane->id);
15116 	}
15117 
15118 	cursor = intel_cursor_plane_create(dev_priv, pipe);
15119 	if (IS_ERR(cursor)) {
15120 		ret = PTR_ERR(cursor);
15121 		goto fail;
15122 	}
15123 	intel_crtc->plane_ids_mask |= BIT(cursor->id);
15124 
15125 	if (HAS_GMCH(dev_priv)) {
15126 		if (IS_CHERRYVIEW(dev_priv) ||
15127 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
15128 			funcs = &g4x_crtc_funcs;
15129 		else if (IS_GEN(dev_priv, 4))
15130 			funcs = &i965_crtc_funcs;
15131 		else if (IS_I945GM(dev_priv))
15132 			funcs = &i945gm_crtc_funcs;
15133 		else if (IS_GEN(dev_priv, 3))
15134 			funcs = &i915_crtc_funcs;
15135 		else
15136 			funcs = &i8xx_crtc_funcs;
15137 	} else {
15138 		if (INTEL_GEN(dev_priv) >= 8)
15139 			funcs = &bdw_crtc_funcs;
15140 		else
15141 			funcs = &ilk_crtc_funcs;
15142 	}
15143 
15144 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
15145 					&primary->base, &cursor->base,
15146 					funcs, "pipe %c", pipe_name(pipe));
15147 	if (ret)
15148 		goto fail;
15149 
15150 	intel_crtc->pipe = pipe;
15151 
15152 	/* initialize shared scalers */
15153 	intel_crtc_init_scalers(intel_crtc, crtc_state);
15154 
15155 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
15156 	       dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
15157 	dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
15158 
15159 	if (INTEL_GEN(dev_priv) < 9) {
15160 		enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
15161 
15162 		BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15163 		       dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
15164 		dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
15165 	}
15166 
15167 	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
15168 
15169 	intel_color_init(intel_crtc);
15170 
15171 	WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
15172 
15173 	return 0;
15174 
15175 fail:
15176 	/*
15177 	 * drm_mode_config_cleanup() will free up any
15178 	 * crtcs/planes already initialized.
15179 	 */
15180 	kfree(crtc_state);
15181 	kfree(intel_crtc);
15182 
15183 	return ret;
15184 }
15185 
15186 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
15187 				      struct drm_file *file)
15188 {
15189 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
15190 	struct drm_crtc *drmmode_crtc;
15191 	struct intel_crtc *crtc;
15192 
15193 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
15194 	if (!drmmode_crtc)
15195 		return -ENOENT;
15196 
15197 	crtc = to_intel_crtc(drmmode_crtc);
15198 	pipe_from_crtc_id->pipe = crtc->pipe;
15199 
15200 	return 0;
15201 }
15202 
15203 static int intel_encoder_clones(struct intel_encoder *encoder)
15204 {
15205 	struct drm_device *dev = encoder->base.dev;
15206 	struct intel_encoder *source_encoder;
15207 	int index_mask = 0;
15208 	int entry = 0;
15209 
15210 	for_each_intel_encoder(dev, source_encoder) {
15211 		if (encoders_cloneable(encoder, source_encoder))
15212 			index_mask |= (1 << entry);
15213 
15214 		entry++;
15215 	}
15216 
15217 	return index_mask;
15218 }
15219 
15220 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
15221 {
15222 	if (!IS_MOBILE(dev_priv))
15223 		return false;
15224 
15225 	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15226 		return false;
15227 
15228 	if (IS_GEN(dev_priv, 5) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
15229 		return false;
15230 
15231 	return true;
15232 }
15233 
15234 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
15235 {
15236 	if (INTEL_GEN(dev_priv) >= 9)
15237 		return false;
15238 
15239 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
15240 		return false;
15241 
15242 	if (HAS_PCH_LPT_H(dev_priv) &&
15243 	    I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
15244 		return false;
15245 
15246 	/* DDI E can't be used if DDI A requires 4 lanes */
15247 	if (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
15248 		return false;
15249 
15250 	if (!dev_priv->vbt.int_crt_support)
15251 		return false;
15252 
15253 	return true;
15254 }
15255 
15256 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15257 {
15258 	int pps_num;
15259 	int pps_idx;
15260 
15261 	if (HAS_DDI(dev_priv))
15262 		return;
15263 	/*
15264 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15265 	 * everywhere where registers can be write protected.
15266 	 */
15267 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15268 		pps_num = 2;
15269 	else
15270 		pps_num = 1;
15271 
15272 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15273 		u32 val = I915_READ(PP_CONTROL(pps_idx));
15274 
15275 		val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15276 		I915_WRITE(PP_CONTROL(pps_idx), val);
15277 	}
15278 }
15279 
15280 static void intel_pps_init(struct drm_i915_private *dev_priv)
15281 {
15282 	if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
15283 		dev_priv->pps_mmio_base = PCH_PPS_BASE;
15284 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15285 		dev_priv->pps_mmio_base = VLV_PPS_BASE;
15286 	else
15287 		dev_priv->pps_mmio_base = PPS_BASE;
15288 
15289 	intel_pps_unlock_regs_wa(dev_priv);
15290 }
15291 
15292 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
15293 {
15294 	struct intel_encoder *encoder;
15295 	bool dpd_is_edp = false;
15296 
15297 	intel_pps_init(dev_priv);
15298 
15299 	if (!HAS_DISPLAY(dev_priv))
15300 		return;
15301 
15302 	if (INTEL_GEN(dev_priv) >= 12) {
15303 		/* TODO: initialize TC ports as well */
15304 		intel_ddi_init(dev_priv, PORT_A);
15305 		intel_ddi_init(dev_priv, PORT_B);
15306 		icl_dsi_init(dev_priv);
15307 	} else if (IS_ELKHARTLAKE(dev_priv)) {
15308 		intel_ddi_init(dev_priv, PORT_A);
15309 		intel_ddi_init(dev_priv, PORT_B);
15310 		intel_ddi_init(dev_priv, PORT_C);
15311 		intel_ddi_init(dev_priv, PORT_D);
15312 		icl_dsi_init(dev_priv);
15313 	} else if (IS_GEN(dev_priv, 11)) {
15314 		intel_ddi_init(dev_priv, PORT_A);
15315 		intel_ddi_init(dev_priv, PORT_B);
15316 		intel_ddi_init(dev_priv, PORT_C);
15317 		intel_ddi_init(dev_priv, PORT_D);
15318 		intel_ddi_init(dev_priv, PORT_E);
15319 		/*
15320 		 * On some ICL SKUs port F is not present. No strap bits for
15321 		 * this, so rely on VBT.
15322 		 * Work around broken VBTs on SKUs known to have no port F.
15323 		 */
15324 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
15325 		    intel_bios_is_port_present(dev_priv, PORT_F))
15326 			intel_ddi_init(dev_priv, PORT_F);
15327 
15328 		icl_dsi_init(dev_priv);
15329 	} else if (IS_GEN9_LP(dev_priv)) {
15330 		/*
15331 		 * FIXME: Broxton doesn't support port detection via the
15332 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15333 		 * detect the ports.
15334 		 */
15335 		intel_ddi_init(dev_priv, PORT_A);
15336 		intel_ddi_init(dev_priv, PORT_B);
15337 		intel_ddi_init(dev_priv, PORT_C);
15338 
15339 		vlv_dsi_init(dev_priv);
15340 	} else if (HAS_DDI(dev_priv)) {
15341 		int found;
15342 
15343 		if (intel_ddi_crt_present(dev_priv))
15344 			intel_crt_init(dev_priv);
15345 
15346 		/*
15347 		 * Haswell uses DDI functions to detect digital outputs.
15348 		 * On SKL pre-D0 the strap isn't connected, so we assume
15349 		 * it's there.
15350 		 */
15351 		found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
15352 		/* WaIgnoreDDIAStrap: skl */
15353 		if (found || IS_GEN9_BC(dev_priv))
15354 			intel_ddi_init(dev_priv, PORT_A);
15355 
15356 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
15357 		 * register */
15358 		found = I915_READ(SFUSE_STRAP);
15359 
15360 		if (found & SFUSE_STRAP_DDIB_DETECTED)
15361 			intel_ddi_init(dev_priv, PORT_B);
15362 		if (found & SFUSE_STRAP_DDIC_DETECTED)
15363 			intel_ddi_init(dev_priv, PORT_C);
15364 		if (found & SFUSE_STRAP_DDID_DETECTED)
15365 			intel_ddi_init(dev_priv, PORT_D);
15366 		if (found & SFUSE_STRAP_DDIF_DETECTED)
15367 			intel_ddi_init(dev_priv, PORT_F);
15368 		/*
15369 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15370 		 */
15371 		if (IS_GEN9_BC(dev_priv) &&
15372 		    intel_bios_is_port_present(dev_priv, PORT_E))
15373 			intel_ddi_init(dev_priv, PORT_E);
15374 
15375 	} else if (HAS_PCH_SPLIT(dev_priv)) {
15376 		int found;
15377 
15378 		/*
15379 		 * intel_edp_init_connector() depends on this completing first,
15380 		 * to prevent the registration of both eDP and LVDS and the
15381 		 * incorrect sharing of the PPS.
15382 		 */
15383 		intel_lvds_init(dev_priv);
15384 		intel_crt_init(dev_priv);
15385 
15386 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
15387 
15388 		if (ilk_has_edp_a(dev_priv))
15389 			intel_dp_init(dev_priv, DP_A, PORT_A);
15390 
15391 		if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
15392 			/* PCH SDVOB multiplex with HDMIB */
15393 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
15394 			if (!found)
15395 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
15396 			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
15397 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
15398 		}
15399 
15400 		if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
15401 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
15402 
15403 		if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
15404 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
15405 
15406 		if (I915_READ(PCH_DP_C) & DP_DETECTED)
15407 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
15408 
15409 		if (I915_READ(PCH_DP_D) & DP_DETECTED)
15410 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
15411 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
15412 		bool has_edp, has_port;
15413 
15414 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
15415 			intel_crt_init(dev_priv);
15416 
15417 		/*
15418 		 * The DP_DETECTED bit is the latched state of the DDC
15419 		 * SDA pin at boot. However since eDP doesn't require DDC
15420 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15421 		 * eDP ports may have been muxed to an alternate function.
15422 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
15423 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
15424 		 * detect eDP ports.
15425 		 *
15426 		 * Sadly the straps seem to be missing sometimes even for HDMI
15427 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15428 		 * and VBT for the presence of the port. Additionally we can't
15429 		 * trust the port type the VBT declares as we've seen at least
15430 		 * HDMI ports that the VBT claim are DP or eDP.
15431 		 */
15432 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
15433 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15434 		if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
15435 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
15436 		if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
15437 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
15438 
15439 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
15440 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15441 		if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
15442 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
15443 		if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
15444 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
15445 
15446 		if (IS_CHERRYVIEW(dev_priv)) {
15447 			/*
15448 			 * eDP not supported on port D,
15449 			 * so no need to worry about it
15450 			 */
15451 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15452 			if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
15453 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
15454 			if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
15455 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
15456 		}
15457 
15458 		vlv_dsi_init(dev_priv);
15459 	} else if (IS_PINEVIEW(dev_priv)) {
15460 		intel_lvds_init(dev_priv);
15461 		intel_crt_init(dev_priv);
15462 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
15463 		bool found = false;
15464 
15465 		if (IS_MOBILE(dev_priv))
15466 			intel_lvds_init(dev_priv);
15467 
15468 		intel_crt_init(dev_priv);
15469 
15470 		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
15471 			DRM_DEBUG_KMS("probing SDVOB\n");
15472 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
15473 			if (!found && IS_G4X(dev_priv)) {
15474 				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
15475 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
15476 			}
15477 
15478 			if (!found && IS_G4X(dev_priv))
15479 				intel_dp_init(dev_priv, DP_B, PORT_B);
15480 		}
15481 
15482 		/* Before G4X SDVOC doesn't have its own detect register */
15483 
15484 		if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
15485 			DRM_DEBUG_KMS("probing SDVOC\n");
15486 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
15487 		}
15488 
15489 		if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
15490 
15491 			if (IS_G4X(dev_priv)) {
15492 				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
15493 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
15494 			}
15495 			if (IS_G4X(dev_priv))
15496 				intel_dp_init(dev_priv, DP_C, PORT_C);
15497 		}
15498 
15499 		if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
15500 			intel_dp_init(dev_priv, DP_D, PORT_D);
15501 
15502 		if (SUPPORTS_TV(dev_priv))
15503 			intel_tv_init(dev_priv);
15504 	} else if (IS_GEN(dev_priv, 2)) {
15505 		if (IS_I85X(dev_priv))
15506 			intel_lvds_init(dev_priv);
15507 
15508 		intel_crt_init(dev_priv);
15509 		intel_dvo_init(dev_priv);
15510 	}
15511 
15512 	intel_psr_init(dev_priv);
15513 
15514 	for_each_intel_encoder(&dev_priv->drm, encoder) {
15515 		encoder->base.possible_crtcs = encoder->crtc_mask;
15516 		encoder->base.possible_clones =
15517 			intel_encoder_clones(encoder);
15518 	}
15519 
15520 	intel_init_pch_refclk(dev_priv);
15521 
15522 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
15523 }
15524 
15525 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15526 {
15527 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15528 
15529 	drm_framebuffer_cleanup(fb);
15530 	intel_frontbuffer_put(intel_fb->frontbuffer);
15531 
15532 	kfree(intel_fb);
15533 }
15534 
15535 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
15536 						struct drm_file *file,
15537 						unsigned int *handle)
15538 {
15539 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
15540 
15541 	if (obj->userptr.mm) {
15542 		DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15543 		return -EINVAL;
15544 	}
15545 
15546 	return drm_gem_handle_create(file, &obj->base, handle);
15547 }
15548 
15549 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15550 					struct drm_file *file,
15551 					unsigned flags, unsigned color,
15552 					struct drm_clip_rect *clips,
15553 					unsigned num_clips)
15554 {
15555 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
15556 
15557 	i915_gem_object_flush_if_display(obj);
15558 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
15559 
15560 	return 0;
15561 }
15562 
15563 static const struct drm_framebuffer_funcs intel_fb_funcs = {
15564 	.destroy = intel_user_framebuffer_destroy,
15565 	.create_handle = intel_user_framebuffer_create_handle,
15566 	.dirty = intel_user_framebuffer_dirty,
15567 };
15568 
15569 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
15570 				  struct drm_i915_gem_object *obj,
15571 				  struct drm_mode_fb_cmd2 *mode_cmd)
15572 {
15573 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
15574 	struct drm_framebuffer *fb = &intel_fb->base;
15575 	u32 max_stride;
15576 	unsigned int tiling, stride;
15577 	int ret = -EINVAL;
15578 	int i;
15579 
15580 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
15581 	if (!intel_fb->frontbuffer)
15582 		return -ENOMEM;
15583 
15584 	i915_gem_object_lock(obj);
15585 	tiling = i915_gem_object_get_tiling(obj);
15586 	stride = i915_gem_object_get_stride(obj);
15587 	i915_gem_object_unlock(obj);
15588 
15589 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
15590 		/*
15591 		 * If there's a fence, enforce that
15592 		 * the fb modifier and tiling mode match.
15593 		 */
15594 		if (tiling != I915_TILING_NONE &&
15595 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15596 			DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
15597 			goto err;
15598 		}
15599 	} else {
15600 		if (tiling == I915_TILING_X) {
15601 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
15602 		} else if (tiling == I915_TILING_Y) {
15603 			DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
15604 			goto err;
15605 		}
15606 	}
15607 
15608 	if (!drm_any_plane_has_format(&dev_priv->drm,
15609 				      mode_cmd->pixel_format,
15610 				      mode_cmd->modifier[0])) {
15611 		struct drm_format_name_buf format_name;
15612 
15613 		DRM_DEBUG_KMS("unsupported pixel format %s / modifier 0x%llx\n",
15614 			      drm_get_format_name(mode_cmd->pixel_format,
15615 						  &format_name),
15616 			      mode_cmd->modifier[0]);
15617 		goto err;
15618 	}
15619 
15620 	/*
15621 	 * gen2/3 display engine uses the fence if present,
15622 	 * so the tiling mode must match the fb modifier exactly.
15623 	 */
15624 	if (INTEL_GEN(dev_priv) < 4 &&
15625 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15626 		DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
15627 		goto err;
15628 	}
15629 
15630 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
15631 					 mode_cmd->modifier[0]);
15632 	if (mode_cmd->pitches[0] > max_stride) {
15633 		DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
15634 			      mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
15635 			      "tiled" : "linear",
15636 			      mode_cmd->pitches[0], max_stride);
15637 		goto err;
15638 	}
15639 
15640 	/*
15641 	 * If there's a fence, enforce that
15642 	 * the fb pitch and fence stride match.
15643 	 */
15644 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
15645 		DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
15646 			      mode_cmd->pitches[0], stride);
15647 		goto err;
15648 	}
15649 
15650 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15651 	if (mode_cmd->offsets[0] != 0)
15652 		goto err;
15653 
15654 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
15655 
15656 	for (i = 0; i < fb->format->num_planes; i++) {
15657 		u32 stride_alignment;
15658 
15659 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
15660 			DRM_DEBUG_KMS("bad plane %d handle\n", i);
15661 			goto err;
15662 		}
15663 
15664 		stride_alignment = intel_fb_stride_alignment(fb, i);
15665 
15666 		/*
15667 		 * Display WA #0531: skl,bxt,kbl,glk
15668 		 *
15669 		 * Render decompression and plane width > 3840
15670 		 * combined with horizontal panning requires the
15671 		 * plane stride to be a multiple of 4. We'll just
15672 		 * require the entire fb to accommodate that to avoid
15673 		 * potential runtime errors at plane configuration time.
15674 		 */
15675 		if (IS_GEN(dev_priv, 9) && i == 0 && fb->width > 3840 &&
15676 		    is_ccs_modifier(fb->modifier))
15677 			stride_alignment *= 4;
15678 
15679 		if (fb->pitches[i] & (stride_alignment - 1)) {
15680 			DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
15681 				      i, fb->pitches[i], stride_alignment);
15682 			goto err;
15683 		}
15684 
15685 		fb->obj[i] = &obj->base;
15686 	}
15687 
15688 	ret = intel_fill_fb_info(dev_priv, fb);
15689 	if (ret)
15690 		goto err;
15691 
15692 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
15693 	if (ret) {
15694 		DRM_ERROR("framebuffer init failed %d\n", ret);
15695 		goto err;
15696 	}
15697 
15698 	return 0;
15699 
15700 err:
15701 	intel_frontbuffer_put(intel_fb->frontbuffer);
15702 	return ret;
15703 }
15704 
15705 static struct drm_framebuffer *
15706 intel_user_framebuffer_create(struct drm_device *dev,
15707 			      struct drm_file *filp,
15708 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
15709 {
15710 	struct drm_framebuffer *fb;
15711 	struct drm_i915_gem_object *obj;
15712 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
15713 
15714 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15715 	if (!obj)
15716 		return ERR_PTR(-ENOENT);
15717 
15718 	fb = intel_framebuffer_create(obj, &mode_cmd);
15719 	i915_gem_object_put(obj);
15720 
15721 	return fb;
15722 }
15723 
15724 static void intel_atomic_state_free(struct drm_atomic_state *state)
15725 {
15726 	struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
15727 
15728 	drm_atomic_state_default_release(state);
15729 
15730 	i915_sw_fence_fini(&intel_state->commit_ready);
15731 
15732 	kfree(state);
15733 }
15734 
15735 static enum drm_mode_status
15736 intel_mode_valid(struct drm_device *dev,
15737 		 const struct drm_display_mode *mode)
15738 {
15739 	struct drm_i915_private *dev_priv = to_i915(dev);
15740 	int hdisplay_max, htotal_max;
15741 	int vdisplay_max, vtotal_max;
15742 
15743 	/*
15744 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
15745 	 * of DBLSCAN modes to the output's mode list when they detect
15746 	 * the scaling mode property on the connector. And they don't
15747 	 * ask the kernel to validate those modes in any way until
15748 	 * modeset time at which point the client gets a protocol error.
15749 	 * So in order to not upset those clients we silently ignore the
15750 	 * DBLSCAN flag on such connectors. For other connectors we will
15751 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
15752 	 * And we always reject DBLSCAN modes in connector->mode_valid()
15753 	 * as we never want such modes on the connector's mode list.
15754 	 */
15755 
15756 	if (mode->vscan > 1)
15757 		return MODE_NO_VSCAN;
15758 
15759 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
15760 		return MODE_H_ILLEGAL;
15761 
15762 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
15763 			   DRM_MODE_FLAG_NCSYNC |
15764 			   DRM_MODE_FLAG_PCSYNC))
15765 		return MODE_HSYNC;
15766 
15767 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
15768 			   DRM_MODE_FLAG_PIXMUX |
15769 			   DRM_MODE_FLAG_CLKDIV2))
15770 		return MODE_BAD;
15771 
15772 	if (INTEL_GEN(dev_priv) >= 9 ||
15773 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
15774 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
15775 		vdisplay_max = 4096;
15776 		htotal_max = 8192;
15777 		vtotal_max = 8192;
15778 	} else if (INTEL_GEN(dev_priv) >= 3) {
15779 		hdisplay_max = 4096;
15780 		vdisplay_max = 4096;
15781 		htotal_max = 8192;
15782 		vtotal_max = 8192;
15783 	} else {
15784 		hdisplay_max = 2048;
15785 		vdisplay_max = 2048;
15786 		htotal_max = 4096;
15787 		vtotal_max = 4096;
15788 	}
15789 
15790 	if (mode->hdisplay > hdisplay_max ||
15791 	    mode->hsync_start > htotal_max ||
15792 	    mode->hsync_end > htotal_max ||
15793 	    mode->htotal > htotal_max)
15794 		return MODE_H_ILLEGAL;
15795 
15796 	if (mode->vdisplay > vdisplay_max ||
15797 	    mode->vsync_start > vtotal_max ||
15798 	    mode->vsync_end > vtotal_max ||
15799 	    mode->vtotal > vtotal_max)
15800 		return MODE_V_ILLEGAL;
15801 
15802 	return MODE_OK;
15803 }
15804 
15805 static const struct drm_mode_config_funcs intel_mode_funcs = {
15806 	.fb_create = intel_user_framebuffer_create,
15807 	.get_format_info = intel_get_format_info,
15808 	.output_poll_changed = intel_fbdev_output_poll_changed,
15809 	.mode_valid = intel_mode_valid,
15810 	.atomic_check = intel_atomic_check,
15811 	.atomic_commit = intel_atomic_commit,
15812 	.atomic_state_alloc = intel_atomic_state_alloc,
15813 	.atomic_state_clear = intel_atomic_state_clear,
15814 	.atomic_state_free = intel_atomic_state_free,
15815 };
15816 
15817 /**
15818  * intel_init_display_hooks - initialize the display modesetting hooks
15819  * @dev_priv: device private
15820  */
15821 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
15822 {
15823 	intel_init_cdclk_hooks(dev_priv);
15824 
15825 	if (INTEL_GEN(dev_priv) >= 9) {
15826 		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
15827 		dev_priv->display.get_initial_plane_config =
15828 			skylake_get_initial_plane_config;
15829 		dev_priv->display.crtc_compute_clock =
15830 			haswell_crtc_compute_clock;
15831 		dev_priv->display.crtc_enable = haswell_crtc_enable;
15832 		dev_priv->display.crtc_disable = haswell_crtc_disable;
15833 	} else if (HAS_DDI(dev_priv)) {
15834 		dev_priv->display.get_pipe_config = haswell_get_pipe_config;
15835 		dev_priv->display.get_initial_plane_config =
15836 			i9xx_get_initial_plane_config;
15837 		dev_priv->display.crtc_compute_clock =
15838 			haswell_crtc_compute_clock;
15839 		dev_priv->display.crtc_enable = haswell_crtc_enable;
15840 		dev_priv->display.crtc_disable = haswell_crtc_disable;
15841 	} else if (HAS_PCH_SPLIT(dev_priv)) {
15842 		dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
15843 		dev_priv->display.get_initial_plane_config =
15844 			i9xx_get_initial_plane_config;
15845 		dev_priv->display.crtc_compute_clock =
15846 			ironlake_crtc_compute_clock;
15847 		dev_priv->display.crtc_enable = ironlake_crtc_enable;
15848 		dev_priv->display.crtc_disable = ironlake_crtc_disable;
15849 	} else if (IS_CHERRYVIEW(dev_priv)) {
15850 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15851 		dev_priv->display.get_initial_plane_config =
15852 			i9xx_get_initial_plane_config;
15853 		dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
15854 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
15855 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15856 	} else if (IS_VALLEYVIEW(dev_priv)) {
15857 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15858 		dev_priv->display.get_initial_plane_config =
15859 			i9xx_get_initial_plane_config;
15860 		dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
15861 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
15862 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15863 	} else if (IS_G4X(dev_priv)) {
15864 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15865 		dev_priv->display.get_initial_plane_config =
15866 			i9xx_get_initial_plane_config;
15867 		dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
15868 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
15869 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15870 	} else if (IS_PINEVIEW(dev_priv)) {
15871 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15872 		dev_priv->display.get_initial_plane_config =
15873 			i9xx_get_initial_plane_config;
15874 		dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
15875 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
15876 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15877 	} else if (!IS_GEN(dev_priv, 2)) {
15878 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15879 		dev_priv->display.get_initial_plane_config =
15880 			i9xx_get_initial_plane_config;
15881 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
15882 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
15883 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15884 	} else {
15885 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
15886 		dev_priv->display.get_initial_plane_config =
15887 			i9xx_get_initial_plane_config;
15888 		dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
15889 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
15890 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
15891 	}
15892 
15893 	if (IS_GEN(dev_priv, 5)) {
15894 		dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
15895 	} else if (IS_GEN(dev_priv, 6)) {
15896 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
15897 	} else if (IS_IVYBRIDGE(dev_priv)) {
15898 		/* FIXME: detect B0+ stepping and use auto training */
15899 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
15900 	} else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
15901 		dev_priv->display.fdi_link_train = hsw_fdi_link_train;
15902 	}
15903 
15904 	if (INTEL_GEN(dev_priv) >= 9)
15905 		dev_priv->display.update_crtcs = skl_update_crtcs;
15906 	else
15907 		dev_priv->display.update_crtcs = intel_update_crtcs;
15908 }
15909 
15910 static i915_reg_t i915_vgacntrl_reg(struct drm_i915_private *dev_priv)
15911 {
15912 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15913 		return VLV_VGACNTRL;
15914 	else if (INTEL_GEN(dev_priv) >= 5)
15915 		return CPU_VGACNTRL;
15916 	else
15917 		return VGACNTRL;
15918 }
15919 
15920 /* Disable the VGA plane that we never use */
15921 static void i915_disable_vga(struct drm_i915_private *dev_priv)
15922 {
15923 	struct pci_dev *pdev = dev_priv->drm.pdev;
15924 	u8 sr1;
15925 	i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
15926 
15927 	/* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
15928 	vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
15929 	outb(SR01, VGA_SR_INDEX);
15930 	sr1 = inb(VGA_SR_DATA);
15931 	outb(sr1 | 1<<5, VGA_SR_DATA);
15932 	vga_put(pdev, VGA_RSRC_LEGACY_IO);
15933 	udelay(300);
15934 
15935 	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
15936 	POSTING_READ(vga_reg);
15937 }
15938 
15939 void intel_modeset_init_hw(struct drm_device *dev)
15940 {
15941 	struct drm_i915_private *dev_priv = to_i915(dev);
15942 
15943 	intel_update_cdclk(dev_priv);
15944 	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
15945 	dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
15946 }
15947 
15948 /*
15949  * Calculate what we think the watermarks should be for the state we've read
15950  * out of the hardware and then immediately program those watermarks so that
15951  * we ensure the hardware settings match our internal state.
15952  *
15953  * We can calculate what we think WM's should be by creating a duplicate of the
15954  * current state (which was constructed during hardware readout) and running it
15955  * through the atomic check code to calculate new watermark values in the
15956  * state object.
15957  */
15958 static void sanitize_watermarks(struct drm_device *dev)
15959 {
15960 	struct drm_i915_private *dev_priv = to_i915(dev);
15961 	struct drm_atomic_state *state;
15962 	struct intel_atomic_state *intel_state;
15963 	struct intel_crtc *crtc;
15964 	struct intel_crtc_state *crtc_state;
15965 	struct drm_modeset_acquire_ctx ctx;
15966 	int ret;
15967 	int i;
15968 
15969 	/* Only supported on platforms that use atomic watermark design */
15970 	if (!dev_priv->display.optimize_watermarks)
15971 		return;
15972 
15973 	/*
15974 	 * We need to hold connection_mutex before calling duplicate_state so
15975 	 * that the connector loop is protected.
15976 	 */
15977 	drm_modeset_acquire_init(&ctx, 0);
15978 retry:
15979 	ret = drm_modeset_lock_all_ctx(dev, &ctx);
15980 	if (ret == -EDEADLK) {
15981 		drm_modeset_backoff(&ctx);
15982 		goto retry;
15983 	} else if (WARN_ON(ret)) {
15984 		goto fail;
15985 	}
15986 
15987 	state = drm_atomic_helper_duplicate_state(dev, &ctx);
15988 	if (WARN_ON(IS_ERR(state)))
15989 		goto fail;
15990 
15991 	intel_state = to_intel_atomic_state(state);
15992 
15993 	/*
15994 	 * Hardware readout is the only time we don't want to calculate
15995 	 * intermediate watermarks (since we don't trust the current
15996 	 * watermarks).
15997 	 */
15998 	if (!HAS_GMCH(dev_priv))
15999 		intel_state->skip_intermediate_wm = true;
16000 
16001 	ret = intel_atomic_check(dev, state);
16002 	if (ret) {
16003 		/*
16004 		 * If we fail here, it means that the hardware appears to be
16005 		 * programmed in a way that shouldn't be possible, given our
16006 		 * understanding of watermark requirements.  This might mean a
16007 		 * mistake in the hardware readout code or a mistake in the
16008 		 * watermark calculations for a given platform.  Raise a WARN
16009 		 * so that this is noticeable.
16010 		 *
16011 		 * If this actually happens, we'll have to just leave the
16012 		 * BIOS-programmed watermarks untouched and hope for the best.
16013 		 */
16014 		WARN(true, "Could not determine valid watermarks for inherited state\n");
16015 		goto put_state;
16016 	}
16017 
16018 	/* Write calculated watermark values back */
16019 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
16020 		crtc_state->wm.need_postvbl_update = true;
16021 		dev_priv->display.optimize_watermarks(intel_state, crtc_state);
16022 
16023 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
16024 	}
16025 
16026 put_state:
16027 	drm_atomic_state_put(state);
16028 fail:
16029 	drm_modeset_drop_locks(&ctx);
16030 	drm_modeset_acquire_fini(&ctx);
16031 }
16032 
16033 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
16034 {
16035 	if (IS_GEN(dev_priv, 5)) {
16036 		u32 fdi_pll_clk =
16037 			I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
16038 
16039 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
16040 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
16041 		dev_priv->fdi_pll_freq = 270000;
16042 	} else {
16043 		return;
16044 	}
16045 
16046 	DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
16047 }
16048 
16049 static int intel_initial_commit(struct drm_device *dev)
16050 {
16051 	struct drm_atomic_state *state = NULL;
16052 	struct drm_modeset_acquire_ctx ctx;
16053 	struct drm_crtc *crtc;
16054 	struct drm_crtc_state *crtc_state;
16055 	int ret = 0;
16056 
16057 	state = drm_atomic_state_alloc(dev);
16058 	if (!state)
16059 		return -ENOMEM;
16060 
16061 	drm_modeset_acquire_init(&ctx, 0);
16062 
16063 retry:
16064 	state->acquire_ctx = &ctx;
16065 
16066 	drm_for_each_crtc(crtc, dev) {
16067 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
16068 		if (IS_ERR(crtc_state)) {
16069 			ret = PTR_ERR(crtc_state);
16070 			goto out;
16071 		}
16072 
16073 		if (crtc_state->active) {
16074 			ret = drm_atomic_add_affected_planes(state, crtc);
16075 			if (ret)
16076 				goto out;
16077 
16078 			/*
16079 			 * FIXME hack to force a LUT update to avoid the
16080 			 * plane update forcing the pipe gamma on without
16081 			 * having a proper LUT loaded. Remove once we
16082 			 * have readout for pipe gamma enable.
16083 			 */
16084 			crtc_state->color_mgmt_changed = true;
16085 		}
16086 	}
16087 
16088 	ret = drm_atomic_commit(state);
16089 
16090 out:
16091 	if (ret == -EDEADLK) {
16092 		drm_atomic_state_clear(state);
16093 		drm_modeset_backoff(&ctx);
16094 		goto retry;
16095 	}
16096 
16097 	drm_atomic_state_put(state);
16098 
16099 	drm_modeset_drop_locks(&ctx);
16100 	drm_modeset_acquire_fini(&ctx);
16101 
16102 	return ret;
16103 }
16104 
16105 int intel_modeset_init(struct drm_device *dev)
16106 {
16107 	struct drm_i915_private *dev_priv = to_i915(dev);
16108 	enum pipe pipe;
16109 	struct intel_crtc *crtc;
16110 	int ret;
16111 
16112 	dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
16113 
16114 	drm_mode_config_init(dev);
16115 
16116 	ret = intel_bw_init(dev_priv);
16117 	if (ret)
16118 		return ret;
16119 
16120 	dev->mode_config.min_width = 0;
16121 	dev->mode_config.min_height = 0;
16122 
16123 	dev->mode_config.preferred_depth = 24;
16124 	dev->mode_config.prefer_shadow = 1;
16125 
16126 	dev->mode_config.allow_fb_modifiers = true;
16127 
16128 	dev->mode_config.funcs = &intel_mode_funcs;
16129 
16130 	init_llist_head(&dev_priv->atomic_helper.free_list);
16131 	INIT_WORK(&dev_priv->atomic_helper.free_work,
16132 		  intel_atomic_helper_free_state_worker);
16133 
16134 	intel_init_quirks(dev_priv);
16135 
16136 	intel_fbc_init(dev_priv);
16137 
16138 	intel_init_pm(dev_priv);
16139 
16140 	/*
16141 	 * There may be no VBT; and if the BIOS enabled SSC we can
16142 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
16143 	 * BIOS isn't using it, don't assume it will work even if the VBT
16144 	 * indicates as much.
16145 	 */
16146 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
16147 		bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16148 					    DREF_SSC1_ENABLE);
16149 
16150 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16151 			DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16152 				     bios_lvds_use_ssc ? "en" : "dis",
16153 				     dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16154 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16155 		}
16156 	}
16157 
16158 	/*
16159 	 * Maximum framebuffer dimensions, chosen to match
16160 	 * the maximum render engine surface size on gen4+.
16161 	 */
16162 	if (INTEL_GEN(dev_priv) >= 7) {
16163 		dev->mode_config.max_width = 16384;
16164 		dev->mode_config.max_height = 16384;
16165 	} else if (INTEL_GEN(dev_priv) >= 4) {
16166 		dev->mode_config.max_width = 8192;
16167 		dev->mode_config.max_height = 8192;
16168 	} else if (IS_GEN(dev_priv, 3)) {
16169 		dev->mode_config.max_width = 4096;
16170 		dev->mode_config.max_height = 4096;
16171 	} else {
16172 		dev->mode_config.max_width = 2048;
16173 		dev->mode_config.max_height = 2048;
16174 	}
16175 
16176 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16177 		dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
16178 		dev->mode_config.cursor_height = 1023;
16179 	} else if (IS_GEN(dev_priv, 2)) {
16180 		dev->mode_config.cursor_width = 64;
16181 		dev->mode_config.cursor_height = 64;
16182 	} else {
16183 		dev->mode_config.cursor_width = 256;
16184 		dev->mode_config.cursor_height = 256;
16185 	}
16186 
16187 	DRM_DEBUG_KMS("%d display pipe%s available.\n",
16188 		      INTEL_INFO(dev_priv)->num_pipes,
16189 		      INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
16190 
16191 	for_each_pipe(dev_priv, pipe) {
16192 		ret = intel_crtc_init(dev_priv, pipe);
16193 		if (ret) {
16194 			drm_mode_config_cleanup(dev);
16195 			return ret;
16196 		}
16197 	}
16198 
16199 	intel_shared_dpll_init(dev);
16200 	intel_update_fdi_pll_freq(dev_priv);
16201 
16202 	intel_update_czclk(dev_priv);
16203 	intel_modeset_init_hw(dev);
16204 
16205 	intel_hdcp_component_init(dev_priv);
16206 
16207 	if (dev_priv->max_cdclk_freq == 0)
16208 		intel_update_max_cdclk(dev_priv);
16209 
16210 	/* Just disable it once at startup */
16211 	i915_disable_vga(dev_priv);
16212 	intel_setup_outputs(dev_priv);
16213 
16214 	drm_modeset_lock_all(dev);
16215 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
16216 	drm_modeset_unlock_all(dev);
16217 
16218 	for_each_intel_crtc(dev, crtc) {
16219 		struct intel_initial_plane_config plane_config = {};
16220 
16221 		if (!crtc->active)
16222 			continue;
16223 
16224 		/*
16225 		 * Note that reserving the BIOS fb up front prevents us
16226 		 * from stuffing other stolen allocations like the ring
16227 		 * on top.  This prevents some ugliness at boot time, and
16228 		 * can even allow for smooth boot transitions if the BIOS
16229 		 * fb is large enough for the active pipe configuration.
16230 		 */
16231 		dev_priv->display.get_initial_plane_config(crtc,
16232 							   &plane_config);
16233 
16234 		/*
16235 		 * If the fb is shared between multiple heads, we'll
16236 		 * just get the first one.
16237 		 */
16238 		intel_find_initial_plane_obj(crtc, &plane_config);
16239 	}
16240 
16241 	/*
16242 	 * Make sure hardware watermarks really match the state we read out.
16243 	 * Note that we need to do this after reconstructing the BIOS fb's
16244 	 * since the watermark calculation done here will use pstate->fb.
16245 	 */
16246 	if (!HAS_GMCH(dev_priv))
16247 		sanitize_watermarks(dev);
16248 
16249 	/*
16250 	 * Force all active planes to recompute their states. So that on
16251 	 * mode_setcrtc after probe, all the intel_plane_state variables
16252 	 * are already calculated and there is no assert_plane warnings
16253 	 * during bootup.
16254 	 */
16255 	ret = intel_initial_commit(dev);
16256 	if (ret)
16257 		DRM_DEBUG_KMS("Initial commit in probe failed.\n");
16258 
16259 	return 0;
16260 }
16261 
16262 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
16263 {
16264 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16265 	/* 640x480@60Hz, ~25175 kHz */
16266 	struct dpll clock = {
16267 		.m1 = 18,
16268 		.m2 = 7,
16269 		.p1 = 13,
16270 		.p2 = 4,
16271 		.n = 2,
16272 	};
16273 	u32 dpll, fp;
16274 	int i;
16275 
16276 	WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
16277 
16278 	DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
16279 		      pipe_name(pipe), clock.vco, clock.dot);
16280 
16281 	fp = i9xx_dpll_compute_fp(&clock);
16282 	dpll = DPLL_DVO_2X_MODE |
16283 		DPLL_VGA_MODE_DIS |
16284 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
16285 		PLL_P2_DIVIDE_BY_4 |
16286 		PLL_REF_INPUT_DREFCLK |
16287 		DPLL_VCO_ENABLE;
16288 
16289 	I915_WRITE(FP0(pipe), fp);
16290 	I915_WRITE(FP1(pipe), fp);
16291 
16292 	I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
16293 	I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
16294 	I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
16295 	I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
16296 	I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
16297 	I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
16298 	I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
16299 
16300 	/*
16301 	 * Apparently we need to have VGA mode enabled prior to changing
16302 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
16303 	 * dividers, even though the register value does change.
16304 	 */
16305 	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
16306 	I915_WRITE(DPLL(pipe), dpll);
16307 
16308 	/* Wait for the clocks to stabilize. */
16309 	POSTING_READ(DPLL(pipe));
16310 	udelay(150);
16311 
16312 	/* The pixel multiplier can only be updated once the
16313 	 * DPLL is enabled and the clocks are stable.
16314 	 *
16315 	 * So write it again.
16316 	 */
16317 	I915_WRITE(DPLL(pipe), dpll);
16318 
16319 	/* We do this three times for luck */
16320 	for (i = 0; i < 3 ; i++) {
16321 		I915_WRITE(DPLL(pipe), dpll);
16322 		POSTING_READ(DPLL(pipe));
16323 		udelay(150); /* wait for warmup */
16324 	}
16325 
16326 	I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
16327 	POSTING_READ(PIPECONF(pipe));
16328 
16329 	intel_wait_for_pipe_scanline_moving(crtc);
16330 }
16331 
16332 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
16333 {
16334 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16335 
16336 	DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
16337 		      pipe_name(pipe));
16338 
16339 	WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
16340 	WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
16341 	WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
16342 	WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
16343 	WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
16344 
16345 	I915_WRITE(PIPECONF(pipe), 0);
16346 	POSTING_READ(PIPECONF(pipe));
16347 
16348 	intel_wait_for_pipe_scanline_stopped(crtc);
16349 
16350 	I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
16351 	POSTING_READ(DPLL(pipe));
16352 }
16353 
16354 static void
16355 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
16356 {
16357 	struct intel_crtc *crtc;
16358 
16359 	if (INTEL_GEN(dev_priv) >= 4)
16360 		return;
16361 
16362 	for_each_intel_crtc(&dev_priv->drm, crtc) {
16363 		struct intel_plane *plane =
16364 			to_intel_plane(crtc->base.primary);
16365 		struct intel_crtc *plane_crtc;
16366 		enum pipe pipe;
16367 
16368 		if (!plane->get_hw_state(plane, &pipe))
16369 			continue;
16370 
16371 		if (pipe == crtc->pipe)
16372 			continue;
16373 
16374 		DRM_DEBUG_KMS("[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
16375 			      plane->base.base.id, plane->base.name);
16376 
16377 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16378 		intel_plane_disable_noatomic(plane_crtc, plane);
16379 	}
16380 }
16381 
16382 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16383 {
16384 	struct drm_device *dev = crtc->base.dev;
16385 	struct intel_encoder *encoder;
16386 
16387 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16388 		return true;
16389 
16390 	return false;
16391 }
16392 
16393 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16394 {
16395 	struct drm_device *dev = encoder->base.dev;
16396 	struct intel_connector *connector;
16397 
16398 	for_each_connector_on_encoder(dev, &encoder->base, connector)
16399 		return connector;
16400 
16401 	return NULL;
16402 }
16403 
16404 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16405 			      enum pipe pch_transcoder)
16406 {
16407 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16408 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
16409 }
16410 
16411 static void intel_sanitize_crtc(struct intel_crtc *crtc,
16412 				struct drm_modeset_acquire_ctx *ctx)
16413 {
16414 	struct drm_device *dev = crtc->base.dev;
16415 	struct drm_i915_private *dev_priv = to_i915(dev);
16416 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
16417 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
16418 
16419 	/* Clear any frame start delays used for debugging left by the BIOS */
16420 	if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
16421 		i915_reg_t reg = PIPECONF(cpu_transcoder);
16422 
16423 		I915_WRITE(reg,
16424 			   I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16425 	}
16426 
16427 	if (crtc_state->base.active) {
16428 		struct intel_plane *plane;
16429 
16430 		/* Disable everything but the primary plane */
16431 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
16432 			const struct intel_plane_state *plane_state =
16433 				to_intel_plane_state(plane->base.state);
16434 
16435 			if (plane_state->base.visible &&
16436 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
16437 				intel_plane_disable_noatomic(crtc, plane);
16438 		}
16439 
16440 		/*
16441 		 * Disable any background color set by the BIOS, but enable the
16442 		 * gamma and CSC to match how we program our planes.
16443 		 */
16444 		if (INTEL_GEN(dev_priv) >= 9)
16445 			I915_WRITE(SKL_BOTTOM_COLOR(crtc->pipe),
16446 				   SKL_BOTTOM_COLOR_GAMMA_ENABLE |
16447 				   SKL_BOTTOM_COLOR_CSC_ENABLE);
16448 	}
16449 
16450 	/* Adjust the state of the output pipe according to whether we
16451 	 * have active connectors/encoders. */
16452 	if (crtc_state->base.active && !intel_crtc_has_encoders(crtc))
16453 		intel_crtc_disable_noatomic(&crtc->base, ctx);
16454 
16455 	if (crtc_state->base.active || HAS_GMCH(dev_priv)) {
16456 		/*
16457 		 * We start out with underrun reporting disabled to avoid races.
16458 		 * For correct bookkeeping mark this on active crtcs.
16459 		 *
16460 		 * Also on gmch platforms we dont have any hardware bits to
16461 		 * disable the underrun reporting. Which means we need to start
16462 		 * out with underrun reporting disabled also on inactive pipes,
16463 		 * since otherwise we'll complain about the garbage we read when
16464 		 * e.g. coming up after runtime pm.
16465 		 *
16466 		 * No protection against concurrent access is required - at
16467 		 * worst a fifo underrun happens which also sets this to false.
16468 		 */
16469 		crtc->cpu_fifo_underrun_disabled = true;
16470 		/*
16471 		 * We track the PCH trancoder underrun reporting state
16472 		 * within the crtc. With crtc for pipe A housing the underrun
16473 		 * reporting state for PCH transcoder A, crtc for pipe B housing
16474 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16475 		 * and marking underrun reporting as disabled for the non-existing
16476 		 * PCH transcoders B and C would prevent enabling the south
16477 		 * error interrupt (see cpt_can_enable_serr_int()).
16478 		 */
16479 		if (has_pch_trancoder(dev_priv, crtc->pipe))
16480 			crtc->pch_fifo_underrun_disabled = true;
16481 	}
16482 }
16483 
16484 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
16485 {
16486 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
16487 
16488 	/*
16489 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
16490 	 * the hardware when a high res displays plugged in. DPLL P
16491 	 * divider is zero, and the pipe timings are bonkers. We'll
16492 	 * try to disable everything in that case.
16493 	 *
16494 	 * FIXME would be nice to be able to sanitize this state
16495 	 * without several WARNs, but for now let's take the easy
16496 	 * road.
16497 	 */
16498 	return IS_GEN(dev_priv, 6) &&
16499 		crtc_state->base.active &&
16500 		crtc_state->shared_dpll &&
16501 		crtc_state->port_clock == 0;
16502 }
16503 
16504 static void intel_sanitize_encoder(struct intel_encoder *encoder)
16505 {
16506 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
16507 	struct intel_connector *connector;
16508 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
16509 	struct intel_crtc_state *crtc_state = crtc ?
16510 		to_intel_crtc_state(crtc->base.state) : NULL;
16511 
16512 	/* We need to check both for a crtc link (meaning that the
16513 	 * encoder is active and trying to read from a pipe) and the
16514 	 * pipe itself being active. */
16515 	bool has_active_crtc = crtc_state &&
16516 		crtc_state->base.active;
16517 
16518 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
16519 		DRM_DEBUG_KMS("BIOS has misprogrammed the hardware. Disabling pipe %c\n",
16520 			      pipe_name(crtc->pipe));
16521 		has_active_crtc = false;
16522 	}
16523 
16524 	connector = intel_encoder_find_connector(encoder);
16525 	if (connector && !has_active_crtc) {
16526 		DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16527 			      encoder->base.base.id,
16528 			      encoder->base.name);
16529 
16530 		/* Connector is active, but has no active pipe. This is
16531 		 * fallout from our resume register restoring. Disable
16532 		 * the encoder manually again. */
16533 		if (crtc_state) {
16534 			struct drm_encoder *best_encoder;
16535 
16536 			DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16537 				      encoder->base.base.id,
16538 				      encoder->base.name);
16539 
16540 			/* avoid oopsing in case the hooks consult best_encoder */
16541 			best_encoder = connector->base.state->best_encoder;
16542 			connector->base.state->best_encoder = &encoder->base;
16543 
16544 			if (encoder->disable)
16545 				encoder->disable(encoder, crtc_state,
16546 						 connector->base.state);
16547 			if (encoder->post_disable)
16548 				encoder->post_disable(encoder, crtc_state,
16549 						      connector->base.state);
16550 
16551 			connector->base.state->best_encoder = best_encoder;
16552 		}
16553 		encoder->base.crtc = NULL;
16554 
16555 		/* Inconsistent output/port/pipe state happens presumably due to
16556 		 * a bug in one of the get_hw_state functions. Or someplace else
16557 		 * in our code, like the register restore mess on resume. Clamp
16558 		 * things to off as a safer default. */
16559 
16560 		connector->base.dpms = DRM_MODE_DPMS_OFF;
16561 		connector->base.encoder = NULL;
16562 	}
16563 
16564 	/* notify opregion of the sanitized encoder state */
16565 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
16566 
16567 	if (INTEL_GEN(dev_priv) >= 11)
16568 		icl_sanitize_encoder_pll_mapping(encoder);
16569 }
16570 
16571 void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
16572 {
16573 	i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
16574 
16575 	if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16576 		DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
16577 		i915_disable_vga(dev_priv);
16578 	}
16579 }
16580 
16581 void i915_redisable_vga(struct drm_i915_private *dev_priv)
16582 {
16583 	intel_wakeref_t wakeref;
16584 
16585 	/*
16586 	 * This function can be called both from intel_modeset_setup_hw_state or
16587 	 * at a very early point in our resume sequence, where the power well
16588 	 * structures are not yet restored. Since this function is at a very
16589 	 * paranoid "someone might have enabled VGA while we were not looking"
16590 	 * level, just check if the power well is enabled instead of trying to
16591 	 * follow the "don't touch the power well if we don't need it" policy
16592 	 * the rest of the driver uses.
16593 	 */
16594 	wakeref = intel_display_power_get_if_enabled(dev_priv,
16595 						     POWER_DOMAIN_VGA);
16596 	if (!wakeref)
16597 		return;
16598 
16599 	i915_redisable_vga_power_on(dev_priv);
16600 
16601 	intel_display_power_put(dev_priv, POWER_DOMAIN_VGA, wakeref);
16602 }
16603 
16604 /* FIXME read out full plane state for all planes */
16605 static void readout_plane_state(struct drm_i915_private *dev_priv)
16606 {
16607 	struct intel_plane *plane;
16608 	struct intel_crtc *crtc;
16609 
16610 	for_each_intel_plane(&dev_priv->drm, plane) {
16611 		struct intel_plane_state *plane_state =
16612 			to_intel_plane_state(plane->base.state);
16613 		struct intel_crtc_state *crtc_state;
16614 		enum pipe pipe = PIPE_A;
16615 		bool visible;
16616 
16617 		visible = plane->get_hw_state(plane, &pipe);
16618 
16619 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16620 		crtc_state = to_intel_crtc_state(crtc->base.state);
16621 
16622 		intel_set_plane_visible(crtc_state, plane_state, visible);
16623 
16624 		DRM_DEBUG_KMS("[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
16625 			      plane->base.base.id, plane->base.name,
16626 			      enableddisabled(visible), pipe_name(pipe));
16627 	}
16628 
16629 	for_each_intel_crtc(&dev_priv->drm, crtc) {
16630 		struct intel_crtc_state *crtc_state =
16631 			to_intel_crtc_state(crtc->base.state);
16632 
16633 		fixup_active_planes(crtc_state);
16634 	}
16635 }
16636 
16637 static void intel_modeset_readout_hw_state(struct drm_device *dev)
16638 {
16639 	struct drm_i915_private *dev_priv = to_i915(dev);
16640 	enum pipe pipe;
16641 	struct intel_crtc *crtc;
16642 	struct intel_encoder *encoder;
16643 	struct intel_connector *connector;
16644 	struct drm_connector_list_iter conn_iter;
16645 	int i;
16646 
16647 	dev_priv->active_crtcs = 0;
16648 
16649 	for_each_intel_crtc(dev, crtc) {
16650 		struct intel_crtc_state *crtc_state =
16651 			to_intel_crtc_state(crtc->base.state);
16652 
16653 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
16654 		memset(crtc_state, 0, sizeof(*crtc_state));
16655 		__drm_atomic_helper_crtc_reset(&crtc->base, &crtc_state->base);
16656 
16657 		crtc_state->base.active = crtc_state->base.enable =
16658 			dev_priv->display.get_pipe_config(crtc, crtc_state);
16659 
16660 		crtc->base.enabled = crtc_state->base.enable;
16661 		crtc->active = crtc_state->base.active;
16662 
16663 		if (crtc_state->base.active)
16664 			dev_priv->active_crtcs |= 1 << crtc->pipe;
16665 
16666 		DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16667 			      crtc->base.base.id, crtc->base.name,
16668 			      enableddisabled(crtc_state->base.active));
16669 	}
16670 
16671 	readout_plane_state(dev_priv);
16672 
16673 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16674 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16675 
16676 		pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
16677 							&pll->state.hw_state);
16678 
16679 		if (IS_ELKHARTLAKE(dev_priv) && pll->on &&
16680 		    pll->info->id == DPLL_ID_EHL_DPLL4) {
16681 			pll->wakeref = intel_display_power_get(dev_priv,
16682 							       POWER_DOMAIN_DPLL_DC_OFF);
16683 		}
16684 
16685 		pll->state.crtc_mask = 0;
16686 		for_each_intel_crtc(dev, crtc) {
16687 			struct intel_crtc_state *crtc_state =
16688 				to_intel_crtc_state(crtc->base.state);
16689 
16690 			if (crtc_state->base.active &&
16691 			    crtc_state->shared_dpll == pll)
16692 				pll->state.crtc_mask |= 1 << crtc->pipe;
16693 		}
16694 		pll->active_mask = pll->state.crtc_mask;
16695 
16696 		DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
16697 			      pll->info->name, pll->state.crtc_mask, pll->on);
16698 	}
16699 
16700 	for_each_intel_encoder(dev, encoder) {
16701 		pipe = 0;
16702 
16703 		if (encoder->get_hw_state(encoder, &pipe)) {
16704 			struct intel_crtc_state *crtc_state;
16705 
16706 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16707 			crtc_state = to_intel_crtc_state(crtc->base.state);
16708 
16709 			encoder->base.crtc = &crtc->base;
16710 			encoder->get_config(encoder, crtc_state);
16711 		} else {
16712 			encoder->base.crtc = NULL;
16713 		}
16714 
16715 		DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
16716 			      encoder->base.base.id, encoder->base.name,
16717 			      enableddisabled(encoder->base.crtc),
16718 			      pipe_name(pipe));
16719 	}
16720 
16721 	drm_connector_list_iter_begin(dev, &conn_iter);
16722 	for_each_intel_connector_iter(connector, &conn_iter) {
16723 		if (connector->get_hw_state(connector)) {
16724 			connector->base.dpms = DRM_MODE_DPMS_ON;
16725 
16726 			encoder = connector->encoder;
16727 			connector->base.encoder = &encoder->base;
16728 
16729 			if (encoder->base.crtc &&
16730 			    encoder->base.crtc->state->active) {
16731 				/*
16732 				 * This has to be done during hardware readout
16733 				 * because anything calling .crtc_disable may
16734 				 * rely on the connector_mask being accurate.
16735 				 */
16736 				encoder->base.crtc->state->connector_mask |=
16737 					drm_connector_mask(&connector->base);
16738 				encoder->base.crtc->state->encoder_mask |=
16739 					drm_encoder_mask(&encoder->base);
16740 			}
16741 
16742 		} else {
16743 			connector->base.dpms = DRM_MODE_DPMS_OFF;
16744 			connector->base.encoder = NULL;
16745 		}
16746 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
16747 			      connector->base.base.id, connector->base.name,
16748 			      enableddisabled(connector->base.encoder));
16749 	}
16750 	drm_connector_list_iter_end(&conn_iter);
16751 
16752 	for_each_intel_crtc(dev, crtc) {
16753 		struct intel_bw_state *bw_state =
16754 			to_intel_bw_state(dev_priv->bw_obj.state);
16755 		struct intel_crtc_state *crtc_state =
16756 			to_intel_crtc_state(crtc->base.state);
16757 		struct intel_plane *plane;
16758 		int min_cdclk = 0;
16759 
16760 		memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16761 		if (crtc_state->base.active) {
16762 			intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
16763 			crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
16764 			crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
16765 			intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
16766 			WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
16767 
16768 			/*
16769 			 * The initial mode needs to be set in order to keep
16770 			 * the atomic core happy. It wants a valid mode if the
16771 			 * crtc's enabled, so we do the above call.
16772 			 *
16773 			 * But we don't set all the derived state fully, hence
16774 			 * set a flag to indicate that a full recalculation is
16775 			 * needed on the next commit.
16776 			 */
16777 			crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
16778 
16779 			intel_crtc_compute_pixel_rate(crtc_state);
16780 
16781 			if (dev_priv->display.modeset_calc_cdclk) {
16782 				min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
16783 				if (WARN_ON(min_cdclk < 0))
16784 					min_cdclk = 0;
16785 			}
16786 
16787 			drm_calc_timestamping_constants(&crtc->base,
16788 							&crtc_state->base.adjusted_mode);
16789 			update_scanline_offset(crtc_state);
16790 		}
16791 
16792 		dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
16793 		dev_priv->min_voltage_level[crtc->pipe] =
16794 			crtc_state->min_voltage_level;
16795 
16796 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
16797 			const struct intel_plane_state *plane_state =
16798 				to_intel_plane_state(plane->base.state);
16799 
16800 			/*
16801 			 * FIXME don't have the fb yet, so can't
16802 			 * use intel_plane_data_rate() :(
16803 			 */
16804 			if (plane_state->base.visible)
16805 				crtc_state->data_rate[plane->id] =
16806 					4 * crtc_state->pixel_rate;
16807 		}
16808 
16809 		intel_bw_crtc_update(bw_state, crtc_state);
16810 
16811 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
16812 	}
16813 }
16814 
16815 static void
16816 get_encoder_power_domains(struct drm_i915_private *dev_priv)
16817 {
16818 	struct intel_encoder *encoder;
16819 
16820 	for_each_intel_encoder(&dev_priv->drm, encoder) {
16821 		struct intel_crtc_state *crtc_state;
16822 
16823 		if (!encoder->get_power_domains)
16824 			continue;
16825 
16826 		/*
16827 		 * MST-primary and inactive encoders don't have a crtc state
16828 		 * and neither of these require any power domain references.
16829 		 */
16830 		if (!encoder->base.crtc)
16831 			continue;
16832 
16833 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
16834 		encoder->get_power_domains(encoder, crtc_state);
16835 	}
16836 }
16837 
16838 static void intel_early_display_was(struct drm_i915_private *dev_priv)
16839 {
16840 	/* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
16841 	if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
16842 		I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
16843 			   DARBF_GATING_DIS);
16844 
16845 	if (IS_HASWELL(dev_priv)) {
16846 		/*
16847 		 * WaRsPkgCStateDisplayPMReq:hsw
16848 		 * System hang if this isn't done before disabling all planes!
16849 		 */
16850 		I915_WRITE(CHICKEN_PAR1_1,
16851 			   I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
16852 	}
16853 }
16854 
16855 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
16856 				       enum port port, i915_reg_t hdmi_reg)
16857 {
16858 	u32 val = I915_READ(hdmi_reg);
16859 
16860 	if (val & SDVO_ENABLE ||
16861 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
16862 		return;
16863 
16864 	DRM_DEBUG_KMS("Sanitizing transcoder select for HDMI %c\n",
16865 		      port_name(port));
16866 
16867 	val &= ~SDVO_PIPE_SEL_MASK;
16868 	val |= SDVO_PIPE_SEL(PIPE_A);
16869 
16870 	I915_WRITE(hdmi_reg, val);
16871 }
16872 
16873 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
16874 				     enum port port, i915_reg_t dp_reg)
16875 {
16876 	u32 val = I915_READ(dp_reg);
16877 
16878 	if (val & DP_PORT_EN ||
16879 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
16880 		return;
16881 
16882 	DRM_DEBUG_KMS("Sanitizing transcoder select for DP %c\n",
16883 		      port_name(port));
16884 
16885 	val &= ~DP_PIPE_SEL_MASK;
16886 	val |= DP_PIPE_SEL(PIPE_A);
16887 
16888 	I915_WRITE(dp_reg, val);
16889 }
16890 
16891 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
16892 {
16893 	/*
16894 	 * The BIOS may select transcoder B on some of the PCH
16895 	 * ports even it doesn't enable the port. This would trip
16896 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
16897 	 * Sanitize the transcoder select bits to prevent that. We
16898 	 * assume that the BIOS never actually enabled the port,
16899 	 * because if it did we'd actually have to toggle the port
16900 	 * on and back off to make the transcoder A select stick
16901 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
16902 	 * intel_disable_sdvo()).
16903 	 */
16904 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
16905 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
16906 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
16907 
16908 	/* PCH SDVOB multiplex with HDMIB */
16909 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
16910 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
16911 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
16912 }
16913 
16914 /* Scan out the current hw modeset state,
16915  * and sanitizes it to the current state
16916  */
16917 static void
16918 intel_modeset_setup_hw_state(struct drm_device *dev,
16919 			     struct drm_modeset_acquire_ctx *ctx)
16920 {
16921 	struct drm_i915_private *dev_priv = to_i915(dev);
16922 	struct intel_crtc_state *crtc_state;
16923 	struct intel_encoder *encoder;
16924 	struct intel_crtc *crtc;
16925 	intel_wakeref_t wakeref;
16926 	int i;
16927 
16928 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
16929 
16930 	intel_early_display_was(dev_priv);
16931 	intel_modeset_readout_hw_state(dev);
16932 
16933 	/* HW state is read out, now we need to sanitize this mess. */
16934 
16935 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
16936 	for_each_intel_encoder(dev, encoder) {
16937 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
16938 
16939 		/* We need to sanitize only the MST primary port. */
16940 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
16941 		    intel_phy_is_tc(dev_priv, phy))
16942 			intel_tc_port_sanitize(enc_to_dig_port(&encoder->base));
16943 	}
16944 
16945 	get_encoder_power_domains(dev_priv);
16946 
16947 	if (HAS_PCH_IBX(dev_priv))
16948 		ibx_sanitize_pch_ports(dev_priv);
16949 
16950 	/*
16951 	 * intel_sanitize_plane_mapping() may need to do vblank
16952 	 * waits, so we need vblank interrupts restored beforehand.
16953 	 */
16954 	for_each_intel_crtc(&dev_priv->drm, crtc) {
16955 		crtc_state = to_intel_crtc_state(crtc->base.state);
16956 
16957 		drm_crtc_vblank_reset(&crtc->base);
16958 
16959 		if (crtc_state->base.active)
16960 			intel_crtc_vblank_on(crtc_state);
16961 	}
16962 
16963 	intel_sanitize_plane_mapping(dev_priv);
16964 
16965 	for_each_intel_encoder(dev, encoder)
16966 		intel_sanitize_encoder(encoder);
16967 
16968 	for_each_intel_crtc(&dev_priv->drm, crtc) {
16969 		crtc_state = to_intel_crtc_state(crtc->base.state);
16970 		intel_sanitize_crtc(crtc, ctx);
16971 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
16972 	}
16973 
16974 	intel_modeset_update_connector_atomic_state(dev);
16975 
16976 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16977 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16978 
16979 		if (!pll->on || pll->active_mask)
16980 			continue;
16981 
16982 		DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
16983 			      pll->info->name);
16984 
16985 		pll->info->funcs->disable(dev_priv, pll);
16986 		pll->on = false;
16987 	}
16988 
16989 	if (IS_G4X(dev_priv)) {
16990 		g4x_wm_get_hw_state(dev_priv);
16991 		g4x_wm_sanitize(dev_priv);
16992 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16993 		vlv_wm_get_hw_state(dev_priv);
16994 		vlv_wm_sanitize(dev_priv);
16995 	} else if (INTEL_GEN(dev_priv) >= 9) {
16996 		skl_wm_get_hw_state(dev_priv);
16997 	} else if (HAS_PCH_SPLIT(dev_priv)) {
16998 		ilk_wm_get_hw_state(dev_priv);
16999 	}
17000 
17001 	for_each_intel_crtc(dev, crtc) {
17002 		u64 put_domains;
17003 
17004 		crtc_state = to_intel_crtc_state(crtc->base.state);
17005 		put_domains = modeset_get_crtc_power_domains(crtc_state);
17006 		if (WARN_ON(put_domains))
17007 			modeset_put_power_domains(dev_priv, put_domains);
17008 	}
17009 
17010 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
17011 
17012 	intel_fbc_init_pipe_state(dev_priv);
17013 }
17014 
17015 void intel_display_resume(struct drm_device *dev)
17016 {
17017 	struct drm_i915_private *dev_priv = to_i915(dev);
17018 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17019 	struct drm_modeset_acquire_ctx ctx;
17020 	int ret;
17021 
17022 	dev_priv->modeset_restore_state = NULL;
17023 	if (state)
17024 		state->acquire_ctx = &ctx;
17025 
17026 	drm_modeset_acquire_init(&ctx, 0);
17027 
17028 	while (1) {
17029 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
17030 		if (ret != -EDEADLK)
17031 			break;
17032 
17033 		drm_modeset_backoff(&ctx);
17034 	}
17035 
17036 	if (!ret)
17037 		ret = __intel_display_resume(dev, state, &ctx);
17038 
17039 	intel_enable_ipc(dev_priv);
17040 	drm_modeset_drop_locks(&ctx);
17041 	drm_modeset_acquire_fini(&ctx);
17042 
17043 	if (ret)
17044 		DRM_ERROR("Restoring old state failed with %i\n", ret);
17045 	if (state)
17046 		drm_atomic_state_put(state);
17047 }
17048 
17049 static void intel_hpd_poll_fini(struct drm_device *dev)
17050 {
17051 	struct intel_connector *connector;
17052 	struct drm_connector_list_iter conn_iter;
17053 
17054 	/* Kill all the work that may have been queued by hpd. */
17055 	drm_connector_list_iter_begin(dev, &conn_iter);
17056 	for_each_intel_connector_iter(connector, &conn_iter) {
17057 		if (connector->modeset_retry_work.func)
17058 			cancel_work_sync(&connector->modeset_retry_work);
17059 		if (connector->hdcp.shim) {
17060 			cancel_delayed_work_sync(&connector->hdcp.check_work);
17061 			cancel_work_sync(&connector->hdcp.prop_work);
17062 		}
17063 	}
17064 	drm_connector_list_iter_end(&conn_iter);
17065 }
17066 
17067 void intel_modeset_driver_remove(struct drm_device *dev)
17068 {
17069 	struct drm_i915_private *dev_priv = to_i915(dev);
17070 
17071 	flush_workqueue(dev_priv->modeset_wq);
17072 
17073 	flush_work(&dev_priv->atomic_helper.free_work);
17074 	WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
17075 
17076 	/*
17077 	 * Interrupts and polling as the first thing to avoid creating havoc.
17078 	 * Too much stuff here (turning of connectors, ...) would
17079 	 * experience fancy races otherwise.
17080 	 */
17081 	intel_irq_uninstall(dev_priv);
17082 
17083 	/*
17084 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
17085 	 * poll handlers. Hence disable polling after hpd handling is shut down.
17086 	 */
17087 	intel_hpd_poll_fini(dev);
17088 
17089 	/* poll work can call into fbdev, hence clean that up afterwards */
17090 	intel_fbdev_fini(dev_priv);
17091 
17092 	intel_unregister_dsm_handler();
17093 
17094 	intel_fbc_global_disable(dev_priv);
17095 
17096 	/* flush any delayed tasks or pending work */
17097 	flush_scheduled_work();
17098 
17099 	intel_hdcp_component_fini(dev_priv);
17100 
17101 	drm_mode_config_cleanup(dev);
17102 
17103 	intel_overlay_cleanup(dev_priv);
17104 
17105 	intel_gmbus_teardown(dev_priv);
17106 
17107 	destroy_workqueue(dev_priv->modeset_wq);
17108 
17109 	intel_fbc_cleanup_cfb(dev_priv);
17110 }
17111 
17112 /*
17113  * set vga decode state - true == enable VGA decode
17114  */
17115 int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
17116 {
17117 	unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
17118 	u16 gmch_ctrl;
17119 
17120 	if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17121 		DRM_ERROR("failed to read control word\n");
17122 		return -EIO;
17123 	}
17124 
17125 	if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17126 		return 0;
17127 
17128 	if (state)
17129 		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17130 	else
17131 		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
17132 
17133 	if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17134 		DRM_ERROR("failed to write control word\n");
17135 		return -EIO;
17136 	}
17137 
17138 	return 0;
17139 }
17140 
17141 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17142 
17143 struct intel_display_error_state {
17144 
17145 	u32 power_well_driver;
17146 
17147 	struct intel_cursor_error_state {
17148 		u32 control;
17149 		u32 position;
17150 		u32 base;
17151 		u32 size;
17152 	} cursor[I915_MAX_PIPES];
17153 
17154 	struct intel_pipe_error_state {
17155 		bool power_domain_on;
17156 		u32 source;
17157 		u32 stat;
17158 	} pipe[I915_MAX_PIPES];
17159 
17160 	struct intel_plane_error_state {
17161 		u32 control;
17162 		u32 stride;
17163 		u32 size;
17164 		u32 pos;
17165 		u32 addr;
17166 		u32 surface;
17167 		u32 tile_offset;
17168 	} plane[I915_MAX_PIPES];
17169 
17170 	struct intel_transcoder_error_state {
17171 		bool available;
17172 		bool power_domain_on;
17173 		enum transcoder cpu_transcoder;
17174 
17175 		u32 conf;
17176 
17177 		u32 htotal;
17178 		u32 hblank;
17179 		u32 hsync;
17180 		u32 vtotal;
17181 		u32 vblank;
17182 		u32 vsync;
17183 	} transcoder[5];
17184 };
17185 
17186 struct intel_display_error_state *
17187 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
17188 {
17189 	struct intel_display_error_state *error;
17190 	int transcoders[] = {
17191 		TRANSCODER_A,
17192 		TRANSCODER_B,
17193 		TRANSCODER_C,
17194 		TRANSCODER_D,
17195 		TRANSCODER_EDP,
17196 	};
17197 	int i;
17198 
17199 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
17200 
17201 	if (!HAS_DISPLAY(dev_priv))
17202 		return NULL;
17203 
17204 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
17205 	if (error == NULL)
17206 		return NULL;
17207 
17208 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
17209 		error->power_well_driver = I915_READ(HSW_PWR_WELL_CTL2);
17210 
17211 	for_each_pipe(dev_priv, i) {
17212 		error->pipe[i].power_domain_on =
17213 			__intel_display_power_is_enabled(dev_priv,
17214 							 POWER_DOMAIN_PIPE(i));
17215 		if (!error->pipe[i].power_domain_on)
17216 			continue;
17217 
17218 		error->cursor[i].control = I915_READ(CURCNTR(i));
17219 		error->cursor[i].position = I915_READ(CURPOS(i));
17220 		error->cursor[i].base = I915_READ(CURBASE(i));
17221 
17222 		error->plane[i].control = I915_READ(DSPCNTR(i));
17223 		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
17224 		if (INTEL_GEN(dev_priv) <= 3) {
17225 			error->plane[i].size = I915_READ(DSPSIZE(i));
17226 			error->plane[i].pos = I915_READ(DSPPOS(i));
17227 		}
17228 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
17229 			error->plane[i].addr = I915_READ(DSPADDR(i));
17230 		if (INTEL_GEN(dev_priv) >= 4) {
17231 			error->plane[i].surface = I915_READ(DSPSURF(i));
17232 			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17233 		}
17234 
17235 		error->pipe[i].source = I915_READ(PIPESRC(i));
17236 
17237 		if (HAS_GMCH(dev_priv))
17238 			error->pipe[i].stat = I915_READ(PIPESTAT(i));
17239 	}
17240 
17241 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
17242 		enum transcoder cpu_transcoder = transcoders[i];
17243 
17244 		if (!INTEL_INFO(dev_priv)->trans_offsets[cpu_transcoder])
17245 			continue;
17246 
17247 		error->transcoder[i].available = true;
17248 		error->transcoder[i].power_domain_on =
17249 			__intel_display_power_is_enabled(dev_priv,
17250 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
17251 		if (!error->transcoder[i].power_domain_on)
17252 			continue;
17253 
17254 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
17255 
17256 		error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17257 		error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17258 		error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17259 		error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17260 		error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17261 		error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17262 		error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
17263 	}
17264 
17265 	return error;
17266 }
17267 
17268 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17269 
17270 void
17271 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
17272 				struct intel_display_error_state *error)
17273 {
17274 	struct drm_i915_private *dev_priv = m->i915;
17275 	int i;
17276 
17277 	if (!error)
17278 		return;
17279 
17280 	err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
17281 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
17282 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
17283 			   error->power_well_driver);
17284 	for_each_pipe(dev_priv, i) {
17285 		err_printf(m, "Pipe [%d]:\n", i);
17286 		err_printf(m, "  Power: %s\n",
17287 			   onoff(error->pipe[i].power_domain_on));
17288 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
17289 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
17290 
17291 		err_printf(m, "Plane [%d]:\n", i);
17292 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
17293 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
17294 		if (INTEL_GEN(dev_priv) <= 3) {
17295 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
17296 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
17297 		}
17298 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
17299 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
17300 		if (INTEL_GEN(dev_priv) >= 4) {
17301 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
17302 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
17303 		}
17304 
17305 		err_printf(m, "Cursor [%d]:\n", i);
17306 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
17307 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
17308 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
17309 	}
17310 
17311 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
17312 		if (!error->transcoder[i].available)
17313 			continue;
17314 
17315 		err_printf(m, "CPU transcoder: %s\n",
17316 			   transcoder_name(error->transcoder[i].cpu_transcoder));
17317 		err_printf(m, "  Power: %s\n",
17318 			   onoff(error->transcoder[i].power_domain_on));
17319 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
17320 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
17321 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
17322 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
17323 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
17324 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
17325 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
17326 	}
17327 }
17328 
17329 #endif
17330