xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_display.c (revision 15a1fbdcfb519c2bd291ed01c6c94e0b89537a77)
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/i2c.h>
28 #include <linux/input.h>
29 #include <linux/intel-iommu.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dma-resv.h>
33 #include <linux/slab.h>
34 
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_atomic_uapi.h>
38 #include <drm/drm_dp_helper.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_plane_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_rect.h>
44 #include <drm/i915_drm.h>
45 
46 #include "display/intel_crt.h"
47 #include "display/intel_ddi.h"
48 #include "display/intel_dp.h"
49 #include "display/intel_dp_mst.h"
50 #include "display/intel_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 "gt/intel_rps.h"
60 
61 #include "i915_drv.h"
62 #include "i915_trace.h"
63 #include "intel_acpi.h"
64 #include "intel_atomic.h"
65 #include "intel_atomic_plane.h"
66 #include "intel_bw.h"
67 #include "intel_cdclk.h"
68 #include "intel_color.h"
69 #include "intel_display_types.h"
70 #include "intel_dp_link_training.h"
71 #include "intel_fbc.h"
72 #include "intel_fbdev.h"
73 #include "intel_fifo_underrun.h"
74 #include "intel_frontbuffer.h"
75 #include "intel_hdcp.h"
76 #include "intel_hotplug.h"
77 #include "intel_overlay.h"
78 #include "intel_pipe_crc.h"
79 #include "intel_pm.h"
80 #include "intel_psr.h"
81 #include "intel_quirks.h"
82 #include "intel_sideband.h"
83 #include "intel_sprite.h"
84 #include "intel_tc.h"
85 #include "intel_vga.h"
86 
87 /* Primary plane formats for gen <= 3 */
88 static const u32 i8xx_primary_formats[] = {
89 	DRM_FORMAT_C8,
90 	DRM_FORMAT_XRGB1555,
91 	DRM_FORMAT_RGB565,
92 	DRM_FORMAT_XRGB8888,
93 };
94 
95 /* Primary plane formats for ivb (no fp16 due to hw issue) */
96 static const u32 ivb_primary_formats[] = {
97 	DRM_FORMAT_C8,
98 	DRM_FORMAT_RGB565,
99 	DRM_FORMAT_XRGB8888,
100 	DRM_FORMAT_XBGR8888,
101 	DRM_FORMAT_XRGB2101010,
102 	DRM_FORMAT_XBGR2101010,
103 };
104 
105 /* Primary plane formats for gen >= 4, except ivb */
106 static const u32 i965_primary_formats[] = {
107 	DRM_FORMAT_C8,
108 	DRM_FORMAT_RGB565,
109 	DRM_FORMAT_XRGB8888,
110 	DRM_FORMAT_XBGR8888,
111 	DRM_FORMAT_XRGB2101010,
112 	DRM_FORMAT_XBGR2101010,
113 	DRM_FORMAT_XBGR16161616F,
114 };
115 
116 /* Primary plane formats for vlv/chv */
117 static const u32 vlv_primary_formats[] = {
118 	DRM_FORMAT_C8,
119 	DRM_FORMAT_RGB565,
120 	DRM_FORMAT_XRGB8888,
121 	DRM_FORMAT_XBGR8888,
122 	DRM_FORMAT_ARGB8888,
123 	DRM_FORMAT_ABGR8888,
124 	DRM_FORMAT_XRGB2101010,
125 	DRM_FORMAT_XBGR2101010,
126 	DRM_FORMAT_ARGB2101010,
127 	DRM_FORMAT_ABGR2101010,
128 	DRM_FORMAT_XBGR16161616F,
129 };
130 
131 static const u64 i9xx_format_modifiers[] = {
132 	I915_FORMAT_MOD_X_TILED,
133 	DRM_FORMAT_MOD_LINEAR,
134 	DRM_FORMAT_MOD_INVALID
135 };
136 
137 /* Cursor formats */
138 static const u32 intel_cursor_formats[] = {
139 	DRM_FORMAT_ARGB8888,
140 };
141 
142 static const u64 cursor_format_modifiers[] = {
143 	DRM_FORMAT_MOD_LINEAR,
144 	DRM_FORMAT_MOD_INVALID
145 };
146 
147 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
148 				struct intel_crtc_state *pipe_config);
149 static void ilk_pch_clock_get(struct intel_crtc *crtc,
150 			      struct intel_crtc_state *pipe_config);
151 
152 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
153 				  struct drm_i915_gem_object *obj,
154 				  struct drm_mode_fb_cmd2 *mode_cmd);
155 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state);
156 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
157 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
158 					 const struct intel_link_m_n *m_n,
159 					 const struct intel_link_m_n *m2_n2);
160 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
161 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
162 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
163 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
164 static void vlv_prepare_pll(struct intel_crtc *crtc,
165 			    const struct intel_crtc_state *pipe_config);
166 static void chv_prepare_pll(struct intel_crtc *crtc,
167 			    const struct intel_crtc_state *pipe_config);
168 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
169 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
170 static void intel_modeset_setup_hw_state(struct drm_device *dev,
171 					 struct drm_modeset_acquire_ctx *ctx);
172 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
173 
174 struct intel_limit {
175 	struct {
176 		int min, max;
177 	} dot, vco, n, m, m1, m2, p, p1;
178 
179 	struct {
180 		int dot_limit;
181 		int p2_slow, p2_fast;
182 	} p2;
183 };
184 
185 /* returns HPLL frequency in kHz */
186 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
187 {
188 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
189 
190 	/* Obtain SKU information */
191 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
192 		CCK_FUSE_HPLL_FREQ_MASK;
193 
194 	return vco_freq[hpll_freq] * 1000;
195 }
196 
197 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
198 		      const char *name, u32 reg, int ref_freq)
199 {
200 	u32 val;
201 	int divider;
202 
203 	val = vlv_cck_read(dev_priv, reg);
204 	divider = val & CCK_FREQUENCY_VALUES;
205 
206 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
207 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
208 		 "%s change in progress\n", name);
209 
210 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
211 }
212 
213 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
214 			   const char *name, u32 reg)
215 {
216 	int hpll;
217 
218 	vlv_cck_get(dev_priv);
219 
220 	if (dev_priv->hpll_freq == 0)
221 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
222 
223 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
224 
225 	vlv_cck_put(dev_priv);
226 
227 	return hpll;
228 }
229 
230 static void intel_update_czclk(struct drm_i915_private *dev_priv)
231 {
232 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
233 		return;
234 
235 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
236 						      CCK_CZ_CLOCK_CONTROL);
237 
238 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
239 		dev_priv->czclk_freq);
240 }
241 
242 static inline u32 /* units of 100MHz */
243 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
244 		    const struct intel_crtc_state *pipe_config)
245 {
246 	if (HAS_DDI(dev_priv))
247 		return pipe_config->port_clock; /* SPLL */
248 	else
249 		return dev_priv->fdi_pll_freq;
250 }
251 
252 static const struct intel_limit intel_limits_i8xx_dac = {
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 = 2, .max = 33 },
261 	.p2 = { .dot_limit = 165000,
262 		.p2_slow = 4, .p2_fast = 2 },
263 };
264 
265 static const struct intel_limit intel_limits_i8xx_dvo = {
266 	.dot = { .min = 25000, .max = 350000 },
267 	.vco = { .min = 908000, .max = 1512000 },
268 	.n = { .min = 2, .max = 16 },
269 	.m = { .min = 96, .max = 140 },
270 	.m1 = { .min = 18, .max = 26 },
271 	.m2 = { .min = 6, .max = 16 },
272 	.p = { .min = 4, .max = 128 },
273 	.p1 = { .min = 2, .max = 33 },
274 	.p2 = { .dot_limit = 165000,
275 		.p2_slow = 4, .p2_fast = 4 },
276 };
277 
278 static const struct intel_limit intel_limits_i8xx_lvds = {
279 	.dot = { .min = 25000, .max = 350000 },
280 	.vco = { .min = 908000, .max = 1512000 },
281 	.n = { .min = 2, .max = 16 },
282 	.m = { .min = 96, .max = 140 },
283 	.m1 = { .min = 18, .max = 26 },
284 	.m2 = { .min = 6, .max = 16 },
285 	.p = { .min = 4, .max = 128 },
286 	.p1 = { .min = 1, .max = 6 },
287 	.p2 = { .dot_limit = 165000,
288 		.p2_slow = 14, .p2_fast = 7 },
289 };
290 
291 static const struct intel_limit intel_limits_i9xx_sdvo = {
292 	.dot = { .min = 20000, .max = 400000 },
293 	.vco = { .min = 1400000, .max = 2800000 },
294 	.n = { .min = 1, .max = 6 },
295 	.m = { .min = 70, .max = 120 },
296 	.m1 = { .min = 8, .max = 18 },
297 	.m2 = { .min = 3, .max = 7 },
298 	.p = { .min = 5, .max = 80 },
299 	.p1 = { .min = 1, .max = 8 },
300 	.p2 = { .dot_limit = 200000,
301 		.p2_slow = 10, .p2_fast = 5 },
302 };
303 
304 static const struct intel_limit intel_limits_i9xx_lvds = {
305 	.dot = { .min = 20000, .max = 400000 },
306 	.vco = { .min = 1400000, .max = 2800000 },
307 	.n = { .min = 1, .max = 6 },
308 	.m = { .min = 70, .max = 120 },
309 	.m1 = { .min = 8, .max = 18 },
310 	.m2 = { .min = 3, .max = 7 },
311 	.p = { .min = 7, .max = 98 },
312 	.p1 = { .min = 1, .max = 8 },
313 	.p2 = { .dot_limit = 112000,
314 		.p2_slow = 14, .p2_fast = 7 },
315 };
316 
317 
318 static const struct intel_limit intel_limits_g4x_sdvo = {
319 	.dot = { .min = 25000, .max = 270000 },
320 	.vco = { .min = 1750000, .max = 3500000},
321 	.n = { .min = 1, .max = 4 },
322 	.m = { .min = 104, .max = 138 },
323 	.m1 = { .min = 17, .max = 23 },
324 	.m2 = { .min = 5, .max = 11 },
325 	.p = { .min = 10, .max = 30 },
326 	.p1 = { .min = 1, .max = 3},
327 	.p2 = { .dot_limit = 270000,
328 		.p2_slow = 10,
329 		.p2_fast = 10
330 	},
331 };
332 
333 static const struct intel_limit intel_limits_g4x_hdmi = {
334 	.dot = { .min = 22000, .max = 400000 },
335 	.vco = { .min = 1750000, .max = 3500000},
336 	.n = { .min = 1, .max = 4 },
337 	.m = { .min = 104, .max = 138 },
338 	.m1 = { .min = 16, .max = 23 },
339 	.m2 = { .min = 5, .max = 11 },
340 	.p = { .min = 5, .max = 80 },
341 	.p1 = { .min = 1, .max = 8},
342 	.p2 = { .dot_limit = 165000,
343 		.p2_slow = 10, .p2_fast = 5 },
344 };
345 
346 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
347 	.dot = { .min = 20000, .max = 115000 },
348 	.vco = { .min = 1750000, .max = 3500000 },
349 	.n = { .min = 1, .max = 3 },
350 	.m = { .min = 104, .max = 138 },
351 	.m1 = { .min = 17, .max = 23 },
352 	.m2 = { .min = 5, .max = 11 },
353 	.p = { .min = 28, .max = 112 },
354 	.p1 = { .min = 2, .max = 8 },
355 	.p2 = { .dot_limit = 0,
356 		.p2_slow = 14, .p2_fast = 14
357 	},
358 };
359 
360 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
361 	.dot = { .min = 80000, .max = 224000 },
362 	.vco = { .min = 1750000, .max = 3500000 },
363 	.n = { .min = 1, .max = 3 },
364 	.m = { .min = 104, .max = 138 },
365 	.m1 = { .min = 17, .max = 23 },
366 	.m2 = { .min = 5, .max = 11 },
367 	.p = { .min = 14, .max = 42 },
368 	.p1 = { .min = 2, .max = 6 },
369 	.p2 = { .dot_limit = 0,
370 		.p2_slow = 7, .p2_fast = 7
371 	},
372 };
373 
374 static const struct intel_limit pnv_limits_sdvo = {
375 	.dot = { .min = 20000, .max = 400000},
376 	.vco = { .min = 1700000, .max = 3500000 },
377 	/* Pineview's Ncounter is a ring counter */
378 	.n = { .min = 3, .max = 6 },
379 	.m = { .min = 2, .max = 256 },
380 	/* Pineview only has one combined m divider, which we treat as m2. */
381 	.m1 = { .min = 0, .max = 0 },
382 	.m2 = { .min = 0, .max = 254 },
383 	.p = { .min = 5, .max = 80 },
384 	.p1 = { .min = 1, .max = 8 },
385 	.p2 = { .dot_limit = 200000,
386 		.p2_slow = 10, .p2_fast = 5 },
387 };
388 
389 static const struct intel_limit pnv_limits_lvds = {
390 	.dot = { .min = 20000, .max = 400000 },
391 	.vco = { .min = 1700000, .max = 3500000 },
392 	.n = { .min = 3, .max = 6 },
393 	.m = { .min = 2, .max = 256 },
394 	.m1 = { .min = 0, .max = 0 },
395 	.m2 = { .min = 0, .max = 254 },
396 	.p = { .min = 7, .max = 112 },
397 	.p1 = { .min = 1, .max = 8 },
398 	.p2 = { .dot_limit = 112000,
399 		.p2_slow = 14, .p2_fast = 14 },
400 };
401 
402 /* Ironlake / Sandybridge
403  *
404  * We calculate clock using (register_value + 2) for N/M1/M2, so here
405  * the range value for them is (actual_value - 2).
406  */
407 static const struct intel_limit ilk_limits_dac = {
408 	.dot = { .min = 25000, .max = 350000 },
409 	.vco = { .min = 1760000, .max = 3510000 },
410 	.n = { .min = 1, .max = 5 },
411 	.m = { .min = 79, .max = 127 },
412 	.m1 = { .min = 12, .max = 22 },
413 	.m2 = { .min = 5, .max = 9 },
414 	.p = { .min = 5, .max = 80 },
415 	.p1 = { .min = 1, .max = 8 },
416 	.p2 = { .dot_limit = 225000,
417 		.p2_slow = 10, .p2_fast = 5 },
418 };
419 
420 static const struct intel_limit ilk_limits_single_lvds = {
421 	.dot = { .min = 25000, .max = 350000 },
422 	.vco = { .min = 1760000, .max = 3510000 },
423 	.n = { .min = 1, .max = 3 },
424 	.m = { .min = 79, .max = 118 },
425 	.m1 = { .min = 12, .max = 22 },
426 	.m2 = { .min = 5, .max = 9 },
427 	.p = { .min = 28, .max = 112 },
428 	.p1 = { .min = 2, .max = 8 },
429 	.p2 = { .dot_limit = 225000,
430 		.p2_slow = 14, .p2_fast = 14 },
431 };
432 
433 static const struct intel_limit ilk_limits_dual_lvds = {
434 	.dot = { .min = 25000, .max = 350000 },
435 	.vco = { .min = 1760000, .max = 3510000 },
436 	.n = { .min = 1, .max = 3 },
437 	.m = { .min = 79, .max = 127 },
438 	.m1 = { .min = 12, .max = 22 },
439 	.m2 = { .min = 5, .max = 9 },
440 	.p = { .min = 14, .max = 56 },
441 	.p1 = { .min = 2, .max = 8 },
442 	.p2 = { .dot_limit = 225000,
443 		.p2_slow = 7, .p2_fast = 7 },
444 };
445 
446 /* LVDS 100mhz refclk limits. */
447 static const struct intel_limit ilk_limits_single_lvds_100m = {
448 	.dot = { .min = 25000, .max = 350000 },
449 	.vco = { .min = 1760000, .max = 3510000 },
450 	.n = { .min = 1, .max = 2 },
451 	.m = { .min = 79, .max = 126 },
452 	.m1 = { .min = 12, .max = 22 },
453 	.m2 = { .min = 5, .max = 9 },
454 	.p = { .min = 28, .max = 112 },
455 	.p1 = { .min = 2, .max = 8 },
456 	.p2 = { .dot_limit = 225000,
457 		.p2_slow = 14, .p2_fast = 14 },
458 };
459 
460 static const struct intel_limit ilk_limits_dual_lvds_100m = {
461 	.dot = { .min = 25000, .max = 350000 },
462 	.vco = { .min = 1760000, .max = 3510000 },
463 	.n = { .min = 1, .max = 3 },
464 	.m = { .min = 79, .max = 126 },
465 	.m1 = { .min = 12, .max = 22 },
466 	.m2 = { .min = 5, .max = 9 },
467 	.p = { .min = 14, .max = 42 },
468 	.p1 = { .min = 2, .max = 6 },
469 	.p2 = { .dot_limit = 225000,
470 		.p2_slow = 7, .p2_fast = 7 },
471 };
472 
473 static const struct intel_limit intel_limits_vlv = {
474 	 /*
475 	  * These are the data rate limits (measured in fast clocks)
476 	  * since those are the strictest limits we have. The fast
477 	  * clock and actual rate limits are more relaxed, so checking
478 	  * them would make no difference.
479 	  */
480 	.dot = { .min = 25000 * 5, .max = 270000 * 5 },
481 	.vco = { .min = 4000000, .max = 6000000 },
482 	.n = { .min = 1, .max = 7 },
483 	.m1 = { .min = 2, .max = 3 },
484 	.m2 = { .min = 11, .max = 156 },
485 	.p1 = { .min = 2, .max = 3 },
486 	.p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
487 };
488 
489 static const struct intel_limit intel_limits_chv = {
490 	/*
491 	 * These are the data rate limits (measured in fast clocks)
492 	 * since those are the strictest limits we have.  The fast
493 	 * clock and actual rate limits are more relaxed, so checking
494 	 * them would make no difference.
495 	 */
496 	.dot = { .min = 25000 * 5, .max = 540000 * 5},
497 	.vco = { .min = 4800000, .max = 6480000 },
498 	.n = { .min = 1, .max = 1 },
499 	.m1 = { .min = 2, .max = 2 },
500 	.m2 = { .min = 24 << 22, .max = 175 << 22 },
501 	.p1 = { .min = 2, .max = 4 },
502 	.p2 = {	.p2_slow = 1, .p2_fast = 14 },
503 };
504 
505 static const struct intel_limit intel_limits_bxt = {
506 	/* FIXME: find real dot limits */
507 	.dot = { .min = 0, .max = INT_MAX },
508 	.vco = { .min = 4800000, .max = 6700000 },
509 	.n = { .min = 1, .max = 1 },
510 	.m1 = { .min = 2, .max = 2 },
511 	/* FIXME: find real m2 limits */
512 	.m2 = { .min = 2 << 22, .max = 255 << 22 },
513 	.p1 = { .min = 2, .max = 4 },
514 	.p2 = { .p2_slow = 1, .p2_fast = 20 },
515 };
516 
517 /* WA Display #0827: Gen9:all */
518 static void
519 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
520 {
521 	if (enable)
522 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
523 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
524 	else
525 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
526 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
527 }
528 
529 /* Wa_2006604312:icl */
530 static void
531 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
532 		       bool enable)
533 {
534 	if (enable)
535 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
536 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
537 	else
538 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
539 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
540 }
541 
542 static bool
543 needs_modeset(const struct intel_crtc_state *state)
544 {
545 	return drm_atomic_crtc_needs_modeset(&state->uapi);
546 }
547 
548 bool
549 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
550 {
551 	return (crtc_state->master_transcoder != INVALID_TRANSCODER ||
552 		crtc_state->sync_mode_slaves_mask);
553 }
554 
555 static bool
556 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
557 {
558 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
559 }
560 
561 /*
562  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
563  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
564  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
565  * The helpers' return value is the rate of the clock that is fed to the
566  * display engine's pipe which can be the above fast dot clock rate or a
567  * divided-down version of it.
568  */
569 /* m1 is reserved as 0 in Pineview, n is a ring counter */
570 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
571 {
572 	clock->m = clock->m2 + 2;
573 	clock->p = clock->p1 * clock->p2;
574 	if (WARN_ON(clock->n == 0 || clock->p == 0))
575 		return 0;
576 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
577 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
578 
579 	return clock->dot;
580 }
581 
582 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
583 {
584 	return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
585 }
586 
587 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
588 {
589 	clock->m = i9xx_dpll_compute_m(clock);
590 	clock->p = clock->p1 * clock->p2;
591 	if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
592 		return 0;
593 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
594 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
595 
596 	return clock->dot;
597 }
598 
599 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
600 {
601 	clock->m = clock->m1 * clock->m2;
602 	clock->p = clock->p1 * clock->p2;
603 	if (WARN_ON(clock->n == 0 || clock->p == 0))
604 		return 0;
605 	clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
606 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
607 
608 	return clock->dot / 5;
609 }
610 
611 int chv_calc_dpll_params(int refclk, struct dpll *clock)
612 {
613 	clock->m = clock->m1 * clock->m2;
614 	clock->p = clock->p1 * clock->p2;
615 	if (WARN_ON(clock->n == 0 || clock->p == 0))
616 		return 0;
617 	clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
618 					   clock->n << 22);
619 	clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
620 
621 	return clock->dot / 5;
622 }
623 
624 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
625 
626 /*
627  * Returns whether the given set of divisors are valid for a given refclk with
628  * the given connectors.
629  */
630 static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
631 			       const struct intel_limit *limit,
632 			       const struct dpll *clock)
633 {
634 	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
635 		INTELPllInvalid("n out of range\n");
636 	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
637 		INTELPllInvalid("p1 out of range\n");
638 	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
639 		INTELPllInvalid("m2 out of range\n");
640 	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
641 		INTELPllInvalid("m1 out of range\n");
642 
643 	if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
644 	    !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
645 		if (clock->m1 <= clock->m2)
646 			INTELPllInvalid("m1 <= m2\n");
647 
648 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
649 	    !IS_GEN9_LP(dev_priv)) {
650 		if (clock->p < limit->p.min || limit->p.max < clock->p)
651 			INTELPllInvalid("p out of range\n");
652 		if (clock->m < limit->m.min || limit->m.max < clock->m)
653 			INTELPllInvalid("m out of range\n");
654 	}
655 
656 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
657 		INTELPllInvalid("vco out of range\n");
658 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
659 	 * connector, etc., rather than just a single range.
660 	 */
661 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
662 		INTELPllInvalid("dot out of range\n");
663 
664 	return true;
665 }
666 
667 static int
668 i9xx_select_p2_div(const struct intel_limit *limit,
669 		   const struct intel_crtc_state *crtc_state,
670 		   int target)
671 {
672 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
673 
674 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
675 		/*
676 		 * For LVDS just rely on its current settings for dual-channel.
677 		 * We haven't figured out how to reliably set up different
678 		 * single/dual channel state, if we even can.
679 		 */
680 		if (intel_is_dual_link_lvds(dev_priv))
681 			return limit->p2.p2_fast;
682 		else
683 			return limit->p2.p2_slow;
684 	} else {
685 		if (target < limit->p2.dot_limit)
686 			return limit->p2.p2_slow;
687 		else
688 			return limit->p2.p2_fast;
689 	}
690 }
691 
692 /*
693  * Returns a set of divisors for the desired target clock with the given
694  * refclk, or FALSE.  The returned values represent the clock equation:
695  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
696  *
697  * Target and reference clocks are specified in kHz.
698  *
699  * If match_clock is provided, then best_clock P divider must match the P
700  * divider from @match_clock used for LVDS downclocking.
701  */
702 static bool
703 i9xx_find_best_dpll(const struct intel_limit *limit,
704 		    struct intel_crtc_state *crtc_state,
705 		    int target, int refclk, struct dpll *match_clock,
706 		    struct dpll *best_clock)
707 {
708 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
709 	struct dpll clock;
710 	int err = target;
711 
712 	memset(best_clock, 0, sizeof(*best_clock));
713 
714 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
715 
716 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
717 	     clock.m1++) {
718 		for (clock.m2 = limit->m2.min;
719 		     clock.m2 <= limit->m2.max; clock.m2++) {
720 			if (clock.m2 >= clock.m1)
721 				break;
722 			for (clock.n = limit->n.min;
723 			     clock.n <= limit->n.max; clock.n++) {
724 				for (clock.p1 = limit->p1.min;
725 					clock.p1 <= limit->p1.max; clock.p1++) {
726 					int this_err;
727 
728 					i9xx_calc_dpll_params(refclk, &clock);
729 					if (!intel_PLL_is_valid(to_i915(dev),
730 								limit,
731 								&clock))
732 						continue;
733 					if (match_clock &&
734 					    clock.p != match_clock->p)
735 						continue;
736 
737 					this_err = abs(clock.dot - target);
738 					if (this_err < err) {
739 						*best_clock = clock;
740 						err = this_err;
741 					}
742 				}
743 			}
744 		}
745 	}
746 
747 	return (err != target);
748 }
749 
750 /*
751  * Returns a set of divisors for the desired target clock with the given
752  * refclk, or FALSE.  The returned values represent the clock equation:
753  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
754  *
755  * Target and reference clocks are specified in kHz.
756  *
757  * If match_clock is provided, then best_clock P divider must match the P
758  * divider from @match_clock used for LVDS downclocking.
759  */
760 static bool
761 pnv_find_best_dpll(const struct intel_limit *limit,
762 		   struct intel_crtc_state *crtc_state,
763 		   int target, int refclk, struct dpll *match_clock,
764 		   struct dpll *best_clock)
765 {
766 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
767 	struct dpll clock;
768 	int err = target;
769 
770 	memset(best_clock, 0, sizeof(*best_clock));
771 
772 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
773 
774 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
775 	     clock.m1++) {
776 		for (clock.m2 = limit->m2.min;
777 		     clock.m2 <= limit->m2.max; clock.m2++) {
778 			for (clock.n = limit->n.min;
779 			     clock.n <= limit->n.max; clock.n++) {
780 				for (clock.p1 = limit->p1.min;
781 					clock.p1 <= limit->p1.max; clock.p1++) {
782 					int this_err;
783 
784 					pnv_calc_dpll_params(refclk, &clock);
785 					if (!intel_PLL_is_valid(to_i915(dev),
786 								limit,
787 								&clock))
788 						continue;
789 					if (match_clock &&
790 					    clock.p != match_clock->p)
791 						continue;
792 
793 					this_err = abs(clock.dot - target);
794 					if (this_err < err) {
795 						*best_clock = clock;
796 						err = this_err;
797 					}
798 				}
799 			}
800 		}
801 	}
802 
803 	return (err != target);
804 }
805 
806 /*
807  * Returns a set of divisors for the desired target clock with the given
808  * refclk, or FALSE.  The returned values represent the clock equation:
809  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
810  *
811  * Target and reference clocks are specified in kHz.
812  *
813  * If match_clock is provided, then best_clock P divider must match the P
814  * divider from @match_clock used for LVDS downclocking.
815  */
816 static bool
817 g4x_find_best_dpll(const struct intel_limit *limit,
818 		   struct intel_crtc_state *crtc_state,
819 		   int target, int refclk, struct dpll *match_clock,
820 		   struct dpll *best_clock)
821 {
822 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
823 	struct dpll clock;
824 	int max_n;
825 	bool found = false;
826 	/* approximately equals target * 0.00585 */
827 	int err_most = (target >> 8) + (target >> 9);
828 
829 	memset(best_clock, 0, sizeof(*best_clock));
830 
831 	clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
832 
833 	max_n = limit->n.max;
834 	/* based on hardware requirement, prefer smaller n to precision */
835 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
836 		/* based on hardware requirement, prefere larger m1,m2 */
837 		for (clock.m1 = limit->m1.max;
838 		     clock.m1 >= limit->m1.min; clock.m1--) {
839 			for (clock.m2 = limit->m2.max;
840 			     clock.m2 >= limit->m2.min; clock.m2--) {
841 				for (clock.p1 = limit->p1.max;
842 				     clock.p1 >= limit->p1.min; clock.p1--) {
843 					int this_err;
844 
845 					i9xx_calc_dpll_params(refclk, &clock);
846 					if (!intel_PLL_is_valid(to_i915(dev),
847 								limit,
848 								&clock))
849 						continue;
850 
851 					this_err = abs(clock.dot - target);
852 					if (this_err < err_most) {
853 						*best_clock = clock;
854 						err_most = this_err;
855 						max_n = clock.n;
856 						found = true;
857 					}
858 				}
859 			}
860 		}
861 	}
862 	return found;
863 }
864 
865 /*
866  * Check if the calculated PLL configuration is more optimal compared to the
867  * best configuration and error found so far. Return the calculated error.
868  */
869 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
870 			       const struct dpll *calculated_clock,
871 			       const struct dpll *best_clock,
872 			       unsigned int best_error_ppm,
873 			       unsigned int *error_ppm)
874 {
875 	/*
876 	 * For CHV ignore the error and consider only the P value.
877 	 * Prefer a bigger P value based on HW requirements.
878 	 */
879 	if (IS_CHERRYVIEW(to_i915(dev))) {
880 		*error_ppm = 0;
881 
882 		return calculated_clock->p > best_clock->p;
883 	}
884 
885 	if (drm_WARN_ON_ONCE(dev, !target_freq))
886 		return false;
887 
888 	*error_ppm = div_u64(1000000ULL *
889 				abs(target_freq - calculated_clock->dot),
890 			     target_freq);
891 	/*
892 	 * Prefer a better P value over a better (smaller) error if the error
893 	 * is small. Ensure this preference for future configurations too by
894 	 * setting the error to 0.
895 	 */
896 	if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
897 		*error_ppm = 0;
898 
899 		return true;
900 	}
901 
902 	return *error_ppm + 10 < best_error_ppm;
903 }
904 
905 /*
906  * Returns a set of divisors for the desired target clock with the given
907  * refclk, or FALSE.  The returned values represent the clock equation:
908  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
909  */
910 static bool
911 vlv_find_best_dpll(const struct intel_limit *limit,
912 		   struct intel_crtc_state *crtc_state,
913 		   int target, int refclk, struct dpll *match_clock,
914 		   struct dpll *best_clock)
915 {
916 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
917 	struct drm_device *dev = crtc->base.dev;
918 	struct dpll clock;
919 	unsigned int bestppm = 1000000;
920 	/* min update 19.2 MHz */
921 	int max_n = min(limit->n.max, refclk / 19200);
922 	bool found = false;
923 
924 	target *= 5; /* fast clock */
925 
926 	memset(best_clock, 0, sizeof(*best_clock));
927 
928 	/* based on hardware requirement, prefer smaller n to precision */
929 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
930 		for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
931 			for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
932 			     clock.p2 -= clock.p2 > 10 ? 2 : 1) {
933 				clock.p = clock.p1 * clock.p2;
934 				/* based on hardware requirement, prefer bigger m1,m2 values */
935 				for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
936 					unsigned int ppm;
937 
938 					clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
939 								     refclk * clock.m1);
940 
941 					vlv_calc_dpll_params(refclk, &clock);
942 
943 					if (!intel_PLL_is_valid(to_i915(dev),
944 								limit,
945 								&clock))
946 						continue;
947 
948 					if (!vlv_PLL_is_optimal(dev, target,
949 								&clock,
950 								best_clock,
951 								bestppm, &ppm))
952 						continue;
953 
954 					*best_clock = clock;
955 					bestppm = ppm;
956 					found = true;
957 				}
958 			}
959 		}
960 	}
961 
962 	return found;
963 }
964 
965 /*
966  * Returns a set of divisors for the desired target clock with the given
967  * refclk, or FALSE.  The returned values represent the clock equation:
968  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
969  */
970 static bool
971 chv_find_best_dpll(const struct intel_limit *limit,
972 		   struct intel_crtc_state *crtc_state,
973 		   int target, int refclk, struct dpll *match_clock,
974 		   struct dpll *best_clock)
975 {
976 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
977 	struct drm_device *dev = crtc->base.dev;
978 	unsigned int best_error_ppm;
979 	struct dpll clock;
980 	u64 m2;
981 	int found = false;
982 
983 	memset(best_clock, 0, sizeof(*best_clock));
984 	best_error_ppm = 1000000;
985 
986 	/*
987 	 * Based on hardware doc, the n always set to 1, and m1 always
988 	 * set to 2.  If requires to support 200Mhz refclk, we need to
989 	 * revisit this because n may not 1 anymore.
990 	 */
991 	clock.n = 1, clock.m1 = 2;
992 	target *= 5;	/* fast clock */
993 
994 	for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
995 		for (clock.p2 = limit->p2.p2_fast;
996 				clock.p2 >= limit->p2.p2_slow;
997 				clock.p2 -= clock.p2 > 10 ? 2 : 1) {
998 			unsigned int error_ppm;
999 
1000 			clock.p = clock.p1 * clock.p2;
1001 
1002 			m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1003 						   refclk * clock.m1);
1004 
1005 			if (m2 > INT_MAX/clock.m1)
1006 				continue;
1007 
1008 			clock.m2 = m2;
1009 
1010 			chv_calc_dpll_params(refclk, &clock);
1011 
1012 			if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
1013 				continue;
1014 
1015 			if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1016 						best_error_ppm, &error_ppm))
1017 				continue;
1018 
1019 			*best_clock = clock;
1020 			best_error_ppm = error_ppm;
1021 			found = true;
1022 		}
1023 	}
1024 
1025 	return found;
1026 }
1027 
1028 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
1029 			struct dpll *best_clock)
1030 {
1031 	int refclk = 100000;
1032 	const struct intel_limit *limit = &intel_limits_bxt;
1033 
1034 	return chv_find_best_dpll(limit, crtc_state,
1035 				  crtc_state->port_clock, refclk,
1036 				  NULL, best_clock);
1037 }
1038 
1039 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1040 				    enum pipe pipe)
1041 {
1042 	i915_reg_t reg = PIPEDSL(pipe);
1043 	u32 line1, line2;
1044 	u32 line_mask;
1045 
1046 	if (IS_GEN(dev_priv, 2))
1047 		line_mask = DSL_LINEMASK_GEN2;
1048 	else
1049 		line_mask = DSL_LINEMASK_GEN3;
1050 
1051 	line1 = intel_de_read(dev_priv, reg) & line_mask;
1052 	msleep(5);
1053 	line2 = intel_de_read(dev_priv, reg) & line_mask;
1054 
1055 	return line1 != line2;
1056 }
1057 
1058 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1059 {
1060 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1061 	enum pipe pipe = crtc->pipe;
1062 
1063 	/* Wait for the display line to settle/start moving */
1064 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1065 		drm_err(&dev_priv->drm,
1066 			"pipe %c scanline %s wait timed out\n",
1067 			pipe_name(pipe), onoff(state));
1068 }
1069 
1070 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1071 {
1072 	wait_for_pipe_scanline_moving(crtc, false);
1073 }
1074 
1075 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1076 {
1077 	wait_for_pipe_scanline_moving(crtc, true);
1078 }
1079 
1080 static void
1081 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1082 {
1083 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1084 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1085 
1086 	if (INTEL_GEN(dev_priv) >= 4) {
1087 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1088 		i915_reg_t reg = PIPECONF(cpu_transcoder);
1089 
1090 		/* Wait for the Pipe State to go off */
1091 		if (intel_de_wait_for_clear(dev_priv, reg,
1092 					    I965_PIPECONF_ACTIVE, 100))
1093 			drm_WARN(&dev_priv->drm, 1,
1094 				 "pipe_off wait timed out\n");
1095 	} else {
1096 		intel_wait_for_pipe_scanline_stopped(crtc);
1097 	}
1098 }
1099 
1100 /* Only for pre-ILK configs */
1101 void assert_pll(struct drm_i915_private *dev_priv,
1102 		enum pipe pipe, bool state)
1103 {
1104 	u32 val;
1105 	bool cur_state;
1106 
1107 	val = intel_de_read(dev_priv, DPLL(pipe));
1108 	cur_state = !!(val & DPLL_VCO_ENABLE);
1109 	I915_STATE_WARN(cur_state != state,
1110 	     "PLL state assertion failure (expected %s, current %s)\n",
1111 			onoff(state), onoff(cur_state));
1112 }
1113 
1114 /* XXX: the dsi pll is shared between MIPI DSI ports */
1115 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1116 {
1117 	u32 val;
1118 	bool cur_state;
1119 
1120 	vlv_cck_get(dev_priv);
1121 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1122 	vlv_cck_put(dev_priv);
1123 
1124 	cur_state = val & DSI_PLL_VCO_EN;
1125 	I915_STATE_WARN(cur_state != state,
1126 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
1127 			onoff(state), onoff(cur_state));
1128 }
1129 
1130 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1131 			  enum pipe pipe, bool state)
1132 {
1133 	bool cur_state;
1134 
1135 	if (HAS_DDI(dev_priv)) {
1136 		/*
1137 		 * DDI does not have a specific FDI_TX register.
1138 		 *
1139 		 * FDI is never fed from EDP transcoder
1140 		 * so pipe->transcoder cast is fine here.
1141 		 */
1142 		enum transcoder cpu_transcoder = (enum transcoder)pipe;
1143 		u32 val = intel_de_read(dev_priv,
1144 					TRANS_DDI_FUNC_CTL(cpu_transcoder));
1145 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1146 	} else {
1147 		u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1148 		cur_state = !!(val & FDI_TX_ENABLE);
1149 	}
1150 	I915_STATE_WARN(cur_state != state,
1151 	     "FDI TX state assertion failure (expected %s, current %s)\n",
1152 			onoff(state), onoff(cur_state));
1153 }
1154 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1155 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1156 
1157 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1158 			  enum pipe pipe, bool state)
1159 {
1160 	u32 val;
1161 	bool cur_state;
1162 
1163 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1164 	cur_state = !!(val & FDI_RX_ENABLE);
1165 	I915_STATE_WARN(cur_state != state,
1166 	     "FDI RX state assertion failure (expected %s, current %s)\n",
1167 			onoff(state), onoff(cur_state));
1168 }
1169 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1170 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1171 
1172 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1173 				      enum pipe pipe)
1174 {
1175 	u32 val;
1176 
1177 	/* ILK FDI PLL is always enabled */
1178 	if (IS_GEN(dev_priv, 5))
1179 		return;
1180 
1181 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
1182 	if (HAS_DDI(dev_priv))
1183 		return;
1184 
1185 	val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1186 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1187 }
1188 
1189 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1190 		       enum pipe pipe, bool state)
1191 {
1192 	u32 val;
1193 	bool cur_state;
1194 
1195 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1196 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
1197 	I915_STATE_WARN(cur_state != state,
1198 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
1199 			onoff(state), onoff(cur_state));
1200 }
1201 
1202 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1203 {
1204 	i915_reg_t pp_reg;
1205 	u32 val;
1206 	enum pipe panel_pipe = INVALID_PIPE;
1207 	bool locked = true;
1208 
1209 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1210 		return;
1211 
1212 	if (HAS_PCH_SPLIT(dev_priv)) {
1213 		u32 port_sel;
1214 
1215 		pp_reg = PP_CONTROL(0);
1216 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1217 
1218 		switch (port_sel) {
1219 		case PANEL_PORT_SELECT_LVDS:
1220 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1221 			break;
1222 		case PANEL_PORT_SELECT_DPA:
1223 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1224 			break;
1225 		case PANEL_PORT_SELECT_DPC:
1226 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1227 			break;
1228 		case PANEL_PORT_SELECT_DPD:
1229 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1230 			break;
1231 		default:
1232 			MISSING_CASE(port_sel);
1233 			break;
1234 		}
1235 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1236 		/* presumably write lock depends on pipe, not port select */
1237 		pp_reg = PP_CONTROL(pipe);
1238 		panel_pipe = pipe;
1239 	} else {
1240 		u32 port_sel;
1241 
1242 		pp_reg = PP_CONTROL(0);
1243 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1244 
1245 		drm_WARN_ON(&dev_priv->drm,
1246 			    port_sel != PANEL_PORT_SELECT_LVDS);
1247 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1248 	}
1249 
1250 	val = intel_de_read(dev_priv, pp_reg);
1251 	if (!(val & PANEL_POWER_ON) ||
1252 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1253 		locked = false;
1254 
1255 	I915_STATE_WARN(panel_pipe == pipe && locked,
1256 	     "panel assertion failure, pipe %c regs locked\n",
1257 	     pipe_name(pipe));
1258 }
1259 
1260 void assert_pipe(struct drm_i915_private *dev_priv,
1261 		 enum transcoder cpu_transcoder, bool state)
1262 {
1263 	bool cur_state;
1264 	enum intel_display_power_domain power_domain;
1265 	intel_wakeref_t wakeref;
1266 
1267 	/* we keep both pipes enabled on 830 */
1268 	if (IS_I830(dev_priv))
1269 		state = true;
1270 
1271 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1272 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1273 	if (wakeref) {
1274 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1275 		cur_state = !!(val & PIPECONF_ENABLE);
1276 
1277 		intel_display_power_put(dev_priv, power_domain, wakeref);
1278 	} else {
1279 		cur_state = false;
1280 	}
1281 
1282 	I915_STATE_WARN(cur_state != state,
1283 			"transcoder %s assertion failure (expected %s, current %s)\n",
1284 			transcoder_name(cpu_transcoder),
1285 			onoff(state), onoff(cur_state));
1286 }
1287 
1288 static void assert_plane(struct intel_plane *plane, bool state)
1289 {
1290 	enum pipe pipe;
1291 	bool cur_state;
1292 
1293 	cur_state = plane->get_hw_state(plane, &pipe);
1294 
1295 	I915_STATE_WARN(cur_state != state,
1296 			"%s assertion failure (expected %s, current %s)\n",
1297 			plane->base.name, onoff(state), onoff(cur_state));
1298 }
1299 
1300 #define assert_plane_enabled(p) assert_plane(p, true)
1301 #define assert_plane_disabled(p) assert_plane(p, false)
1302 
1303 static void assert_planes_disabled(struct intel_crtc *crtc)
1304 {
1305 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1306 	struct intel_plane *plane;
1307 
1308 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1309 		assert_plane_disabled(plane);
1310 }
1311 
1312 static void assert_vblank_disabled(struct drm_crtc *crtc)
1313 {
1314 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1315 		drm_crtc_vblank_put(crtc);
1316 }
1317 
1318 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1319 				    enum pipe pipe)
1320 {
1321 	u32 val;
1322 	bool enabled;
1323 
1324 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
1325 	enabled = !!(val & TRANS_ENABLE);
1326 	I915_STATE_WARN(enabled,
1327 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
1328 	     pipe_name(pipe));
1329 }
1330 
1331 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1332 				   enum pipe pipe, enum port port,
1333 				   i915_reg_t dp_reg)
1334 {
1335 	enum pipe port_pipe;
1336 	bool state;
1337 
1338 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1339 
1340 	I915_STATE_WARN(state && port_pipe == pipe,
1341 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
1342 			port_name(port), pipe_name(pipe));
1343 
1344 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1345 			"IBX PCH DP %c still using transcoder B\n",
1346 			port_name(port));
1347 }
1348 
1349 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1350 				     enum pipe pipe, enum port port,
1351 				     i915_reg_t hdmi_reg)
1352 {
1353 	enum pipe port_pipe;
1354 	bool state;
1355 
1356 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1357 
1358 	I915_STATE_WARN(state && port_pipe == pipe,
1359 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1360 			port_name(port), pipe_name(pipe));
1361 
1362 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1363 			"IBX PCH HDMI %c still using transcoder B\n",
1364 			port_name(port));
1365 }
1366 
1367 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1368 				      enum pipe pipe)
1369 {
1370 	enum pipe port_pipe;
1371 
1372 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1373 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1374 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1375 
1376 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1377 			port_pipe == pipe,
1378 			"PCH VGA enabled on transcoder %c, should be disabled\n",
1379 			pipe_name(pipe));
1380 
1381 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1382 			port_pipe == pipe,
1383 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
1384 			pipe_name(pipe));
1385 
1386 	/* PCH SDVOB multiplex with HDMIB */
1387 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1388 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1389 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1390 }
1391 
1392 static void _vlv_enable_pll(struct intel_crtc *crtc,
1393 			    const struct intel_crtc_state *pipe_config)
1394 {
1395 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1396 	enum pipe pipe = crtc->pipe;
1397 
1398 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1399 	intel_de_posting_read(dev_priv, DPLL(pipe));
1400 	udelay(150);
1401 
1402 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1403 		drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1404 }
1405 
1406 static void vlv_enable_pll(struct intel_crtc *crtc,
1407 			   const struct intel_crtc_state *pipe_config)
1408 {
1409 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1410 	enum pipe pipe = crtc->pipe;
1411 
1412 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1413 
1414 	/* PLL is protected by panel, make sure we can write it */
1415 	assert_panel_unlocked(dev_priv, pipe);
1416 
1417 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1418 		_vlv_enable_pll(crtc, pipe_config);
1419 
1420 	intel_de_write(dev_priv, DPLL_MD(pipe),
1421 		       pipe_config->dpll_hw_state.dpll_md);
1422 	intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1423 }
1424 
1425 
1426 static void _chv_enable_pll(struct intel_crtc *crtc,
1427 			    const struct intel_crtc_state *pipe_config)
1428 {
1429 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1430 	enum pipe pipe = crtc->pipe;
1431 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1432 	u32 tmp;
1433 
1434 	vlv_dpio_get(dev_priv);
1435 
1436 	/* Enable back the 10bit clock to display controller */
1437 	tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1438 	tmp |= DPIO_DCLKP_EN;
1439 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1440 
1441 	vlv_dpio_put(dev_priv);
1442 
1443 	/*
1444 	 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1445 	 */
1446 	udelay(1);
1447 
1448 	/* Enable PLL */
1449 	intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1450 
1451 	/* Check PLL is locked */
1452 	if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1453 		drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1454 }
1455 
1456 static void chv_enable_pll(struct intel_crtc *crtc,
1457 			   const struct intel_crtc_state *pipe_config)
1458 {
1459 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1460 	enum pipe pipe = crtc->pipe;
1461 
1462 	assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1463 
1464 	/* PLL is protected by panel, make sure we can write it */
1465 	assert_panel_unlocked(dev_priv, pipe);
1466 
1467 	if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1468 		_chv_enable_pll(crtc, pipe_config);
1469 
1470 	if (pipe != PIPE_A) {
1471 		/*
1472 		 * WaPixelRepeatModeFixForC0:chv
1473 		 *
1474 		 * DPLLCMD is AWOL. Use chicken bits to propagate
1475 		 * the value from DPLLBMD to either pipe B or C.
1476 		 */
1477 		intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1478 		intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1479 			       pipe_config->dpll_hw_state.dpll_md);
1480 		intel_de_write(dev_priv, CBR4_VLV, 0);
1481 		dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1482 
1483 		/*
1484 		 * DPLLB VGA mode also seems to cause problems.
1485 		 * We should always have it disabled.
1486 		 */
1487 		drm_WARN_ON(&dev_priv->drm,
1488 			    (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1489 			     DPLL_VGA_MODE_DIS) == 0);
1490 	} else {
1491 		intel_de_write(dev_priv, DPLL_MD(pipe),
1492 			       pipe_config->dpll_hw_state.dpll_md);
1493 		intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1494 	}
1495 }
1496 
1497 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1498 {
1499 	if (IS_I830(dev_priv))
1500 		return false;
1501 
1502 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1503 }
1504 
1505 static void i9xx_enable_pll(struct intel_crtc *crtc,
1506 			    const struct intel_crtc_state *crtc_state)
1507 {
1508 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1509 	i915_reg_t reg = DPLL(crtc->pipe);
1510 	u32 dpll = crtc_state->dpll_hw_state.dpll;
1511 	int i;
1512 
1513 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1514 
1515 	/* PLL is protected by panel, make sure we can write it */
1516 	if (i9xx_has_pps(dev_priv))
1517 		assert_panel_unlocked(dev_priv, crtc->pipe);
1518 
1519 	/*
1520 	 * Apparently we need to have VGA mode enabled prior to changing
1521 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1522 	 * dividers, even though the register value does change.
1523 	 */
1524 	intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1525 	intel_de_write(dev_priv, reg, dpll);
1526 
1527 	/* Wait for the clocks to stabilize. */
1528 	intel_de_posting_read(dev_priv, reg);
1529 	udelay(150);
1530 
1531 	if (INTEL_GEN(dev_priv) >= 4) {
1532 		intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1533 			       crtc_state->dpll_hw_state.dpll_md);
1534 	} else {
1535 		/* The pixel multiplier can only be updated once the
1536 		 * DPLL is enabled and the clocks are stable.
1537 		 *
1538 		 * So write it again.
1539 		 */
1540 		intel_de_write(dev_priv, reg, dpll);
1541 	}
1542 
1543 	/* We do this three times for luck */
1544 	for (i = 0; i < 3; i++) {
1545 		intel_de_write(dev_priv, reg, dpll);
1546 		intel_de_posting_read(dev_priv, reg);
1547 		udelay(150); /* wait for warmup */
1548 	}
1549 }
1550 
1551 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1552 {
1553 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1554 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1555 	enum pipe pipe = crtc->pipe;
1556 
1557 	/* Don't disable pipe or pipe PLLs if needed */
1558 	if (IS_I830(dev_priv))
1559 		return;
1560 
1561 	/* Make sure the pipe isn't still relying on us */
1562 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1563 
1564 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1565 	intel_de_posting_read(dev_priv, DPLL(pipe));
1566 }
1567 
1568 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1569 {
1570 	u32 val;
1571 
1572 	/* Make sure the pipe isn't still relying on us */
1573 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1574 
1575 	val = DPLL_INTEGRATED_REF_CLK_VLV |
1576 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1577 	if (pipe != PIPE_A)
1578 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1579 
1580 	intel_de_write(dev_priv, DPLL(pipe), val);
1581 	intel_de_posting_read(dev_priv, DPLL(pipe));
1582 }
1583 
1584 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1585 {
1586 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
1587 	u32 val;
1588 
1589 	/* Make sure the pipe isn't still relying on us */
1590 	assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1591 
1592 	val = DPLL_SSC_REF_CLK_CHV |
1593 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1594 	if (pipe != PIPE_A)
1595 		val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1596 
1597 	intel_de_write(dev_priv, DPLL(pipe), val);
1598 	intel_de_posting_read(dev_priv, DPLL(pipe));
1599 
1600 	vlv_dpio_get(dev_priv);
1601 
1602 	/* Disable 10bit clock to display controller */
1603 	val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1604 	val &= ~DPIO_DCLKP_EN;
1605 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1606 
1607 	vlv_dpio_put(dev_priv);
1608 }
1609 
1610 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1611 			 struct intel_digital_port *dport,
1612 			 unsigned int expected_mask)
1613 {
1614 	u32 port_mask;
1615 	i915_reg_t dpll_reg;
1616 
1617 	switch (dport->base.port) {
1618 	case PORT_B:
1619 		port_mask = DPLL_PORTB_READY_MASK;
1620 		dpll_reg = DPLL(0);
1621 		break;
1622 	case PORT_C:
1623 		port_mask = DPLL_PORTC_READY_MASK;
1624 		dpll_reg = DPLL(0);
1625 		expected_mask <<= 4;
1626 		break;
1627 	case PORT_D:
1628 		port_mask = DPLL_PORTD_READY_MASK;
1629 		dpll_reg = DPIO_PHY_STATUS;
1630 		break;
1631 	default:
1632 		BUG();
1633 	}
1634 
1635 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
1636 				       port_mask, expected_mask, 1000))
1637 		drm_WARN(&dev_priv->drm, 1,
1638 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
1639 			 dport->base.base.base.id, dport->base.base.name,
1640 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
1641 			 expected_mask);
1642 }
1643 
1644 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1645 {
1646 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1647 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1648 	enum pipe pipe = crtc->pipe;
1649 	i915_reg_t reg;
1650 	u32 val, pipeconf_val;
1651 
1652 	/* Make sure PCH DPLL is enabled */
1653 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1654 
1655 	/* FDI must be feeding us bits for PCH ports */
1656 	assert_fdi_tx_enabled(dev_priv, pipe);
1657 	assert_fdi_rx_enabled(dev_priv, pipe);
1658 
1659 	if (HAS_PCH_CPT(dev_priv)) {
1660 		reg = TRANS_CHICKEN2(pipe);
1661 		val = intel_de_read(dev_priv, reg);
1662 		/*
1663 		 * Workaround: Set the timing override bit
1664 		 * before enabling the pch transcoder.
1665 		 */
1666 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1667 		/* Configure frame start delay to match the CPU */
1668 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1669 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1670 		intel_de_write(dev_priv, reg, val);
1671 	}
1672 
1673 	reg = PCH_TRANSCONF(pipe);
1674 	val = intel_de_read(dev_priv, reg);
1675 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
1676 
1677 	if (HAS_PCH_IBX(dev_priv)) {
1678 		/* Configure frame start delay to match the CPU */
1679 		val &= ~TRANS_FRAME_START_DELAY_MASK;
1680 		val |= TRANS_FRAME_START_DELAY(0);
1681 
1682 		/*
1683 		 * Make the BPC in transcoder be consistent with
1684 		 * that in pipeconf reg. For HDMI we must use 8bpc
1685 		 * here for both 8bpc and 12bpc.
1686 		 */
1687 		val &= ~PIPECONF_BPC_MASK;
1688 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1689 			val |= PIPECONF_8BPC;
1690 		else
1691 			val |= pipeconf_val & PIPECONF_BPC_MASK;
1692 	}
1693 
1694 	val &= ~TRANS_INTERLACE_MASK;
1695 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1696 		if (HAS_PCH_IBX(dev_priv) &&
1697 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1698 			val |= TRANS_LEGACY_INTERLACED_ILK;
1699 		else
1700 			val |= TRANS_INTERLACED;
1701 	} else {
1702 		val |= TRANS_PROGRESSIVE;
1703 	}
1704 
1705 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
1706 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1707 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1708 			pipe_name(pipe));
1709 }
1710 
1711 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1712 				      enum transcoder cpu_transcoder)
1713 {
1714 	u32 val, pipeconf_val;
1715 
1716 	/* FDI must be feeding us bits for PCH ports */
1717 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1718 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
1719 
1720 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1721 	/* Workaround: set timing override bit. */
1722 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1723 	/* Configure frame start delay to match the CPU */
1724 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1725 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1726 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1727 
1728 	val = TRANS_ENABLE;
1729 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1730 
1731 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1732 	    PIPECONF_INTERLACED_ILK)
1733 		val |= TRANS_INTERLACED;
1734 	else
1735 		val |= TRANS_PROGRESSIVE;
1736 
1737 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1738 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1739 				  TRANS_STATE_ENABLE, 100))
1740 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
1741 }
1742 
1743 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1744 				       enum pipe pipe)
1745 {
1746 	i915_reg_t reg;
1747 	u32 val;
1748 
1749 	/* FDI relies on the transcoder */
1750 	assert_fdi_tx_disabled(dev_priv, pipe);
1751 	assert_fdi_rx_disabled(dev_priv, pipe);
1752 
1753 	/* Ports must be off as well */
1754 	assert_pch_ports_disabled(dev_priv, pipe);
1755 
1756 	reg = PCH_TRANSCONF(pipe);
1757 	val = intel_de_read(dev_priv, reg);
1758 	val &= ~TRANS_ENABLE;
1759 	intel_de_write(dev_priv, reg, val);
1760 	/* wait for PCH transcoder off, transcoder state */
1761 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1762 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1763 			pipe_name(pipe));
1764 
1765 	if (HAS_PCH_CPT(dev_priv)) {
1766 		/* Workaround: Clear the timing override chicken bit again. */
1767 		reg = TRANS_CHICKEN2(pipe);
1768 		val = intel_de_read(dev_priv, reg);
1769 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1770 		intel_de_write(dev_priv, reg, val);
1771 	}
1772 }
1773 
1774 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1775 {
1776 	u32 val;
1777 
1778 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
1779 	val &= ~TRANS_ENABLE;
1780 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
1781 	/* wait for PCH transcoder off, transcoder state */
1782 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1783 				    TRANS_STATE_ENABLE, 50))
1784 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
1785 
1786 	/* Workaround: clear timing override bit. */
1787 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1788 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1789 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1790 }
1791 
1792 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1793 {
1794 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1795 
1796 	if (HAS_PCH_LPT(dev_priv))
1797 		return PIPE_A;
1798 	else
1799 		return crtc->pipe;
1800 }
1801 
1802 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1803 {
1804 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1805 
1806 	/*
1807 	 * On i965gm the hardware frame counter reads
1808 	 * zero when the TV encoder is enabled :(
1809 	 */
1810 	if (IS_I965GM(dev_priv) &&
1811 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1812 		return 0;
1813 
1814 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1815 		return 0xffffffff; /* full 32 bit counter */
1816 	else if (INTEL_GEN(dev_priv) >= 3)
1817 		return 0xffffff; /* only 24 bits of frame count */
1818 	else
1819 		return 0; /* Gen2 doesn't have a hardware frame counter */
1820 }
1821 
1822 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1823 {
1824 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1825 
1826 	assert_vblank_disabled(&crtc->base);
1827 	drm_crtc_set_max_vblank_count(&crtc->base,
1828 				      intel_crtc_max_vblank_count(crtc_state));
1829 	drm_crtc_vblank_on(&crtc->base);
1830 }
1831 
1832 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
1833 {
1834 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1835 
1836 	drm_crtc_vblank_off(&crtc->base);
1837 	assert_vblank_disabled(&crtc->base);
1838 }
1839 
1840 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1841 {
1842 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1843 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1844 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1845 	enum pipe pipe = crtc->pipe;
1846 	i915_reg_t reg;
1847 	u32 val;
1848 
1849 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
1850 
1851 	assert_planes_disabled(crtc);
1852 
1853 	/*
1854 	 * A pipe without a PLL won't actually be able to drive bits from
1855 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1856 	 * need the check.
1857 	 */
1858 	if (HAS_GMCH(dev_priv)) {
1859 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1860 			assert_dsi_pll_enabled(dev_priv);
1861 		else
1862 			assert_pll_enabled(dev_priv, pipe);
1863 	} else {
1864 		if (new_crtc_state->has_pch_encoder) {
1865 			/* if driving the PCH, we need FDI enabled */
1866 			assert_fdi_rx_pll_enabled(dev_priv,
1867 						  intel_crtc_pch_transcoder(crtc));
1868 			assert_fdi_tx_pll_enabled(dev_priv,
1869 						  (enum pipe) cpu_transcoder);
1870 		}
1871 		/* FIXME: assert CPU port conditions for SNB+ */
1872 	}
1873 
1874 	trace_intel_pipe_enable(crtc);
1875 
1876 	reg = PIPECONF(cpu_transcoder);
1877 	val = intel_de_read(dev_priv, reg);
1878 	if (val & PIPECONF_ENABLE) {
1879 		/* we keep both pipes enabled on 830 */
1880 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
1881 		return;
1882 	}
1883 
1884 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1885 	intel_de_posting_read(dev_priv, reg);
1886 
1887 	/*
1888 	 * Until the pipe starts PIPEDSL reads will return a stale value,
1889 	 * which causes an apparent vblank timestamp jump when PIPEDSL
1890 	 * resets to its proper value. That also messes up the frame count
1891 	 * when it's derived from the timestamps. So let's wait for the
1892 	 * pipe to start properly before we call drm_crtc_vblank_on()
1893 	 */
1894 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1895 		intel_wait_for_pipe_scanline_moving(crtc);
1896 }
1897 
1898 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1899 {
1900 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1901 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1902 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1903 	enum pipe pipe = crtc->pipe;
1904 	i915_reg_t reg;
1905 	u32 val;
1906 
1907 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1908 
1909 	/*
1910 	 * Make sure planes won't keep trying to pump pixels to us,
1911 	 * or we might hang the display.
1912 	 */
1913 	assert_planes_disabled(crtc);
1914 
1915 	trace_intel_pipe_disable(crtc);
1916 
1917 	reg = PIPECONF(cpu_transcoder);
1918 	val = intel_de_read(dev_priv, reg);
1919 	if ((val & PIPECONF_ENABLE) == 0)
1920 		return;
1921 
1922 	/*
1923 	 * Double wide has implications for planes
1924 	 * so best keep it disabled when not needed.
1925 	 */
1926 	if (old_crtc_state->double_wide)
1927 		val &= ~PIPECONF_DOUBLE_WIDE;
1928 
1929 	/* Don't disable pipe or pipe PLLs if needed */
1930 	if (!IS_I830(dev_priv))
1931 		val &= ~PIPECONF_ENABLE;
1932 
1933 	intel_de_write(dev_priv, reg, val);
1934 	if ((val & PIPECONF_ENABLE) == 0)
1935 		intel_wait_for_pipe_off(old_crtc_state);
1936 }
1937 
1938 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1939 {
1940 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1941 }
1942 
1943 static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1944 {
1945 	if (!is_ccs_modifier(fb->modifier))
1946 		return false;
1947 
1948 	return plane >= fb->format->num_planes / 2;
1949 }
1950 
1951 static bool is_gen12_ccs_modifier(u64 modifier)
1952 {
1953 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1954 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1955 
1956 }
1957 
1958 static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1959 {
1960 	return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1961 }
1962 
1963 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1964 {
1965 	if (is_ccs_modifier(fb->modifier))
1966 		return is_ccs_plane(fb, plane);
1967 
1968 	return plane == 1;
1969 }
1970 
1971 static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1972 {
1973 	WARN_ON(!is_ccs_modifier(fb->modifier) ||
1974 		(main_plane && main_plane >= fb->format->num_planes / 2));
1975 
1976 	return fb->format->num_planes / 2 + main_plane;
1977 }
1978 
1979 static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1980 {
1981 	WARN_ON(!is_ccs_modifier(fb->modifier) ||
1982 		ccs_plane < fb->format->num_planes / 2);
1983 
1984 	return ccs_plane - fb->format->num_planes / 2;
1985 }
1986 
1987 /* Return either the main plane's CCS or - if not a CCS FB - UV plane */
1988 int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
1989 {
1990 	if (is_ccs_modifier(fb->modifier))
1991 		return main_to_ccs_plane(fb, main_plane);
1992 
1993 	return 1;
1994 }
1995 
1996 bool
1997 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
1998 				    uint64_t modifier)
1999 {
2000 	return info->is_yuv &&
2001 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2002 }
2003 
2004 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2005 				   int color_plane)
2006 {
2007 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2008 	       color_plane == 1;
2009 }
2010 
2011 static unsigned int
2012 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
2013 {
2014 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2015 	unsigned int cpp = fb->format->cpp[color_plane];
2016 
2017 	switch (fb->modifier) {
2018 	case DRM_FORMAT_MOD_LINEAR:
2019 		return intel_tile_size(dev_priv);
2020 	case I915_FORMAT_MOD_X_TILED:
2021 		if (IS_GEN(dev_priv, 2))
2022 			return 128;
2023 		else
2024 			return 512;
2025 	case I915_FORMAT_MOD_Y_TILED_CCS:
2026 		if (is_ccs_plane(fb, color_plane))
2027 			return 128;
2028 		/* fall through */
2029 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2030 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2031 		if (is_ccs_plane(fb, color_plane))
2032 			return 64;
2033 		/* fall through */
2034 	case I915_FORMAT_MOD_Y_TILED:
2035 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
2036 			return 128;
2037 		else
2038 			return 512;
2039 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2040 		if (is_ccs_plane(fb, color_plane))
2041 			return 128;
2042 		/* fall through */
2043 	case I915_FORMAT_MOD_Yf_TILED:
2044 		switch (cpp) {
2045 		case 1:
2046 			return 64;
2047 		case 2:
2048 		case 4:
2049 			return 128;
2050 		case 8:
2051 		case 16:
2052 			return 256;
2053 		default:
2054 			MISSING_CASE(cpp);
2055 			return cpp;
2056 		}
2057 		break;
2058 	default:
2059 		MISSING_CASE(fb->modifier);
2060 		return cpp;
2061 	}
2062 }
2063 
2064 static unsigned int
2065 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
2066 {
2067 	if (is_gen12_ccs_plane(fb, color_plane))
2068 		return 1;
2069 
2070 	return intel_tile_size(to_i915(fb->dev)) /
2071 		intel_tile_width_bytes(fb, color_plane);
2072 }
2073 
2074 /* Return the tile dimensions in pixel units */
2075 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
2076 			    unsigned int *tile_width,
2077 			    unsigned int *tile_height)
2078 {
2079 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2080 	unsigned int cpp = fb->format->cpp[color_plane];
2081 
2082 	*tile_width = tile_width_bytes / cpp;
2083 	*tile_height = intel_tile_height(fb, color_plane);
2084 }
2085 
2086 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2087 					int color_plane)
2088 {
2089 	unsigned int tile_width, tile_height;
2090 
2091 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2092 
2093 	return fb->pitches[color_plane] * tile_height;
2094 }
2095 
2096 unsigned int
2097 intel_fb_align_height(const struct drm_framebuffer *fb,
2098 		      int color_plane, unsigned int height)
2099 {
2100 	unsigned int tile_height = intel_tile_height(fb, color_plane);
2101 
2102 	return ALIGN(height, tile_height);
2103 }
2104 
2105 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2106 {
2107 	unsigned int size = 0;
2108 	int i;
2109 
2110 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2111 		size += rot_info->plane[i].width * rot_info->plane[i].height;
2112 
2113 	return size;
2114 }
2115 
2116 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2117 {
2118 	unsigned int size = 0;
2119 	int i;
2120 
2121 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2122 		size += rem_info->plane[i].width * rem_info->plane[i].height;
2123 
2124 	return size;
2125 }
2126 
2127 static void
2128 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2129 			const struct drm_framebuffer *fb,
2130 			unsigned int rotation)
2131 {
2132 	view->type = I915_GGTT_VIEW_NORMAL;
2133 	if (drm_rotation_90_or_270(rotation)) {
2134 		view->type = I915_GGTT_VIEW_ROTATED;
2135 		view->rotated = to_intel_framebuffer(fb)->rot_info;
2136 	}
2137 }
2138 
2139 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2140 {
2141 	if (IS_I830(dev_priv))
2142 		return 16 * 1024;
2143 	else if (IS_I85X(dev_priv))
2144 		return 256;
2145 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2146 		return 32;
2147 	else
2148 		return 4 * 1024;
2149 }
2150 
2151 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2152 {
2153 	if (INTEL_GEN(dev_priv) >= 9)
2154 		return 256 * 1024;
2155 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2156 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2157 		return 128 * 1024;
2158 	else if (INTEL_GEN(dev_priv) >= 4)
2159 		return 4 * 1024;
2160 	else
2161 		return 0;
2162 }
2163 
2164 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2165 					 int color_plane)
2166 {
2167 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2168 
2169 	/* AUX_DIST needs only 4K alignment */
2170 	if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2171 	    is_ccs_plane(fb, color_plane))
2172 		return 4096;
2173 
2174 	switch (fb->modifier) {
2175 	case DRM_FORMAT_MOD_LINEAR:
2176 		return intel_linear_alignment(dev_priv);
2177 	case I915_FORMAT_MOD_X_TILED:
2178 		if (INTEL_GEN(dev_priv) >= 9)
2179 			return 256 * 1024;
2180 		return 0;
2181 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2182 		if (is_semiplanar_uv_plane(fb, color_plane))
2183 			return intel_tile_row_size(fb, color_plane);
2184 		/* Fall-through */
2185 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2186 		return 16 * 1024;
2187 	case I915_FORMAT_MOD_Y_TILED_CCS:
2188 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2189 	case I915_FORMAT_MOD_Y_TILED:
2190 		if (INTEL_GEN(dev_priv) >= 12 &&
2191 		    is_semiplanar_uv_plane(fb, color_plane))
2192 			return intel_tile_row_size(fb, color_plane);
2193 		/* Fall-through */
2194 	case I915_FORMAT_MOD_Yf_TILED:
2195 		return 1 * 1024 * 1024;
2196 	default:
2197 		MISSING_CASE(fb->modifier);
2198 		return 0;
2199 	}
2200 }
2201 
2202 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2203 {
2204 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2205 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2206 
2207 	return INTEL_GEN(dev_priv) < 4 ||
2208 		(plane->has_fbc &&
2209 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2210 }
2211 
2212 struct i915_vma *
2213 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2214 			   const struct i915_ggtt_view *view,
2215 			   bool uses_fence,
2216 			   unsigned long *out_flags)
2217 {
2218 	struct drm_device *dev = fb->dev;
2219 	struct drm_i915_private *dev_priv = to_i915(dev);
2220 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2221 	intel_wakeref_t wakeref;
2222 	struct i915_vma *vma;
2223 	unsigned int pinctl;
2224 	u32 alignment;
2225 
2226 	if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
2227 		return ERR_PTR(-EINVAL);
2228 
2229 	alignment = intel_surf_alignment(fb, 0);
2230 	if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
2231 		return ERR_PTR(-EINVAL);
2232 
2233 	/* Note that the w/a also requires 64 PTE of padding following the
2234 	 * bo. We currently fill all unused PTE with the shadow page and so
2235 	 * we should always have valid PTE following the scanout preventing
2236 	 * the VT-d warning.
2237 	 */
2238 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2239 		alignment = 256 * 1024;
2240 
2241 	/*
2242 	 * Global gtt pte registers are special registers which actually forward
2243 	 * writes to a chunk of system memory. Which means that there is no risk
2244 	 * that the register values disappear as soon as we call
2245 	 * intel_runtime_pm_put(), so it is correct to wrap only the
2246 	 * pin/unpin/fence and not more.
2247 	 */
2248 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2249 
2250 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2251 
2252 	/*
2253 	 * Valleyview is definitely limited to scanning out the first
2254 	 * 512MiB. Lets presume this behaviour was inherited from the
2255 	 * g4x display engine and that all earlier gen are similarly
2256 	 * limited. Testing suggests that it is a little more
2257 	 * complicated than this. For example, Cherryview appears quite
2258 	 * happy to scanout from anywhere within its global aperture.
2259 	 */
2260 	pinctl = 0;
2261 	if (HAS_GMCH(dev_priv))
2262 		pinctl |= PIN_MAPPABLE;
2263 
2264 	vma = i915_gem_object_pin_to_display_plane(obj,
2265 						   alignment, view, pinctl);
2266 	if (IS_ERR(vma))
2267 		goto err;
2268 
2269 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2270 		int ret;
2271 
2272 		/*
2273 		 * Install a fence for tiled scan-out. Pre-i965 always needs a
2274 		 * fence, whereas 965+ only requires a fence if using
2275 		 * framebuffer compression.  For simplicity, we always, when
2276 		 * possible, install a fence as the cost is not that onerous.
2277 		 *
2278 		 * If we fail to fence the tiled scanout, then either the
2279 		 * modeset will reject the change (which is highly unlikely as
2280 		 * the affected systems, all but one, do not have unmappable
2281 		 * space) or we will not be able to enable full powersaving
2282 		 * techniques (also likely not to apply due to various limits
2283 		 * FBC and the like impose on the size of the buffer, which
2284 		 * presumably we violated anyway with this unmappable buffer).
2285 		 * Anyway, it is presumably better to stumble onwards with
2286 		 * something and try to run the system in a "less than optimal"
2287 		 * mode that matches the user configuration.
2288 		 */
2289 		ret = i915_vma_pin_fence(vma);
2290 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2291 			i915_gem_object_unpin_from_display_plane(vma);
2292 			vma = ERR_PTR(ret);
2293 			goto err;
2294 		}
2295 
2296 		if (ret == 0 && vma->fence)
2297 			*out_flags |= PLANE_HAS_FENCE;
2298 	}
2299 
2300 	i915_vma_get(vma);
2301 err:
2302 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2303 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2304 	return vma;
2305 }
2306 
2307 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2308 {
2309 	i915_gem_object_lock(vma->obj);
2310 	if (flags & PLANE_HAS_FENCE)
2311 		i915_vma_unpin_fence(vma);
2312 	i915_gem_object_unpin_from_display_plane(vma);
2313 	i915_gem_object_unlock(vma->obj);
2314 
2315 	i915_vma_put(vma);
2316 }
2317 
2318 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2319 			  unsigned int rotation)
2320 {
2321 	if (drm_rotation_90_or_270(rotation))
2322 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2323 	else
2324 		return fb->pitches[color_plane];
2325 }
2326 
2327 /*
2328  * Convert the x/y offsets into a linear offset.
2329  * Only valid with 0/180 degree rotation, which is fine since linear
2330  * offset is only used with linear buffers on pre-hsw and tiled buffers
2331  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2332  */
2333 u32 intel_fb_xy_to_linear(int x, int y,
2334 			  const struct intel_plane_state *state,
2335 			  int color_plane)
2336 {
2337 	const struct drm_framebuffer *fb = state->hw.fb;
2338 	unsigned int cpp = fb->format->cpp[color_plane];
2339 	unsigned int pitch = state->color_plane[color_plane].stride;
2340 
2341 	return y * pitch + x * cpp;
2342 }
2343 
2344 /*
2345  * Add the x/y offsets derived from fb->offsets[] to the user
2346  * specified plane src x/y offsets. The resulting x/y offsets
2347  * specify the start of scanout from the beginning of the gtt mapping.
2348  */
2349 void intel_add_fb_offsets(int *x, int *y,
2350 			  const struct intel_plane_state *state,
2351 			  int color_plane)
2352 
2353 {
2354 	*x += state->color_plane[color_plane].x;
2355 	*y += state->color_plane[color_plane].y;
2356 }
2357 
2358 static u32 intel_adjust_tile_offset(int *x, int *y,
2359 				    unsigned int tile_width,
2360 				    unsigned int tile_height,
2361 				    unsigned int tile_size,
2362 				    unsigned int pitch_tiles,
2363 				    u32 old_offset,
2364 				    u32 new_offset)
2365 {
2366 	unsigned int pitch_pixels = pitch_tiles * tile_width;
2367 	unsigned int tiles;
2368 
2369 	WARN_ON(old_offset & (tile_size - 1));
2370 	WARN_ON(new_offset & (tile_size - 1));
2371 	WARN_ON(new_offset > old_offset);
2372 
2373 	tiles = (old_offset - new_offset) / tile_size;
2374 
2375 	*y += tiles / pitch_tiles * tile_height;
2376 	*x += tiles % pitch_tiles * tile_width;
2377 
2378 	/* minimize x in case it got needlessly big */
2379 	*y += *x / pitch_pixels * tile_height;
2380 	*x %= pitch_pixels;
2381 
2382 	return new_offset;
2383 }
2384 
2385 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2386 {
2387 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2388 	       is_gen12_ccs_plane(fb, color_plane);
2389 }
2390 
2391 static u32 intel_adjust_aligned_offset(int *x, int *y,
2392 				       const struct drm_framebuffer *fb,
2393 				       int color_plane,
2394 				       unsigned int rotation,
2395 				       unsigned int pitch,
2396 				       u32 old_offset, u32 new_offset)
2397 {
2398 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2399 	unsigned int cpp = fb->format->cpp[color_plane];
2400 
2401 	drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
2402 
2403 	if (!is_surface_linear(fb, color_plane)) {
2404 		unsigned int tile_size, tile_width, tile_height;
2405 		unsigned int pitch_tiles;
2406 
2407 		tile_size = intel_tile_size(dev_priv);
2408 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2409 
2410 		if (drm_rotation_90_or_270(rotation)) {
2411 			pitch_tiles = pitch / tile_height;
2412 			swap(tile_width, tile_height);
2413 		} else {
2414 			pitch_tiles = pitch / (tile_width * cpp);
2415 		}
2416 
2417 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2418 					 tile_size, pitch_tiles,
2419 					 old_offset, new_offset);
2420 	} else {
2421 		old_offset += *y * pitch + *x * cpp;
2422 
2423 		*y = (old_offset - new_offset) / pitch;
2424 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
2425 	}
2426 
2427 	return new_offset;
2428 }
2429 
2430 /*
2431  * Adjust the tile offset by moving the difference into
2432  * the x/y offsets.
2433  */
2434 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2435 					     const struct intel_plane_state *state,
2436 					     int color_plane,
2437 					     u32 old_offset, u32 new_offset)
2438 {
2439 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2440 					   state->hw.rotation,
2441 					   state->color_plane[color_plane].stride,
2442 					   old_offset, new_offset);
2443 }
2444 
2445 /*
2446  * Computes the aligned offset to the base tile and adjusts
2447  * x, y. bytes per pixel is assumed to be a power-of-two.
2448  *
2449  * In the 90/270 rotated case, x and y are assumed
2450  * to be already rotated to match the rotated GTT view, and
2451  * pitch is the tile_height aligned framebuffer height.
2452  *
2453  * This function is used when computing the derived information
2454  * under intel_framebuffer, so using any of that information
2455  * here is not allowed. Anything under drm_framebuffer can be
2456  * used. This is why the user has to pass in the pitch since it
2457  * is specified in the rotated orientation.
2458  */
2459 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2460 					int *x, int *y,
2461 					const struct drm_framebuffer *fb,
2462 					int color_plane,
2463 					unsigned int pitch,
2464 					unsigned int rotation,
2465 					u32 alignment)
2466 {
2467 	unsigned int cpp = fb->format->cpp[color_plane];
2468 	u32 offset, offset_aligned;
2469 
2470 	if (!is_surface_linear(fb, color_plane)) {
2471 		unsigned int tile_size, tile_width, tile_height;
2472 		unsigned int tile_rows, tiles, pitch_tiles;
2473 
2474 		tile_size = intel_tile_size(dev_priv);
2475 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2476 
2477 		if (drm_rotation_90_or_270(rotation)) {
2478 			pitch_tiles = pitch / tile_height;
2479 			swap(tile_width, tile_height);
2480 		} else {
2481 			pitch_tiles = pitch / (tile_width * cpp);
2482 		}
2483 
2484 		tile_rows = *y / tile_height;
2485 		*y %= tile_height;
2486 
2487 		tiles = *x / tile_width;
2488 		*x %= tile_width;
2489 
2490 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2491 
2492 		offset_aligned = offset;
2493 		if (alignment)
2494 			offset_aligned = rounddown(offset_aligned, alignment);
2495 
2496 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
2497 					 tile_size, pitch_tiles,
2498 					 offset, offset_aligned);
2499 	} else {
2500 		offset = *y * pitch + *x * cpp;
2501 		offset_aligned = offset;
2502 		if (alignment) {
2503 			offset_aligned = rounddown(offset_aligned, alignment);
2504 			*y = (offset % alignment) / pitch;
2505 			*x = ((offset % alignment) - *y * pitch) / cpp;
2506 		} else {
2507 			*y = *x = 0;
2508 		}
2509 	}
2510 
2511 	return offset_aligned;
2512 }
2513 
2514 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2515 					      const struct intel_plane_state *state,
2516 					      int color_plane)
2517 {
2518 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
2519 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2520 	const struct drm_framebuffer *fb = state->hw.fb;
2521 	unsigned int rotation = state->hw.rotation;
2522 	int pitch = state->color_plane[color_plane].stride;
2523 	u32 alignment;
2524 
2525 	if (intel_plane->id == PLANE_CURSOR)
2526 		alignment = intel_cursor_alignment(dev_priv);
2527 	else
2528 		alignment = intel_surf_alignment(fb, color_plane);
2529 
2530 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2531 					    pitch, rotation, alignment);
2532 }
2533 
2534 /* Convert the fb->offset[] into x/y offsets */
2535 static int intel_fb_offset_to_xy(int *x, int *y,
2536 				 const struct drm_framebuffer *fb,
2537 				 int color_plane)
2538 {
2539 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2540 	unsigned int height;
2541 	u32 alignment;
2542 
2543 	if (INTEL_GEN(dev_priv) >= 12 &&
2544 	    is_semiplanar_uv_plane(fb, color_plane))
2545 		alignment = intel_tile_row_size(fb, color_plane);
2546 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2547 		alignment = intel_tile_size(dev_priv);
2548 	else
2549 		alignment = 0;
2550 
2551 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
2552 		drm_dbg_kms(&dev_priv->drm,
2553 			    "Misaligned offset 0x%08x for color plane %d\n",
2554 			    fb->offsets[color_plane], color_plane);
2555 		return -EINVAL;
2556 	}
2557 
2558 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2559 	height = ALIGN(height, intel_tile_height(fb, color_plane));
2560 
2561 	/* Catch potential overflows early */
2562 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2563 			    fb->offsets[color_plane])) {
2564 		drm_dbg_kms(&dev_priv->drm,
2565 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
2566 			    fb->offsets[color_plane], fb->pitches[color_plane],
2567 			    color_plane);
2568 		return -ERANGE;
2569 	}
2570 
2571 	*x = 0;
2572 	*y = 0;
2573 
2574 	intel_adjust_aligned_offset(x, y,
2575 				    fb, color_plane, DRM_MODE_ROTATE_0,
2576 				    fb->pitches[color_plane],
2577 				    fb->offsets[color_plane], 0);
2578 
2579 	return 0;
2580 }
2581 
2582 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2583 {
2584 	switch (fb_modifier) {
2585 	case I915_FORMAT_MOD_X_TILED:
2586 		return I915_TILING_X;
2587 	case I915_FORMAT_MOD_Y_TILED:
2588 	case I915_FORMAT_MOD_Y_TILED_CCS:
2589 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2590 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2591 		return I915_TILING_Y;
2592 	default:
2593 		return I915_TILING_NONE;
2594 	}
2595 }
2596 
2597 /*
2598  * From the Sky Lake PRM:
2599  * "The Color Control Surface (CCS) contains the compression status of
2600  *  the cache-line pairs. The compression state of the cache-line pair
2601  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2602  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2603  *  cache-line-pairs. CCS is always Y tiled."
2604  *
2605  * Since cache line pairs refers to horizontally adjacent cache lines,
2606  * each cache line in the CCS corresponds to an area of 32x16 cache
2607  * lines on the main surface. Since each pixel is 4 bytes, this gives
2608  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2609  * main surface.
2610  */
2611 static const struct drm_format_info skl_ccs_formats[] = {
2612 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2613 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2614 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2615 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2616 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2617 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2618 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2619 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2620 };
2621 
2622 /*
2623  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2624  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2625  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2626  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2627  * the main surface.
2628  */
2629 static const struct drm_format_info gen12_ccs_formats[] = {
2630 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2631 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2632 	  .hsub = 1, .vsub = 1, },
2633 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2634 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2635 	  .hsub = 1, .vsub = 1, },
2636 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2637 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2638 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2639 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2640 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2641 	  .hsub = 1, .vsub = 1, .has_alpha = true },
2642 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
2643 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2644 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2645 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
2646 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2647 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2648 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
2649 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2650 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2651 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
2652 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2653 	  .hsub = 2, .vsub = 1, .is_yuv = true },
2654 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
2655 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2656 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2657 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
2658 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2659 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2660 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
2661 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2662 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2663 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
2664 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2665 	  .hsub = 2, .vsub = 2, .is_yuv = true },
2666 };
2667 
2668 static const struct drm_format_info *
2669 lookup_format_info(const struct drm_format_info formats[],
2670 		   int num_formats, u32 format)
2671 {
2672 	int i;
2673 
2674 	for (i = 0; i < num_formats; i++) {
2675 		if (formats[i].format == format)
2676 			return &formats[i];
2677 	}
2678 
2679 	return NULL;
2680 }
2681 
2682 static const struct drm_format_info *
2683 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2684 {
2685 	switch (cmd->modifier[0]) {
2686 	case I915_FORMAT_MOD_Y_TILED_CCS:
2687 	case I915_FORMAT_MOD_Yf_TILED_CCS:
2688 		return lookup_format_info(skl_ccs_formats,
2689 					  ARRAY_SIZE(skl_ccs_formats),
2690 					  cmd->pixel_format);
2691 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2692 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2693 		return lookup_format_info(gen12_ccs_formats,
2694 					  ARRAY_SIZE(gen12_ccs_formats),
2695 					  cmd->pixel_format);
2696 	default:
2697 		return NULL;
2698 	}
2699 }
2700 
2701 bool is_ccs_modifier(u64 modifier)
2702 {
2703 	return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2704 	       modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
2705 	       modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2706 	       modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2707 }
2708 
2709 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2710 {
2711 	return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2712 			    512) * 64;
2713 }
2714 
2715 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2716 			      u32 pixel_format, u64 modifier)
2717 {
2718 	struct intel_crtc *crtc;
2719 	struct intel_plane *plane;
2720 
2721 	/*
2722 	 * We assume the primary plane for pipe A has
2723 	 * the highest stride limits of them all.
2724 	 */
2725 	crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
2726 	if (!crtc)
2727 		return 0;
2728 
2729 	plane = to_intel_plane(crtc->base.primary);
2730 
2731 	return plane->max_stride(plane, pixel_format, modifier,
2732 				 DRM_MODE_ROTATE_0);
2733 }
2734 
2735 static
2736 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2737 			u32 pixel_format, u64 modifier)
2738 {
2739 	/*
2740 	 * Arbitrary limit for gen4+ chosen to match the
2741 	 * render engine max stride.
2742 	 *
2743 	 * The new CCS hash mode makes remapping impossible
2744 	 */
2745 	if (!is_ccs_modifier(modifier)) {
2746 		if (INTEL_GEN(dev_priv) >= 7)
2747 			return 256*1024;
2748 		else if (INTEL_GEN(dev_priv) >= 4)
2749 			return 128*1024;
2750 	}
2751 
2752 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2753 }
2754 
2755 static u32
2756 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2757 {
2758 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
2759 	u32 tile_width;
2760 
2761 	if (is_surface_linear(fb, color_plane)) {
2762 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2763 							   fb->format->format,
2764 							   fb->modifier);
2765 
2766 		/*
2767 		 * To make remapping with linear generally feasible
2768 		 * we need the stride to be page aligned.
2769 		 */
2770 		if (fb->pitches[color_plane] > max_stride &&
2771 		    !is_ccs_modifier(fb->modifier))
2772 			return intel_tile_size(dev_priv);
2773 		else
2774 			return 64;
2775 	}
2776 
2777 	tile_width = intel_tile_width_bytes(fb, color_plane);
2778 	if (is_ccs_modifier(fb->modifier)) {
2779 		/*
2780 		 * Display WA #0531: skl,bxt,kbl,glk
2781 		 *
2782 		 * Render decompression and plane width > 3840
2783 		 * combined with horizontal panning requires the
2784 		 * plane stride to be a multiple of 4. We'll just
2785 		 * require the entire fb to accommodate that to avoid
2786 		 * potential runtime errors at plane configuration time.
2787 		 */
2788 		if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
2789 			tile_width *= 4;
2790 		/*
2791 		 * The main surface pitch must be padded to a multiple of four
2792 		 * tile widths.
2793 		 */
2794 		else if (INTEL_GEN(dev_priv) >= 12)
2795 			tile_width *= 4;
2796 	}
2797 	return tile_width;
2798 }
2799 
2800 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2801 {
2802 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2803 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2804 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2805 	int i;
2806 
2807 	/* We don't want to deal with remapping with cursors */
2808 	if (plane->id == PLANE_CURSOR)
2809 		return false;
2810 
2811 	/*
2812 	 * The display engine limits already match/exceed the
2813 	 * render engine limits, so not much point in remapping.
2814 	 * Would also need to deal with the fence POT alignment
2815 	 * and gen2 2KiB GTT tile size.
2816 	 */
2817 	if (INTEL_GEN(dev_priv) < 4)
2818 		return false;
2819 
2820 	/*
2821 	 * The new CCS hash mode isn't compatible with remapping as
2822 	 * the virtual address of the pages affects the compressed data.
2823 	 */
2824 	if (is_ccs_modifier(fb->modifier))
2825 		return false;
2826 
2827 	/* Linear needs a page aligned stride for remapping */
2828 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2829 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
2830 
2831 		for (i = 0; i < fb->format->num_planes; i++) {
2832 			if (fb->pitches[i] & alignment)
2833 				return false;
2834 		}
2835 	}
2836 
2837 	return true;
2838 }
2839 
2840 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2841 {
2842 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2843 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2844 	unsigned int rotation = plane_state->hw.rotation;
2845 	u32 stride, max_stride;
2846 
2847 	/*
2848 	 * No remapping for invisible planes since we don't have
2849 	 * an actual source viewport to remap.
2850 	 */
2851 	if (!plane_state->uapi.visible)
2852 		return false;
2853 
2854 	if (!intel_plane_can_remap(plane_state))
2855 		return false;
2856 
2857 	/*
2858 	 * FIXME: aux plane limits on gen9+ are
2859 	 * unclear in Bspec, for now no checking.
2860 	 */
2861 	stride = intel_fb_pitch(fb, 0, rotation);
2862 	max_stride = plane->max_stride(plane, fb->format->format,
2863 				       fb->modifier, rotation);
2864 
2865 	return stride > max_stride;
2866 }
2867 
2868 static void
2869 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2870 			       const struct drm_framebuffer *fb,
2871 			       int color_plane)
2872 {
2873 	int main_plane;
2874 
2875 	if (color_plane == 0) {
2876 		*hsub = 1;
2877 		*vsub = 1;
2878 
2879 		return;
2880 	}
2881 
2882 	/*
2883 	 * TODO: Deduct the subsampling from the char block for all CCS
2884 	 * formats and planes.
2885 	 */
2886 	if (!is_gen12_ccs_plane(fb, color_plane)) {
2887 		*hsub = fb->format->hsub;
2888 		*vsub = fb->format->vsub;
2889 
2890 		return;
2891 	}
2892 
2893 	main_plane = ccs_to_main_plane(fb, color_plane);
2894 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
2895 		drm_format_info_block_width(fb->format, main_plane);
2896 
2897 	/*
2898 	 * The min stride check in the core framebuffer_check() function
2899 	 * assumes that format->hsub applies to every plane except for the
2900 	 * first plane. That's incorrect for the CCS AUX plane of the first
2901 	 * plane, but for the above check to pass we must define the block
2902 	 * width with that subsampling applied to it. Adjust the width here
2903 	 * accordingly, so we can calculate the actual subsampling factor.
2904 	 */
2905 	if (main_plane == 0)
2906 		*hsub *= fb->format->hsub;
2907 
2908 	*vsub = 32;
2909 }
2910 static int
2911 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2912 {
2913 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2914 	int main_plane;
2915 	int hsub, vsub;
2916 	int tile_width, tile_height;
2917 	int ccs_x, ccs_y;
2918 	int main_x, main_y;
2919 
2920 	if (!is_ccs_plane(fb, ccs_plane))
2921 		return 0;
2922 
2923 	intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2924 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2925 
2926 	tile_width *= hsub;
2927 	tile_height *= vsub;
2928 
2929 	ccs_x = (x * hsub) % tile_width;
2930 	ccs_y = (y * vsub) % tile_height;
2931 
2932 	main_plane = ccs_to_main_plane(fb, ccs_plane);
2933 	main_x = intel_fb->normal[main_plane].x % tile_width;
2934 	main_y = intel_fb->normal[main_plane].y % tile_height;
2935 
2936 	/*
2937 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
2938 	 * x/y offsets must match between CCS and the main surface.
2939 	 */
2940 	if (main_x != ccs_x || main_y != ccs_y) {
2941 		DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2942 			      main_x, main_y,
2943 			      ccs_x, ccs_y,
2944 			      intel_fb->normal[main_plane].x,
2945 			      intel_fb->normal[main_plane].y,
2946 			      x, y);
2947 		return -EINVAL;
2948 	}
2949 
2950 	return 0;
2951 }
2952 
2953 static void
2954 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2955 {
2956 	int main_plane = is_ccs_plane(fb, color_plane) ?
2957 			 ccs_to_main_plane(fb, color_plane) : 0;
2958 	int main_hsub, main_vsub;
2959 	int hsub, vsub;
2960 
2961 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
2962 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
2963 	*w = fb->width / main_hsub / hsub;
2964 	*h = fb->height / main_vsub / vsub;
2965 }
2966 
2967 /*
2968  * Setup the rotated view for an FB plane and return the size the GTT mapping
2969  * requires for this view.
2970  */
2971 static u32
2972 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2973 		  u32 gtt_offset_rotated, int x, int y,
2974 		  unsigned int width, unsigned int height,
2975 		  unsigned int tile_size,
2976 		  unsigned int tile_width, unsigned int tile_height,
2977 		  struct drm_framebuffer *fb)
2978 {
2979 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2980 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2981 	unsigned int pitch_tiles;
2982 	struct drm_rect r;
2983 
2984 	/* Y or Yf modifiers required for 90/270 rotation */
2985 	if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
2986 	    fb->modifier != I915_FORMAT_MOD_Yf_TILED)
2987 		return 0;
2988 
2989 	if (WARN_ON(plane >= ARRAY_SIZE(rot_info->plane)))
2990 		return 0;
2991 
2992 	rot_info->plane[plane] = *plane_info;
2993 
2994 	intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
2995 
2996 	/* rotate the x/y offsets to match the GTT view */
2997 	drm_rect_init(&r, x, y, width, height);
2998 	drm_rect_rotate(&r,
2999 			plane_info->width * tile_width,
3000 			plane_info->height * tile_height,
3001 			DRM_MODE_ROTATE_270);
3002 	x = r.x1;
3003 	y = r.y1;
3004 
3005 	/* rotate the tile dimensions to match the GTT view */
3006 	pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3007 	swap(tile_width, tile_height);
3008 
3009 	/*
3010 	 * We only keep the x/y offsets, so push all of the
3011 	 * gtt offset into the x/y offsets.
3012 	 */
3013 	intel_adjust_tile_offset(&x, &y,
3014 				 tile_width, tile_height,
3015 				 tile_size, pitch_tiles,
3016 				 gtt_offset_rotated * tile_size, 0);
3017 
3018 	/*
3019 	 * First pixel of the framebuffer from
3020 	 * the start of the rotated gtt mapping.
3021 	 */
3022 	intel_fb->rotated[plane].x = x;
3023 	intel_fb->rotated[plane].y = y;
3024 
3025 	return plane_info->width * plane_info->height;
3026 }
3027 
3028 static int
3029 intel_fill_fb_info(struct drm_i915_private *dev_priv,
3030 		   struct drm_framebuffer *fb)
3031 {
3032 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3033 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3034 	u32 gtt_offset_rotated = 0;
3035 	unsigned int max_size = 0;
3036 	int i, num_planes = fb->format->num_planes;
3037 	unsigned int tile_size = intel_tile_size(dev_priv);
3038 
3039 	for (i = 0; i < num_planes; i++) {
3040 		unsigned int width, height;
3041 		unsigned int cpp, size;
3042 		u32 offset;
3043 		int x, y;
3044 		int ret;
3045 
3046 		cpp = fb->format->cpp[i];
3047 		intel_fb_plane_dims(&width, &height, fb, i);
3048 
3049 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3050 		if (ret) {
3051 			drm_dbg_kms(&dev_priv->drm,
3052 				    "bad fb plane %d offset: 0x%x\n",
3053 				    i, fb->offsets[i]);
3054 			return ret;
3055 		}
3056 
3057 		ret = intel_fb_check_ccs_xy(fb, i, x, y);
3058 		if (ret)
3059 			return ret;
3060 
3061 		/*
3062 		 * The fence (if used) is aligned to the start of the object
3063 		 * so having the framebuffer wrap around across the edge of the
3064 		 * fenced region doesn't really work. We have no API to configure
3065 		 * the fence start offset within the object (nor could we probably
3066 		 * on gen2/3). So it's just easier if we just require that the
3067 		 * fb layout agrees with the fence layout. We already check that the
3068 		 * fb stride matches the fence stride elsewhere.
3069 		 */
3070 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
3071 		    (x + width) * cpp > fb->pitches[i]) {
3072 			drm_dbg_kms(&dev_priv->drm,
3073 				    "bad fb plane %d offset: 0x%x\n",
3074 				     i, fb->offsets[i]);
3075 			return -EINVAL;
3076 		}
3077 
3078 		/*
3079 		 * First pixel of the framebuffer from
3080 		 * the start of the normal gtt mapping.
3081 		 */
3082 		intel_fb->normal[i].x = x;
3083 		intel_fb->normal[i].y = y;
3084 
3085 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3086 						      fb->pitches[i],
3087 						      DRM_MODE_ROTATE_0,
3088 						      tile_size);
3089 		offset /= tile_size;
3090 
3091 		if (!is_surface_linear(fb, i)) {
3092 			struct intel_remapped_plane_info plane_info;
3093 			unsigned int tile_width, tile_height;
3094 
3095 			intel_tile_dims(fb, i, &tile_width, &tile_height);
3096 
3097 			plane_info.offset = offset;
3098 			plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3099 							 tile_width * cpp);
3100 			plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3101 			plane_info.height = DIV_ROUND_UP(y + height,
3102 							 tile_height);
3103 
3104 			/* how many tiles does this plane need */
3105 			size = plane_info.stride * plane_info.height;
3106 			/*
3107 			 * If the plane isn't horizontally tile aligned,
3108 			 * we need one more tile.
3109 			 */
3110 			if (x != 0)
3111 				size++;
3112 
3113 			gtt_offset_rotated +=
3114 				setup_fb_rotation(i, &plane_info,
3115 						  gtt_offset_rotated,
3116 						  x, y, width, height,
3117 						  tile_size,
3118 						  tile_width, tile_height,
3119 						  fb);
3120 		} else {
3121 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3122 					    x * cpp, tile_size);
3123 		}
3124 
3125 		/* how many tiles in total needed in the bo */
3126 		max_size = max(max_size, offset + size);
3127 	}
3128 
3129 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
3130 		drm_dbg_kms(&dev_priv->drm,
3131 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3132 			    mul_u32_u32(max_size, tile_size), obj->base.size);
3133 		return -EINVAL;
3134 	}
3135 
3136 	return 0;
3137 }
3138 
3139 static void
3140 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3141 {
3142 	struct drm_i915_private *dev_priv =
3143 		to_i915(plane_state->uapi.plane->dev);
3144 	struct drm_framebuffer *fb = plane_state->hw.fb;
3145 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3146 	struct intel_rotation_info *info = &plane_state->view.rotated;
3147 	unsigned int rotation = plane_state->hw.rotation;
3148 	int i, num_planes = fb->format->num_planes;
3149 	unsigned int tile_size = intel_tile_size(dev_priv);
3150 	unsigned int src_x, src_y;
3151 	unsigned int src_w, src_h;
3152 	u32 gtt_offset = 0;
3153 
3154 	memset(&plane_state->view, 0, sizeof(plane_state->view));
3155 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3156 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3157 
3158 	src_x = plane_state->uapi.src.x1 >> 16;
3159 	src_y = plane_state->uapi.src.y1 >> 16;
3160 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3161 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
3162 
3163 	drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
3164 
3165 	/* Make src coordinates relative to the viewport */
3166 	drm_rect_translate(&plane_state->uapi.src,
3167 			   -(src_x << 16), -(src_y << 16));
3168 
3169 	/* Rotate src coordinates to match rotated GTT view */
3170 	if (drm_rotation_90_or_270(rotation))
3171 		drm_rect_rotate(&plane_state->uapi.src,
3172 				src_w << 16, src_h << 16,
3173 				DRM_MODE_ROTATE_270);
3174 
3175 	for (i = 0; i < num_planes; i++) {
3176 		unsigned int hsub = i ? fb->format->hsub : 1;
3177 		unsigned int vsub = i ? fb->format->vsub : 1;
3178 		unsigned int cpp = fb->format->cpp[i];
3179 		unsigned int tile_width, tile_height;
3180 		unsigned int width, height;
3181 		unsigned int pitch_tiles;
3182 		unsigned int x, y;
3183 		u32 offset;
3184 
3185 		intel_tile_dims(fb, i, &tile_width, &tile_height);
3186 
3187 		x = src_x / hsub;
3188 		y = src_y / vsub;
3189 		width = src_w / hsub;
3190 		height = src_h / vsub;
3191 
3192 		/*
3193 		 * First pixel of the src viewport from the
3194 		 * start of the normal gtt mapping.
3195 		 */
3196 		x += intel_fb->normal[i].x;
3197 		y += intel_fb->normal[i].y;
3198 
3199 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3200 						      fb, i, fb->pitches[i],
3201 						      DRM_MODE_ROTATE_0, tile_size);
3202 		offset /= tile_size;
3203 
3204 		drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
3205 		info->plane[i].offset = offset;
3206 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3207 						     tile_width * cpp);
3208 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3209 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3210 
3211 		if (drm_rotation_90_or_270(rotation)) {
3212 			struct drm_rect r;
3213 
3214 			/* rotate the x/y offsets to match the GTT view */
3215 			drm_rect_init(&r, x, y, width, height);
3216 			drm_rect_rotate(&r,
3217 					info->plane[i].width * tile_width,
3218 					info->plane[i].height * tile_height,
3219 					DRM_MODE_ROTATE_270);
3220 			x = r.x1;
3221 			y = r.y1;
3222 
3223 			pitch_tiles = info->plane[i].height;
3224 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3225 
3226 			/* rotate the tile dimensions to match the GTT view */
3227 			swap(tile_width, tile_height);
3228 		} else {
3229 			pitch_tiles = info->plane[i].width;
3230 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3231 		}
3232 
3233 		/*
3234 		 * We only keep the x/y offsets, so push all of the
3235 		 * gtt offset into the x/y offsets.
3236 		 */
3237 		intel_adjust_tile_offset(&x, &y,
3238 					 tile_width, tile_height,
3239 					 tile_size, pitch_tiles,
3240 					 gtt_offset * tile_size, 0);
3241 
3242 		gtt_offset += info->plane[i].width * info->plane[i].height;
3243 
3244 		plane_state->color_plane[i].offset = 0;
3245 		plane_state->color_plane[i].x = x;
3246 		plane_state->color_plane[i].y = y;
3247 	}
3248 }
3249 
3250 static int
3251 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3252 {
3253 	const struct intel_framebuffer *fb =
3254 		to_intel_framebuffer(plane_state->hw.fb);
3255 	unsigned int rotation = plane_state->hw.rotation;
3256 	int i, num_planes;
3257 
3258 	if (!fb)
3259 		return 0;
3260 
3261 	num_planes = fb->base.format->num_planes;
3262 
3263 	if (intel_plane_needs_remap(plane_state)) {
3264 		intel_plane_remap_gtt(plane_state);
3265 
3266 		/*
3267 		 * Sometimes even remapping can't overcome
3268 		 * the stride limitations :( Can happen with
3269 		 * big plane sizes and suitably misaligned
3270 		 * offsets.
3271 		 */
3272 		return intel_plane_check_stride(plane_state);
3273 	}
3274 
3275 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3276 
3277 	for (i = 0; i < num_planes; i++) {
3278 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3279 		plane_state->color_plane[i].offset = 0;
3280 
3281 		if (drm_rotation_90_or_270(rotation)) {
3282 			plane_state->color_plane[i].x = fb->rotated[i].x;
3283 			plane_state->color_plane[i].y = fb->rotated[i].y;
3284 		} else {
3285 			plane_state->color_plane[i].x = fb->normal[i].x;
3286 			plane_state->color_plane[i].y = fb->normal[i].y;
3287 		}
3288 	}
3289 
3290 	/* Rotate src coordinates to match rotated GTT view */
3291 	if (drm_rotation_90_or_270(rotation))
3292 		drm_rect_rotate(&plane_state->uapi.src,
3293 				fb->base.width << 16, fb->base.height << 16,
3294 				DRM_MODE_ROTATE_270);
3295 
3296 	return intel_plane_check_stride(plane_state);
3297 }
3298 
3299 static int i9xx_format_to_fourcc(int format)
3300 {
3301 	switch (format) {
3302 	case DISPPLANE_8BPP:
3303 		return DRM_FORMAT_C8;
3304 	case DISPPLANE_BGRA555:
3305 		return DRM_FORMAT_ARGB1555;
3306 	case DISPPLANE_BGRX555:
3307 		return DRM_FORMAT_XRGB1555;
3308 	case DISPPLANE_BGRX565:
3309 		return DRM_FORMAT_RGB565;
3310 	default:
3311 	case DISPPLANE_BGRX888:
3312 		return DRM_FORMAT_XRGB8888;
3313 	case DISPPLANE_RGBX888:
3314 		return DRM_FORMAT_XBGR8888;
3315 	case DISPPLANE_BGRA888:
3316 		return DRM_FORMAT_ARGB8888;
3317 	case DISPPLANE_RGBA888:
3318 		return DRM_FORMAT_ABGR8888;
3319 	case DISPPLANE_BGRX101010:
3320 		return DRM_FORMAT_XRGB2101010;
3321 	case DISPPLANE_RGBX101010:
3322 		return DRM_FORMAT_XBGR2101010;
3323 	case DISPPLANE_BGRA101010:
3324 		return DRM_FORMAT_ARGB2101010;
3325 	case DISPPLANE_RGBA101010:
3326 		return DRM_FORMAT_ABGR2101010;
3327 	case DISPPLANE_RGBX161616:
3328 		return DRM_FORMAT_XBGR16161616F;
3329 	}
3330 }
3331 
3332 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
3333 {
3334 	switch (format) {
3335 	case PLANE_CTL_FORMAT_RGB_565:
3336 		return DRM_FORMAT_RGB565;
3337 	case PLANE_CTL_FORMAT_NV12:
3338 		return DRM_FORMAT_NV12;
3339 	case PLANE_CTL_FORMAT_P010:
3340 		return DRM_FORMAT_P010;
3341 	case PLANE_CTL_FORMAT_P012:
3342 		return DRM_FORMAT_P012;
3343 	case PLANE_CTL_FORMAT_P016:
3344 		return DRM_FORMAT_P016;
3345 	case PLANE_CTL_FORMAT_Y210:
3346 		return DRM_FORMAT_Y210;
3347 	case PLANE_CTL_FORMAT_Y212:
3348 		return DRM_FORMAT_Y212;
3349 	case PLANE_CTL_FORMAT_Y216:
3350 		return DRM_FORMAT_Y216;
3351 	case PLANE_CTL_FORMAT_Y410:
3352 		return DRM_FORMAT_XVYU2101010;
3353 	case PLANE_CTL_FORMAT_Y412:
3354 		return DRM_FORMAT_XVYU12_16161616;
3355 	case PLANE_CTL_FORMAT_Y416:
3356 		return DRM_FORMAT_XVYU16161616;
3357 	default:
3358 	case PLANE_CTL_FORMAT_XRGB_8888:
3359 		if (rgb_order) {
3360 			if (alpha)
3361 				return DRM_FORMAT_ABGR8888;
3362 			else
3363 				return DRM_FORMAT_XBGR8888;
3364 		} else {
3365 			if (alpha)
3366 				return DRM_FORMAT_ARGB8888;
3367 			else
3368 				return DRM_FORMAT_XRGB8888;
3369 		}
3370 	case PLANE_CTL_FORMAT_XRGB_2101010:
3371 		if (rgb_order) {
3372 			if (alpha)
3373 				return DRM_FORMAT_ABGR2101010;
3374 			else
3375 				return DRM_FORMAT_XBGR2101010;
3376 		} else {
3377 			if (alpha)
3378 				return DRM_FORMAT_ARGB2101010;
3379 			else
3380 				return DRM_FORMAT_XRGB2101010;
3381 		}
3382 	case PLANE_CTL_FORMAT_XRGB_16161616F:
3383 		if (rgb_order) {
3384 			if (alpha)
3385 				return DRM_FORMAT_ABGR16161616F;
3386 			else
3387 				return DRM_FORMAT_XBGR16161616F;
3388 		} else {
3389 			if (alpha)
3390 				return DRM_FORMAT_ARGB16161616F;
3391 			else
3392 				return DRM_FORMAT_XRGB16161616F;
3393 		}
3394 	}
3395 }
3396 
3397 static struct i915_vma *
3398 initial_plane_vma(struct drm_i915_private *i915,
3399 		  struct intel_initial_plane_config *plane_config)
3400 {
3401 	struct drm_i915_gem_object *obj;
3402 	struct i915_vma *vma;
3403 	u32 base, size;
3404 
3405 	if (plane_config->size == 0)
3406 		return NULL;
3407 
3408 	base = round_down(plane_config->base,
3409 			  I915_GTT_MIN_ALIGNMENT);
3410 	size = round_up(plane_config->base + plane_config->size,
3411 			I915_GTT_MIN_ALIGNMENT);
3412 	size -= base;
3413 
3414 	/*
3415 	 * If the FB is too big, just don't use it since fbdev is not very
3416 	 * important and we should probably use that space with FBC or other
3417 	 * features.
3418 	 */
3419 	if (size * 2 > i915->stolen_usable_size)
3420 		return NULL;
3421 
3422 	obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3423 	if (IS_ERR(obj))
3424 		return NULL;
3425 
3426 	switch (plane_config->tiling) {
3427 	case I915_TILING_NONE:
3428 		break;
3429 	case I915_TILING_X:
3430 	case I915_TILING_Y:
3431 		obj->tiling_and_stride =
3432 			plane_config->fb->base.pitches[0] |
3433 			plane_config->tiling;
3434 		break;
3435 	default:
3436 		MISSING_CASE(plane_config->tiling);
3437 		goto err_obj;
3438 	}
3439 
3440 	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3441 	if (IS_ERR(vma))
3442 		goto err_obj;
3443 
3444 	if (i915_ggtt_pin(vma, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
3445 		goto err_obj;
3446 
3447 	if (i915_gem_object_is_tiled(obj) &&
3448 	    !i915_vma_is_map_and_fenceable(vma))
3449 		goto err_obj;
3450 
3451 	return vma;
3452 
3453 err_obj:
3454 	i915_gem_object_put(obj);
3455 	return NULL;
3456 }
3457 
3458 static bool
3459 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3460 			      struct intel_initial_plane_config *plane_config)
3461 {
3462 	struct drm_device *dev = crtc->base.dev;
3463 	struct drm_i915_private *dev_priv = to_i915(dev);
3464 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3465 	struct drm_framebuffer *fb = &plane_config->fb->base;
3466 	struct i915_vma *vma;
3467 
3468 	switch (fb->modifier) {
3469 	case DRM_FORMAT_MOD_LINEAR:
3470 	case I915_FORMAT_MOD_X_TILED:
3471 	case I915_FORMAT_MOD_Y_TILED:
3472 		break;
3473 	default:
3474 		drm_dbg(&dev_priv->drm,
3475 			"Unsupported modifier for initial FB: 0x%llx\n",
3476 			fb->modifier);
3477 		return false;
3478 	}
3479 
3480 	vma = initial_plane_vma(dev_priv, plane_config);
3481 	if (!vma)
3482 		return false;
3483 
3484 	mode_cmd.pixel_format = fb->format->format;
3485 	mode_cmd.width = fb->width;
3486 	mode_cmd.height = fb->height;
3487 	mode_cmd.pitches[0] = fb->pitches[0];
3488 	mode_cmd.modifier[0] = fb->modifier;
3489 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3490 
3491 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
3492 				   vma->obj, &mode_cmd)) {
3493 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
3494 		goto err_vma;
3495 	}
3496 
3497 	plane_config->vma = vma;
3498 	return true;
3499 
3500 err_vma:
3501 	i915_vma_put(vma);
3502 	return false;
3503 }
3504 
3505 static void
3506 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3507 			struct intel_plane_state *plane_state,
3508 			bool visible)
3509 {
3510 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3511 
3512 	plane_state->uapi.visible = visible;
3513 
3514 	if (visible)
3515 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
3516 	else
3517 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
3518 }
3519 
3520 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3521 {
3522 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3523 	struct drm_plane *plane;
3524 
3525 	/*
3526 	 * Active_planes aliases if multiple "primary" or cursor planes
3527 	 * have been used on the same (or wrong) pipe. plane_mask uses
3528 	 * unique ids, hence we can use that to reconstruct active_planes.
3529 	 */
3530 	crtc_state->active_planes = 0;
3531 
3532 	drm_for_each_plane_mask(plane, &dev_priv->drm,
3533 				crtc_state->uapi.plane_mask)
3534 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3535 }
3536 
3537 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3538 					 struct intel_plane *plane)
3539 {
3540 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3541 	struct intel_crtc_state *crtc_state =
3542 		to_intel_crtc_state(crtc->base.state);
3543 	struct intel_plane_state *plane_state =
3544 		to_intel_plane_state(plane->base.state);
3545 
3546 	drm_dbg_kms(&dev_priv->drm,
3547 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3548 		    plane->base.base.id, plane->base.name,
3549 		    crtc->base.base.id, crtc->base.name);
3550 
3551 	intel_set_plane_visible(crtc_state, plane_state, false);
3552 	fixup_active_planes(crtc_state);
3553 	crtc_state->data_rate[plane->id] = 0;
3554 	crtc_state->min_cdclk[plane->id] = 0;
3555 
3556 	if (plane->id == PLANE_PRIMARY)
3557 		hsw_disable_ips(crtc_state);
3558 
3559 	/*
3560 	 * Vblank time updates from the shadow to live plane control register
3561 	 * are blocked if the memory self-refresh mode is active at that
3562 	 * moment. So to make sure the plane gets truly disabled, disable
3563 	 * first the self-refresh mode. The self-refresh enable bit in turn
3564 	 * will be checked/applied by the HW only at the next frame start
3565 	 * event which is after the vblank start event, so we need to have a
3566 	 * wait-for-vblank between disabling the plane and the pipe.
3567 	 */
3568 	if (HAS_GMCH(dev_priv) &&
3569 	    intel_set_memory_cxsr(dev_priv, false))
3570 		intel_wait_for_vblank(dev_priv, crtc->pipe);
3571 
3572 	/*
3573 	 * Gen2 reports pipe underruns whenever all planes are disabled.
3574 	 * So disable underrun reporting before all the planes get disabled.
3575 	 */
3576 	if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3577 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3578 
3579 	intel_disable_plane(plane, crtc_state);
3580 }
3581 
3582 static struct intel_frontbuffer *
3583 to_intel_frontbuffer(struct drm_framebuffer *fb)
3584 {
3585 	return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3586 }
3587 
3588 static void
3589 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3590 			     struct intel_initial_plane_config *plane_config)
3591 {
3592 	struct drm_device *dev = intel_crtc->base.dev;
3593 	struct drm_i915_private *dev_priv = to_i915(dev);
3594 	struct drm_crtc *c;
3595 	struct drm_plane *primary = intel_crtc->base.primary;
3596 	struct drm_plane_state *plane_state = primary->state;
3597 	struct intel_plane *intel_plane = to_intel_plane(primary);
3598 	struct intel_plane_state *intel_state =
3599 		to_intel_plane_state(plane_state);
3600 	struct drm_framebuffer *fb;
3601 	struct i915_vma *vma;
3602 
3603 	if (!plane_config->fb)
3604 		return;
3605 
3606 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3607 		fb = &plane_config->fb->base;
3608 		vma = plane_config->vma;
3609 		goto valid_fb;
3610 	}
3611 
3612 	/*
3613 	 * Failed to alloc the obj, check to see if we should share
3614 	 * an fb with another CRTC instead
3615 	 */
3616 	for_each_crtc(dev, c) {
3617 		struct intel_plane_state *state;
3618 
3619 		if (c == &intel_crtc->base)
3620 			continue;
3621 
3622 		if (!to_intel_crtc(c)->active)
3623 			continue;
3624 
3625 		state = to_intel_plane_state(c->primary->state);
3626 		if (!state->vma)
3627 			continue;
3628 
3629 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
3630 			fb = state->hw.fb;
3631 			vma = state->vma;
3632 			goto valid_fb;
3633 		}
3634 	}
3635 
3636 	/*
3637 	 * We've failed to reconstruct the BIOS FB.  Current display state
3638 	 * indicates that the primary plane is visible, but has a NULL FB,
3639 	 * which will lead to problems later if we don't fix it up.  The
3640 	 * simplest solution is to just disable the primary plane now and
3641 	 * pretend the BIOS never had it enabled.
3642 	 */
3643 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
3644 
3645 	return;
3646 
3647 valid_fb:
3648 	intel_state->hw.rotation = plane_config->rotation;
3649 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
3650 				intel_state->hw.rotation);
3651 	intel_state->color_plane[0].stride =
3652 		intel_fb_pitch(fb, 0, intel_state->hw.rotation);
3653 
3654 	__i915_vma_pin(vma);
3655 	intel_state->vma = i915_vma_get(vma);
3656 	if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3657 		if (vma->fence)
3658 			intel_state->flags |= PLANE_HAS_FENCE;
3659 
3660 	plane_state->src_x = 0;
3661 	plane_state->src_y = 0;
3662 	plane_state->src_w = fb->width << 16;
3663 	plane_state->src_h = fb->height << 16;
3664 
3665 	plane_state->crtc_x = 0;
3666 	plane_state->crtc_y = 0;
3667 	plane_state->crtc_w = fb->width;
3668 	plane_state->crtc_h = fb->height;
3669 
3670 	intel_state->uapi.src = drm_plane_state_src(plane_state);
3671 	intel_state->uapi.dst = drm_plane_state_dest(plane_state);
3672 
3673 	if (plane_config->tiling)
3674 		dev_priv->preserve_bios_swizzle = true;
3675 
3676 	plane_state->fb = fb;
3677 	drm_framebuffer_get(fb);
3678 
3679 	plane_state->crtc = &intel_crtc->base;
3680 	intel_plane_copy_uapi_to_hw_state(intel_state, intel_state);
3681 
3682 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3683 
3684 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3685 		  &to_intel_frontbuffer(fb)->bits);
3686 }
3687 
3688 static int skl_max_plane_width(const struct drm_framebuffer *fb,
3689 			       int color_plane,
3690 			       unsigned int rotation)
3691 {
3692 	int cpp = fb->format->cpp[color_plane];
3693 
3694 	switch (fb->modifier) {
3695 	case DRM_FORMAT_MOD_LINEAR:
3696 	case I915_FORMAT_MOD_X_TILED:
3697 		/*
3698 		 * Validated limit is 4k, but has 5k should
3699 		 * work apart from the following features:
3700 		 * - Ytile (already limited to 4k)
3701 		 * - FP16 (already limited to 4k)
3702 		 * - render compression (already limited to 4k)
3703 		 * - KVMR sprite and cursor (don't care)
3704 		 * - horizontal panning (TODO verify this)
3705 		 * - pipe and plane scaling (TODO verify this)
3706 		 */
3707 		if (cpp == 8)
3708 			return 4096;
3709 		else
3710 			return 5120;
3711 	case I915_FORMAT_MOD_Y_TILED_CCS:
3712 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3713 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
3714 		/* FIXME AUX plane? */
3715 	case I915_FORMAT_MOD_Y_TILED:
3716 	case I915_FORMAT_MOD_Yf_TILED:
3717 		if (cpp == 8)
3718 			return 2048;
3719 		else
3720 			return 4096;
3721 	default:
3722 		MISSING_CASE(fb->modifier);
3723 		return 2048;
3724 	}
3725 }
3726 
3727 static int glk_max_plane_width(const struct drm_framebuffer *fb,
3728 			       int color_plane,
3729 			       unsigned int rotation)
3730 {
3731 	int cpp = fb->format->cpp[color_plane];
3732 
3733 	switch (fb->modifier) {
3734 	case DRM_FORMAT_MOD_LINEAR:
3735 	case I915_FORMAT_MOD_X_TILED:
3736 		if (cpp == 8)
3737 			return 4096;
3738 		else
3739 			return 5120;
3740 	case I915_FORMAT_MOD_Y_TILED_CCS:
3741 	case I915_FORMAT_MOD_Yf_TILED_CCS:
3742 		/* FIXME AUX plane? */
3743 	case I915_FORMAT_MOD_Y_TILED:
3744 	case I915_FORMAT_MOD_Yf_TILED:
3745 		if (cpp == 8)
3746 			return 2048;
3747 		else
3748 			return 5120;
3749 	default:
3750 		MISSING_CASE(fb->modifier);
3751 		return 2048;
3752 	}
3753 }
3754 
3755 static int icl_max_plane_width(const struct drm_framebuffer *fb,
3756 			       int color_plane,
3757 			       unsigned int rotation)
3758 {
3759 	return 5120;
3760 }
3761 
3762 static int skl_max_plane_height(void)
3763 {
3764 	return 4096;
3765 }
3766 
3767 static int icl_max_plane_height(void)
3768 {
3769 	return 4320;
3770 }
3771 
3772 static bool
3773 skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3774 			       int main_x, int main_y, u32 main_offset,
3775 			       int ccs_plane)
3776 {
3777 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3778 	int aux_x = plane_state->color_plane[ccs_plane].x;
3779 	int aux_y = plane_state->color_plane[ccs_plane].y;
3780 	u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3781 	u32 alignment = intel_surf_alignment(fb, ccs_plane);
3782 	int hsub;
3783 	int vsub;
3784 
3785 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3786 	while (aux_offset >= main_offset && aux_y <= main_y) {
3787 		int x, y;
3788 
3789 		if (aux_x == main_x && aux_y == main_y)
3790 			break;
3791 
3792 		if (aux_offset == 0)
3793 			break;
3794 
3795 		x = aux_x / hsub;
3796 		y = aux_y / vsub;
3797 		aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3798 							       plane_state,
3799 							       ccs_plane,
3800 							       aux_offset,
3801 							       aux_offset -
3802 								alignment);
3803 		aux_x = x * hsub + aux_x % hsub;
3804 		aux_y = y * vsub + aux_y % vsub;
3805 	}
3806 
3807 	if (aux_x != main_x || aux_y != main_y)
3808 		return false;
3809 
3810 	plane_state->color_plane[ccs_plane].offset = aux_offset;
3811 	plane_state->color_plane[ccs_plane].x = aux_x;
3812 	plane_state->color_plane[ccs_plane].y = aux_y;
3813 
3814 	return true;
3815 }
3816 
3817 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3818 {
3819 	struct drm_i915_private *dev_priv = to_i915(plane_state->uapi.plane->dev);
3820 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3821 	unsigned int rotation = plane_state->hw.rotation;
3822 	int x = plane_state->uapi.src.x1 >> 16;
3823 	int y = plane_state->uapi.src.y1 >> 16;
3824 	int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3825 	int h = drm_rect_height(&plane_state->uapi.src) >> 16;
3826 	int max_width;
3827 	int max_height;
3828 	u32 alignment;
3829 	u32 offset;
3830 	int aux_plane = intel_main_to_aux_plane(fb, 0);
3831 	u32 aux_offset = plane_state->color_plane[aux_plane].offset;
3832 
3833 	if (INTEL_GEN(dev_priv) >= 11)
3834 		max_width = icl_max_plane_width(fb, 0, rotation);
3835 	else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
3836 		max_width = glk_max_plane_width(fb, 0, rotation);
3837 	else
3838 		max_width = skl_max_plane_width(fb, 0, rotation);
3839 
3840 	if (INTEL_GEN(dev_priv) >= 11)
3841 		max_height = icl_max_plane_height();
3842 	else
3843 		max_height = skl_max_plane_height();
3844 
3845 	if (w > max_width || h > max_height) {
3846 		drm_dbg_kms(&dev_priv->drm,
3847 			    "requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
3848 			    w, h, max_width, max_height);
3849 		return -EINVAL;
3850 	}
3851 
3852 	intel_add_fb_offsets(&x, &y, plane_state, 0);
3853 	offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3854 	alignment = intel_surf_alignment(fb, 0);
3855 	if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
3856 		return -EINVAL;
3857 
3858 	/*
3859 	 * AUX surface offset is specified as the distance from the
3860 	 * main surface offset, and it must be non-negative. Make
3861 	 * sure that is what we will get.
3862 	 */
3863 	if (offset > aux_offset)
3864 		offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3865 							   offset, aux_offset & ~(alignment - 1));
3866 
3867 	/*
3868 	 * When using an X-tiled surface, the plane blows up
3869 	 * if the x offset + width exceed the stride.
3870 	 *
3871 	 * TODO: linear and Y-tiled seem fine, Yf untested,
3872 	 */
3873 	if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3874 		int cpp = fb->format->cpp[0];
3875 
3876 		while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3877 			if (offset == 0) {
3878 				drm_dbg_kms(&dev_priv->drm,
3879 					    "Unable to find suitable display surface offset due to X-tiling\n");
3880 				return -EINVAL;
3881 			}
3882 
3883 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3884 								   offset, offset - alignment);
3885 		}
3886 	}
3887 
3888 	/*
3889 	 * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3890 	 * they match with the main surface x/y offsets.
3891 	 */
3892 	if (is_ccs_modifier(fb->modifier)) {
3893 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3894 						       offset, aux_plane)) {
3895 			if (offset == 0)
3896 				break;
3897 
3898 			offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3899 								   offset, offset - alignment);
3900 		}
3901 
3902 		if (x != plane_state->color_plane[aux_plane].x ||
3903 		    y != plane_state->color_plane[aux_plane].y) {
3904 			drm_dbg_kms(&dev_priv->drm,
3905 				    "Unable to find suitable display surface offset due to CCS\n");
3906 			return -EINVAL;
3907 		}
3908 	}
3909 
3910 	plane_state->color_plane[0].offset = offset;
3911 	plane_state->color_plane[0].x = x;
3912 	plane_state->color_plane[0].y = y;
3913 
3914 	/*
3915 	 * Put the final coordinates back so that the src
3916 	 * coordinate checks will see the right values.
3917 	 */
3918 	drm_rect_translate_to(&plane_state->uapi.src,
3919 			      x << 16, y << 16);
3920 
3921 	return 0;
3922 }
3923 
3924 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3925 {
3926 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
3927 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3928 	unsigned int rotation = plane_state->hw.rotation;
3929 	int uv_plane = 1;
3930 	int max_width = skl_max_plane_width(fb, uv_plane, rotation);
3931 	int max_height = 4096;
3932 	int x = plane_state->uapi.src.x1 >> 17;
3933 	int y = plane_state->uapi.src.y1 >> 17;
3934 	int w = drm_rect_width(&plane_state->uapi.src) >> 17;
3935 	int h = drm_rect_height(&plane_state->uapi.src) >> 17;
3936 	u32 offset;
3937 
3938 	intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
3939 	offset = intel_plane_compute_aligned_offset(&x, &y,
3940 						    plane_state, uv_plane);
3941 
3942 	/* FIXME not quite sure how/if these apply to the chroma plane */
3943 	if (w > max_width || h > max_height) {
3944 		drm_dbg_kms(&i915->drm,
3945 			    "CbCr source size %dx%d too big (limit %dx%d)\n",
3946 			    w, h, max_width, max_height);
3947 		return -EINVAL;
3948 	}
3949 
3950 	if (is_ccs_modifier(fb->modifier)) {
3951 		int ccs_plane = main_to_ccs_plane(fb, uv_plane);
3952 		int aux_offset = plane_state->color_plane[ccs_plane].offset;
3953 		int alignment = intel_surf_alignment(fb, uv_plane);
3954 
3955 		if (offset > aux_offset)
3956 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3957 								   plane_state,
3958 								   uv_plane,
3959 								   offset,
3960 								   aux_offset & ~(alignment - 1));
3961 
3962 		while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3963 						       offset, ccs_plane)) {
3964 			if (offset == 0)
3965 				break;
3966 
3967 			offset = intel_plane_adjust_aligned_offset(&x, &y,
3968 								   plane_state,
3969 								   uv_plane,
3970 								   offset, offset - alignment);
3971 		}
3972 
3973 		if (x != plane_state->color_plane[ccs_plane].x ||
3974 		    y != plane_state->color_plane[ccs_plane].y) {
3975 			drm_dbg_kms(&i915->drm,
3976 				    "Unable to find suitable display surface offset due to CCS\n");
3977 			return -EINVAL;
3978 		}
3979 	}
3980 
3981 	plane_state->color_plane[uv_plane].offset = offset;
3982 	plane_state->color_plane[uv_plane].x = x;
3983 	plane_state->color_plane[uv_plane].y = y;
3984 
3985 	return 0;
3986 }
3987 
3988 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3989 {
3990 	const struct drm_framebuffer *fb = plane_state->hw.fb;
3991 	int src_x = plane_state->uapi.src.x1 >> 16;
3992 	int src_y = plane_state->uapi.src.y1 >> 16;
3993 	u32 offset;
3994 	int ccs_plane;
3995 
3996 	for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
3997 		int main_hsub, main_vsub;
3998 		int hsub, vsub;
3999 		int x, y;
4000 
4001 		if (!is_ccs_plane(fb, ccs_plane))
4002 			continue;
4003 
4004 		intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
4005 					       ccs_to_main_plane(fb, ccs_plane));
4006 		intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
4007 
4008 		hsub *= main_hsub;
4009 		vsub *= main_vsub;
4010 		x = src_x / hsub;
4011 		y = src_y / vsub;
4012 
4013 		intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
4014 
4015 		offset = intel_plane_compute_aligned_offset(&x, &y,
4016 							    plane_state,
4017 							    ccs_plane);
4018 
4019 		plane_state->color_plane[ccs_plane].offset = offset;
4020 		plane_state->color_plane[ccs_plane].x = (x * hsub +
4021 							 src_x % hsub) /
4022 							main_hsub;
4023 		plane_state->color_plane[ccs_plane].y = (y * vsub +
4024 							 src_y % vsub) /
4025 							main_vsub;
4026 	}
4027 
4028 	return 0;
4029 }
4030 
4031 int skl_check_plane_surface(struct intel_plane_state *plane_state)
4032 {
4033 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4034 	int ret;
4035 	bool needs_aux = false;
4036 
4037 	ret = intel_plane_compute_gtt(plane_state);
4038 	if (ret)
4039 		return ret;
4040 
4041 	if (!plane_state->uapi.visible)
4042 		return 0;
4043 
4044 	/*
4045 	 * Handle the AUX surface first since the main surface setup depends on
4046 	 * it.
4047 	 */
4048 	if (is_ccs_modifier(fb->modifier)) {
4049 		needs_aux = true;
4050 		ret = skl_check_ccs_aux_surface(plane_state);
4051 		if (ret)
4052 			return ret;
4053 	}
4054 
4055 	if (intel_format_info_is_yuv_semiplanar(fb->format,
4056 						fb->modifier)) {
4057 		needs_aux = true;
4058 		ret = skl_check_nv12_aux_surface(plane_state);
4059 		if (ret)
4060 			return ret;
4061 	}
4062 
4063 	if (!needs_aux) {
4064 		int i;
4065 
4066 		for (i = 1; i < fb->format->num_planes; i++) {
4067 			plane_state->color_plane[i].offset = ~0xfff;
4068 			plane_state->color_plane[i].x = 0;
4069 			plane_state->color_plane[i].y = 0;
4070 		}
4071 	}
4072 
4073 	ret = skl_check_main_surface(plane_state);
4074 	if (ret)
4075 		return ret;
4076 
4077 	return 0;
4078 }
4079 
4080 static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4081 			     const struct intel_plane_state *plane_state,
4082 			     unsigned int *num, unsigned int *den)
4083 {
4084 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4085 	unsigned int cpp = fb->format->cpp[0];
4086 
4087 	/*
4088 	 * g4x bspec says 64bpp pixel rate can't exceed 80%
4089 	 * of cdclk when the sprite plane is enabled on the
4090 	 * same pipe. ilk/snb bspec says 64bpp pixel rate is
4091 	 * never allowed to exceed 80% of cdclk. Let's just go
4092 	 * with the ilk/snb limit always.
4093 	 */
4094 	if (cpp == 8) {
4095 		*num = 10;
4096 		*den = 8;
4097 	} else {
4098 		*num = 1;
4099 		*den = 1;
4100 	}
4101 }
4102 
4103 static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4104 				const struct intel_plane_state *plane_state)
4105 {
4106 	unsigned int pixel_rate;
4107 	unsigned int num, den;
4108 
4109 	/*
4110 	 * Note that crtc_state->pixel_rate accounts for both
4111 	 * horizontal and vertical panel fitter downscaling factors.
4112 	 * Pre-HSW bspec tells us to only consider the horizontal
4113 	 * downscaling factor here. We ignore that and just consider
4114 	 * both for simplicity.
4115 	 */
4116 	pixel_rate = crtc_state->pixel_rate;
4117 
4118 	i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4119 
4120 	/* two pixels per clock with double wide pipe */
4121 	if (crtc_state->double_wide)
4122 		den *= 2;
4123 
4124 	return DIV_ROUND_UP(pixel_rate * num, den);
4125 }
4126 
4127 unsigned int
4128 i9xx_plane_max_stride(struct intel_plane *plane,
4129 		      u32 pixel_format, u64 modifier,
4130 		      unsigned int rotation)
4131 {
4132 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4133 
4134 	if (!HAS_GMCH(dev_priv)) {
4135 		return 32*1024;
4136 	} else if (INTEL_GEN(dev_priv) >= 4) {
4137 		if (modifier == I915_FORMAT_MOD_X_TILED)
4138 			return 16*1024;
4139 		else
4140 			return 32*1024;
4141 	} else if (INTEL_GEN(dev_priv) >= 3) {
4142 		if (modifier == I915_FORMAT_MOD_X_TILED)
4143 			return 8*1024;
4144 		else
4145 			return 16*1024;
4146 	} else {
4147 		if (plane->i9xx_plane == PLANE_C)
4148 			return 4*1024;
4149 		else
4150 			return 8*1024;
4151 	}
4152 }
4153 
4154 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4155 {
4156 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4157 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4158 	u32 dspcntr = 0;
4159 
4160 	if (crtc_state->gamma_enable)
4161 		dspcntr |= DISPPLANE_GAMMA_ENABLE;
4162 
4163 	if (crtc_state->csc_enable)
4164 		dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4165 
4166 	if (INTEL_GEN(dev_priv) < 5)
4167 		dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4168 
4169 	return dspcntr;
4170 }
4171 
4172 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4173 			  const struct intel_plane_state *plane_state)
4174 {
4175 	struct drm_i915_private *dev_priv =
4176 		to_i915(plane_state->uapi.plane->dev);
4177 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4178 	unsigned int rotation = plane_state->hw.rotation;
4179 	u32 dspcntr;
4180 
4181 	dspcntr = DISPLAY_PLANE_ENABLE;
4182 
4183 	if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4184 	    IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
4185 		dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
4186 
4187 	switch (fb->format->format) {
4188 	case DRM_FORMAT_C8:
4189 		dspcntr |= DISPPLANE_8BPP;
4190 		break;
4191 	case DRM_FORMAT_XRGB1555:
4192 		dspcntr |= DISPPLANE_BGRX555;
4193 		break;
4194 	case DRM_FORMAT_ARGB1555:
4195 		dspcntr |= DISPPLANE_BGRA555;
4196 		break;
4197 	case DRM_FORMAT_RGB565:
4198 		dspcntr |= DISPPLANE_BGRX565;
4199 		break;
4200 	case DRM_FORMAT_XRGB8888:
4201 		dspcntr |= DISPPLANE_BGRX888;
4202 		break;
4203 	case DRM_FORMAT_XBGR8888:
4204 		dspcntr |= DISPPLANE_RGBX888;
4205 		break;
4206 	case DRM_FORMAT_ARGB8888:
4207 		dspcntr |= DISPPLANE_BGRA888;
4208 		break;
4209 	case DRM_FORMAT_ABGR8888:
4210 		dspcntr |= DISPPLANE_RGBA888;
4211 		break;
4212 	case DRM_FORMAT_XRGB2101010:
4213 		dspcntr |= DISPPLANE_BGRX101010;
4214 		break;
4215 	case DRM_FORMAT_XBGR2101010:
4216 		dspcntr |= DISPPLANE_RGBX101010;
4217 		break;
4218 	case DRM_FORMAT_ARGB2101010:
4219 		dspcntr |= DISPPLANE_BGRA101010;
4220 		break;
4221 	case DRM_FORMAT_ABGR2101010:
4222 		dspcntr |= DISPPLANE_RGBA101010;
4223 		break;
4224 	case DRM_FORMAT_XBGR16161616F:
4225 		dspcntr |= DISPPLANE_RGBX161616;
4226 		break;
4227 	default:
4228 		MISSING_CASE(fb->format->format);
4229 		return 0;
4230 	}
4231 
4232 	if (INTEL_GEN(dev_priv) >= 4 &&
4233 	    fb->modifier == I915_FORMAT_MOD_X_TILED)
4234 		dspcntr |= DISPPLANE_TILED;
4235 
4236 	if (rotation & DRM_MODE_ROTATE_180)
4237 		dspcntr |= DISPPLANE_ROTATE_180;
4238 
4239 	if (rotation & DRM_MODE_REFLECT_X)
4240 		dspcntr |= DISPPLANE_MIRROR;
4241 
4242 	return dspcntr;
4243 }
4244 
4245 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
4246 {
4247 	struct drm_i915_private *dev_priv =
4248 		to_i915(plane_state->uapi.plane->dev);
4249 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4250 	int src_x, src_y, src_w;
4251 	u32 offset;
4252 	int ret;
4253 
4254 	ret = intel_plane_compute_gtt(plane_state);
4255 	if (ret)
4256 		return ret;
4257 
4258 	if (!plane_state->uapi.visible)
4259 		return 0;
4260 
4261 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4262 	src_x = plane_state->uapi.src.x1 >> 16;
4263 	src_y = plane_state->uapi.src.y1 >> 16;
4264 
4265 	/* Undocumented hardware limit on i965/g4x/vlv/chv */
4266 	if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4267 		return -EINVAL;
4268 
4269 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
4270 
4271 	if (INTEL_GEN(dev_priv) >= 4)
4272 		offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4273 							    plane_state, 0);
4274 	else
4275 		offset = 0;
4276 
4277 	/*
4278 	 * Put the final coordinates back so that the src
4279 	 * coordinate checks will see the right values.
4280 	 */
4281 	drm_rect_translate_to(&plane_state->uapi.src,
4282 			      src_x << 16, src_y << 16);
4283 
4284 	/* HSW/BDW do this automagically in hardware */
4285 	if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
4286 		unsigned int rotation = plane_state->hw.rotation;
4287 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4288 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4289 
4290 		if (rotation & DRM_MODE_ROTATE_180) {
4291 			src_x += src_w - 1;
4292 			src_y += src_h - 1;
4293 		} else if (rotation & DRM_MODE_REFLECT_X) {
4294 			src_x += src_w - 1;
4295 		}
4296 	}
4297 
4298 	plane_state->color_plane[0].offset = offset;
4299 	plane_state->color_plane[0].x = src_x;
4300 	plane_state->color_plane[0].y = src_y;
4301 
4302 	return 0;
4303 }
4304 
4305 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4306 {
4307 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4308 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4309 
4310 	if (IS_CHERRYVIEW(dev_priv))
4311 		return i9xx_plane == PLANE_B;
4312 	else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4313 		return false;
4314 	else if (IS_GEN(dev_priv, 4))
4315 		return i9xx_plane == PLANE_C;
4316 	else
4317 		return i9xx_plane == PLANE_B ||
4318 			i9xx_plane == PLANE_C;
4319 }
4320 
4321 static int
4322 i9xx_plane_check(struct intel_crtc_state *crtc_state,
4323 		 struct intel_plane_state *plane_state)
4324 {
4325 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4326 	int ret;
4327 
4328 	ret = chv_plane_check_rotation(plane_state);
4329 	if (ret)
4330 		return ret;
4331 
4332 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
4333 						  &crtc_state->uapi,
4334 						  DRM_PLANE_HELPER_NO_SCALING,
4335 						  DRM_PLANE_HELPER_NO_SCALING,
4336 						  i9xx_plane_has_windowing(plane),
4337 						  true);
4338 	if (ret)
4339 		return ret;
4340 
4341 	ret = i9xx_check_plane_surface(plane_state);
4342 	if (ret)
4343 		return ret;
4344 
4345 	if (!plane_state->uapi.visible)
4346 		return 0;
4347 
4348 	ret = intel_plane_check_src_coordinates(plane_state);
4349 	if (ret)
4350 		return ret;
4351 
4352 	plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4353 
4354 	return 0;
4355 }
4356 
4357 static void i9xx_update_plane(struct intel_plane *plane,
4358 			      const struct intel_crtc_state *crtc_state,
4359 			      const struct intel_plane_state *plane_state)
4360 {
4361 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4362 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4363 	u32 linear_offset;
4364 	int x = plane_state->color_plane[0].x;
4365 	int y = plane_state->color_plane[0].y;
4366 	int crtc_x = plane_state->uapi.dst.x1;
4367 	int crtc_y = plane_state->uapi.dst.y1;
4368 	int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4369 	int crtc_h = drm_rect_height(&plane_state->uapi.dst);
4370 	unsigned long irqflags;
4371 	u32 dspaddr_offset;
4372 	u32 dspcntr;
4373 
4374 	dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
4375 
4376 	linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
4377 
4378 	if (INTEL_GEN(dev_priv) >= 4)
4379 		dspaddr_offset = plane_state->color_plane[0].offset;
4380 	else
4381 		dspaddr_offset = linear_offset;
4382 
4383 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4384 
4385 	intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4386 			  plane_state->color_plane[0].stride);
4387 
4388 	if (INTEL_GEN(dev_priv) < 4) {
4389 		/*
4390 		 * PLANE_A doesn't actually have a full window
4391 		 * generator but let's assume we still need to
4392 		 * program whatever is there.
4393 		 */
4394 		intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4395 				  (crtc_y << 16) | crtc_x);
4396 		intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4397 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4398 	} else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
4399 		intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4400 				  (crtc_y << 16) | crtc_x);
4401 		intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4402 				  ((crtc_h - 1) << 16) | (crtc_w - 1));
4403 		intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
4404 	}
4405 
4406 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
4407 		intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4408 				  (y << 16) | x);
4409 	} else if (INTEL_GEN(dev_priv) >= 4) {
4410 		intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4411 				  linear_offset);
4412 		intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4413 				  (y << 16) | x);
4414 	}
4415 
4416 	/*
4417 	 * The control register self-arms if the plane was previously
4418 	 * disabled. Try to make the plane enable atomic by writing
4419 	 * the control register just before the surface register.
4420 	 */
4421 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4422 	if (INTEL_GEN(dev_priv) >= 4)
4423 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4424 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4425 	else
4426 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4427 				  intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4428 
4429 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4430 }
4431 
4432 static void i9xx_disable_plane(struct intel_plane *plane,
4433 			       const struct intel_crtc_state *crtc_state)
4434 {
4435 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4436 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4437 	unsigned long irqflags;
4438 	u32 dspcntr;
4439 
4440 	/*
4441 	 * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4442 	 * enable on ilk+ affect the pipe bottom color as
4443 	 * well, so we must configure them even if the plane
4444 	 * is disabled.
4445 	 *
4446 	 * On pre-g4x there is no way to gamma correct the
4447 	 * pipe bottom color but we'll keep on doing this
4448 	 * anyway so that the crtc state readout works correctly.
4449 	 */
4450 	dspcntr = i9xx_plane_ctl_crtc(crtc_state);
4451 
4452 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4453 
4454 	intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4455 	if (INTEL_GEN(dev_priv) >= 4)
4456 		intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
4457 	else
4458 		intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
4459 
4460 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4461 }
4462 
4463 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4464 				    enum pipe *pipe)
4465 {
4466 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4467 	enum intel_display_power_domain power_domain;
4468 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4469 	intel_wakeref_t wakeref;
4470 	bool ret;
4471 	u32 val;
4472 
4473 	/*
4474 	 * Not 100% correct for planes that can move between pipes,
4475 	 * but that's only the case for gen2-4 which don't have any
4476 	 * display power wells.
4477 	 */
4478 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
4479 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4480 	if (!wakeref)
4481 		return false;
4482 
4483 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4484 
4485 	ret = val & DISPLAY_PLANE_ENABLE;
4486 
4487 	if (INTEL_GEN(dev_priv) >= 5)
4488 		*pipe = plane->pipe;
4489 	else
4490 		*pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4491 			DISPPLANE_SEL_PIPE_SHIFT;
4492 
4493 	intel_display_power_put(dev_priv, power_domain, wakeref);
4494 
4495 	return ret;
4496 }
4497 
4498 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4499 {
4500 	struct drm_device *dev = intel_crtc->base.dev;
4501 	struct drm_i915_private *dev_priv = to_i915(dev);
4502 	unsigned long irqflags;
4503 
4504 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4505 
4506 	intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4507 	intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4508 	intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
4509 
4510 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4511 }
4512 
4513 /*
4514  * This function detaches (aka. unbinds) unused scalers in hardware
4515  */
4516 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
4517 {
4518 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4519 	const struct intel_crtc_scaler_state *scaler_state =
4520 		&crtc_state->scaler_state;
4521 	int i;
4522 
4523 	/* loop through and disable scalers that aren't in use */
4524 	for (i = 0; i < intel_crtc->num_scalers; i++) {
4525 		if (!scaler_state->scalers[i].in_use)
4526 			skl_detach_scaler(intel_crtc, i);
4527 	}
4528 }
4529 
4530 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4531 					  int color_plane, unsigned int rotation)
4532 {
4533 	/*
4534 	 * The stride is either expressed as a multiple of 64 bytes chunks for
4535 	 * linear buffers or in number of tiles for tiled buffers.
4536 	 */
4537 	if (is_surface_linear(fb, color_plane))
4538 		return 64;
4539 	else if (drm_rotation_90_or_270(rotation))
4540 		return intel_tile_height(fb, color_plane);
4541 	else
4542 		return intel_tile_width_bytes(fb, color_plane);
4543 }
4544 
4545 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
4546 		     int color_plane)
4547 {
4548 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4549 	unsigned int rotation = plane_state->hw.rotation;
4550 	u32 stride = plane_state->color_plane[color_plane].stride;
4551 
4552 	if (color_plane >= fb->format->num_planes)
4553 		return 0;
4554 
4555 	return stride / skl_plane_stride_mult(fb, color_plane, rotation);
4556 }
4557 
4558 static u32 skl_plane_ctl_format(u32 pixel_format)
4559 {
4560 	switch (pixel_format) {
4561 	case DRM_FORMAT_C8:
4562 		return PLANE_CTL_FORMAT_INDEXED;
4563 	case DRM_FORMAT_RGB565:
4564 		return PLANE_CTL_FORMAT_RGB_565;
4565 	case DRM_FORMAT_XBGR8888:
4566 	case DRM_FORMAT_ABGR8888:
4567 		return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
4568 	case DRM_FORMAT_XRGB8888:
4569 	case DRM_FORMAT_ARGB8888:
4570 		return PLANE_CTL_FORMAT_XRGB_8888;
4571 	case DRM_FORMAT_XBGR2101010:
4572 	case DRM_FORMAT_ABGR2101010:
4573 		return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
4574 	case DRM_FORMAT_XRGB2101010:
4575 	case DRM_FORMAT_ARGB2101010:
4576 		return PLANE_CTL_FORMAT_XRGB_2101010;
4577 	case DRM_FORMAT_XBGR16161616F:
4578 	case DRM_FORMAT_ABGR16161616F:
4579 		return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4580 	case DRM_FORMAT_XRGB16161616F:
4581 	case DRM_FORMAT_ARGB16161616F:
4582 		return PLANE_CTL_FORMAT_XRGB_16161616F;
4583 	case DRM_FORMAT_YUYV:
4584 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
4585 	case DRM_FORMAT_YVYU:
4586 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
4587 	case DRM_FORMAT_UYVY:
4588 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
4589 	case DRM_FORMAT_VYUY:
4590 		return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
4591 	case DRM_FORMAT_NV12:
4592 		return PLANE_CTL_FORMAT_NV12;
4593 	case DRM_FORMAT_P010:
4594 		return PLANE_CTL_FORMAT_P010;
4595 	case DRM_FORMAT_P012:
4596 		return PLANE_CTL_FORMAT_P012;
4597 	case DRM_FORMAT_P016:
4598 		return PLANE_CTL_FORMAT_P016;
4599 	case DRM_FORMAT_Y210:
4600 		return PLANE_CTL_FORMAT_Y210;
4601 	case DRM_FORMAT_Y212:
4602 		return PLANE_CTL_FORMAT_Y212;
4603 	case DRM_FORMAT_Y216:
4604 		return PLANE_CTL_FORMAT_Y216;
4605 	case DRM_FORMAT_XVYU2101010:
4606 		return PLANE_CTL_FORMAT_Y410;
4607 	case DRM_FORMAT_XVYU12_16161616:
4608 		return PLANE_CTL_FORMAT_Y412;
4609 	case DRM_FORMAT_XVYU16161616:
4610 		return PLANE_CTL_FORMAT_Y416;
4611 	default:
4612 		MISSING_CASE(pixel_format);
4613 	}
4614 
4615 	return 0;
4616 }
4617 
4618 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4619 {
4620 	if (!plane_state->hw.fb->format->has_alpha)
4621 		return PLANE_CTL_ALPHA_DISABLE;
4622 
4623 	switch (plane_state->hw.pixel_blend_mode) {
4624 	case DRM_MODE_BLEND_PIXEL_NONE:
4625 		return PLANE_CTL_ALPHA_DISABLE;
4626 	case DRM_MODE_BLEND_PREMULTI:
4627 		return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4628 	case DRM_MODE_BLEND_COVERAGE:
4629 		return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4630 	default:
4631 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4632 		return PLANE_CTL_ALPHA_DISABLE;
4633 	}
4634 }
4635 
4636 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4637 {
4638 	if (!plane_state->hw.fb->format->has_alpha)
4639 		return PLANE_COLOR_ALPHA_DISABLE;
4640 
4641 	switch (plane_state->hw.pixel_blend_mode) {
4642 	case DRM_MODE_BLEND_PIXEL_NONE:
4643 		return PLANE_COLOR_ALPHA_DISABLE;
4644 	case DRM_MODE_BLEND_PREMULTI:
4645 		return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4646 	case DRM_MODE_BLEND_COVERAGE:
4647 		return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4648 	default:
4649 		MISSING_CASE(plane_state->hw.pixel_blend_mode);
4650 		return PLANE_COLOR_ALPHA_DISABLE;
4651 	}
4652 }
4653 
4654 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4655 {
4656 	switch (fb_modifier) {
4657 	case DRM_FORMAT_MOD_LINEAR:
4658 		break;
4659 	case I915_FORMAT_MOD_X_TILED:
4660 		return PLANE_CTL_TILED_X;
4661 	case I915_FORMAT_MOD_Y_TILED:
4662 		return PLANE_CTL_TILED_Y;
4663 	case I915_FORMAT_MOD_Y_TILED_CCS:
4664 		return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4665 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4666 		return PLANE_CTL_TILED_Y |
4667 		       PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4668 		       PLANE_CTL_CLEAR_COLOR_DISABLE;
4669 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4670 		return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
4671 	case I915_FORMAT_MOD_Yf_TILED:
4672 		return PLANE_CTL_TILED_YF;
4673 	case I915_FORMAT_MOD_Yf_TILED_CCS:
4674 		return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4675 	default:
4676 		MISSING_CASE(fb_modifier);
4677 	}
4678 
4679 	return 0;
4680 }
4681 
4682 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4683 {
4684 	switch (rotate) {
4685 	case DRM_MODE_ROTATE_0:
4686 		break;
4687 	/*
4688 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4689 	 * while i915 HW rotation is clockwise, thats why this swapping.
4690 	 */
4691 	case DRM_MODE_ROTATE_90:
4692 		return PLANE_CTL_ROTATE_270;
4693 	case DRM_MODE_ROTATE_180:
4694 		return PLANE_CTL_ROTATE_180;
4695 	case DRM_MODE_ROTATE_270:
4696 		return PLANE_CTL_ROTATE_90;
4697 	default:
4698 		MISSING_CASE(rotate);
4699 	}
4700 
4701 	return 0;
4702 }
4703 
4704 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4705 {
4706 	switch (reflect) {
4707 	case 0:
4708 		break;
4709 	case DRM_MODE_REFLECT_X:
4710 		return PLANE_CTL_FLIP_HORIZONTAL;
4711 	case DRM_MODE_REFLECT_Y:
4712 	default:
4713 		MISSING_CASE(reflect);
4714 	}
4715 
4716 	return 0;
4717 }
4718 
4719 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4720 {
4721 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4722 	u32 plane_ctl = 0;
4723 
4724 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4725 		return plane_ctl;
4726 
4727 	if (crtc_state->gamma_enable)
4728 		plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4729 
4730 	if (crtc_state->csc_enable)
4731 		plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4732 
4733 	return plane_ctl;
4734 }
4735 
4736 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4737 		  const struct intel_plane_state *plane_state)
4738 {
4739 	struct drm_i915_private *dev_priv =
4740 		to_i915(plane_state->uapi.plane->dev);
4741 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4742 	unsigned int rotation = plane_state->hw.rotation;
4743 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4744 	u32 plane_ctl;
4745 
4746 	plane_ctl = PLANE_CTL_ENABLE;
4747 
4748 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4749 		plane_ctl |= skl_plane_ctl_alpha(plane_state);
4750 		plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4751 
4752 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4753 			plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4754 
4755 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4756 			plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4757 	}
4758 
4759 	plane_ctl |= skl_plane_ctl_format(fb->format->format);
4760 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4761 	plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4762 
4763 	if (INTEL_GEN(dev_priv) >= 10)
4764 		plane_ctl |= cnl_plane_ctl_flip(rotation &
4765 						DRM_MODE_REFLECT_MASK);
4766 
4767 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
4768 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4769 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
4770 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4771 
4772 	return plane_ctl;
4773 }
4774 
4775 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4776 {
4777 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4778 	u32 plane_color_ctl = 0;
4779 
4780 	if (INTEL_GEN(dev_priv) >= 11)
4781 		return plane_color_ctl;
4782 
4783 	if (crtc_state->gamma_enable)
4784 		plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4785 
4786 	if (crtc_state->csc_enable)
4787 		plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4788 
4789 	return plane_color_ctl;
4790 }
4791 
4792 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4793 			const struct intel_plane_state *plane_state)
4794 {
4795 	struct drm_i915_private *dev_priv =
4796 		to_i915(plane_state->uapi.plane->dev);
4797 	const struct drm_framebuffer *fb = plane_state->hw.fb;
4798 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4799 	u32 plane_color_ctl = 0;
4800 
4801 	plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4802 	plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4803 
4804 	if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4805 		if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4806 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4807 		else
4808 			plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
4809 
4810 		if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4811 			plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4812 	} else if (fb->format->is_yuv) {
4813 		plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4814 	}
4815 
4816 	return plane_color_ctl;
4817 }
4818 
4819 static int
4820 __intel_display_resume(struct drm_device *dev,
4821 		       struct drm_atomic_state *state,
4822 		       struct drm_modeset_acquire_ctx *ctx)
4823 {
4824 	struct drm_crtc_state *crtc_state;
4825 	struct drm_crtc *crtc;
4826 	int i, ret;
4827 
4828 	intel_modeset_setup_hw_state(dev, ctx);
4829 	intel_vga_redisable(to_i915(dev));
4830 
4831 	if (!state)
4832 		return 0;
4833 
4834 	/*
4835 	 * We've duplicated the state, pointers to the old state are invalid.
4836 	 *
4837 	 * Don't attempt to use the old state until we commit the duplicated state.
4838 	 */
4839 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4840 		/*
4841 		 * Force recalculation even if we restore
4842 		 * current state. With fast modeset this may not result
4843 		 * in a modeset when the state is compatible.
4844 		 */
4845 		crtc_state->mode_changed = true;
4846 	}
4847 
4848 	/* ignore any reset values/BIOS leftovers in the WM registers */
4849 	if (!HAS_GMCH(to_i915(dev)))
4850 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
4851 
4852 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4853 
4854 	drm_WARN_ON(dev, ret == -EDEADLK);
4855 	return ret;
4856 }
4857 
4858 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4859 {
4860 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4861 		intel_has_gpu_reset(&dev_priv->gt));
4862 }
4863 
4864 void intel_prepare_reset(struct drm_i915_private *dev_priv)
4865 {
4866 	struct drm_device *dev = &dev_priv->drm;
4867 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4868 	struct drm_atomic_state *state;
4869 	int ret;
4870 
4871 	/* reset doesn't touch the display */
4872 	if (!i915_modparams.force_reset_modeset_test &&
4873 	    !gpu_reset_clobbers_display(dev_priv))
4874 		return;
4875 
4876 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
4877 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4878 	smp_mb__after_atomic();
4879 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4880 
4881 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4882 		drm_dbg_kms(&dev_priv->drm,
4883 			    "Modeset potentially stuck, unbreaking through wedging\n");
4884 		intel_gt_set_wedged(&dev_priv->gt);
4885 	}
4886 
4887 	/*
4888 	 * Need mode_config.mutex so that we don't
4889 	 * trample ongoing ->detect() and whatnot.
4890 	 */
4891 	mutex_lock(&dev->mode_config.mutex);
4892 	drm_modeset_acquire_init(ctx, 0);
4893 	while (1) {
4894 		ret = drm_modeset_lock_all_ctx(dev, ctx);
4895 		if (ret != -EDEADLK)
4896 			break;
4897 
4898 		drm_modeset_backoff(ctx);
4899 	}
4900 	/*
4901 	 * Disabling the crtcs gracefully seems nicer. Also the
4902 	 * g33 docs say we should at least disable all the planes.
4903 	 */
4904 	state = drm_atomic_helper_duplicate_state(dev, ctx);
4905 	if (IS_ERR(state)) {
4906 		ret = PTR_ERR(state);
4907 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4908 			ret);
4909 		return;
4910 	}
4911 
4912 	ret = drm_atomic_helper_disable_all(dev, ctx);
4913 	if (ret) {
4914 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4915 			ret);
4916 		drm_atomic_state_put(state);
4917 		return;
4918 	}
4919 
4920 	dev_priv->modeset_restore_state = state;
4921 	state->acquire_ctx = ctx;
4922 }
4923 
4924 void intel_finish_reset(struct drm_i915_private *dev_priv)
4925 {
4926 	struct drm_device *dev = &dev_priv->drm;
4927 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4928 	struct drm_atomic_state *state;
4929 	int ret;
4930 
4931 	/* reset doesn't touch the display */
4932 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
4933 		return;
4934 
4935 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
4936 	if (!state)
4937 		goto unlock;
4938 
4939 	/* reset doesn't touch the display */
4940 	if (!gpu_reset_clobbers_display(dev_priv)) {
4941 		/* for testing only restore the display */
4942 		ret = __intel_display_resume(dev, state, ctx);
4943 		if (ret)
4944 			drm_err(&dev_priv->drm,
4945 				"Restoring old state failed with %i\n", ret);
4946 	} else {
4947 		/*
4948 		 * The display has been reset as well,
4949 		 * so need a full re-initialization.
4950 		 */
4951 		intel_pps_unlock_regs_wa(dev_priv);
4952 		intel_modeset_init_hw(dev_priv);
4953 		intel_init_clock_gating(dev_priv);
4954 
4955 		spin_lock_irq(&dev_priv->irq_lock);
4956 		if (dev_priv->display.hpd_irq_setup)
4957 			dev_priv->display.hpd_irq_setup(dev_priv);
4958 		spin_unlock_irq(&dev_priv->irq_lock);
4959 
4960 		ret = __intel_display_resume(dev, state, ctx);
4961 		if (ret)
4962 			drm_err(&dev_priv->drm,
4963 				"Restoring old state failed with %i\n", ret);
4964 
4965 		intel_hpd_init(dev_priv);
4966 	}
4967 
4968 	drm_atomic_state_put(state);
4969 unlock:
4970 	drm_modeset_drop_locks(ctx);
4971 	drm_modeset_acquire_fini(ctx);
4972 	mutex_unlock(&dev->mode_config.mutex);
4973 
4974 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4975 }
4976 
4977 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4978 {
4979 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4980 	enum pipe pipe = crtc->pipe;
4981 	u32 tmp;
4982 
4983 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
4984 
4985 	/*
4986 	 * Display WA #1153: icl
4987 	 * enable hardware to bypass the alpha math
4988 	 * and rounding for per-pixel values 00 and 0xff
4989 	 */
4990 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
4991 	/*
4992 	 * Display WA # 1605353570: icl
4993 	 * Set the pixel rounding bit to 1 for allowing
4994 	 * passthrough of Frame buffer pixels unmodified
4995 	 * across pipe
4996 	 */
4997 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
4998 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
4999 }
5000 
5001 static void icl_enable_trans_port_sync(const struct intel_crtc_state *crtc_state)
5002 {
5003 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5004 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5005 	u32 trans_ddi_func_ctl2_val;
5006 	u8 master_select;
5007 
5008 	/*
5009 	 * Configure the master select and enable Transcoder Port Sync for
5010 	 * Slave CRTCs transcoder.
5011 	 */
5012 	if (crtc_state->master_transcoder == INVALID_TRANSCODER)
5013 		return;
5014 
5015 	if (crtc_state->master_transcoder == TRANSCODER_EDP)
5016 		master_select = 0;
5017 	else
5018 		master_select = crtc_state->master_transcoder + 1;
5019 
5020 	/* Set the master select bits for Tranascoder Port Sync */
5021 	trans_ddi_func_ctl2_val = (PORT_SYNC_MODE_MASTER_SELECT(master_select) &
5022 				   PORT_SYNC_MODE_MASTER_SELECT_MASK) <<
5023 		PORT_SYNC_MODE_MASTER_SELECT_SHIFT;
5024 	/* Enable Transcoder Port Sync */
5025 	trans_ddi_func_ctl2_val |= PORT_SYNC_MODE_ENABLE;
5026 
5027 	intel_de_write(dev_priv,
5028 		       TRANS_DDI_FUNC_CTL2(crtc_state->cpu_transcoder),
5029 		       trans_ddi_func_ctl2_val);
5030 }
5031 
5032 static void intel_fdi_normal_train(struct intel_crtc *crtc)
5033 {
5034 	struct drm_device *dev = crtc->base.dev;
5035 	struct drm_i915_private *dev_priv = to_i915(dev);
5036 	enum pipe pipe = crtc->pipe;
5037 	i915_reg_t reg;
5038 	u32 temp;
5039 
5040 	/* enable normal train */
5041 	reg = FDI_TX_CTL(pipe);
5042 	temp = intel_de_read(dev_priv, reg);
5043 	if (IS_IVYBRIDGE(dev_priv)) {
5044 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5045 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
5046 	} else {
5047 		temp &= ~FDI_LINK_TRAIN_NONE;
5048 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
5049 	}
5050 	intel_de_write(dev_priv, reg, temp);
5051 
5052 	reg = FDI_RX_CTL(pipe);
5053 	temp = intel_de_read(dev_priv, reg);
5054 	if (HAS_PCH_CPT(dev_priv)) {
5055 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5056 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5057 	} else {
5058 		temp &= ~FDI_LINK_TRAIN_NONE;
5059 		temp |= FDI_LINK_TRAIN_NONE;
5060 	}
5061 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5062 
5063 	/* wait one idle pattern time */
5064 	intel_de_posting_read(dev_priv, reg);
5065 	udelay(1000);
5066 
5067 	/* IVB wants error correction enabled */
5068 	if (IS_IVYBRIDGE(dev_priv))
5069 		intel_de_write(dev_priv, reg,
5070 		               intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5071 }
5072 
5073 /* The FDI link training functions for ILK/Ibexpeak. */
5074 static void ilk_fdi_link_train(struct intel_crtc *crtc,
5075 			       const struct intel_crtc_state *crtc_state)
5076 {
5077 	struct drm_device *dev = crtc->base.dev;
5078 	struct drm_i915_private *dev_priv = to_i915(dev);
5079 	enum pipe pipe = crtc->pipe;
5080 	i915_reg_t reg;
5081 	u32 temp, tries;
5082 
5083 	/* FDI needs bits from pipe first */
5084 	assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
5085 
5086 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5087 	   for train result */
5088 	reg = FDI_RX_IMR(pipe);
5089 	temp = intel_de_read(dev_priv, reg);
5090 	temp &= ~FDI_RX_SYMBOL_LOCK;
5091 	temp &= ~FDI_RX_BIT_LOCK;
5092 	intel_de_write(dev_priv, reg, temp);
5093 	intel_de_read(dev_priv, reg);
5094 	udelay(150);
5095 
5096 	/* enable CPU FDI TX and PCH FDI RX */
5097 	reg = FDI_TX_CTL(pipe);
5098 	temp = intel_de_read(dev_priv, reg);
5099 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5100 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5101 	temp &= ~FDI_LINK_TRAIN_NONE;
5102 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5103 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5104 
5105 	reg = FDI_RX_CTL(pipe);
5106 	temp = intel_de_read(dev_priv, reg);
5107 	temp &= ~FDI_LINK_TRAIN_NONE;
5108 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5109 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5110 
5111 	intel_de_posting_read(dev_priv, reg);
5112 	udelay(150);
5113 
5114 	/* Ironlake workaround, enable clock pointer after FDI enable*/
5115 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5116 		       FDI_RX_PHASE_SYNC_POINTER_OVR);
5117 	intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5118 		       FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5119 
5120 	reg = FDI_RX_IIR(pipe);
5121 	for (tries = 0; tries < 5; tries++) {
5122 		temp = intel_de_read(dev_priv, reg);
5123 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5124 
5125 		if ((temp & FDI_RX_BIT_LOCK)) {
5126 			drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
5127 			intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
5128 			break;
5129 		}
5130 	}
5131 	if (tries == 5)
5132 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5133 
5134 	/* Train 2 */
5135 	reg = FDI_TX_CTL(pipe);
5136 	temp = intel_de_read(dev_priv, reg);
5137 	temp &= ~FDI_LINK_TRAIN_NONE;
5138 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5139 	intel_de_write(dev_priv, reg, temp);
5140 
5141 	reg = FDI_RX_CTL(pipe);
5142 	temp = intel_de_read(dev_priv, reg);
5143 	temp &= ~FDI_LINK_TRAIN_NONE;
5144 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5145 	intel_de_write(dev_priv, reg, temp);
5146 
5147 	intel_de_posting_read(dev_priv, reg);
5148 	udelay(150);
5149 
5150 	reg = FDI_RX_IIR(pipe);
5151 	for (tries = 0; tries < 5; tries++) {
5152 		temp = intel_de_read(dev_priv, reg);
5153 		drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5154 
5155 		if (temp & FDI_RX_SYMBOL_LOCK) {
5156 			intel_de_write(dev_priv, reg,
5157 				       temp | FDI_RX_SYMBOL_LOCK);
5158 			drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
5159 			break;
5160 		}
5161 	}
5162 	if (tries == 5)
5163 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5164 
5165 	drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5166 
5167 }
5168 
5169 static const int snb_b_fdi_train_param[] = {
5170 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5171 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5172 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5173 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5174 };
5175 
5176 /* The FDI link training functions for SNB/Cougarpoint. */
5177 static void gen6_fdi_link_train(struct intel_crtc *crtc,
5178 				const struct intel_crtc_state *crtc_state)
5179 {
5180 	struct drm_device *dev = crtc->base.dev;
5181 	struct drm_i915_private *dev_priv = to_i915(dev);
5182 	enum pipe pipe = crtc->pipe;
5183 	i915_reg_t reg;
5184 	u32 temp, i, retry;
5185 
5186 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5187 	   for train result */
5188 	reg = FDI_RX_IMR(pipe);
5189 	temp = intel_de_read(dev_priv, reg);
5190 	temp &= ~FDI_RX_SYMBOL_LOCK;
5191 	temp &= ~FDI_RX_BIT_LOCK;
5192 	intel_de_write(dev_priv, reg, temp);
5193 
5194 	intel_de_posting_read(dev_priv, reg);
5195 	udelay(150);
5196 
5197 	/* enable CPU FDI TX and PCH FDI RX */
5198 	reg = FDI_TX_CTL(pipe);
5199 	temp = intel_de_read(dev_priv, reg);
5200 	temp &= ~FDI_DP_PORT_WIDTH_MASK;
5201 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5202 	temp &= ~FDI_LINK_TRAIN_NONE;
5203 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5204 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5205 	/* SNB-B */
5206 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5207 	intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5208 
5209 	intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5210 		       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5211 
5212 	reg = FDI_RX_CTL(pipe);
5213 	temp = intel_de_read(dev_priv, reg);
5214 	if (HAS_PCH_CPT(dev_priv)) {
5215 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5216 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5217 	} else {
5218 		temp &= ~FDI_LINK_TRAIN_NONE;
5219 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5220 	}
5221 	intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5222 
5223 	intel_de_posting_read(dev_priv, reg);
5224 	udelay(150);
5225 
5226 	for (i = 0; i < 4; i++) {
5227 		reg = FDI_TX_CTL(pipe);
5228 		temp = intel_de_read(dev_priv, reg);
5229 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5230 		temp |= snb_b_fdi_train_param[i];
5231 		intel_de_write(dev_priv, reg, temp);
5232 
5233 		intel_de_posting_read(dev_priv, reg);
5234 		udelay(500);
5235 
5236 		for (retry = 0; retry < 5; retry++) {
5237 			reg = FDI_RX_IIR(pipe);
5238 			temp = intel_de_read(dev_priv, reg);
5239 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5240 			if (temp & FDI_RX_BIT_LOCK) {
5241 				intel_de_write(dev_priv, reg,
5242 					       temp | FDI_RX_BIT_LOCK);
5243 				drm_dbg_kms(&dev_priv->drm,
5244 					    "FDI train 1 done.\n");
5245 				break;
5246 			}
5247 			udelay(50);
5248 		}
5249 		if (retry < 5)
5250 			break;
5251 	}
5252 	if (i == 4)
5253 		drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5254 
5255 	/* Train 2 */
5256 	reg = FDI_TX_CTL(pipe);
5257 	temp = intel_de_read(dev_priv, reg);
5258 	temp &= ~FDI_LINK_TRAIN_NONE;
5259 	temp |= FDI_LINK_TRAIN_PATTERN_2;
5260 	if (IS_GEN(dev_priv, 6)) {
5261 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5262 		/* SNB-B */
5263 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5264 	}
5265 	intel_de_write(dev_priv, reg, temp);
5266 
5267 	reg = FDI_RX_CTL(pipe);
5268 	temp = intel_de_read(dev_priv, reg);
5269 	if (HAS_PCH_CPT(dev_priv)) {
5270 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5271 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5272 	} else {
5273 		temp &= ~FDI_LINK_TRAIN_NONE;
5274 		temp |= FDI_LINK_TRAIN_PATTERN_2;
5275 	}
5276 	intel_de_write(dev_priv, reg, temp);
5277 
5278 	intel_de_posting_read(dev_priv, reg);
5279 	udelay(150);
5280 
5281 	for (i = 0; i < 4; i++) {
5282 		reg = FDI_TX_CTL(pipe);
5283 		temp = intel_de_read(dev_priv, reg);
5284 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5285 		temp |= snb_b_fdi_train_param[i];
5286 		intel_de_write(dev_priv, reg, temp);
5287 
5288 		intel_de_posting_read(dev_priv, reg);
5289 		udelay(500);
5290 
5291 		for (retry = 0; retry < 5; retry++) {
5292 			reg = FDI_RX_IIR(pipe);
5293 			temp = intel_de_read(dev_priv, reg);
5294 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5295 			if (temp & FDI_RX_SYMBOL_LOCK) {
5296 				intel_de_write(dev_priv, reg,
5297 					       temp | FDI_RX_SYMBOL_LOCK);
5298 				drm_dbg_kms(&dev_priv->drm,
5299 					    "FDI train 2 done.\n");
5300 				break;
5301 			}
5302 			udelay(50);
5303 		}
5304 		if (retry < 5)
5305 			break;
5306 	}
5307 	if (i == 4)
5308 		drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5309 
5310 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5311 }
5312 
5313 /* Manual link training for Ivy Bridge A0 parts */
5314 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5315 				      const struct intel_crtc_state *crtc_state)
5316 {
5317 	struct drm_device *dev = crtc->base.dev;
5318 	struct drm_i915_private *dev_priv = to_i915(dev);
5319 	enum pipe pipe = crtc->pipe;
5320 	i915_reg_t reg;
5321 	u32 temp, i, j;
5322 
5323 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5324 	   for train result */
5325 	reg = FDI_RX_IMR(pipe);
5326 	temp = intel_de_read(dev_priv, reg);
5327 	temp &= ~FDI_RX_SYMBOL_LOCK;
5328 	temp &= ~FDI_RX_BIT_LOCK;
5329 	intel_de_write(dev_priv, reg, temp);
5330 
5331 	intel_de_posting_read(dev_priv, reg);
5332 	udelay(150);
5333 
5334 	drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
5335 		    intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
5336 
5337 	/* Try each vswing and preemphasis setting twice before moving on */
5338 	for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5339 		/* disable first in case we need to retry */
5340 		reg = FDI_TX_CTL(pipe);
5341 		temp = intel_de_read(dev_priv, reg);
5342 		temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5343 		temp &= ~FDI_TX_ENABLE;
5344 		intel_de_write(dev_priv, reg, temp);
5345 
5346 		reg = FDI_RX_CTL(pipe);
5347 		temp = intel_de_read(dev_priv, reg);
5348 		temp &= ~FDI_LINK_TRAIN_AUTO;
5349 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5350 		temp &= ~FDI_RX_ENABLE;
5351 		intel_de_write(dev_priv, reg, temp);
5352 
5353 		/* enable CPU FDI TX and PCH FDI RX */
5354 		reg = FDI_TX_CTL(pipe);
5355 		temp = intel_de_read(dev_priv, reg);
5356 		temp &= ~FDI_DP_PORT_WIDTH_MASK;
5357 		temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5358 		temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
5359 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5360 		temp |= snb_b_fdi_train_param[j/2];
5361 		temp |= FDI_COMPOSITE_SYNC;
5362 		intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5363 
5364 		intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5365 			       FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5366 
5367 		reg = FDI_RX_CTL(pipe);
5368 		temp = intel_de_read(dev_priv, reg);
5369 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5370 		temp |= FDI_COMPOSITE_SYNC;
5371 		intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5372 
5373 		intel_de_posting_read(dev_priv, reg);
5374 		udelay(1); /* should be 0.5us */
5375 
5376 		for (i = 0; i < 4; i++) {
5377 			reg = FDI_RX_IIR(pipe);
5378 			temp = intel_de_read(dev_priv, reg);
5379 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5380 
5381 			if (temp & FDI_RX_BIT_LOCK ||
5382 			    (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5383 				intel_de_write(dev_priv, reg,
5384 					       temp | FDI_RX_BIT_LOCK);
5385 				drm_dbg_kms(&dev_priv->drm,
5386 					    "FDI train 1 done, level %i.\n",
5387 					    i);
5388 				break;
5389 			}
5390 			udelay(1); /* should be 0.5us */
5391 		}
5392 		if (i == 4) {
5393 			drm_dbg_kms(&dev_priv->drm,
5394 				    "FDI train 1 fail on vswing %d\n", j / 2);
5395 			continue;
5396 		}
5397 
5398 		/* Train 2 */
5399 		reg = FDI_TX_CTL(pipe);
5400 		temp = intel_de_read(dev_priv, reg);
5401 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5402 		temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
5403 		intel_de_write(dev_priv, reg, temp);
5404 
5405 		reg = FDI_RX_CTL(pipe);
5406 		temp = intel_de_read(dev_priv, reg);
5407 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5408 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5409 		intel_de_write(dev_priv, reg, temp);
5410 
5411 		intel_de_posting_read(dev_priv, reg);
5412 		udelay(2); /* should be 1.5us */
5413 
5414 		for (i = 0; i < 4; i++) {
5415 			reg = FDI_RX_IIR(pipe);
5416 			temp = intel_de_read(dev_priv, reg);
5417 			drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5418 
5419 			if (temp & FDI_RX_SYMBOL_LOCK ||
5420 			    (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5421 				intel_de_write(dev_priv, reg,
5422 					       temp | FDI_RX_SYMBOL_LOCK);
5423 				drm_dbg_kms(&dev_priv->drm,
5424 					    "FDI train 2 done, level %i.\n",
5425 					    i);
5426 				goto train_done;
5427 			}
5428 			udelay(2); /* should be 1.5us */
5429 		}
5430 		if (i == 4)
5431 			drm_dbg_kms(&dev_priv->drm,
5432 				    "FDI train 2 fail on vswing %d\n", j / 2);
5433 	}
5434 
5435 train_done:
5436 	drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5437 }
5438 
5439 static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
5440 {
5441 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
5442 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5443 	enum pipe pipe = intel_crtc->pipe;
5444 	i915_reg_t reg;
5445 	u32 temp;
5446 
5447 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5448 	reg = FDI_RX_CTL(pipe);
5449 	temp = intel_de_read(dev_priv, reg);
5450 	temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
5451 	temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5452 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5453 	intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5454 
5455 	intel_de_posting_read(dev_priv, reg);
5456 	udelay(200);
5457 
5458 	/* Switch from Rawclk to PCDclk */
5459 	temp = intel_de_read(dev_priv, reg);
5460 	intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5461 
5462 	intel_de_posting_read(dev_priv, reg);
5463 	udelay(200);
5464 
5465 	/* Enable CPU FDI TX PLL, always on for Ironlake */
5466 	reg = FDI_TX_CTL(pipe);
5467 	temp = intel_de_read(dev_priv, reg);
5468 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
5469 		intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5470 
5471 		intel_de_posting_read(dev_priv, reg);
5472 		udelay(100);
5473 	}
5474 }
5475 
5476 static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
5477 {
5478 	struct drm_device *dev = intel_crtc->base.dev;
5479 	struct drm_i915_private *dev_priv = to_i915(dev);
5480 	enum pipe pipe = intel_crtc->pipe;
5481 	i915_reg_t reg;
5482 	u32 temp;
5483 
5484 	/* Switch from PCDclk to Rawclk */
5485 	reg = FDI_RX_CTL(pipe);
5486 	temp = intel_de_read(dev_priv, reg);
5487 	intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
5488 
5489 	/* Disable CPU FDI TX PLL */
5490 	reg = FDI_TX_CTL(pipe);
5491 	temp = intel_de_read(dev_priv, reg);
5492 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
5493 
5494 	intel_de_posting_read(dev_priv, reg);
5495 	udelay(100);
5496 
5497 	reg = FDI_RX_CTL(pipe);
5498 	temp = intel_de_read(dev_priv, reg);
5499 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
5500 
5501 	/* Wait for the clocks to turn off. */
5502 	intel_de_posting_read(dev_priv, reg);
5503 	udelay(100);
5504 }
5505 
5506 static void ilk_fdi_disable(struct intel_crtc *crtc)
5507 {
5508 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5509 	enum pipe pipe = crtc->pipe;
5510 	i915_reg_t reg;
5511 	u32 temp;
5512 
5513 	/* disable CPU FDI tx and PCH FDI rx */
5514 	reg = FDI_TX_CTL(pipe);
5515 	temp = intel_de_read(dev_priv, reg);
5516 	intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5517 	intel_de_posting_read(dev_priv, reg);
5518 
5519 	reg = FDI_RX_CTL(pipe);
5520 	temp = intel_de_read(dev_priv, reg);
5521 	temp &= ~(0x7 << 16);
5522 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5523 	intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
5524 
5525 	intel_de_posting_read(dev_priv, reg);
5526 	udelay(100);
5527 
5528 	/* Ironlake workaround, disable clock pointer after downing FDI */
5529 	if (HAS_PCH_IBX(dev_priv))
5530 		intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5531 			       FDI_RX_PHASE_SYNC_POINTER_OVR);
5532 
5533 	/* still set train pattern 1 */
5534 	reg = FDI_TX_CTL(pipe);
5535 	temp = intel_de_read(dev_priv, reg);
5536 	temp &= ~FDI_LINK_TRAIN_NONE;
5537 	temp |= FDI_LINK_TRAIN_PATTERN_1;
5538 	intel_de_write(dev_priv, reg, temp);
5539 
5540 	reg = FDI_RX_CTL(pipe);
5541 	temp = intel_de_read(dev_priv, reg);
5542 	if (HAS_PCH_CPT(dev_priv)) {
5543 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5544 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5545 	} else {
5546 		temp &= ~FDI_LINK_TRAIN_NONE;
5547 		temp |= FDI_LINK_TRAIN_PATTERN_1;
5548 	}
5549 	/* BPC in FDI rx is consistent with that in PIPECONF */
5550 	temp &= ~(0x07 << 16);
5551 	temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5552 	intel_de_write(dev_priv, reg, temp);
5553 
5554 	intel_de_posting_read(dev_priv, reg);
5555 	udelay(100);
5556 }
5557 
5558 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5559 {
5560 	struct drm_crtc *crtc;
5561 	bool cleanup_done;
5562 
5563 	drm_for_each_crtc(crtc, &dev_priv->drm) {
5564 		struct drm_crtc_commit *commit;
5565 		spin_lock(&crtc->commit_lock);
5566 		commit = list_first_entry_or_null(&crtc->commit_list,
5567 						  struct drm_crtc_commit, commit_entry);
5568 		cleanup_done = commit ?
5569 			try_wait_for_completion(&commit->cleanup_done) : true;
5570 		spin_unlock(&crtc->commit_lock);
5571 
5572 		if (cleanup_done)
5573 			continue;
5574 
5575 		drm_crtc_wait_one_vblank(crtc);
5576 
5577 		return true;
5578 	}
5579 
5580 	return false;
5581 }
5582 
5583 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
5584 {
5585 	u32 temp;
5586 
5587 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
5588 
5589 	mutex_lock(&dev_priv->sb_lock);
5590 
5591 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5592 	temp |= SBI_SSCCTL_DISABLE;
5593 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5594 
5595 	mutex_unlock(&dev_priv->sb_lock);
5596 }
5597 
5598 /* Program iCLKIP clock to the desired frequency */
5599 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
5600 {
5601 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5602 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5603 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
5604 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
5605 	u32 temp;
5606 
5607 	lpt_disable_iclkip(dev_priv);
5608 
5609 	/* The iCLK virtual clock root frequency is in MHz,
5610 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
5611 	 * divisors, it is necessary to divide one by another, so we
5612 	 * convert the virtual clock precision to KHz here for higher
5613 	 * precision.
5614 	 */
5615 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
5616 		u32 iclk_virtual_root_freq = 172800 * 1000;
5617 		u32 iclk_pi_range = 64;
5618 		u32 desired_divisor;
5619 
5620 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5621 						    clock << auxdiv);
5622 		divsel = (desired_divisor / iclk_pi_range) - 2;
5623 		phaseinc = desired_divisor % iclk_pi_range;
5624 
5625 		/*
5626 		 * Near 20MHz is a corner case which is
5627 		 * out of range for the 7-bit divisor
5628 		 */
5629 		if (divsel <= 0x7f)
5630 			break;
5631 	}
5632 
5633 	/* This should not happen with any sane values */
5634 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5635 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5636 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5637 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5638 
5639 	drm_dbg_kms(&dev_priv->drm,
5640 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5641 		    clock, auxdiv, divsel, phasedir, phaseinc);
5642 
5643 	mutex_lock(&dev_priv->sb_lock);
5644 
5645 	/* Program SSCDIVINTPHASE6 */
5646 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5647 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5648 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5649 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5650 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5651 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5652 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5653 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5654 
5655 	/* Program SSCAUXDIV */
5656 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5657 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5658 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5659 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5660 
5661 	/* Enable modulator and associated divider */
5662 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5663 	temp &= ~SBI_SSCCTL_DISABLE;
5664 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5665 
5666 	mutex_unlock(&dev_priv->sb_lock);
5667 
5668 	/* Wait for initialization time */
5669 	udelay(24);
5670 
5671 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5672 }
5673 
5674 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5675 {
5676 	u32 divsel, phaseinc, auxdiv;
5677 	u32 iclk_virtual_root_freq = 172800 * 1000;
5678 	u32 iclk_pi_range = 64;
5679 	u32 desired_divisor;
5680 	u32 temp;
5681 
5682 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5683 		return 0;
5684 
5685 	mutex_lock(&dev_priv->sb_lock);
5686 
5687 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5688 	if (temp & SBI_SSCCTL_DISABLE) {
5689 		mutex_unlock(&dev_priv->sb_lock);
5690 		return 0;
5691 	}
5692 
5693 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5694 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5695 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5696 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5697 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5698 
5699 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5700 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5701 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5702 
5703 	mutex_unlock(&dev_priv->sb_lock);
5704 
5705 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5706 
5707 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5708 				 desired_divisor << auxdiv);
5709 }
5710 
5711 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5712 					   enum pipe pch_transcoder)
5713 {
5714 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5715 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5716 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5717 
5718 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5719 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5720 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5721 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5722 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5723 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
5724 
5725 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5726 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5727 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5728 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5729 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5730 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5731 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5732 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
5733 }
5734 
5735 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5736 {
5737 	u32 temp;
5738 
5739 	temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
5740 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5741 		return;
5742 
5743 	drm_WARN_ON(&dev_priv->drm,
5744 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5745 		    FDI_RX_ENABLE);
5746 	drm_WARN_ON(&dev_priv->drm,
5747 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5748 		    FDI_RX_ENABLE);
5749 
5750 	temp &= ~FDI_BC_BIFURCATION_SELECT;
5751 	if (enable)
5752 		temp |= FDI_BC_BIFURCATION_SELECT;
5753 
5754 	drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5755 		    enable ? "en" : "dis");
5756 	intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5757 	intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
5758 }
5759 
5760 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5761 {
5762 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5763 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5764 
5765 	switch (crtc->pipe) {
5766 	case PIPE_A:
5767 		break;
5768 	case PIPE_B:
5769 		if (crtc_state->fdi_lanes > 2)
5770 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
5771 		else
5772 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
5773 
5774 		break;
5775 	case PIPE_C:
5776 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
5777 
5778 		break;
5779 	default:
5780 		BUG();
5781 	}
5782 }
5783 
5784 /*
5785  * Finds the encoder associated with the given CRTC. This can only be
5786  * used when we know that the CRTC isn't feeding multiple encoders!
5787  */
5788 static struct intel_encoder *
5789 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5790 			   const struct intel_crtc_state *crtc_state)
5791 {
5792 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5793 	const struct drm_connector_state *connector_state;
5794 	const struct drm_connector *connector;
5795 	struct intel_encoder *encoder = NULL;
5796 	int num_encoders = 0;
5797 	int i;
5798 
5799 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5800 		if (connector_state->crtc != &crtc->base)
5801 			continue;
5802 
5803 		encoder = to_intel_encoder(connector_state->best_encoder);
5804 		num_encoders++;
5805 	}
5806 
5807 	drm_WARN(encoder->base.dev, num_encoders != 1,
5808 		 "%d encoders for pipe %c\n",
5809 		 num_encoders, pipe_name(crtc->pipe));
5810 
5811 	return encoder;
5812 }
5813 
5814 /*
5815  * Enable PCH resources required for PCH ports:
5816  *   - PCH PLLs
5817  *   - FDI training & RX/TX
5818  *   - update transcoder timings
5819  *   - DP transcoding bits
5820  *   - transcoder
5821  */
5822 static void ilk_pch_enable(const struct intel_atomic_state *state,
5823 			   const struct intel_crtc_state *crtc_state)
5824 {
5825 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5826 	struct drm_device *dev = crtc->base.dev;
5827 	struct drm_i915_private *dev_priv = to_i915(dev);
5828 	enum pipe pipe = crtc->pipe;
5829 	u32 temp;
5830 
5831 	assert_pch_transcoder_disabled(dev_priv, pipe);
5832 
5833 	if (IS_IVYBRIDGE(dev_priv))
5834 		ivb_update_fdi_bc_bifurcation(crtc_state);
5835 
5836 	/* Write the TU size bits before fdi link training, so that error
5837 	 * detection works. */
5838 	intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5839 		       intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5840 
5841 	/* For PCH output, training FDI link */
5842 	dev_priv->display.fdi_link_train(crtc, crtc_state);
5843 
5844 	/* We need to program the right clock selection before writing the pixel
5845 	 * mutliplier into the DPLL. */
5846 	if (HAS_PCH_CPT(dev_priv)) {
5847 		u32 sel;
5848 
5849 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5850 		temp |= TRANS_DPLL_ENABLE(pipe);
5851 		sel = TRANS_DPLLB_SEL(pipe);
5852 		if (crtc_state->shared_dpll ==
5853 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5854 			temp |= sel;
5855 		else
5856 			temp &= ~sel;
5857 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
5858 	}
5859 
5860 	/* XXX: pch pll's can be enabled any time before we enable the PCH
5861 	 * transcoder, and we actually should do this to not upset any PCH
5862 	 * transcoder that already use the clock when we share it.
5863 	 *
5864 	 * Note that enable_shared_dpll tries to do the right thing, but
5865 	 * get_shared_dpll unconditionally resets the pll - we need that to have
5866 	 * the right LVDS enable sequence. */
5867 	intel_enable_shared_dpll(crtc_state);
5868 
5869 	/* set transcoder timing, panel must allow it */
5870 	assert_panel_unlocked(dev_priv, pipe);
5871 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
5872 
5873 	intel_fdi_normal_train(crtc);
5874 
5875 	/* For PCH DP, enable TRANS_DP_CTL */
5876 	if (HAS_PCH_CPT(dev_priv) &&
5877 	    intel_crtc_has_dp_encoder(crtc_state)) {
5878 		const struct drm_display_mode *adjusted_mode =
5879 			&crtc_state->hw.adjusted_mode;
5880 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5881 		i915_reg_t reg = TRANS_DP_CTL(pipe);
5882 		enum port port;
5883 
5884 		temp = intel_de_read(dev_priv, reg);
5885 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
5886 			  TRANS_DP_SYNC_MASK |
5887 			  TRANS_DP_BPC_MASK);
5888 		temp |= TRANS_DP_OUTPUT_ENABLE;
5889 		temp |= bpc << 9; /* same format but at 11:9 */
5890 
5891 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5892 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5893 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5894 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5895 
5896 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5897 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
5898 		temp |= TRANS_DP_PORT_SEL(port);
5899 
5900 		intel_de_write(dev_priv, reg, temp);
5901 	}
5902 
5903 	ilk_enable_pch_transcoder(crtc_state);
5904 }
5905 
5906 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
5907 {
5908 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5909 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5910 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5911 
5912 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5913 
5914 	lpt_program_iclkip(crtc_state);
5915 
5916 	/* Set transcoder timing. */
5917 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
5918 
5919 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5920 }
5921 
5922 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5923 			       enum pipe pipe)
5924 {
5925 	i915_reg_t dslreg = PIPEDSL(pipe);
5926 	u32 temp;
5927 
5928 	temp = intel_de_read(dev_priv, dslreg);
5929 	udelay(500);
5930 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5931 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
5932 			drm_err(&dev_priv->drm,
5933 				"mode set failed: pipe %c stuck\n",
5934 				pipe_name(pipe));
5935 	}
5936 }
5937 
5938 /*
5939  * The hardware phase 0.0 refers to the center of the pixel.
5940  * We want to start from the top/left edge which is phase
5941  * -0.5. That matches how the hardware calculates the scaling
5942  * factors (from top-left of the first pixel to bottom-right
5943  * of the last pixel, as opposed to the pixel centers).
5944  *
5945  * For 4:2:0 subsampled chroma planes we obviously have to
5946  * adjust that so that the chroma sample position lands in
5947  * the right spot.
5948  *
5949  * Note that for packed YCbCr 4:2:2 formats there is no way to
5950  * control chroma siting. The hardware simply replicates the
5951  * chroma samples for both of the luma samples, and thus we don't
5952  * actually get the expected MPEG2 chroma siting convention :(
5953  * The same behaviour is observed on pre-SKL platforms as well.
5954  *
5955  * Theory behind the formula (note that we ignore sub-pixel
5956  * source coordinates):
5957  * s = source sample position
5958  * d = destination sample position
5959  *
5960  * Downscaling 4:1:
5961  * -0.5
5962  * | 0.0
5963  * | |     1.5 (initial phase)
5964  * | |     |
5965  * v v     v
5966  * | s | s | s | s |
5967  * |       d       |
5968  *
5969  * Upscaling 1:4:
5970  * -0.5
5971  * | -0.375 (initial phase)
5972  * | |     0.0
5973  * | |     |
5974  * v v     v
5975  * |       s       |
5976  * | d | d | d | d |
5977  */
5978 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
5979 {
5980 	int phase = -0x8000;
5981 	u16 trip = 0;
5982 
5983 	if (chroma_cosited)
5984 		phase += (sub - 1) * 0x8000 / sub;
5985 
5986 	phase += scale / (2 * sub);
5987 
5988 	/*
5989 	 * Hardware initial phase limited to [-0.5:1.5].
5990 	 * Since the max hardware scale factor is 3.0, we
5991 	 * should never actually excdeed 1.0 here.
5992 	 */
5993 	WARN_ON(phase < -0x8000 || phase > 0x18000);
5994 
5995 	if (phase < 0)
5996 		phase = 0x10000 + phase;
5997 	else
5998 		trip = PS_PHASE_TRIP;
5999 
6000 	return ((phase >> 2) & PS_PHASE_MASK) | trip;
6001 }
6002 
6003 #define SKL_MIN_SRC_W 8
6004 #define SKL_MAX_SRC_W 4096
6005 #define SKL_MIN_SRC_H 8
6006 #define SKL_MAX_SRC_H 4096
6007 #define SKL_MIN_DST_W 8
6008 #define SKL_MAX_DST_W 4096
6009 #define SKL_MIN_DST_H 8
6010 #define SKL_MAX_DST_H 4096
6011 #define ICL_MAX_SRC_W 5120
6012 #define ICL_MAX_SRC_H 4096
6013 #define ICL_MAX_DST_W 5120
6014 #define ICL_MAX_DST_H 4096
6015 #define SKL_MIN_YUV_420_SRC_W 16
6016 #define SKL_MIN_YUV_420_SRC_H 16
6017 
6018 static int
6019 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
6020 		  unsigned int scaler_user, int *scaler_id,
6021 		  int src_w, int src_h, int dst_w, int dst_h,
6022 		  const struct drm_format_info *format,
6023 		  u64 modifier, bool need_scaler)
6024 {
6025 	struct intel_crtc_scaler_state *scaler_state =
6026 		&crtc_state->scaler_state;
6027 	struct intel_crtc *intel_crtc =
6028 		to_intel_crtc(crtc_state->uapi.crtc);
6029 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
6030 	const struct drm_display_mode *adjusted_mode =
6031 		&crtc_state->hw.adjusted_mode;
6032 
6033 	/*
6034 	 * Src coordinates are already rotated by 270 degrees for
6035 	 * the 90/270 degree plane rotation cases (to match the
6036 	 * GTT mapping), hence no need to account for rotation here.
6037 	 */
6038 	if (src_w != dst_w || src_h != dst_h)
6039 		need_scaler = true;
6040 
6041 	/*
6042 	 * Scaling/fitting not supported in IF-ID mode in GEN9+
6043 	 * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6044 	 * Once NV12 is enabled, handle it here while allocating scaler
6045 	 * for NV12.
6046 	 */
6047 	if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
6048 	    need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6049 		drm_dbg_kms(&dev_priv->drm,
6050 			    "Pipe/Plane scaling not supported with IF-ID mode\n");
6051 		return -EINVAL;
6052 	}
6053 
6054 	/*
6055 	 * if plane is being disabled or scaler is no more required or force detach
6056 	 *  - free scaler binded to this plane/crtc
6057 	 *  - in order to do this, update crtc->scaler_usage
6058 	 *
6059 	 * Here scaler state in crtc_state is set free so that
6060 	 * scaler can be assigned to other user. Actual register
6061 	 * update to free the scaler is done in plane/panel-fit programming.
6062 	 * For this purpose crtc/plane_state->scaler_id isn't reset here.
6063 	 */
6064 	if (force_detach || !need_scaler) {
6065 		if (*scaler_id >= 0) {
6066 			scaler_state->scaler_users &= ~(1 << scaler_user);
6067 			scaler_state->scalers[*scaler_id].in_use = 0;
6068 
6069 			drm_dbg_kms(&dev_priv->drm,
6070 				    "scaler_user index %u.%u: "
6071 				    "Staged freeing scaler id %d scaler_users = 0x%x\n",
6072 				    intel_crtc->pipe, scaler_user, *scaler_id,
6073 				    scaler_state->scaler_users);
6074 			*scaler_id = -1;
6075 		}
6076 		return 0;
6077 	}
6078 
6079 	if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
6080 	    (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
6081 		drm_dbg_kms(&dev_priv->drm,
6082 			    "Planar YUV: src dimensions not met\n");
6083 		return -EINVAL;
6084 	}
6085 
6086 	/* range checks */
6087 	if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
6088 	    dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
6089 	    (INTEL_GEN(dev_priv) >= 11 &&
6090 	     (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6091 	      dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
6092 	    (INTEL_GEN(dev_priv) < 11 &&
6093 	     (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6094 	      dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H)))	{
6095 		drm_dbg_kms(&dev_priv->drm,
6096 			    "scaler_user index %u.%u: src %ux%u dst %ux%u "
6097 			    "size is out of scaler range\n",
6098 			    intel_crtc->pipe, scaler_user, src_w, src_h,
6099 			    dst_w, dst_h);
6100 		return -EINVAL;
6101 	}
6102 
6103 	/* mark this plane as a scaler user in crtc_state */
6104 	scaler_state->scaler_users |= (1 << scaler_user);
6105 	drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6106 		    "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6107 		    intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6108 		    scaler_state->scaler_users);
6109 
6110 	return 0;
6111 }
6112 
6113 /**
6114  * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
6115  *
6116  * @state: crtc's scaler state
6117  *
6118  * Return
6119  *     0 - scaler_usage updated successfully
6120  *    error - requested scaling cannot be supported or other error condition
6121  */
6122 int skl_update_scaler_crtc(struct intel_crtc_state *state)
6123 {
6124 	const struct drm_display_mode *adjusted_mode = &state->hw.adjusted_mode;
6125 	bool need_scaler = false;
6126 
6127 	if (state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6128 	    state->pch_pfit.enabled)
6129 		need_scaler = true;
6130 
6131 	return skl_update_scaler(state, !state->hw.active, SKL_CRTC_INDEX,
6132 				 &state->scaler_state.scaler_id,
6133 				 state->pipe_src_w, state->pipe_src_h,
6134 				 adjusted_mode->crtc_hdisplay,
6135 				 adjusted_mode->crtc_vdisplay, NULL, 0,
6136 				 need_scaler);
6137 }
6138 
6139 /**
6140  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
6141  * @crtc_state: crtc's scaler state
6142  * @plane_state: atomic plane state to update
6143  *
6144  * Return
6145  *     0 - scaler_usage updated successfully
6146  *    error - requested scaling cannot be supported or other error condition
6147  */
6148 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6149 				   struct intel_plane_state *plane_state)
6150 {
6151 	struct intel_plane *intel_plane =
6152 		to_intel_plane(plane_state->uapi.plane);
6153 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
6154 	struct drm_framebuffer *fb = plane_state->hw.fb;
6155 	int ret;
6156 	bool force_detach = !fb || !plane_state->uapi.visible;
6157 	bool need_scaler = false;
6158 
6159 	/* Pre-gen11 and SDR planes always need a scaler for planar formats. */
6160 	if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
6161 	    fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
6162 		need_scaler = true;
6163 
6164 	ret = skl_update_scaler(crtc_state, force_detach,
6165 				drm_plane_index(&intel_plane->base),
6166 				&plane_state->scaler_id,
6167 				drm_rect_width(&plane_state->uapi.src) >> 16,
6168 				drm_rect_height(&plane_state->uapi.src) >> 16,
6169 				drm_rect_width(&plane_state->uapi.dst),
6170 				drm_rect_height(&plane_state->uapi.dst),
6171 				fb ? fb->format : NULL,
6172 				fb ? fb->modifier : 0,
6173 				need_scaler);
6174 
6175 	if (ret || plane_state->scaler_id < 0)
6176 		return ret;
6177 
6178 	/* check colorkey */
6179 	if (plane_state->ckey.flags) {
6180 		drm_dbg_kms(&dev_priv->drm,
6181 			    "[PLANE:%d:%s] scaling with color key not allowed",
6182 			    intel_plane->base.base.id,
6183 			    intel_plane->base.name);
6184 		return -EINVAL;
6185 	}
6186 
6187 	/* Check src format */
6188 	switch (fb->format->format) {
6189 	case DRM_FORMAT_RGB565:
6190 	case DRM_FORMAT_XBGR8888:
6191 	case DRM_FORMAT_XRGB8888:
6192 	case DRM_FORMAT_ABGR8888:
6193 	case DRM_FORMAT_ARGB8888:
6194 	case DRM_FORMAT_XRGB2101010:
6195 	case DRM_FORMAT_XBGR2101010:
6196 	case DRM_FORMAT_ARGB2101010:
6197 	case DRM_FORMAT_ABGR2101010:
6198 	case DRM_FORMAT_YUYV:
6199 	case DRM_FORMAT_YVYU:
6200 	case DRM_FORMAT_UYVY:
6201 	case DRM_FORMAT_VYUY:
6202 	case DRM_FORMAT_NV12:
6203 	case DRM_FORMAT_P010:
6204 	case DRM_FORMAT_P012:
6205 	case DRM_FORMAT_P016:
6206 	case DRM_FORMAT_Y210:
6207 	case DRM_FORMAT_Y212:
6208 	case DRM_FORMAT_Y216:
6209 	case DRM_FORMAT_XVYU2101010:
6210 	case DRM_FORMAT_XVYU12_16161616:
6211 	case DRM_FORMAT_XVYU16161616:
6212 		break;
6213 	case DRM_FORMAT_XBGR16161616F:
6214 	case DRM_FORMAT_ABGR16161616F:
6215 	case DRM_FORMAT_XRGB16161616F:
6216 	case DRM_FORMAT_ARGB16161616F:
6217 		if (INTEL_GEN(dev_priv) >= 11)
6218 			break;
6219 		/* fall through */
6220 	default:
6221 		drm_dbg_kms(&dev_priv->drm,
6222 			    "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6223 			    intel_plane->base.base.id, intel_plane->base.name,
6224 			    fb->base.id, fb->format->format);
6225 		return -EINVAL;
6226 	}
6227 
6228 	return 0;
6229 }
6230 
6231 void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
6232 {
6233 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6234 	int i;
6235 
6236 	for (i = 0; i < crtc->num_scalers; i++)
6237 		skl_detach_scaler(crtc, i);
6238 }
6239 
6240 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
6241 {
6242 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6243 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6244 	enum pipe pipe = crtc->pipe;
6245 	const struct intel_crtc_scaler_state *scaler_state =
6246 		&crtc_state->scaler_state;
6247 
6248 	if (crtc_state->pch_pfit.enabled) {
6249 		u16 uv_rgb_hphase, uv_rgb_vphase;
6250 		int pfit_w, pfit_h, hscale, vscale;
6251 		unsigned long irqflags;
6252 		int id;
6253 
6254 		if (drm_WARN_ON(&dev_priv->drm,
6255 				crtc_state->scaler_state.scaler_id < 0))
6256 			return;
6257 
6258 		pfit_w = (crtc_state->pch_pfit.size >> 16) & 0xFFFF;
6259 		pfit_h = crtc_state->pch_pfit.size & 0xFFFF;
6260 
6261 		hscale = (crtc_state->pipe_src_w << 16) / pfit_w;
6262 		vscale = (crtc_state->pipe_src_h << 16) / pfit_h;
6263 
6264 		uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6265 		uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
6266 
6267 		id = scaler_state->scaler_id;
6268 
6269 		spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6270 
6271 		intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
6272 				  PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
6273 		intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6274 				  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6275 		intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6276 				  PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6277 		intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
6278 				  crtc_state->pch_pfit.pos);
6279 		intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
6280 				  crtc_state->pch_pfit.size);
6281 
6282 		spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6283 	}
6284 }
6285 
6286 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
6287 {
6288 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6289 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6290 	enum pipe pipe = crtc->pipe;
6291 
6292 	if (crtc_state->pch_pfit.enabled) {
6293 		/* Force use of hard-coded filter coefficients
6294 		 * as some pre-programmed values are broken,
6295 		 * e.g. x201.
6296 		 */
6297 		if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6298 			intel_de_write(dev_priv, PF_CTL(pipe),
6299 				       PF_ENABLE | PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6300 		else
6301 			intel_de_write(dev_priv, PF_CTL(pipe),
6302 				       PF_ENABLE | PF_FILTER_MED_3x3);
6303 		intel_de_write(dev_priv, PF_WIN_POS(pipe),
6304 			       crtc_state->pch_pfit.pos);
6305 		intel_de_write(dev_priv, PF_WIN_SZ(pipe),
6306 			       crtc_state->pch_pfit.size);
6307 	}
6308 }
6309 
6310 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
6311 {
6312 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6313 	struct drm_device *dev = crtc->base.dev;
6314 	struct drm_i915_private *dev_priv = to_i915(dev);
6315 
6316 	if (!crtc_state->ips_enabled)
6317 		return;
6318 
6319 	/*
6320 	 * We can only enable IPS after we enable a plane and wait for a vblank
6321 	 * This function is called from post_plane_update, which is run after
6322 	 * a vblank wait.
6323 	 */
6324 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
6325 
6326 	if (IS_BROADWELL(dev_priv)) {
6327 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6328 							 IPS_ENABLE | IPS_PCODE_CONTROL));
6329 		/* Quoting Art Runyan: "its not safe to expect any particular
6330 		 * value in IPS_CTL bit 31 after enabling IPS through the
6331 		 * mailbox." Moreover, the mailbox may return a bogus state,
6332 		 * so we need to just enable it and continue on.
6333 		 */
6334 	} else {
6335 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
6336 		/* The bit only becomes 1 in the next vblank, so this wait here
6337 		 * is essentially intel_wait_for_vblank. If we don't have this
6338 		 * and don't wait for vblanks until the end of crtc_enable, then
6339 		 * the HW state readout code will complain that the expected
6340 		 * IPS_CTL value is not the one we read. */
6341 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
6342 			drm_err(&dev_priv->drm,
6343 				"Timed out waiting for IPS enable\n");
6344 	}
6345 }
6346 
6347 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
6348 {
6349 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6350 	struct drm_device *dev = crtc->base.dev;
6351 	struct drm_i915_private *dev_priv = to_i915(dev);
6352 
6353 	if (!crtc_state->ips_enabled)
6354 		return;
6355 
6356 	if (IS_BROADWELL(dev_priv)) {
6357 		drm_WARN_ON(dev,
6358 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
6359 		/*
6360 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
6361 		 * 42ms timeout value leads to occasional timeouts so use 100ms
6362 		 * instead.
6363 		 */
6364 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
6365 			drm_err(&dev_priv->drm,
6366 				"Timed out waiting for IPS disable\n");
6367 	} else {
6368 		intel_de_write(dev_priv, IPS_CTL, 0);
6369 		intel_de_posting_read(dev_priv, IPS_CTL);
6370 	}
6371 
6372 	/* We need to wait for a vblank before we can disable the plane. */
6373 	intel_wait_for_vblank(dev_priv, crtc->pipe);
6374 }
6375 
6376 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
6377 {
6378 	if (intel_crtc->overlay)
6379 		(void) intel_overlay_switch_off(intel_crtc->overlay);
6380 
6381 	/* Let userspace switch the overlay on again. In most cases userspace
6382 	 * has to recompute where to put it anyway.
6383 	 */
6384 }
6385 
6386 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6387 				       const struct intel_crtc_state *new_crtc_state)
6388 {
6389 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6390 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6391 
6392 	if (!old_crtc_state->ips_enabled)
6393 		return false;
6394 
6395 	if (needs_modeset(new_crtc_state))
6396 		return true;
6397 
6398 	/*
6399 	 * Workaround : Do not read or write the pipe palette/gamma data while
6400 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6401 	 *
6402 	 * Disable IPS before we program the LUT.
6403 	 */
6404 	if (IS_HASWELL(dev_priv) &&
6405 	    (new_crtc_state->uapi.color_mgmt_changed ||
6406 	     new_crtc_state->update_pipe) &&
6407 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6408 		return true;
6409 
6410 	return !new_crtc_state->ips_enabled;
6411 }
6412 
6413 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6414 				       const struct intel_crtc_state *new_crtc_state)
6415 {
6416 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6417 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6418 
6419 	if (!new_crtc_state->ips_enabled)
6420 		return false;
6421 
6422 	if (needs_modeset(new_crtc_state))
6423 		return true;
6424 
6425 	/*
6426 	 * Workaround : Do not read or write the pipe palette/gamma data while
6427 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6428 	 *
6429 	 * Re-enable IPS after the LUT has been programmed.
6430 	 */
6431 	if (IS_HASWELL(dev_priv) &&
6432 	    (new_crtc_state->uapi.color_mgmt_changed ||
6433 	     new_crtc_state->update_pipe) &&
6434 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6435 		return true;
6436 
6437 	/*
6438 	 * We can't read out IPS on broadwell, assume the worst and
6439 	 * forcibly enable IPS on the first fastset.
6440 	 */
6441 	if (new_crtc_state->update_pipe &&
6442 	    old_crtc_state->hw.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
6443 		return true;
6444 
6445 	return !old_crtc_state->ips_enabled;
6446 }
6447 
6448 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
6449 {
6450 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6451 
6452 	if (!crtc_state->nv12_planes)
6453 		return false;
6454 
6455 	/* WA Display #0827: Gen9:all */
6456 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
6457 		return true;
6458 
6459 	return false;
6460 }
6461 
6462 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
6463 {
6464 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6465 
6466 	/* Wa_2006604312:icl */
6467 	if (crtc_state->scaler_state.scaler_users > 0 && IS_ICELAKE(dev_priv))
6468 		return true;
6469 
6470 	return false;
6471 }
6472 
6473 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6474 			    const struct intel_crtc_state *new_crtc_state)
6475 {
6476 	return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6477 		new_crtc_state->active_planes;
6478 }
6479 
6480 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6481 			     const struct intel_crtc_state *new_crtc_state)
6482 {
6483 	return old_crtc_state->active_planes &&
6484 		(!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6485 }
6486 
6487 static void intel_post_plane_update(struct intel_atomic_state *state,
6488 				    struct intel_crtc *crtc)
6489 {
6490 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6491 	const struct intel_crtc_state *old_crtc_state =
6492 		intel_atomic_get_old_crtc_state(state, crtc);
6493 	const struct intel_crtc_state *new_crtc_state =
6494 		intel_atomic_get_new_crtc_state(state, crtc);
6495 	enum pipe pipe = crtc->pipe;
6496 
6497 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
6498 
6499 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
6500 		intel_update_watermarks(crtc);
6501 
6502 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6503 		hsw_enable_ips(new_crtc_state);
6504 
6505 	intel_fbc_post_update(state, crtc);
6506 
6507 	if (needs_nv12_wa(old_crtc_state) &&
6508 	    !needs_nv12_wa(new_crtc_state))
6509 		skl_wa_827(dev_priv, pipe, false);
6510 
6511 	if (needs_scalerclk_wa(old_crtc_state) &&
6512 	    !needs_scalerclk_wa(new_crtc_state))
6513 		icl_wa_scalerclkgating(dev_priv, pipe, false);
6514 }
6515 
6516 static void intel_pre_plane_update(struct intel_atomic_state *state,
6517 				   struct intel_crtc *crtc)
6518 {
6519 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6520 	const struct intel_crtc_state *old_crtc_state =
6521 		intel_atomic_get_old_crtc_state(state, crtc);
6522 	const struct intel_crtc_state *new_crtc_state =
6523 		intel_atomic_get_new_crtc_state(state, crtc);
6524 	enum pipe pipe = crtc->pipe;
6525 
6526 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
6527 		hsw_disable_ips(old_crtc_state);
6528 
6529 	if (intel_fbc_pre_update(state, crtc))
6530 		intel_wait_for_vblank(dev_priv, pipe);
6531 
6532 	/* Display WA 827 */
6533 	if (!needs_nv12_wa(old_crtc_state) &&
6534 	    needs_nv12_wa(new_crtc_state))
6535 		skl_wa_827(dev_priv, pipe, true);
6536 
6537 	/* Wa_2006604312:icl */
6538 	if (!needs_scalerclk_wa(old_crtc_state) &&
6539 	    needs_scalerclk_wa(new_crtc_state))
6540 		icl_wa_scalerclkgating(dev_priv, pipe, true);
6541 
6542 	/*
6543 	 * Vblank time updates from the shadow to live plane control register
6544 	 * are blocked if the memory self-refresh mode is active at that
6545 	 * moment. So to make sure the plane gets truly disabled, disable
6546 	 * first the self-refresh mode. The self-refresh enable bit in turn
6547 	 * will be checked/applied by the HW only at the next frame start
6548 	 * event which is after the vblank start event, so we need to have a
6549 	 * wait-for-vblank between disabling the plane and the pipe.
6550 	 */
6551 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
6552 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
6553 		intel_wait_for_vblank(dev_priv, pipe);
6554 
6555 	/*
6556 	 * IVB workaround: must disable low power watermarks for at least
6557 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
6558 	 * when scaling is disabled.
6559 	 *
6560 	 * WaCxSRDisabledForSpriteScaling:ivb
6561 	 */
6562 	if (old_crtc_state->hw.active &&
6563 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
6564 		intel_wait_for_vblank(dev_priv, pipe);
6565 
6566 	/*
6567 	 * If we're doing a modeset we don't need to do any
6568 	 * pre-vblank watermark programming here.
6569 	 */
6570 	if (!needs_modeset(new_crtc_state)) {
6571 		/*
6572 		 * For platforms that support atomic watermarks, program the
6573 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6574 		 * will be the intermediate values that are safe for both pre- and
6575 		 * post- vblank; when vblank happens, the 'active' values will be set
6576 		 * to the final 'target' values and we'll do this again to get the
6577 		 * optimal watermarks.  For gen9+ platforms, the values we program here
6578 		 * will be the final target values which will get automatically latched
6579 		 * at vblank time; no further programming will be necessary.
6580 		 *
6581 		 * If a platform hasn't been transitioned to atomic watermarks yet,
6582 		 * we'll continue to update watermarks the old way, if flags tell
6583 		 * us to.
6584 		 */
6585 		if (dev_priv->display.initial_watermarks)
6586 			dev_priv->display.initial_watermarks(state, crtc);
6587 		else if (new_crtc_state->update_wm_pre)
6588 			intel_update_watermarks(crtc);
6589 	}
6590 
6591 	/*
6592 	 * Gen2 reports pipe underruns whenever all planes are disabled.
6593 	 * So disable underrun reporting before all the planes get disabled.
6594 	 *
6595 	 * We do this after .initial_watermarks() so that we have a
6596 	 * chance of catching underruns with the intermediate watermarks
6597 	 * vs. the old plane configuration.
6598 	 */
6599 	if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6600 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6601 }
6602 
6603 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6604 				      struct intel_crtc *crtc)
6605 {
6606 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6607 	const struct intel_crtc_state *new_crtc_state =
6608 		intel_atomic_get_new_crtc_state(state, crtc);
6609 	unsigned int update_mask = new_crtc_state->update_planes;
6610 	const struct intel_plane_state *old_plane_state;
6611 	struct intel_plane *plane;
6612 	unsigned fb_bits = 0;
6613 	int i;
6614 
6615 	intel_crtc_dpms_overlay_disable(crtc);
6616 
6617 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6618 		if (crtc->pipe != plane->pipe ||
6619 		    !(update_mask & BIT(plane->id)))
6620 			continue;
6621 
6622 		intel_disable_plane(plane, new_crtc_state);
6623 
6624 		if (old_plane_state->uapi.visible)
6625 			fb_bits |= plane->frontbuffer_bit;
6626 	}
6627 
6628 	intel_frontbuffer_flip(dev_priv, fb_bits);
6629 }
6630 
6631 /*
6632  * intel_connector_primary_encoder - get the primary encoder for a connector
6633  * @connector: connector for which to return the encoder
6634  *
6635  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6636  * all connectors to their encoder, except for DP-MST connectors which have
6637  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6638  * pointed to by as many DP-MST connectors as there are pipes.
6639  */
6640 static struct intel_encoder *
6641 intel_connector_primary_encoder(struct intel_connector *connector)
6642 {
6643 	struct intel_encoder *encoder;
6644 
6645 	if (connector->mst_port)
6646 		return &dp_to_dig_port(connector->mst_port)->base;
6647 
6648 	encoder = intel_attached_encoder(connector);
6649 	WARN_ON(!encoder);
6650 
6651 	return encoder;
6652 }
6653 
6654 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6655 {
6656 	struct drm_connector_state *new_conn_state;
6657 	struct drm_connector *connector;
6658 	int i;
6659 
6660 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6661 					i) {
6662 		struct intel_connector *intel_connector;
6663 		struct intel_encoder *encoder;
6664 		struct intel_crtc *crtc;
6665 
6666 		if (!intel_connector_needs_modeset(state, connector))
6667 			continue;
6668 
6669 		intel_connector = to_intel_connector(connector);
6670 		encoder = intel_connector_primary_encoder(intel_connector);
6671 		if (!encoder->update_prepare)
6672 			continue;
6673 
6674 		crtc = new_conn_state->crtc ?
6675 			to_intel_crtc(new_conn_state->crtc) : NULL;
6676 		encoder->update_prepare(state, encoder, crtc);
6677 	}
6678 }
6679 
6680 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6681 {
6682 	struct drm_connector_state *new_conn_state;
6683 	struct drm_connector *connector;
6684 	int i;
6685 
6686 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6687 					i) {
6688 		struct intel_connector *intel_connector;
6689 		struct intel_encoder *encoder;
6690 		struct intel_crtc *crtc;
6691 
6692 		if (!intel_connector_needs_modeset(state, connector))
6693 			continue;
6694 
6695 		intel_connector = to_intel_connector(connector);
6696 		encoder = intel_connector_primary_encoder(intel_connector);
6697 		if (!encoder->update_complete)
6698 			continue;
6699 
6700 		crtc = new_conn_state->crtc ?
6701 			to_intel_crtc(new_conn_state->crtc) : NULL;
6702 		encoder->update_complete(state, encoder, crtc);
6703 	}
6704 }
6705 
6706 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6707 					  struct intel_crtc *crtc)
6708 {
6709 	const struct intel_crtc_state *crtc_state =
6710 		intel_atomic_get_new_crtc_state(state, crtc);
6711 	const struct drm_connector_state *conn_state;
6712 	struct drm_connector *conn;
6713 	int i;
6714 
6715 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6716 		struct intel_encoder *encoder =
6717 			to_intel_encoder(conn_state->best_encoder);
6718 
6719 		if (conn_state->crtc != &crtc->base)
6720 			continue;
6721 
6722 		if (encoder->pre_pll_enable)
6723 			encoder->pre_pll_enable(encoder, crtc_state, conn_state);
6724 	}
6725 }
6726 
6727 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6728 				      struct intel_crtc *crtc)
6729 {
6730 	const struct intel_crtc_state *crtc_state =
6731 		intel_atomic_get_new_crtc_state(state, crtc);
6732 	const struct drm_connector_state *conn_state;
6733 	struct drm_connector *conn;
6734 	int i;
6735 
6736 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6737 		struct intel_encoder *encoder =
6738 			to_intel_encoder(conn_state->best_encoder);
6739 
6740 		if (conn_state->crtc != &crtc->base)
6741 			continue;
6742 
6743 		if (encoder->pre_enable)
6744 			encoder->pre_enable(encoder, crtc_state, conn_state);
6745 	}
6746 }
6747 
6748 static void intel_encoders_enable(struct intel_atomic_state *state,
6749 				  struct intel_crtc *crtc)
6750 {
6751 	const struct intel_crtc_state *crtc_state =
6752 		intel_atomic_get_new_crtc_state(state, crtc);
6753 	const struct drm_connector_state *conn_state;
6754 	struct drm_connector *conn;
6755 	int i;
6756 
6757 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6758 		struct intel_encoder *encoder =
6759 			to_intel_encoder(conn_state->best_encoder);
6760 
6761 		if (conn_state->crtc != &crtc->base)
6762 			continue;
6763 
6764 		if (encoder->enable)
6765 			encoder->enable(encoder, crtc_state, conn_state);
6766 		intel_opregion_notify_encoder(encoder, true);
6767 	}
6768 }
6769 
6770 static void intel_encoders_disable(struct intel_atomic_state *state,
6771 				   struct intel_crtc *crtc)
6772 {
6773 	const struct intel_crtc_state *old_crtc_state =
6774 		intel_atomic_get_old_crtc_state(state, crtc);
6775 	const struct drm_connector_state *old_conn_state;
6776 	struct drm_connector *conn;
6777 	int i;
6778 
6779 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6780 		struct intel_encoder *encoder =
6781 			to_intel_encoder(old_conn_state->best_encoder);
6782 
6783 		if (old_conn_state->crtc != &crtc->base)
6784 			continue;
6785 
6786 		intel_opregion_notify_encoder(encoder, false);
6787 		if (encoder->disable)
6788 			encoder->disable(encoder, old_crtc_state, old_conn_state);
6789 	}
6790 }
6791 
6792 static void intel_encoders_post_disable(struct intel_atomic_state *state,
6793 					struct intel_crtc *crtc)
6794 {
6795 	const struct intel_crtc_state *old_crtc_state =
6796 		intel_atomic_get_old_crtc_state(state, crtc);
6797 	const struct drm_connector_state *old_conn_state;
6798 	struct drm_connector *conn;
6799 	int i;
6800 
6801 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6802 		struct intel_encoder *encoder =
6803 			to_intel_encoder(old_conn_state->best_encoder);
6804 
6805 		if (old_conn_state->crtc != &crtc->base)
6806 			continue;
6807 
6808 		if (encoder->post_disable)
6809 			encoder->post_disable(encoder, old_crtc_state, old_conn_state);
6810 	}
6811 }
6812 
6813 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6814 					    struct intel_crtc *crtc)
6815 {
6816 	const struct intel_crtc_state *old_crtc_state =
6817 		intel_atomic_get_old_crtc_state(state, crtc);
6818 	const struct drm_connector_state *old_conn_state;
6819 	struct drm_connector *conn;
6820 	int i;
6821 
6822 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6823 		struct intel_encoder *encoder =
6824 			to_intel_encoder(old_conn_state->best_encoder);
6825 
6826 		if (old_conn_state->crtc != &crtc->base)
6827 			continue;
6828 
6829 		if (encoder->post_pll_disable)
6830 			encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
6831 	}
6832 }
6833 
6834 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6835 				       struct intel_crtc *crtc)
6836 {
6837 	const struct intel_crtc_state *crtc_state =
6838 		intel_atomic_get_new_crtc_state(state, crtc);
6839 	const struct drm_connector_state *conn_state;
6840 	struct drm_connector *conn;
6841 	int i;
6842 
6843 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6844 		struct intel_encoder *encoder =
6845 			to_intel_encoder(conn_state->best_encoder);
6846 
6847 		if (conn_state->crtc != &crtc->base)
6848 			continue;
6849 
6850 		if (encoder->update_pipe)
6851 			encoder->update_pipe(encoder, crtc_state, conn_state);
6852 	}
6853 }
6854 
6855 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6856 {
6857 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6858 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6859 
6860 	plane->disable_plane(plane, crtc_state);
6861 }
6862 
6863 static void ilk_crtc_enable(struct intel_atomic_state *state,
6864 			    struct intel_crtc *crtc)
6865 {
6866 	const struct intel_crtc_state *new_crtc_state =
6867 		intel_atomic_get_new_crtc_state(state, crtc);
6868 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6869 	enum pipe pipe = crtc->pipe;
6870 
6871 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
6872 		return;
6873 
6874 	/*
6875 	 * Sometimes spurious CPU pipe underruns happen during FDI
6876 	 * training, at least with VGA+HDMI cloning. Suppress them.
6877 	 *
6878 	 * On ILK we get an occasional spurious CPU pipe underruns
6879 	 * between eDP port A enable and vdd enable. Also PCH port
6880 	 * enable seems to result in the occasional CPU pipe underrun.
6881 	 *
6882 	 * Spurious PCH underruns also occur during PCH enabling.
6883 	 */
6884 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6885 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
6886 
6887 	if (new_crtc_state->has_pch_encoder)
6888 		intel_prepare_shared_dpll(new_crtc_state);
6889 
6890 	if (intel_crtc_has_dp_encoder(new_crtc_state))
6891 		intel_dp_set_m_n(new_crtc_state, M1_N1);
6892 
6893 	intel_set_pipe_timings(new_crtc_state);
6894 	intel_set_pipe_src_size(new_crtc_state);
6895 
6896 	if (new_crtc_state->has_pch_encoder)
6897 		intel_cpu_transcoder_set_m_n(new_crtc_state,
6898 					     &new_crtc_state->fdi_m_n, NULL);
6899 
6900 	ilk_set_pipeconf(new_crtc_state);
6901 
6902 	crtc->active = true;
6903 
6904 	intel_encoders_pre_enable(state, crtc);
6905 
6906 	if (new_crtc_state->has_pch_encoder) {
6907 		/* Note: FDI PLL enabling _must_ be done before we enable the
6908 		 * cpu pipes, hence this is separate from all the other fdi/pch
6909 		 * enabling. */
6910 		ilk_fdi_pll_enable(new_crtc_state);
6911 	} else {
6912 		assert_fdi_tx_disabled(dev_priv, pipe);
6913 		assert_fdi_rx_disabled(dev_priv, pipe);
6914 	}
6915 
6916 	ilk_pfit_enable(new_crtc_state);
6917 
6918 	/*
6919 	 * On ILK+ LUT must be loaded before the pipe is running but with
6920 	 * clocks enabled
6921 	 */
6922 	intel_color_load_luts(new_crtc_state);
6923 	intel_color_commit(new_crtc_state);
6924 	/* update DSPCNTR to configure gamma for pipe bottom color */
6925 	intel_disable_primary_plane(new_crtc_state);
6926 
6927 	if (dev_priv->display.initial_watermarks)
6928 		dev_priv->display.initial_watermarks(state, crtc);
6929 	intel_enable_pipe(new_crtc_state);
6930 
6931 	if (new_crtc_state->has_pch_encoder)
6932 		ilk_pch_enable(state, new_crtc_state);
6933 
6934 	intel_crtc_vblank_on(new_crtc_state);
6935 
6936 	intel_encoders_enable(state, crtc);
6937 
6938 	if (HAS_PCH_CPT(dev_priv))
6939 		cpt_verify_modeset(dev_priv, pipe);
6940 
6941 	/*
6942 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
6943 	 * And a second vblank wait is needed at least on ILK with
6944 	 * some interlaced HDMI modes. Let's do the double wait always
6945 	 * in case there are more corner cases we don't know about.
6946 	 */
6947 	if (new_crtc_state->has_pch_encoder) {
6948 		intel_wait_for_vblank(dev_priv, pipe);
6949 		intel_wait_for_vblank(dev_priv, pipe);
6950 	}
6951 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6952 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6953 }
6954 
6955 /* IPS only exists on ULT machines and is tied to pipe A. */
6956 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
6957 {
6958 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
6959 }
6960 
6961 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
6962 					    enum pipe pipe, bool apply)
6963 {
6964 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
6965 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
6966 
6967 	if (apply)
6968 		val |= mask;
6969 	else
6970 		val &= ~mask;
6971 
6972 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
6973 }
6974 
6975 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
6976 {
6977 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6978 	enum pipe pipe = crtc->pipe;
6979 	u32 val;
6980 
6981 	val = MBUS_DBOX_A_CREDIT(2);
6982 
6983 	if (INTEL_GEN(dev_priv) >= 12) {
6984 		val |= MBUS_DBOX_BW_CREDIT(2);
6985 		val |= MBUS_DBOX_B_CREDIT(12);
6986 	} else {
6987 		val |= MBUS_DBOX_BW_CREDIT(1);
6988 		val |= MBUS_DBOX_B_CREDIT(8);
6989 	}
6990 
6991 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
6992 }
6993 
6994 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
6995 {
6996 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6997 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6998 
6999 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7000 		       HSW_LINETIME(crtc_state->linetime) |
7001 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
7002 }
7003 
7004 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7005 {
7006 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7007 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7008 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7009 	u32 val;
7010 
7011 	val = intel_de_read(dev_priv, reg);
7012 	val &= ~HSW_FRAME_START_DELAY_MASK;
7013 	val |= HSW_FRAME_START_DELAY(0);
7014 	intel_de_write(dev_priv, reg, val);
7015 }
7016 
7017 static void hsw_crtc_enable(struct intel_atomic_state *state,
7018 			    struct intel_crtc *crtc)
7019 {
7020 	const struct intel_crtc_state *new_crtc_state =
7021 		intel_atomic_get_new_crtc_state(state, crtc);
7022 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7023 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
7024 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
7025 	bool psl_clkgate_wa;
7026 
7027 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7028 		return;
7029 
7030 	intel_encoders_pre_pll_enable(state, crtc);
7031 
7032 	if (new_crtc_state->shared_dpll)
7033 		intel_enable_shared_dpll(new_crtc_state);
7034 
7035 	intel_encoders_pre_enable(state, crtc);
7036 
7037 	if (!transcoder_is_dsi(cpu_transcoder))
7038 		intel_set_pipe_timings(new_crtc_state);
7039 
7040 	if (INTEL_GEN(dev_priv) >= 11)
7041 		icl_enable_trans_port_sync(new_crtc_state);
7042 
7043 	intel_set_pipe_src_size(new_crtc_state);
7044 
7045 	if (cpu_transcoder != TRANSCODER_EDP &&
7046 	    !transcoder_is_dsi(cpu_transcoder))
7047 		intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7048 			       new_crtc_state->pixel_multiplier - 1);
7049 
7050 	if (new_crtc_state->has_pch_encoder)
7051 		intel_cpu_transcoder_set_m_n(new_crtc_state,
7052 					     &new_crtc_state->fdi_m_n, NULL);
7053 
7054 	if (!transcoder_is_dsi(cpu_transcoder)) {
7055 		hsw_set_frame_start_delay(new_crtc_state);
7056 		hsw_set_pipeconf(new_crtc_state);
7057 	}
7058 
7059 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7060 		bdw_set_pipemisc(new_crtc_state);
7061 
7062 	crtc->active = true;
7063 
7064 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7065 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
7066 		new_crtc_state->pch_pfit.enabled;
7067 	if (psl_clkgate_wa)
7068 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7069 
7070 	if (INTEL_GEN(dev_priv) >= 9)
7071 		skl_pfit_enable(new_crtc_state);
7072 	else
7073 		ilk_pfit_enable(new_crtc_state);
7074 
7075 	/*
7076 	 * On ILK+ LUT must be loaded before the pipe is running but with
7077 	 * clocks enabled
7078 	 */
7079 	intel_color_load_luts(new_crtc_state);
7080 	intel_color_commit(new_crtc_state);
7081 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
7082 	if (INTEL_GEN(dev_priv) < 9)
7083 		intel_disable_primary_plane(new_crtc_state);
7084 
7085 	hsw_set_linetime_wm(new_crtc_state);
7086 
7087 	if (INTEL_GEN(dev_priv) >= 11)
7088 		icl_set_pipe_chicken(crtc);
7089 
7090 	if (!transcoder_is_dsi(cpu_transcoder))
7091 		intel_ddi_enable_transcoder_func(new_crtc_state);
7092 
7093 	if (dev_priv->display.initial_watermarks)
7094 		dev_priv->display.initial_watermarks(state, crtc);
7095 
7096 	if (INTEL_GEN(dev_priv) >= 11)
7097 		icl_pipe_mbus_enable(crtc);
7098 
7099 	intel_encoders_enable(state, crtc);
7100 
7101 	if (psl_clkgate_wa) {
7102 		intel_wait_for_vblank(dev_priv, pipe);
7103 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7104 	}
7105 
7106 	/* If we change the relative order between pipe/planes enabling, we need
7107 	 * to change the workaround. */
7108 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
7109 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
7110 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7111 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7112 	}
7113 }
7114 
7115 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7116 {
7117 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7118 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7119 	enum pipe pipe = crtc->pipe;
7120 
7121 	/* To avoid upsetting the power well on haswell only disable the pfit if
7122 	 * it's in use. The hw state code will make sure we get this right. */
7123 	if (old_crtc_state->pch_pfit.enabled) {
7124 		intel_de_write(dev_priv, PF_CTL(pipe), 0);
7125 		intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7126 		intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
7127 	}
7128 }
7129 
7130 static void ilk_crtc_disable(struct intel_atomic_state *state,
7131 			     struct intel_crtc *crtc)
7132 {
7133 	const struct intel_crtc_state *old_crtc_state =
7134 		intel_atomic_get_old_crtc_state(state, crtc);
7135 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7136 	enum pipe pipe = crtc->pipe;
7137 
7138 	/*
7139 	 * Sometimes spurious CPU pipe underruns happen when the
7140 	 * pipe is already disabled, but FDI RX/TX is still enabled.
7141 	 * Happens at least with VGA+HDMI cloning. Suppress them.
7142 	 */
7143 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7144 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7145 
7146 	intel_encoders_disable(state, crtc);
7147 
7148 	intel_crtc_vblank_off(old_crtc_state);
7149 
7150 	intel_disable_pipe(old_crtc_state);
7151 
7152 	ilk_pfit_disable(old_crtc_state);
7153 
7154 	if (old_crtc_state->has_pch_encoder)
7155 		ilk_fdi_disable(crtc);
7156 
7157 	intel_encoders_post_disable(state, crtc);
7158 
7159 	if (old_crtc_state->has_pch_encoder) {
7160 		ilk_disable_pch_transcoder(dev_priv, pipe);
7161 
7162 		if (HAS_PCH_CPT(dev_priv)) {
7163 			i915_reg_t reg;
7164 			u32 temp;
7165 
7166 			/* disable TRANS_DP_CTL */
7167 			reg = TRANS_DP_CTL(pipe);
7168 			temp = intel_de_read(dev_priv, reg);
7169 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7170 				  TRANS_DP_PORT_SEL_MASK);
7171 			temp |= TRANS_DP_PORT_SEL_NONE;
7172 			intel_de_write(dev_priv, reg, temp);
7173 
7174 			/* disable DPLL_SEL */
7175 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
7176 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
7177 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
7178 		}
7179 
7180 		ilk_fdi_pll_disable(crtc);
7181 	}
7182 
7183 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7184 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7185 }
7186 
7187 static void hsw_crtc_disable(struct intel_atomic_state *state,
7188 			     struct intel_crtc *crtc)
7189 {
7190 	/*
7191 	 * FIXME collapse everything to one hook.
7192 	 * Need care with mst->ddi interactions.
7193 	 */
7194 	intel_encoders_disable(state, crtc);
7195 	intel_encoders_post_disable(state, crtc);
7196 }
7197 
7198 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
7199 {
7200 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7201 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7202 
7203 	if (!crtc_state->gmch_pfit.control)
7204 		return;
7205 
7206 	/*
7207 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
7208 	 * according to register description and PRM.
7209 	 */
7210 	drm_WARN_ON(&dev_priv->drm,
7211 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
7212 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
7213 
7214 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7215 		       crtc_state->gmch_pfit.pgm_ratios);
7216 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
7217 
7218 	/* Border color in case we don't scale up to the full screen. Black by
7219 	 * default, change to something else for debugging. */
7220 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
7221 }
7222 
7223 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7224 {
7225 	if (phy == PHY_NONE)
7226 		return false;
7227 
7228 	if (IS_ELKHARTLAKE(dev_priv))
7229 		return phy <= PHY_C;
7230 
7231 	if (INTEL_GEN(dev_priv) >= 11)
7232 		return phy <= PHY_B;
7233 
7234 	return false;
7235 }
7236 
7237 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7238 {
7239 	if (INTEL_GEN(dev_priv) >= 12)
7240 		return phy >= PHY_D && phy <= PHY_I;
7241 
7242 	if (INTEL_GEN(dev_priv) >= 11 && !IS_ELKHARTLAKE(dev_priv))
7243 		return phy >= PHY_C && phy <= PHY_F;
7244 
7245 	return false;
7246 }
7247 
7248 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7249 {
7250 	if (IS_ELKHARTLAKE(i915) && port == PORT_D)
7251 		return PHY_A;
7252 
7253 	return (enum phy)port;
7254 }
7255 
7256 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7257 {
7258 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
7259 		return PORT_TC_NONE;
7260 
7261 	if (INTEL_GEN(dev_priv) >= 12)
7262 		return port - PORT_D;
7263 
7264 	return port - PORT_C;
7265 }
7266 
7267 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
7268 {
7269 	switch (port) {
7270 	case PORT_A:
7271 		return POWER_DOMAIN_PORT_DDI_A_LANES;
7272 	case PORT_B:
7273 		return POWER_DOMAIN_PORT_DDI_B_LANES;
7274 	case PORT_C:
7275 		return POWER_DOMAIN_PORT_DDI_C_LANES;
7276 	case PORT_D:
7277 		return POWER_DOMAIN_PORT_DDI_D_LANES;
7278 	case PORT_E:
7279 		return POWER_DOMAIN_PORT_DDI_E_LANES;
7280 	case PORT_F:
7281 		return POWER_DOMAIN_PORT_DDI_F_LANES;
7282 	case PORT_G:
7283 		return POWER_DOMAIN_PORT_DDI_G_LANES;
7284 	default:
7285 		MISSING_CASE(port);
7286 		return POWER_DOMAIN_PORT_OTHER;
7287 	}
7288 }
7289 
7290 enum intel_display_power_domain
7291 intel_aux_power_domain(struct intel_digital_port *dig_port)
7292 {
7293 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
7294 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
7295 
7296 	if (intel_phy_is_tc(dev_priv, phy) &&
7297 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
7298 		switch (dig_port->aux_ch) {
7299 		case AUX_CH_C:
7300 			return POWER_DOMAIN_AUX_C_TBT;
7301 		case AUX_CH_D:
7302 			return POWER_DOMAIN_AUX_D_TBT;
7303 		case AUX_CH_E:
7304 			return POWER_DOMAIN_AUX_E_TBT;
7305 		case AUX_CH_F:
7306 			return POWER_DOMAIN_AUX_F_TBT;
7307 		case AUX_CH_G:
7308 			return POWER_DOMAIN_AUX_G_TBT;
7309 		default:
7310 			MISSING_CASE(dig_port->aux_ch);
7311 			return POWER_DOMAIN_AUX_C_TBT;
7312 		}
7313 	}
7314 
7315 	switch (dig_port->aux_ch) {
7316 	case AUX_CH_A:
7317 		return POWER_DOMAIN_AUX_A;
7318 	case AUX_CH_B:
7319 		return POWER_DOMAIN_AUX_B;
7320 	case AUX_CH_C:
7321 		return POWER_DOMAIN_AUX_C;
7322 	case AUX_CH_D:
7323 		return POWER_DOMAIN_AUX_D;
7324 	case AUX_CH_E:
7325 		return POWER_DOMAIN_AUX_E;
7326 	case AUX_CH_F:
7327 		return POWER_DOMAIN_AUX_F;
7328 	case AUX_CH_G:
7329 		return POWER_DOMAIN_AUX_G;
7330 	default:
7331 		MISSING_CASE(dig_port->aux_ch);
7332 		return POWER_DOMAIN_AUX_A;
7333 	}
7334 }
7335 
7336 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7337 {
7338 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7339 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7340 	struct drm_encoder *encoder;
7341 	enum pipe pipe = crtc->pipe;
7342 	u64 mask;
7343 	enum transcoder transcoder = crtc_state->cpu_transcoder;
7344 
7345 	if (!crtc_state->hw.active)
7346 		return 0;
7347 
7348 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7349 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
7350 	if (crtc_state->pch_pfit.enabled ||
7351 	    crtc_state->pch_pfit.force_thru)
7352 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
7353 
7354 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
7355 				  crtc_state->uapi.encoder_mask) {
7356 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7357 
7358 		mask |= BIT_ULL(intel_encoder->power_domain);
7359 	}
7360 
7361 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
7362 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
7363 
7364 	if (crtc_state->shared_dpll)
7365 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
7366 
7367 	return mask;
7368 }
7369 
7370 static u64
7371 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7372 {
7373 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7374 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7375 	enum intel_display_power_domain domain;
7376 	u64 domains, new_domains, old_domains;
7377 
7378 	old_domains = crtc->enabled_power_domains;
7379 	crtc->enabled_power_domains = new_domains =
7380 		get_crtc_power_domains(crtc_state);
7381 
7382 	domains = new_domains & ~old_domains;
7383 
7384 	for_each_power_domain(domain, domains)
7385 		intel_display_power_get(dev_priv, domain);
7386 
7387 	return old_domains & ~new_domains;
7388 }
7389 
7390 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
7391 				      u64 domains)
7392 {
7393 	enum intel_display_power_domain domain;
7394 
7395 	for_each_power_domain(domain, domains)
7396 		intel_display_power_put_unchecked(dev_priv, domain);
7397 }
7398 
7399 static void valleyview_crtc_enable(struct intel_atomic_state *state,
7400 				   struct intel_crtc *crtc)
7401 {
7402 	const struct intel_crtc_state *new_crtc_state =
7403 		intel_atomic_get_new_crtc_state(state, crtc);
7404 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7405 	enum pipe pipe = crtc->pipe;
7406 
7407 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7408 		return;
7409 
7410 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7411 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7412 
7413 	intel_set_pipe_timings(new_crtc_state);
7414 	intel_set_pipe_src_size(new_crtc_state);
7415 
7416 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
7417 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7418 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
7419 	}
7420 
7421 	i9xx_set_pipeconf(new_crtc_state);
7422 
7423 	crtc->active = true;
7424 
7425 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7426 
7427 	intel_encoders_pre_pll_enable(state, crtc);
7428 
7429 	if (IS_CHERRYVIEW(dev_priv)) {
7430 		chv_prepare_pll(crtc, new_crtc_state);
7431 		chv_enable_pll(crtc, new_crtc_state);
7432 	} else {
7433 		vlv_prepare_pll(crtc, new_crtc_state);
7434 		vlv_enable_pll(crtc, new_crtc_state);
7435 	}
7436 
7437 	intel_encoders_pre_enable(state, crtc);
7438 
7439 	i9xx_pfit_enable(new_crtc_state);
7440 
7441 	intel_color_load_luts(new_crtc_state);
7442 	intel_color_commit(new_crtc_state);
7443 	/* update DSPCNTR to configure gamma for pipe bottom color */
7444 	intel_disable_primary_plane(new_crtc_state);
7445 
7446 	dev_priv->display.initial_watermarks(state, crtc);
7447 	intel_enable_pipe(new_crtc_state);
7448 
7449 	intel_crtc_vblank_on(new_crtc_state);
7450 
7451 	intel_encoders_enable(state, crtc);
7452 }
7453 
7454 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
7455 {
7456 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7457 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7458 
7459 	intel_de_write(dev_priv, FP0(crtc->pipe),
7460 		       crtc_state->dpll_hw_state.fp0);
7461 	intel_de_write(dev_priv, FP1(crtc->pipe),
7462 		       crtc_state->dpll_hw_state.fp1);
7463 }
7464 
7465 static void i9xx_crtc_enable(struct intel_atomic_state *state,
7466 			     struct intel_crtc *crtc)
7467 {
7468 	const struct intel_crtc_state *new_crtc_state =
7469 		intel_atomic_get_new_crtc_state(state, crtc);
7470 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7471 	enum pipe pipe = crtc->pipe;
7472 
7473 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7474 		return;
7475 
7476 	i9xx_set_pll_dividers(new_crtc_state);
7477 
7478 	if (intel_crtc_has_dp_encoder(new_crtc_state))
7479 		intel_dp_set_m_n(new_crtc_state, M1_N1);
7480 
7481 	intel_set_pipe_timings(new_crtc_state);
7482 	intel_set_pipe_src_size(new_crtc_state);
7483 
7484 	i9xx_set_pipeconf(new_crtc_state);
7485 
7486 	crtc->active = true;
7487 
7488 	if (!IS_GEN(dev_priv, 2))
7489 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7490 
7491 	intel_encoders_pre_enable(state, crtc);
7492 
7493 	i9xx_enable_pll(crtc, new_crtc_state);
7494 
7495 	i9xx_pfit_enable(new_crtc_state);
7496 
7497 	intel_color_load_luts(new_crtc_state);
7498 	intel_color_commit(new_crtc_state);
7499 	/* update DSPCNTR to configure gamma for pipe bottom color */
7500 	intel_disable_primary_plane(new_crtc_state);
7501 
7502 	if (dev_priv->display.initial_watermarks)
7503 		dev_priv->display.initial_watermarks(state, crtc);
7504 	else
7505 		intel_update_watermarks(crtc);
7506 	intel_enable_pipe(new_crtc_state);
7507 
7508 	intel_crtc_vblank_on(new_crtc_state);
7509 
7510 	intel_encoders_enable(state, crtc);
7511 }
7512 
7513 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7514 {
7515 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7516 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7517 
7518 	if (!old_crtc_state->gmch_pfit.control)
7519 		return;
7520 
7521 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7522 
7523 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
7524 		    intel_de_read(dev_priv, PFIT_CONTROL));
7525 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
7526 }
7527 
7528 static void i9xx_crtc_disable(struct intel_atomic_state *state,
7529 			      struct intel_crtc *crtc)
7530 {
7531 	struct intel_crtc_state *old_crtc_state =
7532 		intel_atomic_get_old_crtc_state(state, crtc);
7533 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7534 	enum pipe pipe = crtc->pipe;
7535 
7536 	/*
7537 	 * On gen2 planes are double buffered but the pipe isn't, so we must
7538 	 * wait for planes to fully turn off before disabling the pipe.
7539 	 */
7540 	if (IS_GEN(dev_priv, 2))
7541 		intel_wait_for_vblank(dev_priv, pipe);
7542 
7543 	intel_encoders_disable(state, crtc);
7544 
7545 	intel_crtc_vblank_off(old_crtc_state);
7546 
7547 	intel_disable_pipe(old_crtc_state);
7548 
7549 	i9xx_pfit_disable(old_crtc_state);
7550 
7551 	intel_encoders_post_disable(state, crtc);
7552 
7553 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7554 		if (IS_CHERRYVIEW(dev_priv))
7555 			chv_disable_pll(dev_priv, pipe);
7556 		else if (IS_VALLEYVIEW(dev_priv))
7557 			vlv_disable_pll(dev_priv, pipe);
7558 		else
7559 			i9xx_disable_pll(old_crtc_state);
7560 	}
7561 
7562 	intel_encoders_post_pll_disable(state, crtc);
7563 
7564 	if (!IS_GEN(dev_priv, 2))
7565 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7566 
7567 	if (!dev_priv->display.initial_watermarks)
7568 		intel_update_watermarks(crtc);
7569 
7570 	/* clock the pipe down to 640x480@60 to potentially save power */
7571 	if (IS_I830(dev_priv))
7572 		i830_enable_pipe(dev_priv, pipe);
7573 }
7574 
7575 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
7576 					struct drm_modeset_acquire_ctx *ctx)
7577 {
7578 	struct intel_encoder *encoder;
7579 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7580 	struct intel_bw_state *bw_state =
7581 		to_intel_bw_state(dev_priv->bw_obj.state);
7582 	struct intel_cdclk_state *cdclk_state =
7583 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
7584 	struct intel_crtc_state *crtc_state =
7585 		to_intel_crtc_state(crtc->base.state);
7586 	enum intel_display_power_domain domain;
7587 	struct intel_plane *plane;
7588 	struct drm_atomic_state *state;
7589 	struct intel_crtc_state *temp_crtc_state;
7590 	enum pipe pipe = crtc->pipe;
7591 	u64 domains;
7592 	int ret;
7593 
7594 	if (!crtc_state->hw.active)
7595 		return;
7596 
7597 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7598 		const struct intel_plane_state *plane_state =
7599 			to_intel_plane_state(plane->base.state);
7600 
7601 		if (plane_state->uapi.visible)
7602 			intel_plane_disable_noatomic(crtc, plane);
7603 	}
7604 
7605 	state = drm_atomic_state_alloc(&dev_priv->drm);
7606 	if (!state) {
7607 		drm_dbg_kms(&dev_priv->drm,
7608 			    "failed to disable [CRTC:%d:%s], out of memory",
7609 			    crtc->base.base.id, crtc->base.name);
7610 		return;
7611 	}
7612 
7613 	state->acquire_ctx = ctx;
7614 
7615 	/* Everything's already locked, -EDEADLK can't happen. */
7616 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7617 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
7618 
7619 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
7620 
7621 	dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
7622 
7623 	drm_atomic_state_put(state);
7624 
7625 	drm_dbg_kms(&dev_priv->drm,
7626 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7627 		    crtc->base.base.id, crtc->base.name);
7628 
7629 	crtc->active = false;
7630 	crtc->base.enabled = false;
7631 
7632 	drm_WARN_ON(&dev_priv->drm,
7633 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
7634 	crtc_state->uapi.active = false;
7635 	crtc_state->uapi.connector_mask = 0;
7636 	crtc_state->uapi.encoder_mask = 0;
7637 	intel_crtc_free_hw_state(crtc_state);
7638 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
7639 
7640 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
7641 		encoder->base.crtc = NULL;
7642 
7643 	intel_fbc_disable(crtc);
7644 	intel_update_watermarks(crtc);
7645 	intel_disable_shared_dpll(crtc_state);
7646 
7647 	domains = crtc->enabled_power_domains;
7648 	for_each_power_domain(domain, domains)
7649 		intel_display_power_put_unchecked(dev_priv, domain);
7650 	crtc->enabled_power_domains = 0;
7651 
7652 	dev_priv->active_pipes &= ~BIT(pipe);
7653 	cdclk_state->min_cdclk[pipe] = 0;
7654 	cdclk_state->min_voltage_level[pipe] = 0;
7655 	cdclk_state->active_pipes &= ~BIT(pipe);
7656 
7657 	bw_state->data_rate[pipe] = 0;
7658 	bw_state->num_active_planes[pipe] = 0;
7659 }
7660 
7661 /*
7662  * turn all crtc's off, but do not adjust state
7663  * This has to be paired with a call to intel_modeset_setup_hw_state.
7664  */
7665 int intel_display_suspend(struct drm_device *dev)
7666 {
7667 	struct drm_i915_private *dev_priv = to_i915(dev);
7668 	struct drm_atomic_state *state;
7669 	int ret;
7670 
7671 	state = drm_atomic_helper_suspend(dev);
7672 	ret = PTR_ERR_OR_ZERO(state);
7673 	if (ret)
7674 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7675 			ret);
7676 	else
7677 		dev_priv->modeset_restore_state = state;
7678 	return ret;
7679 }
7680 
7681 void intel_encoder_destroy(struct drm_encoder *encoder)
7682 {
7683 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7684 
7685 	drm_encoder_cleanup(encoder);
7686 	kfree(intel_encoder);
7687 }
7688 
7689 /* Cross check the actual hw state with our own modeset state tracking (and it's
7690  * internal consistency). */
7691 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7692 					 struct drm_connector_state *conn_state)
7693 {
7694 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
7695 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
7696 
7697 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7698 		    connector->base.base.id, connector->base.name);
7699 
7700 	if (connector->get_hw_state(connector)) {
7701 		struct intel_encoder *encoder = intel_attached_encoder(connector);
7702 
7703 		I915_STATE_WARN(!crtc_state,
7704 			 "connector enabled without attached crtc\n");
7705 
7706 		if (!crtc_state)
7707 			return;
7708 
7709 		I915_STATE_WARN(!crtc_state->hw.active,
7710 				"connector is active, but attached crtc isn't\n");
7711 
7712 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7713 			return;
7714 
7715 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7716 			"atomic encoder doesn't match attached encoder\n");
7717 
7718 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7719 			"attached encoder crtc differs from connector crtc\n");
7720 	} else {
7721 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7722 				"attached crtc is active, but connector isn't\n");
7723 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7724 			"best encoder set without crtc!\n");
7725 	}
7726 }
7727 
7728 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7729 {
7730 	if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
7731 		return crtc_state->fdi_lanes;
7732 
7733 	return 0;
7734 }
7735 
7736 static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7737 			       struct intel_crtc_state *pipe_config)
7738 {
7739 	struct drm_i915_private *dev_priv = to_i915(dev);
7740 	struct drm_atomic_state *state = pipe_config->uapi.state;
7741 	struct intel_crtc *other_crtc;
7742 	struct intel_crtc_state *other_crtc_state;
7743 
7744 	drm_dbg_kms(&dev_priv->drm,
7745 		    "checking fdi config on pipe %c, lanes %i\n",
7746 		    pipe_name(pipe), pipe_config->fdi_lanes);
7747 	if (pipe_config->fdi_lanes > 4) {
7748 		drm_dbg_kms(&dev_priv->drm,
7749 			    "invalid fdi lane config on pipe %c: %i lanes\n",
7750 			    pipe_name(pipe), pipe_config->fdi_lanes);
7751 		return -EINVAL;
7752 	}
7753 
7754 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7755 		if (pipe_config->fdi_lanes > 2) {
7756 			drm_dbg_kms(&dev_priv->drm,
7757 				    "only 2 lanes on haswell, required: %i lanes\n",
7758 				    pipe_config->fdi_lanes);
7759 			return -EINVAL;
7760 		} else {
7761 			return 0;
7762 		}
7763 	}
7764 
7765 	if (INTEL_NUM_PIPES(dev_priv) == 2)
7766 		return 0;
7767 
7768 	/* Ivybridge 3 pipe is really complicated */
7769 	switch (pipe) {
7770 	case PIPE_A:
7771 		return 0;
7772 	case PIPE_B:
7773 		if (pipe_config->fdi_lanes <= 2)
7774 			return 0;
7775 
7776 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
7777 		other_crtc_state =
7778 			intel_atomic_get_crtc_state(state, other_crtc);
7779 		if (IS_ERR(other_crtc_state))
7780 			return PTR_ERR(other_crtc_state);
7781 
7782 		if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
7783 			drm_dbg_kms(&dev_priv->drm,
7784 				    "invalid shared fdi lane config on pipe %c: %i lanes\n",
7785 				    pipe_name(pipe), pipe_config->fdi_lanes);
7786 			return -EINVAL;
7787 		}
7788 		return 0;
7789 	case PIPE_C:
7790 		if (pipe_config->fdi_lanes > 2) {
7791 			drm_dbg_kms(&dev_priv->drm,
7792 				    "only 2 lanes on pipe %c: required %i lanes\n",
7793 				    pipe_name(pipe), pipe_config->fdi_lanes);
7794 			return -EINVAL;
7795 		}
7796 
7797 		other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
7798 		other_crtc_state =
7799 			intel_atomic_get_crtc_state(state, other_crtc);
7800 		if (IS_ERR(other_crtc_state))
7801 			return PTR_ERR(other_crtc_state);
7802 
7803 		if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
7804 			drm_dbg_kms(&dev_priv->drm,
7805 				    "fdi link B uses too many lanes to enable link C\n");
7806 			return -EINVAL;
7807 		}
7808 		return 0;
7809 	default:
7810 		BUG();
7811 	}
7812 }
7813 
7814 #define RETRY 1
7815 static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
7816 				  struct intel_crtc_state *pipe_config)
7817 {
7818 	struct drm_device *dev = intel_crtc->base.dev;
7819 	struct drm_i915_private *i915 = to_i915(dev);
7820 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7821 	int lane, link_bw, fdi_dotclock, ret;
7822 	bool needs_recompute = false;
7823 
7824 retry:
7825 	/* FDI is a binary signal running at ~2.7GHz, encoding
7826 	 * each output octet as 10 bits. The actual frequency
7827 	 * is stored as a divider into a 100MHz clock, and the
7828 	 * mode pixel clock is stored in units of 1KHz.
7829 	 * Hence the bw of each lane in terms of the mode signal
7830 	 * is:
7831 	 */
7832 	link_bw = intel_fdi_link_freq(i915, pipe_config);
7833 
7834 	fdi_dotclock = adjusted_mode->crtc_clock;
7835 
7836 	lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
7837 				      pipe_config->pipe_bpp);
7838 
7839 	pipe_config->fdi_lanes = lane;
7840 
7841 	intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
7842 			       link_bw, &pipe_config->fdi_m_n, false, false);
7843 
7844 	ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
7845 	if (ret == -EDEADLK)
7846 		return ret;
7847 
7848 	if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
7849 		pipe_config->pipe_bpp -= 2*3;
7850 		drm_dbg_kms(&i915->drm,
7851 			    "fdi link bw constraint, reducing pipe bpp to %i\n",
7852 			    pipe_config->pipe_bpp);
7853 		needs_recompute = true;
7854 		pipe_config->bw_constrained = true;
7855 
7856 		goto retry;
7857 	}
7858 
7859 	if (needs_recompute)
7860 		return RETRY;
7861 
7862 	return ret;
7863 }
7864 
7865 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
7866 {
7867 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7868 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7869 
7870 	/* IPS only exists on ULT machines and is tied to pipe A. */
7871 	if (!hsw_crtc_supports_ips(crtc))
7872 		return false;
7873 
7874 	if (!i915_modparams.enable_ips)
7875 		return false;
7876 
7877 	if (crtc_state->pipe_bpp > 24)
7878 		return false;
7879 
7880 	/*
7881 	 * We compare against max which means we must take
7882 	 * the increased cdclk requirement into account when
7883 	 * calculating the new cdclk.
7884 	 *
7885 	 * Should measure whether using a lower cdclk w/o IPS
7886 	 */
7887 	if (IS_BROADWELL(dev_priv) &&
7888 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
7889 		return false;
7890 
7891 	return true;
7892 }
7893 
7894 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
7895 {
7896 	struct drm_i915_private *dev_priv =
7897 		to_i915(crtc_state->uapi.crtc->dev);
7898 	struct intel_atomic_state *state =
7899 		to_intel_atomic_state(crtc_state->uapi.state);
7900 
7901 	crtc_state->ips_enabled = false;
7902 
7903 	if (!hsw_crtc_state_ips_capable(crtc_state))
7904 		return 0;
7905 
7906 	/*
7907 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
7908 	 * enabled and disabled dynamically based on package C states,
7909 	 * user space can't make reliable use of the CRCs, so let's just
7910 	 * completely disable it.
7911 	 */
7912 	if (crtc_state->crc_enabled)
7913 		return 0;
7914 
7915 	/* IPS should be fine as long as at least one plane is enabled. */
7916 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
7917 		return 0;
7918 
7919 	if (IS_BROADWELL(dev_priv)) {
7920 		const struct intel_cdclk_state *cdclk_state;
7921 
7922 		cdclk_state = intel_atomic_get_cdclk_state(state);
7923 		if (IS_ERR(cdclk_state))
7924 			return PTR_ERR(cdclk_state);
7925 
7926 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
7927 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
7928 			return 0;
7929 	}
7930 
7931 	crtc_state->ips_enabled = true;
7932 
7933 	return 0;
7934 }
7935 
7936 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7937 {
7938 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7939 
7940 	/* GDG double wide on either pipe, otherwise pipe A only */
7941 	return INTEL_GEN(dev_priv) < 4 &&
7942 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7943 }
7944 
7945 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
7946 {
7947 	u32 pixel_rate;
7948 
7949 	pixel_rate = pipe_config->hw.adjusted_mode.crtc_clock;
7950 
7951 	/*
7952 	 * We only use IF-ID interlacing. If we ever use
7953 	 * PF-ID we'll need to adjust the pixel_rate here.
7954 	 */
7955 
7956 	if (pipe_config->pch_pfit.enabled) {
7957 		u64 pipe_w, pipe_h, pfit_w, pfit_h;
7958 		u32 pfit_size = pipe_config->pch_pfit.size;
7959 
7960 		pipe_w = pipe_config->pipe_src_w;
7961 		pipe_h = pipe_config->pipe_src_h;
7962 
7963 		pfit_w = (pfit_size >> 16) & 0xFFFF;
7964 		pfit_h = pfit_size & 0xFFFF;
7965 		if (pipe_w < pfit_w)
7966 			pipe_w = pfit_w;
7967 		if (pipe_h < pfit_h)
7968 			pipe_h = pfit_h;
7969 
7970 		if (WARN_ON(!pfit_w || !pfit_h))
7971 			return pixel_rate;
7972 
7973 		pixel_rate = div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
7974 				     pfit_w * pfit_h);
7975 	}
7976 
7977 	return pixel_rate;
7978 }
7979 
7980 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
7981 {
7982 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
7983 
7984 	if (HAS_GMCH(dev_priv))
7985 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
7986 		crtc_state->pixel_rate =
7987 			crtc_state->hw.adjusted_mode.crtc_clock;
7988 	else
7989 		crtc_state->pixel_rate =
7990 			ilk_pipe_pixel_rate(crtc_state);
7991 }
7992 
7993 static int intel_crtc_compute_config(struct intel_crtc *crtc,
7994 				     struct intel_crtc_state *pipe_config)
7995 {
7996 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7997 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7998 	int clock_limit = dev_priv->max_dotclk_freq;
7999 
8000 	if (INTEL_GEN(dev_priv) < 4) {
8001 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
8002 
8003 		/*
8004 		 * Enable double wide mode when the dot clock
8005 		 * is > 90% of the (display) core speed.
8006 		 */
8007 		if (intel_crtc_supports_double_wide(crtc) &&
8008 		    adjusted_mode->crtc_clock > clock_limit) {
8009 			clock_limit = dev_priv->max_dotclk_freq;
8010 			pipe_config->double_wide = true;
8011 		}
8012 	}
8013 
8014 	if (adjusted_mode->crtc_clock > clock_limit) {
8015 		drm_dbg_kms(&dev_priv->drm,
8016 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
8017 			    adjusted_mode->crtc_clock, clock_limit,
8018 			    yesno(pipe_config->double_wide));
8019 		return -EINVAL;
8020 	}
8021 
8022 	if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8023 	     pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
8024 	     pipe_config->hw.ctm) {
8025 		/*
8026 		 * There is only one pipe CSC unit per pipe, and we need that
8027 		 * for output conversion from RGB->YCBCR. So if CTM is already
8028 		 * applied we can't support YCBCR420 output.
8029 		 */
8030 		drm_dbg_kms(&dev_priv->drm,
8031 			    "YCBCR420 and CTM together are not possible\n");
8032 		return -EINVAL;
8033 	}
8034 
8035 	/*
8036 	 * Pipe horizontal size must be even in:
8037 	 * - DVO ganged mode
8038 	 * - LVDS dual channel mode
8039 	 * - Double wide pipe
8040 	 */
8041 	if (pipe_config->pipe_src_w & 1) {
8042 		if (pipe_config->double_wide) {
8043 			drm_dbg_kms(&dev_priv->drm,
8044 				    "Odd pipe source width not supported with double wide pipe\n");
8045 			return -EINVAL;
8046 		}
8047 
8048 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
8049 		    intel_is_dual_link_lvds(dev_priv)) {
8050 			drm_dbg_kms(&dev_priv->drm,
8051 				    "Odd pipe source width not supported with dual link LVDS\n");
8052 			return -EINVAL;
8053 		}
8054 	}
8055 
8056 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
8057 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8058 	 */
8059 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8060 		adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
8061 		return -EINVAL;
8062 
8063 	intel_crtc_compute_pixel_rate(pipe_config);
8064 
8065 	if (pipe_config->has_pch_encoder)
8066 		return ilk_fdi_compute_config(crtc, pipe_config);
8067 
8068 	return 0;
8069 }
8070 
8071 static void
8072 intel_reduce_m_n_ratio(u32 *num, u32 *den)
8073 {
8074 	while (*num > DATA_LINK_M_N_MASK ||
8075 	       *den > DATA_LINK_M_N_MASK) {
8076 		*num >>= 1;
8077 		*den >>= 1;
8078 	}
8079 }
8080 
8081 static void compute_m_n(unsigned int m, unsigned int n,
8082 			u32 *ret_m, u32 *ret_n,
8083 			bool constant_n)
8084 {
8085 	/*
8086 	 * Several DP dongles in particular seem to be fussy about
8087 	 * too large link M/N values. Give N value as 0x8000 that
8088 	 * should be acceptable by specific devices. 0x8000 is the
8089 	 * specified fixed N value for asynchronous clock mode,
8090 	 * which the devices expect also in synchronous clock mode.
8091 	 */
8092 	if (constant_n)
8093 		*ret_n = 0x8000;
8094 	else
8095 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
8096 
8097 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
8098 	intel_reduce_m_n_ratio(ret_m, ret_n);
8099 }
8100 
8101 void
8102 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
8103 		       int pixel_clock, int link_clock,
8104 		       struct intel_link_m_n *m_n,
8105 		       bool constant_n, bool fec_enable)
8106 {
8107 	u32 data_clock = bits_per_pixel * pixel_clock;
8108 
8109 	if (fec_enable)
8110 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
8111 
8112 	m_n->tu = 64;
8113 	compute_m_n(data_clock,
8114 		    link_clock * nlanes * 8,
8115 		    &m_n->gmch_m, &m_n->gmch_n,
8116 		    constant_n);
8117 
8118 	compute_m_n(pixel_clock, link_clock,
8119 		    &m_n->link_m, &m_n->link_n,
8120 		    constant_n);
8121 }
8122 
8123 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8124 {
8125 	/*
8126 	 * There may be no VBT; and if the BIOS enabled SSC we can
8127 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
8128 	 * BIOS isn't using it, don't assume it will work even if the VBT
8129 	 * indicates as much.
8130 	 */
8131 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
8132 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8133 						       PCH_DREF_CONTROL) &
8134 			DREF_SSC1_ENABLE;
8135 
8136 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
8137 			drm_dbg_kms(&dev_priv->drm,
8138 				    "SSC %s by BIOS, overriding VBT which says %s\n",
8139 				    enableddisabled(bios_lvds_use_ssc),
8140 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
8141 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8142 		}
8143 	}
8144 }
8145 
8146 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
8147 {
8148 	if (i915_modparams.panel_use_ssc >= 0)
8149 		return i915_modparams.panel_use_ssc != 0;
8150 	return dev_priv->vbt.lvds_use_ssc
8151 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
8152 }
8153 
8154 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
8155 {
8156 	return (1 << dpll->n) << 16 | dpll->m2;
8157 }
8158 
8159 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
8160 {
8161 	return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
8162 }
8163 
8164 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
8165 				     struct intel_crtc_state *crtc_state,
8166 				     struct dpll *reduced_clock)
8167 {
8168 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8169 	u32 fp, fp2 = 0;
8170 
8171 	if (IS_PINEVIEW(dev_priv)) {
8172 		fp = pnv_dpll_compute_fp(&crtc_state->dpll);
8173 		if (reduced_clock)
8174 			fp2 = pnv_dpll_compute_fp(reduced_clock);
8175 	} else {
8176 		fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8177 		if (reduced_clock)
8178 			fp2 = i9xx_dpll_compute_fp(reduced_clock);
8179 	}
8180 
8181 	crtc_state->dpll_hw_state.fp0 = fp;
8182 
8183 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8184 	    reduced_clock) {
8185 		crtc_state->dpll_hw_state.fp1 = fp2;
8186 	} else {
8187 		crtc_state->dpll_hw_state.fp1 = fp;
8188 	}
8189 }
8190 
8191 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8192 		pipe)
8193 {
8194 	u32 reg_val;
8195 
8196 	/*
8197 	 * PLLB opamp always calibrates to max value of 0x3f, force enable it
8198 	 * and set it to a reasonable value instead.
8199 	 */
8200 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8201 	reg_val &= 0xffffff00;
8202 	reg_val |= 0x00000030;
8203 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8204 
8205 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8206 	reg_val &= 0x00ffffff;
8207 	reg_val |= 0x8c000000;
8208 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8209 
8210 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8211 	reg_val &= 0xffffff00;
8212 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8213 
8214 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8215 	reg_val &= 0x00ffffff;
8216 	reg_val |= 0xb0000000;
8217 	vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8218 }
8219 
8220 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8221 					 const struct intel_link_m_n *m_n)
8222 {
8223 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8224 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8225 	enum pipe pipe = crtc->pipe;
8226 
8227 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8228 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
8229 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8230 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8231 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
8232 }
8233 
8234 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8235 				 enum transcoder transcoder)
8236 {
8237 	if (IS_HASWELL(dev_priv))
8238 		return transcoder == TRANSCODER_EDP;
8239 
8240 	/*
8241 	 * Strictly speaking some registers are available before
8242 	 * gen7, but we only support DRRS on gen7+
8243 	 */
8244 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
8245 }
8246 
8247 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8248 					 const struct intel_link_m_n *m_n,
8249 					 const struct intel_link_m_n *m2_n2)
8250 {
8251 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8252 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8253 	enum pipe pipe = crtc->pipe;
8254 	enum transcoder transcoder = crtc_state->cpu_transcoder;
8255 
8256 	if (INTEL_GEN(dev_priv) >= 5) {
8257 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8258 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8259 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8260 			       m_n->gmch_n);
8261 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8262 			       m_n->link_m);
8263 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8264 			       m_n->link_n);
8265 		/*
8266 		 *  M2_N2 registers are set only if DRRS is supported
8267 		 * (to make sure the registers are not unnecessarily accessed).
8268 		 */
8269 		if (m2_n2 && crtc_state->has_drrs &&
8270 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
8271 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8272 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8273 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8274 				       m2_n2->gmch_n);
8275 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8276 				       m2_n2->link_m);
8277 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8278 				       m2_n2->link_n);
8279 		}
8280 	} else {
8281 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8282 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
8283 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8284 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8285 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
8286 	}
8287 }
8288 
8289 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
8290 {
8291 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
8292 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
8293 
8294 	if (m_n == M1_N1) {
8295 		dp_m_n = &crtc_state->dp_m_n;
8296 		dp_m2_n2 = &crtc_state->dp_m2_n2;
8297 	} else if (m_n == M2_N2) {
8298 
8299 		/*
8300 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
8301 		 * needs to be programmed into M1_N1.
8302 		 */
8303 		dp_m_n = &crtc_state->dp_m2_n2;
8304 	} else {
8305 		drm_err(&i915->drm, "Unsupported divider value\n");
8306 		return;
8307 	}
8308 
8309 	if (crtc_state->has_pch_encoder)
8310 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
8311 	else
8312 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
8313 }
8314 
8315 static void vlv_compute_dpll(struct intel_crtc *crtc,
8316 			     struct intel_crtc_state *pipe_config)
8317 {
8318 	pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
8319 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8320 	if (crtc->pipe != PIPE_A)
8321 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8322 
8323 	/* DPLL not used with DSI, but still need the rest set up */
8324 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8325 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8326 			DPLL_EXT_BUFFER_ENABLE_VLV;
8327 
8328 	pipe_config->dpll_hw_state.dpll_md =
8329 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8330 }
8331 
8332 static void chv_compute_dpll(struct intel_crtc *crtc,
8333 			     struct intel_crtc_state *pipe_config)
8334 {
8335 	pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
8336 		DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8337 	if (crtc->pipe != PIPE_A)
8338 		pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8339 
8340 	/* DPLL not used with DSI, but still need the rest set up */
8341 	if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8342 		pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8343 
8344 	pipe_config->dpll_hw_state.dpll_md =
8345 		(pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8346 }
8347 
8348 static void vlv_prepare_pll(struct intel_crtc *crtc,
8349 			    const struct intel_crtc_state *pipe_config)
8350 {
8351 	struct drm_device *dev = crtc->base.dev;
8352 	struct drm_i915_private *dev_priv = to_i915(dev);
8353 	enum pipe pipe = crtc->pipe;
8354 	u32 mdiv;
8355 	u32 bestn, bestm1, bestm2, bestp1, bestp2;
8356 	u32 coreclk, reg_val;
8357 
8358 	/* Enable Refclk */
8359 	intel_de_write(dev_priv, DPLL(pipe),
8360 		       pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
8361 
8362 	/* No need to actually set up the DPLL with DSI */
8363 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8364 		return;
8365 
8366 	vlv_dpio_get(dev_priv);
8367 
8368 	bestn = pipe_config->dpll.n;
8369 	bestm1 = pipe_config->dpll.m1;
8370 	bestm2 = pipe_config->dpll.m2;
8371 	bestp1 = pipe_config->dpll.p1;
8372 	bestp2 = pipe_config->dpll.p2;
8373 
8374 	/* See eDP HDMI DPIO driver vbios notes doc */
8375 
8376 	/* PLL B needs special handling */
8377 	if (pipe == PIPE_B)
8378 		vlv_pllb_recal_opamp(dev_priv, pipe);
8379 
8380 	/* Set up Tx target for periodic Rcomp update */
8381 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
8382 
8383 	/* Disable target IRef on PLL */
8384 	reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
8385 	reg_val &= 0x00ffffff;
8386 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
8387 
8388 	/* Disable fast lock */
8389 	vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
8390 
8391 	/* Set idtafcrecal before PLL is enabled */
8392 	mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8393 	mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8394 	mdiv |= ((bestn << DPIO_N_SHIFT));
8395 	mdiv |= (1 << DPIO_K_SHIFT);
8396 
8397 	/*
8398 	 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8399 	 * but we don't support that).
8400 	 * Note: don't use the DAC post divider as it seems unstable.
8401 	 */
8402 	mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
8403 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8404 
8405 	mdiv |= DPIO_ENABLE_CALIBRATION;
8406 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8407 
8408 	/* Set HBR and RBR LPF coefficients */
8409 	if (pipe_config->port_clock == 162000 ||
8410 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8411 	    intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
8412 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8413 				 0x009f0003);
8414 	else
8415 		vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8416 				 0x00d0000f);
8417 
8418 	if (intel_crtc_has_dp_encoder(pipe_config)) {
8419 		/* Use SSC source */
8420 		if (pipe == PIPE_A)
8421 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8422 					 0x0df40000);
8423 		else
8424 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8425 					 0x0df70000);
8426 	} else { /* HDMI or VGA */
8427 		/* Use bend source */
8428 		if (pipe == PIPE_A)
8429 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8430 					 0x0df70000);
8431 		else
8432 			vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8433 					 0x0df40000);
8434 	}
8435 
8436 	coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
8437 	coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
8438 	if (intel_crtc_has_dp_encoder(pipe_config))
8439 		coreclk |= 0x01000000;
8440 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
8441 
8442 	vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
8443 
8444 	vlv_dpio_put(dev_priv);
8445 }
8446 
8447 static void chv_prepare_pll(struct intel_crtc *crtc,
8448 			    const struct intel_crtc_state *pipe_config)
8449 {
8450 	struct drm_device *dev = crtc->base.dev;
8451 	struct drm_i915_private *dev_priv = to_i915(dev);
8452 	enum pipe pipe = crtc->pipe;
8453 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
8454 	u32 loopfilter, tribuf_calcntr;
8455 	u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
8456 	u32 dpio_val;
8457 	int vco;
8458 
8459 	/* Enable Refclk and SSC */
8460 	intel_de_write(dev_priv, DPLL(pipe),
8461 		       pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8462 
8463 	/* No need to actually set up the DPLL with DSI */
8464 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8465 		return;
8466 
8467 	bestn = pipe_config->dpll.n;
8468 	bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8469 	bestm1 = pipe_config->dpll.m1;
8470 	bestm2 = pipe_config->dpll.m2 >> 22;
8471 	bestp1 = pipe_config->dpll.p1;
8472 	bestp2 = pipe_config->dpll.p2;
8473 	vco = pipe_config->dpll.vco;
8474 	dpio_val = 0;
8475 	loopfilter = 0;
8476 
8477 	vlv_dpio_get(dev_priv);
8478 
8479 	/* p1 and p2 divider */
8480 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8481 			5 << DPIO_CHV_S1_DIV_SHIFT |
8482 			bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8483 			bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8484 			1 << DPIO_CHV_K_DIV_SHIFT);
8485 
8486 	/* Feedback post-divider - m2 */
8487 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8488 
8489 	/* Feedback refclk divider - n and m1 */
8490 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8491 			DPIO_CHV_M1_DIV_BY_2 |
8492 			1 << DPIO_CHV_N_DIV_SHIFT);
8493 
8494 	/* M2 fraction division */
8495 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
8496 
8497 	/* M2 fraction division enable */
8498 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8499 	dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8500 	dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8501 	if (bestm2_frac)
8502 		dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8503 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
8504 
8505 	/* Program digital lock detect threshold */
8506 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8507 	dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8508 					DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8509 	dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8510 	if (!bestm2_frac)
8511 		dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8512 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8513 
8514 	/* Loop filter */
8515 	if (vco == 5400000) {
8516 		loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8517 		loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8518 		loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8519 		tribuf_calcntr = 0x9;
8520 	} else if (vco <= 6200000) {
8521 		loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8522 		loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8523 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8524 		tribuf_calcntr = 0x9;
8525 	} else if (vco <= 6480000) {
8526 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8527 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8528 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8529 		tribuf_calcntr = 0x8;
8530 	} else {
8531 		/* Not supported. Apply the same limits as in the max case */
8532 		loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8533 		loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8534 		loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8535 		tribuf_calcntr = 0;
8536 	}
8537 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8538 
8539 	dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
8540 	dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8541 	dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8542 	vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8543 
8544 	/* AFC Recal */
8545 	vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8546 			vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8547 			DPIO_AFC_RECAL);
8548 
8549 	vlv_dpio_put(dev_priv);
8550 }
8551 
8552 /**
8553  * vlv_force_pll_on - forcibly enable just the PLL
8554  * @dev_priv: i915 private structure
8555  * @pipe: pipe PLL to enable
8556  * @dpll: PLL configuration
8557  *
8558  * Enable the PLL for @pipe using the supplied @dpll config. To be used
8559  * in cases where we need the PLL enabled even when @pipe is not going to
8560  * be enabled.
8561  */
8562 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
8563 		     const struct dpll *dpll)
8564 {
8565 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
8566 	struct intel_crtc_state *pipe_config;
8567 
8568 	pipe_config = intel_crtc_state_alloc(crtc);
8569 	if (!pipe_config)
8570 		return -ENOMEM;
8571 
8572 	pipe_config->cpu_transcoder = (enum transcoder)pipe;
8573 	pipe_config->pixel_multiplier = 1;
8574 	pipe_config->dpll = *dpll;
8575 
8576 	if (IS_CHERRYVIEW(dev_priv)) {
8577 		chv_compute_dpll(crtc, pipe_config);
8578 		chv_prepare_pll(crtc, pipe_config);
8579 		chv_enable_pll(crtc, pipe_config);
8580 	} else {
8581 		vlv_compute_dpll(crtc, pipe_config);
8582 		vlv_prepare_pll(crtc, pipe_config);
8583 		vlv_enable_pll(crtc, pipe_config);
8584 	}
8585 
8586 	kfree(pipe_config);
8587 
8588 	return 0;
8589 }
8590 
8591 /**
8592  * vlv_force_pll_off - forcibly disable just the PLL
8593  * @dev_priv: i915 private structure
8594  * @pipe: pipe PLL to disable
8595  *
8596  * Disable the PLL for @pipe. To be used in cases where we need
8597  * the PLL enabled even when @pipe is not going to be enabled.
8598  */
8599 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
8600 {
8601 	if (IS_CHERRYVIEW(dev_priv))
8602 		chv_disable_pll(dev_priv, pipe);
8603 	else
8604 		vlv_disable_pll(dev_priv, pipe);
8605 }
8606 
8607 static void i9xx_compute_dpll(struct intel_crtc *crtc,
8608 			      struct intel_crtc_state *crtc_state,
8609 			      struct dpll *reduced_clock)
8610 {
8611 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8612 	u32 dpll;
8613 	struct dpll *clock = &crtc_state->dpll;
8614 
8615 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8616 
8617 	dpll = DPLL_VGA_MODE_DIS;
8618 
8619 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
8620 		dpll |= DPLLB_MODE_LVDS;
8621 	else
8622 		dpll |= DPLLB_MODE_DAC_SERIAL;
8623 
8624 	if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8625 	    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8626 		dpll |= (crtc_state->pixel_multiplier - 1)
8627 			<< SDVO_MULTIPLIER_SHIFT_HIRES;
8628 	}
8629 
8630 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8631 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8632 		dpll |= DPLL_SDVO_HIGH_SPEED;
8633 
8634 	if (intel_crtc_has_dp_encoder(crtc_state))
8635 		dpll |= DPLL_SDVO_HIGH_SPEED;
8636 
8637 	/* compute bitmask from p1 value */
8638 	if (IS_PINEVIEW(dev_priv))
8639 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8640 	else {
8641 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8642 		if (IS_G4X(dev_priv) && reduced_clock)
8643 			dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8644 	}
8645 	switch (clock->p2) {
8646 	case 5:
8647 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8648 		break;
8649 	case 7:
8650 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8651 		break;
8652 	case 10:
8653 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8654 		break;
8655 	case 14:
8656 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8657 		break;
8658 	}
8659 	if (INTEL_GEN(dev_priv) >= 4)
8660 		dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8661 
8662 	if (crtc_state->sdvo_tv_clock)
8663 		dpll |= PLL_REF_INPUT_TVCLKINBC;
8664 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8665 		 intel_panel_use_ssc(dev_priv))
8666 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8667 	else
8668 		dpll |= PLL_REF_INPUT_DREFCLK;
8669 
8670 	dpll |= DPLL_VCO_ENABLE;
8671 	crtc_state->dpll_hw_state.dpll = dpll;
8672 
8673 	if (INTEL_GEN(dev_priv) >= 4) {
8674 		u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8675 			<< DPLL_MD_UDI_MULTIPLIER_SHIFT;
8676 		crtc_state->dpll_hw_state.dpll_md = dpll_md;
8677 	}
8678 }
8679 
8680 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8681 			      struct intel_crtc_state *crtc_state,
8682 			      struct dpll *reduced_clock)
8683 {
8684 	struct drm_device *dev = crtc->base.dev;
8685 	struct drm_i915_private *dev_priv = to_i915(dev);
8686 	u32 dpll;
8687 	struct dpll *clock = &crtc_state->dpll;
8688 
8689 	i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8690 
8691 	dpll = DPLL_VGA_MODE_DIS;
8692 
8693 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8694 		dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8695 	} else {
8696 		if (clock->p1 == 2)
8697 			dpll |= PLL_P1_DIVIDE_BY_TWO;
8698 		else
8699 			dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8700 		if (clock->p2 == 4)
8701 			dpll |= PLL_P2_DIVIDE_BY_4;
8702 	}
8703 
8704 	/*
8705 	 * Bspec:
8706 	 * "[Almador Errata}: For the correct operation of the muxed DVO pins
8707 	 *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8708 	 *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8709 	 *  Enable) must be set to “1” in both the DPLL A Control Register
8710 	 *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8711 	 *
8712 	 * For simplicity We simply keep both bits always enabled in
8713 	 * both DPLLS. The spec says we should disable the DVO 2X clock
8714 	 * when not needed, but this seems to work fine in practice.
8715 	 */
8716 	if (IS_I830(dev_priv) ||
8717 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8718 		dpll |= DPLL_DVO_2X_MODE;
8719 
8720 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8721 	    intel_panel_use_ssc(dev_priv))
8722 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8723 	else
8724 		dpll |= PLL_REF_INPUT_DREFCLK;
8725 
8726 	dpll |= DPLL_VCO_ENABLE;
8727 	crtc_state->dpll_hw_state.dpll = dpll;
8728 }
8729 
8730 static void intel_set_pipe_timings(const struct intel_crtc_state *crtc_state)
8731 {
8732 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8733 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8734 	enum pipe pipe = crtc->pipe;
8735 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8736 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8737 	u32 crtc_vtotal, crtc_vblank_end;
8738 	int vsyncshift = 0;
8739 
8740 	/* We need to be careful not to changed the adjusted mode, for otherwise
8741 	 * the hw state checker will get angry at the mismatch. */
8742 	crtc_vtotal = adjusted_mode->crtc_vtotal;
8743 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8744 
8745 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8746 		/* the chip adds 2 halflines automatically */
8747 		crtc_vtotal -= 1;
8748 		crtc_vblank_end -= 1;
8749 
8750 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8751 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8752 		else
8753 			vsyncshift = adjusted_mode->crtc_hsync_start -
8754 				adjusted_mode->crtc_htotal / 2;
8755 		if (vsyncshift < 0)
8756 			vsyncshift += adjusted_mode->crtc_htotal;
8757 	}
8758 
8759 	if (INTEL_GEN(dev_priv) > 3)
8760 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
8761 		               vsyncshift);
8762 
8763 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
8764 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
8765 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
8766 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
8767 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
8768 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
8769 
8770 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
8771 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
8772 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
8773 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
8774 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
8775 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
8776 
8777 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8778 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8779 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8780 	 * bits. */
8781 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
8782 	    (pipe == PIPE_B || pipe == PIPE_C))
8783 		intel_de_write(dev_priv, VTOTAL(pipe),
8784 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
8785 
8786 }
8787 
8788 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
8789 {
8790 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8791 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8792 	enum pipe pipe = crtc->pipe;
8793 
8794 	/* pipesrc controls the size that is scaled from, which should
8795 	 * always be the user's requested size.
8796 	 */
8797 	intel_de_write(dev_priv, PIPESRC(pipe),
8798 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
8799 }
8800 
8801 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
8802 {
8803 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8804 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8805 
8806 	if (IS_GEN(dev_priv, 2))
8807 		return false;
8808 
8809 	if (INTEL_GEN(dev_priv) >= 9 ||
8810 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
8811 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
8812 	else
8813 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
8814 }
8815 
8816 static void intel_get_pipe_timings(struct intel_crtc *crtc,
8817 				   struct intel_crtc_state *pipe_config)
8818 {
8819 	struct drm_device *dev = crtc->base.dev;
8820 	struct drm_i915_private *dev_priv = to_i915(dev);
8821 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8822 	u32 tmp;
8823 
8824 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
8825 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8826 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
8827 
8828 	if (!transcoder_is_dsi(cpu_transcoder)) {
8829 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
8830 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
8831 							(tmp & 0xffff) + 1;
8832 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
8833 						((tmp >> 16) & 0xffff) + 1;
8834 	}
8835 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
8836 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8837 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
8838 
8839 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
8840 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8841 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
8842 
8843 	if (!transcoder_is_dsi(cpu_transcoder)) {
8844 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
8845 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
8846 							(tmp & 0xffff) + 1;
8847 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
8848 						((tmp >> 16) & 0xffff) + 1;
8849 	}
8850 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
8851 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8852 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
8853 
8854 	if (intel_pipe_is_interlaced(pipe_config)) {
8855 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8856 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
8857 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
8858 	}
8859 }
8860 
8861 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8862 				    struct intel_crtc_state *pipe_config)
8863 {
8864 	struct drm_device *dev = crtc->base.dev;
8865 	struct drm_i915_private *dev_priv = to_i915(dev);
8866 	u32 tmp;
8867 
8868 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
8869 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8870 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8871 
8872 	pipe_config->hw.mode.vdisplay = pipe_config->pipe_src_h;
8873 	pipe_config->hw.mode.hdisplay = pipe_config->pipe_src_w;
8874 }
8875 
8876 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
8877 				 struct intel_crtc_state *pipe_config)
8878 {
8879 	mode->hdisplay = pipe_config->hw.adjusted_mode.crtc_hdisplay;
8880 	mode->htotal = pipe_config->hw.adjusted_mode.crtc_htotal;
8881 	mode->hsync_start = pipe_config->hw.adjusted_mode.crtc_hsync_start;
8882 	mode->hsync_end = pipe_config->hw.adjusted_mode.crtc_hsync_end;
8883 
8884 	mode->vdisplay = pipe_config->hw.adjusted_mode.crtc_vdisplay;
8885 	mode->vtotal = pipe_config->hw.adjusted_mode.crtc_vtotal;
8886 	mode->vsync_start = pipe_config->hw.adjusted_mode.crtc_vsync_start;
8887 	mode->vsync_end = pipe_config->hw.adjusted_mode.crtc_vsync_end;
8888 
8889 	mode->flags = pipe_config->hw.adjusted_mode.flags;
8890 	mode->type = DRM_MODE_TYPE_DRIVER;
8891 
8892 	mode->clock = pipe_config->hw.adjusted_mode.crtc_clock;
8893 
8894 	mode->hsync = drm_mode_hsync(mode);
8895 	mode->vrefresh = drm_mode_vrefresh(mode);
8896 	drm_mode_set_name(mode);
8897 }
8898 
8899 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
8900 {
8901 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8902 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8903 	u32 pipeconf;
8904 
8905 	pipeconf = 0;
8906 
8907 	/* we keep both pipes enabled on 830 */
8908 	if (IS_I830(dev_priv))
8909 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
8910 
8911 	if (crtc_state->double_wide)
8912 		pipeconf |= PIPECONF_DOUBLE_WIDE;
8913 
8914 	/* only g4x and later have fancy bpc/dither controls */
8915 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8916 	    IS_CHERRYVIEW(dev_priv)) {
8917 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
8918 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
8919 			pipeconf |= PIPECONF_DITHER_EN |
8920 				    PIPECONF_DITHER_TYPE_SP;
8921 
8922 		switch (crtc_state->pipe_bpp) {
8923 		case 18:
8924 			pipeconf |= PIPECONF_6BPC;
8925 			break;
8926 		case 24:
8927 			pipeconf |= PIPECONF_8BPC;
8928 			break;
8929 		case 30:
8930 			pipeconf |= PIPECONF_10BPC;
8931 			break;
8932 		default:
8933 			/* Case prevented by intel_choose_pipe_bpp_dither. */
8934 			BUG();
8935 		}
8936 	}
8937 
8938 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
8939 		if (INTEL_GEN(dev_priv) < 4 ||
8940 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8941 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8942 		else
8943 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8944 	} else {
8945 		pipeconf |= PIPECONF_PROGRESSIVE;
8946 	}
8947 
8948 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
8949 	     crtc_state->limited_color_range)
8950 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
8951 
8952 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
8953 
8954 	pipeconf |= PIPECONF_FRAME_START_DELAY(0);
8955 
8956 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
8957 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
8958 }
8959 
8960 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8961 				   struct intel_crtc_state *crtc_state)
8962 {
8963 	struct drm_device *dev = crtc->base.dev;
8964 	struct drm_i915_private *dev_priv = to_i915(dev);
8965 	const struct intel_limit *limit;
8966 	int refclk = 48000;
8967 
8968 	memset(&crtc_state->dpll_hw_state, 0,
8969 	       sizeof(crtc_state->dpll_hw_state));
8970 
8971 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8972 		if (intel_panel_use_ssc(dev_priv)) {
8973 			refclk = dev_priv->vbt.lvds_ssc_freq;
8974 			drm_dbg_kms(&dev_priv->drm,
8975 				    "using SSC reference clock of %d kHz\n",
8976 				    refclk);
8977 		}
8978 
8979 		limit = &intel_limits_i8xx_lvds;
8980 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
8981 		limit = &intel_limits_i8xx_dvo;
8982 	} else {
8983 		limit = &intel_limits_i8xx_dac;
8984 	}
8985 
8986 	if (!crtc_state->clock_set &&
8987 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8988 				 refclk, NULL, &crtc_state->dpll)) {
8989 		drm_err(&dev_priv->drm,
8990 			"Couldn't find PLL settings for mode!\n");
8991 		return -EINVAL;
8992 	}
8993 
8994 	i8xx_compute_dpll(crtc, crtc_state, NULL);
8995 
8996 	return 0;
8997 }
8998 
8999 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
9000 				  struct intel_crtc_state *crtc_state)
9001 {
9002 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9003 	const struct intel_limit *limit;
9004 	int refclk = 96000;
9005 
9006 	memset(&crtc_state->dpll_hw_state, 0,
9007 	       sizeof(crtc_state->dpll_hw_state));
9008 
9009 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9010 		if (intel_panel_use_ssc(dev_priv)) {
9011 			refclk = dev_priv->vbt.lvds_ssc_freq;
9012 			drm_dbg_kms(&dev_priv->drm,
9013 				    "using SSC reference clock of %d kHz\n",
9014 				    refclk);
9015 		}
9016 
9017 		if (intel_is_dual_link_lvds(dev_priv))
9018 			limit = &intel_limits_g4x_dual_channel_lvds;
9019 		else
9020 			limit = &intel_limits_g4x_single_channel_lvds;
9021 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9022 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
9023 		limit = &intel_limits_g4x_hdmi;
9024 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
9025 		limit = &intel_limits_g4x_sdvo;
9026 	} else {
9027 		/* The option is for other outputs */
9028 		limit = &intel_limits_i9xx_sdvo;
9029 	}
9030 
9031 	if (!crtc_state->clock_set &&
9032 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9033 				refclk, NULL, &crtc_state->dpll)) {
9034 		drm_err(&dev_priv->drm,
9035 			"Couldn't find PLL settings for mode!\n");
9036 		return -EINVAL;
9037 	}
9038 
9039 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9040 
9041 	return 0;
9042 }
9043 
9044 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9045 				  struct intel_crtc_state *crtc_state)
9046 {
9047 	struct drm_device *dev = crtc->base.dev;
9048 	struct drm_i915_private *dev_priv = to_i915(dev);
9049 	const struct intel_limit *limit;
9050 	int refclk = 96000;
9051 
9052 	memset(&crtc_state->dpll_hw_state, 0,
9053 	       sizeof(crtc_state->dpll_hw_state));
9054 
9055 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9056 		if (intel_panel_use_ssc(dev_priv)) {
9057 			refclk = dev_priv->vbt.lvds_ssc_freq;
9058 			drm_dbg_kms(&dev_priv->drm,
9059 				    "using SSC reference clock of %d kHz\n",
9060 				    refclk);
9061 		}
9062 
9063 		limit = &pnv_limits_lvds;
9064 	} else {
9065 		limit = &pnv_limits_sdvo;
9066 	}
9067 
9068 	if (!crtc_state->clock_set &&
9069 	    !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9070 				refclk, NULL, &crtc_state->dpll)) {
9071 		drm_err(&dev_priv->drm,
9072 			"Couldn't find PLL settings for mode!\n");
9073 		return -EINVAL;
9074 	}
9075 
9076 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9077 
9078 	return 0;
9079 }
9080 
9081 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9082 				   struct intel_crtc_state *crtc_state)
9083 {
9084 	struct drm_device *dev = crtc->base.dev;
9085 	struct drm_i915_private *dev_priv = to_i915(dev);
9086 	const struct intel_limit *limit;
9087 	int refclk = 96000;
9088 
9089 	memset(&crtc_state->dpll_hw_state, 0,
9090 	       sizeof(crtc_state->dpll_hw_state));
9091 
9092 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9093 		if (intel_panel_use_ssc(dev_priv)) {
9094 			refclk = dev_priv->vbt.lvds_ssc_freq;
9095 			drm_dbg_kms(&dev_priv->drm,
9096 				    "using SSC reference clock of %d kHz\n",
9097 				    refclk);
9098 		}
9099 
9100 		limit = &intel_limits_i9xx_lvds;
9101 	} else {
9102 		limit = &intel_limits_i9xx_sdvo;
9103 	}
9104 
9105 	if (!crtc_state->clock_set &&
9106 	    !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9107 				 refclk, NULL, &crtc_state->dpll)) {
9108 		drm_err(&dev_priv->drm,
9109 			"Couldn't find PLL settings for mode!\n");
9110 		return -EINVAL;
9111 	}
9112 
9113 	i9xx_compute_dpll(crtc, crtc_state, NULL);
9114 
9115 	return 0;
9116 }
9117 
9118 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9119 				  struct intel_crtc_state *crtc_state)
9120 {
9121 	int refclk = 100000;
9122 	const struct intel_limit *limit = &intel_limits_chv;
9123 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9124 
9125 	memset(&crtc_state->dpll_hw_state, 0,
9126 	       sizeof(crtc_state->dpll_hw_state));
9127 
9128 	if (!crtc_state->clock_set &&
9129 	    !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9130 				refclk, NULL, &crtc_state->dpll)) {
9131 		drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
9132 		return -EINVAL;
9133 	}
9134 
9135 	chv_compute_dpll(crtc, crtc_state);
9136 
9137 	return 0;
9138 }
9139 
9140 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9141 				  struct intel_crtc_state *crtc_state)
9142 {
9143 	int refclk = 100000;
9144 	const struct intel_limit *limit = &intel_limits_vlv;
9145 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9146 
9147 	memset(&crtc_state->dpll_hw_state, 0,
9148 	       sizeof(crtc_state->dpll_hw_state));
9149 
9150 	if (!crtc_state->clock_set &&
9151 	    !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9152 				refclk, NULL, &crtc_state->dpll)) {
9153 		drm_err(&i915->drm,  "Couldn't find PLL settings for mode!\n");
9154 		return -EINVAL;
9155 	}
9156 
9157 	vlv_compute_dpll(crtc, crtc_state);
9158 
9159 	return 0;
9160 }
9161 
9162 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9163 {
9164 	if (IS_I830(dev_priv))
9165 		return false;
9166 
9167 	return INTEL_GEN(dev_priv) >= 4 ||
9168 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9169 }
9170 
9171 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
9172 				 struct intel_crtc_state *pipe_config)
9173 {
9174 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9175 	u32 tmp;
9176 
9177 	if (!i9xx_has_pfit(dev_priv))
9178 		return;
9179 
9180 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
9181 	if (!(tmp & PFIT_ENABLE))
9182 		return;
9183 
9184 	/* Check whether the pfit is attached to our pipe. */
9185 	if (INTEL_GEN(dev_priv) < 4) {
9186 		if (crtc->pipe != PIPE_B)
9187 			return;
9188 	} else {
9189 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9190 			return;
9191 	}
9192 
9193 	pipe_config->gmch_pfit.control = tmp;
9194 	pipe_config->gmch_pfit.pgm_ratios = intel_de_read(dev_priv,
9195 							  PFIT_PGM_RATIOS);
9196 }
9197 
9198 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
9199 			       struct intel_crtc_state *pipe_config)
9200 {
9201 	struct drm_device *dev = crtc->base.dev;
9202 	struct drm_i915_private *dev_priv = to_i915(dev);
9203 	enum pipe pipe = crtc->pipe;
9204 	struct dpll clock;
9205 	u32 mdiv;
9206 	int refclk = 100000;
9207 
9208 	/* In case of DSI, DPLL will not be used */
9209 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9210 		return;
9211 
9212 	vlv_dpio_get(dev_priv);
9213 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
9214 	vlv_dpio_put(dev_priv);
9215 
9216 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9217 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
9218 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9219 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9220 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9221 
9222 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
9223 }
9224 
9225 static void
9226 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9227 			      struct intel_initial_plane_config *plane_config)
9228 {
9229 	struct drm_device *dev = crtc->base.dev;
9230 	struct drm_i915_private *dev_priv = to_i915(dev);
9231 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9232 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9233 	enum pipe pipe;
9234 	u32 val, base, offset;
9235 	int fourcc, pixel_format;
9236 	unsigned int aligned_height;
9237 	struct drm_framebuffer *fb;
9238 	struct intel_framebuffer *intel_fb;
9239 
9240 	if (!plane->get_hw_state(plane, &pipe))
9241 		return;
9242 
9243 	drm_WARN_ON(dev, pipe != crtc->pipe);
9244 
9245 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9246 	if (!intel_fb) {
9247 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
9248 		return;
9249 	}
9250 
9251 	fb = &intel_fb->base;
9252 
9253 	fb->dev = dev;
9254 
9255 	val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9256 
9257 	if (INTEL_GEN(dev_priv) >= 4) {
9258 		if (val & DISPPLANE_TILED) {
9259 			plane_config->tiling = I915_TILING_X;
9260 			fb->modifier = I915_FORMAT_MOD_X_TILED;
9261 		}
9262 
9263 		if (val & DISPPLANE_ROTATE_180)
9264 			plane_config->rotation = DRM_MODE_ROTATE_180;
9265 	}
9266 
9267 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9268 	    val & DISPPLANE_MIRROR)
9269 		plane_config->rotation |= DRM_MODE_REFLECT_X;
9270 
9271 	pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9272 	fourcc = i9xx_format_to_fourcc(pixel_format);
9273 	fb->format = drm_format_info(fourcc);
9274 
9275 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
9276 		offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9277 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9278 	} else if (INTEL_GEN(dev_priv) >= 4) {
9279 		if (plane_config->tiling)
9280 			offset = intel_de_read(dev_priv,
9281 					       DSPTILEOFF(i9xx_plane));
9282 		else
9283 			offset = intel_de_read(dev_priv,
9284 					       DSPLINOFF(i9xx_plane));
9285 		base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9286 	} else {
9287 		base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
9288 	}
9289 	plane_config->base = base;
9290 
9291 	val = intel_de_read(dev_priv, PIPESRC(pipe));
9292 	fb->width = ((val >> 16) & 0xfff) + 1;
9293 	fb->height = ((val >> 0) & 0xfff) + 1;
9294 
9295 	val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
9296 	fb->pitches[0] = val & 0xffffffc0;
9297 
9298 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
9299 
9300 	plane_config->size = fb->pitches[0] * aligned_height;
9301 
9302 	drm_dbg_kms(&dev_priv->drm,
9303 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9304 		    crtc->base.name, plane->base.name, fb->width, fb->height,
9305 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
9306 		    plane_config->size);
9307 
9308 	plane_config->fb = intel_fb;
9309 }
9310 
9311 static void chv_crtc_clock_get(struct intel_crtc *crtc,
9312 			       struct intel_crtc_state *pipe_config)
9313 {
9314 	struct drm_device *dev = crtc->base.dev;
9315 	struct drm_i915_private *dev_priv = to_i915(dev);
9316 	enum pipe pipe = crtc->pipe;
9317 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
9318 	struct dpll clock;
9319 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
9320 	int refclk = 100000;
9321 
9322 	/* In case of DSI, DPLL will not be used */
9323 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9324 		return;
9325 
9326 	vlv_dpio_get(dev_priv);
9327 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9328 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9329 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9330 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
9331 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
9332 	vlv_dpio_put(dev_priv);
9333 
9334 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
9335 	clock.m2 = (pll_dw0 & 0xff) << 22;
9336 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9337 		clock.m2 |= pll_dw2 & 0x3fffff;
9338 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9339 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9340 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9341 
9342 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
9343 }
9344 
9345 static enum intel_output_format
9346 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
9347 {
9348 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9349 	u32 tmp;
9350 
9351 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
9352 
9353 	if (tmp & PIPEMISC_YUV420_ENABLE) {
9354 		/* We support 4:2:0 in full blend mode only */
9355 		drm_WARN_ON(&dev_priv->drm,
9356 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
9357 
9358 		return INTEL_OUTPUT_FORMAT_YCBCR420;
9359 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9360 		return INTEL_OUTPUT_FORMAT_YCBCR444;
9361 	} else {
9362 		return INTEL_OUTPUT_FORMAT_RGB;
9363 	}
9364 }
9365 
9366 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9367 {
9368 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9369 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9370 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9371 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9372 	u32 tmp;
9373 
9374 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9375 
9376 	if (tmp & DISPPLANE_GAMMA_ENABLE)
9377 		crtc_state->gamma_enable = true;
9378 
9379 	if (!HAS_GMCH(dev_priv) &&
9380 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
9381 		crtc_state->csc_enable = true;
9382 }
9383 
9384 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
9385 				 struct intel_crtc_state *pipe_config)
9386 {
9387 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9388 	enum intel_display_power_domain power_domain;
9389 	intel_wakeref_t wakeref;
9390 	u32 tmp;
9391 	bool ret;
9392 
9393 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9394 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9395 	if (!wakeref)
9396 		return false;
9397 
9398 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9399 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9400 	pipe_config->shared_dpll = NULL;
9401 	pipe_config->master_transcoder = INVALID_TRANSCODER;
9402 
9403 	ret = false;
9404 
9405 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
9406 	if (!(tmp & PIPECONF_ENABLE))
9407 		goto out;
9408 
9409 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9410 	    IS_CHERRYVIEW(dev_priv)) {
9411 		switch (tmp & PIPECONF_BPC_MASK) {
9412 		case PIPECONF_6BPC:
9413 			pipe_config->pipe_bpp = 18;
9414 			break;
9415 		case PIPECONF_8BPC:
9416 			pipe_config->pipe_bpp = 24;
9417 			break;
9418 		case PIPECONF_10BPC:
9419 			pipe_config->pipe_bpp = 30;
9420 			break;
9421 		default:
9422 			break;
9423 		}
9424 	}
9425 
9426 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9427 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
9428 		pipe_config->limited_color_range = true;
9429 
9430 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9431 		PIPECONF_GAMMA_MODE_SHIFT;
9432 
9433 	if (IS_CHERRYVIEW(dev_priv))
9434 		pipe_config->cgm_mode = intel_de_read(dev_priv,
9435 						      CGM_PIPE_MODE(crtc->pipe));
9436 
9437 	i9xx_get_pipe_color_config(pipe_config);
9438 	intel_color_get_config(pipe_config);
9439 
9440 	if (INTEL_GEN(dev_priv) < 4)
9441 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9442 
9443 	intel_get_pipe_timings(crtc, pipe_config);
9444 	intel_get_pipe_src_size(crtc, pipe_config);
9445 
9446 	i9xx_get_pfit_config(crtc, pipe_config);
9447 
9448 	if (INTEL_GEN(dev_priv) >= 4) {
9449 		/* No way to read it out on pipes B and C */
9450 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
9451 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
9452 		else
9453 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
9454 		pipe_config->pixel_multiplier =
9455 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9456 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
9457 		pipe_config->dpll_hw_state.dpll_md = tmp;
9458 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
9459 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
9460 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
9461 		pipe_config->pixel_multiplier =
9462 			((tmp & SDVO_MULTIPLIER_MASK)
9463 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9464 	} else {
9465 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
9466 		 * port and will be fixed up in the encoder->get_config
9467 		 * function. */
9468 		pipe_config->pixel_multiplier = 1;
9469 	}
9470 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9471 							DPLL(crtc->pipe));
9472 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
9473 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9474 							       FP0(crtc->pipe));
9475 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9476 							       FP1(crtc->pipe));
9477 	} else {
9478 		/* Mask out read-only status bits. */
9479 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9480 						     DPLL_PORTC_READY_MASK |
9481 						     DPLL_PORTB_READY_MASK);
9482 	}
9483 
9484 	if (IS_CHERRYVIEW(dev_priv))
9485 		chv_crtc_clock_get(crtc, pipe_config);
9486 	else if (IS_VALLEYVIEW(dev_priv))
9487 		vlv_crtc_clock_get(crtc, pipe_config);
9488 	else
9489 		i9xx_crtc_clock_get(crtc, pipe_config);
9490 
9491 	/*
9492 	 * Normally the dotclock is filled in by the encoder .get_config()
9493 	 * but in case the pipe is enabled w/o any ports we need a sane
9494 	 * default.
9495 	 */
9496 	pipe_config->hw.adjusted_mode.crtc_clock =
9497 		pipe_config->port_clock / pipe_config->pixel_multiplier;
9498 
9499 	ret = true;
9500 
9501 out:
9502 	intel_display_power_put(dev_priv, power_domain, wakeref);
9503 
9504 	return ret;
9505 }
9506 
9507 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
9508 {
9509 	struct intel_encoder *encoder;
9510 	int i;
9511 	u32 val, final;
9512 	bool has_lvds = false;
9513 	bool has_cpu_edp = false;
9514 	bool has_panel = false;
9515 	bool has_ck505 = false;
9516 	bool can_ssc = false;
9517 	bool using_ssc_source = false;
9518 
9519 	/* We need to take the global config into account */
9520 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9521 		switch (encoder->type) {
9522 		case INTEL_OUTPUT_LVDS:
9523 			has_panel = true;
9524 			has_lvds = true;
9525 			break;
9526 		case INTEL_OUTPUT_EDP:
9527 			has_panel = true;
9528 			if (encoder->port == PORT_A)
9529 				has_cpu_edp = true;
9530 			break;
9531 		default:
9532 			break;
9533 		}
9534 	}
9535 
9536 	if (HAS_PCH_IBX(dev_priv)) {
9537 		has_ck505 = dev_priv->vbt.display_clock_mode;
9538 		can_ssc = has_ck505;
9539 	} else {
9540 		has_ck505 = false;
9541 		can_ssc = true;
9542 	}
9543 
9544 	/* Check if any DPLLs are using the SSC source */
9545 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9546 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
9547 
9548 		if (!(temp & DPLL_VCO_ENABLE))
9549 			continue;
9550 
9551 		if ((temp & PLL_REF_INPUT_MASK) ==
9552 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9553 			using_ssc_source = true;
9554 			break;
9555 		}
9556 	}
9557 
9558 	drm_dbg_kms(&dev_priv->drm,
9559 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9560 		    has_panel, has_lvds, has_ck505, using_ssc_source);
9561 
9562 	/* Ironlake: try to setup display ref clock before DPLL
9563 	 * enabling. This is only under driver's control after
9564 	 * PCH B stepping, previous chipset stepping should be
9565 	 * ignoring this setting.
9566 	 */
9567 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
9568 
9569 	/* As we must carefully and slowly disable/enable each source in turn,
9570 	 * compute the final state we want first and check if we need to
9571 	 * make any changes at all.
9572 	 */
9573 	final = val;
9574 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
9575 	if (has_ck505)
9576 		final |= DREF_NONSPREAD_CK505_ENABLE;
9577 	else
9578 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
9579 
9580 	final &= ~DREF_SSC_SOURCE_MASK;
9581 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9582 	final &= ~DREF_SSC1_ENABLE;
9583 
9584 	if (has_panel) {
9585 		final |= DREF_SSC_SOURCE_ENABLE;
9586 
9587 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
9588 			final |= DREF_SSC1_ENABLE;
9589 
9590 		if (has_cpu_edp) {
9591 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
9592 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9593 			else
9594 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9595 		} else
9596 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9597 	} else if (using_ssc_source) {
9598 		final |= DREF_SSC_SOURCE_ENABLE;
9599 		final |= DREF_SSC1_ENABLE;
9600 	}
9601 
9602 	if (final == val)
9603 		return;
9604 
9605 	/* Always enable nonspread source */
9606 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
9607 
9608 	if (has_ck505)
9609 		val |= DREF_NONSPREAD_CK505_ENABLE;
9610 	else
9611 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
9612 
9613 	if (has_panel) {
9614 		val &= ~DREF_SSC_SOURCE_MASK;
9615 		val |= DREF_SSC_SOURCE_ENABLE;
9616 
9617 		/* SSC must be turned on before enabling the CPU output  */
9618 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9619 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
9620 			val |= DREF_SSC1_ENABLE;
9621 		} else
9622 			val &= ~DREF_SSC1_ENABLE;
9623 
9624 		/* Get SSC going before enabling the outputs */
9625 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9626 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9627 		udelay(200);
9628 
9629 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9630 
9631 		/* Enable CPU source on CPU attached eDP */
9632 		if (has_cpu_edp) {
9633 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9634 				drm_dbg_kms(&dev_priv->drm,
9635 					    "Using SSC on eDP\n");
9636 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9637 			} else
9638 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9639 		} else
9640 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9641 
9642 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9643 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9644 		udelay(200);
9645 	} else {
9646 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
9647 
9648 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9649 
9650 		/* Turn off CPU output */
9651 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9652 
9653 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9654 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9655 		udelay(200);
9656 
9657 		if (!using_ssc_source) {
9658 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
9659 
9660 			/* Turn off the SSC source */
9661 			val &= ~DREF_SSC_SOURCE_MASK;
9662 			val |= DREF_SSC_SOURCE_DISABLE;
9663 
9664 			/* Turn off SSC1 */
9665 			val &= ~DREF_SSC1_ENABLE;
9666 
9667 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9668 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9669 			udelay(200);
9670 		}
9671 	}
9672 
9673 	BUG_ON(val != final);
9674 }
9675 
9676 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9677 {
9678 	u32 tmp;
9679 
9680 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9681 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9682 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9683 
9684 	if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9685 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9686 		drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
9687 
9688 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9689 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9690 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9691 
9692 	if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9693 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9694 		drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
9695 }
9696 
9697 /* WaMPhyProgramming:hsw */
9698 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9699 {
9700 	u32 tmp;
9701 
9702 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9703 	tmp &= ~(0xFF << 24);
9704 	tmp |= (0x12 << 24);
9705 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9706 
9707 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9708 	tmp |= (1 << 11);
9709 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9710 
9711 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9712 	tmp |= (1 << 11);
9713 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9714 
9715 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9716 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9717 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9718 
9719 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9720 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9721 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9722 
9723 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9724 	tmp &= ~(7 << 13);
9725 	tmp |= (5 << 13);
9726 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9727 
9728 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9729 	tmp &= ~(7 << 13);
9730 	tmp |= (5 << 13);
9731 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9732 
9733 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9734 	tmp &= ~0xFF;
9735 	tmp |= 0x1C;
9736 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9737 
9738 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9739 	tmp &= ~0xFF;
9740 	tmp |= 0x1C;
9741 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9742 
9743 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9744 	tmp &= ~(0xFF << 16);
9745 	tmp |= (0x1C << 16);
9746 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9747 
9748 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9749 	tmp &= ~(0xFF << 16);
9750 	tmp |= (0x1C << 16);
9751 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9752 
9753 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9754 	tmp |= (1 << 27);
9755 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9756 
9757 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9758 	tmp |= (1 << 27);
9759 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
9760 
9761 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9762 	tmp &= ~(0xF << 28);
9763 	tmp |= (4 << 28);
9764 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
9765 
9766 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9767 	tmp &= ~(0xF << 28);
9768 	tmp |= (4 << 28);
9769 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
9770 }
9771 
9772 /* Implements 3 different sequences from BSpec chapter "Display iCLK
9773  * Programming" based on the parameters passed:
9774  * - Sequence to enable CLKOUT_DP
9775  * - Sequence to enable CLKOUT_DP without spread
9776  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9777  */
9778 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9779 				 bool with_spread, bool with_fdi)
9780 {
9781 	u32 reg, tmp;
9782 
9783 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
9784 		     "FDI requires downspread\n"))
9785 		with_spread = true;
9786 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
9787 		     with_fdi, "LP PCH doesn't have FDI\n"))
9788 		with_fdi = false;
9789 
9790 	mutex_lock(&dev_priv->sb_lock);
9791 
9792 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9793 	tmp &= ~SBI_SSCCTL_DISABLE;
9794 	tmp |= SBI_SSCCTL_PATHALT;
9795 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9796 
9797 	udelay(24);
9798 
9799 	if (with_spread) {
9800 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9801 		tmp &= ~SBI_SSCCTL_PATHALT;
9802 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9803 
9804 		if (with_fdi) {
9805 			lpt_reset_fdi_mphy(dev_priv);
9806 			lpt_program_fdi_mphy(dev_priv);
9807 		}
9808 	}
9809 
9810 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9811 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9812 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9813 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9814 
9815 	mutex_unlock(&dev_priv->sb_lock);
9816 }
9817 
9818 /* Sequence to disable CLKOUT_DP */
9819 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
9820 {
9821 	u32 reg, tmp;
9822 
9823 	mutex_lock(&dev_priv->sb_lock);
9824 
9825 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9826 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9827 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9828 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9829 
9830 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9831 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
9832 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
9833 			tmp |= SBI_SSCCTL_PATHALT;
9834 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9835 			udelay(32);
9836 		}
9837 		tmp |= SBI_SSCCTL_DISABLE;
9838 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9839 	}
9840 
9841 	mutex_unlock(&dev_priv->sb_lock);
9842 }
9843 
9844 #define BEND_IDX(steps) ((50 + (steps)) / 5)
9845 
9846 static const u16 sscdivintphase[] = {
9847 	[BEND_IDX( 50)] = 0x3B23,
9848 	[BEND_IDX( 45)] = 0x3B23,
9849 	[BEND_IDX( 40)] = 0x3C23,
9850 	[BEND_IDX( 35)] = 0x3C23,
9851 	[BEND_IDX( 30)] = 0x3D23,
9852 	[BEND_IDX( 25)] = 0x3D23,
9853 	[BEND_IDX( 20)] = 0x3E23,
9854 	[BEND_IDX( 15)] = 0x3E23,
9855 	[BEND_IDX( 10)] = 0x3F23,
9856 	[BEND_IDX(  5)] = 0x3F23,
9857 	[BEND_IDX(  0)] = 0x0025,
9858 	[BEND_IDX( -5)] = 0x0025,
9859 	[BEND_IDX(-10)] = 0x0125,
9860 	[BEND_IDX(-15)] = 0x0125,
9861 	[BEND_IDX(-20)] = 0x0225,
9862 	[BEND_IDX(-25)] = 0x0225,
9863 	[BEND_IDX(-30)] = 0x0325,
9864 	[BEND_IDX(-35)] = 0x0325,
9865 	[BEND_IDX(-40)] = 0x0425,
9866 	[BEND_IDX(-45)] = 0x0425,
9867 	[BEND_IDX(-50)] = 0x0525,
9868 };
9869 
9870 /*
9871  * Bend CLKOUT_DP
9872  * steps -50 to 50 inclusive, in steps of 5
9873  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9874  * change in clock period = -(steps / 10) * 5.787 ps
9875  */
9876 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9877 {
9878 	u32 tmp;
9879 	int idx = BEND_IDX(steps);
9880 
9881 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
9882 		return;
9883 
9884 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
9885 		return;
9886 
9887 	mutex_lock(&dev_priv->sb_lock);
9888 
9889 	if (steps % 10 != 0)
9890 		tmp = 0xAAAAAAAB;
9891 	else
9892 		tmp = 0x00000000;
9893 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9894 
9895 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9896 	tmp &= 0xffff0000;
9897 	tmp |= sscdivintphase[idx];
9898 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9899 
9900 	mutex_unlock(&dev_priv->sb_lock);
9901 }
9902 
9903 #undef BEND_IDX
9904 
9905 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
9906 {
9907 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9908 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
9909 
9910 	if ((ctl & SPLL_PLL_ENABLE) == 0)
9911 		return false;
9912 
9913 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
9914 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9915 		return true;
9916 
9917 	if (IS_BROADWELL(dev_priv) &&
9918 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
9919 		return true;
9920 
9921 	return false;
9922 }
9923 
9924 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
9925 			       enum intel_dpll_id id)
9926 {
9927 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
9928 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
9929 
9930 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
9931 		return false;
9932 
9933 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
9934 		return true;
9935 
9936 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
9937 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
9938 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
9939 		return true;
9940 
9941 	return false;
9942 }
9943 
9944 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
9945 {
9946 	struct intel_encoder *encoder;
9947 	bool has_fdi = false;
9948 
9949 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9950 		switch (encoder->type) {
9951 		case INTEL_OUTPUT_ANALOG:
9952 			has_fdi = true;
9953 			break;
9954 		default:
9955 			break;
9956 		}
9957 	}
9958 
9959 	/*
9960 	 * The BIOS may have decided to use the PCH SSC
9961 	 * reference so we must not disable it until the
9962 	 * relevant PLLs have stopped relying on it. We'll
9963 	 * just leave the PCH SSC reference enabled in case
9964 	 * any active PLL is using it. It will get disabled
9965 	 * after runtime suspend if we don't have FDI.
9966 	 *
9967 	 * TODO: Move the whole reference clock handling
9968 	 * to the modeset sequence proper so that we can
9969 	 * actually enable/disable/reconfigure these things
9970 	 * safely. To do that we need to introduce a real
9971 	 * clock hierarchy. That would also allow us to do
9972 	 * clock bending finally.
9973 	 */
9974 	dev_priv->pch_ssc_use = 0;
9975 
9976 	if (spll_uses_pch_ssc(dev_priv)) {
9977 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
9978 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
9979 	}
9980 
9981 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
9982 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
9983 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
9984 	}
9985 
9986 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
9987 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
9988 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
9989 	}
9990 
9991 	if (dev_priv->pch_ssc_use)
9992 		return;
9993 
9994 	if (has_fdi) {
9995 		lpt_bend_clkout_dp(dev_priv, 0);
9996 		lpt_enable_clkout_dp(dev_priv, true, true);
9997 	} else {
9998 		lpt_disable_clkout_dp(dev_priv);
9999 	}
10000 }
10001 
10002 /*
10003  * Initialize reference clocks when the driver loads
10004  */
10005 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
10006 {
10007 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
10008 		ilk_init_pch_refclk(dev_priv);
10009 	else if (HAS_PCH_LPT(dev_priv))
10010 		lpt_init_pch_refclk(dev_priv);
10011 }
10012 
10013 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
10014 {
10015 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10016 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10017 	enum pipe pipe = crtc->pipe;
10018 	u32 val;
10019 
10020 	val = 0;
10021 
10022 	switch (crtc_state->pipe_bpp) {
10023 	case 18:
10024 		val |= PIPECONF_6BPC;
10025 		break;
10026 	case 24:
10027 		val |= PIPECONF_8BPC;
10028 		break;
10029 	case 30:
10030 		val |= PIPECONF_10BPC;
10031 		break;
10032 	case 36:
10033 		val |= PIPECONF_12BPC;
10034 		break;
10035 	default:
10036 		/* Case prevented by intel_choose_pipe_bpp_dither. */
10037 		BUG();
10038 	}
10039 
10040 	if (crtc_state->dither)
10041 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10042 
10043 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10044 		val |= PIPECONF_INTERLACED_ILK;
10045 	else
10046 		val |= PIPECONF_PROGRESSIVE;
10047 
10048 	/*
10049 	 * This would end up with an odd purple hue over
10050 	 * the entire display. Make sure we don't do it.
10051 	 */
10052 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10053 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
10054 
10055 	if (crtc_state->limited_color_range)
10056 		val |= PIPECONF_COLOR_RANGE_SELECT;
10057 
10058 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10059 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10060 
10061 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10062 
10063 	val |= PIPECONF_FRAME_START_DELAY(0);
10064 
10065 	intel_de_write(dev_priv, PIPECONF(pipe), val);
10066 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
10067 }
10068 
10069 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
10070 {
10071 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10072 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10073 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
10074 	u32 val = 0;
10075 
10076 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
10077 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10078 
10079 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10080 		val |= PIPECONF_INTERLACED_ILK;
10081 	else
10082 		val |= PIPECONF_PROGRESSIVE;
10083 
10084 	if (IS_HASWELL(dev_priv) &&
10085 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10086 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10087 
10088 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10089 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
10090 }
10091 
10092 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
10093 {
10094 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10095 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10096 	u32 val = 0;
10097 
10098 	switch (crtc_state->pipe_bpp) {
10099 	case 18:
10100 		val |= PIPEMISC_DITHER_6_BPC;
10101 		break;
10102 	case 24:
10103 		val |= PIPEMISC_DITHER_8_BPC;
10104 		break;
10105 	case 30:
10106 		val |= PIPEMISC_DITHER_10_BPC;
10107 		break;
10108 	case 36:
10109 		val |= PIPEMISC_DITHER_12_BPC;
10110 		break;
10111 	default:
10112 		MISSING_CASE(crtc_state->pipe_bpp);
10113 		break;
10114 	}
10115 
10116 	if (crtc_state->dither)
10117 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
10118 
10119 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10120 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10121 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
10122 
10123 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10124 		val |= PIPEMISC_YUV420_ENABLE |
10125 			PIPEMISC_YUV420_MODE_FULL_BLEND;
10126 
10127 	if (INTEL_GEN(dev_priv) >= 11 &&
10128 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10129 					   BIT(PLANE_CURSOR))) == 0)
10130 		val |= PIPEMISC_HDR_MODE_PRECISION;
10131 
10132 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
10133 }
10134 
10135 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10136 {
10137 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10138 	u32 tmp;
10139 
10140 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
10141 
10142 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10143 	case PIPEMISC_DITHER_6_BPC:
10144 		return 18;
10145 	case PIPEMISC_DITHER_8_BPC:
10146 		return 24;
10147 	case PIPEMISC_DITHER_10_BPC:
10148 		return 30;
10149 	case PIPEMISC_DITHER_12_BPC:
10150 		return 36;
10151 	default:
10152 		MISSING_CASE(tmp);
10153 		return 0;
10154 	}
10155 }
10156 
10157 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
10158 {
10159 	/*
10160 	 * Account for spread spectrum to avoid
10161 	 * oversubscribing the link. Max center spread
10162 	 * is 2.5%; use 5% for safety's sake.
10163 	 */
10164 	u32 bps = target_clock * bpp * 21 / 20;
10165 	return DIV_ROUND_UP(bps, link_bw * 8);
10166 }
10167 
10168 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
10169 {
10170 	return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
10171 }
10172 
10173 static void ilk_compute_dpll(struct intel_crtc *crtc,
10174 			     struct intel_crtc_state *crtc_state,
10175 			     struct dpll *reduced_clock)
10176 {
10177 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10178 	u32 dpll, fp, fp2;
10179 	int factor;
10180 
10181 	/* Enable autotuning of the PLL clock (if permissible) */
10182 	factor = 21;
10183 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10184 		if ((intel_panel_use_ssc(dev_priv) &&
10185 		     dev_priv->vbt.lvds_ssc_freq == 100000) ||
10186 		    (HAS_PCH_IBX(dev_priv) &&
10187 		     intel_is_dual_link_lvds(dev_priv)))
10188 			factor = 25;
10189 	} else if (crtc_state->sdvo_tv_clock) {
10190 		factor = 20;
10191 	}
10192 
10193 	fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10194 
10195 	if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
10196 		fp |= FP_CB_TUNE;
10197 
10198 	if (reduced_clock) {
10199 		fp2 = i9xx_dpll_compute_fp(reduced_clock);
10200 
10201 		if (reduced_clock->m < factor * reduced_clock->n)
10202 			fp2 |= FP_CB_TUNE;
10203 	} else {
10204 		fp2 = fp;
10205 	}
10206 
10207 	dpll = 0;
10208 
10209 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
10210 		dpll |= DPLLB_MODE_LVDS;
10211 	else
10212 		dpll |= DPLLB_MODE_DAC_SERIAL;
10213 
10214 	dpll |= (crtc_state->pixel_multiplier - 1)
10215 		<< PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
10216 
10217 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10218 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
10219 		dpll |= DPLL_SDVO_HIGH_SPEED;
10220 
10221 	if (intel_crtc_has_dp_encoder(crtc_state))
10222 		dpll |= DPLL_SDVO_HIGH_SPEED;
10223 
10224 	/*
10225 	 * The high speed IO clock is only really required for
10226 	 * SDVO/HDMI/DP, but we also enable it for CRT to make it
10227 	 * possible to share the DPLL between CRT and HDMI. Enabling
10228 	 * the clock needlessly does no real harm, except use up a
10229 	 * bit of power potentially.
10230 	 *
10231 	 * We'll limit this to IVB with 3 pipes, since it has only two
10232 	 * DPLLs and so DPLL sharing is the only way to get three pipes
10233 	 * driving PCH ports at the same time. On SNB we could do this,
10234 	 * and potentially avoid enabling the second DPLL, but it's not
10235 	 * clear if it''s a win or loss power wise. No point in doing
10236 	 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10237 	 */
10238 	if (INTEL_NUM_PIPES(dev_priv) == 3 &&
10239 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10240 		dpll |= DPLL_SDVO_HIGH_SPEED;
10241 
10242 	/* compute bitmask from p1 value */
10243 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
10244 	/* also FPA1 */
10245 	dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
10246 
10247 	switch (crtc_state->dpll.p2) {
10248 	case 5:
10249 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10250 		break;
10251 	case 7:
10252 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10253 		break;
10254 	case 10:
10255 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10256 		break;
10257 	case 14:
10258 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10259 		break;
10260 	}
10261 
10262 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10263 	    intel_panel_use_ssc(dev_priv))
10264 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
10265 	else
10266 		dpll |= PLL_REF_INPUT_DREFCLK;
10267 
10268 	dpll |= DPLL_VCO_ENABLE;
10269 
10270 	crtc_state->dpll_hw_state.dpll = dpll;
10271 	crtc_state->dpll_hw_state.fp0 = fp;
10272 	crtc_state->dpll_hw_state.fp1 = fp2;
10273 }
10274 
10275 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10276 				  struct intel_crtc_state *crtc_state)
10277 {
10278 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10279 	struct intel_atomic_state *state =
10280 		to_intel_atomic_state(crtc_state->uapi.state);
10281 	const struct intel_limit *limit;
10282 	int refclk = 120000;
10283 
10284 	memset(&crtc_state->dpll_hw_state, 0,
10285 	       sizeof(crtc_state->dpll_hw_state));
10286 
10287 	/* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10288 	if (!crtc_state->has_pch_encoder)
10289 		return 0;
10290 
10291 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10292 		if (intel_panel_use_ssc(dev_priv)) {
10293 			drm_dbg_kms(&dev_priv->drm,
10294 				    "using SSC reference clock of %d kHz\n",
10295 				    dev_priv->vbt.lvds_ssc_freq);
10296 			refclk = dev_priv->vbt.lvds_ssc_freq;
10297 		}
10298 
10299 		if (intel_is_dual_link_lvds(dev_priv)) {
10300 			if (refclk == 100000)
10301 				limit = &ilk_limits_dual_lvds_100m;
10302 			else
10303 				limit = &ilk_limits_dual_lvds;
10304 		} else {
10305 			if (refclk == 100000)
10306 				limit = &ilk_limits_single_lvds_100m;
10307 			else
10308 				limit = &ilk_limits_single_lvds;
10309 		}
10310 	} else {
10311 		limit = &ilk_limits_dac;
10312 	}
10313 
10314 	if (!crtc_state->clock_set &&
10315 	    !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10316 				refclk, NULL, &crtc_state->dpll)) {
10317 		drm_err(&dev_priv->drm,
10318 			"Couldn't find PLL settings for mode!\n");
10319 		return -EINVAL;
10320 	}
10321 
10322 	ilk_compute_dpll(crtc, crtc_state, NULL);
10323 
10324 	if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
10325 		drm_dbg_kms(&dev_priv->drm,
10326 			    "failed to find PLL for pipe %c\n",
10327 			    pipe_name(crtc->pipe));
10328 		return -EINVAL;
10329 	}
10330 
10331 	return 0;
10332 }
10333 
10334 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10335 					 struct intel_link_m_n *m_n)
10336 {
10337 	struct drm_device *dev = crtc->base.dev;
10338 	struct drm_i915_private *dev_priv = to_i915(dev);
10339 	enum pipe pipe = crtc->pipe;
10340 
10341 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10342 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10343 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10344 		& ~TU_SIZE_MASK;
10345 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10346 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10347 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10348 }
10349 
10350 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10351 					 enum transcoder transcoder,
10352 					 struct intel_link_m_n *m_n,
10353 					 struct intel_link_m_n *m2_n2)
10354 {
10355 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10356 	enum pipe pipe = crtc->pipe;
10357 
10358 	if (INTEL_GEN(dev_priv) >= 5) {
10359 		m_n->link_m = intel_de_read(dev_priv,
10360 					    PIPE_LINK_M1(transcoder));
10361 		m_n->link_n = intel_de_read(dev_priv,
10362 					    PIPE_LINK_N1(transcoder));
10363 		m_n->gmch_m = intel_de_read(dev_priv,
10364 					    PIPE_DATA_M1(transcoder))
10365 			& ~TU_SIZE_MASK;
10366 		m_n->gmch_n = intel_de_read(dev_priv,
10367 					    PIPE_DATA_N1(transcoder));
10368 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
10369 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10370 
10371 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
10372 			m2_n2->link_m = intel_de_read(dev_priv,
10373 						      PIPE_LINK_M2(transcoder));
10374 			m2_n2->link_n =	intel_de_read(dev_priv,
10375 							     PIPE_LINK_N2(transcoder));
10376 			m2_n2->gmch_m =	intel_de_read(dev_priv,
10377 							     PIPE_DATA_M2(transcoder))
10378 					& ~TU_SIZE_MASK;
10379 			m2_n2->gmch_n =	intel_de_read(dev_priv,
10380 							     PIPE_DATA_N2(transcoder));
10381 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
10382 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10383 		}
10384 	} else {
10385 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10386 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10387 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10388 			& ~TU_SIZE_MASK;
10389 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10390 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10391 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10392 	}
10393 }
10394 
10395 void intel_dp_get_m_n(struct intel_crtc *crtc,
10396 		      struct intel_crtc_state *pipe_config)
10397 {
10398 	if (pipe_config->has_pch_encoder)
10399 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10400 	else
10401 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10402 					     &pipe_config->dp_m_n,
10403 					     &pipe_config->dp_m2_n2);
10404 }
10405 
10406 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10407 				   struct intel_crtc_state *pipe_config)
10408 {
10409 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10410 				     &pipe_config->fdi_m_n, NULL);
10411 }
10412 
10413 static void skl_get_pfit_config(struct intel_crtc *crtc,
10414 				struct intel_crtc_state *pipe_config)
10415 {
10416 	struct drm_device *dev = crtc->base.dev;
10417 	struct drm_i915_private *dev_priv = to_i915(dev);
10418 	struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
10419 	u32 ps_ctrl = 0;
10420 	int id = -1;
10421 	int i;
10422 
10423 	/* find scaler attached to this pipe */
10424 	for (i = 0; i < crtc->num_scalers; i++) {
10425 		ps_ctrl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10426 		if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
10427 			id = i;
10428 			pipe_config->pch_pfit.enabled = true;
10429 			pipe_config->pch_pfit.pos = intel_de_read(dev_priv,
10430 								  SKL_PS_WIN_POS(crtc->pipe, i));
10431 			pipe_config->pch_pfit.size = intel_de_read(dev_priv,
10432 								   SKL_PS_WIN_SZ(crtc->pipe, i));
10433 			scaler_state->scalers[i].in_use = true;
10434 			break;
10435 		}
10436 	}
10437 
10438 	scaler_state->scaler_id = id;
10439 	if (id >= 0) {
10440 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
10441 	} else {
10442 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
10443 	}
10444 }
10445 
10446 static void
10447 skl_get_initial_plane_config(struct intel_crtc *crtc,
10448 			     struct intel_initial_plane_config *plane_config)
10449 {
10450 	struct drm_device *dev = crtc->base.dev;
10451 	struct drm_i915_private *dev_priv = to_i915(dev);
10452 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10453 	enum plane_id plane_id = plane->id;
10454 	enum pipe pipe;
10455 	u32 val, base, offset, stride_mult, tiling, alpha;
10456 	int fourcc, pixel_format;
10457 	unsigned int aligned_height;
10458 	struct drm_framebuffer *fb;
10459 	struct intel_framebuffer *intel_fb;
10460 
10461 	if (!plane->get_hw_state(plane, &pipe))
10462 		return;
10463 
10464 	drm_WARN_ON(dev, pipe != crtc->pipe);
10465 
10466 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10467 	if (!intel_fb) {
10468 		drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
10469 		return;
10470 	}
10471 
10472 	fb = &intel_fb->base;
10473 
10474 	fb->dev = dev;
10475 
10476 	val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
10477 
10478 	if (INTEL_GEN(dev_priv) >= 11)
10479 		pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10480 	else
10481 		pixel_format = val & PLANE_CTL_FORMAT_MASK;
10482 
10483 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
10484 		alpha = intel_de_read(dev_priv,
10485 				      PLANE_COLOR_CTL(pipe, plane_id));
10486 		alpha &= PLANE_COLOR_ALPHA_MASK;
10487 	} else {
10488 		alpha = val & PLANE_CTL_ALPHA_MASK;
10489 	}
10490 
10491 	fourcc = skl_format_to_fourcc(pixel_format,
10492 				      val & PLANE_CTL_ORDER_RGBX, alpha);
10493 	fb->format = drm_format_info(fourcc);
10494 
10495 	tiling = val & PLANE_CTL_TILED_MASK;
10496 	switch (tiling) {
10497 	case PLANE_CTL_TILED_LINEAR:
10498 		fb->modifier = DRM_FORMAT_MOD_LINEAR;
10499 		break;
10500 	case PLANE_CTL_TILED_X:
10501 		plane_config->tiling = I915_TILING_X;
10502 		fb->modifier = I915_FORMAT_MOD_X_TILED;
10503 		break;
10504 	case PLANE_CTL_TILED_Y:
10505 		plane_config->tiling = I915_TILING_Y;
10506 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10507 			fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10508 				I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10509 				I915_FORMAT_MOD_Y_TILED_CCS;
10510 		else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10511 			fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
10512 		else
10513 			fb->modifier = I915_FORMAT_MOD_Y_TILED;
10514 		break;
10515 	case PLANE_CTL_TILED_YF:
10516 		if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10517 			fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10518 		else
10519 			fb->modifier = I915_FORMAT_MOD_Yf_TILED;
10520 		break;
10521 	default:
10522 		MISSING_CASE(tiling);
10523 		goto error;
10524 	}
10525 
10526 	/*
10527 	 * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10528 	 * while i915 HW rotation is clockwise, thats why this swapping.
10529 	 */
10530 	switch (val & PLANE_CTL_ROTATE_MASK) {
10531 	case PLANE_CTL_ROTATE_0:
10532 		plane_config->rotation = DRM_MODE_ROTATE_0;
10533 		break;
10534 	case PLANE_CTL_ROTATE_90:
10535 		plane_config->rotation = DRM_MODE_ROTATE_270;
10536 		break;
10537 	case PLANE_CTL_ROTATE_180:
10538 		plane_config->rotation = DRM_MODE_ROTATE_180;
10539 		break;
10540 	case PLANE_CTL_ROTATE_270:
10541 		plane_config->rotation = DRM_MODE_ROTATE_90;
10542 		break;
10543 	}
10544 
10545 	if (INTEL_GEN(dev_priv) >= 10 &&
10546 	    val & PLANE_CTL_FLIP_HORIZONTAL)
10547 		plane_config->rotation |= DRM_MODE_REFLECT_X;
10548 
10549 	base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
10550 	plane_config->base = base;
10551 
10552 	offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
10553 
10554 	val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
10555 	fb->height = ((val >> 16) & 0xffff) + 1;
10556 	fb->width = ((val >> 0) & 0xffff) + 1;
10557 
10558 	val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
10559 	stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
10560 	fb->pitches[0] = (val & 0x3ff) * stride_mult;
10561 
10562 	aligned_height = intel_fb_align_height(fb, 0, fb->height);
10563 
10564 	plane_config->size = fb->pitches[0] * aligned_height;
10565 
10566 	drm_dbg_kms(&dev_priv->drm,
10567 		    "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10568 		    crtc->base.name, plane->base.name, fb->width, fb->height,
10569 		    fb->format->cpp[0] * 8, base, fb->pitches[0],
10570 		    plane_config->size);
10571 
10572 	plane_config->fb = intel_fb;
10573 	return;
10574 
10575 error:
10576 	kfree(intel_fb);
10577 }
10578 
10579 static void ilk_get_pfit_config(struct intel_crtc *crtc,
10580 				struct intel_crtc_state *pipe_config)
10581 {
10582 	struct drm_device *dev = crtc->base.dev;
10583 	struct drm_i915_private *dev_priv = to_i915(dev);
10584 	u32 tmp;
10585 
10586 	tmp = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10587 
10588 	if (tmp & PF_ENABLE) {
10589 		pipe_config->pch_pfit.enabled = true;
10590 		pipe_config->pch_pfit.pos = intel_de_read(dev_priv,
10591 							  PF_WIN_POS(crtc->pipe));
10592 		pipe_config->pch_pfit.size = intel_de_read(dev_priv,
10593 							   PF_WIN_SZ(crtc->pipe));
10594 
10595 		/* We currently do not free assignements of panel fitters on
10596 		 * ivb/hsw (since we don't use the higher upscaling modes which
10597 		 * differentiates them) so just WARN about this case for now. */
10598 		if (IS_GEN(dev_priv, 7)) {
10599 			drm_WARN_ON(dev, (tmp & PF_PIPE_SEL_MASK_IVB) !=
10600 				    PF_PIPE_SEL_IVB(crtc->pipe));
10601 		}
10602 	}
10603 }
10604 
10605 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10606 				struct intel_crtc_state *pipe_config)
10607 {
10608 	struct drm_device *dev = crtc->base.dev;
10609 	struct drm_i915_private *dev_priv = to_i915(dev);
10610 	enum intel_display_power_domain power_domain;
10611 	intel_wakeref_t wakeref;
10612 	u32 tmp;
10613 	bool ret;
10614 
10615 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10616 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10617 	if (!wakeref)
10618 		return false;
10619 
10620 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10621 	pipe_config->shared_dpll = NULL;
10622 	pipe_config->master_transcoder = INVALID_TRANSCODER;
10623 
10624 	ret = false;
10625 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
10626 	if (!(tmp & PIPECONF_ENABLE))
10627 		goto out;
10628 
10629 	switch (tmp & PIPECONF_BPC_MASK) {
10630 	case PIPECONF_6BPC:
10631 		pipe_config->pipe_bpp = 18;
10632 		break;
10633 	case PIPECONF_8BPC:
10634 		pipe_config->pipe_bpp = 24;
10635 		break;
10636 	case PIPECONF_10BPC:
10637 		pipe_config->pipe_bpp = 30;
10638 		break;
10639 	case PIPECONF_12BPC:
10640 		pipe_config->pipe_bpp = 36;
10641 		break;
10642 	default:
10643 		break;
10644 	}
10645 
10646 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10647 		pipe_config->limited_color_range = true;
10648 
10649 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10650 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10651 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10652 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10653 		break;
10654 	default:
10655 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10656 		break;
10657 	}
10658 
10659 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10660 		PIPECONF_GAMMA_MODE_SHIFT;
10661 
10662 	pipe_config->csc_mode = intel_de_read(dev_priv,
10663 					      PIPE_CSC_MODE(crtc->pipe));
10664 
10665 	i9xx_get_pipe_color_config(pipe_config);
10666 	intel_color_get_config(pipe_config);
10667 
10668 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
10669 		struct intel_shared_dpll *pll;
10670 		enum intel_dpll_id pll_id;
10671 
10672 		pipe_config->has_pch_encoder = true;
10673 
10674 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
10675 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10676 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
10677 
10678 		ilk_get_fdi_m_n_config(crtc, pipe_config);
10679 
10680 		if (HAS_PCH_IBX(dev_priv)) {
10681 			/*
10682 			 * The pipe->pch transcoder and pch transcoder->pll
10683 			 * mapping is fixed.
10684 			 */
10685 			pll_id = (enum intel_dpll_id) crtc->pipe;
10686 		} else {
10687 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
10688 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
10689 				pll_id = DPLL_ID_PCH_PLL_B;
10690 			else
10691 				pll_id= DPLL_ID_PCH_PLL_A;
10692 		}
10693 
10694 		pipe_config->shared_dpll =
10695 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
10696 		pll = pipe_config->shared_dpll;
10697 
10698 		drm_WARN_ON(dev, !pll->info->funcs->get_hw_state(dev_priv, pll,
10699 						 &pipe_config->dpll_hw_state));
10700 
10701 		tmp = pipe_config->dpll_hw_state.dpll;
10702 		pipe_config->pixel_multiplier =
10703 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10704 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10705 
10706 		ilk_pch_clock_get(crtc, pipe_config);
10707 	} else {
10708 		pipe_config->pixel_multiplier = 1;
10709 	}
10710 
10711 	intel_get_pipe_timings(crtc, pipe_config);
10712 	intel_get_pipe_src_size(crtc, pipe_config);
10713 
10714 	ilk_get_pfit_config(crtc, pipe_config);
10715 
10716 	ret = true;
10717 
10718 out:
10719 	intel_display_power_put(dev_priv, power_domain, wakeref);
10720 
10721 	return ret;
10722 }
10723 
10724 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
10725 				  struct intel_crtc_state *crtc_state)
10726 {
10727 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10728 	struct intel_atomic_state *state =
10729 		to_intel_atomic_state(crtc_state->uapi.state);
10730 
10731 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10732 	    INTEL_GEN(dev_priv) >= 11) {
10733 		struct intel_encoder *encoder =
10734 			intel_get_crtc_new_encoder(state, crtc_state);
10735 
10736 		if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10737 			drm_dbg_kms(&dev_priv->drm,
10738 				    "failed to find PLL for pipe %c\n",
10739 				    pipe_name(crtc->pipe));
10740 			return -EINVAL;
10741 		}
10742 	}
10743 
10744 	return 0;
10745 }
10746 
10747 static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10748 			    struct intel_crtc_state *pipe_config)
10749 {
10750 	enum intel_dpll_id id;
10751 	u32 temp;
10752 
10753 	temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
10754 	id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
10755 
10756 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
10757 		return;
10758 
10759 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10760 }
10761 
10762 static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10763 			    struct intel_crtc_state *pipe_config)
10764 {
10765 	enum phy phy = intel_port_to_phy(dev_priv, port);
10766 	enum icl_port_dpll_id port_dpll_id;
10767 	enum intel_dpll_id id;
10768 	u32 temp;
10769 
10770 	if (intel_phy_is_combo(dev_priv, phy)) {
10771 		temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) &
10772 			ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10773 		id = temp >> ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10774 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10775 	} else if (intel_phy_is_tc(dev_priv, phy)) {
10776 		u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
10777 
10778 		if (clk_sel == DDI_CLK_SEL_MG) {
10779 			id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
10780 								    port));
10781 			port_dpll_id = ICL_PORT_DPLL_MG_PHY;
10782 		} else {
10783 			drm_WARN_ON(&dev_priv->drm,
10784 				    clk_sel < DDI_CLK_SEL_TBT_162);
10785 			id = DPLL_ID_ICL_TBTPLL;
10786 			port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10787 		}
10788 	} else {
10789 		drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
10790 		return;
10791 	}
10792 
10793 	pipe_config->icl_port_dplls[port_dpll_id].pll =
10794 		intel_get_shared_dpll_by_id(dev_priv, id);
10795 
10796 	icl_set_active_port_dpll(pipe_config, port_dpll_id);
10797 }
10798 
10799 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10800 				enum port port,
10801 				struct intel_crtc_state *pipe_config)
10802 {
10803 	enum intel_dpll_id id;
10804 
10805 	switch (port) {
10806 	case PORT_A:
10807 		id = DPLL_ID_SKL_DPLL0;
10808 		break;
10809 	case PORT_B:
10810 		id = DPLL_ID_SKL_DPLL1;
10811 		break;
10812 	case PORT_C:
10813 		id = DPLL_ID_SKL_DPLL2;
10814 		break;
10815 	default:
10816 		drm_err(&dev_priv->drm, "Incorrect port type\n");
10817 		return;
10818 	}
10819 
10820 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10821 }
10822 
10823 static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10824 			    struct intel_crtc_state *pipe_config)
10825 {
10826 	enum intel_dpll_id id;
10827 	u32 temp;
10828 
10829 	temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
10830 	id = temp >> (port * 3 + 1);
10831 
10832 	if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
10833 		return;
10834 
10835 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10836 }
10837 
10838 static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10839 			    struct intel_crtc_state *pipe_config)
10840 {
10841 	enum intel_dpll_id id;
10842 	u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
10843 
10844 	switch (ddi_pll_sel) {
10845 	case PORT_CLK_SEL_WRPLL1:
10846 		id = DPLL_ID_WRPLL1;
10847 		break;
10848 	case PORT_CLK_SEL_WRPLL2:
10849 		id = DPLL_ID_WRPLL2;
10850 		break;
10851 	case PORT_CLK_SEL_SPLL:
10852 		id = DPLL_ID_SPLL;
10853 		break;
10854 	case PORT_CLK_SEL_LCPLL_810:
10855 		id = DPLL_ID_LCPLL_810;
10856 		break;
10857 	case PORT_CLK_SEL_LCPLL_1350:
10858 		id = DPLL_ID_LCPLL_1350;
10859 		break;
10860 	case PORT_CLK_SEL_LCPLL_2700:
10861 		id = DPLL_ID_LCPLL_2700;
10862 		break;
10863 	default:
10864 		MISSING_CASE(ddi_pll_sel);
10865 		/* fall through */
10866 	case PORT_CLK_SEL_NONE:
10867 		return;
10868 	}
10869 
10870 	pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10871 }
10872 
10873 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10874 				     struct intel_crtc_state *pipe_config,
10875 				     u64 *power_domain_mask,
10876 				     intel_wakeref_t *wakerefs)
10877 {
10878 	struct drm_device *dev = crtc->base.dev;
10879 	struct drm_i915_private *dev_priv = to_i915(dev);
10880 	enum intel_display_power_domain power_domain;
10881 	unsigned long panel_transcoder_mask = 0;
10882 	unsigned long enabled_panel_transcoders = 0;
10883 	enum transcoder panel_transcoder;
10884 	intel_wakeref_t wf;
10885 	u32 tmp;
10886 
10887 	if (INTEL_GEN(dev_priv) >= 11)
10888 		panel_transcoder_mask |=
10889 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
10890 
10891 	if (HAS_TRANSCODER_EDP(dev_priv))
10892 		panel_transcoder_mask |= BIT(TRANSCODER_EDP);
10893 
10894 	/*
10895 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
10896 	 * and DSI transcoders handled below.
10897 	 */
10898 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10899 
10900 	/*
10901 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10902 	 * consistency and less surprising code; it's in always on power).
10903 	 */
10904 	for_each_set_bit(panel_transcoder,
10905 			 &panel_transcoder_mask,
10906 			 ARRAY_SIZE(INTEL_INFO(dev_priv)->trans_offsets)) {
10907 		bool force_thru = false;
10908 		enum pipe trans_pipe;
10909 
10910 		tmp = intel_de_read(dev_priv,
10911 				    TRANS_DDI_FUNC_CTL(panel_transcoder));
10912 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
10913 			continue;
10914 
10915 		/*
10916 		 * Log all enabled ones, only use the first one.
10917 		 *
10918 		 * FIXME: This won't work for two separate DSI displays.
10919 		 */
10920 		enabled_panel_transcoders |= BIT(panel_transcoder);
10921 		if (enabled_panel_transcoders != BIT(panel_transcoder))
10922 			continue;
10923 
10924 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10925 		default:
10926 			drm_WARN(dev, 1,
10927 				 "unknown pipe linked to transcoder %s\n",
10928 				 transcoder_name(panel_transcoder));
10929 			/* fall through */
10930 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
10931 			force_thru = true;
10932 			/* fall through */
10933 		case TRANS_DDI_EDP_INPUT_A_ON:
10934 			trans_pipe = PIPE_A;
10935 			break;
10936 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
10937 			trans_pipe = PIPE_B;
10938 			break;
10939 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
10940 			trans_pipe = PIPE_C;
10941 			break;
10942 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
10943 			trans_pipe = PIPE_D;
10944 			break;
10945 		}
10946 
10947 		if (trans_pipe == crtc->pipe) {
10948 			pipe_config->cpu_transcoder = panel_transcoder;
10949 			pipe_config->pch_pfit.force_thru = force_thru;
10950 		}
10951 	}
10952 
10953 	/*
10954 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
10955 	 */
10956 	drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
10957 		    enabled_panel_transcoders != BIT(TRANSCODER_EDP));
10958 
10959 	power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10960 	drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
10961 
10962 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10963 	if (!wf)
10964 		return false;
10965 
10966 	wakerefs[power_domain] = wf;
10967 	*power_domain_mask |= BIT_ULL(power_domain);
10968 
10969 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
10970 
10971 	return tmp & PIPECONF_ENABLE;
10972 }
10973 
10974 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10975 					 struct intel_crtc_state *pipe_config,
10976 					 u64 *power_domain_mask,
10977 					 intel_wakeref_t *wakerefs)
10978 {
10979 	struct drm_device *dev = crtc->base.dev;
10980 	struct drm_i915_private *dev_priv = to_i915(dev);
10981 	enum intel_display_power_domain power_domain;
10982 	enum transcoder cpu_transcoder;
10983 	intel_wakeref_t wf;
10984 	enum port port;
10985 	u32 tmp;
10986 
10987 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10988 		if (port == PORT_A)
10989 			cpu_transcoder = TRANSCODER_DSI_A;
10990 		else
10991 			cpu_transcoder = TRANSCODER_DSI_C;
10992 
10993 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10994 		drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
10995 
10996 		wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
10997 		if (!wf)
10998 			continue;
10999 
11000 		wakerefs[power_domain] = wf;
11001 		*power_domain_mask |= BIT_ULL(power_domain);
11002 
11003 		/*
11004 		 * The PLL needs to be enabled with a valid divider
11005 		 * configuration, otherwise accessing DSI registers will hang
11006 		 * the machine. See BSpec North Display Engine
11007 		 * registers/MIPI[BXT]. We can break out here early, since we
11008 		 * need the same DSI PLL to be enabled for both DSI ports.
11009 		 */
11010 		if (!bxt_dsi_pll_is_enabled(dev_priv))
11011 			break;
11012 
11013 		/* XXX: this works for video mode only */
11014 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
11015 		if (!(tmp & DPI_ENABLE))
11016 			continue;
11017 
11018 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
11019 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11020 			continue;
11021 
11022 		pipe_config->cpu_transcoder = cpu_transcoder;
11023 		break;
11024 	}
11025 
11026 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
11027 }
11028 
11029 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11030 				   struct intel_crtc_state *pipe_config)
11031 {
11032 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11033 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
11034 	struct intel_shared_dpll *pll;
11035 	enum port port;
11036 	u32 tmp;
11037 
11038 	if (transcoder_is_dsi(cpu_transcoder)) {
11039 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11040 						PORT_A : PORT_B;
11041 	} else {
11042 		tmp = intel_de_read(dev_priv,
11043 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
11044 		if (INTEL_GEN(dev_priv) >= 12)
11045 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11046 		else
11047 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11048 	}
11049 
11050 	if (INTEL_GEN(dev_priv) >= 11)
11051 		icl_get_ddi_pll(dev_priv, port, pipe_config);
11052 	else if (IS_CANNONLAKE(dev_priv))
11053 		cnl_get_ddi_pll(dev_priv, port, pipe_config);
11054 	else if (IS_GEN9_BC(dev_priv))
11055 		skl_get_ddi_pll(dev_priv, port, pipe_config);
11056 	else if (IS_GEN9_LP(dev_priv))
11057 		bxt_get_ddi_pll(dev_priv, port, pipe_config);
11058 	else
11059 		hsw_get_ddi_pll(dev_priv, port, pipe_config);
11060 
11061 	pll = pipe_config->shared_dpll;
11062 	if (pll) {
11063 		drm_WARN_ON(&dev_priv->drm,
11064 			    !pll->info->funcs->get_hw_state(dev_priv, pll,
11065 						&pipe_config->dpll_hw_state));
11066 	}
11067 
11068 	/*
11069 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
11070 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
11071 	 * the PCH transcoder is on.
11072 	 */
11073 	if (INTEL_GEN(dev_priv) < 9 &&
11074 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
11075 		pipe_config->has_pch_encoder = true;
11076 
11077 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
11078 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11079 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
11080 
11081 		ilk_get_fdi_m_n_config(crtc, pipe_config);
11082 	}
11083 }
11084 
11085 static enum transcoder transcoder_master_readout(struct drm_i915_private *dev_priv,
11086 						 enum transcoder cpu_transcoder)
11087 {
11088 	u32 trans_port_sync, master_select;
11089 
11090 	trans_port_sync = intel_de_read(dev_priv,
11091 				        TRANS_DDI_FUNC_CTL2(cpu_transcoder));
11092 
11093 	if ((trans_port_sync & PORT_SYNC_MODE_ENABLE) == 0)
11094 		return INVALID_TRANSCODER;
11095 
11096 	master_select = trans_port_sync &
11097 			PORT_SYNC_MODE_MASTER_SELECT_MASK;
11098 	if (master_select == 0)
11099 		return TRANSCODER_EDP;
11100 	else
11101 		return master_select - 1;
11102 }
11103 
11104 static void icl_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
11105 {
11106 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
11107 	u32 transcoders;
11108 	enum transcoder cpu_transcoder;
11109 
11110 	crtc_state->master_transcoder = transcoder_master_readout(dev_priv,
11111 								  crtc_state->cpu_transcoder);
11112 
11113 	transcoders = BIT(TRANSCODER_A) |
11114 		BIT(TRANSCODER_B) |
11115 		BIT(TRANSCODER_C) |
11116 		BIT(TRANSCODER_D);
11117 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
11118 		enum intel_display_power_domain power_domain;
11119 		intel_wakeref_t trans_wakeref;
11120 
11121 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
11122 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
11123 								   power_domain);
11124 
11125 		if (!trans_wakeref)
11126 			continue;
11127 
11128 		if (transcoder_master_readout(dev_priv, cpu_transcoder) ==
11129 		    crtc_state->cpu_transcoder)
11130 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
11131 
11132 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
11133 	}
11134 
11135 	drm_WARN_ON(&dev_priv->drm,
11136 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
11137 		    crtc_state->sync_mode_slaves_mask);
11138 }
11139 
11140 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11141 				struct intel_crtc_state *pipe_config)
11142 {
11143 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11144 	intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
11145 	enum intel_display_power_domain power_domain;
11146 	u64 power_domain_mask;
11147 	bool active;
11148 	u32 tmp;
11149 
11150 	pipe_config->master_transcoder = INVALID_TRANSCODER;
11151 
11152 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
11153 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11154 	if (!wf)
11155 		return false;
11156 
11157 	wakerefs[power_domain] = wf;
11158 	power_domain_mask = BIT_ULL(power_domain);
11159 
11160 	pipe_config->shared_dpll = NULL;
11161 
11162 	active = hsw_get_transcoder_state(crtc, pipe_config,
11163 					  &power_domain_mask, wakerefs);
11164 
11165 	if (IS_GEN9_LP(dev_priv) &&
11166 	    bxt_get_dsi_transcoder_state(crtc, pipe_config,
11167 					 &power_domain_mask, wakerefs)) {
11168 		drm_WARN_ON(&dev_priv->drm, active);
11169 		active = true;
11170 	}
11171 
11172 	if (!active)
11173 		goto out;
11174 
11175 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
11176 	    INTEL_GEN(dev_priv) >= 11) {
11177 		hsw_get_ddi_port_state(crtc, pipe_config);
11178 		intel_get_pipe_timings(crtc, pipe_config);
11179 	}
11180 
11181 	intel_get_pipe_src_size(crtc, pipe_config);
11182 
11183 	if (IS_HASWELL(dev_priv)) {
11184 		u32 tmp = intel_de_read(dev_priv,
11185 					PIPECONF(pipe_config->cpu_transcoder));
11186 
11187 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11188 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11189 		else
11190 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11191 	} else {
11192 		pipe_config->output_format =
11193 			bdw_get_pipemisc_output_format(crtc);
11194 
11195 		/*
11196 		 * Currently there is no interface defined to
11197 		 * check user preference between RGB/YCBCR444
11198 		 * or YCBCR420. So the only possible case for
11199 		 * YCBCR444 usage is driving YCBCR420 output
11200 		 * with LSPCON, when pipe is configured for
11201 		 * YCBCR444 output and LSPCON takes care of
11202 		 * downsampling it.
11203 		 */
11204 		pipe_config->lspcon_downsampling =
11205 			pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444;
11206 	}
11207 
11208 	pipe_config->gamma_mode = intel_de_read(dev_priv,
11209 						GAMMA_MODE(crtc->pipe));
11210 
11211 	pipe_config->csc_mode = intel_de_read(dev_priv,
11212 					      PIPE_CSC_MODE(crtc->pipe));
11213 
11214 	if (INTEL_GEN(dev_priv) >= 9) {
11215 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
11216 
11217 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11218 			pipe_config->gamma_enable = true;
11219 
11220 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11221 			pipe_config->csc_enable = true;
11222 	} else {
11223 		i9xx_get_pipe_color_config(pipe_config);
11224 	}
11225 
11226 	intel_color_get_config(pipe_config);
11227 
11228 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11229 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11230 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11231 		pipe_config->ips_linetime =
11232 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11233 
11234 	power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
11235 	drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
11236 
11237 	wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11238 	if (wf) {
11239 		wakerefs[power_domain] = wf;
11240 		power_domain_mask |= BIT_ULL(power_domain);
11241 
11242 		if (INTEL_GEN(dev_priv) >= 9)
11243 			skl_get_pfit_config(crtc, pipe_config);
11244 		else
11245 			ilk_get_pfit_config(crtc, pipe_config);
11246 	}
11247 
11248 	if (hsw_crtc_supports_ips(crtc)) {
11249 		if (IS_HASWELL(dev_priv))
11250 			pipe_config->ips_enabled = intel_de_read(dev_priv,
11251 								 IPS_CTL) & IPS_ENABLE;
11252 		else {
11253 			/*
11254 			 * We cannot readout IPS state on broadwell, set to
11255 			 * true so we can set it to a defined state on first
11256 			 * commit.
11257 			 */
11258 			pipe_config->ips_enabled = true;
11259 		}
11260 	}
11261 
11262 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
11263 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
11264 		pipe_config->pixel_multiplier =
11265 			intel_de_read(dev_priv,
11266 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
11267 	} else {
11268 		pipe_config->pixel_multiplier = 1;
11269 	}
11270 
11271 	if (INTEL_GEN(dev_priv) >= 11 &&
11272 	    !transcoder_is_dsi(pipe_config->cpu_transcoder))
11273 		icl_get_trans_port_sync_config(pipe_config);
11274 
11275 out:
11276 	for_each_power_domain(power_domain, power_domain_mask)
11277 		intel_display_power_put(dev_priv,
11278 					power_domain, wakerefs[power_domain]);
11279 
11280 	return active;
11281 }
11282 
11283 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
11284 {
11285 	struct drm_i915_private *dev_priv =
11286 		to_i915(plane_state->uapi.plane->dev);
11287 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11288 	const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11289 	u32 base;
11290 
11291 	if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
11292 		base = sg_dma_address(obj->mm.pages->sgl);
11293 	else
11294 		base = intel_plane_ggtt_offset(plane_state);
11295 
11296 	return base + plane_state->color_plane[0].offset;
11297 }
11298 
11299 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11300 {
11301 	int x = plane_state->uapi.dst.x1;
11302 	int y = plane_state->uapi.dst.y1;
11303 	u32 pos = 0;
11304 
11305 	if (x < 0) {
11306 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11307 		x = -x;
11308 	}
11309 	pos |= x << CURSOR_X_SHIFT;
11310 
11311 	if (y < 0) {
11312 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11313 		y = -y;
11314 	}
11315 	pos |= y << CURSOR_Y_SHIFT;
11316 
11317 	return pos;
11318 }
11319 
11320 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11321 {
11322 	const struct drm_mode_config *config =
11323 		&plane_state->uapi.plane->dev->mode_config;
11324 	int width = drm_rect_width(&plane_state->uapi.dst);
11325 	int height = drm_rect_height(&plane_state->uapi.dst);
11326 
11327 	return width > 0 && width <= config->cursor_width &&
11328 		height > 0 && height <= config->cursor_height;
11329 }
11330 
11331 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
11332 {
11333 	struct drm_i915_private *dev_priv =
11334 		to_i915(plane_state->uapi.plane->dev);
11335 	unsigned int rotation = plane_state->hw.rotation;
11336 	int src_x, src_y;
11337 	u32 offset;
11338 	int ret;
11339 
11340 	ret = intel_plane_compute_gtt(plane_state);
11341 	if (ret)
11342 		return ret;
11343 
11344 	if (!plane_state->uapi.visible)
11345 		return 0;
11346 
11347 	src_x = plane_state->uapi.src.x1 >> 16;
11348 	src_y = plane_state->uapi.src.y1 >> 16;
11349 
11350 	intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11351 	offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11352 						    plane_state, 0);
11353 
11354 	if (src_x != 0 || src_y != 0) {
11355 		drm_dbg_kms(&dev_priv->drm,
11356 			    "Arbitrary cursor panning not supported\n");
11357 		return -EINVAL;
11358 	}
11359 
11360 	/*
11361 	 * Put the final coordinates back so that the src
11362 	 * coordinate checks will see the right values.
11363 	 */
11364 	drm_rect_translate_to(&plane_state->uapi.src,
11365 			      src_x << 16, src_y << 16);
11366 
11367 	/* ILK+ do this automagically in hardware */
11368 	if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
11369 		const struct drm_framebuffer *fb = plane_state->hw.fb;
11370 		int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11371 		int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
11372 
11373 		offset += (src_h * src_w - 1) * fb->format->cpp[0];
11374 	}
11375 
11376 	plane_state->color_plane[0].offset = offset;
11377 	plane_state->color_plane[0].x = src_x;
11378 	plane_state->color_plane[0].y = src_y;
11379 
11380 	return 0;
11381 }
11382 
11383 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11384 			      struct intel_plane_state *plane_state)
11385 {
11386 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11387 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11388 	int ret;
11389 
11390 	if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
11391 		drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
11392 		return -EINVAL;
11393 	}
11394 
11395 	ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
11396 						  &crtc_state->uapi,
11397 						  DRM_PLANE_HELPER_NO_SCALING,
11398 						  DRM_PLANE_HELPER_NO_SCALING,
11399 						  true, true);
11400 	if (ret)
11401 		return ret;
11402 
11403 	/* Use the unclipped src/dst rectangles, which we program to hw */
11404 	plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11405 	plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
11406 
11407 	ret = intel_cursor_check_surface(plane_state);
11408 	if (ret)
11409 		return ret;
11410 
11411 	if (!plane_state->uapi.visible)
11412 		return 0;
11413 
11414 	ret = intel_plane_check_src_coordinates(plane_state);
11415 	if (ret)
11416 		return ret;
11417 
11418 	return 0;
11419 }
11420 
11421 static unsigned int
11422 i845_cursor_max_stride(struct intel_plane *plane,
11423 		       u32 pixel_format, u64 modifier,
11424 		       unsigned int rotation)
11425 {
11426 	return 2048;
11427 }
11428 
11429 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11430 {
11431 	u32 cntl = 0;
11432 
11433 	if (crtc_state->gamma_enable)
11434 		cntl |= CURSOR_GAMMA_ENABLE;
11435 
11436 	return cntl;
11437 }
11438 
11439 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11440 			   const struct intel_plane_state *plane_state)
11441 {
11442 	return CURSOR_ENABLE |
11443 		CURSOR_FORMAT_ARGB |
11444 		CURSOR_STRIDE(plane_state->color_plane[0].stride);
11445 }
11446 
11447 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11448 {
11449 	int width = drm_rect_width(&plane_state->uapi.dst);
11450 
11451 	/*
11452 	 * 845g/865g are only limited by the width of their cursors,
11453 	 * the height is arbitrary up to the precision of the register.
11454 	 */
11455 	return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
11456 }
11457 
11458 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
11459 			     struct intel_plane_state *plane_state)
11460 {
11461 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11462 	struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11463 	int ret;
11464 
11465 	ret = intel_check_cursor(crtc_state, plane_state);
11466 	if (ret)
11467 		return ret;
11468 
11469 	/* if we want to turn off the cursor ignore width and height */
11470 	if (!fb)
11471 		return 0;
11472 
11473 	/* Check for which cursor types we support */
11474 	if (!i845_cursor_size_ok(plane_state)) {
11475 		drm_dbg_kms(&i915->drm,
11476 			    "Cursor dimension %dx%d not supported\n",
11477 			    drm_rect_width(&plane_state->uapi.dst),
11478 			    drm_rect_height(&plane_state->uapi.dst));
11479 		return -EINVAL;
11480 	}
11481 
11482 	drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11483 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11484 
11485 	switch (fb->pitches[0]) {
11486 	case 256:
11487 	case 512:
11488 	case 1024:
11489 	case 2048:
11490 		break;
11491 	default:
11492 		 drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11493 			     fb->pitches[0]);
11494 		return -EINVAL;
11495 	}
11496 
11497 	plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11498 
11499 	return 0;
11500 }
11501 
11502 static void i845_update_cursor(struct intel_plane *plane,
11503 			       const struct intel_crtc_state *crtc_state,
11504 			       const struct intel_plane_state *plane_state)
11505 {
11506 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11507 	u32 cntl = 0, base = 0, pos = 0, size = 0;
11508 	unsigned long irqflags;
11509 
11510 	if (plane_state && plane_state->uapi.visible) {
11511 		unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11512 		unsigned int height = drm_rect_height(&plane_state->uapi.dst);
11513 
11514 		cntl = plane_state->ctl |
11515 			i845_cursor_ctl_crtc(crtc_state);
11516 
11517 		size = (height << 12) | width;
11518 
11519 		base = intel_cursor_base(plane_state);
11520 		pos = intel_cursor_position(plane_state);
11521 	}
11522 
11523 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11524 
11525 	/* On these chipsets we can only modify the base/size/stride
11526 	 * whilst the cursor is disabled.
11527 	 */
11528 	if (plane->cursor.base != base ||
11529 	    plane->cursor.size != size ||
11530 	    plane->cursor.cntl != cntl) {
11531 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11532 		intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11533 		intel_de_write_fw(dev_priv, CURSIZE, size);
11534 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11535 		intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
11536 
11537 		plane->cursor.base = base;
11538 		plane->cursor.size = size;
11539 		plane->cursor.cntl = cntl;
11540 	} else {
11541 		intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11542 	}
11543 
11544 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11545 }
11546 
11547 static void i845_disable_cursor(struct intel_plane *plane,
11548 				const struct intel_crtc_state *crtc_state)
11549 {
11550 	i845_update_cursor(plane, crtc_state, NULL);
11551 }
11552 
11553 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11554 				     enum pipe *pipe)
11555 {
11556 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11557 	enum intel_display_power_domain power_domain;
11558 	intel_wakeref_t wakeref;
11559 	bool ret;
11560 
11561 	power_domain = POWER_DOMAIN_PIPE(PIPE_A);
11562 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11563 	if (!wakeref)
11564 		return false;
11565 
11566 	ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
11567 
11568 	*pipe = PIPE_A;
11569 
11570 	intel_display_power_put(dev_priv, power_domain, wakeref);
11571 
11572 	return ret;
11573 }
11574 
11575 static unsigned int
11576 i9xx_cursor_max_stride(struct intel_plane *plane,
11577 		       u32 pixel_format, u64 modifier,
11578 		       unsigned int rotation)
11579 {
11580 	return plane->base.dev->mode_config.cursor_width * 4;
11581 }
11582 
11583 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11584 {
11585 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11586 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11587 	u32 cntl = 0;
11588 
11589 	if (INTEL_GEN(dev_priv) >= 11)
11590 		return cntl;
11591 
11592 	if (crtc_state->gamma_enable)
11593 		cntl = MCURSOR_GAMMA_ENABLE;
11594 
11595 	if (crtc_state->csc_enable)
11596 		cntl |= MCURSOR_PIPE_CSC_ENABLE;
11597 
11598 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11599 		cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
11600 
11601 	return cntl;
11602 }
11603 
11604 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11605 			   const struct intel_plane_state *plane_state)
11606 {
11607 	struct drm_i915_private *dev_priv =
11608 		to_i915(plane_state->uapi.plane->dev);
11609 	u32 cntl = 0;
11610 
11611 	if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11612 		cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11613 
11614 	switch (drm_rect_width(&plane_state->uapi.dst)) {
11615 	case 64:
11616 		cntl |= MCURSOR_MODE_64_ARGB_AX;
11617 		break;
11618 	case 128:
11619 		cntl |= MCURSOR_MODE_128_ARGB_AX;
11620 		break;
11621 	case 256:
11622 		cntl |= MCURSOR_MODE_256_ARGB_AX;
11623 		break;
11624 	default:
11625 		MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
11626 		return 0;
11627 	}
11628 
11629 	if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
11630 		cntl |= MCURSOR_ROTATE_180;
11631 
11632 	return cntl;
11633 }
11634 
11635 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
11636 {
11637 	struct drm_i915_private *dev_priv =
11638 		to_i915(plane_state->uapi.plane->dev);
11639 	int width = drm_rect_width(&plane_state->uapi.dst);
11640 	int height = drm_rect_height(&plane_state->uapi.dst);
11641 
11642 	if (!intel_cursor_size_ok(plane_state))
11643 		return false;
11644 
11645 	/* Cursor width is limited to a few power-of-two sizes */
11646 	switch (width) {
11647 	case 256:
11648 	case 128:
11649 	case 64:
11650 		break;
11651 	default:
11652 		return false;
11653 	}
11654 
11655 	/*
11656 	 * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11657 	 * height from 8 lines up to the cursor width, when the
11658 	 * cursor is not rotated. Everything else requires square
11659 	 * cursors.
11660 	 */
11661 	if (HAS_CUR_FBC(dev_priv) &&
11662 	    plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
11663 		if (height < 8 || height > width)
11664 			return false;
11665 	} else {
11666 		if (height != width)
11667 			return false;
11668 	}
11669 
11670 	return true;
11671 }
11672 
11673 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
11674 			     struct intel_plane_state *plane_state)
11675 {
11676 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11677 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11678 	const struct drm_framebuffer *fb = plane_state->hw.fb;
11679 	enum pipe pipe = plane->pipe;
11680 	int ret;
11681 
11682 	ret = intel_check_cursor(crtc_state, plane_state);
11683 	if (ret)
11684 		return ret;
11685 
11686 	/* if we want to turn off the cursor ignore width and height */
11687 	if (!fb)
11688 		return 0;
11689 
11690 	/* Check for which cursor types we support */
11691 	if (!i9xx_cursor_size_ok(plane_state)) {
11692 		drm_dbg(&dev_priv->drm,
11693 			"Cursor dimension %dx%d not supported\n",
11694 			drm_rect_width(&plane_state->uapi.dst),
11695 			drm_rect_height(&plane_state->uapi.dst));
11696 		return -EINVAL;
11697 	}
11698 
11699 	drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
11700 		    plane_state->color_plane[0].stride != fb->pitches[0]);
11701 
11702 	if (fb->pitches[0] !=
11703 	    drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
11704 		drm_dbg_kms(&dev_priv->drm,
11705 			    "Invalid cursor stride (%u) (cursor width %d)\n",
11706 			    fb->pitches[0],
11707 			    drm_rect_width(&plane_state->uapi.dst));
11708 		return -EINVAL;
11709 	}
11710 
11711 	/*
11712 	 * There's something wrong with the cursor on CHV pipe C.
11713 	 * If it straddles the left edge of the screen then
11714 	 * moving it away from the edge or disabling it often
11715 	 * results in a pipe underrun, and often that can lead to
11716 	 * dead pipe (constant underrun reported, and it scans
11717 	 * out just a solid color). To recover from that, the
11718 	 * display power well must be turned off and on again.
11719 	 * Refuse the put the cursor into that compromised position.
11720 	 */
11721 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
11722 	    plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
11723 		drm_dbg_kms(&dev_priv->drm,
11724 			    "CHV cursor C not allowed to straddle the left screen edge\n");
11725 		return -EINVAL;
11726 	}
11727 
11728 	plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
11729 
11730 	return 0;
11731 }
11732 
11733 static void i9xx_update_cursor(struct intel_plane *plane,
11734 			       const struct intel_crtc_state *crtc_state,
11735 			       const struct intel_plane_state *plane_state)
11736 {
11737 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11738 	enum pipe pipe = plane->pipe;
11739 	u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
11740 	unsigned long irqflags;
11741 
11742 	if (plane_state && plane_state->uapi.visible) {
11743 		unsigned width = drm_rect_width(&plane_state->uapi.dst);
11744 		unsigned height = drm_rect_height(&plane_state->uapi.dst);
11745 
11746 		cntl = plane_state->ctl |
11747 			i9xx_cursor_ctl_crtc(crtc_state);
11748 
11749 		if (width != height)
11750 			fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
11751 
11752 		base = intel_cursor_base(plane_state);
11753 		pos = intel_cursor_position(plane_state);
11754 	}
11755 
11756 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11757 
11758 	/*
11759 	 * On some platforms writing CURCNTR first will also
11760 	 * cause CURPOS to be armed by the CURBASE write.
11761 	 * Without the CURCNTR write the CURPOS write would
11762 	 * arm itself. Thus we always update CURCNTR before
11763 	 * CURPOS.
11764 	 *
11765 	 * On other platforms CURPOS always requires the
11766 	 * CURBASE write to arm the update. Additonally
11767 	 * a write to any of the cursor register will cancel
11768 	 * an already armed cursor update. Thus leaving out
11769 	 * the CURBASE write after CURPOS could lead to a
11770 	 * cursor that doesn't appear to move, or even change
11771 	 * shape. Thus we always write CURBASE.
11772 	 *
11773 	 * The other registers are armed by by the CURBASE write
11774 	 * except when the plane is getting enabled at which time
11775 	 * the CURCNTR write arms the update.
11776 	 */
11777 
11778 	if (INTEL_GEN(dev_priv) >= 9)
11779 		skl_write_cursor_wm(plane, crtc_state);
11780 
11781 	if (plane->cursor.base != base ||
11782 	    plane->cursor.size != fbc_ctl ||
11783 	    plane->cursor.cntl != cntl) {
11784 		if (HAS_CUR_FBC(dev_priv))
11785 			intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
11786 					  fbc_ctl);
11787 		intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
11788 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11789 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11790 
11791 		plane->cursor.base = base;
11792 		plane->cursor.size = fbc_ctl;
11793 		plane->cursor.cntl = cntl;
11794 	} else {
11795 		intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11796 		intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11797 	}
11798 
11799 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11800 }
11801 
11802 static void i9xx_disable_cursor(struct intel_plane *plane,
11803 				const struct intel_crtc_state *crtc_state)
11804 {
11805 	i9xx_update_cursor(plane, crtc_state, NULL);
11806 }
11807 
11808 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
11809 				     enum pipe *pipe)
11810 {
11811 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11812 	enum intel_display_power_domain power_domain;
11813 	intel_wakeref_t wakeref;
11814 	bool ret;
11815 	u32 val;
11816 
11817 	/*
11818 	 * Not 100% correct for planes that can move between pipes,
11819 	 * but that's only the case for gen2-3 which don't have any
11820 	 * display power wells.
11821 	 */
11822 	power_domain = POWER_DOMAIN_PIPE(plane->pipe);
11823 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11824 	if (!wakeref)
11825 		return false;
11826 
11827 	val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
11828 
11829 	ret = val & MCURSOR_MODE;
11830 
11831 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
11832 		*pipe = plane->pipe;
11833 	else
11834 		*pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
11835 			MCURSOR_PIPE_SELECT_SHIFT;
11836 
11837 	intel_display_power_put(dev_priv, power_domain, wakeref);
11838 
11839 	return ret;
11840 }
11841 
11842 /* VESA 640x480x72Hz mode to set on the pipe */
11843 static const struct drm_display_mode load_detect_mode = {
11844 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11845 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11846 };
11847 
11848 struct drm_framebuffer *
11849 intel_framebuffer_create(struct drm_i915_gem_object *obj,
11850 			 struct drm_mode_fb_cmd2 *mode_cmd)
11851 {
11852 	struct intel_framebuffer *intel_fb;
11853 	int ret;
11854 
11855 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
11856 	if (!intel_fb)
11857 		return ERR_PTR(-ENOMEM);
11858 
11859 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
11860 	if (ret)
11861 		goto err;
11862 
11863 	return &intel_fb->base;
11864 
11865 err:
11866 	kfree(intel_fb);
11867 	return ERR_PTR(ret);
11868 }
11869 
11870 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
11871 					struct drm_crtc *crtc)
11872 {
11873 	struct drm_plane *plane;
11874 	struct drm_plane_state *plane_state;
11875 	int ret, i;
11876 
11877 	ret = drm_atomic_add_affected_planes(state, crtc);
11878 	if (ret)
11879 		return ret;
11880 
11881 	for_each_new_plane_in_state(state, plane, plane_state, i) {
11882 		if (plane_state->crtc != crtc)
11883 			continue;
11884 
11885 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
11886 		if (ret)
11887 			return ret;
11888 
11889 		drm_atomic_set_fb_for_plane(plane_state, NULL);
11890 	}
11891 
11892 	return 0;
11893 }
11894 
11895 int intel_get_load_detect_pipe(struct drm_connector *connector,
11896 			       struct intel_load_detect_pipe *old,
11897 			       struct drm_modeset_acquire_ctx *ctx)
11898 {
11899 	struct intel_crtc *intel_crtc;
11900 	struct intel_encoder *intel_encoder =
11901 		intel_attached_encoder(to_intel_connector(connector));
11902 	struct drm_crtc *possible_crtc;
11903 	struct drm_encoder *encoder = &intel_encoder->base;
11904 	struct drm_crtc *crtc = NULL;
11905 	struct drm_device *dev = encoder->dev;
11906 	struct drm_i915_private *dev_priv = to_i915(dev);
11907 	struct drm_mode_config *config = &dev->mode_config;
11908 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
11909 	struct drm_connector_state *connector_state;
11910 	struct intel_crtc_state *crtc_state;
11911 	int ret, i = -1;
11912 
11913 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
11914 		    connector->base.id, connector->name,
11915 		    encoder->base.id, encoder->name);
11916 
11917 	old->restore_state = NULL;
11918 
11919 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
11920 
11921 	/*
11922 	 * Algorithm gets a little messy:
11923 	 *
11924 	 *   - if the connector already has an assigned crtc, use it (but make
11925 	 *     sure it's on first)
11926 	 *
11927 	 *   - try to find the first unused crtc that can drive this connector,
11928 	 *     and use that if we find one
11929 	 */
11930 
11931 	/* See if we already have a CRTC for this connector */
11932 	if (connector->state->crtc) {
11933 		crtc = connector->state->crtc;
11934 
11935 		ret = drm_modeset_lock(&crtc->mutex, ctx);
11936 		if (ret)
11937 			goto fail;
11938 
11939 		/* Make sure the crtc and connector are running */
11940 		goto found;
11941 	}
11942 
11943 	/* Find an unused one (if possible) */
11944 	for_each_crtc(dev, possible_crtc) {
11945 		i++;
11946 		if (!(encoder->possible_crtcs & (1 << i)))
11947 			continue;
11948 
11949 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11950 		if (ret)
11951 			goto fail;
11952 
11953 		if (possible_crtc->state->enable) {
11954 			drm_modeset_unlock(&possible_crtc->mutex);
11955 			continue;
11956 		}
11957 
11958 		crtc = possible_crtc;
11959 		break;
11960 	}
11961 
11962 	/*
11963 	 * If we didn't find an unused CRTC, don't use any.
11964 	 */
11965 	if (!crtc) {
11966 		drm_dbg_kms(&dev_priv->drm,
11967 			    "no pipe available for load-detect\n");
11968 		ret = -ENODEV;
11969 		goto fail;
11970 	}
11971 
11972 found:
11973 	intel_crtc = to_intel_crtc(crtc);
11974 
11975 	state = drm_atomic_state_alloc(dev);
11976 	restore_state = drm_atomic_state_alloc(dev);
11977 	if (!state || !restore_state) {
11978 		ret = -ENOMEM;
11979 		goto fail;
11980 	}
11981 
11982 	state->acquire_ctx = ctx;
11983 	restore_state->acquire_ctx = ctx;
11984 
11985 	connector_state = drm_atomic_get_connector_state(state, connector);
11986 	if (IS_ERR(connector_state)) {
11987 		ret = PTR_ERR(connector_state);
11988 		goto fail;
11989 	}
11990 
11991 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11992 	if (ret)
11993 		goto fail;
11994 
11995 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11996 	if (IS_ERR(crtc_state)) {
11997 		ret = PTR_ERR(crtc_state);
11998 		goto fail;
11999 	}
12000 
12001 	crtc_state->uapi.active = true;
12002 
12003 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
12004 					   &load_detect_mode);
12005 	if (ret)
12006 		goto fail;
12007 
12008 	ret = intel_modeset_disable_planes(state, crtc);
12009 	if (ret)
12010 		goto fail;
12011 
12012 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12013 	if (!ret)
12014 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
12015 	if (!ret)
12016 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
12017 	if (ret) {
12018 		drm_dbg_kms(&dev_priv->drm,
12019 			    "Failed to create a copy of old state to restore: %i\n",
12020 			    ret);
12021 		goto fail;
12022 	}
12023 
12024 	ret = drm_atomic_commit(state);
12025 	if (ret) {
12026 		drm_dbg_kms(&dev_priv->drm,
12027 			    "failed to set mode on load-detect pipe\n");
12028 		goto fail;
12029 	}
12030 
12031 	old->restore_state = restore_state;
12032 	drm_atomic_state_put(state);
12033 
12034 	/* let the connector get through one full cycle before testing */
12035 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
12036 	return true;
12037 
12038 fail:
12039 	if (state) {
12040 		drm_atomic_state_put(state);
12041 		state = NULL;
12042 	}
12043 	if (restore_state) {
12044 		drm_atomic_state_put(restore_state);
12045 		restore_state = NULL;
12046 	}
12047 
12048 	if (ret == -EDEADLK)
12049 		return ret;
12050 
12051 	return false;
12052 }
12053 
12054 void intel_release_load_detect_pipe(struct drm_connector *connector,
12055 				    struct intel_load_detect_pipe *old,
12056 				    struct drm_modeset_acquire_ctx *ctx)
12057 {
12058 	struct intel_encoder *intel_encoder =
12059 		intel_attached_encoder(to_intel_connector(connector));
12060 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
12061 	struct drm_encoder *encoder = &intel_encoder->base;
12062 	struct drm_atomic_state *state = old->restore_state;
12063 	int ret;
12064 
12065 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12066 		    connector->base.id, connector->name,
12067 		    encoder->base.id, encoder->name);
12068 
12069 	if (!state)
12070 		return;
12071 
12072 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
12073 	if (ret)
12074 		drm_dbg_kms(&i915->drm,
12075 			    "Couldn't release load detect pipe: %i\n", ret);
12076 	drm_atomic_state_put(state);
12077 }
12078 
12079 static int i9xx_pll_refclk(struct drm_device *dev,
12080 			   const struct intel_crtc_state *pipe_config)
12081 {
12082 	struct drm_i915_private *dev_priv = to_i915(dev);
12083 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12084 
12085 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
12086 		return dev_priv->vbt.lvds_ssc_freq;
12087 	else if (HAS_PCH_SPLIT(dev_priv))
12088 		return 120000;
12089 	else if (!IS_GEN(dev_priv, 2))
12090 		return 96000;
12091 	else
12092 		return 48000;
12093 }
12094 
12095 /* Returns the clock of the currently programmed mode of the given pipe. */
12096 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
12097 				struct intel_crtc_state *pipe_config)
12098 {
12099 	struct drm_device *dev = crtc->base.dev;
12100 	struct drm_i915_private *dev_priv = to_i915(dev);
12101 	enum pipe pipe = crtc->pipe;
12102 	u32 dpll = pipe_config->dpll_hw_state.dpll;
12103 	u32 fp;
12104 	struct dpll clock;
12105 	int port_clock;
12106 	int refclk = i9xx_pll_refclk(dev, pipe_config);
12107 
12108 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
12109 		fp = pipe_config->dpll_hw_state.fp0;
12110 	else
12111 		fp = pipe_config->dpll_hw_state.fp1;
12112 
12113 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
12114 	if (IS_PINEVIEW(dev_priv)) {
12115 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12116 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
12117 	} else {
12118 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12119 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12120 	}
12121 
12122 	if (!IS_GEN(dev_priv, 2)) {
12123 		if (IS_PINEVIEW(dev_priv))
12124 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12125 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
12126 		else
12127 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
12128 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
12129 
12130 		switch (dpll & DPLL_MODE_MASK) {
12131 		case DPLLB_MODE_DAC_SERIAL:
12132 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12133 				5 : 10;
12134 			break;
12135 		case DPLLB_MODE_LVDS:
12136 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12137 				7 : 14;
12138 			break;
12139 		default:
12140 			drm_dbg_kms(&dev_priv->drm,
12141 				    "Unknown DPLL mode %08x in programmed "
12142 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
12143 			return;
12144 		}
12145 
12146 		if (IS_PINEVIEW(dev_priv))
12147 			port_clock = pnv_calc_dpll_params(refclk, &clock);
12148 		else
12149 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
12150 	} else {
12151 		u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12152 								 LVDS);
12153 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
12154 
12155 		if (is_lvds) {
12156 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12157 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
12158 
12159 			if (lvds & LVDS_CLKB_POWER_UP)
12160 				clock.p2 = 7;
12161 			else
12162 				clock.p2 = 14;
12163 		} else {
12164 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
12165 				clock.p1 = 2;
12166 			else {
12167 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12168 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12169 			}
12170 			if (dpll & PLL_P2_DIVIDE_BY_4)
12171 				clock.p2 = 4;
12172 			else
12173 				clock.p2 = 2;
12174 		}
12175 
12176 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
12177 	}
12178 
12179 	/*
12180 	 * This value includes pixel_multiplier. We will use
12181 	 * port_clock to compute adjusted_mode.crtc_clock in the
12182 	 * encoder's get_config() function.
12183 	 */
12184 	pipe_config->port_clock = port_clock;
12185 }
12186 
12187 int intel_dotclock_calculate(int link_freq,
12188 			     const struct intel_link_m_n *m_n)
12189 {
12190 	/*
12191 	 * The calculation for the data clock is:
12192 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
12193 	 * But we want to avoid losing precison if possible, so:
12194 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
12195 	 *
12196 	 * and the link clock is simpler:
12197 	 * link_clock = (m * link_clock) / n
12198 	 */
12199 
12200 	if (!m_n->link_n)
12201 		return 0;
12202 
12203 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
12204 }
12205 
12206 static void ilk_pch_clock_get(struct intel_crtc *crtc,
12207 			      struct intel_crtc_state *pipe_config)
12208 {
12209 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12210 
12211 	/* read out port_clock from the DPLL */
12212 	i9xx_crtc_clock_get(crtc, pipe_config);
12213 
12214 	/*
12215 	 * In case there is an active pipe without active ports,
12216 	 * we may need some idea for the dotclock anyway.
12217 	 * Calculate one based on the FDI configuration.
12218 	 */
12219 	pipe_config->hw.adjusted_mode.crtc_clock =
12220 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12221 					 &pipe_config->fdi_m_n);
12222 }
12223 
12224 static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12225 				   struct intel_crtc *crtc)
12226 {
12227 	memset(crtc_state, 0, sizeof(*crtc_state));
12228 
12229 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12230 
12231 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12232 	crtc_state->master_transcoder = INVALID_TRANSCODER;
12233 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12234 	crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12235 	crtc_state->scaler_state.scaler_id = -1;
12236 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
12237 }
12238 
12239 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12240 {
12241 	struct intel_crtc_state *crtc_state;
12242 
12243 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12244 
12245 	if (crtc_state)
12246 		intel_crtc_state_reset(crtc_state, crtc);
12247 
12248 	return crtc_state;
12249 }
12250 
12251 /* Returns the currently programmed mode of the given encoder. */
12252 struct drm_display_mode *
12253 intel_encoder_current_mode(struct intel_encoder *encoder)
12254 {
12255 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12256 	struct intel_crtc_state *crtc_state;
12257 	struct drm_display_mode *mode;
12258 	struct intel_crtc *crtc;
12259 	enum pipe pipe;
12260 
12261 	if (!encoder->get_hw_state(encoder, &pipe))
12262 		return NULL;
12263 
12264 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12265 
12266 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12267 	if (!mode)
12268 		return NULL;
12269 
12270 	crtc_state = intel_crtc_state_alloc(crtc);
12271 	if (!crtc_state) {
12272 		kfree(mode);
12273 		return NULL;
12274 	}
12275 
12276 	if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
12277 		kfree(crtc_state);
12278 		kfree(mode);
12279 		return NULL;
12280 	}
12281 
12282 	encoder->get_config(encoder, crtc_state);
12283 
12284 	intel_mode_from_pipe_config(mode, crtc_state);
12285 
12286 	kfree(crtc_state);
12287 
12288 	return mode;
12289 }
12290 
12291 static void intel_crtc_destroy(struct drm_crtc *crtc)
12292 {
12293 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12294 
12295 	drm_crtc_cleanup(crtc);
12296 	kfree(intel_crtc);
12297 }
12298 
12299 /**
12300  * intel_wm_need_update - Check whether watermarks need updating
12301  * @cur: current plane state
12302  * @new: new plane state
12303  *
12304  * Check current plane state versus the new one to determine whether
12305  * watermarks need to be recalculated.
12306  *
12307  * Returns true or false.
12308  */
12309 static bool intel_wm_need_update(const struct intel_plane_state *cur,
12310 				 struct intel_plane_state *new)
12311 {
12312 	/* Update watermarks on tiling or size changes. */
12313 	if (new->uapi.visible != cur->uapi.visible)
12314 		return true;
12315 
12316 	if (!cur->hw.fb || !new->hw.fb)
12317 		return false;
12318 
12319 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12320 	    cur->hw.rotation != new->hw.rotation ||
12321 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12322 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12323 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12324 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
12325 		return true;
12326 
12327 	return false;
12328 }
12329 
12330 static bool needs_scaling(const struct intel_plane_state *state)
12331 {
12332 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
12333 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
12334 	int dst_w = drm_rect_width(&state->uapi.dst);
12335 	int dst_h = drm_rect_height(&state->uapi.dst);
12336 
12337 	return (src_w != dst_w || src_h != dst_h);
12338 }
12339 
12340 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
12341 				    struct intel_crtc_state *crtc_state,
12342 				    const struct intel_plane_state *old_plane_state,
12343 				    struct intel_plane_state *plane_state)
12344 {
12345 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12346 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12347 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12348 	bool mode_changed = needs_modeset(crtc_state);
12349 	bool was_crtc_enabled = old_crtc_state->hw.active;
12350 	bool is_crtc_enabled = crtc_state->hw.active;
12351 	bool turn_off, turn_on, visible, was_visible;
12352 	int ret;
12353 
12354 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
12355 		ret = skl_update_scaler_plane(crtc_state, plane_state);
12356 		if (ret)
12357 			return ret;
12358 	}
12359 
12360 	was_visible = old_plane_state->uapi.visible;
12361 	visible = plane_state->uapi.visible;
12362 
12363 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
12364 		was_visible = false;
12365 
12366 	/*
12367 	 * Visibility is calculated as if the crtc was on, but
12368 	 * after scaler setup everything depends on it being off
12369 	 * when the crtc isn't active.
12370 	 *
12371 	 * FIXME this is wrong for watermarks. Watermarks should also
12372 	 * be computed as if the pipe would be active. Perhaps move
12373 	 * per-plane wm computation to the .check_plane() hook, and
12374 	 * only combine the results from all planes in the current place?
12375 	 */
12376 	if (!is_crtc_enabled) {
12377 		plane_state->uapi.visible = visible = false;
12378 		crtc_state->active_planes &= ~BIT(plane->id);
12379 		crtc_state->data_rate[plane->id] = 0;
12380 		crtc_state->min_cdclk[plane->id] = 0;
12381 	}
12382 
12383 	if (!was_visible && !visible)
12384 		return 0;
12385 
12386 	turn_off = was_visible && (!visible || mode_changed);
12387 	turn_on = visible && (!was_visible || mode_changed);
12388 
12389 	drm_dbg_atomic(&dev_priv->drm,
12390 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12391 		       crtc->base.base.id, crtc->base.name,
12392 		       plane->base.base.id, plane->base.name,
12393 		       was_visible, visible,
12394 		       turn_off, turn_on, mode_changed);
12395 
12396 	if (turn_on) {
12397 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12398 			crtc_state->update_wm_pre = true;
12399 
12400 		/* must disable cxsr around plane enable/disable */
12401 		if (plane->id != PLANE_CURSOR)
12402 			crtc_state->disable_cxsr = true;
12403 	} else if (turn_off) {
12404 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12405 			crtc_state->update_wm_post = true;
12406 
12407 		/* must disable cxsr around plane enable/disable */
12408 		if (plane->id != PLANE_CURSOR)
12409 			crtc_state->disable_cxsr = true;
12410 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
12411 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
12412 			/* FIXME bollocks */
12413 			crtc_state->update_wm_pre = true;
12414 			crtc_state->update_wm_post = true;
12415 		}
12416 	}
12417 
12418 	if (visible || was_visible)
12419 		crtc_state->fb_bits |= plane->frontbuffer_bit;
12420 
12421 	/*
12422 	 * ILK/SNB DVSACNTR/Sprite Enable
12423 	 * IVB SPR_CTL/Sprite Enable
12424 	 * "When in Self Refresh Big FIFO mode, a write to enable the
12425 	 *  plane will be internally buffered and delayed while Big FIFO
12426 	 *  mode is exiting."
12427 	 *
12428 	 * Which means that enabling the sprite can take an extra frame
12429 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
12430 	 * down to LP0 and wait for vblank in order to make sure the
12431 	 * sprite gets enabled on the next vblank after the register write.
12432 	 * Doing otherwise would risk enabling the sprite one frame after
12433 	 * we've already signalled flip completion. We can resume LP1+
12434 	 * once the sprite has been enabled.
12435 	 *
12436 	 *
12437 	 * WaCxSRDisabledForSpriteScaling:ivb
12438 	 * IVB SPR_SCALE/Scaling Enable
12439 	 * "Low Power watermarks must be disabled for at least one
12440 	 *  frame before enabling sprite scaling, and kept disabled
12441 	 *  until sprite scaling is disabled."
12442 	 *
12443 	 * ILK/SNB DVSASCALE/Scaling Enable
12444 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
12445 	 *  masked off while Big FIFO mode is exiting."
12446 	 *
12447 	 * Despite the w/a only being listed for IVB we assume that
12448 	 * the ILK/SNB note has similar ramifications, hence we apply
12449 	 * the w/a on all three platforms.
12450 	 *
12451 	 * With experimental results seems this is needed also for primary
12452 	 * plane, not only sprite plane.
12453 	 */
12454 	if (plane->id != PLANE_CURSOR &&
12455 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
12456 	     IS_IVYBRIDGE(dev_priv)) &&
12457 	    (turn_on || (!needs_scaling(old_plane_state) &&
12458 			 needs_scaling(plane_state))))
12459 		crtc_state->disable_lp_wm = true;
12460 
12461 	return 0;
12462 }
12463 
12464 static bool encoders_cloneable(const struct intel_encoder *a,
12465 			       const struct intel_encoder *b)
12466 {
12467 	/* masks could be asymmetric, so check both ways */
12468 	return a == b || (a->cloneable & (1 << b->type) &&
12469 			  b->cloneable & (1 << a->type));
12470 }
12471 
12472 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12473 					 struct intel_crtc *crtc,
12474 					 struct intel_encoder *encoder)
12475 {
12476 	struct intel_encoder *source_encoder;
12477 	struct drm_connector *connector;
12478 	struct drm_connector_state *connector_state;
12479 	int i;
12480 
12481 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12482 		if (connector_state->crtc != &crtc->base)
12483 			continue;
12484 
12485 		source_encoder =
12486 			to_intel_encoder(connector_state->best_encoder);
12487 		if (!encoders_cloneable(encoder, source_encoder))
12488 			return false;
12489 	}
12490 
12491 	return true;
12492 }
12493 
12494 static int icl_add_linked_planes(struct intel_atomic_state *state)
12495 {
12496 	struct intel_plane *plane, *linked;
12497 	struct intel_plane_state *plane_state, *linked_plane_state;
12498 	int i;
12499 
12500 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12501 		linked = plane_state->planar_linked_plane;
12502 
12503 		if (!linked)
12504 			continue;
12505 
12506 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
12507 		if (IS_ERR(linked_plane_state))
12508 			return PTR_ERR(linked_plane_state);
12509 
12510 		WARN_ON(linked_plane_state->planar_linked_plane != plane);
12511 		WARN_ON(linked_plane_state->planar_slave == plane_state->planar_slave);
12512 	}
12513 
12514 	return 0;
12515 }
12516 
12517 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12518 {
12519 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12520 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12521 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
12522 	struct intel_plane *plane, *linked;
12523 	struct intel_plane_state *plane_state;
12524 	int i;
12525 
12526 	if (INTEL_GEN(dev_priv) < 11)
12527 		return 0;
12528 
12529 	/*
12530 	 * Destroy all old plane links and make the slave plane invisible
12531 	 * in the crtc_state->active_planes mask.
12532 	 */
12533 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12534 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
12535 			continue;
12536 
12537 		plane_state->planar_linked_plane = NULL;
12538 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
12539 			crtc_state->active_planes &= ~BIT(plane->id);
12540 			crtc_state->update_planes |= BIT(plane->id);
12541 		}
12542 
12543 		plane_state->planar_slave = false;
12544 	}
12545 
12546 	if (!crtc_state->nv12_planes)
12547 		return 0;
12548 
12549 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12550 		struct intel_plane_state *linked_state = NULL;
12551 
12552 		if (plane->pipe != crtc->pipe ||
12553 		    !(crtc_state->nv12_planes & BIT(plane->id)))
12554 			continue;
12555 
12556 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
12557 			if (!icl_is_nv12_y_plane(linked->id))
12558 				continue;
12559 
12560 			if (crtc_state->active_planes & BIT(linked->id))
12561 				continue;
12562 
12563 			linked_state = intel_atomic_get_plane_state(state, linked);
12564 			if (IS_ERR(linked_state))
12565 				return PTR_ERR(linked_state);
12566 
12567 			break;
12568 		}
12569 
12570 		if (!linked_state) {
12571 			drm_dbg_kms(&dev_priv->drm,
12572 				    "Need %d free Y planes for planar YUV\n",
12573 				    hweight8(crtc_state->nv12_planes));
12574 
12575 			return -EINVAL;
12576 		}
12577 
12578 		plane_state->planar_linked_plane = linked;
12579 
12580 		linked_state->planar_slave = true;
12581 		linked_state->planar_linked_plane = plane;
12582 		crtc_state->active_planes |= BIT(linked->id);
12583 		crtc_state->update_planes |= BIT(linked->id);
12584 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12585 			    linked->base.name, plane->base.name);
12586 
12587 		/* Copy parameters to slave plane */
12588 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12589 		linked_state->color_ctl = plane_state->color_ctl;
12590 		linked_state->view = plane_state->view;
12591 		memcpy(linked_state->color_plane, plane_state->color_plane,
12592 		       sizeof(linked_state->color_plane));
12593 
12594 		intel_plane_copy_uapi_to_hw_state(linked_state, plane_state);
12595 		linked_state->uapi.src = plane_state->uapi.src;
12596 		linked_state->uapi.dst = plane_state->uapi.dst;
12597 
12598 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
12599 			if (linked->id == PLANE_SPRITE5)
12600 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12601 			else if (linked->id == PLANE_SPRITE4)
12602 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
12603 			else
12604 				MISSING_CASE(linked->id);
12605 		}
12606 	}
12607 
12608 	return 0;
12609 }
12610 
12611 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12612 {
12613 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
12614 	struct intel_atomic_state *state =
12615 		to_intel_atomic_state(new_crtc_state->uapi.state);
12616 	const struct intel_crtc_state *old_crtc_state =
12617 		intel_atomic_get_old_crtc_state(state, crtc);
12618 
12619 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12620 }
12621 
12622 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12623 {
12624 	const struct drm_display_mode *adjusted_mode =
12625 		&crtc_state->hw.adjusted_mode;
12626 
12627 	if (!crtc_state->hw.enable)
12628 		return 0;
12629 
12630 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12631 				 adjusted_mode->crtc_clock);
12632 }
12633 
12634 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12635 			       const struct intel_cdclk_state *cdclk_state)
12636 {
12637 	const struct drm_display_mode *adjusted_mode =
12638 		&crtc_state->hw.adjusted_mode;
12639 
12640 	if (!crtc_state->hw.enable)
12641 		return 0;
12642 
12643 	return DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12644 				 cdclk_state->logical.cdclk);
12645 }
12646 
12647 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12648 {
12649 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12650 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12651 	const struct drm_display_mode *adjusted_mode =
12652 		&crtc_state->hw.adjusted_mode;
12653 	u16 linetime_wm;
12654 
12655 	if (!crtc_state->hw.enable)
12656 		return 0;
12657 
12658 	linetime_wm = DIV_ROUND_UP(adjusted_mode->crtc_htotal * 1000 * 8,
12659 				   crtc_state->pixel_rate);
12660 
12661 	/* Display WA #1135: BXT:ALL GLK:ALL */
12662 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12663 		linetime_wm /= 2;
12664 
12665 	return linetime_wm;
12666 }
12667 
12668 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12669 				   struct intel_crtc *crtc)
12670 {
12671 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12672 	struct intel_crtc_state *crtc_state =
12673 		intel_atomic_get_new_crtc_state(state, crtc);
12674 	const struct intel_cdclk_state *cdclk_state;
12675 
12676 	if (INTEL_GEN(dev_priv) >= 9)
12677 		crtc_state->linetime = skl_linetime_wm(crtc_state);
12678 	else
12679 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
12680 
12681 	if (!hsw_crtc_supports_ips(crtc))
12682 		return 0;
12683 
12684 	cdclk_state = intel_atomic_get_cdclk_state(state);
12685 	if (IS_ERR(cdclk_state))
12686 		return PTR_ERR(cdclk_state);
12687 
12688 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
12689 						       cdclk_state);
12690 
12691 	return 0;
12692 }
12693 
12694 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
12695 				   struct intel_crtc *crtc)
12696 {
12697 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12698 	struct intel_crtc_state *crtc_state =
12699 		intel_atomic_get_new_crtc_state(state, crtc);
12700 	bool mode_changed = needs_modeset(crtc_state);
12701 	int ret;
12702 
12703 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
12704 	    mode_changed && !crtc_state->hw.active)
12705 		crtc_state->update_wm_post = true;
12706 
12707 	if (mode_changed && crtc_state->hw.enable &&
12708 	    dev_priv->display.crtc_compute_clock &&
12709 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
12710 		ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
12711 		if (ret)
12712 			return ret;
12713 	}
12714 
12715 	/*
12716 	 * May need to update pipe gamma enable bits
12717 	 * when C8 planes are getting enabled/disabled.
12718 	 */
12719 	if (c8_planes_changed(crtc_state))
12720 		crtc_state->uapi.color_mgmt_changed = true;
12721 
12722 	if (mode_changed || crtc_state->update_pipe ||
12723 	    crtc_state->uapi.color_mgmt_changed) {
12724 		ret = intel_color_check(crtc_state);
12725 		if (ret)
12726 			return ret;
12727 	}
12728 
12729 	if (dev_priv->display.compute_pipe_wm) {
12730 		ret = dev_priv->display.compute_pipe_wm(crtc_state);
12731 		if (ret) {
12732 			drm_dbg_kms(&dev_priv->drm,
12733 				    "Target pipe watermarks are invalid\n");
12734 			return ret;
12735 		}
12736 	}
12737 
12738 	if (dev_priv->display.compute_intermediate_wm) {
12739 		if (drm_WARN_ON(&dev_priv->drm,
12740 				!dev_priv->display.compute_pipe_wm))
12741 			return 0;
12742 
12743 		/*
12744 		 * Calculate 'intermediate' watermarks that satisfy both the
12745 		 * old state and the new state.  We can program these
12746 		 * immediately.
12747 		 */
12748 		ret = dev_priv->display.compute_intermediate_wm(crtc_state);
12749 		if (ret) {
12750 			drm_dbg_kms(&dev_priv->drm,
12751 				    "No valid intermediate pipe watermarks are possible\n");
12752 			return ret;
12753 		}
12754 	}
12755 
12756 	if (INTEL_GEN(dev_priv) >= 9) {
12757 		if (mode_changed || crtc_state->update_pipe) {
12758 			ret = skl_update_scaler_crtc(crtc_state);
12759 			if (ret)
12760 				return ret;
12761 		}
12762 
12763 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
12764 		if (ret)
12765 			return ret;
12766 	}
12767 
12768 	if (HAS_IPS(dev_priv)) {
12769 		ret = hsw_compute_ips_config(crtc_state);
12770 		if (ret)
12771 			return ret;
12772 	}
12773 
12774 	if (INTEL_GEN(dev_priv) >= 9 ||
12775 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12776 		ret = hsw_compute_linetime_wm(state, crtc);
12777 		if (ret)
12778 			return ret;
12779 
12780 	}
12781 
12782 	return 0;
12783 }
12784 
12785 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12786 {
12787 	struct intel_connector *connector;
12788 	struct drm_connector_list_iter conn_iter;
12789 
12790 	drm_connector_list_iter_begin(dev, &conn_iter);
12791 	for_each_intel_connector_iter(connector, &conn_iter) {
12792 		if (connector->base.state->crtc)
12793 			drm_connector_put(&connector->base);
12794 
12795 		if (connector->base.encoder) {
12796 			connector->base.state->best_encoder =
12797 				connector->base.encoder;
12798 			connector->base.state->crtc =
12799 				connector->base.encoder->crtc;
12800 
12801 			drm_connector_get(&connector->base);
12802 		} else {
12803 			connector->base.state->best_encoder = NULL;
12804 			connector->base.state->crtc = NULL;
12805 		}
12806 	}
12807 	drm_connector_list_iter_end(&conn_iter);
12808 }
12809 
12810 static int
12811 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
12812 		      struct intel_crtc_state *pipe_config)
12813 {
12814 	struct drm_connector *connector = conn_state->connector;
12815 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12816 	const struct drm_display_info *info = &connector->display_info;
12817 	int bpp;
12818 
12819 	switch (conn_state->max_bpc) {
12820 	case 6 ... 7:
12821 		bpp = 6 * 3;
12822 		break;
12823 	case 8 ... 9:
12824 		bpp = 8 * 3;
12825 		break;
12826 	case 10 ... 11:
12827 		bpp = 10 * 3;
12828 		break;
12829 	case 12:
12830 		bpp = 12 * 3;
12831 		break;
12832 	default:
12833 		return -EINVAL;
12834 	}
12835 
12836 	if (bpp < pipe_config->pipe_bpp) {
12837 		drm_dbg_kms(&i915->drm,
12838 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
12839 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
12840 			    connector->base.id, connector->name,
12841 			    bpp, 3 * info->bpc,
12842 			    3 * conn_state->max_requested_bpc,
12843 			    pipe_config->pipe_bpp);
12844 
12845 		pipe_config->pipe_bpp = bpp;
12846 	}
12847 
12848 	return 0;
12849 }
12850 
12851 static int
12852 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
12853 			  struct intel_crtc_state *pipe_config)
12854 {
12855 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12856 	struct drm_atomic_state *state = pipe_config->uapi.state;
12857 	struct drm_connector *connector;
12858 	struct drm_connector_state *connector_state;
12859 	int bpp, i;
12860 
12861 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12862 	    IS_CHERRYVIEW(dev_priv)))
12863 		bpp = 10*3;
12864 	else if (INTEL_GEN(dev_priv) >= 5)
12865 		bpp = 12*3;
12866 	else
12867 		bpp = 8*3;
12868 
12869 	pipe_config->pipe_bpp = bpp;
12870 
12871 	/* Clamp display bpp to connector max bpp */
12872 	for_each_new_connector_in_state(state, connector, connector_state, i) {
12873 		int ret;
12874 
12875 		if (connector_state->crtc != &crtc->base)
12876 			continue;
12877 
12878 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
12879 		if (ret)
12880 			return ret;
12881 	}
12882 
12883 	return 0;
12884 }
12885 
12886 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12887 {
12888 	DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12889 		      "type: 0x%x flags: 0x%x\n",
12890 		      mode->crtc_clock,
12891 		      mode->crtc_hdisplay, mode->crtc_hsync_start,
12892 		      mode->crtc_hsync_end, mode->crtc_htotal,
12893 		      mode->crtc_vdisplay, mode->crtc_vsync_start,
12894 		      mode->crtc_vsync_end, mode->crtc_vtotal,
12895 		      mode->type, mode->flags);
12896 }
12897 
12898 static inline void
12899 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
12900 		      const char *id, unsigned int lane_count,
12901 		      const struct intel_link_m_n *m_n)
12902 {
12903 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12904 
12905 	drm_dbg_kms(&i915->drm,
12906 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12907 		    id, lane_count,
12908 		    m_n->gmch_m, m_n->gmch_n,
12909 		    m_n->link_m, m_n->link_n, m_n->tu);
12910 }
12911 
12912 static void
12913 intel_dump_infoframe(struct drm_i915_private *dev_priv,
12914 		     const union hdmi_infoframe *frame)
12915 {
12916 	if (!drm_debug_enabled(DRM_UT_KMS))
12917 		return;
12918 
12919 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
12920 }
12921 
12922 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
12923 
12924 static const char * const output_type_str[] = {
12925 	OUTPUT_TYPE(UNUSED),
12926 	OUTPUT_TYPE(ANALOG),
12927 	OUTPUT_TYPE(DVO),
12928 	OUTPUT_TYPE(SDVO),
12929 	OUTPUT_TYPE(LVDS),
12930 	OUTPUT_TYPE(TVOUT),
12931 	OUTPUT_TYPE(HDMI),
12932 	OUTPUT_TYPE(DP),
12933 	OUTPUT_TYPE(EDP),
12934 	OUTPUT_TYPE(DSI),
12935 	OUTPUT_TYPE(DDI),
12936 	OUTPUT_TYPE(DP_MST),
12937 };
12938 
12939 #undef OUTPUT_TYPE
12940 
12941 static void snprintf_output_types(char *buf, size_t len,
12942 				  unsigned int output_types)
12943 {
12944 	char *str = buf;
12945 	int i;
12946 
12947 	str[0] = '\0';
12948 
12949 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
12950 		int r;
12951 
12952 		if ((output_types & BIT(i)) == 0)
12953 			continue;
12954 
12955 		r = snprintf(str, len, "%s%s",
12956 			     str != buf ? "," : "", output_type_str[i]);
12957 		if (r >= len)
12958 			break;
12959 		str += r;
12960 		len -= r;
12961 
12962 		output_types &= ~BIT(i);
12963 	}
12964 
12965 	WARN_ON_ONCE(output_types != 0);
12966 }
12967 
12968 static const char * const output_format_str[] = {
12969 	[INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
12970 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
12971 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
12972 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
12973 };
12974 
12975 static const char *output_formats(enum intel_output_format format)
12976 {
12977 	if (format >= ARRAY_SIZE(output_format_str))
12978 		format = INTEL_OUTPUT_FORMAT_INVALID;
12979 	return output_format_str[format];
12980 }
12981 
12982 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
12983 {
12984 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12985 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
12986 	const struct drm_framebuffer *fb = plane_state->hw.fb;
12987 	struct drm_format_name_buf format_name;
12988 
12989 	if (!fb) {
12990 		drm_dbg_kms(&i915->drm,
12991 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
12992 			    plane->base.base.id, plane->base.name,
12993 			    yesno(plane_state->uapi.visible));
12994 		return;
12995 	}
12996 
12997 	drm_dbg_kms(&i915->drm,
12998 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s, visible: %s\n",
12999 		    plane->base.base.id, plane->base.name,
13000 		    fb->base.id, fb->width, fb->height,
13001 		    drm_get_format_name(fb->format->format, &format_name),
13002 		    yesno(plane_state->uapi.visible));
13003 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13004 		    plane_state->hw.rotation, plane_state->scaler_id);
13005 	if (plane_state->uapi.visible)
13006 		drm_dbg_kms(&i915->drm,
13007 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13008 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
13009 			    DRM_RECT_ARG(&plane_state->uapi.dst));
13010 }
13011 
13012 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
13013 				   struct intel_atomic_state *state,
13014 				   const char *context)
13015 {
13016 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13017 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13018 	const struct intel_plane_state *plane_state;
13019 	struct intel_plane *plane;
13020 	char buf[64];
13021 	int i;
13022 
13023 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13024 		    crtc->base.base.id, crtc->base.name,
13025 		    yesno(pipe_config->hw.enable), context);
13026 
13027 	if (!pipe_config->hw.enable)
13028 		goto dump_planes;
13029 
13030 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
13031 	drm_dbg_kms(&dev_priv->drm,
13032 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
13033 		    yesno(pipe_config->hw.active),
13034 		    buf, pipe_config->output_types,
13035 		    output_formats(pipe_config->output_format));
13036 
13037 	drm_dbg_kms(&dev_priv->drm,
13038 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13039 		    transcoder_name(pipe_config->cpu_transcoder),
13040 		    pipe_config->pipe_bpp, pipe_config->dither);
13041 
13042 	if (pipe_config->has_pch_encoder)
13043 		intel_dump_m_n_config(pipe_config, "fdi",
13044 				      pipe_config->fdi_lanes,
13045 				      &pipe_config->fdi_m_n);
13046 
13047 	if (intel_crtc_has_dp_encoder(pipe_config)) {
13048 		intel_dump_m_n_config(pipe_config, "dp m_n",
13049 				pipe_config->lane_count, &pipe_config->dp_m_n);
13050 		if (pipe_config->has_drrs)
13051 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
13052 					      pipe_config->lane_count,
13053 					      &pipe_config->dp_m2_n2);
13054 	}
13055 
13056 	drm_dbg_kms(&dev_priv->drm,
13057 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13058 		    pipe_config->has_audio, pipe_config->has_infoframe,
13059 		    pipe_config->infoframes.enable);
13060 
13061 	if (pipe_config->infoframes.enable &
13062 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
13063 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13064 			    pipe_config->infoframes.gcp);
13065 	if (pipe_config->infoframes.enable &
13066 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13067 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13068 	if (pipe_config->infoframes.enable &
13069 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13070 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13071 	if (pipe_config->infoframes.enable &
13072 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13073 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
13074 
13075 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
13076 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
13077 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
13078 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
13079 	intel_dump_crtc_timings(&pipe_config->hw.adjusted_mode);
13080 	drm_dbg_kms(&dev_priv->drm,
13081 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13082 		    pipe_config->port_clock,
13083 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13084 		    pipe_config->pixel_rate);
13085 
13086 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13087 		    pipe_config->linetime, pipe_config->ips_linetime);
13088 
13089 	if (INTEL_GEN(dev_priv) >= 9)
13090 		drm_dbg_kms(&dev_priv->drm,
13091 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13092 			    crtc->num_scalers,
13093 			    pipe_config->scaler_state.scaler_users,
13094 			    pipe_config->scaler_state.scaler_id);
13095 
13096 	if (HAS_GMCH(dev_priv))
13097 		drm_dbg_kms(&dev_priv->drm,
13098 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13099 			    pipe_config->gmch_pfit.control,
13100 			    pipe_config->gmch_pfit.pgm_ratios,
13101 			    pipe_config->gmch_pfit.lvds_border_bits);
13102 	else
13103 		drm_dbg_kms(&dev_priv->drm,
13104 			    "pch pfit: pos: 0x%08x, size: 0x%08x, %s, force thru: %s\n",
13105 			    pipe_config->pch_pfit.pos,
13106 			    pipe_config->pch_pfit.size,
13107 			    enableddisabled(pipe_config->pch_pfit.enabled),
13108 			    yesno(pipe_config->pch_pfit.force_thru));
13109 
13110 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13111 		    pipe_config->ips_enabled, pipe_config->double_wide);
13112 
13113 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
13114 
13115 	if (IS_CHERRYVIEW(dev_priv))
13116 		drm_dbg_kms(&dev_priv->drm,
13117 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13118 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
13119 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13120 	else
13121 		drm_dbg_kms(&dev_priv->drm,
13122 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13123 			    pipe_config->csc_mode, pipe_config->gamma_mode,
13124 			    pipe_config->gamma_enable, pipe_config->csc_enable);
13125 
13126 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13127 		    transcoder_name(pipe_config->mst_master_transcoder));
13128 
13129 dump_planes:
13130 	if (!state)
13131 		return;
13132 
13133 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13134 		if (plane->pipe == crtc->pipe)
13135 			intel_dump_plane_state(plane_state);
13136 	}
13137 }
13138 
13139 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
13140 {
13141 	struct drm_device *dev = state->base.dev;
13142 	struct drm_connector *connector;
13143 	struct drm_connector_list_iter conn_iter;
13144 	unsigned int used_ports = 0;
13145 	unsigned int used_mst_ports = 0;
13146 	bool ret = true;
13147 
13148 	/*
13149 	 * We're going to peek into connector->state,
13150 	 * hence connection_mutex must be held.
13151 	 */
13152 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13153 
13154 	/*
13155 	 * Walk the connector list instead of the encoder
13156 	 * list to detect the problem on ddi platforms
13157 	 * where there's just one encoder per digital port.
13158 	 */
13159 	drm_connector_list_iter_begin(dev, &conn_iter);
13160 	drm_for_each_connector_iter(connector, &conn_iter) {
13161 		struct drm_connector_state *connector_state;
13162 		struct intel_encoder *encoder;
13163 
13164 		connector_state =
13165 			drm_atomic_get_new_connector_state(&state->base,
13166 							   connector);
13167 		if (!connector_state)
13168 			connector_state = connector->state;
13169 
13170 		if (!connector_state->best_encoder)
13171 			continue;
13172 
13173 		encoder = to_intel_encoder(connector_state->best_encoder);
13174 
13175 		drm_WARN_ON(dev, !connector_state->crtc);
13176 
13177 		switch (encoder->type) {
13178 		case INTEL_OUTPUT_DDI:
13179 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
13180 				break;
13181 			/* else, fall through */
13182 		case INTEL_OUTPUT_DP:
13183 		case INTEL_OUTPUT_HDMI:
13184 		case INTEL_OUTPUT_EDP:
13185 			/* the same port mustn't appear more than once */
13186 			if (used_ports & BIT(encoder->port))
13187 				ret = false;
13188 
13189 			used_ports |= BIT(encoder->port);
13190 			break;
13191 		case INTEL_OUTPUT_DP_MST:
13192 			used_mst_ports |=
13193 				1 << encoder->port;
13194 			break;
13195 		default:
13196 			break;
13197 		}
13198 	}
13199 	drm_connector_list_iter_end(&conn_iter);
13200 
13201 	/* can't mix MST and SST/HDMI on the same port */
13202 	if (used_ports & used_mst_ports)
13203 		return false;
13204 
13205 	return ret;
13206 }
13207 
13208 static void
13209 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_crtc_state *crtc_state)
13210 {
13211 	intel_crtc_copy_color_blobs(crtc_state);
13212 }
13213 
13214 static void
13215 intel_crtc_copy_uapi_to_hw_state(struct intel_crtc_state *crtc_state)
13216 {
13217 	crtc_state->hw.enable = crtc_state->uapi.enable;
13218 	crtc_state->hw.active = crtc_state->uapi.active;
13219 	crtc_state->hw.mode = crtc_state->uapi.mode;
13220 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
13221 	intel_crtc_copy_uapi_to_hw_state_nomodeset(crtc_state);
13222 }
13223 
13224 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13225 {
13226 	crtc_state->uapi.enable = crtc_state->hw.enable;
13227 	crtc_state->uapi.active = crtc_state->hw.active;
13228 	WARN_ON(drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
13229 
13230 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
13231 
13232 	/* copy color blobs to uapi */
13233 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13234 				  crtc_state->hw.degamma_lut);
13235 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13236 				  crtc_state->hw.gamma_lut);
13237 	drm_property_replace_blob(&crtc_state->uapi.ctm,
13238 				  crtc_state->hw.ctm);
13239 }
13240 
13241 static int
13242 intel_crtc_prepare_cleared_state(struct intel_crtc_state *crtc_state)
13243 {
13244 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13245 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13246 	struct intel_crtc_state *saved_state;
13247 
13248 	saved_state = intel_crtc_state_alloc(crtc);
13249 	if (!saved_state)
13250 		return -ENOMEM;
13251 
13252 	/* free the old crtc_state->hw members */
13253 	intel_crtc_free_hw_state(crtc_state);
13254 
13255 	/* FIXME: before the switch to atomic started, a new pipe_config was
13256 	 * kzalloc'd. Code that depends on any field being zero should be
13257 	 * fixed, so that the crtc_state can be safely duplicated. For now,
13258 	 * only fields that are know to not cause problems are preserved. */
13259 
13260 	saved_state->uapi = crtc_state->uapi;
13261 	saved_state->scaler_state = crtc_state->scaler_state;
13262 	saved_state->shared_dpll = crtc_state->shared_dpll;
13263 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13264 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13265 	       sizeof(saved_state->icl_port_dplls));
13266 	saved_state->crc_enabled = crtc_state->crc_enabled;
13267 	if (IS_G4X(dev_priv) ||
13268 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13269 		saved_state->wm = crtc_state->wm;
13270 
13271 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13272 	kfree(saved_state);
13273 
13274 	intel_crtc_copy_uapi_to_hw_state(crtc_state);
13275 
13276 	return 0;
13277 }
13278 
13279 static int
13280 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
13281 {
13282 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
13283 	struct drm_atomic_state *state = pipe_config->uapi.state;
13284 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13285 	struct drm_connector *connector;
13286 	struct drm_connector_state *connector_state;
13287 	int base_bpp, ret, i;
13288 	bool retry = true;
13289 
13290 	pipe_config->cpu_transcoder =
13291 		(enum transcoder) to_intel_crtc(crtc)->pipe;
13292 
13293 	/*
13294 	 * Sanitize sync polarity flags based on requested ones. If neither
13295 	 * positive or negative polarity is requested, treat this as meaning
13296 	 * negative polarity.
13297 	 */
13298 	if (!(pipe_config->hw.adjusted_mode.flags &
13299 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
13300 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
13301 
13302 	if (!(pipe_config->hw.adjusted_mode.flags &
13303 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
13304 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
13305 
13306 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13307 					pipe_config);
13308 	if (ret)
13309 		return ret;
13310 
13311 	base_bpp = pipe_config->pipe_bpp;
13312 
13313 	/*
13314 	 * Determine the real pipe dimensions. Note that stereo modes can
13315 	 * increase the actual pipe size due to the frame doubling and
13316 	 * insertion of additional space for blanks between the frame. This
13317 	 * is stored in the crtc timings. We use the requested mode to do this
13318 	 * computation to clearly distinguish it from the adjusted mode, which
13319 	 * can be changed by the connectors in the below retry loop.
13320 	 */
13321 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
13322 			       &pipe_config->pipe_src_w,
13323 			       &pipe_config->pipe_src_h);
13324 
13325 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13326 		struct intel_encoder *encoder =
13327 			to_intel_encoder(connector_state->best_encoder);
13328 
13329 		if (connector_state->crtc != crtc)
13330 			continue;
13331 
13332 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13333 			drm_dbg_kms(&i915->drm,
13334 				    "rejecting invalid cloning configuration\n");
13335 			return -EINVAL;
13336 		}
13337 
13338 		/*
13339 		 * Determine output_types before calling the .compute_config()
13340 		 * hooks so that the hooks can use this information safely.
13341 		 */
13342 		if (encoder->compute_output_type)
13343 			pipe_config->output_types |=
13344 				BIT(encoder->compute_output_type(encoder, pipe_config,
13345 								 connector_state));
13346 		else
13347 			pipe_config->output_types |= BIT(encoder->type);
13348 	}
13349 
13350 encoder_retry:
13351 	/* Ensure the port clock defaults are reset when retrying. */
13352 	pipe_config->port_clock = 0;
13353 	pipe_config->pixel_multiplier = 1;
13354 
13355 	/* Fill in default crtc timings, allow encoders to overwrite them. */
13356 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
13357 			      CRTC_STEREO_DOUBLE);
13358 
13359 	/* Pass our mode to the connectors and the CRTC to give them a chance to
13360 	 * adjust it according to limitations or connector properties, and also
13361 	 * a chance to reject the mode entirely.
13362 	 */
13363 	for_each_new_connector_in_state(state, connector, connector_state, i) {
13364 		struct intel_encoder *encoder =
13365 			to_intel_encoder(connector_state->best_encoder);
13366 
13367 		if (connector_state->crtc != crtc)
13368 			continue;
13369 
13370 		ret = encoder->compute_config(encoder, pipe_config,
13371 					      connector_state);
13372 		if (ret < 0) {
13373 			if (ret != -EDEADLK)
13374 				drm_dbg_kms(&i915->drm,
13375 					    "Encoder config failure: %d\n",
13376 					    ret);
13377 			return ret;
13378 		}
13379 	}
13380 
13381 	/* Set default port clock if not overwritten by the encoder. Needs to be
13382 	 * done afterwards in case the encoder adjusts the mode. */
13383 	if (!pipe_config->port_clock)
13384 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
13385 			* pipe_config->pixel_multiplier;
13386 
13387 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
13388 	if (ret == -EDEADLK)
13389 		return ret;
13390 	if (ret < 0) {
13391 		drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
13392 		return ret;
13393 	}
13394 
13395 	if (ret == RETRY) {
13396 		if (drm_WARN(&i915->drm, !retry,
13397 			     "loop in pipe configuration computation\n"))
13398 			return -EINVAL;
13399 
13400 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
13401 		retry = false;
13402 		goto encoder_retry;
13403 	}
13404 
13405 	/* Dithering seems to not pass-through bits correctly when it should, so
13406 	 * only enable it on 6bpc panels and when its not a compliance
13407 	 * test requesting 6bpc video pattern.
13408 	 */
13409 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13410 		!pipe_config->dither_force_disable;
13411 	drm_dbg_kms(&i915->drm,
13412 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13413 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
13414 
13415 	/*
13416 	 * Make drm_calc_timestamping_constants in
13417 	 * drm_atomic_helper_update_legacy_modeset_state() happy
13418 	 */
13419 	pipe_config->uapi.adjusted_mode = pipe_config->hw.adjusted_mode;
13420 
13421 	return 0;
13422 }
13423 
13424 static int
13425 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13426 {
13427 	struct intel_atomic_state *state =
13428 		to_intel_atomic_state(crtc_state->uapi.state);
13429 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13430 	struct drm_connector_state *conn_state;
13431 	struct drm_connector *connector;
13432 	int i;
13433 
13434 	for_each_new_connector_in_state(&state->base, connector,
13435 					conn_state, i) {
13436 		struct intel_encoder *encoder =
13437 			to_intel_encoder(conn_state->best_encoder);
13438 		int ret;
13439 
13440 		if (conn_state->crtc != &crtc->base ||
13441 		    !encoder->compute_config_late)
13442 			continue;
13443 
13444 		ret = encoder->compute_config_late(encoder, crtc_state,
13445 						   conn_state);
13446 		if (ret)
13447 			return ret;
13448 	}
13449 
13450 	return 0;
13451 }
13452 
13453 bool intel_fuzzy_clock_check(int clock1, int clock2)
13454 {
13455 	int diff;
13456 
13457 	if (clock1 == clock2)
13458 		return true;
13459 
13460 	if (!clock1 || !clock2)
13461 		return false;
13462 
13463 	diff = abs(clock1 - clock2);
13464 
13465 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13466 		return true;
13467 
13468 	return false;
13469 }
13470 
13471 static bool
13472 intel_compare_m_n(unsigned int m, unsigned int n,
13473 		  unsigned int m2, unsigned int n2,
13474 		  bool exact)
13475 {
13476 	if (m == m2 && n == n2)
13477 		return true;
13478 
13479 	if (exact || !m || !n || !m2 || !n2)
13480 		return false;
13481 
13482 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13483 
13484 	if (n > n2) {
13485 		while (n > n2) {
13486 			m2 <<= 1;
13487 			n2 <<= 1;
13488 		}
13489 	} else if (n < n2) {
13490 		while (n < n2) {
13491 			m <<= 1;
13492 			n <<= 1;
13493 		}
13494 	}
13495 
13496 	if (n != n2)
13497 		return false;
13498 
13499 	return intel_fuzzy_clock_check(m, m2);
13500 }
13501 
13502 static bool
13503 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13504 		       const struct intel_link_m_n *m2_n2,
13505 		       bool exact)
13506 {
13507 	return m_n->tu == m2_n2->tu &&
13508 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13509 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13510 		intel_compare_m_n(m_n->link_m, m_n->link_n,
13511 				  m2_n2->link_m, m2_n2->link_n, exact);
13512 }
13513 
13514 static bool
13515 intel_compare_infoframe(const union hdmi_infoframe *a,
13516 			const union hdmi_infoframe *b)
13517 {
13518 	return memcmp(a, b, sizeof(*a)) == 0;
13519 }
13520 
13521 static void
13522 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13523 			       bool fastset, const char *name,
13524 			       const union hdmi_infoframe *a,
13525 			       const union hdmi_infoframe *b)
13526 {
13527 	if (fastset) {
13528 		if (!drm_debug_enabled(DRM_UT_KMS))
13529 			return;
13530 
13531 		drm_dbg_kms(&dev_priv->drm,
13532 			    "fastset mismatch in %s infoframe\n", name);
13533 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
13534 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
13535 		drm_dbg_kms(&dev_priv->drm, "found:\n");
13536 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13537 	} else {
13538 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13539 		drm_err(&dev_priv->drm, "expected:\n");
13540 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
13541 		drm_err(&dev_priv->drm, "found:\n");
13542 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13543 	}
13544 }
13545 
13546 static void __printf(4, 5)
13547 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13548 		     const char *name, const char *format, ...)
13549 {
13550 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
13551 	struct va_format vaf;
13552 	va_list args;
13553 
13554 	va_start(args, format);
13555 	vaf.fmt = format;
13556 	vaf.va = &args;
13557 
13558 	if (fastset)
13559 		drm_dbg_kms(&i915->drm,
13560 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
13561 			    crtc->base.base.id, crtc->base.name, name, &vaf);
13562 	else
13563 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
13564 			crtc->base.base.id, crtc->base.name, name, &vaf);
13565 
13566 	va_end(args);
13567 }
13568 
13569 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
13570 {
13571 	if (i915_modparams.fastboot != -1)
13572 		return i915_modparams.fastboot;
13573 
13574 	/* Enable fastboot by default on Skylake and newer */
13575 	if (INTEL_GEN(dev_priv) >= 9)
13576 		return true;
13577 
13578 	/* Enable fastboot by default on VLV and CHV */
13579 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13580 		return true;
13581 
13582 	/* Disabled by default on all others */
13583 	return false;
13584 }
13585 
13586 static bool
13587 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
13588 			  const struct intel_crtc_state *pipe_config,
13589 			  bool fastset)
13590 {
13591 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
13592 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13593 	bool ret = true;
13594 	u32 bp_gamma = 0;
13595 	bool fixup_inherited = fastset &&
13596 		(current_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
13597 		!(pipe_config->hw.mode.private_flags & I915_MODE_FLAG_INHERITED);
13598 
13599 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
13600 		drm_dbg_kms(&dev_priv->drm,
13601 			    "initial modeset and fastboot not set\n");
13602 		ret = false;
13603 	}
13604 
13605 #define PIPE_CONF_CHECK_X(name) do { \
13606 	if (current_config->name != pipe_config->name) { \
13607 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13608 				     "(expected 0x%08x, found 0x%08x)", \
13609 				     current_config->name, \
13610 				     pipe_config->name); \
13611 		ret = false; \
13612 	} \
13613 } while (0)
13614 
13615 #define PIPE_CONF_CHECK_I(name) do { \
13616 	if (current_config->name != pipe_config->name) { \
13617 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13618 				     "(expected %i, found %i)", \
13619 				     current_config->name, \
13620 				     pipe_config->name); \
13621 		ret = false; \
13622 	} \
13623 } while (0)
13624 
13625 #define PIPE_CONF_CHECK_BOOL(name) do { \
13626 	if (current_config->name != pipe_config->name) { \
13627 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
13628 				     "(expected %s, found %s)", \
13629 				     yesno(current_config->name), \
13630 				     yesno(pipe_config->name)); \
13631 		ret = false; \
13632 	} \
13633 } while (0)
13634 
13635 /*
13636  * Checks state where we only read out the enabling, but not the entire
13637  * state itself (like full infoframes or ELD for audio). These states
13638  * require a full modeset on bootup to fix up.
13639  */
13640 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
13641 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
13642 		PIPE_CONF_CHECK_BOOL(name); \
13643 	} else { \
13644 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13645 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
13646 				     yesno(current_config->name), \
13647 				     yesno(pipe_config->name)); \
13648 		ret = false; \
13649 	} \
13650 } while (0)
13651 
13652 #define PIPE_CONF_CHECK_P(name) do { \
13653 	if (current_config->name != pipe_config->name) { \
13654 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13655 				     "(expected %p, found %p)", \
13656 				     current_config->name, \
13657 				     pipe_config->name); \
13658 		ret = false; \
13659 	} \
13660 } while (0)
13661 
13662 #define PIPE_CONF_CHECK_M_N(name) do { \
13663 	if (!intel_compare_link_m_n(&current_config->name, \
13664 				    &pipe_config->name,\
13665 				    !fastset)) { \
13666 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13667 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13668 				     "found tu %i, gmch %i/%i link %i/%i)", \
13669 				     current_config->name.tu, \
13670 				     current_config->name.gmch_m, \
13671 				     current_config->name.gmch_n, \
13672 				     current_config->name.link_m, \
13673 				     current_config->name.link_n, \
13674 				     pipe_config->name.tu, \
13675 				     pipe_config->name.gmch_m, \
13676 				     pipe_config->name.gmch_n, \
13677 				     pipe_config->name.link_m, \
13678 				     pipe_config->name.link_n); \
13679 		ret = false; \
13680 	} \
13681 } while (0)
13682 
13683 /* This is required for BDW+ where there is only one set of registers for
13684  * switching between high and low RR.
13685  * This macro can be used whenever a comparison has to be made between one
13686  * hw state and multiple sw state variables.
13687  */
13688 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
13689 	if (!intel_compare_link_m_n(&current_config->name, \
13690 				    &pipe_config->name, !fastset) && \
13691 	    !intel_compare_link_m_n(&current_config->alt_name, \
13692 				    &pipe_config->name, !fastset)) { \
13693 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13694 				     "(expected tu %i gmch %i/%i link %i/%i, " \
13695 				     "or tu %i gmch %i/%i link %i/%i, " \
13696 				     "found tu %i, gmch %i/%i link %i/%i)", \
13697 				     current_config->name.tu, \
13698 				     current_config->name.gmch_m, \
13699 				     current_config->name.gmch_n, \
13700 				     current_config->name.link_m, \
13701 				     current_config->name.link_n, \
13702 				     current_config->alt_name.tu, \
13703 				     current_config->alt_name.gmch_m, \
13704 				     current_config->alt_name.gmch_n, \
13705 				     current_config->alt_name.link_m, \
13706 				     current_config->alt_name.link_n, \
13707 				     pipe_config->name.tu, \
13708 				     pipe_config->name.gmch_m, \
13709 				     pipe_config->name.gmch_n, \
13710 				     pipe_config->name.link_m, \
13711 				     pipe_config->name.link_n); \
13712 		ret = false; \
13713 	} \
13714 } while (0)
13715 
13716 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
13717 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
13718 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13719 				     "(%x) (expected %i, found %i)", \
13720 				     (mask), \
13721 				     current_config->name & (mask), \
13722 				     pipe_config->name & (mask)); \
13723 		ret = false; \
13724 	} \
13725 } while (0)
13726 
13727 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
13728 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
13729 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
13730 				     "(expected %i, found %i)", \
13731 				     current_config->name, \
13732 				     pipe_config->name); \
13733 		ret = false; \
13734 	} \
13735 } while (0)
13736 
13737 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
13738 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
13739 				     &pipe_config->infoframes.name)) { \
13740 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
13741 					       &current_config->infoframes.name, \
13742 					       &pipe_config->infoframes.name); \
13743 		ret = false; \
13744 	} \
13745 } while (0)
13746 
13747 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
13748 	if (current_config->name1 != pipe_config->name1) { \
13749 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
13750 				"(expected %i, found %i, won't compare lut values)", \
13751 				current_config->name1, \
13752 				pipe_config->name1); \
13753 		ret = false;\
13754 	} else { \
13755 		if (!intel_color_lut_equal(current_config->name2, \
13756 					pipe_config->name2, pipe_config->name1, \
13757 					bit_precision)) { \
13758 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
13759 					"hw_state doesn't match sw_state"); \
13760 			ret = false; \
13761 		} \
13762 	} \
13763 } while (0)
13764 
13765 #define PIPE_CONF_QUIRK(quirk) \
13766 	((current_config->quirks | pipe_config->quirks) & (quirk))
13767 
13768 	PIPE_CONF_CHECK_I(cpu_transcoder);
13769 
13770 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
13771 	PIPE_CONF_CHECK_I(fdi_lanes);
13772 	PIPE_CONF_CHECK_M_N(fdi_m_n);
13773 
13774 	PIPE_CONF_CHECK_I(lane_count);
13775 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
13776 
13777 	if (INTEL_GEN(dev_priv) < 8) {
13778 		PIPE_CONF_CHECK_M_N(dp_m_n);
13779 
13780 		if (current_config->has_drrs)
13781 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
13782 	} else
13783 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
13784 
13785 	PIPE_CONF_CHECK_X(output_types);
13786 
13787 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
13788 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
13789 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
13790 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
13791 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
13792 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
13793 
13794 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
13795 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
13796 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
13797 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
13798 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
13799 	PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
13800 
13801 	PIPE_CONF_CHECK_I(pixel_multiplier);
13802 	PIPE_CONF_CHECK_I(output_format);
13803 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
13804 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
13805 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13806 		PIPE_CONF_CHECK_BOOL(limited_color_range);
13807 
13808 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
13809 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
13810 	PIPE_CONF_CHECK_BOOL(has_infoframe);
13811 	PIPE_CONF_CHECK_BOOL(fec_enable);
13812 
13813 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
13814 
13815 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13816 			      DRM_MODE_FLAG_INTERLACE);
13817 
13818 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
13819 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13820 				      DRM_MODE_FLAG_PHSYNC);
13821 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13822 				      DRM_MODE_FLAG_NHSYNC);
13823 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13824 				      DRM_MODE_FLAG_PVSYNC);
13825 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
13826 				      DRM_MODE_FLAG_NVSYNC);
13827 	}
13828 
13829 	PIPE_CONF_CHECK_X(gmch_pfit.control);
13830 	/* pfit ratios are autocomputed by the hw on gen4+ */
13831 	if (INTEL_GEN(dev_priv) < 4)
13832 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
13833 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
13834 
13835 	/*
13836 	 * Changing the EDP transcoder input mux
13837 	 * (A_ONOFF vs. A_ON) requires a full modeset.
13838 	 */
13839 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
13840 
13841 	if (!fastset) {
13842 		PIPE_CONF_CHECK_I(pipe_src_w);
13843 		PIPE_CONF_CHECK_I(pipe_src_h);
13844 
13845 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
13846 		if (current_config->pch_pfit.enabled) {
13847 			PIPE_CONF_CHECK_X(pch_pfit.pos);
13848 			PIPE_CONF_CHECK_X(pch_pfit.size);
13849 		}
13850 
13851 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13852 		PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
13853 
13854 		PIPE_CONF_CHECK_X(gamma_mode);
13855 		if (IS_CHERRYVIEW(dev_priv))
13856 			PIPE_CONF_CHECK_X(cgm_mode);
13857 		else
13858 			PIPE_CONF_CHECK_X(csc_mode);
13859 		PIPE_CONF_CHECK_BOOL(gamma_enable);
13860 		PIPE_CONF_CHECK_BOOL(csc_enable);
13861 
13862 		PIPE_CONF_CHECK_I(linetime);
13863 		PIPE_CONF_CHECK_I(ips_linetime);
13864 
13865 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
13866 		if (bp_gamma)
13867 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
13868 	}
13869 
13870 	PIPE_CONF_CHECK_BOOL(double_wide);
13871 
13872 	PIPE_CONF_CHECK_P(shared_dpll);
13873 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
13874 	PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
13875 	PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13876 	PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
13877 	PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
13878 	PIPE_CONF_CHECK_X(dpll_hw_state.spll);
13879 	PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13880 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13881 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
13882 	PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
13883 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
13884 	PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
13885 	PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
13886 	PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
13887 	PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
13888 	PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
13889 	PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
13890 	PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
13891 	PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
13892 	PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
13893 	PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
13894 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
13895 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
13896 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
13897 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
13898 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
13899 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
13900 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
13901 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
13902 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
13903 	PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
13904 
13905 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13906 	PIPE_CONF_CHECK_X(dsi_pll.div);
13907 
13908 	if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
13909 		PIPE_CONF_CHECK_I(pipe_bpp);
13910 
13911 	PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
13912 	PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
13913 
13914 	PIPE_CONF_CHECK_I(min_voltage_level);
13915 
13916 	PIPE_CONF_CHECK_X(infoframes.enable);
13917 	PIPE_CONF_CHECK_X(infoframes.gcp);
13918 	PIPE_CONF_CHECK_INFOFRAME(avi);
13919 	PIPE_CONF_CHECK_INFOFRAME(spd);
13920 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
13921 	PIPE_CONF_CHECK_INFOFRAME(drm);
13922 
13923 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
13924 	PIPE_CONF_CHECK_I(master_transcoder);
13925 
13926 	PIPE_CONF_CHECK_I(dsc.compression_enable);
13927 	PIPE_CONF_CHECK_I(dsc.dsc_split);
13928 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
13929 
13930 	PIPE_CONF_CHECK_I(mst_master_transcoder);
13931 
13932 #undef PIPE_CONF_CHECK_X
13933 #undef PIPE_CONF_CHECK_I
13934 #undef PIPE_CONF_CHECK_BOOL
13935 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
13936 #undef PIPE_CONF_CHECK_P
13937 #undef PIPE_CONF_CHECK_FLAGS
13938 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
13939 #undef PIPE_CONF_CHECK_COLOR_LUT
13940 #undef PIPE_CONF_QUIRK
13941 
13942 	return ret;
13943 }
13944 
13945 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13946 					   const struct intel_crtc_state *pipe_config)
13947 {
13948 	if (pipe_config->has_pch_encoder) {
13949 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
13950 							    &pipe_config->fdi_m_n);
13951 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
13952 
13953 		/*
13954 		 * FDI already provided one idea for the dotclock.
13955 		 * Yell if the encoder disagrees.
13956 		 */
13957 		drm_WARN(&dev_priv->drm,
13958 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13959 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13960 			 fdi_dotclock, dotclock);
13961 	}
13962 }
13963 
13964 static void verify_wm_state(struct intel_crtc *crtc,
13965 			    struct intel_crtc_state *new_crtc_state)
13966 {
13967 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13968 	struct skl_hw_state {
13969 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
13970 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
13971 		struct skl_pipe_wm wm;
13972 	} *hw;
13973 	struct skl_pipe_wm *sw_wm;
13974 	struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
13975 	u8 hw_enabled_slices;
13976 	const enum pipe pipe = crtc->pipe;
13977 	int plane, level, max_level = ilk_wm_max_level(dev_priv);
13978 
13979 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
13980 		return;
13981 
13982 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
13983 	if (!hw)
13984 		return;
13985 
13986 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
13987 	sw_wm = &new_crtc_state->wm.skl.optimal;
13988 
13989 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
13990 
13991 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
13992 
13993 	if (INTEL_GEN(dev_priv) >= 11 &&
13994 	    hw_enabled_slices != dev_priv->enabled_dbuf_slices_mask)
13995 		drm_err(&dev_priv->drm,
13996 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
13997 			dev_priv->enabled_dbuf_slices_mask,
13998 			hw_enabled_slices);
13999 
14000 	/* planes */
14001 	for_each_universal_plane(dev_priv, pipe, plane) {
14002 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14003 
14004 		hw_plane_wm = &hw->wm.planes[plane];
14005 		sw_plane_wm = &sw_wm->planes[plane];
14006 
14007 		/* Watermarks */
14008 		for (level = 0; level <= max_level; level++) {
14009 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14010 						&sw_plane_wm->wm[level]))
14011 				continue;
14012 
14013 			drm_err(&dev_priv->drm,
14014 				"mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14015 				pipe_name(pipe), plane + 1, level,
14016 				sw_plane_wm->wm[level].plane_en,
14017 				sw_plane_wm->wm[level].plane_res_b,
14018 				sw_plane_wm->wm[level].plane_res_l,
14019 				hw_plane_wm->wm[level].plane_en,
14020 				hw_plane_wm->wm[level].plane_res_b,
14021 				hw_plane_wm->wm[level].plane_res_l);
14022 		}
14023 
14024 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14025 					 &sw_plane_wm->trans_wm)) {
14026 			drm_err(&dev_priv->drm,
14027 				"mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14028 				pipe_name(pipe), plane + 1,
14029 				sw_plane_wm->trans_wm.plane_en,
14030 				sw_plane_wm->trans_wm.plane_res_b,
14031 				sw_plane_wm->trans_wm.plane_res_l,
14032 				hw_plane_wm->trans_wm.plane_en,
14033 				hw_plane_wm->trans_wm.plane_res_b,
14034 				hw_plane_wm->trans_wm.plane_res_l);
14035 		}
14036 
14037 		/* DDB */
14038 		hw_ddb_entry = &hw->ddb_y[plane];
14039 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
14040 
14041 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14042 			drm_err(&dev_priv->drm,
14043 				"mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14044 				pipe_name(pipe), plane + 1,
14045 				sw_ddb_entry->start, sw_ddb_entry->end,
14046 				hw_ddb_entry->start, hw_ddb_entry->end);
14047 		}
14048 	}
14049 
14050 	/*
14051 	 * cursor
14052 	 * If the cursor plane isn't active, we may not have updated it's ddb
14053 	 * allocation. In that case since the ddb allocation will be updated
14054 	 * once the plane becomes visible, we can skip this check
14055 	 */
14056 	if (1) {
14057 		struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14058 
14059 		hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
14060 		sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14061 
14062 		/* Watermarks */
14063 		for (level = 0; level <= max_level; level++) {
14064 			if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14065 						&sw_plane_wm->wm[level]))
14066 				continue;
14067 
14068 			drm_err(&dev_priv->drm,
14069 				"mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14070 				pipe_name(pipe), level,
14071 				sw_plane_wm->wm[level].plane_en,
14072 				sw_plane_wm->wm[level].plane_res_b,
14073 				sw_plane_wm->wm[level].plane_res_l,
14074 				hw_plane_wm->wm[level].plane_en,
14075 				hw_plane_wm->wm[level].plane_res_b,
14076 				hw_plane_wm->wm[level].plane_res_l);
14077 		}
14078 
14079 		if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14080 					 &sw_plane_wm->trans_wm)) {
14081 			drm_err(&dev_priv->drm,
14082 				"mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14083 				pipe_name(pipe),
14084 				sw_plane_wm->trans_wm.plane_en,
14085 				sw_plane_wm->trans_wm.plane_res_b,
14086 				sw_plane_wm->trans_wm.plane_res_l,
14087 				hw_plane_wm->trans_wm.plane_en,
14088 				hw_plane_wm->trans_wm.plane_res_b,
14089 				hw_plane_wm->trans_wm.plane_res_l);
14090 		}
14091 
14092 		/* DDB */
14093 		hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
14094 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
14095 
14096 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14097 			drm_err(&dev_priv->drm,
14098 				"mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14099 				pipe_name(pipe),
14100 				sw_ddb_entry->start, sw_ddb_entry->end,
14101 				hw_ddb_entry->start, hw_ddb_entry->end);
14102 		}
14103 	}
14104 
14105 	kfree(hw);
14106 }
14107 
14108 static void
14109 verify_connector_state(struct intel_atomic_state *state,
14110 		       struct intel_crtc *crtc)
14111 {
14112 	struct drm_connector *connector;
14113 	struct drm_connector_state *new_conn_state;
14114 	int i;
14115 
14116 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
14117 		struct drm_encoder *encoder = connector->encoder;
14118 		struct intel_crtc_state *crtc_state = NULL;
14119 
14120 		if (new_conn_state->crtc != &crtc->base)
14121 			continue;
14122 
14123 		if (crtc)
14124 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
14125 
14126 		intel_connector_verify_state(crtc_state, new_conn_state);
14127 
14128 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
14129 		     "connector's atomic encoder doesn't match legacy encoder\n");
14130 	}
14131 }
14132 
14133 static void
14134 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
14135 {
14136 	struct intel_encoder *encoder;
14137 	struct drm_connector *connector;
14138 	struct drm_connector_state *old_conn_state, *new_conn_state;
14139 	int i;
14140 
14141 	for_each_intel_encoder(&dev_priv->drm, encoder) {
14142 		bool enabled = false, found = false;
14143 		enum pipe pipe;
14144 
14145 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14146 			    encoder->base.base.id,
14147 			    encoder->base.name);
14148 
14149 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
14150 						   new_conn_state, i) {
14151 			if (old_conn_state->best_encoder == &encoder->base)
14152 				found = true;
14153 
14154 			if (new_conn_state->best_encoder != &encoder->base)
14155 				continue;
14156 			found = enabled = true;
14157 
14158 			I915_STATE_WARN(new_conn_state->crtc !=
14159 					encoder->base.crtc,
14160 			     "connector's crtc doesn't match encoder crtc\n");
14161 		}
14162 
14163 		if (!found)
14164 			continue;
14165 
14166 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
14167 		     "encoder's enabled state mismatch "
14168 		     "(expected %i, found %i)\n",
14169 		     !!encoder->base.crtc, enabled);
14170 
14171 		if (!encoder->base.crtc) {
14172 			bool active;
14173 
14174 			active = encoder->get_hw_state(encoder, &pipe);
14175 			I915_STATE_WARN(active,
14176 			     "encoder detached but still enabled on pipe %c.\n",
14177 			     pipe_name(pipe));
14178 		}
14179 	}
14180 }
14181 
14182 static void
14183 verify_crtc_state(struct intel_crtc *crtc,
14184 		  struct intel_crtc_state *old_crtc_state,
14185 		  struct intel_crtc_state *new_crtc_state)
14186 {
14187 	struct drm_device *dev = crtc->base.dev;
14188 	struct drm_i915_private *dev_priv = to_i915(dev);
14189 	struct intel_encoder *encoder;
14190 	struct intel_crtc_state *pipe_config = old_crtc_state;
14191 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
14192 	bool active;
14193 
14194 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
14195 	intel_crtc_free_hw_state(old_crtc_state);
14196 	intel_crtc_state_reset(old_crtc_state, crtc);
14197 	old_crtc_state->uapi.state = state;
14198 
14199 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14200 		    crtc->base.name);
14201 
14202 	active = dev_priv->display.get_pipe_config(crtc, pipe_config);
14203 
14204 	/* we keep both pipes enabled on 830 */
14205 	if (IS_I830(dev_priv))
14206 		active = new_crtc_state->hw.active;
14207 
14208 	I915_STATE_WARN(new_crtc_state->hw.active != active,
14209 			"crtc active state doesn't match with hw state "
14210 			"(expected %i, found %i)\n",
14211 			new_crtc_state->hw.active, active);
14212 
14213 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14214 			"transitional active state does not match atomic hw state "
14215 			"(expected %i, found %i)\n",
14216 			new_crtc_state->hw.active, crtc->active);
14217 
14218 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14219 		enum pipe pipe;
14220 
14221 		active = encoder->get_hw_state(encoder, &pipe);
14222 		I915_STATE_WARN(active != new_crtc_state->hw.active,
14223 				"[ENCODER:%i] active %i with crtc active %i\n",
14224 				encoder->base.base.id, active,
14225 				new_crtc_state->hw.active);
14226 
14227 		I915_STATE_WARN(active && crtc->pipe != pipe,
14228 				"Encoder connected to wrong pipe %c\n",
14229 				pipe_name(pipe));
14230 
14231 		if (active)
14232 			encoder->get_config(encoder, pipe_config);
14233 	}
14234 
14235 	intel_crtc_compute_pixel_rate(pipe_config);
14236 
14237 	if (!new_crtc_state->hw.active)
14238 		return;
14239 
14240 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
14241 
14242 	if (!intel_pipe_config_compare(new_crtc_state,
14243 				       pipe_config, false)) {
14244 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
14245 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
14246 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
14247 	}
14248 }
14249 
14250 static void
14251 intel_verify_planes(struct intel_atomic_state *state)
14252 {
14253 	struct intel_plane *plane;
14254 	const struct intel_plane_state *plane_state;
14255 	int i;
14256 
14257 	for_each_new_intel_plane_in_state(state, plane,
14258 					  plane_state, i)
14259 		assert_plane(plane, plane_state->planar_slave ||
14260 			     plane_state->uapi.visible);
14261 }
14262 
14263 static void
14264 verify_single_dpll_state(struct drm_i915_private *dev_priv,
14265 			 struct intel_shared_dpll *pll,
14266 			 struct intel_crtc *crtc,
14267 			 struct intel_crtc_state *new_crtc_state)
14268 {
14269 	struct intel_dpll_hw_state dpll_hw_state;
14270 	unsigned int crtc_mask;
14271 	bool active;
14272 
14273 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
14274 
14275 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
14276 
14277 	active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
14278 
14279 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
14280 		I915_STATE_WARN(!pll->on && pll->active_mask,
14281 		     "pll in active use but not on in sw tracking\n");
14282 		I915_STATE_WARN(pll->on && !pll->active_mask,
14283 		     "pll is on but not used by any active crtc\n");
14284 		I915_STATE_WARN(pll->on != active,
14285 		     "pll on state mismatch (expected %i, found %i)\n",
14286 		     pll->on, active);
14287 	}
14288 
14289 	if (!crtc) {
14290 		I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
14291 				"more active pll users than references: %x vs %x\n",
14292 				pll->active_mask, pll->state.crtc_mask);
14293 
14294 		return;
14295 	}
14296 
14297 	crtc_mask = drm_crtc_mask(&crtc->base);
14298 
14299 	if (new_crtc_state->hw.active)
14300 		I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14301 				"pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
14302 				pipe_name(drm_crtc_index(&crtc->base)), pll->active_mask);
14303 	else
14304 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14305 				"pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
14306 				pipe_name(drm_crtc_index(&crtc->base)), pll->active_mask);
14307 
14308 	I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
14309 			"pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
14310 			crtc_mask, pll->state.crtc_mask);
14311 
14312 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
14313 					  &dpll_hw_state,
14314 					  sizeof(dpll_hw_state)),
14315 			"pll hw state mismatch\n");
14316 }
14317 
14318 static void
14319 verify_shared_dpll_state(struct intel_crtc *crtc,
14320 			 struct intel_crtc_state *old_crtc_state,
14321 			 struct intel_crtc_state *new_crtc_state)
14322 {
14323 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14324 
14325 	if (new_crtc_state->shared_dpll)
14326 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
14327 
14328 	if (old_crtc_state->shared_dpll &&
14329 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14330 		unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14331 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
14332 
14333 		I915_STATE_WARN(pll->active_mask & crtc_mask,
14334 				"pll active mismatch (didn't expect pipe %c in active mask)\n",
14335 				pipe_name(drm_crtc_index(&crtc->base)));
14336 		I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
14337 				"pll enabled crtcs mismatch (found %x in enabled mask)\n",
14338 				pipe_name(drm_crtc_index(&crtc->base)));
14339 	}
14340 }
14341 
14342 static void
14343 intel_modeset_verify_crtc(struct intel_crtc *crtc,
14344 			  struct intel_atomic_state *state,
14345 			  struct intel_crtc_state *old_crtc_state,
14346 			  struct intel_crtc_state *new_crtc_state)
14347 {
14348 	if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
14349 		return;
14350 
14351 	verify_wm_state(crtc, new_crtc_state);
14352 	verify_connector_state(state, crtc);
14353 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14354 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
14355 }
14356 
14357 static void
14358 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
14359 {
14360 	int i;
14361 
14362 	for (i = 0; i < dev_priv->num_shared_dpll; i++)
14363 		verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
14364 }
14365 
14366 static void
14367 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
14368 			      struct intel_atomic_state *state)
14369 {
14370 	verify_encoder_state(dev_priv, state);
14371 	verify_connector_state(state, NULL);
14372 	verify_disabled_dpll_state(dev_priv);
14373 }
14374 
14375 static void
14376 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
14377 {
14378 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
14379 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14380 	const struct drm_display_mode *adjusted_mode =
14381 		&crtc_state->hw.adjusted_mode;
14382 
14383 	drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
14384 
14385 	/*
14386 	 * The scanline counter increments at the leading edge of hsync.
14387 	 *
14388 	 * On most platforms it starts counting from vtotal-1 on the
14389 	 * first active line. That means the scanline counter value is
14390 	 * always one less than what we would expect. Ie. just after
14391 	 * start of vblank, which also occurs at start of hsync (on the
14392 	 * last active line), the scanline counter will read vblank_start-1.
14393 	 *
14394 	 * On gen2 the scanline counter starts counting from 1 instead
14395 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14396 	 * to keep the value positive), instead of adding one.
14397 	 *
14398 	 * On HSW+ the behaviour of the scanline counter depends on the output
14399 	 * type. For DP ports it behaves like most other platforms, but on HDMI
14400 	 * there's an extra 1 line difference. So we need to add two instead of
14401 	 * one to the value.
14402 	 *
14403 	 * On VLV/CHV DSI the scanline counter would appear to increment
14404 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
14405 	 * that means we can't tell whether we're in vblank or not while
14406 	 * we're on that particular line. We must still set scanline_offset
14407 	 * to 1 so that the vblank timestamps come out correct when we query
14408 	 * the scanline counter from within the vblank interrupt handler.
14409 	 * However if queried just before the start of vblank we'll get an
14410 	 * answer that's slightly in the future.
14411 	 */
14412 	if (IS_GEN(dev_priv, 2)) {
14413 		int vtotal;
14414 
14415 		vtotal = adjusted_mode->crtc_vtotal;
14416 		if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
14417 			vtotal /= 2;
14418 
14419 		crtc->scanline_offset = vtotal - 1;
14420 	} else if (HAS_DDI(dev_priv) &&
14421 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
14422 		crtc->scanline_offset = 2;
14423 	} else {
14424 		crtc->scanline_offset = 1;
14425 	}
14426 }
14427 
14428 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
14429 {
14430 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14431 	struct intel_crtc_state *new_crtc_state;
14432 	struct intel_crtc *crtc;
14433 	int i;
14434 
14435 	if (!dev_priv->display.crtc_compute_clock)
14436 		return;
14437 
14438 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14439 		if (!needs_modeset(new_crtc_state))
14440 			continue;
14441 
14442 		intel_release_shared_dplls(state, crtc);
14443 	}
14444 }
14445 
14446 /*
14447  * This implements the workaround described in the "notes" section of the mode
14448  * set sequence documentation. When going from no pipes or single pipe to
14449  * multiple pipes, and planes are enabled after the pipe, we need to wait at
14450  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14451  */
14452 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
14453 {
14454 	struct intel_crtc_state *crtc_state;
14455 	struct intel_crtc *crtc;
14456 	struct intel_crtc_state *first_crtc_state = NULL;
14457 	struct intel_crtc_state *other_crtc_state = NULL;
14458 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14459 	int i;
14460 
14461 	/* look at all crtc's that are going to be enabled in during modeset */
14462 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14463 		if (!crtc_state->hw.active ||
14464 		    !needs_modeset(crtc_state))
14465 			continue;
14466 
14467 		if (first_crtc_state) {
14468 			other_crtc_state = crtc_state;
14469 			break;
14470 		} else {
14471 			first_crtc_state = crtc_state;
14472 			first_pipe = crtc->pipe;
14473 		}
14474 	}
14475 
14476 	/* No workaround needed? */
14477 	if (!first_crtc_state)
14478 		return 0;
14479 
14480 	/* w/a possibly needed, check how many crtc's are already enabled. */
14481 	for_each_intel_crtc(state->base.dev, crtc) {
14482 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14483 		if (IS_ERR(crtc_state))
14484 			return PTR_ERR(crtc_state);
14485 
14486 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
14487 
14488 		if (!crtc_state->hw.active ||
14489 		    needs_modeset(crtc_state))
14490 			continue;
14491 
14492 		/* 2 or more enabled crtcs means no need for w/a */
14493 		if (enabled_pipe != INVALID_PIPE)
14494 			return 0;
14495 
14496 		enabled_pipe = crtc->pipe;
14497 	}
14498 
14499 	if (enabled_pipe != INVALID_PIPE)
14500 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
14501 	else if (other_crtc_state)
14502 		other_crtc_state->hsw_workaround_pipe = first_pipe;
14503 
14504 	return 0;
14505 }
14506 
14507 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
14508 			   u8 active_pipes)
14509 {
14510 	const struct intel_crtc_state *crtc_state;
14511 	struct intel_crtc *crtc;
14512 	int i;
14513 
14514 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14515 		if (crtc_state->hw.active)
14516 			active_pipes |= BIT(crtc->pipe);
14517 		else
14518 			active_pipes &= ~BIT(crtc->pipe);
14519 	}
14520 
14521 	return active_pipes;
14522 }
14523 
14524 static int intel_modeset_checks(struct intel_atomic_state *state)
14525 {
14526 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14527 	int ret;
14528 
14529 	state->modeset = true;
14530 	state->active_pipes = intel_calc_active_pipes(state, dev_priv->active_pipes);
14531 
14532 	state->active_pipe_changes = state->active_pipes ^ dev_priv->active_pipes;
14533 
14534 	if (state->active_pipe_changes) {
14535 		ret = _intel_atomic_lock_global_state(state);
14536 		if (ret)
14537 			return ret;
14538 	}
14539 
14540 	ret = intel_modeset_calc_cdclk(state);
14541 	if (ret)
14542 		return ret;
14543 
14544 	intel_modeset_clear_plls(state);
14545 
14546 	if (IS_HASWELL(dev_priv))
14547 		return hsw_mode_set_planes_workaround(state);
14548 
14549 	return 0;
14550 }
14551 
14552 /*
14553  * Handle calculation of various watermark data at the end of the atomic check
14554  * phase.  The code here should be run after the per-crtc and per-plane 'check'
14555  * handlers to ensure that all derived state has been updated.
14556  */
14557 static int calc_watermark_data(struct intel_atomic_state *state)
14558 {
14559 	struct drm_device *dev = state->base.dev;
14560 	struct drm_i915_private *dev_priv = to_i915(dev);
14561 
14562 	/* Is there platform-specific watermark information to calculate? */
14563 	if (dev_priv->display.compute_global_watermarks)
14564 		return dev_priv->display.compute_global_watermarks(state);
14565 
14566 	return 0;
14567 }
14568 
14569 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
14570 				     struct intel_crtc_state *new_crtc_state)
14571 {
14572 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
14573 		return;
14574 
14575 	new_crtc_state->uapi.mode_changed = false;
14576 	new_crtc_state->update_pipe = true;
14577 }
14578 
14579 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
14580 				    struct intel_crtc_state *new_crtc_state)
14581 {
14582 	/*
14583 	 * If we're not doing the full modeset we want to
14584 	 * keep the current M/N values as they may be
14585 	 * sufficiently different to the computed values
14586 	 * to cause problems.
14587 	 *
14588 	 * FIXME: should really copy more fuzzy state here
14589 	 */
14590 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
14591 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
14592 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
14593 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
14594 }
14595 
14596 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
14597 					  struct intel_crtc *crtc,
14598 					  u8 plane_ids_mask)
14599 {
14600 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14601 	struct intel_plane *plane;
14602 
14603 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14604 		struct intel_plane_state *plane_state;
14605 
14606 		if ((plane_ids_mask & BIT(plane->id)) == 0)
14607 			continue;
14608 
14609 		plane_state = intel_atomic_get_plane_state(state, plane);
14610 		if (IS_ERR(plane_state))
14611 			return PTR_ERR(plane_state);
14612 	}
14613 
14614 	return 0;
14615 }
14616 
14617 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
14618 {
14619 	/* See {hsw,vlv,ivb}_plane_ratio() */
14620 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
14621 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
14622 		IS_IVYBRIDGE(dev_priv);
14623 }
14624 
14625 static int intel_atomic_check_planes(struct intel_atomic_state *state,
14626 				     bool *need_cdclk_calc)
14627 {
14628 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14629 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14630 	struct intel_plane_state *plane_state;
14631 	struct intel_plane *plane;
14632 	struct intel_crtc *crtc;
14633 	int i, ret;
14634 
14635 	ret = icl_add_linked_planes(state);
14636 	if (ret)
14637 		return ret;
14638 
14639 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14640 		ret = intel_plane_atomic_check(state, plane);
14641 		if (ret) {
14642 			drm_dbg_atomic(&dev_priv->drm,
14643 				       "[PLANE:%d:%s] atomic driver check failed\n",
14644 				       plane->base.base.id, plane->base.name);
14645 			return ret;
14646 		}
14647 	}
14648 
14649 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14650 					    new_crtc_state, i) {
14651 		u8 old_active_planes, new_active_planes;
14652 
14653 		ret = icl_check_nv12_planes(new_crtc_state);
14654 		if (ret)
14655 			return ret;
14656 
14657 		/*
14658 		 * On some platforms the number of active planes affects
14659 		 * the planes' minimum cdclk calculation. Add such planes
14660 		 * to the state before we compute the minimum cdclk.
14661 		 */
14662 		if (!active_planes_affects_min_cdclk(dev_priv))
14663 			continue;
14664 
14665 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14666 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14667 
14668 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
14669 			continue;
14670 
14671 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
14672 		if (ret)
14673 			return ret;
14674 	}
14675 
14676 	/*
14677 	 * active_planes bitmask has been updated, and potentially
14678 	 * affected planes are part of the state. We can now
14679 	 * compute the minimum cdclk for each plane.
14680 	 */
14681 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14682 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
14683 		if (ret)
14684 			return ret;
14685 	}
14686 
14687 	return 0;
14688 }
14689 
14690 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
14691 {
14692 	struct intel_crtc_state *crtc_state;
14693 	struct intel_crtc *crtc;
14694 	int i;
14695 
14696 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14697 		int ret = intel_crtc_atomic_check(state, crtc);
14698 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
14699 		if (ret) {
14700 			drm_dbg_atomic(&i915->drm,
14701 				       "[CRTC:%d:%s] atomic driver check failed\n",
14702 				       crtc->base.base.id, crtc->base.name);
14703 			return ret;
14704 		}
14705 	}
14706 
14707 	return 0;
14708 }
14709 
14710 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
14711 					       u8 transcoders)
14712 {
14713 	const struct intel_crtc_state *new_crtc_state;
14714 	struct intel_crtc *crtc;
14715 	int i;
14716 
14717 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14718 		if (new_crtc_state->hw.enable &&
14719 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
14720 		    needs_modeset(new_crtc_state))
14721 			return true;
14722 	}
14723 
14724 	return false;
14725 }
14726 
14727 /**
14728  * intel_atomic_check - validate state object
14729  * @dev: drm device
14730  * @_state: state to validate
14731  */
14732 static int intel_atomic_check(struct drm_device *dev,
14733 			      struct drm_atomic_state *_state)
14734 {
14735 	struct drm_i915_private *dev_priv = to_i915(dev);
14736 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
14737 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14738 	struct intel_cdclk_state *new_cdclk_state;
14739 	struct intel_crtc *crtc;
14740 	int ret, i;
14741 	bool any_ms = false;
14742 
14743 	/* Catch I915_MODE_FLAG_INHERITED */
14744 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14745 					    new_crtc_state, i) {
14746 		if (new_crtc_state->hw.mode.private_flags !=
14747 		    old_crtc_state->hw.mode.private_flags)
14748 			new_crtc_state->uapi.mode_changed = true;
14749 	}
14750 
14751 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
14752 	if (ret)
14753 		goto fail;
14754 
14755 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14756 					    new_crtc_state, i) {
14757 		if (!needs_modeset(new_crtc_state)) {
14758 			/* Light copy */
14759 			intel_crtc_copy_uapi_to_hw_state_nomodeset(new_crtc_state);
14760 
14761 			continue;
14762 		}
14763 
14764 		ret = intel_crtc_prepare_cleared_state(new_crtc_state);
14765 		if (ret)
14766 			goto fail;
14767 
14768 		if (!new_crtc_state->hw.enable)
14769 			continue;
14770 
14771 		ret = intel_modeset_pipe_config(new_crtc_state);
14772 		if (ret)
14773 			goto fail;
14774 	}
14775 
14776 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14777 					    new_crtc_state, i) {
14778 		if (!needs_modeset(new_crtc_state))
14779 			continue;
14780 
14781 		ret = intel_modeset_pipe_config_late(new_crtc_state);
14782 		if (ret)
14783 			goto fail;
14784 
14785 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
14786 	}
14787 
14788 	/**
14789 	 * Check if fastset is allowed by external dependencies like other
14790 	 * pipes and transcoders.
14791 	 *
14792 	 * Right now it only forces a fullmodeset when the MST master
14793 	 * transcoder did not changed but the pipe of the master transcoder
14794 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
14795 	 * in case of port synced crtcs, if one of the synced crtcs
14796 	 * needs a full modeset, all other synced crtcs should be
14797 	 * forced a full modeset.
14798 	 */
14799 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14800 		if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
14801 			continue;
14802 
14803 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
14804 			enum transcoder master = new_crtc_state->mst_master_transcoder;
14805 
14806 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
14807 				new_crtc_state->uapi.mode_changed = true;
14808 				new_crtc_state->update_pipe = false;
14809 			}
14810 		}
14811 
14812 		if (is_trans_port_sync_mode(new_crtc_state)) {
14813 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
14814 
14815 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
14816 				trans |= BIT(new_crtc_state->master_transcoder);
14817 
14818 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
14819 				new_crtc_state->uapi.mode_changed = true;
14820 				new_crtc_state->update_pipe = false;
14821 			}
14822 		}
14823 	}
14824 
14825 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14826 					    new_crtc_state, i) {
14827 		if (needs_modeset(new_crtc_state)) {
14828 			any_ms = true;
14829 			continue;
14830 		}
14831 
14832 		if (!new_crtc_state->update_pipe)
14833 			continue;
14834 
14835 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
14836 	}
14837 
14838 	if (any_ms && !check_digital_port_conflicts(state)) {
14839 		drm_dbg_kms(&dev_priv->drm,
14840 			    "rejecting conflicting digital port configuration\n");
14841 		ret = EINVAL;
14842 		goto fail;
14843 	}
14844 
14845 	ret = drm_dp_mst_atomic_check(&state->base);
14846 	if (ret)
14847 		goto fail;
14848 
14849 	ret = intel_atomic_check_planes(state, &any_ms);
14850 	if (ret)
14851 		goto fail;
14852 
14853 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
14854 	if (new_cdclk_state && new_cdclk_state->force_min_cdclk_changed)
14855 		any_ms = true;
14856 
14857 	/*
14858 	 * distrust_bios_wm will force a full dbuf recomputation
14859 	 * but the hardware state will only get updated accordingly
14860 	 * if state->modeset==true. Hence distrust_bios_wm==true &&
14861 	 * state->modeset==false is an invalid combination which
14862 	 * would cause the hardware and software dbuf state to get
14863 	 * out of sync. We must prevent that.
14864 	 *
14865 	 * FIXME clean up this mess and introduce better
14866 	 * state tracking for dbuf.
14867 	 */
14868 	if (dev_priv->wm.distrust_bios_wm)
14869 		any_ms = true;
14870 
14871 	if (any_ms) {
14872 		ret = intel_modeset_checks(state);
14873 		if (ret)
14874 			goto fail;
14875 	}
14876 
14877 	ret = intel_atomic_check_crtcs(state);
14878 	if (ret)
14879 		goto fail;
14880 
14881 	intel_fbc_choose_crtc(dev_priv, state);
14882 	ret = calc_watermark_data(state);
14883 	if (ret)
14884 		goto fail;
14885 
14886 	ret = intel_bw_atomic_check(state);
14887 	if (ret)
14888 		goto fail;
14889 
14890 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14891 					    new_crtc_state, i) {
14892 		if (!needs_modeset(new_crtc_state) &&
14893 		    !new_crtc_state->update_pipe)
14894 			continue;
14895 
14896 		intel_dump_pipe_config(new_crtc_state, state,
14897 				       needs_modeset(new_crtc_state) ?
14898 				       "[modeset]" : "[fastset]");
14899 	}
14900 
14901 	return 0;
14902 
14903  fail:
14904 	if (ret == -EDEADLK)
14905 		return ret;
14906 
14907 	/*
14908 	 * FIXME would probably be nice to know which crtc specifically
14909 	 * caused the failure, in cases where we can pinpoint it.
14910 	 */
14911 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14912 					    new_crtc_state, i)
14913 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
14914 
14915 	return ret;
14916 }
14917 
14918 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
14919 {
14920 	return drm_atomic_helper_prepare_planes(state->base.dev,
14921 						&state->base);
14922 }
14923 
14924 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14925 {
14926 	struct drm_device *dev = crtc->base.dev;
14927 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
14928 
14929 	if (!vblank->max_vblank_count)
14930 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
14931 
14932 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
14933 }
14934 
14935 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
14936 				  struct intel_crtc_state *crtc_state)
14937 {
14938 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14939 
14940 	if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
14941 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
14942 
14943 	if (crtc_state->has_pch_encoder) {
14944 		enum pipe pch_transcoder =
14945 			intel_crtc_pch_transcoder(crtc);
14946 
14947 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
14948 	}
14949 }
14950 
14951 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
14952 			       const struct intel_crtc_state *new_crtc_state)
14953 {
14954 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
14955 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14956 
14957 	/*
14958 	 * Update pipe size and adjust fitter if needed: the reason for this is
14959 	 * that in compute_mode_changes we check the native mode (not the pfit
14960 	 * mode) to see if we can flip rather than do a full mode set. In the
14961 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
14962 	 * pfit state, we'll end up with a big fb scanned out into the wrong
14963 	 * sized surface.
14964 	 */
14965 	intel_set_pipe_src_size(new_crtc_state);
14966 
14967 	/* on skylake this is done by detaching scalers */
14968 	if (INTEL_GEN(dev_priv) >= 9) {
14969 		skl_detach_scalers(new_crtc_state);
14970 
14971 		if (new_crtc_state->pch_pfit.enabled)
14972 			skl_pfit_enable(new_crtc_state);
14973 	} else if (HAS_PCH_SPLIT(dev_priv)) {
14974 		if (new_crtc_state->pch_pfit.enabled)
14975 			ilk_pfit_enable(new_crtc_state);
14976 		else if (old_crtc_state->pch_pfit.enabled)
14977 			ilk_pfit_disable(old_crtc_state);
14978 	}
14979 
14980 	/*
14981 	 * The register is supposedly single buffered so perhaps
14982 	 * not 100% correct to do this here. But SKL+ calculate
14983 	 * this based on the adjust pixel rate so pfit changes do
14984 	 * affect it and so it must be updated for fastsets.
14985 	 * HSW/BDW only really need this here for fastboot, after
14986 	 * that the value should not change without a full modeset.
14987 	 */
14988 	if (INTEL_GEN(dev_priv) >= 9 ||
14989 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
14990 		hsw_set_linetime_wm(new_crtc_state);
14991 
14992 	if (INTEL_GEN(dev_priv) >= 11)
14993 		icl_set_pipe_chicken(crtc);
14994 }
14995 
14996 static void commit_pipe_config(struct intel_atomic_state *state,
14997 			       struct intel_crtc_state *old_crtc_state,
14998 			       struct intel_crtc_state *new_crtc_state)
14999 {
15000 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
15001 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15002 	bool modeset = needs_modeset(new_crtc_state);
15003 
15004 	/*
15005 	 * During modesets pipe configuration was programmed as the
15006 	 * CRTC was enabled.
15007 	 */
15008 	if (!modeset) {
15009 		if (new_crtc_state->uapi.color_mgmt_changed ||
15010 		    new_crtc_state->update_pipe)
15011 			intel_color_commit(new_crtc_state);
15012 
15013 		if (INTEL_GEN(dev_priv) >= 9)
15014 			skl_detach_scalers(new_crtc_state);
15015 
15016 		if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15017 			bdw_set_pipemisc(new_crtc_state);
15018 
15019 		if (new_crtc_state->update_pipe)
15020 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
15021 	}
15022 
15023 	if (dev_priv->display.atomic_update_watermarks)
15024 		dev_priv->display.atomic_update_watermarks(state, crtc);
15025 }
15026 
15027 static void intel_update_crtc(struct intel_crtc *crtc,
15028 			      struct intel_atomic_state *state,
15029 			      struct intel_crtc_state *old_crtc_state,
15030 			      struct intel_crtc_state *new_crtc_state)
15031 {
15032 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15033 	bool modeset = needs_modeset(new_crtc_state);
15034 
15035 	if (modeset) {
15036 		intel_crtc_update_active_timings(new_crtc_state);
15037 
15038 		dev_priv->display.crtc_enable(state, crtc);
15039 
15040 		/* vblanks work again, re-enable pipe CRC. */
15041 		intel_crtc_enable_pipe_crc(crtc);
15042 	} else {
15043 		if (new_crtc_state->preload_luts &&
15044 		    (new_crtc_state->uapi.color_mgmt_changed ||
15045 		     new_crtc_state->update_pipe))
15046 			intel_color_load_luts(new_crtc_state);
15047 
15048 		intel_pre_plane_update(state, crtc);
15049 
15050 		if (new_crtc_state->update_pipe)
15051 			intel_encoders_update_pipe(state, crtc);
15052 	}
15053 
15054 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15055 		intel_fbc_disable(crtc);
15056 	else
15057 		intel_fbc_enable(state, crtc);
15058 
15059 	/* Perform vblank evasion around commit operation */
15060 	intel_pipe_update_start(new_crtc_state);
15061 
15062 	commit_pipe_config(state, old_crtc_state, new_crtc_state);
15063 
15064 	if (INTEL_GEN(dev_priv) >= 9)
15065 		skl_update_planes_on_crtc(state, crtc);
15066 	else
15067 		i9xx_update_planes_on_crtc(state, crtc);
15068 
15069 	intel_pipe_update_end(new_crtc_state);
15070 
15071 	/*
15072 	 * We usually enable FIFO underrun interrupts as part of the
15073 	 * CRTC enable sequence during modesets.  But when we inherit a
15074 	 * valid pipe configuration from the BIOS we need to take care
15075 	 * of enabling them on the CRTC's first fastset.
15076 	 */
15077 	if (new_crtc_state->update_pipe && !modeset &&
15078 	    old_crtc_state->hw.mode.private_flags & I915_MODE_FLAG_INHERITED)
15079 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15080 }
15081 
15082 static struct intel_crtc *intel_get_slave_crtc(const struct intel_crtc_state *new_crtc_state)
15083 {
15084 	struct drm_i915_private *dev_priv = to_i915(new_crtc_state->uapi.crtc->dev);
15085 	enum transcoder slave_transcoder;
15086 
15087 	drm_WARN_ON(&dev_priv->drm,
15088 		    !is_power_of_2(new_crtc_state->sync_mode_slaves_mask));
15089 
15090 	slave_transcoder = ffs(new_crtc_state->sync_mode_slaves_mask) - 1;
15091 	return intel_get_crtc_for_pipe(dev_priv,
15092 				       (enum pipe)slave_transcoder);
15093 }
15094 
15095 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15096 					  struct intel_crtc_state *old_crtc_state,
15097 					  struct intel_crtc_state *new_crtc_state,
15098 					  struct intel_crtc *crtc)
15099 {
15100 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15101 
15102 	intel_crtc_disable_planes(state, crtc);
15103 
15104 	/*
15105 	 * We need to disable pipe CRC before disabling the pipe,
15106 	 * or we race against vblank off.
15107 	 */
15108 	intel_crtc_disable_pipe_crc(crtc);
15109 
15110 	dev_priv->display.crtc_disable(state, crtc);
15111 	crtc->active = false;
15112 	intel_fbc_disable(crtc);
15113 	intel_disable_shared_dpll(old_crtc_state);
15114 
15115 	/* FIXME unify this for all platforms */
15116 	if (!new_crtc_state->hw.active &&
15117 	    !HAS_GMCH(dev_priv) &&
15118 	    dev_priv->display.initial_watermarks)
15119 		dev_priv->display.initial_watermarks(state, crtc);
15120 }
15121 
15122 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15123 {
15124 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15125 	struct intel_crtc *crtc;
15126 	u32 handled = 0;
15127 	int i;
15128 
15129 	/* Only disable port sync and MST slaves */
15130 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15131 					    new_crtc_state, i) {
15132 		if (!needs_modeset(new_crtc_state))
15133 			continue;
15134 
15135 		if (!old_crtc_state->hw.active)
15136 			continue;
15137 
15138 		/* In case of Transcoder port Sync master slave CRTCs can be
15139 		 * assigned in any order and we need to make sure that
15140 		 * slave CRTCs are disabled first and then master CRTC since
15141 		 * Slave vblanks are masked till Master Vblanks.
15142 		 */
15143 		if (!is_trans_port_sync_slave(old_crtc_state) &&
15144 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
15145 			continue;
15146 
15147 		intel_pre_plane_update(state, crtc);
15148 		intel_old_crtc_state_disables(state, old_crtc_state,
15149 					      new_crtc_state, crtc);
15150 		handled |= BIT(crtc->pipe);
15151 	}
15152 
15153 	/* Disable everything else left on */
15154 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15155 					    new_crtc_state, i) {
15156 		if (!needs_modeset(new_crtc_state) ||
15157 		    (handled & BIT(crtc->pipe)))
15158 			continue;
15159 
15160 		intel_pre_plane_update(state, crtc);
15161 		if (old_crtc_state->hw.active)
15162 			intel_old_crtc_state_disables(state, old_crtc_state,
15163 						      new_crtc_state, crtc);
15164 	}
15165 }
15166 
15167 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
15168 {
15169 	struct intel_crtc *crtc;
15170 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15171 	int i;
15172 
15173 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15174 		if (!new_crtc_state->hw.active)
15175 			continue;
15176 
15177 		intel_update_crtc(crtc, state, old_crtc_state,
15178 				  new_crtc_state);
15179 	}
15180 }
15181 
15182 static void intel_crtc_enable_trans_port_sync(struct intel_crtc *crtc,
15183 					      struct intel_atomic_state *state,
15184 					      struct intel_crtc_state *new_crtc_state)
15185 {
15186 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15187 
15188 	intel_crtc_update_active_timings(new_crtc_state);
15189 	dev_priv->display.crtc_enable(state, crtc);
15190 	intel_crtc_enable_pipe_crc(crtc);
15191 }
15192 
15193 static void intel_set_dp_tp_ctl_normal(struct intel_crtc *crtc,
15194 				       struct intel_atomic_state *state)
15195 {
15196 	struct drm_connector *uninitialized_var(conn);
15197 	struct drm_connector_state *conn_state;
15198 	struct intel_dp *intel_dp;
15199 	int i;
15200 
15201 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
15202 		if (conn_state->crtc == &crtc->base)
15203 			break;
15204 	}
15205 	intel_dp = intel_attached_dp(to_intel_connector(conn));
15206 	intel_dp_stop_link_train(intel_dp);
15207 }
15208 
15209 /*
15210  * TODO: This is only called from port sync and it is identical to what will be
15211  * executed again in intel_update_crtc() over port sync pipes
15212  */
15213 static void intel_post_crtc_enable_updates(struct intel_crtc *crtc,
15214 					   struct intel_atomic_state *state)
15215 {
15216 	struct intel_crtc_state *new_crtc_state =
15217 		intel_atomic_get_new_crtc_state(state, crtc);
15218 	struct intel_crtc_state *old_crtc_state =
15219 		intel_atomic_get_old_crtc_state(state, crtc);
15220 	bool modeset = needs_modeset(new_crtc_state);
15221 
15222 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15223 		intel_fbc_disable(crtc);
15224 	else
15225 		intel_fbc_enable(state, crtc);
15226 
15227 	/* Perform vblank evasion around commit operation */
15228 	intel_pipe_update_start(new_crtc_state);
15229 	commit_pipe_config(state, old_crtc_state, new_crtc_state);
15230 	skl_update_planes_on_crtc(state, crtc);
15231 	intel_pipe_update_end(new_crtc_state);
15232 
15233 	/*
15234 	 * We usually enable FIFO underrun interrupts as part of the
15235 	 * CRTC enable sequence during modesets.  But when we inherit a
15236 	 * valid pipe configuration from the BIOS we need to take care
15237 	 * of enabling them on the CRTC's first fastset.
15238 	 */
15239 	if (new_crtc_state->update_pipe && !modeset &&
15240 	    old_crtc_state->hw.mode.private_flags & I915_MODE_FLAG_INHERITED)
15241 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15242 }
15243 
15244 static void intel_update_trans_port_sync_crtcs(struct intel_crtc *crtc,
15245 					       struct intel_atomic_state *state,
15246 					       struct intel_crtc_state *old_crtc_state,
15247 					       struct intel_crtc_state *new_crtc_state)
15248 {
15249 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
15250 	struct intel_crtc *slave_crtc = intel_get_slave_crtc(new_crtc_state);
15251 	struct intel_crtc_state *new_slave_crtc_state =
15252 		intel_atomic_get_new_crtc_state(state, slave_crtc);
15253 	struct intel_crtc_state *old_slave_crtc_state =
15254 		intel_atomic_get_old_crtc_state(state, slave_crtc);
15255 
15256 	drm_WARN_ON(&i915->drm, !slave_crtc || !new_slave_crtc_state ||
15257 		    !old_slave_crtc_state);
15258 
15259 	drm_dbg_kms(&i915->drm,
15260 		    "Updating Transcoder Port Sync Master CRTC = %d %s and Slave CRTC %d %s\n",
15261 		    crtc->base.base.id, crtc->base.name,
15262 		    slave_crtc->base.base.id, slave_crtc->base.name);
15263 
15264 	/* Enable seq for slave with with DP_TP_CTL left Idle until the
15265 	 * master is ready
15266 	 */
15267 	intel_crtc_enable_trans_port_sync(slave_crtc,
15268 					  state,
15269 					  new_slave_crtc_state);
15270 
15271 	/* Enable seq for master with with DP_TP_CTL left Idle */
15272 	intel_crtc_enable_trans_port_sync(crtc,
15273 					  state,
15274 					  new_crtc_state);
15275 
15276 	/* Set Slave's DP_TP_CTL to Normal */
15277 	intel_set_dp_tp_ctl_normal(slave_crtc,
15278 				   state);
15279 
15280 	/* Set Master's DP_TP_CTL To Normal */
15281 	usleep_range(200, 400);
15282 	intel_set_dp_tp_ctl_normal(crtc,
15283 				   state);
15284 
15285 	/* Now do the post crtc enable for all master and slaves */
15286 	intel_post_crtc_enable_updates(slave_crtc,
15287 				       state);
15288 	intel_post_crtc_enable_updates(crtc,
15289 				       state);
15290 }
15291 
15292 static void icl_dbuf_slice_pre_update(struct intel_atomic_state *state)
15293 {
15294 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15295 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15296 	u8 required_slices = state->enabled_dbuf_slices_mask;
15297 	u8 slices_union = hw_enabled_slices | required_slices;
15298 
15299 	/* If 2nd DBuf slice required, enable it here */
15300 	if (INTEL_GEN(dev_priv) >= 11 && slices_union != hw_enabled_slices)
15301 		icl_dbuf_slices_update(dev_priv, slices_union);
15302 }
15303 
15304 static void icl_dbuf_slice_post_update(struct intel_atomic_state *state)
15305 {
15306 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15307 	u8 hw_enabled_slices = dev_priv->enabled_dbuf_slices_mask;
15308 	u8 required_slices = state->enabled_dbuf_slices_mask;
15309 
15310 	/* If 2nd DBuf slice is no more required disable it */
15311 	if (INTEL_GEN(dev_priv) >= 11 && required_slices != hw_enabled_slices)
15312 		icl_dbuf_slices_update(dev_priv, required_slices);
15313 }
15314 
15315 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
15316 {
15317 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15318 	struct intel_crtc *crtc;
15319 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15320 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
15321 	const u8 num_pipes = INTEL_NUM_PIPES(dev_priv);
15322 	u8 update_pipes = 0, modeset_pipes = 0;
15323 	int i;
15324 
15325 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15326 		enum pipe pipe = crtc->pipe;
15327 
15328 		if (!new_crtc_state->hw.active)
15329 			continue;
15330 
15331 		/* ignore allocations for crtc's that have been turned off. */
15332 		if (!needs_modeset(new_crtc_state)) {
15333 			entries[pipe] = old_crtc_state->wm.skl.ddb;
15334 			update_pipes |= BIT(pipe);
15335 		} else {
15336 			modeset_pipes |= BIT(pipe);
15337 		}
15338 	}
15339 
15340 	/*
15341 	 * Whenever the number of active pipes changes, we need to make sure we
15342 	 * update the pipes in the right order so that their ddb allocations
15343 	 * never overlap with each other between CRTC updates. Otherwise we'll
15344 	 * cause pipe underruns and other bad stuff.
15345 	 *
15346 	 * So first lets enable all pipes that do not need a fullmodeset as
15347 	 * those don't have any external dependency.
15348 	 */
15349 	while (update_pipes) {
15350 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15351 						    new_crtc_state, i) {
15352 			enum pipe pipe = crtc->pipe;
15353 
15354 			if ((update_pipes & BIT(pipe)) == 0)
15355 				continue;
15356 
15357 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15358 							entries, num_pipes, pipe))
15359 				continue;
15360 
15361 			entries[pipe] = new_crtc_state->wm.skl.ddb;
15362 			update_pipes &= ~BIT(pipe);
15363 
15364 			intel_update_crtc(crtc, state, old_crtc_state,
15365 					  new_crtc_state);
15366 
15367 			/*
15368 			 * If this is an already active pipe, it's DDB changed,
15369 			 * and this isn't the last pipe that needs updating
15370 			 * then we need to wait for a vblank to pass for the
15371 			 * new ddb allocation to take effect.
15372 			 */
15373 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
15374 						 &old_crtc_state->wm.skl.ddb) &&
15375 			    (update_pipes | modeset_pipes))
15376 				intel_wait_for_vblank(dev_priv, pipe);
15377 		}
15378 	}
15379 
15380 	/*
15381 	 * Enable all pipes that needs a modeset and do not depends on other
15382 	 * pipes
15383 	 */
15384 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15385 					    new_crtc_state, i) {
15386 		enum pipe pipe = crtc->pipe;
15387 
15388 		if ((modeset_pipes & BIT(pipe)) == 0)
15389 			continue;
15390 
15391 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
15392 		    is_trans_port_sync_slave(new_crtc_state))
15393 			continue;
15394 
15395 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15396 									entries, num_pipes, pipe));
15397 
15398 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15399 		modeset_pipes &= ~BIT(pipe);
15400 
15401 		if (is_trans_port_sync_mode(new_crtc_state)) {
15402 			struct intel_crtc *slave_crtc;
15403 
15404 			intel_update_trans_port_sync_crtcs(crtc, state,
15405 							   old_crtc_state,
15406 							   new_crtc_state);
15407 
15408 			slave_crtc = intel_get_slave_crtc(new_crtc_state);
15409 			/* TODO: update entries[] of slave */
15410 			modeset_pipes &= ~BIT(slave_crtc->pipe);
15411 
15412 		} else {
15413 			intel_update_crtc(crtc, state, old_crtc_state,
15414 					  new_crtc_state);
15415 		}
15416 	}
15417 
15418 	/*
15419 	 * Finally enable all pipes that needs a modeset and depends on
15420 	 * other pipes, right now it is only MST slaves as both port sync slave
15421 	 * and master are enabled together
15422 	 */
15423 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15424 					    new_crtc_state, i) {
15425 		enum pipe pipe = crtc->pipe;
15426 
15427 		if ((modeset_pipes & BIT(pipe)) == 0)
15428 			continue;
15429 
15430 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15431 									entries, num_pipes, pipe));
15432 
15433 		entries[pipe] = new_crtc_state->wm.skl.ddb;
15434 		modeset_pipes &= ~BIT(pipe);
15435 
15436 		intel_update_crtc(crtc, state, old_crtc_state, new_crtc_state);
15437 	}
15438 
15439 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
15440 
15441 }
15442 
15443 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
15444 {
15445 	struct intel_atomic_state *state, *next;
15446 	struct llist_node *freed;
15447 
15448 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
15449 	llist_for_each_entry_safe(state, next, freed, freed)
15450 		drm_atomic_state_put(&state->base);
15451 }
15452 
15453 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
15454 {
15455 	struct drm_i915_private *dev_priv =
15456 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
15457 
15458 	intel_atomic_helper_free_state(dev_priv);
15459 }
15460 
15461 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
15462 {
15463 	struct wait_queue_entry wait_fence, wait_reset;
15464 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
15465 
15466 	init_wait_entry(&wait_fence, 0);
15467 	init_wait_entry(&wait_reset, 0);
15468 	for (;;) {
15469 		prepare_to_wait(&intel_state->commit_ready.wait,
15470 				&wait_fence, TASK_UNINTERRUPTIBLE);
15471 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15472 					      I915_RESET_MODESET),
15473 				&wait_reset, TASK_UNINTERRUPTIBLE);
15474 
15475 
15476 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
15477 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
15478 			break;
15479 
15480 		schedule();
15481 	}
15482 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
15483 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15484 				  I915_RESET_MODESET),
15485 		    &wait_reset);
15486 }
15487 
15488 static void intel_atomic_cleanup_work(struct work_struct *work)
15489 {
15490 	struct drm_atomic_state *state =
15491 		container_of(work, struct drm_atomic_state, commit_work);
15492 	struct drm_i915_private *i915 = to_i915(state->dev);
15493 
15494 	drm_atomic_helper_cleanup_planes(&i915->drm, state);
15495 	drm_atomic_helper_commit_cleanup_done(state);
15496 	drm_atomic_state_put(state);
15497 
15498 	intel_atomic_helper_free_state(i915);
15499 }
15500 
15501 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
15502 {
15503 	struct drm_device *dev = state->base.dev;
15504 	struct drm_i915_private *dev_priv = to_i915(dev);
15505 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15506 	struct intel_crtc *crtc;
15507 	u64 put_domains[I915_MAX_PIPES] = {};
15508 	intel_wakeref_t wakeref = 0;
15509 	int i;
15510 
15511 	intel_atomic_commit_fence_wait(state);
15512 
15513 	drm_atomic_helper_wait_for_dependencies(&state->base);
15514 
15515 	if (state->modeset)
15516 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
15517 
15518 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15519 					    new_crtc_state, i) {
15520 		if (needs_modeset(new_crtc_state) ||
15521 		    new_crtc_state->update_pipe) {
15522 
15523 			put_domains[crtc->pipe] =
15524 				modeset_get_crtc_power_domains(new_crtc_state);
15525 		}
15526 	}
15527 
15528 	intel_commit_modeset_disables(state);
15529 
15530 	/* FIXME: Eventually get rid of our crtc->config pointer */
15531 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15532 		crtc->config = new_crtc_state;
15533 
15534 	if (state->modeset) {
15535 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
15536 
15537 		intel_set_cdclk_pre_plane_update(state);
15538 
15539 		/*
15540 		 * SKL workaround: bspec recommends we disable the SAGV when we
15541 		 * have more then one pipe enabled
15542 		 */
15543 		if (!intel_can_enable_sagv(state))
15544 			intel_disable_sagv(dev_priv);
15545 
15546 		intel_modeset_verify_disabled(dev_priv, state);
15547 	}
15548 
15549 	/* Complete the events for pipes that have now been disabled */
15550 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15551 		bool modeset = needs_modeset(new_crtc_state);
15552 
15553 		/* Complete events for now disable pipes here. */
15554 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
15555 			spin_lock_irq(&dev->event_lock);
15556 			drm_crtc_send_vblank_event(&crtc->base,
15557 						   new_crtc_state->uapi.event);
15558 			spin_unlock_irq(&dev->event_lock);
15559 
15560 			new_crtc_state->uapi.event = NULL;
15561 		}
15562 	}
15563 
15564 	if (state->modeset)
15565 		intel_encoders_update_prepare(state);
15566 
15567 	/* Enable all new slices, we might need */
15568 	if (state->modeset)
15569 		icl_dbuf_slice_pre_update(state);
15570 
15571 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
15572 	dev_priv->display.commit_modeset_enables(state);
15573 
15574 	if (state->modeset) {
15575 		intel_encoders_update_complete(state);
15576 
15577 		intel_set_cdclk_post_plane_update(state);
15578 	}
15579 
15580 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
15581 	 * already, but still need the state for the delayed optimization. To
15582 	 * fix this:
15583 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
15584 	 * - schedule that vblank worker _before_ calling hw_done
15585 	 * - at the start of commit_tail, cancel it _synchrously
15586 	 * - switch over to the vblank wait helper in the core after that since
15587 	 *   we don't need out special handling any more.
15588 	 */
15589 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
15590 
15591 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15592 		if (new_crtc_state->hw.active &&
15593 		    !needs_modeset(new_crtc_state) &&
15594 		    !new_crtc_state->preload_luts &&
15595 		    (new_crtc_state->uapi.color_mgmt_changed ||
15596 		     new_crtc_state->update_pipe))
15597 			intel_color_load_luts(new_crtc_state);
15598 	}
15599 
15600 	/*
15601 	 * Now that the vblank has passed, we can go ahead and program the
15602 	 * optimal watermarks on platforms that need two-step watermark
15603 	 * programming.
15604 	 *
15605 	 * TODO: Move this (and other cleanup) to an async worker eventually.
15606 	 */
15607 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15608 					    new_crtc_state, i) {
15609 		/*
15610 		 * Gen2 reports pipe underruns whenever all planes are disabled.
15611 		 * So re-enable underrun reporting after some planes get enabled.
15612 		 *
15613 		 * We do this before .optimize_watermarks() so that we have a
15614 		 * chance of catching underruns with the intermediate watermarks
15615 		 * vs. the new plane configuration.
15616 		 */
15617 		if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
15618 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15619 
15620 		if (dev_priv->display.optimize_watermarks)
15621 			dev_priv->display.optimize_watermarks(state, crtc);
15622 	}
15623 
15624 	/* Disable all slices, we don't need */
15625 	if (state->modeset)
15626 		icl_dbuf_slice_post_update(state);
15627 
15628 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15629 		intel_post_plane_update(state, crtc);
15630 
15631 		if (put_domains[i])
15632 			modeset_put_power_domains(dev_priv, put_domains[i]);
15633 
15634 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
15635 	}
15636 
15637 	/* Underruns don't always raise interrupts, so check manually */
15638 	intel_check_cpu_fifo_underruns(dev_priv);
15639 	intel_check_pch_fifo_underruns(dev_priv);
15640 
15641 	if (state->modeset)
15642 		intel_verify_planes(state);
15643 
15644 	if (state->modeset && intel_can_enable_sagv(state))
15645 		intel_enable_sagv(dev_priv);
15646 
15647 	drm_atomic_helper_commit_hw_done(&state->base);
15648 
15649 	if (state->modeset) {
15650 		/* As one of the primary mmio accessors, KMS has a high
15651 		 * likelihood of triggering bugs in unclaimed access. After we
15652 		 * finish modesetting, see if an error has been flagged, and if
15653 		 * so enable debugging for the next modeset - and hope we catch
15654 		 * the culprit.
15655 		 */
15656 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
15657 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
15658 	}
15659 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15660 
15661 	/*
15662 	 * Defer the cleanup of the old state to a separate worker to not
15663 	 * impede the current task (userspace for blocking modesets) that
15664 	 * are executed inline. For out-of-line asynchronous modesets/flips,
15665 	 * deferring to a new worker seems overkill, but we would place a
15666 	 * schedule point (cond_resched()) here anyway to keep latencies
15667 	 * down.
15668 	 */
15669 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
15670 	queue_work(system_highpri_wq, &state->base.commit_work);
15671 }
15672 
15673 static void intel_atomic_commit_work(struct work_struct *work)
15674 {
15675 	struct intel_atomic_state *state =
15676 		container_of(work, struct intel_atomic_state, base.commit_work);
15677 
15678 	intel_atomic_commit_tail(state);
15679 }
15680 
15681 static int __i915_sw_fence_call
15682 intel_atomic_commit_ready(struct i915_sw_fence *fence,
15683 			  enum i915_sw_fence_notify notify)
15684 {
15685 	struct intel_atomic_state *state =
15686 		container_of(fence, struct intel_atomic_state, commit_ready);
15687 
15688 	switch (notify) {
15689 	case FENCE_COMPLETE:
15690 		/* we do blocking waits in the worker, nothing to do here */
15691 		break;
15692 	case FENCE_FREE:
15693 		{
15694 			struct intel_atomic_helper *helper =
15695 				&to_i915(state->base.dev)->atomic_helper;
15696 
15697 			if (llist_add(&state->freed, &helper->free_list))
15698 				schedule_work(&helper->free_work);
15699 			break;
15700 		}
15701 	}
15702 
15703 	return NOTIFY_DONE;
15704 }
15705 
15706 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
15707 {
15708 	struct intel_plane_state *old_plane_state, *new_plane_state;
15709 	struct intel_plane *plane;
15710 	int i;
15711 
15712 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15713 					     new_plane_state, i)
15714 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
15715 					to_intel_frontbuffer(new_plane_state->hw.fb),
15716 					plane->frontbuffer_bit);
15717 }
15718 
15719 static void assert_global_state_locked(struct drm_i915_private *dev_priv)
15720 {
15721 	struct intel_crtc *crtc;
15722 
15723 	for_each_intel_crtc(&dev_priv->drm, crtc)
15724 		drm_modeset_lock_assert_held(&crtc->base.mutex);
15725 }
15726 
15727 static int intel_atomic_commit(struct drm_device *dev,
15728 			       struct drm_atomic_state *_state,
15729 			       bool nonblock)
15730 {
15731 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
15732 	struct drm_i915_private *dev_priv = to_i915(dev);
15733 	int ret = 0;
15734 
15735 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
15736 
15737 	drm_atomic_state_get(&state->base);
15738 	i915_sw_fence_init(&state->commit_ready,
15739 			   intel_atomic_commit_ready);
15740 
15741 	/*
15742 	 * The intel_legacy_cursor_update() fast path takes care
15743 	 * of avoiding the vblank waits for simple cursor
15744 	 * movement and flips. For cursor on/off and size changes,
15745 	 * we want to perform the vblank waits so that watermark
15746 	 * updates happen during the correct frames. Gen9+ have
15747 	 * double buffered watermarks and so shouldn't need this.
15748 	 *
15749 	 * Unset state->legacy_cursor_update before the call to
15750 	 * drm_atomic_helper_setup_commit() because otherwise
15751 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
15752 	 * we get FIFO underruns because we didn't wait
15753 	 * for vblank.
15754 	 *
15755 	 * FIXME doing watermarks and fb cleanup from a vblank worker
15756 	 * (assuming we had any) would solve these problems.
15757 	 */
15758 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
15759 		struct intel_crtc_state *new_crtc_state;
15760 		struct intel_crtc *crtc;
15761 		int i;
15762 
15763 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15764 			if (new_crtc_state->wm.need_postvbl_update ||
15765 			    new_crtc_state->update_wm_post)
15766 				state->base.legacy_cursor_update = false;
15767 	}
15768 
15769 	ret = intel_atomic_prepare_commit(state);
15770 	if (ret) {
15771 		drm_dbg_atomic(&dev_priv->drm,
15772 			       "Preparing state failed with %i\n", ret);
15773 		i915_sw_fence_commit(&state->commit_ready);
15774 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15775 		return ret;
15776 	}
15777 
15778 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
15779 	if (!ret)
15780 		ret = drm_atomic_helper_swap_state(&state->base, true);
15781 	if (!ret)
15782 		intel_atomic_swap_global_state(state);
15783 
15784 	if (ret) {
15785 		i915_sw_fence_commit(&state->commit_ready);
15786 
15787 		drm_atomic_helper_cleanup_planes(dev, &state->base);
15788 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15789 		return ret;
15790 	}
15791 	dev_priv->wm.distrust_bios_wm = false;
15792 	intel_shared_dpll_swap_state(state);
15793 	intel_atomic_track_fbs(state);
15794 
15795 	if (state->global_state_changed) {
15796 		assert_global_state_locked(dev_priv);
15797 
15798 		dev_priv->active_pipes = state->active_pipes;
15799 	}
15800 
15801 	drm_atomic_state_get(&state->base);
15802 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
15803 
15804 	i915_sw_fence_commit(&state->commit_ready);
15805 	if (nonblock && state->modeset) {
15806 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
15807 	} else if (nonblock) {
15808 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
15809 	} else {
15810 		if (state->modeset)
15811 			flush_workqueue(dev_priv->modeset_wq);
15812 		intel_atomic_commit_tail(state);
15813 	}
15814 
15815 	return 0;
15816 }
15817 
15818 struct wait_rps_boost {
15819 	struct wait_queue_entry wait;
15820 
15821 	struct drm_crtc *crtc;
15822 	struct i915_request *request;
15823 };
15824 
15825 static int do_rps_boost(struct wait_queue_entry *_wait,
15826 			unsigned mode, int sync, void *key)
15827 {
15828 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
15829 	struct i915_request *rq = wait->request;
15830 
15831 	/*
15832 	 * If we missed the vblank, but the request is already running it
15833 	 * is reasonable to assume that it will complete before the next
15834 	 * vblank without our intervention, so leave RPS alone.
15835 	 */
15836 	if (!i915_request_started(rq))
15837 		intel_rps_boost(rq);
15838 	i915_request_put(rq);
15839 
15840 	drm_crtc_vblank_put(wait->crtc);
15841 
15842 	list_del(&wait->wait.entry);
15843 	kfree(wait);
15844 	return 1;
15845 }
15846 
15847 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
15848 				       struct dma_fence *fence)
15849 {
15850 	struct wait_rps_boost *wait;
15851 
15852 	if (!dma_fence_is_i915(fence))
15853 		return;
15854 
15855 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
15856 		return;
15857 
15858 	if (drm_crtc_vblank_get(crtc))
15859 		return;
15860 
15861 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
15862 	if (!wait) {
15863 		drm_crtc_vblank_put(crtc);
15864 		return;
15865 	}
15866 
15867 	wait->request = to_request(dma_fence_get(fence));
15868 	wait->crtc = crtc;
15869 
15870 	wait->wait.func = do_rps_boost;
15871 	wait->wait.flags = 0;
15872 
15873 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
15874 }
15875 
15876 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
15877 {
15878 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
15879 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15880 	struct drm_framebuffer *fb = plane_state->hw.fb;
15881 	struct i915_vma *vma;
15882 
15883 	if (plane->id == PLANE_CURSOR &&
15884 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
15885 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
15886 		const int align = intel_cursor_alignment(dev_priv);
15887 		int err;
15888 
15889 		err = i915_gem_object_attach_phys(obj, align);
15890 		if (err)
15891 			return err;
15892 	}
15893 
15894 	vma = intel_pin_and_fence_fb_obj(fb,
15895 					 &plane_state->view,
15896 					 intel_plane_uses_fence(plane_state),
15897 					 &plane_state->flags);
15898 	if (IS_ERR(vma))
15899 		return PTR_ERR(vma);
15900 
15901 	plane_state->vma = vma;
15902 
15903 	return 0;
15904 }
15905 
15906 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
15907 {
15908 	struct i915_vma *vma;
15909 
15910 	vma = fetch_and_zero(&old_plane_state->vma);
15911 	if (vma)
15912 		intel_unpin_fb_vma(vma, old_plane_state->flags);
15913 }
15914 
15915 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
15916 {
15917 	struct i915_sched_attr attr = {
15918 		.priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
15919 	};
15920 
15921 	i915_gem_object_wait_priority(obj, 0, &attr);
15922 }
15923 
15924 /**
15925  * intel_prepare_plane_fb - Prepare fb for usage on plane
15926  * @_plane: drm plane to prepare for
15927  * @_new_plane_state: the plane state being prepared
15928  *
15929  * Prepares a framebuffer for usage on a display plane.  Generally this
15930  * involves pinning the underlying object and updating the frontbuffer tracking
15931  * bits.  Some older platforms need special physical address handling for
15932  * cursor planes.
15933  *
15934  * Returns 0 on success, negative error code on failure.
15935  */
15936 int
15937 intel_prepare_plane_fb(struct drm_plane *_plane,
15938 		       struct drm_plane_state *_new_plane_state)
15939 {
15940 	struct intel_plane *plane = to_intel_plane(_plane);
15941 	struct intel_plane_state *new_plane_state =
15942 		to_intel_plane_state(_new_plane_state);
15943 	struct intel_atomic_state *state =
15944 		to_intel_atomic_state(new_plane_state->uapi.state);
15945 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
15946 	const struct intel_plane_state *old_plane_state =
15947 		intel_atomic_get_old_plane_state(state, plane);
15948 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
15949 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
15950 	int ret;
15951 
15952 	if (old_obj) {
15953 		const struct intel_crtc_state *crtc_state =
15954 			intel_atomic_get_new_crtc_state(state,
15955 							to_intel_crtc(old_plane_state->hw.crtc));
15956 
15957 		/* Big Hammer, we also need to ensure that any pending
15958 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
15959 		 * current scanout is retired before unpinning the old
15960 		 * framebuffer. Note that we rely on userspace rendering
15961 		 * into the buffer attached to the pipe they are waiting
15962 		 * on. If not, userspace generates a GPU hang with IPEHR
15963 		 * point to the MI_WAIT_FOR_EVENT.
15964 		 *
15965 		 * This should only fail upon a hung GPU, in which case we
15966 		 * can safely continue.
15967 		 */
15968 		if (needs_modeset(crtc_state)) {
15969 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
15970 							      old_obj->base.resv, NULL,
15971 							      false, 0,
15972 							      GFP_KERNEL);
15973 			if (ret < 0)
15974 				return ret;
15975 		}
15976 	}
15977 
15978 	if (new_plane_state->uapi.fence) { /* explicit fencing */
15979 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
15980 						    new_plane_state->uapi.fence,
15981 						    I915_FENCE_TIMEOUT,
15982 						    GFP_KERNEL);
15983 		if (ret < 0)
15984 			return ret;
15985 	}
15986 
15987 	if (!obj)
15988 		return 0;
15989 
15990 	ret = i915_gem_object_pin_pages(obj);
15991 	if (ret)
15992 		return ret;
15993 
15994 	ret = intel_plane_pin_fb(new_plane_state);
15995 
15996 	i915_gem_object_unpin_pages(obj);
15997 	if (ret)
15998 		return ret;
15999 
16000 	fb_obj_bump_render_priority(obj);
16001 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
16002 
16003 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
16004 		struct dma_fence *fence;
16005 
16006 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
16007 						      obj->base.resv, NULL,
16008 						      false, I915_FENCE_TIMEOUT,
16009 						      GFP_KERNEL);
16010 		if (ret < 0)
16011 			goto unpin_fb;
16012 
16013 		fence = dma_resv_get_excl_rcu(obj->base.resv);
16014 		if (fence) {
16015 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16016 						   fence);
16017 			dma_fence_put(fence);
16018 		}
16019 	} else {
16020 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16021 					   new_plane_state->uapi.fence);
16022 	}
16023 
16024 	/*
16025 	 * We declare pageflips to be interactive and so merit a small bias
16026 	 * towards upclocking to deliver the frame on time. By only changing
16027 	 * the RPS thresholds to sample more regularly and aim for higher
16028 	 * clocks we can hopefully deliver low power workloads (like kodi)
16029 	 * that are not quite steady state without resorting to forcing
16030 	 * maximum clocks following a vblank miss (see do_rps_boost()).
16031 	 */
16032 	if (!state->rps_interactive) {
16033 		intel_rps_mark_interactive(&dev_priv->gt.rps, true);
16034 		state->rps_interactive = true;
16035 	}
16036 
16037 	return 0;
16038 
16039 unpin_fb:
16040 	intel_plane_unpin_fb(new_plane_state);
16041 
16042 	return ret;
16043 }
16044 
16045 /**
16046  * intel_cleanup_plane_fb - Cleans up an fb after plane use
16047  * @plane: drm plane to clean up for
16048  * @_old_plane_state: the state from the previous modeset
16049  *
16050  * Cleans up a framebuffer that has just been removed from a plane.
16051  */
16052 void
16053 intel_cleanup_plane_fb(struct drm_plane *plane,
16054 		       struct drm_plane_state *_old_plane_state)
16055 {
16056 	struct intel_plane_state *old_plane_state =
16057 		to_intel_plane_state(_old_plane_state);
16058 	struct intel_atomic_state *state =
16059 		to_intel_atomic_state(old_plane_state->uapi.state);
16060 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
16061 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16062 
16063 	if (!obj)
16064 		return;
16065 
16066 	if (state->rps_interactive) {
16067 		intel_rps_mark_interactive(&dev_priv->gt.rps, false);
16068 		state->rps_interactive = false;
16069 	}
16070 
16071 	/* Should only be called after a successful intel_prepare_plane_fb()! */
16072 	intel_plane_unpin_fb(old_plane_state);
16073 }
16074 
16075 /**
16076  * intel_plane_destroy - destroy a plane
16077  * @plane: plane to destroy
16078  *
16079  * Common destruction function for all types of planes (primary, cursor,
16080  * sprite).
16081  */
16082 void intel_plane_destroy(struct drm_plane *plane)
16083 {
16084 	drm_plane_cleanup(plane);
16085 	kfree(to_intel_plane(plane));
16086 }
16087 
16088 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16089 					    u32 format, u64 modifier)
16090 {
16091 	switch (modifier) {
16092 	case DRM_FORMAT_MOD_LINEAR:
16093 	case I915_FORMAT_MOD_X_TILED:
16094 		break;
16095 	default:
16096 		return false;
16097 	}
16098 
16099 	switch (format) {
16100 	case DRM_FORMAT_C8:
16101 	case DRM_FORMAT_RGB565:
16102 	case DRM_FORMAT_XRGB1555:
16103 	case DRM_FORMAT_XRGB8888:
16104 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16105 			modifier == I915_FORMAT_MOD_X_TILED;
16106 	default:
16107 		return false;
16108 	}
16109 }
16110 
16111 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16112 					    u32 format, u64 modifier)
16113 {
16114 	switch (modifier) {
16115 	case DRM_FORMAT_MOD_LINEAR:
16116 	case I915_FORMAT_MOD_X_TILED:
16117 		break;
16118 	default:
16119 		return false;
16120 	}
16121 
16122 	switch (format) {
16123 	case DRM_FORMAT_C8:
16124 	case DRM_FORMAT_RGB565:
16125 	case DRM_FORMAT_XRGB8888:
16126 	case DRM_FORMAT_XBGR8888:
16127 	case DRM_FORMAT_ARGB8888:
16128 	case DRM_FORMAT_ABGR8888:
16129 	case DRM_FORMAT_XRGB2101010:
16130 	case DRM_FORMAT_XBGR2101010:
16131 	case DRM_FORMAT_ARGB2101010:
16132 	case DRM_FORMAT_ABGR2101010:
16133 	case DRM_FORMAT_XBGR16161616F:
16134 		return modifier == DRM_FORMAT_MOD_LINEAR ||
16135 			modifier == I915_FORMAT_MOD_X_TILED;
16136 	default:
16137 		return false;
16138 	}
16139 }
16140 
16141 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16142 					      u32 format, u64 modifier)
16143 {
16144 	return modifier == DRM_FORMAT_MOD_LINEAR &&
16145 		format == DRM_FORMAT_ARGB8888;
16146 }
16147 
16148 static const struct drm_plane_funcs i965_plane_funcs = {
16149 	.update_plane = drm_atomic_helper_update_plane,
16150 	.disable_plane = drm_atomic_helper_disable_plane,
16151 	.destroy = intel_plane_destroy,
16152 	.atomic_duplicate_state = intel_plane_duplicate_state,
16153 	.atomic_destroy_state = intel_plane_destroy_state,
16154 	.format_mod_supported = i965_plane_format_mod_supported,
16155 };
16156 
16157 static const struct drm_plane_funcs i8xx_plane_funcs = {
16158 	.update_plane = drm_atomic_helper_update_plane,
16159 	.disable_plane = drm_atomic_helper_disable_plane,
16160 	.destroy = intel_plane_destroy,
16161 	.atomic_duplicate_state = intel_plane_duplicate_state,
16162 	.atomic_destroy_state = intel_plane_destroy_state,
16163 	.format_mod_supported = i8xx_plane_format_mod_supported,
16164 };
16165 
16166 static int
16167 intel_legacy_cursor_update(struct drm_plane *_plane,
16168 			   struct drm_crtc *_crtc,
16169 			   struct drm_framebuffer *fb,
16170 			   int crtc_x, int crtc_y,
16171 			   unsigned int crtc_w, unsigned int crtc_h,
16172 			   u32 src_x, u32 src_y,
16173 			   u32 src_w, u32 src_h,
16174 			   struct drm_modeset_acquire_ctx *ctx)
16175 {
16176 	struct intel_plane *plane = to_intel_plane(_plane);
16177 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
16178 	struct intel_plane_state *old_plane_state =
16179 		to_intel_plane_state(plane->base.state);
16180 	struct intel_plane_state *new_plane_state;
16181 	struct intel_crtc_state *crtc_state =
16182 		to_intel_crtc_state(crtc->base.state);
16183 	struct intel_crtc_state *new_crtc_state;
16184 	int ret;
16185 
16186 	/*
16187 	 * When crtc is inactive or there is a modeset pending,
16188 	 * wait for it to complete in the slowpath
16189 	 */
16190 	if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
16191 	    crtc_state->update_pipe)
16192 		goto slow;
16193 
16194 	/*
16195 	 * Don't do an async update if there is an outstanding commit modifying
16196 	 * the plane.  This prevents our async update's changes from getting
16197 	 * overridden by a previous synchronous update's state.
16198 	 */
16199 	if (old_plane_state->uapi.commit &&
16200 	    !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
16201 		goto slow;
16202 
16203 	/*
16204 	 * If any parameters change that may affect watermarks,
16205 	 * take the slowpath. Only changing fb or position should be
16206 	 * in the fastpath.
16207 	 */
16208 	if (old_plane_state->uapi.crtc != &crtc->base ||
16209 	    old_plane_state->uapi.src_w != src_w ||
16210 	    old_plane_state->uapi.src_h != src_h ||
16211 	    old_plane_state->uapi.crtc_w != crtc_w ||
16212 	    old_plane_state->uapi.crtc_h != crtc_h ||
16213 	    !old_plane_state->uapi.fb != !fb)
16214 		goto slow;
16215 
16216 	new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
16217 	if (!new_plane_state)
16218 		return -ENOMEM;
16219 
16220 	new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
16221 	if (!new_crtc_state) {
16222 		ret = -ENOMEM;
16223 		goto out_free;
16224 	}
16225 
16226 	drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
16227 
16228 	new_plane_state->uapi.src_x = src_x;
16229 	new_plane_state->uapi.src_y = src_y;
16230 	new_plane_state->uapi.src_w = src_w;
16231 	new_plane_state->uapi.src_h = src_h;
16232 	new_plane_state->uapi.crtc_x = crtc_x;
16233 	new_plane_state->uapi.crtc_y = crtc_y;
16234 	new_plane_state->uapi.crtc_w = crtc_w;
16235 	new_plane_state->uapi.crtc_h = crtc_h;
16236 
16237 	intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state);
16238 
16239 	ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
16240 						  old_plane_state, new_plane_state);
16241 	if (ret)
16242 		goto out_free;
16243 
16244 	ret = intel_plane_pin_fb(new_plane_state);
16245 	if (ret)
16246 		goto out_free;
16247 
16248 	intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16249 				ORIGIN_FLIP);
16250 	intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16251 				to_intel_frontbuffer(new_plane_state->hw.fb),
16252 				plane->frontbuffer_bit);
16253 
16254 	/* Swap plane state */
16255 	plane->base.state = &new_plane_state->uapi;
16256 
16257 	/*
16258 	 * We cannot swap crtc_state as it may be in use by an atomic commit or
16259 	 * page flip that's running simultaneously. If we swap crtc_state and
16260 	 * destroy the old state, we will cause a use-after-free there.
16261 	 *
16262 	 * Only update active_planes, which is needed for our internal
16263 	 * bookkeeping. Either value will do the right thing when updating
16264 	 * planes atomically. If the cursor was part of the atomic update then
16265 	 * we would have taken the slowpath.
16266 	 */
16267 	crtc_state->active_planes = new_crtc_state->active_planes;
16268 
16269 	if (new_plane_state->uapi.visible)
16270 		intel_update_plane(plane, crtc_state, new_plane_state);
16271 	else
16272 		intel_disable_plane(plane, crtc_state);
16273 
16274 	intel_plane_unpin_fb(old_plane_state);
16275 
16276 out_free:
16277 	if (new_crtc_state)
16278 		intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
16279 	if (ret)
16280 		intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
16281 	else
16282 		intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
16283 	return ret;
16284 
16285 slow:
16286 	return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
16287 					      crtc_x, crtc_y, crtc_w, crtc_h,
16288 					      src_x, src_y, src_w, src_h, ctx);
16289 }
16290 
16291 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
16292 	.update_plane = intel_legacy_cursor_update,
16293 	.disable_plane = drm_atomic_helper_disable_plane,
16294 	.destroy = intel_plane_destroy,
16295 	.atomic_duplicate_state = intel_plane_duplicate_state,
16296 	.atomic_destroy_state = intel_plane_destroy_state,
16297 	.format_mod_supported = intel_cursor_format_mod_supported,
16298 };
16299 
16300 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
16301 			       enum i9xx_plane_id i9xx_plane)
16302 {
16303 	if (!HAS_FBC(dev_priv))
16304 		return false;
16305 
16306 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16307 		return i9xx_plane == PLANE_A; /* tied to pipe A */
16308 	else if (IS_IVYBRIDGE(dev_priv))
16309 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
16310 			i9xx_plane == PLANE_C;
16311 	else if (INTEL_GEN(dev_priv) >= 4)
16312 		return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
16313 	else
16314 		return i9xx_plane == PLANE_A;
16315 }
16316 
16317 static struct intel_plane *
16318 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
16319 {
16320 	struct intel_plane *plane;
16321 	const struct drm_plane_funcs *plane_funcs;
16322 	unsigned int supported_rotations;
16323 	unsigned int possible_crtcs;
16324 	const u32 *formats;
16325 	int num_formats;
16326 	int ret, zpos;
16327 
16328 	if (INTEL_GEN(dev_priv) >= 9)
16329 		return skl_universal_plane_create(dev_priv, pipe,
16330 						  PLANE_PRIMARY);
16331 
16332 	plane = intel_plane_alloc();
16333 	if (IS_ERR(plane))
16334 		return plane;
16335 
16336 	plane->pipe = pipe;
16337 	/*
16338 	 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
16339 	 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
16340 	 */
16341 	if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
16342 		plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
16343 	else
16344 		plane->i9xx_plane = (enum i9xx_plane_id) pipe;
16345 	plane->id = PLANE_PRIMARY;
16346 	plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
16347 
16348 	plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
16349 	if (plane->has_fbc) {
16350 		struct intel_fbc *fbc = &dev_priv->fbc;
16351 
16352 		fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
16353 	}
16354 
16355 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16356 		formats = vlv_primary_formats;
16357 		num_formats = ARRAY_SIZE(vlv_primary_formats);
16358 	} else if (INTEL_GEN(dev_priv) >= 4) {
16359 		/*
16360 		 * WaFP16GammaEnabling:ivb
16361 		 * "Workaround : When using the 64-bit format, the plane
16362 		 *  output on each color channel has one quarter amplitude.
16363 		 *  It can be brought up to full amplitude by using pipe
16364 		 *  gamma correction or pipe color space conversion to
16365 		 *  multiply the plane output by four."
16366 		 *
16367 		 * There is no dedicated plane gamma for the primary plane,
16368 		 * and using the pipe gamma/csc could conflict with other
16369 		 * planes, so we choose not to expose fp16 on IVB primary
16370 		 * planes. HSW primary planes no longer have this problem.
16371 		 */
16372 		if (IS_IVYBRIDGE(dev_priv)) {
16373 			formats = ivb_primary_formats;
16374 			num_formats = ARRAY_SIZE(ivb_primary_formats);
16375 		} else {
16376 			formats = i965_primary_formats;
16377 			num_formats = ARRAY_SIZE(i965_primary_formats);
16378 		}
16379 	} else {
16380 		formats = i8xx_primary_formats;
16381 		num_formats = ARRAY_SIZE(i8xx_primary_formats);
16382 	}
16383 
16384 	if (INTEL_GEN(dev_priv) >= 4)
16385 		plane_funcs = &i965_plane_funcs;
16386 	else
16387 		plane_funcs = &i8xx_plane_funcs;
16388 
16389 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16390 		plane->min_cdclk = vlv_plane_min_cdclk;
16391 	else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16392 		plane->min_cdclk = hsw_plane_min_cdclk;
16393 	else if (IS_IVYBRIDGE(dev_priv))
16394 		plane->min_cdclk = ivb_plane_min_cdclk;
16395 	else
16396 		plane->min_cdclk = i9xx_plane_min_cdclk;
16397 
16398 	plane->max_stride = i9xx_plane_max_stride;
16399 	plane->update_plane = i9xx_update_plane;
16400 	plane->disable_plane = i9xx_disable_plane;
16401 	plane->get_hw_state = i9xx_plane_get_hw_state;
16402 	plane->check_plane = i9xx_plane_check;
16403 
16404 	possible_crtcs = BIT(pipe);
16405 
16406 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16407 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16408 					       possible_crtcs, plane_funcs,
16409 					       formats, num_formats,
16410 					       i9xx_format_modifiers,
16411 					       DRM_PLANE_TYPE_PRIMARY,
16412 					       "primary %c", pipe_name(pipe));
16413 	else
16414 		ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16415 					       possible_crtcs, plane_funcs,
16416 					       formats, num_formats,
16417 					       i9xx_format_modifiers,
16418 					       DRM_PLANE_TYPE_PRIMARY,
16419 					       "plane %c",
16420 					       plane_name(plane->i9xx_plane));
16421 	if (ret)
16422 		goto fail;
16423 
16424 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
16425 		supported_rotations =
16426 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
16427 			DRM_MODE_REFLECT_X;
16428 	} else if (INTEL_GEN(dev_priv) >= 4) {
16429 		supported_rotations =
16430 			DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
16431 	} else {
16432 		supported_rotations = DRM_MODE_ROTATE_0;
16433 	}
16434 
16435 	if (INTEL_GEN(dev_priv) >= 4)
16436 		drm_plane_create_rotation_property(&plane->base,
16437 						   DRM_MODE_ROTATE_0,
16438 						   supported_rotations);
16439 
16440 	zpos = 0;
16441 	drm_plane_create_zpos_immutable_property(&plane->base, zpos);
16442 
16443 	drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
16444 
16445 	return plane;
16446 
16447 fail:
16448 	intel_plane_free(plane);
16449 
16450 	return ERR_PTR(ret);
16451 }
16452 
16453 static struct intel_plane *
16454 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
16455 			  enum pipe pipe)
16456 {
16457 	unsigned int possible_crtcs;
16458 	struct intel_plane *cursor;
16459 	int ret, zpos;
16460 
16461 	cursor = intel_plane_alloc();
16462 	if (IS_ERR(cursor))
16463 		return cursor;
16464 
16465 	cursor->pipe = pipe;
16466 	cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
16467 	cursor->id = PLANE_CURSOR;
16468 	cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
16469 
16470 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16471 		cursor->max_stride = i845_cursor_max_stride;
16472 		cursor->update_plane = i845_update_cursor;
16473 		cursor->disable_plane = i845_disable_cursor;
16474 		cursor->get_hw_state = i845_cursor_get_hw_state;
16475 		cursor->check_plane = i845_check_cursor;
16476 	} else {
16477 		cursor->max_stride = i9xx_cursor_max_stride;
16478 		cursor->update_plane = i9xx_update_cursor;
16479 		cursor->disable_plane = i9xx_disable_cursor;
16480 		cursor->get_hw_state = i9xx_cursor_get_hw_state;
16481 		cursor->check_plane = i9xx_check_cursor;
16482 	}
16483 
16484 	cursor->cursor.base = ~0;
16485 	cursor->cursor.cntl = ~0;
16486 
16487 	if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
16488 		cursor->cursor.size = ~0;
16489 
16490 	possible_crtcs = BIT(pipe);
16491 
16492 	ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
16493 				       possible_crtcs, &intel_cursor_plane_funcs,
16494 				       intel_cursor_formats,
16495 				       ARRAY_SIZE(intel_cursor_formats),
16496 				       cursor_format_modifiers,
16497 				       DRM_PLANE_TYPE_CURSOR,
16498 				       "cursor %c", pipe_name(pipe));
16499 	if (ret)
16500 		goto fail;
16501 
16502 	if (INTEL_GEN(dev_priv) >= 4)
16503 		drm_plane_create_rotation_property(&cursor->base,
16504 						   DRM_MODE_ROTATE_0,
16505 						   DRM_MODE_ROTATE_0 |
16506 						   DRM_MODE_ROTATE_180);
16507 
16508 	zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
16509 	drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
16510 
16511 	drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
16512 
16513 	return cursor;
16514 
16515 fail:
16516 	intel_plane_free(cursor);
16517 
16518 	return ERR_PTR(ret);
16519 }
16520 
16521 #define INTEL_CRTC_FUNCS \
16522 	.gamma_set = drm_atomic_helper_legacy_gamma_set, \
16523 	.set_config = drm_atomic_helper_set_config, \
16524 	.destroy = intel_crtc_destroy, \
16525 	.page_flip = drm_atomic_helper_page_flip, \
16526 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
16527 	.atomic_destroy_state = intel_crtc_destroy_state, \
16528 	.set_crc_source = intel_crtc_set_crc_source, \
16529 	.verify_crc_source = intel_crtc_verify_crc_source, \
16530 	.get_crc_sources = intel_crtc_get_crc_sources
16531 
16532 static const struct drm_crtc_funcs bdw_crtc_funcs = {
16533 	INTEL_CRTC_FUNCS,
16534 
16535 	.get_vblank_counter = g4x_get_vblank_counter,
16536 	.enable_vblank = bdw_enable_vblank,
16537 	.disable_vblank = bdw_disable_vblank,
16538 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16539 };
16540 
16541 static const struct drm_crtc_funcs ilk_crtc_funcs = {
16542 	INTEL_CRTC_FUNCS,
16543 
16544 	.get_vblank_counter = g4x_get_vblank_counter,
16545 	.enable_vblank = ilk_enable_vblank,
16546 	.disable_vblank = ilk_disable_vblank,
16547 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16548 };
16549 
16550 static const struct drm_crtc_funcs g4x_crtc_funcs = {
16551 	INTEL_CRTC_FUNCS,
16552 
16553 	.get_vblank_counter = g4x_get_vblank_counter,
16554 	.enable_vblank = i965_enable_vblank,
16555 	.disable_vblank = i965_disable_vblank,
16556 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16557 };
16558 
16559 static const struct drm_crtc_funcs i965_crtc_funcs = {
16560 	INTEL_CRTC_FUNCS,
16561 
16562 	.get_vblank_counter = i915_get_vblank_counter,
16563 	.enable_vblank = i965_enable_vblank,
16564 	.disable_vblank = i965_disable_vblank,
16565 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16566 };
16567 
16568 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
16569 	INTEL_CRTC_FUNCS,
16570 
16571 	.get_vblank_counter = i915_get_vblank_counter,
16572 	.enable_vblank = i915gm_enable_vblank,
16573 	.disable_vblank = i915gm_disable_vblank,
16574 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16575 };
16576 
16577 static const struct drm_crtc_funcs i915_crtc_funcs = {
16578 	INTEL_CRTC_FUNCS,
16579 
16580 	.get_vblank_counter = i915_get_vblank_counter,
16581 	.enable_vblank = i8xx_enable_vblank,
16582 	.disable_vblank = i8xx_disable_vblank,
16583 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16584 };
16585 
16586 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
16587 	INTEL_CRTC_FUNCS,
16588 
16589 	/* no hw vblank counter */
16590 	.enable_vblank = i8xx_enable_vblank,
16591 	.disable_vblank = i8xx_disable_vblank,
16592 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16593 };
16594 
16595 static struct intel_crtc *intel_crtc_alloc(void)
16596 {
16597 	struct intel_crtc_state *crtc_state;
16598 	struct intel_crtc *crtc;
16599 
16600 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
16601 	if (!crtc)
16602 		return ERR_PTR(-ENOMEM);
16603 
16604 	crtc_state = intel_crtc_state_alloc(crtc);
16605 	if (!crtc_state) {
16606 		kfree(crtc);
16607 		return ERR_PTR(-ENOMEM);
16608 	}
16609 
16610 	crtc->base.state = &crtc_state->uapi;
16611 	crtc->config = crtc_state;
16612 
16613 	return crtc;
16614 }
16615 
16616 static void intel_crtc_free(struct intel_crtc *crtc)
16617 {
16618 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
16619 	kfree(crtc);
16620 }
16621 
16622 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
16623 {
16624 	struct intel_plane *primary, *cursor;
16625 	const struct drm_crtc_funcs *funcs;
16626 	struct intel_crtc *crtc;
16627 	int sprite, ret;
16628 
16629 	crtc = intel_crtc_alloc();
16630 	if (IS_ERR(crtc))
16631 		return PTR_ERR(crtc);
16632 
16633 	crtc->pipe = pipe;
16634 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
16635 
16636 	primary = intel_primary_plane_create(dev_priv, pipe);
16637 	if (IS_ERR(primary)) {
16638 		ret = PTR_ERR(primary);
16639 		goto fail;
16640 	}
16641 	crtc->plane_ids_mask |= BIT(primary->id);
16642 
16643 	for_each_sprite(dev_priv, pipe, sprite) {
16644 		struct intel_plane *plane;
16645 
16646 		plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
16647 		if (IS_ERR(plane)) {
16648 			ret = PTR_ERR(plane);
16649 			goto fail;
16650 		}
16651 		crtc->plane_ids_mask |= BIT(plane->id);
16652 	}
16653 
16654 	cursor = intel_cursor_plane_create(dev_priv, pipe);
16655 	if (IS_ERR(cursor)) {
16656 		ret = PTR_ERR(cursor);
16657 		goto fail;
16658 	}
16659 	crtc->plane_ids_mask |= BIT(cursor->id);
16660 
16661 	if (HAS_GMCH(dev_priv)) {
16662 		if (IS_CHERRYVIEW(dev_priv) ||
16663 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
16664 			funcs = &g4x_crtc_funcs;
16665 		else if (IS_GEN(dev_priv, 4))
16666 			funcs = &i965_crtc_funcs;
16667 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
16668 			funcs = &i915gm_crtc_funcs;
16669 		else if (IS_GEN(dev_priv, 3))
16670 			funcs = &i915_crtc_funcs;
16671 		else
16672 			funcs = &i8xx_crtc_funcs;
16673 	} else {
16674 		if (INTEL_GEN(dev_priv) >= 8)
16675 			funcs = &bdw_crtc_funcs;
16676 		else
16677 			funcs = &ilk_crtc_funcs;
16678 	}
16679 
16680 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
16681 					&primary->base, &cursor->base,
16682 					funcs, "pipe %c", pipe_name(pipe));
16683 	if (ret)
16684 		goto fail;
16685 
16686 	BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
16687 	       dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
16688 	dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
16689 
16690 	if (INTEL_GEN(dev_priv) < 9) {
16691 		enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
16692 
16693 		BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
16694 		       dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
16695 		dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
16696 	}
16697 
16698 	intel_color_init(crtc);
16699 
16700 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
16701 
16702 	return 0;
16703 
16704 fail:
16705 	intel_crtc_free(crtc);
16706 
16707 	return ret;
16708 }
16709 
16710 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
16711 				      struct drm_file *file)
16712 {
16713 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
16714 	struct drm_crtc *drmmode_crtc;
16715 	struct intel_crtc *crtc;
16716 
16717 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
16718 	if (!drmmode_crtc)
16719 		return -ENOENT;
16720 
16721 	crtc = to_intel_crtc(drmmode_crtc);
16722 	pipe_from_crtc_id->pipe = crtc->pipe;
16723 
16724 	return 0;
16725 }
16726 
16727 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
16728 {
16729 	struct drm_device *dev = encoder->base.dev;
16730 	struct intel_encoder *source_encoder;
16731 	u32 possible_clones = 0;
16732 
16733 	for_each_intel_encoder(dev, source_encoder) {
16734 		if (encoders_cloneable(encoder, source_encoder))
16735 			possible_clones |= drm_encoder_mask(&source_encoder->base);
16736 	}
16737 
16738 	return possible_clones;
16739 }
16740 
16741 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
16742 {
16743 	struct drm_device *dev = encoder->base.dev;
16744 	struct intel_crtc *crtc;
16745 	u32 possible_crtcs = 0;
16746 
16747 	for_each_intel_crtc(dev, crtc) {
16748 		if (encoder->pipe_mask & BIT(crtc->pipe))
16749 			possible_crtcs |= drm_crtc_mask(&crtc->base);
16750 	}
16751 
16752 	return possible_crtcs;
16753 }
16754 
16755 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
16756 {
16757 	if (!IS_MOBILE(dev_priv))
16758 		return false;
16759 
16760 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
16761 		return false;
16762 
16763 	if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
16764 		return false;
16765 
16766 	return true;
16767 }
16768 
16769 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
16770 {
16771 	if (INTEL_GEN(dev_priv) >= 9)
16772 		return false;
16773 
16774 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
16775 		return false;
16776 
16777 	if (HAS_PCH_LPT_H(dev_priv) &&
16778 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
16779 		return false;
16780 
16781 	/* DDI E can't be used if DDI A requires 4 lanes */
16782 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
16783 		return false;
16784 
16785 	if (!dev_priv->vbt.int_crt_support)
16786 		return false;
16787 
16788 	return true;
16789 }
16790 
16791 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
16792 {
16793 	int pps_num;
16794 	int pps_idx;
16795 
16796 	if (HAS_DDI(dev_priv))
16797 		return;
16798 	/*
16799 	 * This w/a is needed at least on CPT/PPT, but to be sure apply it
16800 	 * everywhere where registers can be write protected.
16801 	 */
16802 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16803 		pps_num = 2;
16804 	else
16805 		pps_num = 1;
16806 
16807 	for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
16808 		u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
16809 
16810 		val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
16811 		intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
16812 	}
16813 }
16814 
16815 static void intel_pps_init(struct drm_i915_private *dev_priv)
16816 {
16817 	if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
16818 		dev_priv->pps_mmio_base = PCH_PPS_BASE;
16819 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16820 		dev_priv->pps_mmio_base = VLV_PPS_BASE;
16821 	else
16822 		dev_priv->pps_mmio_base = PPS_BASE;
16823 
16824 	intel_pps_unlock_regs_wa(dev_priv);
16825 }
16826 
16827 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
16828 {
16829 	struct intel_encoder *encoder;
16830 	bool dpd_is_edp = false;
16831 
16832 	intel_pps_init(dev_priv);
16833 
16834 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
16835 		return;
16836 
16837 	if (INTEL_GEN(dev_priv) >= 12) {
16838 		intel_ddi_init(dev_priv, PORT_A);
16839 		intel_ddi_init(dev_priv, PORT_B);
16840 		intel_ddi_init(dev_priv, PORT_D);
16841 		intel_ddi_init(dev_priv, PORT_E);
16842 		intel_ddi_init(dev_priv, PORT_F);
16843 		intel_ddi_init(dev_priv, PORT_G);
16844 		intel_ddi_init(dev_priv, PORT_H);
16845 		intel_ddi_init(dev_priv, PORT_I);
16846 		icl_dsi_init(dev_priv);
16847 	} else if (IS_ELKHARTLAKE(dev_priv)) {
16848 		intel_ddi_init(dev_priv, PORT_A);
16849 		intel_ddi_init(dev_priv, PORT_B);
16850 		intel_ddi_init(dev_priv, PORT_C);
16851 		intel_ddi_init(dev_priv, PORT_D);
16852 		icl_dsi_init(dev_priv);
16853 	} else if (IS_GEN(dev_priv, 11)) {
16854 		intel_ddi_init(dev_priv, PORT_A);
16855 		intel_ddi_init(dev_priv, PORT_B);
16856 		intel_ddi_init(dev_priv, PORT_C);
16857 		intel_ddi_init(dev_priv, PORT_D);
16858 		intel_ddi_init(dev_priv, PORT_E);
16859 		/*
16860 		 * On some ICL SKUs port F is not present. No strap bits for
16861 		 * this, so rely on VBT.
16862 		 * Work around broken VBTs on SKUs known to have no port F.
16863 		 */
16864 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
16865 		    intel_bios_is_port_present(dev_priv, PORT_F))
16866 			intel_ddi_init(dev_priv, PORT_F);
16867 
16868 		icl_dsi_init(dev_priv);
16869 	} else if (IS_GEN9_LP(dev_priv)) {
16870 		/*
16871 		 * FIXME: Broxton doesn't support port detection via the
16872 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
16873 		 * detect the ports.
16874 		 */
16875 		intel_ddi_init(dev_priv, PORT_A);
16876 		intel_ddi_init(dev_priv, PORT_B);
16877 		intel_ddi_init(dev_priv, PORT_C);
16878 
16879 		vlv_dsi_init(dev_priv);
16880 	} else if (HAS_DDI(dev_priv)) {
16881 		int found;
16882 
16883 		if (intel_ddi_crt_present(dev_priv))
16884 			intel_crt_init(dev_priv);
16885 
16886 		/*
16887 		 * Haswell uses DDI functions to detect digital outputs.
16888 		 * On SKL pre-D0 the strap isn't connected, so we assume
16889 		 * it's there.
16890 		 */
16891 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
16892 		/* WaIgnoreDDIAStrap: skl */
16893 		if (found || IS_GEN9_BC(dev_priv))
16894 			intel_ddi_init(dev_priv, PORT_A);
16895 
16896 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
16897 		 * register */
16898 		found = intel_de_read(dev_priv, SFUSE_STRAP);
16899 
16900 		if (found & SFUSE_STRAP_DDIB_DETECTED)
16901 			intel_ddi_init(dev_priv, PORT_B);
16902 		if (found & SFUSE_STRAP_DDIC_DETECTED)
16903 			intel_ddi_init(dev_priv, PORT_C);
16904 		if (found & SFUSE_STRAP_DDID_DETECTED)
16905 			intel_ddi_init(dev_priv, PORT_D);
16906 		if (found & SFUSE_STRAP_DDIF_DETECTED)
16907 			intel_ddi_init(dev_priv, PORT_F);
16908 		/*
16909 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
16910 		 */
16911 		if (IS_GEN9_BC(dev_priv) &&
16912 		    intel_bios_is_port_present(dev_priv, PORT_E))
16913 			intel_ddi_init(dev_priv, PORT_E);
16914 
16915 	} else if (HAS_PCH_SPLIT(dev_priv)) {
16916 		int found;
16917 
16918 		/*
16919 		 * intel_edp_init_connector() depends on this completing first,
16920 		 * to prevent the registration of both eDP and LVDS and the
16921 		 * incorrect sharing of the PPS.
16922 		 */
16923 		intel_lvds_init(dev_priv);
16924 		intel_crt_init(dev_priv);
16925 
16926 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
16927 
16928 		if (ilk_has_edp_a(dev_priv))
16929 			intel_dp_init(dev_priv, DP_A, PORT_A);
16930 
16931 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
16932 			/* PCH SDVOB multiplex with HDMIB */
16933 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
16934 			if (!found)
16935 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
16936 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
16937 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
16938 		}
16939 
16940 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
16941 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
16942 
16943 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
16944 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
16945 
16946 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
16947 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
16948 
16949 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
16950 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
16951 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16952 		bool has_edp, has_port;
16953 
16954 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
16955 			intel_crt_init(dev_priv);
16956 
16957 		/*
16958 		 * The DP_DETECTED bit is the latched state of the DDC
16959 		 * SDA pin at boot. However since eDP doesn't require DDC
16960 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
16961 		 * eDP ports may have been muxed to an alternate function.
16962 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
16963 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
16964 		 * detect eDP ports.
16965 		 *
16966 		 * Sadly the straps seem to be missing sometimes even for HDMI
16967 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
16968 		 * and VBT for the presence of the port. Additionally we can't
16969 		 * trust the port type the VBT declares as we've seen at least
16970 		 * HDMI ports that the VBT claim are DP or eDP.
16971 		 */
16972 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
16973 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
16974 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
16975 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
16976 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
16977 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
16978 
16979 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
16980 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
16981 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
16982 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
16983 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
16984 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
16985 
16986 		if (IS_CHERRYVIEW(dev_priv)) {
16987 			/*
16988 			 * eDP not supported on port D,
16989 			 * so no need to worry about it
16990 			 */
16991 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
16992 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
16993 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
16994 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
16995 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
16996 		}
16997 
16998 		vlv_dsi_init(dev_priv);
16999 	} else if (IS_PINEVIEW(dev_priv)) {
17000 		intel_lvds_init(dev_priv);
17001 		intel_crt_init(dev_priv);
17002 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
17003 		bool found = false;
17004 
17005 		if (IS_MOBILE(dev_priv))
17006 			intel_lvds_init(dev_priv);
17007 
17008 		intel_crt_init(dev_priv);
17009 
17010 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17011 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
17012 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
17013 			if (!found && IS_G4X(dev_priv)) {
17014 				drm_dbg_kms(&dev_priv->drm,
17015 					    "probing HDMI on SDVOB\n");
17016 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
17017 			}
17018 
17019 			if (!found && IS_G4X(dev_priv))
17020 				intel_dp_init(dev_priv, DP_B, PORT_B);
17021 		}
17022 
17023 		/* Before G4X SDVOC doesn't have its own detect register */
17024 
17025 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17026 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
17027 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
17028 		}
17029 
17030 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
17031 
17032 			if (IS_G4X(dev_priv)) {
17033 				drm_dbg_kms(&dev_priv->drm,
17034 					    "probing HDMI on SDVOC\n");
17035 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
17036 			}
17037 			if (IS_G4X(dev_priv))
17038 				intel_dp_init(dev_priv, DP_C, PORT_C);
17039 		}
17040 
17041 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
17042 			intel_dp_init(dev_priv, DP_D, PORT_D);
17043 
17044 		if (SUPPORTS_TV(dev_priv))
17045 			intel_tv_init(dev_priv);
17046 	} else if (IS_GEN(dev_priv, 2)) {
17047 		if (IS_I85X(dev_priv))
17048 			intel_lvds_init(dev_priv);
17049 
17050 		intel_crt_init(dev_priv);
17051 		intel_dvo_init(dev_priv);
17052 	}
17053 
17054 	intel_psr_init(dev_priv);
17055 
17056 	for_each_intel_encoder(&dev_priv->drm, encoder) {
17057 		encoder->base.possible_crtcs =
17058 			intel_encoder_possible_crtcs(encoder);
17059 		encoder->base.possible_clones =
17060 			intel_encoder_possible_clones(encoder);
17061 	}
17062 
17063 	intel_init_pch_refclk(dev_priv);
17064 
17065 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
17066 }
17067 
17068 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17069 {
17070 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
17071 
17072 	drm_framebuffer_cleanup(fb);
17073 	intel_frontbuffer_put(intel_fb->frontbuffer);
17074 
17075 	kfree(intel_fb);
17076 }
17077 
17078 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
17079 						struct drm_file *file,
17080 						unsigned int *handle)
17081 {
17082 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17083 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
17084 
17085 	if (obj->userptr.mm) {
17086 		drm_dbg(&i915->drm,
17087 			"attempting to use a userptr for a framebuffer, denied\n");
17088 		return -EINVAL;
17089 	}
17090 
17091 	return drm_gem_handle_create(file, &obj->base, handle);
17092 }
17093 
17094 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17095 					struct drm_file *file,
17096 					unsigned flags, unsigned color,
17097 					struct drm_clip_rect *clips,
17098 					unsigned num_clips)
17099 {
17100 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17101 
17102 	i915_gem_object_flush_if_display(obj);
17103 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
17104 
17105 	return 0;
17106 }
17107 
17108 static const struct drm_framebuffer_funcs intel_fb_funcs = {
17109 	.destroy = intel_user_framebuffer_destroy,
17110 	.create_handle = intel_user_framebuffer_create_handle,
17111 	.dirty = intel_user_framebuffer_dirty,
17112 };
17113 
17114 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17115 				  struct drm_i915_gem_object *obj,
17116 				  struct drm_mode_fb_cmd2 *mode_cmd)
17117 {
17118 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
17119 	struct drm_framebuffer *fb = &intel_fb->base;
17120 	u32 max_stride;
17121 	unsigned int tiling, stride;
17122 	int ret = -EINVAL;
17123 	int i;
17124 
17125 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17126 	if (!intel_fb->frontbuffer)
17127 		return -ENOMEM;
17128 
17129 	i915_gem_object_lock(obj);
17130 	tiling = i915_gem_object_get_tiling(obj);
17131 	stride = i915_gem_object_get_stride(obj);
17132 	i915_gem_object_unlock(obj);
17133 
17134 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
17135 		/*
17136 		 * If there's a fence, enforce that
17137 		 * the fb modifier and tiling mode match.
17138 		 */
17139 		if (tiling != I915_TILING_NONE &&
17140 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17141 			drm_dbg_kms(&dev_priv->drm,
17142 				    "tiling_mode doesn't match fb modifier\n");
17143 			goto err;
17144 		}
17145 	} else {
17146 		if (tiling == I915_TILING_X) {
17147 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
17148 		} else if (tiling == I915_TILING_Y) {
17149 			drm_dbg_kms(&dev_priv->drm,
17150 				    "No Y tiling for legacy addfb\n");
17151 			goto err;
17152 		}
17153 	}
17154 
17155 	if (!drm_any_plane_has_format(&dev_priv->drm,
17156 				      mode_cmd->pixel_format,
17157 				      mode_cmd->modifier[0])) {
17158 		struct drm_format_name_buf format_name;
17159 
17160 		drm_dbg_kms(&dev_priv->drm,
17161 			    "unsupported pixel format %s / modifier 0x%llx\n",
17162 			    drm_get_format_name(mode_cmd->pixel_format,
17163 						&format_name),
17164 			    mode_cmd->modifier[0]);
17165 		goto err;
17166 	}
17167 
17168 	/*
17169 	 * gen2/3 display engine uses the fence if present,
17170 	 * so the tiling mode must match the fb modifier exactly.
17171 	 */
17172 	if (INTEL_GEN(dev_priv) < 4 &&
17173 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17174 		drm_dbg_kms(&dev_priv->drm,
17175 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
17176 		goto err;
17177 	}
17178 
17179 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17180 					 mode_cmd->modifier[0]);
17181 	if (mode_cmd->pitches[0] > max_stride) {
17182 		drm_dbg_kms(&dev_priv->drm,
17183 			    "%s pitch (%u) must be at most %d\n",
17184 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17185 			    "tiled" : "linear",
17186 			    mode_cmd->pitches[0], max_stride);
17187 		goto err;
17188 	}
17189 
17190 	/*
17191 	 * If there's a fence, enforce that
17192 	 * the fb pitch and fence stride match.
17193 	 */
17194 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
17195 		drm_dbg_kms(&dev_priv->drm,
17196 			    "pitch (%d) must match tiling stride (%d)\n",
17197 			    mode_cmd->pitches[0], stride);
17198 		goto err;
17199 	}
17200 
17201 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
17202 	if (mode_cmd->offsets[0] != 0) {
17203 		drm_dbg_kms(&dev_priv->drm,
17204 			    "plane 0 offset (0x%08x) must be 0\n",
17205 			    mode_cmd->offsets[0]);
17206 		goto err;
17207 	}
17208 
17209 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
17210 
17211 	for (i = 0; i < fb->format->num_planes; i++) {
17212 		u32 stride_alignment;
17213 
17214 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
17215 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17216 				    i);
17217 			goto err;
17218 		}
17219 
17220 		stride_alignment = intel_fb_stride_alignment(fb, i);
17221 		if (fb->pitches[i] & (stride_alignment - 1)) {
17222 			drm_dbg_kms(&dev_priv->drm,
17223 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
17224 				    i, fb->pitches[i], stride_alignment);
17225 			goto err;
17226 		}
17227 
17228 		if (is_gen12_ccs_plane(fb, i)) {
17229 			int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17230 
17231 			if (fb->pitches[i] != ccs_aux_stride) {
17232 				drm_dbg_kms(&dev_priv->drm,
17233 					    "ccs aux plane %d pitch (%d) must be %d\n",
17234 					    i,
17235 					    fb->pitches[i], ccs_aux_stride);
17236 				goto err;
17237 			}
17238 		}
17239 
17240 		fb->obj[i] = &obj->base;
17241 	}
17242 
17243 	ret = intel_fill_fb_info(dev_priv, fb);
17244 	if (ret)
17245 		goto err;
17246 
17247 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
17248 	if (ret) {
17249 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
17250 		goto err;
17251 	}
17252 
17253 	return 0;
17254 
17255 err:
17256 	intel_frontbuffer_put(intel_fb->frontbuffer);
17257 	return ret;
17258 }
17259 
17260 static struct drm_framebuffer *
17261 intel_user_framebuffer_create(struct drm_device *dev,
17262 			      struct drm_file *filp,
17263 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
17264 {
17265 	struct drm_framebuffer *fb;
17266 	struct drm_i915_gem_object *obj;
17267 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
17268 
17269 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
17270 	if (!obj)
17271 		return ERR_PTR(-ENOENT);
17272 
17273 	fb = intel_framebuffer_create(obj, &mode_cmd);
17274 	i915_gem_object_put(obj);
17275 
17276 	return fb;
17277 }
17278 
17279 static enum drm_mode_status
17280 intel_mode_valid(struct drm_device *dev,
17281 		 const struct drm_display_mode *mode)
17282 {
17283 	struct drm_i915_private *dev_priv = to_i915(dev);
17284 	int hdisplay_max, htotal_max;
17285 	int vdisplay_max, vtotal_max;
17286 
17287 	/*
17288 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
17289 	 * of DBLSCAN modes to the output's mode list when they detect
17290 	 * the scaling mode property on the connector. And they don't
17291 	 * ask the kernel to validate those modes in any way until
17292 	 * modeset time at which point the client gets a protocol error.
17293 	 * So in order to not upset those clients we silently ignore the
17294 	 * DBLSCAN flag on such connectors. For other connectors we will
17295 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
17296 	 * And we always reject DBLSCAN modes in connector->mode_valid()
17297 	 * as we never want such modes on the connector's mode list.
17298 	 */
17299 
17300 	if (mode->vscan > 1)
17301 		return MODE_NO_VSCAN;
17302 
17303 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
17304 		return MODE_H_ILLEGAL;
17305 
17306 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
17307 			   DRM_MODE_FLAG_NCSYNC |
17308 			   DRM_MODE_FLAG_PCSYNC))
17309 		return MODE_HSYNC;
17310 
17311 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
17312 			   DRM_MODE_FLAG_PIXMUX |
17313 			   DRM_MODE_FLAG_CLKDIV2))
17314 		return MODE_BAD;
17315 
17316 	/* Transcoder timing limits */
17317 	if (INTEL_GEN(dev_priv) >= 11) {
17318 		hdisplay_max = 16384;
17319 		vdisplay_max = 8192;
17320 		htotal_max = 16384;
17321 		vtotal_max = 8192;
17322 	} else if (INTEL_GEN(dev_priv) >= 9 ||
17323 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17324 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
17325 		vdisplay_max = 4096;
17326 		htotal_max = 8192;
17327 		vtotal_max = 8192;
17328 	} else if (INTEL_GEN(dev_priv) >= 3) {
17329 		hdisplay_max = 4096;
17330 		vdisplay_max = 4096;
17331 		htotal_max = 8192;
17332 		vtotal_max = 8192;
17333 	} else {
17334 		hdisplay_max = 2048;
17335 		vdisplay_max = 2048;
17336 		htotal_max = 4096;
17337 		vtotal_max = 4096;
17338 	}
17339 
17340 	if (mode->hdisplay > hdisplay_max ||
17341 	    mode->hsync_start > htotal_max ||
17342 	    mode->hsync_end > htotal_max ||
17343 	    mode->htotal > htotal_max)
17344 		return MODE_H_ILLEGAL;
17345 
17346 	if (mode->vdisplay > vdisplay_max ||
17347 	    mode->vsync_start > vtotal_max ||
17348 	    mode->vsync_end > vtotal_max ||
17349 	    mode->vtotal > vtotal_max)
17350 		return MODE_V_ILLEGAL;
17351 
17352 	if (INTEL_GEN(dev_priv) >= 5) {
17353 		if (mode->hdisplay < 64 ||
17354 		    mode->htotal - mode->hdisplay < 32)
17355 			return MODE_H_ILLEGAL;
17356 
17357 		if (mode->vtotal - mode->vdisplay < 5)
17358 			return MODE_V_ILLEGAL;
17359 	} else {
17360 		if (mode->htotal - mode->hdisplay < 32)
17361 			return MODE_H_ILLEGAL;
17362 
17363 		if (mode->vtotal - mode->vdisplay < 3)
17364 			return MODE_V_ILLEGAL;
17365 	}
17366 
17367 	return MODE_OK;
17368 }
17369 
17370 enum drm_mode_status
17371 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
17372 				const struct drm_display_mode *mode)
17373 {
17374 	int plane_width_max, plane_height_max;
17375 
17376 	/*
17377 	 * intel_mode_valid() should be
17378 	 * sufficient on older platforms.
17379 	 */
17380 	if (INTEL_GEN(dev_priv) < 9)
17381 		return MODE_OK;
17382 
17383 	/*
17384 	 * Most people will probably want a fullscreen
17385 	 * plane so let's not advertize modes that are
17386 	 * too big for that.
17387 	 */
17388 	if (INTEL_GEN(dev_priv) >= 11) {
17389 		plane_width_max = 5120;
17390 		plane_height_max = 4320;
17391 	} else {
17392 		plane_width_max = 5120;
17393 		plane_height_max = 4096;
17394 	}
17395 
17396 	if (mode->hdisplay > plane_width_max)
17397 		return MODE_H_ILLEGAL;
17398 
17399 	if (mode->vdisplay > plane_height_max)
17400 		return MODE_V_ILLEGAL;
17401 
17402 	return MODE_OK;
17403 }
17404 
17405 static const struct drm_mode_config_funcs intel_mode_funcs = {
17406 	.fb_create = intel_user_framebuffer_create,
17407 	.get_format_info = intel_get_format_info,
17408 	.output_poll_changed = intel_fbdev_output_poll_changed,
17409 	.mode_valid = intel_mode_valid,
17410 	.atomic_check = intel_atomic_check,
17411 	.atomic_commit = intel_atomic_commit,
17412 	.atomic_state_alloc = intel_atomic_state_alloc,
17413 	.atomic_state_clear = intel_atomic_state_clear,
17414 	.atomic_state_free = intel_atomic_state_free,
17415 };
17416 
17417 /**
17418  * intel_init_display_hooks - initialize the display modesetting hooks
17419  * @dev_priv: device private
17420  */
17421 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
17422 {
17423 	intel_init_cdclk_hooks(dev_priv);
17424 
17425 	if (INTEL_GEN(dev_priv) >= 9) {
17426 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17427 		dev_priv->display.get_initial_plane_config =
17428 			skl_get_initial_plane_config;
17429 		dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
17430 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17431 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17432 	} else if (HAS_DDI(dev_priv)) {
17433 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17434 		dev_priv->display.get_initial_plane_config =
17435 			i9xx_get_initial_plane_config;
17436 		dev_priv->display.crtc_compute_clock =
17437 			hsw_crtc_compute_clock;
17438 		dev_priv->display.crtc_enable = hsw_crtc_enable;
17439 		dev_priv->display.crtc_disable = hsw_crtc_disable;
17440 	} else if (HAS_PCH_SPLIT(dev_priv)) {
17441 		dev_priv->display.get_pipe_config = ilk_get_pipe_config;
17442 		dev_priv->display.get_initial_plane_config =
17443 			i9xx_get_initial_plane_config;
17444 		dev_priv->display.crtc_compute_clock =
17445 			ilk_crtc_compute_clock;
17446 		dev_priv->display.crtc_enable = ilk_crtc_enable;
17447 		dev_priv->display.crtc_disable = ilk_crtc_disable;
17448 	} else if (IS_CHERRYVIEW(dev_priv)) {
17449 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17450 		dev_priv->display.get_initial_plane_config =
17451 			i9xx_get_initial_plane_config;
17452 		dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
17453 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17454 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17455 	} else if (IS_VALLEYVIEW(dev_priv)) {
17456 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17457 		dev_priv->display.get_initial_plane_config =
17458 			i9xx_get_initial_plane_config;
17459 		dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
17460 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
17461 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17462 	} else if (IS_G4X(dev_priv)) {
17463 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17464 		dev_priv->display.get_initial_plane_config =
17465 			i9xx_get_initial_plane_config;
17466 		dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
17467 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17468 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17469 	} else if (IS_PINEVIEW(dev_priv)) {
17470 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17471 		dev_priv->display.get_initial_plane_config =
17472 			i9xx_get_initial_plane_config;
17473 		dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
17474 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17475 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17476 	} else if (!IS_GEN(dev_priv, 2)) {
17477 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17478 		dev_priv->display.get_initial_plane_config =
17479 			i9xx_get_initial_plane_config;
17480 		dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
17481 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17482 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17483 	} else {
17484 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17485 		dev_priv->display.get_initial_plane_config =
17486 			i9xx_get_initial_plane_config;
17487 		dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
17488 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
17489 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
17490 	}
17491 
17492 	if (IS_GEN(dev_priv, 5)) {
17493 		dev_priv->display.fdi_link_train = ilk_fdi_link_train;
17494 	} else if (IS_GEN(dev_priv, 6)) {
17495 		dev_priv->display.fdi_link_train = gen6_fdi_link_train;
17496 	} else if (IS_IVYBRIDGE(dev_priv)) {
17497 		/* FIXME: detect B0+ stepping and use auto training */
17498 		dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
17499 	}
17500 
17501 	if (INTEL_GEN(dev_priv) >= 9)
17502 		dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
17503 	else
17504 		dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
17505 
17506 }
17507 
17508 void intel_modeset_init_hw(struct drm_i915_private *i915)
17509 {
17510 	struct intel_cdclk_state *cdclk_state =
17511 		to_intel_cdclk_state(i915->cdclk.obj.state);
17512 
17513 	intel_update_cdclk(i915);
17514 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
17515 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
17516 }
17517 
17518 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
17519 {
17520 	struct drm_plane *plane;
17521 	struct drm_crtc *crtc;
17522 
17523 	drm_for_each_crtc(crtc, state->dev) {
17524 		struct drm_crtc_state *crtc_state;
17525 
17526 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
17527 		if (IS_ERR(crtc_state))
17528 			return PTR_ERR(crtc_state);
17529 	}
17530 
17531 	drm_for_each_plane(plane, state->dev) {
17532 		struct drm_plane_state *plane_state;
17533 
17534 		plane_state = drm_atomic_get_plane_state(state, plane);
17535 		if (IS_ERR(plane_state))
17536 			return PTR_ERR(plane_state);
17537 	}
17538 
17539 	return 0;
17540 }
17541 
17542 /*
17543  * Calculate what we think the watermarks should be for the state we've read
17544  * out of the hardware and then immediately program those watermarks so that
17545  * we ensure the hardware settings match our internal state.
17546  *
17547  * We can calculate what we think WM's should be by creating a duplicate of the
17548  * current state (which was constructed during hardware readout) and running it
17549  * through the atomic check code to calculate new watermark values in the
17550  * state object.
17551  */
17552 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
17553 {
17554 	struct drm_atomic_state *state;
17555 	struct intel_atomic_state *intel_state;
17556 	struct intel_crtc *crtc;
17557 	struct intel_crtc_state *crtc_state;
17558 	struct drm_modeset_acquire_ctx ctx;
17559 	int ret;
17560 	int i;
17561 
17562 	/* Only supported on platforms that use atomic watermark design */
17563 	if (!dev_priv->display.optimize_watermarks)
17564 		return;
17565 
17566 	state = drm_atomic_state_alloc(&dev_priv->drm);
17567 	if (drm_WARN_ON(&dev_priv->drm, !state))
17568 		return;
17569 
17570 	intel_state = to_intel_atomic_state(state);
17571 
17572 	drm_modeset_acquire_init(&ctx, 0);
17573 
17574 retry:
17575 	state->acquire_ctx = &ctx;
17576 
17577 	/*
17578 	 * Hardware readout is the only time we don't want to calculate
17579 	 * intermediate watermarks (since we don't trust the current
17580 	 * watermarks).
17581 	 */
17582 	if (!HAS_GMCH(dev_priv))
17583 		intel_state->skip_intermediate_wm = true;
17584 
17585 	ret = sanitize_watermarks_add_affected(state);
17586 	if (ret)
17587 		goto fail;
17588 
17589 	ret = intel_atomic_check(&dev_priv->drm, state);
17590 	if (ret)
17591 		goto fail;
17592 
17593 	/* Write calculated watermark values back */
17594 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
17595 		crtc_state->wm.need_postvbl_update = true;
17596 		dev_priv->display.optimize_watermarks(intel_state, crtc);
17597 
17598 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
17599 	}
17600 
17601 fail:
17602 	if (ret == -EDEADLK) {
17603 		drm_atomic_state_clear(state);
17604 		drm_modeset_backoff(&ctx);
17605 		goto retry;
17606 	}
17607 
17608 	/*
17609 	 * If we fail here, it means that the hardware appears to be
17610 	 * programmed in a way that shouldn't be possible, given our
17611 	 * understanding of watermark requirements.  This might mean a
17612 	 * mistake in the hardware readout code or a mistake in the
17613 	 * watermark calculations for a given platform.  Raise a WARN
17614 	 * so that this is noticeable.
17615 	 *
17616 	 * If this actually happens, we'll have to just leave the
17617 	 * BIOS-programmed watermarks untouched and hope for the best.
17618 	 */
17619 	drm_WARN(&dev_priv->drm, ret,
17620 		 "Could not determine valid watermarks for inherited state\n");
17621 
17622 	drm_atomic_state_put(state);
17623 
17624 	drm_modeset_drop_locks(&ctx);
17625 	drm_modeset_acquire_fini(&ctx);
17626 }
17627 
17628 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
17629 {
17630 	if (IS_GEN(dev_priv, 5)) {
17631 		u32 fdi_pll_clk =
17632 			intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
17633 
17634 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
17635 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
17636 		dev_priv->fdi_pll_freq = 270000;
17637 	} else {
17638 		return;
17639 	}
17640 
17641 	drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
17642 }
17643 
17644 static int intel_initial_commit(struct drm_device *dev)
17645 {
17646 	struct drm_atomic_state *state = NULL;
17647 	struct drm_modeset_acquire_ctx ctx;
17648 	struct intel_crtc *crtc;
17649 	int ret = 0;
17650 
17651 	state = drm_atomic_state_alloc(dev);
17652 	if (!state)
17653 		return -ENOMEM;
17654 
17655 	drm_modeset_acquire_init(&ctx, 0);
17656 
17657 retry:
17658 	state->acquire_ctx = &ctx;
17659 
17660 	for_each_intel_crtc(dev, crtc) {
17661 		struct intel_crtc_state *crtc_state =
17662 			intel_atomic_get_crtc_state(state, crtc);
17663 
17664 		if (IS_ERR(crtc_state)) {
17665 			ret = PTR_ERR(crtc_state);
17666 			goto out;
17667 		}
17668 
17669 		if (crtc_state->hw.active) {
17670 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
17671 			if (ret)
17672 				goto out;
17673 
17674 			/*
17675 			 * FIXME hack to force a LUT update to avoid the
17676 			 * plane update forcing the pipe gamma on without
17677 			 * having a proper LUT loaded. Remove once we
17678 			 * have readout for pipe gamma enable.
17679 			 */
17680 			crtc_state->uapi.color_mgmt_changed = true;
17681 
17682 			/*
17683 			 * FIXME hack to force full modeset when DSC is being
17684 			 * used.
17685 			 *
17686 			 * As long as we do not have full state readout and
17687 			 * config comparison of crtc_state->dsc, we have no way
17688 			 * to ensure reliable fastset. Remove once we have
17689 			 * readout for DSC.
17690 			 */
17691 			if (crtc_state->dsc.compression_enable) {
17692 				ret = drm_atomic_add_affected_connectors(state,
17693 									 &crtc->base);
17694 				if (ret)
17695 					goto out;
17696 				crtc_state->uapi.mode_changed = true;
17697 				drm_dbg_kms(dev, "Force full modeset for DSC\n");
17698 			}
17699 		}
17700 	}
17701 
17702 	ret = drm_atomic_commit(state);
17703 
17704 out:
17705 	if (ret == -EDEADLK) {
17706 		drm_atomic_state_clear(state);
17707 		drm_modeset_backoff(&ctx);
17708 		goto retry;
17709 	}
17710 
17711 	drm_atomic_state_put(state);
17712 
17713 	drm_modeset_drop_locks(&ctx);
17714 	drm_modeset_acquire_fini(&ctx);
17715 
17716 	return ret;
17717 }
17718 
17719 static void intel_mode_config_init(struct drm_i915_private *i915)
17720 {
17721 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
17722 
17723 	drm_mode_config_init(&i915->drm);
17724 	INIT_LIST_HEAD(&i915->global_obj_list);
17725 
17726 	mode_config->min_width = 0;
17727 	mode_config->min_height = 0;
17728 
17729 	mode_config->preferred_depth = 24;
17730 	mode_config->prefer_shadow = 1;
17731 
17732 	mode_config->allow_fb_modifiers = true;
17733 
17734 	mode_config->funcs = &intel_mode_funcs;
17735 
17736 	/*
17737 	 * Maximum framebuffer dimensions, chosen to match
17738 	 * the maximum render engine surface size on gen4+.
17739 	 */
17740 	if (INTEL_GEN(i915) >= 7) {
17741 		mode_config->max_width = 16384;
17742 		mode_config->max_height = 16384;
17743 	} else if (INTEL_GEN(i915) >= 4) {
17744 		mode_config->max_width = 8192;
17745 		mode_config->max_height = 8192;
17746 	} else if (IS_GEN(i915, 3)) {
17747 		mode_config->max_width = 4096;
17748 		mode_config->max_height = 4096;
17749 	} else {
17750 		mode_config->max_width = 2048;
17751 		mode_config->max_height = 2048;
17752 	}
17753 
17754 	if (IS_I845G(i915) || IS_I865G(i915)) {
17755 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
17756 		mode_config->cursor_height = 1023;
17757 	} else if (IS_GEN(i915, 2)) {
17758 		mode_config->cursor_width = 64;
17759 		mode_config->cursor_height = 64;
17760 	} else {
17761 		mode_config->cursor_width = 256;
17762 		mode_config->cursor_height = 256;
17763 	}
17764 }
17765 
17766 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
17767 {
17768 	intel_atomic_global_obj_cleanup(i915);
17769 	drm_mode_config_cleanup(&i915->drm);
17770 }
17771 
17772 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
17773 {
17774 	if (plane_config->fb) {
17775 		struct drm_framebuffer *fb = &plane_config->fb->base;
17776 
17777 		/* We may only have the stub and not a full framebuffer */
17778 		if (drm_framebuffer_read_refcount(fb))
17779 			drm_framebuffer_put(fb);
17780 		else
17781 			kfree(fb);
17782 	}
17783 
17784 	if (plane_config->vma)
17785 		i915_vma_put(plane_config->vma);
17786 }
17787 
17788 int intel_modeset_init(struct drm_i915_private *i915)
17789 {
17790 	struct drm_device *dev = &i915->drm;
17791 	enum pipe pipe;
17792 	struct intel_crtc *crtc;
17793 	int ret;
17794 
17795 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
17796 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
17797 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
17798 
17799 	intel_mode_config_init(i915);
17800 
17801 	ret = intel_cdclk_init(i915);
17802 	if (ret)
17803 		return ret;
17804 
17805 	ret = intel_bw_init(i915);
17806 	if (ret)
17807 		return ret;
17808 
17809 	init_llist_head(&i915->atomic_helper.free_list);
17810 	INIT_WORK(&i915->atomic_helper.free_work,
17811 		  intel_atomic_helper_free_state_worker);
17812 
17813 	intel_init_quirks(i915);
17814 
17815 	intel_fbc_init(i915);
17816 
17817 	intel_init_pm(i915);
17818 
17819 	intel_panel_sanitize_ssc(i915);
17820 
17821 	intel_gmbus_setup(i915);
17822 
17823 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
17824 		    INTEL_NUM_PIPES(i915),
17825 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
17826 
17827 	if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
17828 		for_each_pipe(i915, pipe) {
17829 			ret = intel_crtc_init(i915, pipe);
17830 			if (ret) {
17831 				intel_mode_config_cleanup(i915);
17832 				return ret;
17833 			}
17834 		}
17835 	}
17836 
17837 	intel_shared_dpll_init(dev);
17838 	intel_update_fdi_pll_freq(i915);
17839 
17840 	intel_update_czclk(i915);
17841 	intel_modeset_init_hw(i915);
17842 
17843 	intel_hdcp_component_init(i915);
17844 
17845 	if (i915->max_cdclk_freq == 0)
17846 		intel_update_max_cdclk(i915);
17847 
17848 	/* Just disable it once at startup */
17849 	intel_vga_disable(i915);
17850 	intel_setup_outputs(i915);
17851 
17852 	drm_modeset_lock_all(dev);
17853 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
17854 	drm_modeset_unlock_all(dev);
17855 
17856 	for_each_intel_crtc(dev, crtc) {
17857 		struct intel_initial_plane_config plane_config = {};
17858 
17859 		if (!crtc->active)
17860 			continue;
17861 
17862 		/*
17863 		 * Note that reserving the BIOS fb up front prevents us
17864 		 * from stuffing other stolen allocations like the ring
17865 		 * on top.  This prevents some ugliness at boot time, and
17866 		 * can even allow for smooth boot transitions if the BIOS
17867 		 * fb is large enough for the active pipe configuration.
17868 		 */
17869 		i915->display.get_initial_plane_config(crtc, &plane_config);
17870 
17871 		/*
17872 		 * If the fb is shared between multiple heads, we'll
17873 		 * just get the first one.
17874 		 */
17875 		intel_find_initial_plane_obj(crtc, &plane_config);
17876 
17877 		plane_config_fini(&plane_config);
17878 	}
17879 
17880 	/*
17881 	 * Make sure hardware watermarks really match the state we read out.
17882 	 * Note that we need to do this after reconstructing the BIOS fb's
17883 	 * since the watermark calculation done here will use pstate->fb.
17884 	 */
17885 	if (!HAS_GMCH(i915))
17886 		sanitize_watermarks(i915);
17887 
17888 	/*
17889 	 * Force all active planes to recompute their states. So that on
17890 	 * mode_setcrtc after probe, all the intel_plane_state variables
17891 	 * are already calculated and there is no assert_plane warnings
17892 	 * during bootup.
17893 	 */
17894 	ret = intel_initial_commit(dev);
17895 	if (ret)
17896 		drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
17897 
17898 	return 0;
17899 }
17900 
17901 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17902 {
17903 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17904 	/* 640x480@60Hz, ~25175 kHz */
17905 	struct dpll clock = {
17906 		.m1 = 18,
17907 		.m2 = 7,
17908 		.p1 = 13,
17909 		.p2 = 4,
17910 		.n = 2,
17911 	};
17912 	u32 dpll, fp;
17913 	int i;
17914 
17915 	drm_WARN_ON(&dev_priv->drm,
17916 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
17917 
17918 	drm_dbg_kms(&dev_priv->drm,
17919 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
17920 		    pipe_name(pipe), clock.vco, clock.dot);
17921 
17922 	fp = i9xx_dpll_compute_fp(&clock);
17923 	dpll = DPLL_DVO_2X_MODE |
17924 		DPLL_VGA_MODE_DIS |
17925 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
17926 		PLL_P2_DIVIDE_BY_4 |
17927 		PLL_REF_INPUT_DREFCLK |
17928 		DPLL_VCO_ENABLE;
17929 
17930 	intel_de_write(dev_priv, FP0(pipe), fp);
17931 	intel_de_write(dev_priv, FP1(pipe), fp);
17932 
17933 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
17934 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
17935 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
17936 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
17937 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
17938 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
17939 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
17940 
17941 	/*
17942 	 * Apparently we need to have VGA mode enabled prior to changing
17943 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
17944 	 * dividers, even though the register value does change.
17945 	 */
17946 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
17947 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17948 
17949 	/* Wait for the clocks to stabilize. */
17950 	intel_de_posting_read(dev_priv, DPLL(pipe));
17951 	udelay(150);
17952 
17953 	/* The pixel multiplier can only be updated once the
17954 	 * DPLL is enabled and the clocks are stable.
17955 	 *
17956 	 * So write it again.
17957 	 */
17958 	intel_de_write(dev_priv, DPLL(pipe), dpll);
17959 
17960 	/* We do this three times for luck */
17961 	for (i = 0; i < 3 ; i++) {
17962 		intel_de_write(dev_priv, DPLL(pipe), dpll);
17963 		intel_de_posting_read(dev_priv, DPLL(pipe));
17964 		udelay(150); /* wait for warmup */
17965 	}
17966 
17967 	intel_de_write(dev_priv, PIPECONF(pipe),
17968 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
17969 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
17970 
17971 	intel_wait_for_pipe_scanline_moving(crtc);
17972 }
17973 
17974 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
17975 {
17976 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17977 
17978 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
17979 		    pipe_name(pipe));
17980 
17981 	drm_WARN_ON(&dev_priv->drm,
17982 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
17983 		    DISPLAY_PLANE_ENABLE);
17984 	drm_WARN_ON(&dev_priv->drm,
17985 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
17986 		    DISPLAY_PLANE_ENABLE);
17987 	drm_WARN_ON(&dev_priv->drm,
17988 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
17989 		    DISPLAY_PLANE_ENABLE);
17990 	drm_WARN_ON(&dev_priv->drm,
17991 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
17992 	drm_WARN_ON(&dev_priv->drm,
17993 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
17994 
17995 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
17996 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
17997 
17998 	intel_wait_for_pipe_scanline_stopped(crtc);
17999 
18000 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18001 	intel_de_posting_read(dev_priv, DPLL(pipe));
18002 }
18003 
18004 static void
18005 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18006 {
18007 	struct intel_crtc *crtc;
18008 
18009 	if (INTEL_GEN(dev_priv) >= 4)
18010 		return;
18011 
18012 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18013 		struct intel_plane *plane =
18014 			to_intel_plane(crtc->base.primary);
18015 		struct intel_crtc *plane_crtc;
18016 		enum pipe pipe;
18017 
18018 		if (!plane->get_hw_state(plane, &pipe))
18019 			continue;
18020 
18021 		if (pipe == crtc->pipe)
18022 			continue;
18023 
18024 		drm_dbg_kms(&dev_priv->drm,
18025 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18026 			    plane->base.base.id, plane->base.name);
18027 
18028 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18029 		intel_plane_disable_noatomic(plane_crtc, plane);
18030 	}
18031 }
18032 
18033 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18034 {
18035 	struct drm_device *dev = crtc->base.dev;
18036 	struct intel_encoder *encoder;
18037 
18038 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18039 		return true;
18040 
18041 	return false;
18042 }
18043 
18044 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18045 {
18046 	struct drm_device *dev = encoder->base.dev;
18047 	struct intel_connector *connector;
18048 
18049 	for_each_connector_on_encoder(dev, &encoder->base, connector)
18050 		return connector;
18051 
18052 	return NULL;
18053 }
18054 
18055 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
18056 			      enum pipe pch_transcoder)
18057 {
18058 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
18059 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
18060 }
18061 
18062 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
18063 {
18064 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18065 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
18066 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
18067 
18068 	if (INTEL_GEN(dev_priv) >= 9 ||
18069 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18070 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18071 		u32 val;
18072 
18073 		if (transcoder_is_dsi(cpu_transcoder))
18074 			return;
18075 
18076 		val = intel_de_read(dev_priv, reg);
18077 		val &= ~HSW_FRAME_START_DELAY_MASK;
18078 		val |= HSW_FRAME_START_DELAY(0);
18079 		intel_de_write(dev_priv, reg, val);
18080 	} else {
18081 		i915_reg_t reg = PIPECONF(cpu_transcoder);
18082 		u32 val;
18083 
18084 		val = intel_de_read(dev_priv, reg);
18085 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18086 		val |= PIPECONF_FRAME_START_DELAY(0);
18087 		intel_de_write(dev_priv, reg, val);
18088 	}
18089 
18090 	if (!crtc_state->has_pch_encoder)
18091 		return;
18092 
18093 	if (HAS_PCH_IBX(dev_priv)) {
18094 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18095 		u32 val;
18096 
18097 		val = intel_de_read(dev_priv, reg);
18098 		val &= ~TRANS_FRAME_START_DELAY_MASK;
18099 		val |= TRANS_FRAME_START_DELAY(0);
18100 		intel_de_write(dev_priv, reg, val);
18101 	} else {
18102 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18103 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
18104 		u32 val;
18105 
18106 		val = intel_de_read(dev_priv, reg);
18107 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18108 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
18109 		intel_de_write(dev_priv, reg, val);
18110 	}
18111 }
18112 
18113 static void intel_sanitize_crtc(struct intel_crtc *crtc,
18114 				struct drm_modeset_acquire_ctx *ctx)
18115 {
18116 	struct drm_device *dev = crtc->base.dev;
18117 	struct drm_i915_private *dev_priv = to_i915(dev);
18118 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
18119 
18120 	if (crtc_state->hw.active) {
18121 		struct intel_plane *plane;
18122 
18123 		/* Clear any frame start delays used for debugging left by the BIOS */
18124 		intel_sanitize_frame_start_delay(crtc_state);
18125 
18126 		/* Disable everything but the primary plane */
18127 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
18128 			const struct intel_plane_state *plane_state =
18129 				to_intel_plane_state(plane->base.state);
18130 
18131 			if (plane_state->uapi.visible &&
18132 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18133 				intel_plane_disable_noatomic(crtc, plane);
18134 		}
18135 
18136 		/*
18137 		 * Disable any background color set by the BIOS, but enable the
18138 		 * gamma and CSC to match how we program our planes.
18139 		 */
18140 		if (INTEL_GEN(dev_priv) >= 9)
18141 			intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
18142 				       SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
18143 	}
18144 
18145 	/* Adjust the state of the output pipe according to whether we
18146 	 * have active connectors/encoders. */
18147 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc))
18148 		intel_crtc_disable_noatomic(crtc, ctx);
18149 
18150 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
18151 		/*
18152 		 * We start out with underrun reporting disabled to avoid races.
18153 		 * For correct bookkeeping mark this on active crtcs.
18154 		 *
18155 		 * Also on gmch platforms we dont have any hardware bits to
18156 		 * disable the underrun reporting. Which means we need to start
18157 		 * out with underrun reporting disabled also on inactive pipes,
18158 		 * since otherwise we'll complain about the garbage we read when
18159 		 * e.g. coming up after runtime pm.
18160 		 *
18161 		 * No protection against concurrent access is required - at
18162 		 * worst a fifo underrun happens which also sets this to false.
18163 		 */
18164 		crtc->cpu_fifo_underrun_disabled = true;
18165 		/*
18166 		 * We track the PCH trancoder underrun reporting state
18167 		 * within the crtc. With crtc for pipe A housing the underrun
18168 		 * reporting state for PCH transcoder A, crtc for pipe B housing
18169 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
18170 		 * and marking underrun reporting as disabled for the non-existing
18171 		 * PCH transcoders B and C would prevent enabling the south
18172 		 * error interrupt (see cpt_can_enable_serr_int()).
18173 		 */
18174 		if (has_pch_trancoder(dev_priv, crtc->pipe))
18175 			crtc->pch_fifo_underrun_disabled = true;
18176 	}
18177 }
18178 
18179 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
18180 {
18181 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
18182 
18183 	/*
18184 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
18185 	 * the hardware when a high res displays plugged in. DPLL P
18186 	 * divider is zero, and the pipe timings are bonkers. We'll
18187 	 * try to disable everything in that case.
18188 	 *
18189 	 * FIXME would be nice to be able to sanitize this state
18190 	 * without several WARNs, but for now let's take the easy
18191 	 * road.
18192 	 */
18193 	return IS_GEN(dev_priv, 6) &&
18194 		crtc_state->hw.active &&
18195 		crtc_state->shared_dpll &&
18196 		crtc_state->port_clock == 0;
18197 }
18198 
18199 static void intel_sanitize_encoder(struct intel_encoder *encoder)
18200 {
18201 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
18202 	struct intel_connector *connector;
18203 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
18204 	struct intel_crtc_state *crtc_state = crtc ?
18205 		to_intel_crtc_state(crtc->base.state) : NULL;
18206 
18207 	/* We need to check both for a crtc link (meaning that the
18208 	 * encoder is active and trying to read from a pipe) and the
18209 	 * pipe itself being active. */
18210 	bool has_active_crtc = crtc_state &&
18211 		crtc_state->hw.active;
18212 
18213 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
18214 		drm_dbg_kms(&dev_priv->drm,
18215 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
18216 			    pipe_name(crtc->pipe));
18217 		has_active_crtc = false;
18218 	}
18219 
18220 	connector = intel_encoder_find_connector(encoder);
18221 	if (connector && !has_active_crtc) {
18222 		drm_dbg_kms(&dev_priv->drm,
18223 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
18224 			    encoder->base.base.id,
18225 			    encoder->base.name);
18226 
18227 		/* Connector is active, but has no active pipe. This is
18228 		 * fallout from our resume register restoring. Disable
18229 		 * the encoder manually again. */
18230 		if (crtc_state) {
18231 			struct drm_encoder *best_encoder;
18232 
18233 			drm_dbg_kms(&dev_priv->drm,
18234 				    "[ENCODER:%d:%s] manually disabled\n",
18235 				    encoder->base.base.id,
18236 				    encoder->base.name);
18237 
18238 			/* avoid oopsing in case the hooks consult best_encoder */
18239 			best_encoder = connector->base.state->best_encoder;
18240 			connector->base.state->best_encoder = &encoder->base;
18241 
18242 			if (encoder->disable)
18243 				encoder->disable(encoder, crtc_state,
18244 						 connector->base.state);
18245 			if (encoder->post_disable)
18246 				encoder->post_disable(encoder, crtc_state,
18247 						      connector->base.state);
18248 
18249 			connector->base.state->best_encoder = best_encoder;
18250 		}
18251 		encoder->base.crtc = NULL;
18252 
18253 		/* Inconsistent output/port/pipe state happens presumably due to
18254 		 * a bug in one of the get_hw_state functions. Or someplace else
18255 		 * in our code, like the register restore mess on resume. Clamp
18256 		 * things to off as a safer default. */
18257 
18258 		connector->base.dpms = DRM_MODE_DPMS_OFF;
18259 		connector->base.encoder = NULL;
18260 	}
18261 
18262 	/* notify opregion of the sanitized encoder state */
18263 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
18264 
18265 	if (INTEL_GEN(dev_priv) >= 11)
18266 		icl_sanitize_encoder_pll_mapping(encoder);
18267 }
18268 
18269 /* FIXME read out full plane state for all planes */
18270 static void readout_plane_state(struct drm_i915_private *dev_priv)
18271 {
18272 	struct intel_plane *plane;
18273 	struct intel_crtc *crtc;
18274 
18275 	for_each_intel_plane(&dev_priv->drm, plane) {
18276 		struct intel_plane_state *plane_state =
18277 			to_intel_plane_state(plane->base.state);
18278 		struct intel_crtc_state *crtc_state;
18279 		enum pipe pipe = PIPE_A;
18280 		bool visible;
18281 
18282 		visible = plane->get_hw_state(plane, &pipe);
18283 
18284 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18285 		crtc_state = to_intel_crtc_state(crtc->base.state);
18286 
18287 		intel_set_plane_visible(crtc_state, plane_state, visible);
18288 
18289 		drm_dbg_kms(&dev_priv->drm,
18290 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
18291 			    plane->base.base.id, plane->base.name,
18292 			    enableddisabled(visible), pipe_name(pipe));
18293 	}
18294 
18295 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18296 		struct intel_crtc_state *crtc_state =
18297 			to_intel_crtc_state(crtc->base.state);
18298 
18299 		fixup_active_planes(crtc_state);
18300 	}
18301 }
18302 
18303 static void intel_modeset_readout_hw_state(struct drm_device *dev)
18304 {
18305 	struct drm_i915_private *dev_priv = to_i915(dev);
18306 	struct intel_cdclk_state *cdclk_state =
18307 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
18308 	enum pipe pipe;
18309 	struct intel_crtc *crtc;
18310 	struct intel_encoder *encoder;
18311 	struct intel_connector *connector;
18312 	struct drm_connector_list_iter conn_iter;
18313 	u8 active_pipes = 0;
18314 	int i;
18315 
18316 	for_each_intel_crtc(dev, crtc) {
18317 		struct intel_crtc_state *crtc_state =
18318 			to_intel_crtc_state(crtc->base.state);
18319 
18320 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
18321 		intel_crtc_free_hw_state(crtc_state);
18322 		intel_crtc_state_reset(crtc_state, crtc);
18323 
18324 		crtc_state->hw.active = crtc_state->hw.enable =
18325 			dev_priv->display.get_pipe_config(crtc, crtc_state);
18326 
18327 		crtc->base.enabled = crtc_state->hw.enable;
18328 		crtc->active = crtc_state->hw.active;
18329 
18330 		if (crtc_state->hw.active)
18331 			active_pipes |= BIT(crtc->pipe);
18332 
18333 		drm_dbg_kms(&dev_priv->drm,
18334 			    "[CRTC:%d:%s] hw state readout: %s\n",
18335 			    crtc->base.base.id, crtc->base.name,
18336 			    enableddisabled(crtc_state->hw.active));
18337 	}
18338 
18339 	dev_priv->active_pipes = cdclk_state->active_pipes = active_pipes;
18340 
18341 	readout_plane_state(dev_priv);
18342 
18343 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
18344 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
18345 
18346 		pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
18347 							&pll->state.hw_state);
18348 
18349 		if (IS_ELKHARTLAKE(dev_priv) && pll->on &&
18350 		    pll->info->id == DPLL_ID_EHL_DPLL4) {
18351 			pll->wakeref = intel_display_power_get(dev_priv,
18352 							       POWER_DOMAIN_DPLL_DC_OFF);
18353 		}
18354 
18355 		pll->state.crtc_mask = 0;
18356 		for_each_intel_crtc(dev, crtc) {
18357 			struct intel_crtc_state *crtc_state =
18358 				to_intel_crtc_state(crtc->base.state);
18359 
18360 			if (crtc_state->hw.active &&
18361 			    crtc_state->shared_dpll == pll)
18362 				pll->state.crtc_mask |= 1 << crtc->pipe;
18363 		}
18364 		pll->active_mask = pll->state.crtc_mask;
18365 
18366 		drm_dbg_kms(&dev_priv->drm,
18367 			    "%s hw state readout: crtc_mask 0x%08x, on %i\n",
18368 			    pll->info->name, pll->state.crtc_mask, pll->on);
18369 	}
18370 
18371 	for_each_intel_encoder(dev, encoder) {
18372 		pipe = 0;
18373 
18374 		if (encoder->get_hw_state(encoder, &pipe)) {
18375 			struct intel_crtc_state *crtc_state;
18376 
18377 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18378 			crtc_state = to_intel_crtc_state(crtc->base.state);
18379 
18380 			encoder->base.crtc = &crtc->base;
18381 			encoder->get_config(encoder, crtc_state);
18382 		} else {
18383 			encoder->base.crtc = NULL;
18384 		}
18385 
18386 		drm_dbg_kms(&dev_priv->drm,
18387 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
18388 			    encoder->base.base.id, encoder->base.name,
18389 			    enableddisabled(encoder->base.crtc),
18390 			    pipe_name(pipe));
18391 	}
18392 
18393 	drm_connector_list_iter_begin(dev, &conn_iter);
18394 	for_each_intel_connector_iter(connector, &conn_iter) {
18395 		if (connector->get_hw_state(connector)) {
18396 			struct intel_crtc_state *crtc_state;
18397 			struct intel_crtc *crtc;
18398 
18399 			connector->base.dpms = DRM_MODE_DPMS_ON;
18400 
18401 			encoder = intel_attached_encoder(connector);
18402 			connector->base.encoder = &encoder->base;
18403 
18404 			crtc = to_intel_crtc(encoder->base.crtc);
18405 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
18406 
18407 			if (crtc_state && crtc_state->hw.active) {
18408 				/*
18409 				 * This has to be done during hardware readout
18410 				 * because anything calling .crtc_disable may
18411 				 * rely on the connector_mask being accurate.
18412 				 */
18413 				crtc_state->uapi.connector_mask |=
18414 					drm_connector_mask(&connector->base);
18415 				crtc_state->uapi.encoder_mask |=
18416 					drm_encoder_mask(&encoder->base);
18417 			}
18418 		} else {
18419 			connector->base.dpms = DRM_MODE_DPMS_OFF;
18420 			connector->base.encoder = NULL;
18421 		}
18422 		drm_dbg_kms(&dev_priv->drm,
18423 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
18424 			    connector->base.base.id, connector->base.name,
18425 			    enableddisabled(connector->base.encoder));
18426 	}
18427 	drm_connector_list_iter_end(&conn_iter);
18428 
18429 	for_each_intel_crtc(dev, crtc) {
18430 		struct intel_bw_state *bw_state =
18431 			to_intel_bw_state(dev_priv->bw_obj.state);
18432 		struct intel_crtc_state *crtc_state =
18433 			to_intel_crtc_state(crtc->base.state);
18434 		struct intel_plane *plane;
18435 		int min_cdclk = 0;
18436 
18437 		if (crtc_state->hw.active) {
18438 			struct drm_display_mode *mode = &crtc_state->hw.mode;
18439 
18440 			intel_mode_from_pipe_config(&crtc_state->hw.adjusted_mode,
18441 						    crtc_state);
18442 
18443 			*mode = crtc_state->hw.adjusted_mode;
18444 			mode->hdisplay = crtc_state->pipe_src_w;
18445 			mode->vdisplay = crtc_state->pipe_src_h;
18446 
18447 			/*
18448 			 * The initial mode needs to be set in order to keep
18449 			 * the atomic core happy. It wants a valid mode if the
18450 			 * crtc's enabled, so we do the above call.
18451 			 *
18452 			 * But we don't set all the derived state fully, hence
18453 			 * set a flag to indicate that a full recalculation is
18454 			 * needed on the next commit.
18455 			 */
18456 			mode->private_flags = I915_MODE_FLAG_INHERITED;
18457 
18458 			intel_crtc_compute_pixel_rate(crtc_state);
18459 
18460 			intel_crtc_update_active_timings(crtc_state);
18461 
18462 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
18463 		}
18464 
18465 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
18466 			const struct intel_plane_state *plane_state =
18467 				to_intel_plane_state(plane->base.state);
18468 
18469 			/*
18470 			 * FIXME don't have the fb yet, so can't
18471 			 * use intel_plane_data_rate() :(
18472 			 */
18473 			if (plane_state->uapi.visible)
18474 				crtc_state->data_rate[plane->id] =
18475 					4 * crtc_state->pixel_rate;
18476 			/*
18477 			 * FIXME don't have the fb yet, so can't
18478 			 * use plane->min_cdclk() :(
18479 			 */
18480 			if (plane_state->uapi.visible && plane->min_cdclk) {
18481 				if (crtc_state->double_wide ||
18482 				    INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
18483 					crtc_state->min_cdclk[plane->id] =
18484 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
18485 				else
18486 					crtc_state->min_cdclk[plane->id] =
18487 						crtc_state->pixel_rate;
18488 			}
18489 			drm_dbg_kms(&dev_priv->drm,
18490 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
18491 				    plane->base.base.id, plane->base.name,
18492 				    crtc_state->min_cdclk[plane->id]);
18493 		}
18494 
18495 		if (crtc_state->hw.active) {
18496 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
18497 			if (drm_WARN_ON(dev, min_cdclk < 0))
18498 				min_cdclk = 0;
18499 		}
18500 
18501 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
18502 		cdclk_state->min_voltage_level[crtc->pipe] =
18503 			crtc_state->min_voltage_level;
18504 
18505 		intel_bw_crtc_update(bw_state, crtc_state);
18506 
18507 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
18508 	}
18509 }
18510 
18511 static void
18512 get_encoder_power_domains(struct drm_i915_private *dev_priv)
18513 {
18514 	struct intel_encoder *encoder;
18515 
18516 	for_each_intel_encoder(&dev_priv->drm, encoder) {
18517 		struct intel_crtc_state *crtc_state;
18518 
18519 		if (!encoder->get_power_domains)
18520 			continue;
18521 
18522 		/*
18523 		 * MST-primary and inactive encoders don't have a crtc state
18524 		 * and neither of these require any power domain references.
18525 		 */
18526 		if (!encoder->base.crtc)
18527 			continue;
18528 
18529 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
18530 		encoder->get_power_domains(encoder, crtc_state);
18531 	}
18532 }
18533 
18534 static void intel_early_display_was(struct drm_i915_private *dev_priv)
18535 {
18536 	/*
18537 	 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
18538 	 * Also known as Wa_14010480278.
18539 	 */
18540 	if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
18541 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
18542 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
18543 
18544 	if (IS_HASWELL(dev_priv)) {
18545 		/*
18546 		 * WaRsPkgCStateDisplayPMReq:hsw
18547 		 * System hang if this isn't done before disabling all planes!
18548 		 */
18549 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
18550 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
18551 	}
18552 }
18553 
18554 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
18555 				       enum port port, i915_reg_t hdmi_reg)
18556 {
18557 	u32 val = intel_de_read(dev_priv, hdmi_reg);
18558 
18559 	if (val & SDVO_ENABLE ||
18560 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
18561 		return;
18562 
18563 	drm_dbg_kms(&dev_priv->drm,
18564 		    "Sanitizing transcoder select for HDMI %c\n",
18565 		    port_name(port));
18566 
18567 	val &= ~SDVO_PIPE_SEL_MASK;
18568 	val |= SDVO_PIPE_SEL(PIPE_A);
18569 
18570 	intel_de_write(dev_priv, hdmi_reg, val);
18571 }
18572 
18573 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
18574 				     enum port port, i915_reg_t dp_reg)
18575 {
18576 	u32 val = intel_de_read(dev_priv, dp_reg);
18577 
18578 	if (val & DP_PORT_EN ||
18579 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
18580 		return;
18581 
18582 	drm_dbg_kms(&dev_priv->drm,
18583 		    "Sanitizing transcoder select for DP %c\n",
18584 		    port_name(port));
18585 
18586 	val &= ~DP_PIPE_SEL_MASK;
18587 	val |= DP_PIPE_SEL(PIPE_A);
18588 
18589 	intel_de_write(dev_priv, dp_reg, val);
18590 }
18591 
18592 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
18593 {
18594 	/*
18595 	 * The BIOS may select transcoder B on some of the PCH
18596 	 * ports even it doesn't enable the port. This would trip
18597 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
18598 	 * Sanitize the transcoder select bits to prevent that. We
18599 	 * assume that the BIOS never actually enabled the port,
18600 	 * because if it did we'd actually have to toggle the port
18601 	 * on and back off to make the transcoder A select stick
18602 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
18603 	 * intel_disable_sdvo()).
18604 	 */
18605 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
18606 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
18607 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
18608 
18609 	/* PCH SDVOB multiplex with HDMIB */
18610 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
18611 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
18612 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
18613 }
18614 
18615 /* Scan out the current hw modeset state,
18616  * and sanitizes it to the current state
18617  */
18618 static void
18619 intel_modeset_setup_hw_state(struct drm_device *dev,
18620 			     struct drm_modeset_acquire_ctx *ctx)
18621 {
18622 	struct drm_i915_private *dev_priv = to_i915(dev);
18623 	struct intel_encoder *encoder;
18624 	struct intel_crtc *crtc;
18625 	intel_wakeref_t wakeref;
18626 	int i;
18627 
18628 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
18629 
18630 	intel_early_display_was(dev_priv);
18631 	intel_modeset_readout_hw_state(dev);
18632 
18633 	/* HW state is read out, now we need to sanitize this mess. */
18634 
18635 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
18636 	for_each_intel_encoder(dev, encoder) {
18637 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
18638 
18639 		/* We need to sanitize only the MST primary port. */
18640 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
18641 		    intel_phy_is_tc(dev_priv, phy))
18642 			intel_tc_port_sanitize(enc_to_dig_port(encoder));
18643 	}
18644 
18645 	get_encoder_power_domains(dev_priv);
18646 
18647 	if (HAS_PCH_IBX(dev_priv))
18648 		ibx_sanitize_pch_ports(dev_priv);
18649 
18650 	/*
18651 	 * intel_sanitize_plane_mapping() may need to do vblank
18652 	 * waits, so we need vblank interrupts restored beforehand.
18653 	 */
18654 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18655 		struct intel_crtc_state *crtc_state =
18656 			to_intel_crtc_state(crtc->base.state);
18657 
18658 		drm_crtc_vblank_reset(&crtc->base);
18659 
18660 		if (crtc_state->hw.active)
18661 			intel_crtc_vblank_on(crtc_state);
18662 	}
18663 
18664 	intel_sanitize_plane_mapping(dev_priv);
18665 
18666 	for_each_intel_encoder(dev, encoder)
18667 		intel_sanitize_encoder(encoder);
18668 
18669 	for_each_intel_crtc(&dev_priv->drm, crtc) {
18670 		struct intel_crtc_state *crtc_state =
18671 			to_intel_crtc_state(crtc->base.state);
18672 
18673 		intel_sanitize_crtc(crtc, ctx);
18674 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
18675 	}
18676 
18677 	intel_modeset_update_connector_atomic_state(dev);
18678 
18679 	for (i = 0; i < dev_priv->num_shared_dpll; i++) {
18680 		struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
18681 
18682 		if (!pll->on || pll->active_mask)
18683 			continue;
18684 
18685 		drm_dbg_kms(&dev_priv->drm,
18686 			    "%s enabled but not in use, disabling\n",
18687 			    pll->info->name);
18688 
18689 		pll->info->funcs->disable(dev_priv, pll);
18690 		pll->on = false;
18691 	}
18692 
18693 	if (IS_G4X(dev_priv)) {
18694 		g4x_wm_get_hw_state(dev_priv);
18695 		g4x_wm_sanitize(dev_priv);
18696 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
18697 		vlv_wm_get_hw_state(dev_priv);
18698 		vlv_wm_sanitize(dev_priv);
18699 	} else if (INTEL_GEN(dev_priv) >= 9) {
18700 		skl_wm_get_hw_state(dev_priv);
18701 	} else if (HAS_PCH_SPLIT(dev_priv)) {
18702 		ilk_wm_get_hw_state(dev_priv);
18703 	}
18704 
18705 	for_each_intel_crtc(dev, crtc) {
18706 		struct intel_crtc_state *crtc_state =
18707 			to_intel_crtc_state(crtc->base.state);
18708 		u64 put_domains;
18709 
18710 		put_domains = modeset_get_crtc_power_domains(crtc_state);
18711 		if (drm_WARN_ON(dev, put_domains))
18712 			modeset_put_power_domains(dev_priv, put_domains);
18713 	}
18714 
18715 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
18716 }
18717 
18718 void intel_display_resume(struct drm_device *dev)
18719 {
18720 	struct drm_i915_private *dev_priv = to_i915(dev);
18721 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
18722 	struct drm_modeset_acquire_ctx ctx;
18723 	int ret;
18724 
18725 	dev_priv->modeset_restore_state = NULL;
18726 	if (state)
18727 		state->acquire_ctx = &ctx;
18728 
18729 	drm_modeset_acquire_init(&ctx, 0);
18730 
18731 	while (1) {
18732 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
18733 		if (ret != -EDEADLK)
18734 			break;
18735 
18736 		drm_modeset_backoff(&ctx);
18737 	}
18738 
18739 	if (!ret)
18740 		ret = __intel_display_resume(dev, state, &ctx);
18741 
18742 	intel_enable_ipc(dev_priv);
18743 	drm_modeset_drop_locks(&ctx);
18744 	drm_modeset_acquire_fini(&ctx);
18745 
18746 	if (ret)
18747 		drm_err(&dev_priv->drm,
18748 			"Restoring old state failed with %i\n", ret);
18749 	if (state)
18750 		drm_atomic_state_put(state);
18751 }
18752 
18753 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
18754 {
18755 	struct intel_connector *connector;
18756 	struct drm_connector_list_iter conn_iter;
18757 
18758 	/* Kill all the work that may have been queued by hpd. */
18759 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
18760 	for_each_intel_connector_iter(connector, &conn_iter) {
18761 		if (connector->modeset_retry_work.func)
18762 			cancel_work_sync(&connector->modeset_retry_work);
18763 		if (connector->hdcp.shim) {
18764 			cancel_delayed_work_sync(&connector->hdcp.check_work);
18765 			cancel_work_sync(&connector->hdcp.prop_work);
18766 		}
18767 	}
18768 	drm_connector_list_iter_end(&conn_iter);
18769 }
18770 
18771 /* part #1: call before irq uninstall */
18772 void intel_modeset_driver_remove(struct drm_i915_private *i915)
18773 {
18774 	flush_workqueue(i915->flip_wq);
18775 	flush_workqueue(i915->modeset_wq);
18776 
18777 	flush_work(&i915->atomic_helper.free_work);
18778 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
18779 }
18780 
18781 /* part #2: call after irq uninstall */
18782 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
18783 {
18784 	/*
18785 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
18786 	 * poll handlers. Hence disable polling after hpd handling is shut down.
18787 	 */
18788 	intel_hpd_poll_fini(i915);
18789 
18790 	/*
18791 	 * MST topology needs to be suspended so we don't have any calls to
18792 	 * fbdev after it's finalized. MST will be destroyed later as part of
18793 	 * drm_mode_config_cleanup()
18794 	 */
18795 	intel_dp_mst_suspend(i915);
18796 
18797 	/* poll work can call into fbdev, hence clean that up afterwards */
18798 	intel_fbdev_fini(i915);
18799 
18800 	intel_unregister_dsm_handler();
18801 
18802 	intel_fbc_global_disable(i915);
18803 
18804 	/* flush any delayed tasks or pending work */
18805 	flush_scheduled_work();
18806 
18807 	intel_hdcp_component_fini(i915);
18808 
18809 	intel_mode_config_cleanup(i915);
18810 
18811 	intel_overlay_cleanup(i915);
18812 
18813 	intel_gmbus_teardown(i915);
18814 
18815 	destroy_workqueue(i915->flip_wq);
18816 	destroy_workqueue(i915->modeset_wq);
18817 
18818 	intel_fbc_cleanup_cfb(i915);
18819 }
18820 
18821 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
18822 
18823 struct intel_display_error_state {
18824 
18825 	u32 power_well_driver;
18826 
18827 	struct intel_cursor_error_state {
18828 		u32 control;
18829 		u32 position;
18830 		u32 base;
18831 		u32 size;
18832 	} cursor[I915_MAX_PIPES];
18833 
18834 	struct intel_pipe_error_state {
18835 		bool power_domain_on;
18836 		u32 source;
18837 		u32 stat;
18838 	} pipe[I915_MAX_PIPES];
18839 
18840 	struct intel_plane_error_state {
18841 		u32 control;
18842 		u32 stride;
18843 		u32 size;
18844 		u32 pos;
18845 		u32 addr;
18846 		u32 surface;
18847 		u32 tile_offset;
18848 	} plane[I915_MAX_PIPES];
18849 
18850 	struct intel_transcoder_error_state {
18851 		bool available;
18852 		bool power_domain_on;
18853 		enum transcoder cpu_transcoder;
18854 
18855 		u32 conf;
18856 
18857 		u32 htotal;
18858 		u32 hblank;
18859 		u32 hsync;
18860 		u32 vtotal;
18861 		u32 vblank;
18862 		u32 vsync;
18863 	} transcoder[5];
18864 };
18865 
18866 struct intel_display_error_state *
18867 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
18868 {
18869 	struct intel_display_error_state *error;
18870 	int transcoders[] = {
18871 		TRANSCODER_A,
18872 		TRANSCODER_B,
18873 		TRANSCODER_C,
18874 		TRANSCODER_D,
18875 		TRANSCODER_EDP,
18876 	};
18877 	int i;
18878 
18879 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
18880 
18881 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))
18882 		return NULL;
18883 
18884 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
18885 	if (error == NULL)
18886 		return NULL;
18887 
18888 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18889 		error->power_well_driver = intel_de_read(dev_priv,
18890 							 HSW_PWR_WELL_CTL2);
18891 
18892 	for_each_pipe(dev_priv, i) {
18893 		error->pipe[i].power_domain_on =
18894 			__intel_display_power_is_enabled(dev_priv,
18895 							 POWER_DOMAIN_PIPE(i));
18896 		if (!error->pipe[i].power_domain_on)
18897 			continue;
18898 
18899 		error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
18900 		error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
18901 		error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
18902 
18903 		error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
18904 		error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
18905 		if (INTEL_GEN(dev_priv) <= 3) {
18906 			error->plane[i].size = intel_de_read(dev_priv,
18907 							     DSPSIZE(i));
18908 			error->plane[i].pos = intel_de_read(dev_priv,
18909 							    DSPPOS(i));
18910 		}
18911 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18912 			error->plane[i].addr = intel_de_read(dev_priv,
18913 							     DSPADDR(i));
18914 		if (INTEL_GEN(dev_priv) >= 4) {
18915 			error->plane[i].surface = intel_de_read(dev_priv,
18916 								DSPSURF(i));
18917 			error->plane[i].tile_offset = intel_de_read(dev_priv,
18918 								    DSPTILEOFF(i));
18919 		}
18920 
18921 		error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
18922 
18923 		if (HAS_GMCH(dev_priv))
18924 			error->pipe[i].stat = intel_de_read(dev_priv,
18925 							    PIPESTAT(i));
18926 	}
18927 
18928 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
18929 		enum transcoder cpu_transcoder = transcoders[i];
18930 
18931 		if (!INTEL_INFO(dev_priv)->trans_offsets[cpu_transcoder])
18932 			continue;
18933 
18934 		error->transcoder[i].available = true;
18935 		error->transcoder[i].power_domain_on =
18936 			__intel_display_power_is_enabled(dev_priv,
18937 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
18938 		if (!error->transcoder[i].power_domain_on)
18939 			continue;
18940 
18941 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
18942 
18943 		error->transcoder[i].conf = intel_de_read(dev_priv,
18944 							  PIPECONF(cpu_transcoder));
18945 		error->transcoder[i].htotal = intel_de_read(dev_priv,
18946 							    HTOTAL(cpu_transcoder));
18947 		error->transcoder[i].hblank = intel_de_read(dev_priv,
18948 							    HBLANK(cpu_transcoder));
18949 		error->transcoder[i].hsync = intel_de_read(dev_priv,
18950 							   HSYNC(cpu_transcoder));
18951 		error->transcoder[i].vtotal = intel_de_read(dev_priv,
18952 							    VTOTAL(cpu_transcoder));
18953 		error->transcoder[i].vblank = intel_de_read(dev_priv,
18954 							    VBLANK(cpu_transcoder));
18955 		error->transcoder[i].vsync = intel_de_read(dev_priv,
18956 							   VSYNC(cpu_transcoder));
18957 	}
18958 
18959 	return error;
18960 }
18961 
18962 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
18963 
18964 void
18965 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
18966 				struct intel_display_error_state *error)
18967 {
18968 	struct drm_i915_private *dev_priv = m->i915;
18969 	int i;
18970 
18971 	if (!error)
18972 		return;
18973 
18974 	err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
18975 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
18976 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
18977 			   error->power_well_driver);
18978 	for_each_pipe(dev_priv, i) {
18979 		err_printf(m, "Pipe [%d]:\n", i);
18980 		err_printf(m, "  Power: %s\n",
18981 			   onoff(error->pipe[i].power_domain_on));
18982 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
18983 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
18984 
18985 		err_printf(m, "Plane [%d]:\n", i);
18986 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
18987 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
18988 		if (INTEL_GEN(dev_priv) <= 3) {
18989 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
18990 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
18991 		}
18992 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
18993 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
18994 		if (INTEL_GEN(dev_priv) >= 4) {
18995 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
18996 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
18997 		}
18998 
18999 		err_printf(m, "Cursor [%d]:\n", i);
19000 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
19001 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
19002 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
19003 	}
19004 
19005 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19006 		if (!error->transcoder[i].available)
19007 			continue;
19008 
19009 		err_printf(m, "CPU transcoder: %s\n",
19010 			   transcoder_name(error->transcoder[i].cpu_transcoder));
19011 		err_printf(m, "  Power: %s\n",
19012 			   onoff(error->transcoder[i].power_domain_on));
19013 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
19014 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
19015 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
19016 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
19017 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
19018 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
19019 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
19020 	}
19021 }
19022 
19023 #endif
19024