ebf621ff7ef6f7e237311f332792aad0262b634d
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / i915 / display / intel_display.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26
27 #include <linux/i2c.h>
28 #include <linux/input.h>
29 #include <linux/intel-iommu.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/dma-resv.h>
33 #include <linux/slab.h>
34
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_atomic_uapi.h>
38 #include <drm/drm_damage_helper.h>
39 #include <drm/drm_dp_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_plane_helper.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/drm_rect.h>
45
46 #include "display/intel_crt.h"
47 #include "display/intel_ddi.h"
48 #include "display/intel_dp.h"
49 #include "display/intel_dp_mst.h"
50 #include "display/intel_dpll_mgr.h"
51 #include "display/intel_dsi.h"
52 #include "display/intel_dvo.h"
53 #include "display/intel_gmbus.h"
54 #include "display/intel_hdmi.h"
55 #include "display/intel_lvds.h"
56 #include "display/intel_sdvo.h"
57 #include "display/intel_tv.h"
58 #include "display/intel_vdsc.h"
59
60 #include "gt/intel_rps.h"
61
62 #include "i915_drv.h"
63 #include "i915_trace.h"
64 #include "intel_acpi.h"
65 #include "intel_atomic.h"
66 #include "intel_atomic_plane.h"
67 #include "intel_bw.h"
68 #include "intel_cdclk.h"
69 #include "intel_color.h"
70 #include "intel_csr.h"
71 #include "intel_display_types.h"
72 #include "intel_dp_link_training.h"
73 #include "intel_fbc.h"
74 #include "intel_fbdev.h"
75 #include "intel_fifo_underrun.h"
76 #include "intel_frontbuffer.h"
77 #include "intel_hdcp.h"
78 #include "intel_hotplug.h"
79 #include "intel_overlay.h"
80 #include "intel_pipe_crc.h"
81 #include "intel_pm.h"
82 #include "intel_psr.h"
83 #include "intel_quirks.h"
84 #include "intel_sideband.h"
85 #include "intel_sprite.h"
86 #include "intel_tc.h"
87 #include "intel_vga.h"
88
89 /* Primary plane formats for gen <= 3 */
90 static const u32 i8xx_primary_formats[] = {
91         DRM_FORMAT_C8,
92         DRM_FORMAT_XRGB1555,
93         DRM_FORMAT_RGB565,
94         DRM_FORMAT_XRGB8888,
95 };
96
97 /* Primary plane formats for ivb (no fp16 due to hw issue) */
98 static const u32 ivb_primary_formats[] = {
99         DRM_FORMAT_C8,
100         DRM_FORMAT_RGB565,
101         DRM_FORMAT_XRGB8888,
102         DRM_FORMAT_XBGR8888,
103         DRM_FORMAT_XRGB2101010,
104         DRM_FORMAT_XBGR2101010,
105 };
106
107 /* Primary plane formats for gen >= 4, except ivb */
108 static const u32 i965_primary_formats[] = {
109         DRM_FORMAT_C8,
110         DRM_FORMAT_RGB565,
111         DRM_FORMAT_XRGB8888,
112         DRM_FORMAT_XBGR8888,
113         DRM_FORMAT_XRGB2101010,
114         DRM_FORMAT_XBGR2101010,
115         DRM_FORMAT_XBGR16161616F,
116 };
117
118 /* Primary plane formats for vlv/chv */
119 static const u32 vlv_primary_formats[] = {
120         DRM_FORMAT_C8,
121         DRM_FORMAT_RGB565,
122         DRM_FORMAT_XRGB8888,
123         DRM_FORMAT_XBGR8888,
124         DRM_FORMAT_ARGB8888,
125         DRM_FORMAT_ABGR8888,
126         DRM_FORMAT_XRGB2101010,
127         DRM_FORMAT_XBGR2101010,
128         DRM_FORMAT_ARGB2101010,
129         DRM_FORMAT_ABGR2101010,
130         DRM_FORMAT_XBGR16161616F,
131 };
132
133 static const u64 i9xx_format_modifiers[] = {
134         I915_FORMAT_MOD_X_TILED,
135         DRM_FORMAT_MOD_LINEAR,
136         DRM_FORMAT_MOD_INVALID
137 };
138
139 /* Cursor formats */
140 static const u32 intel_cursor_formats[] = {
141         DRM_FORMAT_ARGB8888,
142 };
143
144 static const u64 cursor_format_modifiers[] = {
145         DRM_FORMAT_MOD_LINEAR,
146         DRM_FORMAT_MOD_INVALID
147 };
148
149 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
150                                 struct intel_crtc_state *pipe_config);
151 static void ilk_pch_clock_get(struct intel_crtc *crtc,
152                               struct intel_crtc_state *pipe_config);
153
154 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
155                                   struct drm_i915_gem_object *obj,
156                                   struct drm_mode_fb_cmd2 *mode_cmd);
157 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
158 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
159 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
160                                          const struct intel_link_m_n *m_n,
161                                          const struct intel_link_m_n *m2_n2);
162 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
163 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
164 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
165 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
166 static void vlv_prepare_pll(struct intel_crtc *crtc,
167                             const struct intel_crtc_state *pipe_config);
168 static void chv_prepare_pll(struct intel_crtc *crtc,
169                             const struct intel_crtc_state *pipe_config);
170 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state);
171 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
172 static void intel_modeset_setup_hw_state(struct drm_device *dev,
173                                          struct drm_modeset_acquire_ctx *ctx);
174 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc);
175
176 struct intel_limit {
177         struct {
178                 int min, max;
179         } dot, vco, n, m, m1, m2, p, p1;
180
181         struct {
182                 int dot_limit;
183                 int p2_slow, p2_fast;
184         } p2;
185 };
186
187 /* returns HPLL frequency in kHz */
188 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
189 {
190         int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
191
192         /* Obtain SKU information */
193         hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
194                 CCK_FUSE_HPLL_FREQ_MASK;
195
196         return vco_freq[hpll_freq] * 1000;
197 }
198
199 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
200                       const char *name, u32 reg, int ref_freq)
201 {
202         u32 val;
203         int divider;
204
205         val = vlv_cck_read(dev_priv, reg);
206         divider = val & CCK_FREQUENCY_VALUES;
207
208         drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
209                  (divider << CCK_FREQUENCY_STATUS_SHIFT),
210                  "%s change in progress\n", name);
211
212         return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
213 }
214
215 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
216                            const char *name, u32 reg)
217 {
218         int hpll;
219
220         vlv_cck_get(dev_priv);
221
222         if (dev_priv->hpll_freq == 0)
223                 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
224
225         hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
226
227         vlv_cck_put(dev_priv);
228
229         return hpll;
230 }
231
232 static void intel_update_czclk(struct drm_i915_private *dev_priv)
233 {
234         if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
235                 return;
236
237         dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
238                                                       CCK_CZ_CLOCK_CONTROL);
239
240         drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
241                 dev_priv->czclk_freq);
242 }
243
244 /* units of 100MHz */
245 static u32 intel_fdi_link_freq(struct drm_i915_private *dev_priv,
246                                const struct intel_crtc_state *pipe_config)
247 {
248         if (HAS_DDI(dev_priv))
249                 return pipe_config->port_clock; /* SPLL */
250         else
251                 return dev_priv->fdi_pll_freq;
252 }
253
254 static const struct intel_limit intel_limits_i8xx_dac = {
255         .dot = { .min = 25000, .max = 350000 },
256         .vco = { .min = 908000, .max = 1512000 },
257         .n = { .min = 2, .max = 16 },
258         .m = { .min = 96, .max = 140 },
259         .m1 = { .min = 18, .max = 26 },
260         .m2 = { .min = 6, .max = 16 },
261         .p = { .min = 4, .max = 128 },
262         .p1 = { .min = 2, .max = 33 },
263         .p2 = { .dot_limit = 165000,
264                 .p2_slow = 4, .p2_fast = 2 },
265 };
266
267 static const struct intel_limit intel_limits_i8xx_dvo = {
268         .dot = { .min = 25000, .max = 350000 },
269         .vco = { .min = 908000, .max = 1512000 },
270         .n = { .min = 2, .max = 16 },
271         .m = { .min = 96, .max = 140 },
272         .m1 = { .min = 18, .max = 26 },
273         .m2 = { .min = 6, .max = 16 },
274         .p = { .min = 4, .max = 128 },
275         .p1 = { .min = 2, .max = 33 },
276         .p2 = { .dot_limit = 165000,
277                 .p2_slow = 4, .p2_fast = 4 },
278 };
279
280 static const struct intel_limit intel_limits_i8xx_lvds = {
281         .dot = { .min = 25000, .max = 350000 },
282         .vco = { .min = 908000, .max = 1512000 },
283         .n = { .min = 2, .max = 16 },
284         .m = { .min = 96, .max = 140 },
285         .m1 = { .min = 18, .max = 26 },
286         .m2 = { .min = 6, .max = 16 },
287         .p = { .min = 4, .max = 128 },
288         .p1 = { .min = 1, .max = 6 },
289         .p2 = { .dot_limit = 165000,
290                 .p2_slow = 14, .p2_fast = 7 },
291 };
292
293 static const struct intel_limit intel_limits_i9xx_sdvo = {
294         .dot = { .min = 20000, .max = 400000 },
295         .vco = { .min = 1400000, .max = 2800000 },
296         .n = { .min = 1, .max = 6 },
297         .m = { .min = 70, .max = 120 },
298         .m1 = { .min = 8, .max = 18 },
299         .m2 = { .min = 3, .max = 7 },
300         .p = { .min = 5, .max = 80 },
301         .p1 = { .min = 1, .max = 8 },
302         .p2 = { .dot_limit = 200000,
303                 .p2_slow = 10, .p2_fast = 5 },
304 };
305
306 static const struct intel_limit intel_limits_i9xx_lvds = {
307         .dot = { .min = 20000, .max = 400000 },
308         .vco = { .min = 1400000, .max = 2800000 },
309         .n = { .min = 1, .max = 6 },
310         .m = { .min = 70, .max = 120 },
311         .m1 = { .min = 8, .max = 18 },
312         .m2 = { .min = 3, .max = 7 },
313         .p = { .min = 7, .max = 98 },
314         .p1 = { .min = 1, .max = 8 },
315         .p2 = { .dot_limit = 112000,
316                 .p2_slow = 14, .p2_fast = 7 },
317 };
318
319
320 static const struct intel_limit intel_limits_g4x_sdvo = {
321         .dot = { .min = 25000, .max = 270000 },
322         .vco = { .min = 1750000, .max = 3500000},
323         .n = { .min = 1, .max = 4 },
324         .m = { .min = 104, .max = 138 },
325         .m1 = { .min = 17, .max = 23 },
326         .m2 = { .min = 5, .max = 11 },
327         .p = { .min = 10, .max = 30 },
328         .p1 = { .min = 1, .max = 3},
329         .p2 = { .dot_limit = 270000,
330                 .p2_slow = 10,
331                 .p2_fast = 10
332         },
333 };
334
335 static const struct intel_limit intel_limits_g4x_hdmi = {
336         .dot = { .min = 22000, .max = 400000 },
337         .vco = { .min = 1750000, .max = 3500000},
338         .n = { .min = 1, .max = 4 },
339         .m = { .min = 104, .max = 138 },
340         .m1 = { .min = 16, .max = 23 },
341         .m2 = { .min = 5, .max = 11 },
342         .p = { .min = 5, .max = 80 },
343         .p1 = { .min = 1, .max = 8},
344         .p2 = { .dot_limit = 165000,
345                 .p2_slow = 10, .p2_fast = 5 },
346 };
347
348 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
349         .dot = { .min = 20000, .max = 115000 },
350         .vco = { .min = 1750000, .max = 3500000 },
351         .n = { .min = 1, .max = 3 },
352         .m = { .min = 104, .max = 138 },
353         .m1 = { .min = 17, .max = 23 },
354         .m2 = { .min = 5, .max = 11 },
355         .p = { .min = 28, .max = 112 },
356         .p1 = { .min = 2, .max = 8 },
357         .p2 = { .dot_limit = 0,
358                 .p2_slow = 14, .p2_fast = 14
359         },
360 };
361
362 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
363         .dot = { .min = 80000, .max = 224000 },
364         .vco = { .min = 1750000, .max = 3500000 },
365         .n = { .min = 1, .max = 3 },
366         .m = { .min = 104, .max = 138 },
367         .m1 = { .min = 17, .max = 23 },
368         .m2 = { .min = 5, .max = 11 },
369         .p = { .min = 14, .max = 42 },
370         .p1 = { .min = 2, .max = 6 },
371         .p2 = { .dot_limit = 0,
372                 .p2_slow = 7, .p2_fast = 7
373         },
374 };
375
376 static const struct intel_limit pnv_limits_sdvo = {
377         .dot = { .min = 20000, .max = 400000},
378         .vco = { .min = 1700000, .max = 3500000 },
379         /* Pineview's Ncounter is a ring counter */
380         .n = { .min = 3, .max = 6 },
381         .m = { .min = 2, .max = 256 },
382         /* Pineview only has one combined m divider, which we treat as m2. */
383         .m1 = { .min = 0, .max = 0 },
384         .m2 = { .min = 0, .max = 254 },
385         .p = { .min = 5, .max = 80 },
386         .p1 = { .min = 1, .max = 8 },
387         .p2 = { .dot_limit = 200000,
388                 .p2_slow = 10, .p2_fast = 5 },
389 };
390
391 static const struct intel_limit pnv_limits_lvds = {
392         .dot = { .min = 20000, .max = 400000 },
393         .vco = { .min = 1700000, .max = 3500000 },
394         .n = { .min = 3, .max = 6 },
395         .m = { .min = 2, .max = 256 },
396         .m1 = { .min = 0, .max = 0 },
397         .m2 = { .min = 0, .max = 254 },
398         .p = { .min = 7, .max = 112 },
399         .p1 = { .min = 1, .max = 8 },
400         .p2 = { .dot_limit = 112000,
401                 .p2_slow = 14, .p2_fast = 14 },
402 };
403
404 /* Ironlake / Sandybridge
405  *
406  * We calculate clock using (register_value + 2) for N/M1/M2, so here
407  * the range value for them is (actual_value - 2).
408  */
409 static const struct intel_limit ilk_limits_dac = {
410         .dot = { .min = 25000, .max = 350000 },
411         .vco = { .min = 1760000, .max = 3510000 },
412         .n = { .min = 1, .max = 5 },
413         .m = { .min = 79, .max = 127 },
414         .m1 = { .min = 12, .max = 22 },
415         .m2 = { .min = 5, .max = 9 },
416         .p = { .min = 5, .max = 80 },
417         .p1 = { .min = 1, .max = 8 },
418         .p2 = { .dot_limit = 225000,
419                 .p2_slow = 10, .p2_fast = 5 },
420 };
421
422 static const struct intel_limit ilk_limits_single_lvds = {
423         .dot = { .min = 25000, .max = 350000 },
424         .vco = { .min = 1760000, .max = 3510000 },
425         .n = { .min = 1, .max = 3 },
426         .m = { .min = 79, .max = 118 },
427         .m1 = { .min = 12, .max = 22 },
428         .m2 = { .min = 5, .max = 9 },
429         .p = { .min = 28, .max = 112 },
430         .p1 = { .min = 2, .max = 8 },
431         .p2 = { .dot_limit = 225000,
432                 .p2_slow = 14, .p2_fast = 14 },
433 };
434
435 static const struct intel_limit ilk_limits_dual_lvds = {
436         .dot = { .min = 25000, .max = 350000 },
437         .vco = { .min = 1760000, .max = 3510000 },
438         .n = { .min = 1, .max = 3 },
439         .m = { .min = 79, .max = 127 },
440         .m1 = { .min = 12, .max = 22 },
441         .m2 = { .min = 5, .max = 9 },
442         .p = { .min = 14, .max = 56 },
443         .p1 = { .min = 2, .max = 8 },
444         .p2 = { .dot_limit = 225000,
445                 .p2_slow = 7, .p2_fast = 7 },
446 };
447
448 /* LVDS 100mhz refclk limits. */
449 static const struct intel_limit ilk_limits_single_lvds_100m = {
450         .dot = { .min = 25000, .max = 350000 },
451         .vco = { .min = 1760000, .max = 3510000 },
452         .n = { .min = 1, .max = 2 },
453         .m = { .min = 79, .max = 126 },
454         .m1 = { .min = 12, .max = 22 },
455         .m2 = { .min = 5, .max = 9 },
456         .p = { .min = 28, .max = 112 },
457         .p1 = { .min = 2, .max = 8 },
458         .p2 = { .dot_limit = 225000,
459                 .p2_slow = 14, .p2_fast = 14 },
460 };
461
462 static const struct intel_limit ilk_limits_dual_lvds_100m = {
463         .dot = { .min = 25000, .max = 350000 },
464         .vco = { .min = 1760000, .max = 3510000 },
465         .n = { .min = 1, .max = 3 },
466         .m = { .min = 79, .max = 126 },
467         .m1 = { .min = 12, .max = 22 },
468         .m2 = { .min = 5, .max = 9 },
469         .p = { .min = 14, .max = 42 },
470         .p1 = { .min = 2, .max = 6 },
471         .p2 = { .dot_limit = 225000,
472                 .p2_slow = 7, .p2_fast = 7 },
473 };
474
475 static const struct intel_limit intel_limits_vlv = {
476          /*
477           * These are the data rate limits (measured in fast clocks)
478           * since those are the strictest limits we have. The fast
479           * clock and actual rate limits are more relaxed, so checking
480           * them would make no difference.
481           */
482         .dot = { .min = 25000 * 5, .max = 270000 * 5 },
483         .vco = { .min = 4000000, .max = 6000000 },
484         .n = { .min = 1, .max = 7 },
485         .m1 = { .min = 2, .max = 3 },
486         .m2 = { .min = 11, .max = 156 },
487         .p1 = { .min = 2, .max = 3 },
488         .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
489 };
490
491 static const struct intel_limit intel_limits_chv = {
492         /*
493          * These are the data rate limits (measured in fast clocks)
494          * since those are the strictest limits we have.  The fast
495          * clock and actual rate limits are more relaxed, so checking
496          * them would make no difference.
497          */
498         .dot = { .min = 25000 * 5, .max = 540000 * 5},
499         .vco = { .min = 4800000, .max = 6480000 },
500         .n = { .min = 1, .max = 1 },
501         .m1 = { .min = 2, .max = 2 },
502         .m2 = { .min = 24 << 22, .max = 175 << 22 },
503         .p1 = { .min = 2, .max = 4 },
504         .p2 = { .p2_slow = 1, .p2_fast = 14 },
505 };
506
507 static const struct intel_limit intel_limits_bxt = {
508         /* FIXME: find real dot limits */
509         .dot = { .min = 0, .max = INT_MAX },
510         .vco = { .min = 4800000, .max = 6700000 },
511         .n = { .min = 1, .max = 1 },
512         .m1 = { .min = 2, .max = 2 },
513         /* FIXME: find real m2 limits */
514         .m2 = { .min = 2 << 22, .max = 255 << 22 },
515         .p1 = { .min = 2, .max = 4 },
516         .p2 = { .p2_slow = 1, .p2_fast = 20 },
517 };
518
519 /* WA Display #0827: Gen9:all */
520 static void
521 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
522 {
523         if (enable)
524                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
525                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
526         else
527                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
528                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
529 }
530
531 /* Wa_2006604312:icl,ehl */
532 static void
533 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
534                        bool enable)
535 {
536         if (enable)
537                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
538                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
539         else
540                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
541                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
542 }
543
544 static bool
545 needs_modeset(const struct intel_crtc_state *state)
546 {
547         return drm_atomic_crtc_needs_modeset(&state->uapi);
548 }
549
550 static bool
551 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
552 {
553         return crtc_state->master_transcoder != INVALID_TRANSCODER;
554 }
555
556 static bool
557 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
558 {
559         return crtc_state->sync_mode_slaves_mask != 0;
560 }
561
562 bool
563 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
564 {
565         return is_trans_port_sync_master(crtc_state) ||
566                 is_trans_port_sync_slave(crtc_state);
567 }
568
569 /*
570  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
571  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
572  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
573  * The helpers' return value is the rate of the clock that is fed to the
574  * display engine's pipe which can be the above fast dot clock rate or a
575  * divided-down version of it.
576  */
577 /* m1 is reserved as 0 in Pineview, n is a ring counter */
578 static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
579 {
580         clock->m = clock->m2 + 2;
581         clock->p = clock->p1 * clock->p2;
582         if (WARN_ON(clock->n == 0 || clock->p == 0))
583                 return 0;
584         clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
585         clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
586
587         return clock->dot;
588 }
589
590 static u32 i9xx_dpll_compute_m(struct dpll *dpll)
591 {
592         return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
593 }
594
595 static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
596 {
597         clock->m = i9xx_dpll_compute_m(clock);
598         clock->p = clock->p1 * clock->p2;
599         if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
600                 return 0;
601         clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
602         clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
603
604         return clock->dot;
605 }
606
607 static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
608 {
609         clock->m = clock->m1 * clock->m2;
610         clock->p = clock->p1 * clock->p2;
611         if (WARN_ON(clock->n == 0 || clock->p == 0))
612                 return 0;
613         clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
614         clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
615
616         return clock->dot / 5;
617 }
618
619 int chv_calc_dpll_params(int refclk, struct dpll *clock)
620 {
621         clock->m = clock->m1 * clock->m2;
622         clock->p = clock->p1 * clock->p2;
623         if (WARN_ON(clock->n == 0 || clock->p == 0))
624                 return 0;
625         clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
626                                            clock->n << 22);
627         clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
628
629         return clock->dot / 5;
630 }
631
632 /*
633  * Returns whether the given set of divisors are valid for a given refclk with
634  * the given connectors.
635  */
636 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
637                                const struct intel_limit *limit,
638                                const struct dpll *clock)
639 {
640         if (clock->n < limit->n.min || limit->n.max < clock->n)
641                 return false;
642         if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
643                 return false;
644         if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
645                 return false;
646         if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
647                 return false;
648
649         if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
650             !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
651                 if (clock->m1 <= clock->m2)
652                         return false;
653
654         if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
655             !IS_GEN9_LP(dev_priv)) {
656                 if (clock->p < limit->p.min || limit->p.max < clock->p)
657                         return false;
658                 if (clock->m < limit->m.min || limit->m.max < clock->m)
659                         return false;
660         }
661
662         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
663                 return false;
664         /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
665          * connector, etc., rather than just a single range.
666          */
667         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
668                 return false;
669
670         return true;
671 }
672
673 static int
674 i9xx_select_p2_div(const struct intel_limit *limit,
675                    const struct intel_crtc_state *crtc_state,
676                    int target)
677 {
678         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
679
680         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
681                 /*
682                  * For LVDS just rely on its current settings for dual-channel.
683                  * We haven't figured out how to reliably set up different
684                  * single/dual channel state, if we even can.
685                  */
686                 if (intel_is_dual_link_lvds(dev_priv))
687                         return limit->p2.p2_fast;
688                 else
689                         return limit->p2.p2_slow;
690         } else {
691                 if (target < limit->p2.dot_limit)
692                         return limit->p2.p2_slow;
693                 else
694                         return limit->p2.p2_fast;
695         }
696 }
697
698 /*
699  * Returns a set of divisors for the desired target clock with the given
700  * refclk, or FALSE.  The returned values represent the clock equation:
701  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
702  *
703  * Target and reference clocks are specified in kHz.
704  *
705  * If match_clock is provided, then best_clock P divider must match the P
706  * divider from @match_clock used for LVDS downclocking.
707  */
708 static bool
709 i9xx_find_best_dpll(const struct intel_limit *limit,
710                     struct intel_crtc_state *crtc_state,
711                     int target, int refclk, struct dpll *match_clock,
712                     struct dpll *best_clock)
713 {
714         struct drm_device *dev = crtc_state->uapi.crtc->dev;
715         struct dpll clock;
716         int err = target;
717
718         memset(best_clock, 0, sizeof(*best_clock));
719
720         clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
721
722         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
723              clock.m1++) {
724                 for (clock.m2 = limit->m2.min;
725                      clock.m2 <= limit->m2.max; clock.m2++) {
726                         if (clock.m2 >= clock.m1)
727                                 break;
728                         for (clock.n = limit->n.min;
729                              clock.n <= limit->n.max; clock.n++) {
730                                 for (clock.p1 = limit->p1.min;
731                                         clock.p1 <= limit->p1.max; clock.p1++) {
732                                         int this_err;
733
734                                         i9xx_calc_dpll_params(refclk, &clock);
735                                         if (!intel_pll_is_valid(to_i915(dev),
736                                                                 limit,
737                                                                 &clock))
738                                                 continue;
739                                         if (match_clock &&
740                                             clock.p != match_clock->p)
741                                                 continue;
742
743                                         this_err = abs(clock.dot - target);
744                                         if (this_err < err) {
745                                                 *best_clock = clock;
746                                                 err = this_err;
747                                         }
748                                 }
749                         }
750                 }
751         }
752
753         return (err != target);
754 }
755
756 /*
757  * Returns a set of divisors for the desired target clock with the given
758  * refclk, or FALSE.  The returned values represent the clock equation:
759  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
760  *
761  * Target and reference clocks are specified in kHz.
762  *
763  * If match_clock is provided, then best_clock P divider must match the P
764  * divider from @match_clock used for LVDS downclocking.
765  */
766 static bool
767 pnv_find_best_dpll(const struct intel_limit *limit,
768                    struct intel_crtc_state *crtc_state,
769                    int target, int refclk, struct dpll *match_clock,
770                    struct dpll *best_clock)
771 {
772         struct drm_device *dev = crtc_state->uapi.crtc->dev;
773         struct dpll clock;
774         int err = target;
775
776         memset(best_clock, 0, sizeof(*best_clock));
777
778         clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
779
780         for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
781              clock.m1++) {
782                 for (clock.m2 = limit->m2.min;
783                      clock.m2 <= limit->m2.max; clock.m2++) {
784                         for (clock.n = limit->n.min;
785                              clock.n <= limit->n.max; clock.n++) {
786                                 for (clock.p1 = limit->p1.min;
787                                         clock.p1 <= limit->p1.max; clock.p1++) {
788                                         int this_err;
789
790                                         pnv_calc_dpll_params(refclk, &clock);
791                                         if (!intel_pll_is_valid(to_i915(dev),
792                                                                 limit,
793                                                                 &clock))
794                                                 continue;
795                                         if (match_clock &&
796                                             clock.p != match_clock->p)
797                                                 continue;
798
799                                         this_err = abs(clock.dot - target);
800                                         if (this_err < err) {
801                                                 *best_clock = clock;
802                                                 err = this_err;
803                                         }
804                                 }
805                         }
806                 }
807         }
808
809         return (err != target);
810 }
811
812 /*
813  * Returns a set of divisors for the desired target clock with the given
814  * refclk, or FALSE.  The returned values represent the clock equation:
815  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
816  *
817  * Target and reference clocks are specified in kHz.
818  *
819  * If match_clock is provided, then best_clock P divider must match the P
820  * divider from @match_clock used for LVDS downclocking.
821  */
822 static bool
823 g4x_find_best_dpll(const struct intel_limit *limit,
824                    struct intel_crtc_state *crtc_state,
825                    int target, int refclk, struct dpll *match_clock,
826                    struct dpll *best_clock)
827 {
828         struct drm_device *dev = crtc_state->uapi.crtc->dev;
829         struct dpll clock;
830         int max_n;
831         bool found = false;
832         /* approximately equals target * 0.00585 */
833         int err_most = (target >> 8) + (target >> 9);
834
835         memset(best_clock, 0, sizeof(*best_clock));
836
837         clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
838
839         max_n = limit->n.max;
840         /* based on hardware requirement, prefer smaller n to precision */
841         for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
842                 /* based on hardware requirement, prefere larger m1,m2 */
843                 for (clock.m1 = limit->m1.max;
844                      clock.m1 >= limit->m1.min; clock.m1--) {
845                         for (clock.m2 = limit->m2.max;
846                              clock.m2 >= limit->m2.min; clock.m2--) {
847                                 for (clock.p1 = limit->p1.max;
848                                      clock.p1 >= limit->p1.min; clock.p1--) {
849                                         int this_err;
850
851                                         i9xx_calc_dpll_params(refclk, &clock);
852                                         if (!intel_pll_is_valid(to_i915(dev),
853                                                                 limit,
854                                                                 &clock))
855                                                 continue;
856
857                                         this_err = abs(clock.dot - target);
858                                         if (this_err < err_most) {
859                                                 *best_clock = clock;
860                                                 err_most = this_err;
861                                                 max_n = clock.n;
862                                                 found = true;
863                                         }
864                                 }
865                         }
866                 }
867         }
868         return found;
869 }
870
871 /*
872  * Check if the calculated PLL configuration is more optimal compared to the
873  * best configuration and error found so far. Return the calculated error.
874  */
875 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
876                                const struct dpll *calculated_clock,
877                                const struct dpll *best_clock,
878                                unsigned int best_error_ppm,
879                                unsigned int *error_ppm)
880 {
881         /*
882          * For CHV ignore the error and consider only the P value.
883          * Prefer a bigger P value based on HW requirements.
884          */
885         if (IS_CHERRYVIEW(to_i915(dev))) {
886                 *error_ppm = 0;
887
888                 return calculated_clock->p > best_clock->p;
889         }
890
891         if (drm_WARN_ON_ONCE(dev, !target_freq))
892                 return false;
893
894         *error_ppm = div_u64(1000000ULL *
895                                 abs(target_freq - calculated_clock->dot),
896                              target_freq);
897         /*
898          * Prefer a better P value over a better (smaller) error if the error
899          * is small. Ensure this preference for future configurations too by
900          * setting the error to 0.
901          */
902         if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
903                 *error_ppm = 0;
904
905                 return true;
906         }
907
908         return *error_ppm + 10 < best_error_ppm;
909 }
910
911 /*
912  * Returns a set of divisors for the desired target clock with the given
913  * refclk, or FALSE.  The returned values represent the clock equation:
914  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
915  */
916 static bool
917 vlv_find_best_dpll(const struct intel_limit *limit,
918                    struct intel_crtc_state *crtc_state,
919                    int target, int refclk, struct dpll *match_clock,
920                    struct dpll *best_clock)
921 {
922         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
923         struct drm_device *dev = crtc->base.dev;
924         struct dpll clock;
925         unsigned int bestppm = 1000000;
926         /* min update 19.2 MHz */
927         int max_n = min(limit->n.max, refclk / 19200);
928         bool found = false;
929
930         target *= 5; /* fast clock */
931
932         memset(best_clock, 0, sizeof(*best_clock));
933
934         /* based on hardware requirement, prefer smaller n to precision */
935         for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
936                 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
937                         for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
938                              clock.p2 -= clock.p2 > 10 ? 2 : 1) {
939                                 clock.p = clock.p1 * clock.p2;
940                                 /* based on hardware requirement, prefer bigger m1,m2 values */
941                                 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
942                                         unsigned int ppm;
943
944                                         clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
945                                                                      refclk * clock.m1);
946
947                                         vlv_calc_dpll_params(refclk, &clock);
948
949                                         if (!intel_pll_is_valid(to_i915(dev),
950                                                                 limit,
951                                                                 &clock))
952                                                 continue;
953
954                                         if (!vlv_PLL_is_optimal(dev, target,
955                                                                 &clock,
956                                                                 best_clock,
957                                                                 bestppm, &ppm))
958                                                 continue;
959
960                                         *best_clock = clock;
961                                         bestppm = ppm;
962                                         found = true;
963                                 }
964                         }
965                 }
966         }
967
968         return found;
969 }
970
971 /*
972  * Returns a set of divisors for the desired target clock with the given
973  * refclk, or FALSE.  The returned values represent the clock equation:
974  * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
975  */
976 static bool
977 chv_find_best_dpll(const struct intel_limit *limit,
978                    struct intel_crtc_state *crtc_state,
979                    int target, int refclk, struct dpll *match_clock,
980                    struct dpll *best_clock)
981 {
982         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
983         struct drm_device *dev = crtc->base.dev;
984         unsigned int best_error_ppm;
985         struct dpll clock;
986         u64 m2;
987         int found = false;
988
989         memset(best_clock, 0, sizeof(*best_clock));
990         best_error_ppm = 1000000;
991
992         /*
993          * Based on hardware doc, the n always set to 1, and m1 always
994          * set to 2.  If requires to support 200Mhz refclk, we need to
995          * revisit this because n may not 1 anymore.
996          */
997         clock.n = 1, clock.m1 = 2;
998         target *= 5;    /* fast clock */
999
1000         for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
1001                 for (clock.p2 = limit->p2.p2_fast;
1002                                 clock.p2 >= limit->p2.p2_slow;
1003                                 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
1004                         unsigned int error_ppm;
1005
1006                         clock.p = clock.p1 * clock.p2;
1007
1008                         m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
1009                                                    refclk * clock.m1);
1010
1011                         if (m2 > INT_MAX/clock.m1)
1012                                 continue;
1013
1014                         clock.m2 = m2;
1015
1016                         chv_calc_dpll_params(refclk, &clock);
1017
1018                         if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
1019                                 continue;
1020
1021                         if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
1022                                                 best_error_ppm, &error_ppm))
1023                                 continue;
1024
1025                         *best_clock = clock;
1026                         best_error_ppm = error_ppm;
1027                         found = true;
1028                 }
1029         }
1030
1031         return found;
1032 }
1033
1034 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
1035                         struct dpll *best_clock)
1036 {
1037         int refclk = 100000;
1038         const struct intel_limit *limit = &intel_limits_bxt;
1039
1040         return chv_find_best_dpll(limit, crtc_state,
1041                                   crtc_state->port_clock, refclk,
1042                                   NULL, best_clock);
1043 }
1044
1045 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
1046                                     enum pipe pipe)
1047 {
1048         i915_reg_t reg = PIPEDSL(pipe);
1049         u32 line1, line2;
1050         u32 line_mask;
1051
1052         if (IS_GEN(dev_priv, 2))
1053                 line_mask = DSL_LINEMASK_GEN2;
1054         else
1055                 line_mask = DSL_LINEMASK_GEN3;
1056
1057         line1 = intel_de_read(dev_priv, reg) & line_mask;
1058         msleep(5);
1059         line2 = intel_de_read(dev_priv, reg) & line_mask;
1060
1061         return line1 != line2;
1062 }
1063
1064 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
1065 {
1066         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1067         enum pipe pipe = crtc->pipe;
1068
1069         /* Wait for the display line to settle/start moving */
1070         if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
1071                 drm_err(&dev_priv->drm,
1072                         "pipe %c scanline %s wait timed out\n",
1073                         pipe_name(pipe), onoff(state));
1074 }
1075
1076 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
1077 {
1078         wait_for_pipe_scanline_moving(crtc, false);
1079 }
1080
1081 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
1082 {
1083         wait_for_pipe_scanline_moving(crtc, true);
1084 }
1085
1086 static void
1087 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
1088 {
1089         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1090         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1091
1092         if (INTEL_GEN(dev_priv) >= 4) {
1093                 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1094                 i915_reg_t reg = PIPECONF(cpu_transcoder);
1095
1096                 /* Wait for the Pipe State to go off */
1097                 if (intel_de_wait_for_clear(dev_priv, reg,
1098                                             I965_PIPECONF_ACTIVE, 100))
1099                         drm_WARN(&dev_priv->drm, 1,
1100                                  "pipe_off wait timed out\n");
1101         } else {
1102                 intel_wait_for_pipe_scanline_stopped(crtc);
1103         }
1104 }
1105
1106 /* Only for pre-ILK configs */
1107 void assert_pll(struct drm_i915_private *dev_priv,
1108                 enum pipe pipe, bool state)
1109 {
1110         u32 val;
1111         bool cur_state;
1112
1113         val = intel_de_read(dev_priv, DPLL(pipe));
1114         cur_state = !!(val & DPLL_VCO_ENABLE);
1115         I915_STATE_WARN(cur_state != state,
1116              "PLL state assertion failure (expected %s, current %s)\n",
1117                         onoff(state), onoff(cur_state));
1118 }
1119
1120 /* XXX: the dsi pll is shared between MIPI DSI ports */
1121 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1122 {
1123         u32 val;
1124         bool cur_state;
1125
1126         vlv_cck_get(dev_priv);
1127         val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1128         vlv_cck_put(dev_priv);
1129
1130         cur_state = val & DSI_PLL_VCO_EN;
1131         I915_STATE_WARN(cur_state != state,
1132              "DSI PLL state assertion failure (expected %s, current %s)\n",
1133                         onoff(state), onoff(cur_state));
1134 }
1135
1136 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1137                           enum pipe pipe, bool state)
1138 {
1139         bool cur_state;
1140
1141         if (HAS_DDI(dev_priv)) {
1142                 /*
1143                  * DDI does not have a specific FDI_TX register.
1144                  *
1145                  * FDI is never fed from EDP transcoder
1146                  * so pipe->transcoder cast is fine here.
1147                  */
1148                 enum transcoder cpu_transcoder = (enum transcoder)pipe;
1149                 u32 val = intel_de_read(dev_priv,
1150                                         TRANS_DDI_FUNC_CTL(cpu_transcoder));
1151                 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1152         } else {
1153                 u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1154                 cur_state = !!(val & FDI_TX_ENABLE);
1155         }
1156         I915_STATE_WARN(cur_state != state,
1157              "FDI TX state assertion failure (expected %s, current %s)\n",
1158                         onoff(state), onoff(cur_state));
1159 }
1160 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1161 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1162
1163 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1164                           enum pipe pipe, bool state)
1165 {
1166         u32 val;
1167         bool cur_state;
1168
1169         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1170         cur_state = !!(val & FDI_RX_ENABLE);
1171         I915_STATE_WARN(cur_state != state,
1172              "FDI RX state assertion failure (expected %s, current %s)\n",
1173                         onoff(state), onoff(cur_state));
1174 }
1175 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1176 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1177
1178 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1179                                       enum pipe pipe)
1180 {
1181         u32 val;
1182
1183         /* ILK FDI PLL is always enabled */
1184         if (IS_GEN(dev_priv, 5))
1185                 return;
1186
1187         /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1188         if (HAS_DDI(dev_priv))
1189                 return;
1190
1191         val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
1192         I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1193 }
1194
1195 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1196                        enum pipe pipe, bool state)
1197 {
1198         u32 val;
1199         bool cur_state;
1200
1201         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
1202         cur_state = !!(val & FDI_RX_PLL_ENABLE);
1203         I915_STATE_WARN(cur_state != state,
1204              "FDI RX PLL assertion failure (expected %s, current %s)\n",
1205                         onoff(state), onoff(cur_state));
1206 }
1207
1208 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
1209 {
1210         i915_reg_t pp_reg;
1211         u32 val;
1212         enum pipe panel_pipe = INVALID_PIPE;
1213         bool locked = true;
1214
1215         if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
1216                 return;
1217
1218         if (HAS_PCH_SPLIT(dev_priv)) {
1219                 u32 port_sel;
1220
1221                 pp_reg = PP_CONTROL(0);
1222                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1223
1224                 switch (port_sel) {
1225                 case PANEL_PORT_SELECT_LVDS:
1226                         intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
1227                         break;
1228                 case PANEL_PORT_SELECT_DPA:
1229                         intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
1230                         break;
1231                 case PANEL_PORT_SELECT_DPC:
1232                         intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
1233                         break;
1234                 case PANEL_PORT_SELECT_DPD:
1235                         intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
1236                         break;
1237                 default:
1238                         MISSING_CASE(port_sel);
1239                         break;
1240                 }
1241         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1242                 /* presumably write lock depends on pipe, not port select */
1243                 pp_reg = PP_CONTROL(pipe);
1244                 panel_pipe = pipe;
1245         } else {
1246                 u32 port_sel;
1247
1248                 pp_reg = PP_CONTROL(0);
1249                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
1250
1251                 drm_WARN_ON(&dev_priv->drm,
1252                             port_sel != PANEL_PORT_SELECT_LVDS);
1253                 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
1254         }
1255
1256         val = intel_de_read(dev_priv, pp_reg);
1257         if (!(val & PANEL_POWER_ON) ||
1258             ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1259                 locked = false;
1260
1261         I915_STATE_WARN(panel_pipe == pipe && locked,
1262              "panel assertion failure, pipe %c regs locked\n",
1263              pipe_name(pipe));
1264 }
1265
1266 void assert_pipe(struct drm_i915_private *dev_priv,
1267                  enum transcoder cpu_transcoder, bool state)
1268 {
1269         bool cur_state;
1270         enum intel_display_power_domain power_domain;
1271         intel_wakeref_t wakeref;
1272
1273         /* we keep both pipes enabled on 830 */
1274         if (IS_I830(dev_priv))
1275                 state = true;
1276
1277         power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1278         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1279         if (wakeref) {
1280                 u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1281                 cur_state = !!(val & PIPECONF_ENABLE);
1282
1283                 intel_display_power_put(dev_priv, power_domain, wakeref);
1284         } else {
1285                 cur_state = false;
1286         }
1287
1288         I915_STATE_WARN(cur_state != state,
1289                         "transcoder %s assertion failure (expected %s, current %s)\n",
1290                         transcoder_name(cpu_transcoder),
1291                         onoff(state), onoff(cur_state));
1292 }
1293
1294 static void assert_plane(struct intel_plane *plane, bool state)
1295 {
1296         enum pipe pipe;
1297         bool cur_state;
1298
1299         cur_state = plane->get_hw_state(plane, &pipe);
1300
1301         I915_STATE_WARN(cur_state != state,
1302                         "%s assertion failure (expected %s, current %s)\n",
1303                         plane->base.name, onoff(state), onoff(cur_state));
1304 }
1305
1306 #define assert_plane_enabled(p) assert_plane(p, true)
1307 #define assert_plane_disabled(p) assert_plane(p, false)
1308
1309 static void assert_planes_disabled(struct intel_crtc *crtc)
1310 {
1311         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1312         struct intel_plane *plane;
1313
1314         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
1315                 assert_plane_disabled(plane);
1316 }
1317
1318 static void assert_vblank_disabled(struct drm_crtc *crtc)
1319 {
1320         if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1321                 drm_crtc_vblank_put(crtc);
1322 }
1323
1324 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1325                                     enum pipe pipe)
1326 {
1327         u32 val;
1328         bool enabled;
1329
1330         val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
1331         enabled = !!(val & TRANS_ENABLE);
1332         I915_STATE_WARN(enabled,
1333              "transcoder assertion failed, should be off on pipe %c but is still active\n",
1334              pipe_name(pipe));
1335 }
1336
1337 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1338                                    enum pipe pipe, enum port port,
1339                                    i915_reg_t dp_reg)
1340 {
1341         enum pipe port_pipe;
1342         bool state;
1343
1344         state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
1345
1346         I915_STATE_WARN(state && port_pipe == pipe,
1347                         "PCH DP %c enabled on transcoder %c, should be disabled\n",
1348                         port_name(port), pipe_name(pipe));
1349
1350         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1351                         "IBX PCH DP %c still using transcoder B\n",
1352                         port_name(port));
1353 }
1354
1355 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1356                                      enum pipe pipe, enum port port,
1357                                      i915_reg_t hdmi_reg)
1358 {
1359         enum pipe port_pipe;
1360         bool state;
1361
1362         state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
1363
1364         I915_STATE_WARN(state && port_pipe == pipe,
1365                         "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
1366                         port_name(port), pipe_name(pipe));
1367
1368         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
1369                         "IBX PCH HDMI %c still using transcoder B\n",
1370                         port_name(port));
1371 }
1372
1373 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1374                                       enum pipe pipe)
1375 {
1376         enum pipe port_pipe;
1377
1378         assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
1379         assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
1380         assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
1381
1382         I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
1383                         port_pipe == pipe,
1384                         "PCH VGA enabled on transcoder %c, should be disabled\n",
1385                         pipe_name(pipe));
1386
1387         I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
1388                         port_pipe == pipe,
1389                         "PCH LVDS enabled on transcoder %c, should be disabled\n",
1390                         pipe_name(pipe));
1391
1392         /* PCH SDVOB multiplex with HDMIB */
1393         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
1394         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
1395         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
1396 }
1397
1398 static void _vlv_enable_pll(struct intel_crtc *crtc,
1399                             const struct intel_crtc_state *pipe_config)
1400 {
1401         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1402         enum pipe pipe = crtc->pipe;
1403
1404         intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1405         intel_de_posting_read(dev_priv, DPLL(pipe));
1406         udelay(150);
1407
1408         if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1409                 drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1410 }
1411
1412 static void vlv_enable_pll(struct intel_crtc *crtc,
1413                            const struct intel_crtc_state *pipe_config)
1414 {
1415         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1416         enum pipe pipe = crtc->pipe;
1417
1418         assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1419
1420         /* PLL is protected by panel, make sure we can write it */
1421         assert_panel_unlocked(dev_priv, pipe);
1422
1423         if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1424                 _vlv_enable_pll(crtc, pipe_config);
1425
1426         intel_de_write(dev_priv, DPLL_MD(pipe),
1427                        pipe_config->dpll_hw_state.dpll_md);
1428         intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1429 }
1430
1431
1432 static void _chv_enable_pll(struct intel_crtc *crtc,
1433                             const struct intel_crtc_state *pipe_config)
1434 {
1435         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1436         enum pipe pipe = crtc->pipe;
1437         enum dpio_channel port = vlv_pipe_to_channel(pipe);
1438         u32 tmp;
1439
1440         vlv_dpio_get(dev_priv);
1441
1442         /* Enable back the 10bit clock to display controller */
1443         tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1444         tmp |= DPIO_DCLKP_EN;
1445         vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1446
1447         vlv_dpio_put(dev_priv);
1448
1449         /*
1450          * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1451          */
1452         udelay(1);
1453
1454         /* Enable PLL */
1455         intel_de_write(dev_priv, DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1456
1457         /* Check PLL is locked */
1458         if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1459                 drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1460 }
1461
1462 static void chv_enable_pll(struct intel_crtc *crtc,
1463                            const struct intel_crtc_state *pipe_config)
1464 {
1465         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1466         enum pipe pipe = crtc->pipe;
1467
1468         assert_pipe_disabled(dev_priv, pipe_config->cpu_transcoder);
1469
1470         /* PLL is protected by panel, make sure we can write it */
1471         assert_panel_unlocked(dev_priv, pipe);
1472
1473         if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1474                 _chv_enable_pll(crtc, pipe_config);
1475
1476         if (pipe != PIPE_A) {
1477                 /*
1478                  * WaPixelRepeatModeFixForC0:chv
1479                  *
1480                  * DPLLCMD is AWOL. Use chicken bits to propagate
1481                  * the value from DPLLBMD to either pipe B or C.
1482                  */
1483                 intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1484                 intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1485                                pipe_config->dpll_hw_state.dpll_md);
1486                 intel_de_write(dev_priv, CBR4_VLV, 0);
1487                 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1488
1489                 /*
1490                  * DPLLB VGA mode also seems to cause problems.
1491                  * We should always have it disabled.
1492                  */
1493                 drm_WARN_ON(&dev_priv->drm,
1494                             (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1495                              DPLL_VGA_MODE_DIS) == 0);
1496         } else {
1497                 intel_de_write(dev_priv, DPLL_MD(pipe),
1498                                pipe_config->dpll_hw_state.dpll_md);
1499                 intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1500         }
1501 }
1502
1503 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1504 {
1505         if (IS_I830(dev_priv))
1506                 return false;
1507
1508         return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1509 }
1510
1511 static void i9xx_enable_pll(struct intel_crtc *crtc,
1512                             const struct intel_crtc_state *crtc_state)
1513 {
1514         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1515         i915_reg_t reg = DPLL(crtc->pipe);
1516         u32 dpll = crtc_state->dpll_hw_state.dpll;
1517         int i;
1518
1519         assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1520
1521         /* PLL is protected by panel, make sure we can write it */
1522         if (i9xx_has_pps(dev_priv))
1523                 assert_panel_unlocked(dev_priv, crtc->pipe);
1524
1525         /*
1526          * Apparently we need to have VGA mode enabled prior to changing
1527          * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1528          * dividers, even though the register value does change.
1529          */
1530         intel_de_write(dev_priv, reg, dpll & ~DPLL_VGA_MODE_DIS);
1531         intel_de_write(dev_priv, reg, dpll);
1532
1533         /* Wait for the clocks to stabilize. */
1534         intel_de_posting_read(dev_priv, reg);
1535         udelay(150);
1536
1537         if (INTEL_GEN(dev_priv) >= 4) {
1538                 intel_de_write(dev_priv, DPLL_MD(crtc->pipe),
1539                                crtc_state->dpll_hw_state.dpll_md);
1540         } else {
1541                 /* The pixel multiplier can only be updated once the
1542                  * DPLL is enabled and the clocks are stable.
1543                  *
1544                  * So write it again.
1545                  */
1546                 intel_de_write(dev_priv, reg, dpll);
1547         }
1548
1549         /* We do this three times for luck */
1550         for (i = 0; i < 3; i++) {
1551                 intel_de_write(dev_priv, reg, dpll);
1552                 intel_de_posting_read(dev_priv, reg);
1553                 udelay(150); /* wait for warmup */
1554         }
1555 }
1556
1557 static void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1558 {
1559         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1560         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1561         enum pipe pipe = crtc->pipe;
1562
1563         /* Don't disable pipe or pipe PLLs if needed */
1564         if (IS_I830(dev_priv))
1565                 return;
1566
1567         /* Make sure the pipe isn't still relying on us */
1568         assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
1569
1570         intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1571         intel_de_posting_read(dev_priv, DPLL(pipe));
1572 }
1573
1574 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1575 {
1576         u32 val;
1577
1578         /* Make sure the pipe isn't still relying on us */
1579         assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1580
1581         val = DPLL_INTEGRATED_REF_CLK_VLV |
1582                 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1583         if (pipe != PIPE_A)
1584                 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1585
1586         intel_de_write(dev_priv, DPLL(pipe), val);
1587         intel_de_posting_read(dev_priv, DPLL(pipe));
1588 }
1589
1590 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1591 {
1592         enum dpio_channel port = vlv_pipe_to_channel(pipe);
1593         u32 val;
1594
1595         /* Make sure the pipe isn't still relying on us */
1596         assert_pipe_disabled(dev_priv, (enum transcoder)pipe);
1597
1598         val = DPLL_SSC_REF_CLK_CHV |
1599                 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1600         if (pipe != PIPE_A)
1601                 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1602
1603         intel_de_write(dev_priv, DPLL(pipe), val);
1604         intel_de_posting_read(dev_priv, DPLL(pipe));
1605
1606         vlv_dpio_get(dev_priv);
1607
1608         /* Disable 10bit clock to display controller */
1609         val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1610         val &= ~DPIO_DCLKP_EN;
1611         vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1612
1613         vlv_dpio_put(dev_priv);
1614 }
1615
1616 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1617                          struct intel_digital_port *dig_port,
1618                          unsigned int expected_mask)
1619 {
1620         u32 port_mask;
1621         i915_reg_t dpll_reg;
1622
1623         switch (dig_port->base.port) {
1624         case PORT_B:
1625                 port_mask = DPLL_PORTB_READY_MASK;
1626                 dpll_reg = DPLL(0);
1627                 break;
1628         case PORT_C:
1629                 port_mask = DPLL_PORTC_READY_MASK;
1630                 dpll_reg = DPLL(0);
1631                 expected_mask <<= 4;
1632                 break;
1633         case PORT_D:
1634                 port_mask = DPLL_PORTD_READY_MASK;
1635                 dpll_reg = DPIO_PHY_STATUS;
1636                 break;
1637         default:
1638                 BUG();
1639         }
1640
1641         if (intel_de_wait_for_register(dev_priv, dpll_reg,
1642                                        port_mask, expected_mask, 1000))
1643                 drm_WARN(&dev_priv->drm, 1,
1644                          "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
1645                          dig_port->base.base.base.id, dig_port->base.base.name,
1646                          intel_de_read(dev_priv, dpll_reg) & port_mask,
1647                          expected_mask);
1648 }
1649
1650 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
1651 {
1652         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1653         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1654         enum pipe pipe = crtc->pipe;
1655         i915_reg_t reg;
1656         u32 val, pipeconf_val;
1657
1658         /* Make sure PCH DPLL is enabled */
1659         assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
1660
1661         /* FDI must be feeding us bits for PCH ports */
1662         assert_fdi_tx_enabled(dev_priv, pipe);
1663         assert_fdi_rx_enabled(dev_priv, pipe);
1664
1665         if (HAS_PCH_CPT(dev_priv)) {
1666                 reg = TRANS_CHICKEN2(pipe);
1667                 val = intel_de_read(dev_priv, reg);
1668                 /*
1669                  * Workaround: Set the timing override bit
1670                  * before enabling the pch transcoder.
1671                  */
1672                 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1673                 /* Configure frame start delay to match the CPU */
1674                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1675                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1676                 intel_de_write(dev_priv, reg, val);
1677         }
1678
1679         reg = PCH_TRANSCONF(pipe);
1680         val = intel_de_read(dev_priv, reg);
1681         pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
1682
1683         if (HAS_PCH_IBX(dev_priv)) {
1684                 /* Configure frame start delay to match the CPU */
1685                 val &= ~TRANS_FRAME_START_DELAY_MASK;
1686                 val |= TRANS_FRAME_START_DELAY(0);
1687
1688                 /*
1689                  * Make the BPC in transcoder be consistent with
1690                  * that in pipeconf reg. For HDMI we must use 8bpc
1691                  * here for both 8bpc and 12bpc.
1692                  */
1693                 val &= ~PIPECONF_BPC_MASK;
1694                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1695                         val |= PIPECONF_8BPC;
1696                 else
1697                         val |= pipeconf_val & PIPECONF_BPC_MASK;
1698         }
1699
1700         val &= ~TRANS_INTERLACE_MASK;
1701         if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
1702                 if (HAS_PCH_IBX(dev_priv) &&
1703                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
1704                         val |= TRANS_LEGACY_INTERLACED_ILK;
1705                 else
1706                         val |= TRANS_INTERLACED;
1707         } else {
1708                 val |= TRANS_PROGRESSIVE;
1709         }
1710
1711         intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
1712         if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
1713                 drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
1714                         pipe_name(pipe));
1715 }
1716
1717 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1718                                       enum transcoder cpu_transcoder)
1719 {
1720         u32 val, pipeconf_val;
1721
1722         /* FDI must be feeding us bits for PCH ports */
1723         assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
1724         assert_fdi_rx_enabled(dev_priv, PIPE_A);
1725
1726         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1727         /* Workaround: set timing override bit. */
1728         val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1729         /* Configure frame start delay to match the CPU */
1730         val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
1731         val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
1732         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1733
1734         val = TRANS_ENABLE;
1735         pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
1736
1737         if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1738             PIPECONF_INTERLACED_ILK)
1739                 val |= TRANS_INTERLACED;
1740         else
1741                 val |= TRANS_PROGRESSIVE;
1742
1743         intel_de_write(dev_priv, LPT_TRANSCONF, val);
1744         if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
1745                                   TRANS_STATE_ENABLE, 100))
1746                 drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
1747 }
1748
1749 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1750                                        enum pipe pipe)
1751 {
1752         i915_reg_t reg;
1753         u32 val;
1754
1755         /* FDI relies on the transcoder */
1756         assert_fdi_tx_disabled(dev_priv, pipe);
1757         assert_fdi_rx_disabled(dev_priv, pipe);
1758
1759         /* Ports must be off as well */
1760         assert_pch_ports_disabled(dev_priv, pipe);
1761
1762         reg = PCH_TRANSCONF(pipe);
1763         val = intel_de_read(dev_priv, reg);
1764         val &= ~TRANS_ENABLE;
1765         intel_de_write(dev_priv, reg, val);
1766         /* wait for PCH transcoder off, transcoder state */
1767         if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
1768                 drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
1769                         pipe_name(pipe));
1770
1771         if (HAS_PCH_CPT(dev_priv)) {
1772                 /* Workaround: Clear the timing override chicken bit again. */
1773                 reg = TRANS_CHICKEN2(pipe);
1774                 val = intel_de_read(dev_priv, reg);
1775                 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1776                 intel_de_write(dev_priv, reg, val);
1777         }
1778 }
1779
1780 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
1781 {
1782         u32 val;
1783
1784         val = intel_de_read(dev_priv, LPT_TRANSCONF);
1785         val &= ~TRANS_ENABLE;
1786         intel_de_write(dev_priv, LPT_TRANSCONF, val);
1787         /* wait for PCH transcoder off, transcoder state */
1788         if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
1789                                     TRANS_STATE_ENABLE, 50))
1790                 drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
1791
1792         /* Workaround: clear timing override bit. */
1793         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
1794         val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1795         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
1796 }
1797
1798 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1799 {
1800         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1801
1802         if (HAS_PCH_LPT(dev_priv))
1803                 return PIPE_A;
1804         else
1805                 return crtc->pipe;
1806 }
1807
1808 static u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
1809 {
1810         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1811         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1812         u32 mode_flags = crtc->mode_flags;
1813
1814         /*
1815          * From Gen 11, In case of dsi cmd mode, frame counter wouldnt
1816          * have updated at the beginning of TE, if we want to use
1817          * the hw counter, then we would find it updated in only
1818          * the next TE, hence switching to sw counter.
1819          */
1820         if (mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 | I915_MODE_FLAG_DSI_USE_TE1))
1821                 return 0;
1822
1823         /*
1824          * On i965gm the hardware frame counter reads
1825          * zero when the TV encoder is enabled :(
1826          */
1827         if (IS_I965GM(dev_priv) &&
1828             (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
1829                 return 0;
1830
1831         if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
1832                 return 0xffffffff; /* full 32 bit counter */
1833         else if (INTEL_GEN(dev_priv) >= 3)
1834                 return 0xffffff; /* only 24 bits of frame count */
1835         else
1836                 return 0; /* Gen2 doesn't have a hardware frame counter */
1837 }
1838
1839 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
1840 {
1841         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1842
1843         assert_vblank_disabled(&crtc->base);
1844         drm_crtc_set_max_vblank_count(&crtc->base,
1845                                       intel_crtc_max_vblank_count(crtc_state));
1846         drm_crtc_vblank_on(&crtc->base);
1847 }
1848
1849 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
1850 {
1851         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1852
1853         drm_crtc_vblank_off(&crtc->base);
1854         assert_vblank_disabled(&crtc->base);
1855 }
1856
1857 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
1858 {
1859         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1860         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1861         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1862         enum pipe pipe = crtc->pipe;
1863         i915_reg_t reg;
1864         u32 val;
1865
1866         drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
1867
1868         assert_planes_disabled(crtc);
1869
1870         /*
1871          * A pipe without a PLL won't actually be able to drive bits from
1872          * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1873          * need the check.
1874          */
1875         if (HAS_GMCH(dev_priv)) {
1876                 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
1877                         assert_dsi_pll_enabled(dev_priv);
1878                 else
1879                         assert_pll_enabled(dev_priv, pipe);
1880         } else {
1881                 if (new_crtc_state->has_pch_encoder) {
1882                         /* if driving the PCH, we need FDI enabled */
1883                         assert_fdi_rx_pll_enabled(dev_priv,
1884                                                   intel_crtc_pch_transcoder(crtc));
1885                         assert_fdi_tx_pll_enabled(dev_priv,
1886                                                   (enum pipe) cpu_transcoder);
1887                 }
1888                 /* FIXME: assert CPU port conditions for SNB+ */
1889         }
1890
1891         trace_intel_pipe_enable(crtc);
1892
1893         reg = PIPECONF(cpu_transcoder);
1894         val = intel_de_read(dev_priv, reg);
1895         if (val & PIPECONF_ENABLE) {
1896                 /* we keep both pipes enabled on 830 */
1897                 drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
1898                 return;
1899         }
1900
1901         intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
1902         intel_de_posting_read(dev_priv, reg);
1903
1904         /*
1905          * Until the pipe starts PIPEDSL reads will return a stale value,
1906          * which causes an apparent vblank timestamp jump when PIPEDSL
1907          * resets to its proper value. That also messes up the frame count
1908          * when it's derived from the timestamps. So let's wait for the
1909          * pipe to start properly before we call drm_crtc_vblank_on()
1910          */
1911         if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1912                 intel_wait_for_pipe_scanline_moving(crtc);
1913 }
1914
1915 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1916 {
1917         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1918         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1919         enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1920         enum pipe pipe = crtc->pipe;
1921         i915_reg_t reg;
1922         u32 val;
1923
1924         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1925
1926         /*
1927          * Make sure planes won't keep trying to pump pixels to us,
1928          * or we might hang the display.
1929          */
1930         assert_planes_disabled(crtc);
1931
1932         trace_intel_pipe_disable(crtc);
1933
1934         reg = PIPECONF(cpu_transcoder);
1935         val = intel_de_read(dev_priv, reg);
1936         if ((val & PIPECONF_ENABLE) == 0)
1937                 return;
1938
1939         /*
1940          * Double wide has implications for planes
1941          * so best keep it disabled when not needed.
1942          */
1943         if (old_crtc_state->double_wide)
1944                 val &= ~PIPECONF_DOUBLE_WIDE;
1945
1946         /* Don't disable pipe or pipe PLLs if needed */
1947         if (!IS_I830(dev_priv))
1948                 val &= ~PIPECONF_ENABLE;
1949
1950         intel_de_write(dev_priv, reg, val);
1951         if ((val & PIPECONF_ENABLE) == 0)
1952                 intel_wait_for_pipe_off(old_crtc_state);
1953 }
1954
1955 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
1956 {
1957         return IS_GEN(dev_priv, 2) ? 2048 : 4096;
1958 }
1959
1960 static bool is_ccs_plane(const struct drm_framebuffer *fb, int plane)
1961 {
1962         if (!is_ccs_modifier(fb->modifier))
1963                 return false;
1964
1965         return plane >= fb->format->num_planes / 2;
1966 }
1967
1968 static bool is_gen12_ccs_modifier(u64 modifier)
1969 {
1970         return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
1971                modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
1972
1973 }
1974
1975 static bool is_gen12_ccs_plane(const struct drm_framebuffer *fb, int plane)
1976 {
1977         return is_gen12_ccs_modifier(fb->modifier) && is_ccs_plane(fb, plane);
1978 }
1979
1980 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
1981 {
1982         if (is_ccs_modifier(fb->modifier))
1983                 return is_ccs_plane(fb, plane);
1984
1985         return plane == 1;
1986 }
1987
1988 static int main_to_ccs_plane(const struct drm_framebuffer *fb, int main_plane)
1989 {
1990         drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1991                     (main_plane && main_plane >= fb->format->num_planes / 2));
1992
1993         return fb->format->num_planes / 2 + main_plane;
1994 }
1995
1996 static int ccs_to_main_plane(const struct drm_framebuffer *fb, int ccs_plane)
1997 {
1998         drm_WARN_ON(fb->dev, !is_ccs_modifier(fb->modifier) ||
1999                     ccs_plane < fb->format->num_planes / 2);
2000
2001         return ccs_plane - fb->format->num_planes / 2;
2002 }
2003
2004 int intel_main_to_aux_plane(const struct drm_framebuffer *fb, int main_plane)
2005 {
2006         struct drm_i915_private *i915 = to_i915(fb->dev);
2007
2008         if (is_ccs_modifier(fb->modifier))
2009                 return main_to_ccs_plane(fb, main_plane);
2010         else if (INTEL_GEN(i915) < 11 &&
2011                  intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
2012                 return 1;
2013         else
2014                 return 0;
2015 }
2016
2017 bool
2018 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
2019                                     uint64_t modifier)
2020 {
2021         return info->is_yuv &&
2022                info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
2023 }
2024
2025 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
2026                                    int color_plane)
2027 {
2028         return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
2029                color_plane == 1;
2030 }
2031
2032 static unsigned int
2033 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
2034 {
2035         struct drm_i915_private *dev_priv = to_i915(fb->dev);
2036         unsigned int cpp = fb->format->cpp[color_plane];
2037
2038         switch (fb->modifier) {
2039         case DRM_FORMAT_MOD_LINEAR:
2040                 return intel_tile_size(dev_priv);
2041         case I915_FORMAT_MOD_X_TILED:
2042                 if (IS_GEN(dev_priv, 2))
2043                         return 128;
2044                 else
2045                         return 512;
2046         case I915_FORMAT_MOD_Y_TILED_CCS:
2047                 if (is_ccs_plane(fb, color_plane))
2048                         return 128;
2049                 fallthrough;
2050         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2051         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2052                 if (is_ccs_plane(fb, color_plane))
2053                         return 64;
2054                 fallthrough;
2055         case I915_FORMAT_MOD_Y_TILED:
2056                 if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
2057                         return 128;
2058                 else
2059                         return 512;
2060         case I915_FORMAT_MOD_Yf_TILED_CCS:
2061                 if (is_ccs_plane(fb, color_plane))
2062                         return 128;
2063                 fallthrough;
2064         case I915_FORMAT_MOD_Yf_TILED:
2065                 switch (cpp) {
2066                 case 1:
2067                         return 64;
2068                 case 2:
2069                 case 4:
2070                         return 128;
2071                 case 8:
2072                 case 16:
2073                         return 256;
2074                 default:
2075                         MISSING_CASE(cpp);
2076                         return cpp;
2077                 }
2078                 break;
2079         default:
2080                 MISSING_CASE(fb->modifier);
2081                 return cpp;
2082         }
2083 }
2084
2085 static unsigned int
2086 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
2087 {
2088         if (is_gen12_ccs_plane(fb, color_plane))
2089                 return 1;
2090
2091         return intel_tile_size(to_i915(fb->dev)) /
2092                 intel_tile_width_bytes(fb, color_plane);
2093 }
2094
2095 /* Return the tile dimensions in pixel units */
2096 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
2097                             unsigned int *tile_width,
2098                             unsigned int *tile_height)
2099 {
2100         unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
2101         unsigned int cpp = fb->format->cpp[color_plane];
2102
2103         *tile_width = tile_width_bytes / cpp;
2104         *tile_height = intel_tile_height(fb, color_plane);
2105 }
2106
2107 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
2108                                         int color_plane)
2109 {
2110         unsigned int tile_width, tile_height;
2111
2112         intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2113
2114         return fb->pitches[color_plane] * tile_height;
2115 }
2116
2117 unsigned int
2118 intel_fb_align_height(const struct drm_framebuffer *fb,
2119                       int color_plane, unsigned int height)
2120 {
2121         unsigned int tile_height = intel_tile_height(fb, color_plane);
2122
2123         return ALIGN(height, tile_height);
2124 }
2125
2126 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2127 {
2128         unsigned int size = 0;
2129         int i;
2130
2131         for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2132                 size += rot_info->plane[i].width * rot_info->plane[i].height;
2133
2134         return size;
2135 }
2136
2137 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
2138 {
2139         unsigned int size = 0;
2140         int i;
2141
2142         for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
2143                 size += rem_info->plane[i].width * rem_info->plane[i].height;
2144
2145         return size;
2146 }
2147
2148 static void
2149 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2150                         const struct drm_framebuffer *fb,
2151                         unsigned int rotation)
2152 {
2153         view->type = I915_GGTT_VIEW_NORMAL;
2154         if (drm_rotation_90_or_270(rotation)) {
2155                 view->type = I915_GGTT_VIEW_ROTATED;
2156                 view->rotated = to_intel_framebuffer(fb)->rot_info;
2157         }
2158 }
2159
2160 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
2161 {
2162         if (IS_I830(dev_priv))
2163                 return 16 * 1024;
2164         else if (IS_I85X(dev_priv))
2165                 return 256;
2166         else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
2167                 return 32;
2168         else
2169                 return 4 * 1024;
2170 }
2171
2172 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
2173 {
2174         if (INTEL_GEN(dev_priv) >= 9)
2175                 return 256 * 1024;
2176         else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
2177                  IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2178                 return 128 * 1024;
2179         else if (INTEL_GEN(dev_priv) >= 4)
2180                 return 4 * 1024;
2181         else
2182                 return 0;
2183 }
2184
2185 static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
2186                                          int color_plane)
2187 {
2188         struct drm_i915_private *dev_priv = to_i915(fb->dev);
2189
2190         /* AUX_DIST needs only 4K alignment */
2191         if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
2192             is_ccs_plane(fb, color_plane))
2193                 return 4096;
2194
2195         switch (fb->modifier) {
2196         case DRM_FORMAT_MOD_LINEAR:
2197                 return intel_linear_alignment(dev_priv);
2198         case I915_FORMAT_MOD_X_TILED:
2199                 if (INTEL_GEN(dev_priv) >= 9)
2200                         return 256 * 1024;
2201                 return 0;
2202         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2203                 if (is_semiplanar_uv_plane(fb, color_plane))
2204                         return intel_tile_row_size(fb, color_plane);
2205                 fallthrough;
2206         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2207                 return 16 * 1024;
2208         case I915_FORMAT_MOD_Y_TILED_CCS:
2209         case I915_FORMAT_MOD_Yf_TILED_CCS:
2210         case I915_FORMAT_MOD_Y_TILED:
2211                 if (INTEL_GEN(dev_priv) >= 12 &&
2212                     is_semiplanar_uv_plane(fb, color_plane))
2213                         return intel_tile_row_size(fb, color_plane);
2214                 fallthrough;
2215         case I915_FORMAT_MOD_Yf_TILED:
2216                 return 1 * 1024 * 1024;
2217         default:
2218                 MISSING_CASE(fb->modifier);
2219                 return 0;
2220         }
2221 }
2222
2223 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
2224 {
2225         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2226         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2227
2228         return INTEL_GEN(dev_priv) < 4 ||
2229                 (plane->has_fbc &&
2230                  plane_state->view.type == I915_GGTT_VIEW_NORMAL);
2231 }
2232
2233 struct i915_vma *
2234 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
2235                            const struct i915_ggtt_view *view,
2236                            bool uses_fence,
2237                            unsigned long *out_flags)
2238 {
2239         struct drm_device *dev = fb->dev;
2240         struct drm_i915_private *dev_priv = to_i915(dev);
2241         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2242         intel_wakeref_t wakeref;
2243         struct i915_vma *vma;
2244         unsigned int pinctl;
2245         u32 alignment;
2246
2247         if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
2248                 return ERR_PTR(-EINVAL);
2249
2250         alignment = intel_surf_alignment(fb, 0);
2251         if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
2252                 return ERR_PTR(-EINVAL);
2253
2254         /* Note that the w/a also requires 64 PTE of padding following the
2255          * bo. We currently fill all unused PTE with the shadow page and so
2256          * we should always have valid PTE following the scanout preventing
2257          * the VT-d warning.
2258          */
2259         if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
2260                 alignment = 256 * 1024;
2261
2262         /*
2263          * Global gtt pte registers are special registers which actually forward
2264          * writes to a chunk of system memory. Which means that there is no risk
2265          * that the register values disappear as soon as we call
2266          * intel_runtime_pm_put(), so it is correct to wrap only the
2267          * pin/unpin/fence and not more.
2268          */
2269         wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
2270
2271         atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
2272
2273         /*
2274          * Valleyview is definitely limited to scanning out the first
2275          * 512MiB. Lets presume this behaviour was inherited from the
2276          * g4x display engine and that all earlier gen are similarly
2277          * limited. Testing suggests that it is a little more
2278          * complicated than this. For example, Cherryview appears quite
2279          * happy to scanout from anywhere within its global aperture.
2280          */
2281         pinctl = 0;
2282         if (HAS_GMCH(dev_priv))
2283                 pinctl |= PIN_MAPPABLE;
2284
2285         vma = i915_gem_object_pin_to_display_plane(obj,
2286                                                    alignment, view, pinctl);
2287         if (IS_ERR(vma))
2288                 goto err;
2289
2290         if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
2291                 int ret;
2292
2293                 /*
2294                  * Install a fence for tiled scan-out. Pre-i965 always needs a
2295                  * fence, whereas 965+ only requires a fence if using
2296                  * framebuffer compression.  For simplicity, we always, when
2297                  * possible, install a fence as the cost is not that onerous.
2298                  *
2299                  * If we fail to fence the tiled scanout, then either the
2300                  * modeset will reject the change (which is highly unlikely as
2301                  * the affected systems, all but one, do not have unmappable
2302                  * space) or we will not be able to enable full powersaving
2303                  * techniques (also likely not to apply due to various limits
2304                  * FBC and the like impose on the size of the buffer, which
2305                  * presumably we violated anyway with this unmappable buffer).
2306                  * Anyway, it is presumably better to stumble onwards with
2307                  * something and try to run the system in a "less than optimal"
2308                  * mode that matches the user configuration.
2309                  */
2310                 ret = i915_vma_pin_fence(vma);
2311                 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
2312                         i915_gem_object_unpin_from_display_plane(vma);
2313                         vma = ERR_PTR(ret);
2314                         goto err;
2315                 }
2316
2317                 if (ret == 0 && vma->fence)
2318                         *out_flags |= PLANE_HAS_FENCE;
2319         }
2320
2321         i915_vma_get(vma);
2322 err:
2323         atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
2324         intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
2325         return vma;
2326 }
2327
2328 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
2329 {
2330         i915_gem_object_lock(vma->obj, NULL);
2331         if (flags & PLANE_HAS_FENCE)
2332                 i915_vma_unpin_fence(vma);
2333         i915_gem_object_unpin_from_display_plane(vma);
2334         i915_gem_object_unlock(vma->obj);
2335
2336         i915_vma_put(vma);
2337 }
2338
2339 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
2340                           unsigned int rotation)
2341 {
2342         if (drm_rotation_90_or_270(rotation))
2343                 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
2344         else
2345                 return fb->pitches[color_plane];
2346 }
2347
2348 /*
2349  * Convert the x/y offsets into a linear offset.
2350  * Only valid with 0/180 degree rotation, which is fine since linear
2351  * offset is only used with linear buffers on pre-hsw and tiled buffers
2352  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2353  */
2354 u32 intel_fb_xy_to_linear(int x, int y,
2355                           const struct intel_plane_state *state,
2356                           int color_plane)
2357 {
2358         const struct drm_framebuffer *fb = state->hw.fb;
2359         unsigned int cpp = fb->format->cpp[color_plane];
2360         unsigned int pitch = state->color_plane[color_plane].stride;
2361
2362         return y * pitch + x * cpp;
2363 }
2364
2365 /*
2366  * Add the x/y offsets derived from fb->offsets[] to the user
2367  * specified plane src x/y offsets. The resulting x/y offsets
2368  * specify the start of scanout from the beginning of the gtt mapping.
2369  */
2370 void intel_add_fb_offsets(int *x, int *y,
2371                           const struct intel_plane_state *state,
2372                           int color_plane)
2373
2374 {
2375         *x += state->color_plane[color_plane].x;
2376         *y += state->color_plane[color_plane].y;
2377 }
2378
2379 static u32 intel_adjust_tile_offset(int *x, int *y,
2380                                     unsigned int tile_width,
2381                                     unsigned int tile_height,
2382                                     unsigned int tile_size,
2383                                     unsigned int pitch_tiles,
2384                                     u32 old_offset,
2385                                     u32 new_offset)
2386 {
2387         unsigned int pitch_pixels = pitch_tiles * tile_width;
2388         unsigned int tiles;
2389
2390         WARN_ON(old_offset & (tile_size - 1));
2391         WARN_ON(new_offset & (tile_size - 1));
2392         WARN_ON(new_offset > old_offset);
2393
2394         tiles = (old_offset - new_offset) / tile_size;
2395
2396         *y += tiles / pitch_tiles * tile_height;
2397         *x += tiles % pitch_tiles * tile_width;
2398
2399         /* minimize x in case it got needlessly big */
2400         *y += *x / pitch_pixels * tile_height;
2401         *x %= pitch_pixels;
2402
2403         return new_offset;
2404 }
2405
2406 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
2407 {
2408         return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
2409                is_gen12_ccs_plane(fb, color_plane);
2410 }
2411
2412 static u32 intel_adjust_aligned_offset(int *x, int *y,
2413                                        const struct drm_framebuffer *fb,
2414                                        int color_plane,
2415                                        unsigned int rotation,
2416                                        unsigned int pitch,
2417                                        u32 old_offset, u32 new_offset)
2418 {
2419         struct drm_i915_private *dev_priv = to_i915(fb->dev);
2420         unsigned int cpp = fb->format->cpp[color_plane];
2421
2422         drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
2423
2424         if (!is_surface_linear(fb, color_plane)) {
2425                 unsigned int tile_size, tile_width, tile_height;
2426                 unsigned int pitch_tiles;
2427
2428                 tile_size = intel_tile_size(dev_priv);
2429                 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2430
2431                 if (drm_rotation_90_or_270(rotation)) {
2432                         pitch_tiles = pitch / tile_height;
2433                         swap(tile_width, tile_height);
2434                 } else {
2435                         pitch_tiles = pitch / (tile_width * cpp);
2436                 }
2437
2438                 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2439                                          tile_size, pitch_tiles,
2440                                          old_offset, new_offset);
2441         } else {
2442                 old_offset += *y * pitch + *x * cpp;
2443
2444                 *y = (old_offset - new_offset) / pitch;
2445                 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2446         }
2447
2448         return new_offset;
2449 }
2450
2451 /*
2452  * Adjust the tile offset by moving the difference into
2453  * the x/y offsets.
2454  */
2455 static u32 intel_plane_adjust_aligned_offset(int *x, int *y,
2456                                              const struct intel_plane_state *state,
2457                                              int color_plane,
2458                                              u32 old_offset, u32 new_offset)
2459 {
2460         return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
2461                                            state->hw.rotation,
2462                                            state->color_plane[color_plane].stride,
2463                                            old_offset, new_offset);
2464 }
2465
2466 /*
2467  * Computes the aligned offset to the base tile and adjusts
2468  * x, y. bytes per pixel is assumed to be a power-of-two.
2469  *
2470  * In the 90/270 rotated case, x and y are assumed
2471  * to be already rotated to match the rotated GTT view, and
2472  * pitch is the tile_height aligned framebuffer height.
2473  *
2474  * This function is used when computing the derived information
2475  * under intel_framebuffer, so using any of that information
2476  * here is not allowed. Anything under drm_framebuffer can be
2477  * used. This is why the user has to pass in the pitch since it
2478  * is specified in the rotated orientation.
2479  */
2480 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
2481                                         int *x, int *y,
2482                                         const struct drm_framebuffer *fb,
2483                                         int color_plane,
2484                                         unsigned int pitch,
2485                                         unsigned int rotation,
2486                                         u32 alignment)
2487 {
2488         unsigned int cpp = fb->format->cpp[color_plane];
2489         u32 offset, offset_aligned;
2490
2491         if (!is_surface_linear(fb, color_plane)) {
2492                 unsigned int tile_size, tile_width, tile_height;
2493                 unsigned int tile_rows, tiles, pitch_tiles;
2494
2495                 tile_size = intel_tile_size(dev_priv);
2496                 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
2497
2498                 if (drm_rotation_90_or_270(rotation)) {
2499                         pitch_tiles = pitch / tile_height;
2500                         swap(tile_width, tile_height);
2501                 } else {
2502                         pitch_tiles = pitch / (tile_width * cpp);
2503                 }
2504
2505                 tile_rows = *y / tile_height;
2506                 *y %= tile_height;
2507
2508                 tiles = *x / tile_width;
2509                 *x %= tile_width;
2510
2511                 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2512
2513                 offset_aligned = offset;
2514                 if (alignment)
2515                         offset_aligned = rounddown(offset_aligned, alignment);
2516
2517                 intel_adjust_tile_offset(x, y, tile_width, tile_height,
2518                                          tile_size, pitch_tiles,
2519                                          offset, offset_aligned);
2520         } else {
2521                 offset = *y * pitch + *x * cpp;
2522                 offset_aligned = offset;
2523                 if (alignment) {
2524                         offset_aligned = rounddown(offset_aligned, alignment);
2525                         *y = (offset % alignment) / pitch;
2526                         *x = ((offset % alignment) - *y * pitch) / cpp;
2527                 } else {
2528                         *y = *x = 0;
2529                 }
2530         }
2531
2532         return offset_aligned;
2533 }
2534
2535 static u32 intel_plane_compute_aligned_offset(int *x, int *y,
2536                                               const struct intel_plane_state *state,
2537                                               int color_plane)
2538 {
2539         struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
2540         struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
2541         const struct drm_framebuffer *fb = state->hw.fb;
2542         unsigned int rotation = state->hw.rotation;
2543         int pitch = state->color_plane[color_plane].stride;
2544         u32 alignment;
2545
2546         if (intel_plane->id == PLANE_CURSOR)
2547                 alignment = intel_cursor_alignment(dev_priv);
2548         else
2549                 alignment = intel_surf_alignment(fb, color_plane);
2550
2551         return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
2552                                             pitch, rotation, alignment);
2553 }
2554
2555 /* Convert the fb->offset[] into x/y offsets */
2556 static int intel_fb_offset_to_xy(int *x, int *y,
2557                                  const struct drm_framebuffer *fb,
2558                                  int color_plane)
2559 {
2560         struct drm_i915_private *dev_priv = to_i915(fb->dev);
2561         unsigned int height;
2562         u32 alignment;
2563
2564         if (INTEL_GEN(dev_priv) >= 12 &&
2565             is_semiplanar_uv_plane(fb, color_plane))
2566                 alignment = intel_tile_row_size(fb, color_plane);
2567         else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
2568                 alignment = intel_tile_size(dev_priv);
2569         else
2570                 alignment = 0;
2571
2572         if (alignment != 0 && fb->offsets[color_plane] % alignment) {
2573                 drm_dbg_kms(&dev_priv->drm,
2574                             "Misaligned offset 0x%08x for color plane %d\n",
2575                             fb->offsets[color_plane], color_plane);
2576                 return -EINVAL;
2577         }
2578
2579         height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
2580         height = ALIGN(height, intel_tile_height(fb, color_plane));
2581
2582         /* Catch potential overflows early */
2583         if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
2584                             fb->offsets[color_plane])) {
2585                 drm_dbg_kms(&dev_priv->drm,
2586                             "Bad offset 0x%08x or pitch %d for color plane %d\n",
2587                             fb->offsets[color_plane], fb->pitches[color_plane],
2588                             color_plane);
2589                 return -ERANGE;
2590         }
2591
2592         *x = 0;
2593         *y = 0;
2594
2595         intel_adjust_aligned_offset(x, y,
2596                                     fb, color_plane, DRM_MODE_ROTATE_0,
2597                                     fb->pitches[color_plane],
2598                                     fb->offsets[color_plane], 0);
2599
2600         return 0;
2601 }
2602
2603 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
2604 {
2605         switch (fb_modifier) {
2606         case I915_FORMAT_MOD_X_TILED:
2607                 return I915_TILING_X;
2608         case I915_FORMAT_MOD_Y_TILED:
2609         case I915_FORMAT_MOD_Y_TILED_CCS:
2610         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2611         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2612                 return I915_TILING_Y;
2613         default:
2614                 return I915_TILING_NONE;
2615         }
2616 }
2617
2618 /*
2619  * From the Sky Lake PRM:
2620  * "The Color Control Surface (CCS) contains the compression status of
2621  *  the cache-line pairs. The compression state of the cache-line pair
2622  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
2623  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
2624  *  cache-line-pairs. CCS is always Y tiled."
2625  *
2626  * Since cache line pairs refers to horizontally adjacent cache lines,
2627  * each cache line in the CCS corresponds to an area of 32x16 cache
2628  * lines on the main surface. Since each pixel is 4 bytes, this gives
2629  * us a ratio of one byte in the CCS for each 8x16 pixels in the
2630  * main surface.
2631  */
2632 static const struct drm_format_info skl_ccs_formats[] = {
2633         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2634           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2635         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2636           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
2637         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2638           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2639         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2640           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
2641 };
2642
2643 /*
2644  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
2645  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
2646  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
2647  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
2648  * the main surface.
2649  */
2650 static const struct drm_format_info gen12_ccs_formats[] = {
2651         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
2652           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2653           .hsub = 1, .vsub = 1, },
2654         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
2655           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2656           .hsub = 1, .vsub = 1, },
2657         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
2658           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2659           .hsub = 1, .vsub = 1, .has_alpha = true },
2660         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
2661           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2662           .hsub = 1, .vsub = 1, .has_alpha = true },
2663         { .format = DRM_FORMAT_YUYV, .num_planes = 2,
2664           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2665           .hsub = 2, .vsub = 1, .is_yuv = true },
2666         { .format = DRM_FORMAT_YVYU, .num_planes = 2,
2667           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2668           .hsub = 2, .vsub = 1, .is_yuv = true },
2669         { .format = DRM_FORMAT_UYVY, .num_planes = 2,
2670           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2671           .hsub = 2, .vsub = 1, .is_yuv = true },
2672         { .format = DRM_FORMAT_VYUY, .num_planes = 2,
2673           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
2674           .hsub = 2, .vsub = 1, .is_yuv = true },
2675         { .format = DRM_FORMAT_NV12, .num_planes = 4,
2676           .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
2677           .hsub = 2, .vsub = 2, .is_yuv = true },
2678         { .format = DRM_FORMAT_P010, .num_planes = 4,
2679           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2680           .hsub = 2, .vsub = 2, .is_yuv = true },
2681         { .format = DRM_FORMAT_P012, .num_planes = 4,
2682           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2683           .hsub = 2, .vsub = 2, .is_yuv = true },
2684         { .format = DRM_FORMAT_P016, .num_planes = 4,
2685           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
2686           .hsub = 2, .vsub = 2, .is_yuv = true },
2687 };
2688
2689 static const struct drm_format_info *
2690 lookup_format_info(const struct drm_format_info formats[],
2691                    int num_formats, u32 format)
2692 {
2693         int i;
2694
2695         for (i = 0; i < num_formats; i++) {
2696                 if (formats[i].format == format)
2697                         return &formats[i];
2698         }
2699
2700         return NULL;
2701 }
2702
2703 static const struct drm_format_info *
2704 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
2705 {
2706         switch (cmd->modifier[0]) {
2707         case I915_FORMAT_MOD_Y_TILED_CCS:
2708         case I915_FORMAT_MOD_Yf_TILED_CCS:
2709                 return lookup_format_info(skl_ccs_formats,
2710                                           ARRAY_SIZE(skl_ccs_formats),
2711                                           cmd->pixel_format);
2712         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
2713         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
2714                 return lookup_format_info(gen12_ccs_formats,
2715                                           ARRAY_SIZE(gen12_ccs_formats),
2716                                           cmd->pixel_format);
2717         default:
2718                 return NULL;
2719         }
2720 }
2721
2722 bool is_ccs_modifier(u64 modifier)
2723 {
2724         return modifier == I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS ||
2725                modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS ||
2726                modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
2727                modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
2728 }
2729
2730 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
2731 {
2732         return DIV_ROUND_UP(fb->pitches[ccs_to_main_plane(fb, ccs_plane)],
2733                             512) * 64;
2734 }
2735
2736 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
2737                               u32 pixel_format, u64 modifier)
2738 {
2739         struct intel_crtc *crtc;
2740         struct intel_plane *plane;
2741
2742         /*
2743          * We assume the primary plane for pipe A has
2744          * the highest stride limits of them all,
2745          * if in case pipe A is disabled, use the first pipe from pipe_mask.
2746          */
2747         crtc = intel_get_first_crtc(dev_priv);
2748         if (!crtc)
2749                 return 0;
2750
2751         plane = to_intel_plane(crtc->base.primary);
2752
2753         return plane->max_stride(plane, pixel_format, modifier,
2754                                  DRM_MODE_ROTATE_0);
2755 }
2756
2757 static
2758 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
2759                         u32 pixel_format, u64 modifier)
2760 {
2761         /*
2762          * Arbitrary limit for gen4+ chosen to match the
2763          * render engine max stride.
2764          *
2765          * The new CCS hash mode makes remapping impossible
2766          */
2767         if (!is_ccs_modifier(modifier)) {
2768                 if (INTEL_GEN(dev_priv) >= 7)
2769                         return 256*1024;
2770                 else if (INTEL_GEN(dev_priv) >= 4)
2771                         return 128*1024;
2772         }
2773
2774         return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
2775 }
2776
2777 static u32
2778 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
2779 {
2780         struct drm_i915_private *dev_priv = to_i915(fb->dev);
2781         u32 tile_width;
2782
2783         if (is_surface_linear(fb, color_plane)) {
2784                 u32 max_stride = intel_plane_fb_max_stride(dev_priv,
2785                                                            fb->format->format,
2786                                                            fb->modifier);
2787
2788                 /*
2789                  * To make remapping with linear generally feasible
2790                  * we need the stride to be page aligned.
2791                  */
2792                 if (fb->pitches[color_plane] > max_stride &&
2793                     !is_ccs_modifier(fb->modifier))
2794                         return intel_tile_size(dev_priv);
2795                 else
2796                         return 64;
2797         }
2798
2799         tile_width = intel_tile_width_bytes(fb, color_plane);
2800         if (is_ccs_modifier(fb->modifier)) {
2801                 /*
2802                  * Display WA #0531: skl,bxt,kbl,glk
2803                  *
2804                  * Render decompression and plane width > 3840
2805                  * combined with horizontal panning requires the
2806                  * plane stride to be a multiple of 4. We'll just
2807                  * require the entire fb to accommodate that to avoid
2808                  * potential runtime errors at plane configuration time.
2809                  */
2810                 if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
2811                         tile_width *= 4;
2812                 /*
2813                  * The main surface pitch must be padded to a multiple of four
2814                  * tile widths.
2815                  */
2816                 else if (INTEL_GEN(dev_priv) >= 12)
2817                         tile_width *= 4;
2818         }
2819         return tile_width;
2820 }
2821
2822 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
2823 {
2824         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2825         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
2826         const struct drm_framebuffer *fb = plane_state->hw.fb;
2827         int i;
2828
2829         /* We don't want to deal with remapping with cursors */
2830         if (plane->id == PLANE_CURSOR)
2831                 return false;
2832
2833         /*
2834          * The display engine limits already match/exceed the
2835          * render engine limits, so not much point in remapping.
2836          * Would also need to deal with the fence POT alignment
2837          * and gen2 2KiB GTT tile size.
2838          */
2839         if (INTEL_GEN(dev_priv) < 4)
2840                 return false;
2841
2842         /*
2843          * The new CCS hash mode isn't compatible with remapping as
2844          * the virtual address of the pages affects the compressed data.
2845          */
2846         if (is_ccs_modifier(fb->modifier))
2847                 return false;
2848
2849         /* Linear needs a page aligned stride for remapping */
2850         if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
2851                 unsigned int alignment = intel_tile_size(dev_priv) - 1;
2852
2853                 for (i = 0; i < fb->format->num_planes; i++) {
2854                         if (fb->pitches[i] & alignment)
2855                                 return false;
2856                 }
2857         }
2858
2859         return true;
2860 }
2861
2862 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
2863 {
2864         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2865         const struct drm_framebuffer *fb = plane_state->hw.fb;
2866         unsigned int rotation = plane_state->hw.rotation;
2867         u32 stride, max_stride;
2868
2869         /*
2870          * No remapping for invisible planes since we don't have
2871          * an actual source viewport to remap.
2872          */
2873         if (!plane_state->uapi.visible)
2874                 return false;
2875
2876         if (!intel_plane_can_remap(plane_state))
2877                 return false;
2878
2879         /*
2880          * FIXME: aux plane limits on gen9+ are
2881          * unclear in Bspec, for now no checking.
2882          */
2883         stride = intel_fb_pitch(fb, 0, rotation);
2884         max_stride = plane->max_stride(plane, fb->format->format,
2885                                        fb->modifier, rotation);
2886
2887         return stride > max_stride;
2888 }
2889
2890 static void
2891 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
2892                                const struct drm_framebuffer *fb,
2893                                int color_plane)
2894 {
2895         int main_plane;
2896
2897         if (color_plane == 0) {
2898                 *hsub = 1;
2899                 *vsub = 1;
2900
2901                 return;
2902         }
2903
2904         /*
2905          * TODO: Deduct the subsampling from the char block for all CCS
2906          * formats and planes.
2907          */
2908         if (!is_gen12_ccs_plane(fb, color_plane)) {
2909                 *hsub = fb->format->hsub;
2910                 *vsub = fb->format->vsub;
2911
2912                 return;
2913         }
2914
2915         main_plane = ccs_to_main_plane(fb, color_plane);
2916         *hsub = drm_format_info_block_width(fb->format, color_plane) /
2917                 drm_format_info_block_width(fb->format, main_plane);
2918
2919         /*
2920          * The min stride check in the core framebuffer_check() function
2921          * assumes that format->hsub applies to every plane except for the
2922          * first plane. That's incorrect for the CCS AUX plane of the first
2923          * plane, but for the above check to pass we must define the block
2924          * width with that subsampling applied to it. Adjust the width here
2925          * accordingly, so we can calculate the actual subsampling factor.
2926          */
2927         if (main_plane == 0)
2928                 *hsub *= fb->format->hsub;
2929
2930         *vsub = 32;
2931 }
2932 static int
2933 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
2934 {
2935         struct drm_i915_private *i915 = to_i915(fb->dev);
2936         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2937         int main_plane;
2938         int hsub, vsub;
2939         int tile_width, tile_height;
2940         int ccs_x, ccs_y;
2941         int main_x, main_y;
2942
2943         if (!is_ccs_plane(fb, ccs_plane))
2944                 return 0;
2945
2946         intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
2947         intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
2948
2949         tile_width *= hsub;
2950         tile_height *= vsub;
2951
2952         ccs_x = (x * hsub) % tile_width;
2953         ccs_y = (y * vsub) % tile_height;
2954
2955         main_plane = ccs_to_main_plane(fb, ccs_plane);
2956         main_x = intel_fb->normal[main_plane].x % tile_width;
2957         main_y = intel_fb->normal[main_plane].y % tile_height;
2958
2959         /*
2960          * CCS doesn't have its own x/y offset register, so the intra CCS tile
2961          * x/y offsets must match between CCS and the main surface.
2962          */
2963         if (main_x != ccs_x || main_y != ccs_y) {
2964                 drm_dbg_kms(&i915->drm,
2965                               "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
2966                               main_x, main_y,
2967                               ccs_x, ccs_y,
2968                               intel_fb->normal[main_plane].x,
2969                               intel_fb->normal[main_plane].y,
2970                               x, y);
2971                 return -EINVAL;
2972         }
2973
2974         return 0;
2975 }
2976
2977 static void
2978 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
2979 {
2980         int main_plane = is_ccs_plane(fb, color_plane) ?
2981                          ccs_to_main_plane(fb, color_plane) : 0;
2982         int main_hsub, main_vsub;
2983         int hsub, vsub;
2984
2985         intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
2986         intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
2987         *w = fb->width / main_hsub / hsub;
2988         *h = fb->height / main_vsub / vsub;
2989 }
2990
2991 /*
2992  * Setup the rotated view for an FB plane and return the size the GTT mapping
2993  * requires for this view.
2994  */
2995 static u32
2996 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
2997                   u32 gtt_offset_rotated, int x, int y,
2998                   unsigned int width, unsigned int height,
2999                   unsigned int tile_size,
3000                   unsigned int tile_width, unsigned int tile_height,
3001                   struct drm_framebuffer *fb)
3002 {
3003         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3004         struct intel_rotation_info *rot_info = &intel_fb->rot_info;
3005         unsigned int pitch_tiles;
3006         struct drm_rect r;
3007
3008         /* Y or Yf modifiers required for 90/270 rotation */
3009         if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3010             fb->modifier != I915_FORMAT_MOD_Yf_TILED)
3011                 return 0;
3012
3013         if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
3014                 return 0;
3015
3016         rot_info->plane[plane] = *plane_info;
3017
3018         intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
3019
3020         /* rotate the x/y offsets to match the GTT view */
3021         drm_rect_init(&r, x, y, width, height);
3022         drm_rect_rotate(&r,
3023                         plane_info->width * tile_width,
3024                         plane_info->height * tile_height,
3025                         DRM_MODE_ROTATE_270);
3026         x = r.x1;
3027         y = r.y1;
3028
3029         /* rotate the tile dimensions to match the GTT view */
3030         pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
3031         swap(tile_width, tile_height);
3032
3033         /*
3034          * We only keep the x/y offsets, so push all of the
3035          * gtt offset into the x/y offsets.
3036          */
3037         intel_adjust_tile_offset(&x, &y,
3038                                  tile_width, tile_height,
3039                                  tile_size, pitch_tiles,
3040                                  gtt_offset_rotated * tile_size, 0);
3041
3042         /*
3043          * First pixel of the framebuffer from
3044          * the start of the rotated gtt mapping.
3045          */
3046         intel_fb->rotated[plane].x = x;
3047         intel_fb->rotated[plane].y = y;
3048
3049         return plane_info->width * plane_info->height;
3050 }
3051
3052 static int
3053 intel_fill_fb_info(struct drm_i915_private *dev_priv,
3054                    struct drm_framebuffer *fb)
3055 {
3056         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3057         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3058         u32 gtt_offset_rotated = 0;
3059         unsigned int max_size = 0;
3060         int i, num_planes = fb->format->num_planes;
3061         unsigned int tile_size = intel_tile_size(dev_priv);
3062
3063         for (i = 0; i < num_planes; i++) {
3064                 unsigned int width, height;
3065                 unsigned int cpp, size;
3066                 u32 offset;
3067                 int x, y;
3068                 int ret;
3069
3070                 cpp = fb->format->cpp[i];
3071                 intel_fb_plane_dims(&width, &height, fb, i);
3072
3073                 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
3074                 if (ret) {
3075                         drm_dbg_kms(&dev_priv->drm,
3076                                     "bad fb plane %d offset: 0x%x\n",
3077                                     i, fb->offsets[i]);
3078                         return ret;
3079                 }
3080
3081                 ret = intel_fb_check_ccs_xy(fb, i, x, y);
3082                 if (ret)
3083                         return ret;
3084
3085                 /*
3086                  * The fence (if used) is aligned to the start of the object
3087                  * so having the framebuffer wrap around across the edge of the
3088                  * fenced region doesn't really work. We have no API to configure
3089                  * the fence start offset within the object (nor could we probably
3090                  * on gen2/3). So it's just easier if we just require that the
3091                  * fb layout agrees with the fence layout. We already check that the
3092                  * fb stride matches the fence stride elsewhere.
3093                  */
3094                 if (i == 0 && i915_gem_object_is_tiled(obj) &&
3095                     (x + width) * cpp > fb->pitches[i]) {
3096                         drm_dbg_kms(&dev_priv->drm,
3097                                     "bad fb plane %d offset: 0x%x\n",
3098                                      i, fb->offsets[i]);
3099                         return -EINVAL;
3100                 }
3101
3102                 /*
3103                  * First pixel of the framebuffer from
3104                  * the start of the normal gtt mapping.
3105                  */
3106                 intel_fb->normal[i].x = x;
3107                 intel_fb->normal[i].y = y;
3108
3109                 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
3110                                                       fb->pitches[i],
3111                                                       DRM_MODE_ROTATE_0,
3112                                                       tile_size);
3113                 offset /= tile_size;
3114
3115                 if (!is_surface_linear(fb, i)) {
3116                         struct intel_remapped_plane_info plane_info;
3117                         unsigned int tile_width, tile_height;
3118
3119                         intel_tile_dims(fb, i, &tile_width, &tile_height);
3120
3121                         plane_info.offset = offset;
3122                         plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
3123                                                          tile_width * cpp);
3124                         plane_info.width = DIV_ROUND_UP(x + width, tile_width);
3125                         plane_info.height = DIV_ROUND_UP(y + height,
3126                                                          tile_height);
3127
3128                         /* how many tiles does this plane need */
3129                         size = plane_info.stride * plane_info.height;
3130                         /*
3131                          * If the plane isn't horizontally tile aligned,
3132                          * we need one more tile.
3133                          */
3134                         if (x != 0)
3135                                 size++;
3136
3137                         gtt_offset_rotated +=
3138                                 setup_fb_rotation(i, &plane_info,
3139                                                   gtt_offset_rotated,
3140                                                   x, y, width, height,
3141                                                   tile_size,
3142                                                   tile_width, tile_height,
3143                                                   fb);
3144                 } else {
3145                         size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
3146                                             x * cpp, tile_size);
3147                 }
3148
3149                 /* how many tiles in total needed in the bo */
3150                 max_size = max(max_size, offset + size);
3151         }
3152
3153         if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
3154                 drm_dbg_kms(&dev_priv->drm,
3155                             "fb too big for bo (need %llu bytes, have %zu bytes)\n",
3156                             mul_u32_u32(max_size, tile_size), obj->base.size);
3157                 return -EINVAL;
3158         }
3159
3160         return 0;
3161 }
3162
3163 static void
3164 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
3165 {
3166         struct drm_i915_private *dev_priv =
3167                 to_i915(plane_state->uapi.plane->dev);
3168         struct drm_framebuffer *fb = plane_state->hw.fb;
3169         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
3170         struct intel_rotation_info *info = &plane_state->view.rotated;
3171         unsigned int rotation = plane_state->hw.rotation;
3172         int i, num_planes = fb->format->num_planes;
3173         unsigned int tile_size = intel_tile_size(dev_priv);
3174         unsigned int src_x, src_y;
3175         unsigned int src_w, src_h;
3176         u32 gtt_offset = 0;
3177
3178         memset(&plane_state->view, 0, sizeof(plane_state->view));
3179         plane_state->view.type = drm_rotation_90_or_270(rotation) ?
3180                 I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
3181
3182         src_x = plane_state->uapi.src.x1 >> 16;
3183         src_y = plane_state->uapi.src.y1 >> 16;
3184         src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
3185         src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
3186
3187         drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
3188
3189         /* Make src coordinates relative to the viewport */
3190         drm_rect_translate(&plane_state->uapi.src,
3191                            -(src_x << 16), -(src_y << 16));
3192
3193         /* Rotate src coordinates to match rotated GTT view */
3194         if (drm_rotation_90_or_270(rotation))
3195                 drm_rect_rotate(&plane_state->uapi.src,
3196                                 src_w << 16, src_h << 16,
3197                                 DRM_MODE_ROTATE_270);
3198
3199         for (i = 0; i < num_planes; i++) {
3200                 unsigned int hsub = i ? fb->format->hsub : 1;
3201                 unsigned int vsub = i ? fb->format->vsub : 1;
3202                 unsigned int cpp = fb->format->cpp[i];
3203                 unsigned int tile_width, tile_height;
3204                 unsigned int width, height;
3205                 unsigned int pitch_tiles;
3206                 unsigned int x, y;
3207                 u32 offset;
3208
3209                 intel_tile_dims(fb, i, &tile_width, &tile_height);
3210
3211                 x = src_x / hsub;
3212                 y = src_y / vsub;
3213                 width = src_w / hsub;
3214                 height = src_h / vsub;
3215
3216                 /*
3217                  * First pixel of the src viewport from the
3218                  * start of the normal gtt mapping.
3219                  */
3220                 x += intel_fb->normal[i].x;
3221                 y += intel_fb->normal[i].y;
3222
3223                 offset = intel_compute_aligned_offset(dev_priv, &x, &y,
3224                                                       fb, i, fb->pitches[i],
3225                                                       DRM_MODE_ROTATE_0, tile_size);
3226                 offset /= tile_size;
3227
3228                 drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
3229                 info->plane[i].offset = offset;
3230                 info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
3231                                                      tile_width * cpp);
3232                 info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
3233                 info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
3234
3235                 if (drm_rotation_90_or_270(rotation)) {
3236                         struct drm_rect r;
3237
3238                         /* rotate the x/y offsets to match the GTT view */
3239                         drm_rect_init(&r, x, y, width, height);
3240                         drm_rect_rotate(&r,
3241                                         info->plane[i].width * tile_width,
3242                                         info->plane[i].height * tile_height,
3243                                         DRM_MODE_ROTATE_270);
3244                         x = r.x1;
3245                         y = r.y1;
3246
3247                         pitch_tiles = info->plane[i].height;
3248                         plane_state->color_plane[i].stride = pitch_tiles * tile_height;
3249
3250                         /* rotate the tile dimensions to match the GTT view */
3251                         swap(tile_width, tile_height);
3252                 } else {
3253                         pitch_tiles = info->plane[i].width;
3254                         plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
3255                 }
3256
3257                 /*
3258                  * We only keep the x/y offsets, so push all of the
3259                  * gtt offset into the x/y offsets.
3260                  */
3261                 intel_adjust_tile_offset(&x, &y,
3262                                          tile_width, tile_height,
3263                                          tile_size, pitch_tiles,
3264                                          gtt_offset * tile_size, 0);
3265
3266                 gtt_offset += info->plane[i].width * info->plane[i].height;
3267
3268                 plane_state->color_plane[i].offset = 0;
3269                 plane_state->color_plane[i].x = x;
3270                 plane_state->color_plane[i].y = y;
3271         }
3272 }
3273
3274 static int
3275 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
3276 {
3277         const struct intel_framebuffer *fb =
3278                 to_intel_framebuffer(plane_state->hw.fb);
3279         unsigned int rotation = plane_state->hw.rotation;
3280         int i, num_planes;
3281
3282         if (!fb)
3283                 return 0;
3284
3285         num_planes = fb->base.format->num_planes;
3286
3287         if (intel_plane_needs_remap(plane_state)) {
3288                 intel_plane_remap_gtt(plane_state);
3289
3290                 /*
3291                  * Sometimes even remapping can't overcome
3292                  * the stride limitations :( Can happen with
3293                  * big plane sizes and suitably misaligned
3294                  * offsets.
3295                  */
3296                 return intel_plane_check_stride(plane_state);
3297         }
3298
3299         intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
3300
3301         for (i = 0; i < num_planes; i++) {
3302                 plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
3303                 plane_state->color_plane[i].offset = 0;
3304
3305                 if (drm_rotation_90_or_270(rotation)) {
3306                         plane_state->color_plane[i].x = fb->rotated[i].x;
3307                         plane_state->color_plane[i].y = fb->rotated[i].y;
3308                 } else {
3309                         plane_state->color_plane[i].x = fb->normal[i].x;
3310                         plane_state->color_plane[i].y = fb->normal[i].y;
3311                 }
3312         }
3313
3314         /* Rotate src coordinates to match rotated GTT view */
3315         if (drm_rotation_90_or_270(rotation))
3316                 drm_rect_rotate(&plane_state->uapi.src,
3317                                 fb->base.width << 16, fb->base.height << 16,
3318                                 DRM_MODE_ROTATE_270);
3319
3320         return intel_plane_check_stride(plane_state);
3321 }
3322
3323 static int i9xx_format_to_fourcc(int format)
3324 {
3325         switch (format) {
3326         case DISPPLANE_8BPP:
3327                 return DRM_FORMAT_C8;
3328         case DISPPLANE_BGRA555:
3329                 return DRM_FORMAT_ARGB1555;
3330         case DISPPLANE_BGRX555:
3331                 return DRM_FORMAT_XRGB1555;
3332         case DISPPLANE_BGRX565:
3333                 return DRM_FORMAT_RGB565;
3334         default:
3335         case DISPPLANE_BGRX888:
3336                 return DRM_FORMAT_XRGB8888;
3337         case DISPPLANE_RGBX888:
3338                 return DRM_FORMAT_XBGR8888;
3339         case DISPPLANE_BGRA888:
3340                 return DRM_FORMAT_ARGB8888;
3341         case DISPPLANE_RGBA888:
3342                 return DRM_FORMAT_ABGR8888;
3343         case DISPPLANE_BGRX101010:
3344                 return DRM_FORMAT_XRGB2101010;
3345         case DISPPLANE_RGBX101010:
3346                 return DRM_FORMAT_XBGR2101010;
3347         case DISPPLANE_BGRA101010:
3348                 return DRM_FORMAT_ARGB2101010;
3349         case DISPPLANE_RGBA101010:
3350                 return DRM_FORMAT_ABGR2101010;
3351         case DISPPLANE_RGBX161616:
3352                 return DRM_FORMAT_XBGR16161616F;
3353         }
3354 }
3355
3356 int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
3357 {
3358         switch (format) {
3359         case PLANE_CTL_FORMAT_RGB_565:
3360                 return DRM_FORMAT_RGB565;
3361         case PLANE_CTL_FORMAT_NV12:
3362                 return DRM_FORMAT_NV12;
3363         case PLANE_CTL_FORMAT_XYUV:
3364                 return DRM_FORMAT_XYUV8888;
3365         case PLANE_CTL_FORMAT_P010:
3366                 return DRM_FORMAT_P010;
3367         case PLANE_CTL_FORMAT_P012:
3368                 return DRM_FORMAT_P012;
3369         case PLANE_CTL_FORMAT_P016:
3370                 return DRM_FORMAT_P016;
3371         case PLANE_CTL_FORMAT_Y210:
3372                 return DRM_FORMAT_Y210;
3373         case PLANE_CTL_FORMAT_Y212:
3374                 return DRM_FORMAT_Y212;
3375         case PLANE_CTL_FORMAT_Y216:
3376                 return DRM_FORMAT_Y216;
3377         case PLANE_CTL_FORMAT_Y410:
3378                 return DRM_FORMAT_XVYU2101010;
3379         case PLANE_CTL_FORMAT_Y412:
3380                 return DRM_FORMAT_XVYU12_16161616;
3381         case PLANE_CTL_FORMAT_Y416:
3382                 return DRM_FORMAT_XVYU16161616;
3383         default:
3384         case PLANE_CTL_FORMAT_XRGB_8888:
3385                 if (rgb_order) {
3386                         if (alpha)
3387                                 return DRM_FORMAT_ABGR8888;
3388                         else
3389                                 return DRM_FORMAT_XBGR8888;
3390                 } else {
3391                         if (alpha)
3392                                 return DRM_FORMAT_ARGB8888;
3393                         else
3394                                 return DRM_FORMAT_XRGB8888;
3395                 }
3396         case PLANE_CTL_FORMAT_XRGB_2101010:
3397                 if (rgb_order) {
3398                         if (alpha)
3399                                 return DRM_FORMAT_ABGR2101010;
3400                         else
3401                                 return DRM_FORMAT_XBGR2101010;
3402                 } else {
3403                         if (alpha)
3404                                 return DRM_FORMAT_ARGB2101010;
3405                         else
3406                                 return DRM_FORMAT_XRGB2101010;
3407                 }
3408         case PLANE_CTL_FORMAT_XRGB_16161616F:
3409                 if (rgb_order) {
3410                         if (alpha)
3411                                 return DRM_FORMAT_ABGR16161616F;
3412                         else
3413                                 return DRM_FORMAT_XBGR16161616F;
3414                 } else {
3415                         if (alpha)
3416                                 return DRM_FORMAT_ARGB16161616F;
3417                         else
3418                                 return DRM_FORMAT_XRGB16161616F;
3419                 }
3420         }
3421 }
3422
3423 static struct i915_vma *
3424 initial_plane_vma(struct drm_i915_private *i915,
3425                   struct intel_initial_plane_config *plane_config)
3426 {
3427         struct drm_i915_gem_object *obj;
3428         struct i915_vma *vma;
3429         u32 base, size;
3430
3431         if (plane_config->size == 0)
3432                 return NULL;
3433
3434         base = round_down(plane_config->base,
3435                           I915_GTT_MIN_ALIGNMENT);
3436         size = round_up(plane_config->base + plane_config->size,
3437                         I915_GTT_MIN_ALIGNMENT);
3438         size -= base;
3439
3440         /*
3441          * If the FB is too big, just don't use it since fbdev is not very
3442          * important and we should probably use that space with FBC or other
3443          * features.
3444          */
3445         if (size * 2 > i915->stolen_usable_size)
3446                 return NULL;
3447
3448         obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
3449         if (IS_ERR(obj))
3450                 return NULL;
3451
3452         /*
3453          * Mark it WT ahead of time to avoid changing the
3454          * cache_level during fbdev initialization. The
3455          * unbind there would get stuck waiting for rcu.
3456          */
3457         i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
3458                                             I915_CACHE_WT : I915_CACHE_NONE);
3459
3460         switch (plane_config->tiling) {
3461         case I915_TILING_NONE:
3462                 break;
3463         case I915_TILING_X:
3464         case I915_TILING_Y:
3465                 obj->tiling_and_stride =
3466                         plane_config->fb->base.pitches[0] |
3467                         plane_config->tiling;
3468                 break;
3469         default:
3470                 MISSING_CASE(plane_config->tiling);
3471                 goto err_obj;
3472         }
3473
3474         vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
3475         if (IS_ERR(vma))
3476                 goto err_obj;
3477
3478         if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
3479                 goto err_obj;
3480
3481         if (i915_gem_object_is_tiled(obj) &&
3482             !i915_vma_is_map_and_fenceable(vma))
3483                 goto err_obj;
3484
3485         return vma;
3486
3487 err_obj:
3488         i915_gem_object_put(obj);
3489         return NULL;
3490 }
3491
3492 static bool
3493 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
3494                               struct intel_initial_plane_config *plane_config)
3495 {
3496         struct drm_device *dev = crtc->base.dev;
3497         struct drm_i915_private *dev_priv = to_i915(dev);
3498         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
3499         struct drm_framebuffer *fb = &plane_config->fb->base;
3500         struct i915_vma *vma;
3501
3502         switch (fb->modifier) {
3503         case DRM_FORMAT_MOD_LINEAR:
3504         case I915_FORMAT_MOD_X_TILED:
3505         case I915_FORMAT_MOD_Y_TILED:
3506                 break;
3507         default:
3508                 drm_dbg(&dev_priv->drm,
3509                         "Unsupported modifier for initial FB: 0x%llx\n",
3510                         fb->modifier);
3511                 return false;
3512         }
3513
3514         vma = initial_plane_vma(dev_priv, plane_config);
3515         if (!vma)
3516                 return false;
3517
3518         mode_cmd.pixel_format = fb->format->format;
3519         mode_cmd.width = fb->width;
3520         mode_cmd.height = fb->height;
3521         mode_cmd.pitches[0] = fb->pitches[0];
3522         mode_cmd.modifier[0] = fb->modifier;
3523         mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
3524
3525         if (intel_framebuffer_init(to_intel_framebuffer(fb),
3526                                    vma->obj, &mode_cmd)) {
3527                 drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
3528                 goto err_vma;
3529         }
3530
3531         plane_config->vma = vma;
3532         return true;
3533
3534 err_vma:
3535         i915_vma_put(vma);
3536         return false;
3537 }
3538
3539 static void
3540 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
3541                         struct intel_plane_state *plane_state,
3542                         bool visible)
3543 {
3544         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3545
3546         plane_state->uapi.visible = visible;
3547
3548         if (visible)
3549                 crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
3550         else
3551                 crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
3552 }
3553
3554 static void fixup_active_planes(struct intel_crtc_state *crtc_state)
3555 {
3556         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3557         struct drm_plane *plane;
3558
3559         /*
3560          * Active_planes aliases if multiple "primary" or cursor planes
3561          * have been used on the same (or wrong) pipe. plane_mask uses
3562          * unique ids, hence we can use that to reconstruct active_planes.
3563          */
3564         crtc_state->active_planes = 0;
3565
3566         drm_for_each_plane_mask(plane, &dev_priv->drm,
3567                                 crtc_state->uapi.plane_mask)
3568                 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
3569 }
3570
3571 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
3572                                          struct intel_plane *plane)
3573 {
3574         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3575         struct intel_crtc_state *crtc_state =
3576                 to_intel_crtc_state(crtc->base.state);
3577         struct intel_plane_state *plane_state =
3578                 to_intel_plane_state(plane->base.state);
3579
3580         drm_dbg_kms(&dev_priv->drm,
3581                     "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
3582                     plane->base.base.id, plane->base.name,
3583                     crtc->base.base.id, crtc->base.name);
3584
3585         intel_set_plane_visible(crtc_state, plane_state, false);
3586         fixup_active_planes(crtc_state);
3587         crtc_state->data_rate[plane->id] = 0;
3588         crtc_state->min_cdclk[plane->id] = 0;
3589
3590         if (plane->id == PLANE_PRIMARY)
3591                 hsw_disable_ips(crtc_state);
3592
3593         /*
3594          * Vblank time updates from the shadow to live plane control register
3595          * are blocked if the memory self-refresh mode is active at that
3596          * moment. So to make sure the plane gets truly disabled, disable
3597          * first the self-refresh mode. The self-refresh enable bit in turn
3598          * will be checked/applied by the HW only at the next frame start
3599          * event which is after the vblank start event, so we need to have a
3600          * wait-for-vblank between disabling the plane and the pipe.
3601          */
3602         if (HAS_GMCH(dev_priv) &&
3603             intel_set_memory_cxsr(dev_priv, false))
3604                 intel_wait_for_vblank(dev_priv, crtc->pipe);
3605
3606         /*
3607          * Gen2 reports pipe underruns whenever all planes are disabled.
3608          * So disable underrun reporting before all the planes get disabled.
3609          */
3610         if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
3611                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
3612
3613         intel_disable_plane(plane, crtc_state);
3614 }
3615
3616 static struct intel_frontbuffer *
3617 to_intel_frontbuffer(struct drm_framebuffer *fb)
3618 {
3619         return fb ? to_intel_framebuffer(fb)->frontbuffer : NULL;
3620 }
3621
3622 static void
3623 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
3624                              struct intel_initial_plane_config *plane_config)
3625 {
3626         struct drm_device *dev = intel_crtc->base.dev;
3627         struct drm_i915_private *dev_priv = to_i915(dev);
3628         struct drm_crtc *c;
3629         struct drm_plane *primary = intel_crtc->base.primary;
3630         struct drm_plane_state *plane_state = primary->state;
3631         struct intel_plane *intel_plane = to_intel_plane(primary);
3632         struct intel_plane_state *intel_state =
3633                 to_intel_plane_state(plane_state);
3634         struct drm_framebuffer *fb;
3635         struct i915_vma *vma;
3636
3637         if (!plane_config->fb)
3638                 return;
3639
3640         if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
3641                 fb = &plane_config->fb->base;
3642                 vma = plane_config->vma;
3643                 goto valid_fb;
3644         }
3645
3646         /*
3647          * Failed to alloc the obj, check to see if we should share
3648          * an fb with another CRTC instead
3649          */
3650         for_each_crtc(dev, c) {
3651                 struct intel_plane_state *state;
3652
3653                 if (c == &intel_crtc->base)
3654                         continue;
3655
3656                 if (!to_intel_crtc(c)->active)
3657                         continue;
3658
3659                 state = to_intel_plane_state(c->primary->state);
3660                 if (!state->vma)
3661                         continue;
3662
3663                 if (intel_plane_ggtt_offset(state) == plane_config->base) {
3664                         fb = state->hw.fb;
3665                         vma = state->vma;
3666                         goto valid_fb;
3667                 }
3668         }
3669
3670         /*
3671          * We've failed to reconstruct the BIOS FB.  Current display state
3672          * indicates that the primary plane is visible, but has a NULL FB,
3673          * which will lead to problems later if we don't fix it up.  The
3674          * simplest solution is to just disable the primary plane now and
3675          * pretend the BIOS never had it enabled.
3676          */
3677         intel_plane_disable_noatomic(intel_crtc, intel_plane);
3678
3679         return;
3680
3681 valid_fb:
3682         intel_state->hw.rotation = plane_config->rotation;
3683         intel_fill_fb_ggtt_view(&intel_state->view, fb,
3684                                 intel_state->hw.rotation);
3685         intel_state->color_plane[0].stride =
3686                 intel_fb_pitch(fb, 0, intel_state->hw.rotation);
3687
3688         __i915_vma_pin(vma);
3689         intel_state->vma = i915_vma_get(vma);
3690         if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
3691                 if (vma->fence)
3692                         intel_state->flags |= PLANE_HAS_FENCE;
3693
3694         plane_state->src_x = 0;
3695         plane_state->src_y = 0;
3696         plane_state->src_w = fb->width << 16;
3697         plane_state->src_h = fb->height << 16;
3698
3699         plane_state->crtc_x = 0;
3700         plane_state->crtc_y = 0;
3701         plane_state->crtc_w = fb->width;
3702         plane_state->crtc_h = fb->height;
3703
3704         intel_state->uapi.src = drm_plane_state_src(plane_state);
3705         intel_state->uapi.dst = drm_plane_state_dest(plane_state);
3706
3707         if (plane_config->tiling)
3708                 dev_priv->preserve_bios_swizzle = true;
3709
3710         plane_state->fb = fb;
3711         drm_framebuffer_get(fb);
3712
3713         plane_state->crtc = &intel_crtc->base;
3714         intel_plane_copy_uapi_to_hw_state(intel_state, intel_state);
3715
3716         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
3717
3718         atomic_or(to_intel_plane(primary)->frontbuffer_bit,
3719                   &to_intel_frontbuffer(fb)->bits);
3720 }
3721
3722
3723 static bool
3724 skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
3725                                int main_x, int main_y, u32 main_offset,
3726                                int ccs_plane)
3727 {
3728         const struct drm_framebuffer *fb = plane_state->hw.fb;
3729         int aux_x = plane_state->color_plane[ccs_plane].x;
3730         int aux_y = plane_state->color_plane[ccs_plane].y;
3731         u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3732         u32 alignment = intel_surf_alignment(fb, ccs_plane);
3733         int hsub;
3734         int vsub;
3735
3736         intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3737         while (aux_offset >= main_offset && aux_y <= main_y) {
3738                 int x, y;
3739
3740                 if (aux_x == main_x && aux_y == main_y)
3741                         break;
3742
3743                 if (aux_offset == 0)
3744                         break;
3745
3746                 x = aux_x / hsub;
3747                 y = aux_y / vsub;
3748                 aux_offset = intel_plane_adjust_aligned_offset(&x, &y,
3749                                                                plane_state,
3750                                                                ccs_plane,
3751                                                                aux_offset,
3752                                                                aux_offset -
3753                                                                 alignment);
3754                 aux_x = x * hsub + aux_x % hsub;
3755                 aux_y = y * vsub + aux_y % vsub;
3756         }
3757
3758         if (aux_x != main_x || aux_y != main_y)
3759                 return false;
3760
3761         plane_state->color_plane[ccs_plane].offset = aux_offset;
3762         plane_state->color_plane[ccs_plane].x = aux_x;
3763         plane_state->color_plane[ccs_plane].y = aux_y;
3764
3765         return true;
3766 }
3767
3768 unsigned int
3769 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
3770 {
3771         int x = 0, y = 0;
3772
3773         intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3774                                           plane_state->color_plane[0].offset, 0);
3775
3776         return y;
3777 }
3778
3779 static int intel_plane_min_width(struct intel_plane *plane,
3780                                  const struct drm_framebuffer *fb,
3781                                  int color_plane,
3782                                  unsigned int rotation)
3783 {
3784         if (plane->min_width)
3785                 return plane->min_width(fb, color_plane, rotation);
3786         else
3787                 return 1;
3788 }
3789
3790 static int intel_plane_max_width(struct intel_plane *plane,
3791                                  const struct drm_framebuffer *fb,
3792                                  int color_plane,
3793                                  unsigned int rotation)
3794 {
3795         if (plane->max_width)
3796                 return plane->max_width(fb, color_plane, rotation);
3797         else
3798                 return INT_MAX;
3799 }
3800
3801 static int intel_plane_max_height(struct intel_plane *plane,
3802                                   const struct drm_framebuffer *fb,
3803                                   int color_plane,
3804                                   unsigned int rotation)
3805 {
3806         if (plane->max_height)
3807                 return plane->max_height(fb, color_plane, rotation);
3808         else
3809                 return INT_MAX;
3810 }
3811
3812 static int skl_check_main_surface(struct intel_plane_state *plane_state)
3813 {
3814         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3815         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
3816         const struct drm_framebuffer *fb = plane_state->hw.fb;
3817         unsigned int rotation = plane_state->hw.rotation;
3818         int x = plane_state->uapi.src.x1 >> 16;
3819         int y = plane_state->uapi.src.y1 >> 16;
3820         int w = drm_rect_width(&plane_state->uapi.src) >> 16;
3821         int h = drm_rect_height(&plane_state->uapi.src) >> 16;
3822         int min_width = intel_plane_min_width(plane, fb, 0, rotation);
3823         int max_width = intel_plane_max_width(plane, fb, 0, rotation);
3824         int max_height = intel_plane_max_height(plane, fb, 0, rotation);
3825         int aux_plane = intel_main_to_aux_plane(fb, 0);
3826         u32 aux_offset = plane_state->color_plane[aux_plane].offset;
3827         u32 alignment, offset;
3828
3829         if (w > max_width || w < min_width || h > max_height) {
3830                 drm_dbg_kms(&dev_priv->drm,
3831                             "requested Y/RGB source size %dx%d outside limits (min: %dx1 max: %dx%d)\n",
3832                             w, h, min_width, max_width, max_height);
3833                 return -EINVAL;
3834         }
3835
3836         intel_add_fb_offsets(&x, &y, plane_state, 0);
3837         offset = intel_plane_compute_aligned_offset(&x, &y, plane_state, 0);
3838         alignment = intel_surf_alignment(fb, 0);
3839         if (drm_WARN_ON(&dev_priv->drm, alignment && !is_power_of_2(alignment)))
3840                 return -EINVAL;
3841
3842         /*
3843          * AUX surface offset is specified as the distance from the
3844          * main surface offset, and it must be non-negative. Make
3845          * sure that is what we will get.
3846          */
3847         if (aux_plane && offset > aux_offset)
3848                 offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3849                                                            offset, aux_offset & ~(alignment - 1));
3850
3851         /*
3852          * When using an X-tiled surface, the plane blows up
3853          * if the x offset + width exceed the stride.
3854          *
3855          * TODO: linear and Y-tiled seem fine, Yf untested,
3856          */
3857         if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
3858                 int cpp = fb->format->cpp[0];
3859
3860                 while ((x + w) * cpp > plane_state->color_plane[0].stride) {
3861                         if (offset == 0) {
3862                                 drm_dbg_kms(&dev_priv->drm,
3863                                             "Unable to find suitable display surface offset due to X-tiling\n");
3864                                 return -EINVAL;
3865                         }
3866
3867                         offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3868                                                                    offset, offset - alignment);
3869                 }
3870         }
3871
3872         /*
3873          * CCS AUX surface doesn't have its own x/y offsets, we must make sure
3874          * they match with the main surface x/y offsets.
3875          */
3876         if (is_ccs_modifier(fb->modifier)) {
3877                 while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3878                                                        offset, aux_plane)) {
3879                         if (offset == 0)
3880                                 break;
3881
3882                         offset = intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
3883                                                                    offset, offset - alignment);
3884                 }
3885
3886                 if (x != plane_state->color_plane[aux_plane].x ||
3887                     y != plane_state->color_plane[aux_plane].y) {
3888                         drm_dbg_kms(&dev_priv->drm,
3889                                     "Unable to find suitable display surface offset due to CCS\n");
3890                         return -EINVAL;
3891                 }
3892         }
3893
3894         plane_state->color_plane[0].offset = offset;
3895         plane_state->color_plane[0].x = x;
3896         plane_state->color_plane[0].y = y;
3897
3898         /*
3899          * Put the final coordinates back so that the src
3900          * coordinate checks will see the right values.
3901          */
3902         drm_rect_translate_to(&plane_state->uapi.src,
3903                               x << 16, y << 16);
3904
3905         return 0;
3906 }
3907
3908 static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
3909 {
3910         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
3911         struct drm_i915_private *i915 = to_i915(plane->base.dev);
3912         const struct drm_framebuffer *fb = plane_state->hw.fb;
3913         unsigned int rotation = plane_state->hw.rotation;
3914         int uv_plane = 1;
3915         int max_width = intel_plane_max_width(plane, fb, uv_plane, rotation);
3916         int max_height = intel_plane_max_height(plane, fb, uv_plane, rotation);
3917         int x = plane_state->uapi.src.x1 >> 17;
3918         int y = plane_state->uapi.src.y1 >> 17;
3919         int w = drm_rect_width(&plane_state->uapi.src) >> 17;
3920         int h = drm_rect_height(&plane_state->uapi.src) >> 17;
3921         u32 offset;
3922
3923         /* FIXME not quite sure how/if these apply to the chroma plane */
3924         if (w > max_width || h > max_height) {
3925                 drm_dbg_kms(&i915->drm,
3926                             "CbCr source size %dx%d too big (limit %dx%d)\n",
3927                             w, h, max_width, max_height);
3928                 return -EINVAL;
3929         }
3930
3931         intel_add_fb_offsets(&x, &y, plane_state, uv_plane);
3932         offset = intel_plane_compute_aligned_offset(&x, &y,
3933                                                     plane_state, uv_plane);
3934
3935         if (is_ccs_modifier(fb->modifier)) {
3936                 int ccs_plane = main_to_ccs_plane(fb, uv_plane);
3937                 u32 aux_offset = plane_state->color_plane[ccs_plane].offset;
3938                 u32 alignment = intel_surf_alignment(fb, uv_plane);
3939
3940                 if (offset > aux_offset)
3941                         offset = intel_plane_adjust_aligned_offset(&x, &y,
3942                                                                    plane_state,
3943                                                                    uv_plane,
3944                                                                    offset,
3945                                                                    aux_offset & ~(alignment - 1));
3946
3947                 while (!skl_check_main_ccs_coordinates(plane_state, x, y,
3948                                                        offset, ccs_plane)) {
3949                         if (offset == 0)
3950                                 break;
3951
3952                         offset = intel_plane_adjust_aligned_offset(&x, &y,
3953                                                                    plane_state,
3954                                                                    uv_plane,
3955                                                                    offset, offset - alignment);
3956                 }
3957
3958                 if (x != plane_state->color_plane[ccs_plane].x ||
3959                     y != plane_state->color_plane[ccs_plane].y) {
3960                         drm_dbg_kms(&i915->drm,
3961                                     "Unable to find suitable display surface offset due to CCS\n");
3962                         return -EINVAL;
3963                 }
3964         }
3965
3966         plane_state->color_plane[uv_plane].offset = offset;
3967         plane_state->color_plane[uv_plane].x = x;
3968         plane_state->color_plane[uv_plane].y = y;
3969
3970         return 0;
3971 }
3972
3973 static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
3974 {
3975         const struct drm_framebuffer *fb = plane_state->hw.fb;
3976         int src_x = plane_state->uapi.src.x1 >> 16;
3977         int src_y = plane_state->uapi.src.y1 >> 16;
3978         u32 offset;
3979         int ccs_plane;
3980
3981         for (ccs_plane = 0; ccs_plane < fb->format->num_planes; ccs_plane++) {
3982                 int main_hsub, main_vsub;
3983                 int hsub, vsub;
3984                 int x, y;
3985
3986                 if (!is_ccs_plane(fb, ccs_plane))
3987                         continue;
3988
3989                 intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb,
3990                                                ccs_to_main_plane(fb, ccs_plane));
3991                 intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
3992
3993                 hsub *= main_hsub;
3994                 vsub *= main_vsub;
3995                 x = src_x / hsub;
3996                 y = src_y / vsub;
3997
3998                 intel_add_fb_offsets(&x, &y, plane_state, ccs_plane);
3999
4000                 offset = intel_plane_compute_aligned_offset(&x, &y,
4001                                                             plane_state,
4002                                                             ccs_plane);
4003
4004                 plane_state->color_plane[ccs_plane].offset = offset;
4005                 plane_state->color_plane[ccs_plane].x = (x * hsub +
4006                                                          src_x % hsub) /
4007                                                         main_hsub;
4008                 plane_state->color_plane[ccs_plane].y = (y * vsub +
4009                                                          src_y % vsub) /
4010                                                         main_vsub;
4011         }
4012
4013         return 0;
4014 }
4015
4016 int skl_check_plane_surface(struct intel_plane_state *plane_state)
4017 {
4018         const struct drm_framebuffer *fb = plane_state->hw.fb;
4019         int ret, i;
4020
4021         ret = intel_plane_compute_gtt(plane_state);
4022         if (ret)
4023                 return ret;
4024
4025         if (!plane_state->uapi.visible)
4026                 return 0;
4027
4028         /*
4029          * Handle the AUX surface first since the main surface setup depends on
4030          * it.
4031          */
4032         if (is_ccs_modifier(fb->modifier)) {
4033                 ret = skl_check_ccs_aux_surface(plane_state);
4034                 if (ret)
4035                         return ret;
4036         }
4037
4038         if (intel_format_info_is_yuv_semiplanar(fb->format,
4039                                                 fb->modifier)) {
4040                 ret = skl_check_nv12_aux_surface(plane_state);
4041                 if (ret)
4042                         return ret;
4043         }
4044
4045         for (i = fb->format->num_planes; i < ARRAY_SIZE(plane_state->color_plane); i++) {
4046                 plane_state->color_plane[i].offset = 0;
4047                 plane_state->color_plane[i].x = 0;
4048                 plane_state->color_plane[i].y = 0;
4049         }
4050
4051         ret = skl_check_main_surface(plane_state);
4052         if (ret)
4053                 return ret;
4054
4055         return 0;
4056 }
4057
4058 static void i9xx_plane_ratio(const struct intel_crtc_state *crtc_state,
4059                              const struct intel_plane_state *plane_state,
4060                              unsigned int *num, unsigned int *den)
4061 {
4062         const struct drm_framebuffer *fb = plane_state->hw.fb;
4063         unsigned int cpp = fb->format->cpp[0];
4064
4065         /*
4066          * g4x bspec says 64bpp pixel rate can't exceed 80%
4067          * of cdclk when the sprite plane is enabled on the
4068          * same pipe. ilk/snb bspec says 64bpp pixel rate is
4069          * never allowed to exceed 80% of cdclk. Let's just go
4070          * with the ilk/snb limit always.
4071          */
4072         if (cpp == 8) {
4073                 *num = 10;
4074                 *den = 8;
4075         } else {
4076                 *num = 1;
4077                 *den = 1;
4078         }
4079 }
4080
4081 static int i9xx_plane_min_cdclk(const struct intel_crtc_state *crtc_state,
4082                                 const struct intel_plane_state *plane_state)
4083 {
4084         unsigned int pixel_rate;
4085         unsigned int num, den;
4086
4087         /*
4088          * Note that crtc_state->pixel_rate accounts for both
4089          * horizontal and vertical panel fitter downscaling factors.
4090          * Pre-HSW bspec tells us to only consider the horizontal
4091          * downscaling factor here. We ignore that and just consider
4092          * both for simplicity.
4093          */
4094         pixel_rate = crtc_state->pixel_rate;
4095
4096         i9xx_plane_ratio(crtc_state, plane_state, &num, &den);
4097
4098         /* two pixels per clock with double wide pipe */
4099         if (crtc_state->double_wide)
4100                 den *= 2;
4101
4102         return DIV_ROUND_UP(pixel_rate * num, den);
4103 }
4104
4105 unsigned int
4106 i9xx_plane_max_stride(struct intel_plane *plane,
4107                       u32 pixel_format, u64 modifier,
4108                       unsigned int rotation)
4109 {
4110         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4111
4112         if (!HAS_GMCH(dev_priv)) {
4113                 return 32*1024;
4114         } else if (INTEL_GEN(dev_priv) >= 4) {
4115                 if (modifier == I915_FORMAT_MOD_X_TILED)
4116                         return 16*1024;
4117                 else
4118                         return 32*1024;
4119         } else if (INTEL_GEN(dev_priv) >= 3) {
4120                 if (modifier == I915_FORMAT_MOD_X_TILED)
4121                         return 8*1024;
4122                 else
4123                         return 16*1024;
4124         } else {
4125                 if (plane->i9xx_plane == PLANE_C)
4126                         return 4*1024;
4127                 else
4128                         return 8*1024;
4129         }
4130 }
4131
4132 static u32 i9xx_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4133 {
4134         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4135         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4136         u32 dspcntr = 0;
4137
4138         if (crtc_state->gamma_enable)
4139                 dspcntr |= DISPPLANE_GAMMA_ENABLE;
4140
4141         if (crtc_state->csc_enable)
4142                 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
4143
4144         if (INTEL_GEN(dev_priv) < 5)
4145                 dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
4146
4147         return dspcntr;
4148 }
4149
4150 static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
4151                           const struct intel_plane_state *plane_state)
4152 {
4153         struct drm_i915_private *dev_priv =
4154                 to_i915(plane_state->uapi.plane->dev);
4155         const struct drm_framebuffer *fb = plane_state->hw.fb;
4156         unsigned int rotation = plane_state->hw.rotation;
4157         u32 dspcntr;
4158
4159         dspcntr = DISPLAY_PLANE_ENABLE;
4160
4161         if (IS_G4X(dev_priv) || IS_GEN(dev_priv, 5) ||
4162             IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
4163                 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
4164
4165         switch (fb->format->format) {
4166         case DRM_FORMAT_C8:
4167                 dspcntr |= DISPPLANE_8BPP;
4168                 break;
4169         case DRM_FORMAT_XRGB1555:
4170                 dspcntr |= DISPPLANE_BGRX555;
4171                 break;
4172         case DRM_FORMAT_ARGB1555:
4173                 dspcntr |= DISPPLANE_BGRA555;
4174                 break;
4175         case DRM_FORMAT_RGB565:
4176                 dspcntr |= DISPPLANE_BGRX565;
4177                 break;
4178         case DRM_FORMAT_XRGB8888:
4179                 dspcntr |= DISPPLANE_BGRX888;
4180                 break;
4181         case DRM_FORMAT_XBGR8888:
4182                 dspcntr |= DISPPLANE_RGBX888;
4183                 break;
4184         case DRM_FORMAT_ARGB8888:
4185                 dspcntr |= DISPPLANE_BGRA888;
4186                 break;
4187         case DRM_FORMAT_ABGR8888:
4188                 dspcntr |= DISPPLANE_RGBA888;
4189                 break;
4190         case DRM_FORMAT_XRGB2101010:
4191                 dspcntr |= DISPPLANE_BGRX101010;
4192                 break;
4193         case DRM_FORMAT_XBGR2101010:
4194                 dspcntr |= DISPPLANE_RGBX101010;
4195                 break;
4196         case DRM_FORMAT_ARGB2101010:
4197                 dspcntr |= DISPPLANE_BGRA101010;
4198                 break;
4199         case DRM_FORMAT_ABGR2101010:
4200                 dspcntr |= DISPPLANE_RGBA101010;
4201                 break;
4202         case DRM_FORMAT_XBGR16161616F:
4203                 dspcntr |= DISPPLANE_RGBX161616;
4204                 break;
4205         default:
4206                 MISSING_CASE(fb->format->format);
4207                 return 0;
4208         }
4209
4210         if (INTEL_GEN(dev_priv) >= 4 &&
4211             fb->modifier == I915_FORMAT_MOD_X_TILED)
4212                 dspcntr |= DISPPLANE_TILED;
4213
4214         if (rotation & DRM_MODE_ROTATE_180)
4215                 dspcntr |= DISPPLANE_ROTATE_180;
4216
4217         if (rotation & DRM_MODE_REFLECT_X)
4218                 dspcntr |= DISPPLANE_MIRROR;
4219
4220         return dspcntr;
4221 }
4222
4223 int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
4224 {
4225         struct drm_i915_private *dev_priv =
4226                 to_i915(plane_state->uapi.plane->dev);
4227         const struct drm_framebuffer *fb = plane_state->hw.fb;
4228         int src_x, src_y, src_w;
4229         u32 offset;
4230         int ret;
4231
4232         ret = intel_plane_compute_gtt(plane_state);
4233         if (ret)
4234                 return ret;
4235
4236         if (!plane_state->uapi.visible)
4237                 return 0;
4238
4239         src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4240         src_x = plane_state->uapi.src.x1 >> 16;
4241         src_y = plane_state->uapi.src.y1 >> 16;
4242
4243         /* Undocumented hardware limit on i965/g4x/vlv/chv */
4244         if (HAS_GMCH(dev_priv) && fb->format->cpp[0] == 8 && src_w > 2048)
4245                 return -EINVAL;
4246
4247         intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
4248
4249         if (INTEL_GEN(dev_priv) >= 4)
4250                 offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
4251                                                             plane_state, 0);
4252         else
4253                 offset = 0;
4254
4255         /*
4256          * Put the final coordinates back so that the src
4257          * coordinate checks will see the right values.
4258          */
4259         drm_rect_translate_to(&plane_state->uapi.src,
4260                               src_x << 16, src_y << 16);
4261
4262         /* HSW/BDW do this automagically in hardware */
4263         if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
4264                 unsigned int rotation = plane_state->hw.rotation;
4265                 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4266                 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4267
4268                 if (rotation & DRM_MODE_ROTATE_180) {
4269                         src_x += src_w - 1;
4270                         src_y += src_h - 1;
4271                 } else if (rotation & DRM_MODE_REFLECT_X) {
4272                         src_x += src_w - 1;
4273                 }
4274         }
4275
4276         plane_state->color_plane[0].offset = offset;
4277         plane_state->color_plane[0].x = src_x;
4278         plane_state->color_plane[0].y = src_y;
4279
4280         return 0;
4281 }
4282
4283 static bool i9xx_plane_has_windowing(struct intel_plane *plane)
4284 {
4285         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4286         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4287
4288         if (IS_CHERRYVIEW(dev_priv))
4289                 return i9xx_plane == PLANE_B;
4290         else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4291                 return false;
4292         else if (IS_GEN(dev_priv, 4))
4293                 return i9xx_plane == PLANE_C;
4294         else
4295                 return i9xx_plane == PLANE_B ||
4296                         i9xx_plane == PLANE_C;
4297 }
4298
4299 static int
4300 i9xx_plane_check(struct intel_crtc_state *crtc_state,
4301                  struct intel_plane_state *plane_state)
4302 {
4303         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4304         int ret;
4305
4306         ret = chv_plane_check_rotation(plane_state);
4307         if (ret)
4308                 return ret;
4309
4310         ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
4311                                                   &crtc_state->uapi,
4312                                                   DRM_PLANE_HELPER_NO_SCALING,
4313                                                   DRM_PLANE_HELPER_NO_SCALING,
4314                                                   i9xx_plane_has_windowing(plane),
4315                                                   true);
4316         if (ret)
4317                 return ret;
4318
4319         ret = i9xx_check_plane_surface(plane_state);
4320         if (ret)
4321                 return ret;
4322
4323         if (!plane_state->uapi.visible)
4324                 return 0;
4325
4326         ret = intel_plane_check_src_coordinates(plane_state);
4327         if (ret)
4328                 return ret;
4329
4330         plane_state->ctl = i9xx_plane_ctl(crtc_state, plane_state);
4331
4332         return 0;
4333 }
4334
4335 static void i9xx_update_plane(struct intel_plane *plane,
4336                               const struct intel_crtc_state *crtc_state,
4337                               const struct intel_plane_state *plane_state)
4338 {
4339         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4340         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4341         u32 linear_offset;
4342         int x = plane_state->color_plane[0].x;
4343         int y = plane_state->color_plane[0].y;
4344         int crtc_x = plane_state->uapi.dst.x1;
4345         int crtc_y = plane_state->uapi.dst.y1;
4346         int crtc_w = drm_rect_width(&plane_state->uapi.dst);
4347         int crtc_h = drm_rect_height(&plane_state->uapi.dst);
4348         unsigned long irqflags;
4349         u32 dspaddr_offset;
4350         u32 dspcntr;
4351
4352         dspcntr = plane_state->ctl | i9xx_plane_ctl_crtc(crtc_state);
4353
4354         linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
4355
4356         if (INTEL_GEN(dev_priv) >= 4)
4357                 dspaddr_offset = plane_state->color_plane[0].offset;
4358         else
4359                 dspaddr_offset = linear_offset;
4360
4361         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4362
4363         intel_de_write_fw(dev_priv, DSPSTRIDE(i9xx_plane),
4364                           plane_state->color_plane[0].stride);
4365
4366         if (INTEL_GEN(dev_priv) < 4) {
4367                 /*
4368                  * PLANE_A doesn't actually have a full window
4369                  * generator but let's assume we still need to
4370                  * program whatever is there.
4371                  */
4372                 intel_de_write_fw(dev_priv, DSPPOS(i9xx_plane),
4373                                   (crtc_y << 16) | crtc_x);
4374                 intel_de_write_fw(dev_priv, DSPSIZE(i9xx_plane),
4375                                   ((crtc_h - 1) << 16) | (crtc_w - 1));
4376         } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
4377                 intel_de_write_fw(dev_priv, PRIMPOS(i9xx_plane),
4378                                   (crtc_y << 16) | crtc_x);
4379                 intel_de_write_fw(dev_priv, PRIMSIZE(i9xx_plane),
4380                                   ((crtc_h - 1) << 16) | (crtc_w - 1));
4381                 intel_de_write_fw(dev_priv, PRIMCNSTALPHA(i9xx_plane), 0);
4382         }
4383
4384         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
4385                 intel_de_write_fw(dev_priv, DSPOFFSET(i9xx_plane),
4386                                   (y << 16) | x);
4387         } else if (INTEL_GEN(dev_priv) >= 4) {
4388                 intel_de_write_fw(dev_priv, DSPLINOFF(i9xx_plane),
4389                                   linear_offset);
4390                 intel_de_write_fw(dev_priv, DSPTILEOFF(i9xx_plane),
4391                                   (y << 16) | x);
4392         }
4393
4394         /*
4395          * The control register self-arms if the plane was previously
4396          * disabled. Try to make the plane enable atomic by writing
4397          * the control register just before the surface register.
4398          */
4399         intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4400         if (INTEL_GEN(dev_priv) >= 4)
4401                 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane),
4402                                   intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4403         else
4404                 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane),
4405                                   intel_plane_ggtt_offset(plane_state) + dspaddr_offset);
4406
4407         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4408 }
4409
4410 static void i9xx_disable_plane(struct intel_plane *plane,
4411                                const struct intel_crtc_state *crtc_state)
4412 {
4413         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4414         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4415         unsigned long irqflags;
4416         u32 dspcntr;
4417
4418         /*
4419          * DSPCNTR pipe gamma enable on g4x+ and pipe csc
4420          * enable on ilk+ affect the pipe bottom color as
4421          * well, so we must configure them even if the plane
4422          * is disabled.
4423          *
4424          * On pre-g4x there is no way to gamma correct the
4425          * pipe bottom color but we'll keep on doing this
4426          * anyway so that the crtc state readout works correctly.
4427          */
4428         dspcntr = i9xx_plane_ctl_crtc(crtc_state);
4429
4430         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4431
4432         intel_de_write_fw(dev_priv, DSPCNTR(i9xx_plane), dspcntr);
4433         if (INTEL_GEN(dev_priv) >= 4)
4434                 intel_de_write_fw(dev_priv, DSPSURF(i9xx_plane), 0);
4435         else
4436                 intel_de_write_fw(dev_priv, DSPADDR(i9xx_plane), 0);
4437
4438         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4439 }
4440
4441 static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
4442                                     enum pipe *pipe)
4443 {
4444         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
4445         enum intel_display_power_domain power_domain;
4446         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4447         intel_wakeref_t wakeref;
4448         bool ret;
4449         u32 val;
4450
4451         /*
4452          * Not 100% correct for planes that can move between pipes,
4453          * but that's only the case for gen2-4 which don't have any
4454          * display power wells.
4455          */
4456         power_domain = POWER_DOMAIN_PIPE(plane->pipe);
4457         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4458         if (!wakeref)
4459                 return false;
4460
4461         val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4462
4463         ret = val & DISPLAY_PLANE_ENABLE;
4464
4465         if (INTEL_GEN(dev_priv) >= 5)
4466                 *pipe = plane->pipe;
4467         else
4468                 *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
4469                         DISPPLANE_SEL_PIPE_SHIFT;
4470
4471         intel_display_power_put(dev_priv, power_domain, wakeref);
4472
4473         return ret;
4474 }
4475
4476 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
4477 {
4478         struct drm_device *dev = intel_crtc->base.dev;
4479         struct drm_i915_private *dev_priv = to_i915(dev);
4480         unsigned long irqflags;
4481
4482         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
4483
4484         intel_de_write_fw(dev_priv, SKL_PS_CTRL(intel_crtc->pipe, id), 0);
4485         intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
4486         intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
4487
4488         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
4489 }
4490
4491 /*
4492  * This function detaches (aka. unbinds) unused scalers in hardware
4493  */
4494 static void skl_detach_scalers(const struct intel_crtc_state *crtc_state)
4495 {
4496         struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
4497         const struct intel_crtc_scaler_state *scaler_state =
4498                 &crtc_state->scaler_state;
4499         int i;
4500
4501         /* loop through and disable scalers that aren't in use */
4502         for (i = 0; i < intel_crtc->num_scalers; i++) {
4503                 if (!scaler_state->scalers[i].in_use)
4504                         skl_detach_scaler(intel_crtc, i);
4505         }
4506 }
4507
4508 static unsigned int skl_plane_stride_mult(const struct drm_framebuffer *fb,
4509                                           int color_plane, unsigned int rotation)
4510 {
4511         /*
4512          * The stride is either expressed as a multiple of 64 bytes chunks for
4513          * linear buffers or in number of tiles for tiled buffers.
4514          */
4515         if (is_surface_linear(fb, color_plane))
4516                 return 64;
4517         else if (drm_rotation_90_or_270(rotation))
4518                 return intel_tile_height(fb, color_plane);
4519         else
4520                 return intel_tile_width_bytes(fb, color_plane);
4521 }
4522
4523 u32 skl_plane_stride(const struct intel_plane_state *plane_state,
4524                      int color_plane)
4525 {
4526         const struct drm_framebuffer *fb = plane_state->hw.fb;
4527         unsigned int rotation = plane_state->hw.rotation;
4528         u32 stride = plane_state->color_plane[color_plane].stride;
4529
4530         if (color_plane >= fb->format->num_planes)
4531                 return 0;
4532
4533         return stride / skl_plane_stride_mult(fb, color_plane, rotation);
4534 }
4535
4536 static u32 skl_plane_ctl_format(u32 pixel_format)
4537 {
4538         switch (pixel_format) {
4539         case DRM_FORMAT_C8:
4540                 return PLANE_CTL_FORMAT_INDEXED;
4541         case DRM_FORMAT_RGB565:
4542                 return PLANE_CTL_FORMAT_RGB_565;
4543         case DRM_FORMAT_XBGR8888:
4544         case DRM_FORMAT_ABGR8888:
4545                 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
4546         case DRM_FORMAT_XRGB8888:
4547         case DRM_FORMAT_ARGB8888:
4548                 return PLANE_CTL_FORMAT_XRGB_8888;
4549         case DRM_FORMAT_XBGR2101010:
4550         case DRM_FORMAT_ABGR2101010:
4551                 return PLANE_CTL_FORMAT_XRGB_2101010 | PLANE_CTL_ORDER_RGBX;
4552         case DRM_FORMAT_XRGB2101010:
4553         case DRM_FORMAT_ARGB2101010:
4554                 return PLANE_CTL_FORMAT_XRGB_2101010;
4555         case DRM_FORMAT_XBGR16161616F:
4556         case DRM_FORMAT_ABGR16161616F:
4557                 return PLANE_CTL_FORMAT_XRGB_16161616F | PLANE_CTL_ORDER_RGBX;
4558         case DRM_FORMAT_XRGB16161616F:
4559         case DRM_FORMAT_ARGB16161616F:
4560                 return PLANE_CTL_FORMAT_XRGB_16161616F;
4561         case DRM_FORMAT_XYUV8888:
4562                 return PLANE_CTL_FORMAT_XYUV;
4563         case DRM_FORMAT_YUYV:
4564                 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
4565         case DRM_FORMAT_YVYU:
4566                 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
4567         case DRM_FORMAT_UYVY:
4568                 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
4569         case DRM_FORMAT_VYUY:
4570                 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
4571         case DRM_FORMAT_NV12:
4572                 return PLANE_CTL_FORMAT_NV12;
4573         case DRM_FORMAT_P010:
4574                 return PLANE_CTL_FORMAT_P010;
4575         case DRM_FORMAT_P012:
4576                 return PLANE_CTL_FORMAT_P012;
4577         case DRM_FORMAT_P016:
4578                 return PLANE_CTL_FORMAT_P016;
4579         case DRM_FORMAT_Y210:
4580                 return PLANE_CTL_FORMAT_Y210;
4581         case DRM_FORMAT_Y212:
4582                 return PLANE_CTL_FORMAT_Y212;
4583         case DRM_FORMAT_Y216:
4584                 return PLANE_CTL_FORMAT_Y216;
4585         case DRM_FORMAT_XVYU2101010:
4586                 return PLANE_CTL_FORMAT_Y410;
4587         case DRM_FORMAT_XVYU12_16161616:
4588                 return PLANE_CTL_FORMAT_Y412;
4589         case DRM_FORMAT_XVYU16161616:
4590                 return PLANE_CTL_FORMAT_Y416;
4591         default:
4592                 MISSING_CASE(pixel_format);
4593         }
4594
4595         return 0;
4596 }
4597
4598 static u32 skl_plane_ctl_alpha(const struct intel_plane_state *plane_state)
4599 {
4600         if (!plane_state->hw.fb->format->has_alpha)
4601                 return PLANE_CTL_ALPHA_DISABLE;
4602
4603         switch (plane_state->hw.pixel_blend_mode) {
4604         case DRM_MODE_BLEND_PIXEL_NONE:
4605                 return PLANE_CTL_ALPHA_DISABLE;
4606         case DRM_MODE_BLEND_PREMULTI:
4607                 return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
4608         case DRM_MODE_BLEND_COVERAGE:
4609                 return PLANE_CTL_ALPHA_HW_PREMULTIPLY;
4610         default:
4611                 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4612                 return PLANE_CTL_ALPHA_DISABLE;
4613         }
4614 }
4615
4616 static u32 glk_plane_color_ctl_alpha(const struct intel_plane_state *plane_state)
4617 {
4618         if (!plane_state->hw.fb->format->has_alpha)
4619                 return PLANE_COLOR_ALPHA_DISABLE;
4620
4621         switch (plane_state->hw.pixel_blend_mode) {
4622         case DRM_MODE_BLEND_PIXEL_NONE:
4623                 return PLANE_COLOR_ALPHA_DISABLE;
4624         case DRM_MODE_BLEND_PREMULTI:
4625                 return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
4626         case DRM_MODE_BLEND_COVERAGE:
4627                 return PLANE_COLOR_ALPHA_HW_PREMULTIPLY;
4628         default:
4629                 MISSING_CASE(plane_state->hw.pixel_blend_mode);
4630                 return PLANE_COLOR_ALPHA_DISABLE;
4631         }
4632 }
4633
4634 static u32 skl_plane_ctl_tiling(u64 fb_modifier)
4635 {
4636         switch (fb_modifier) {
4637         case DRM_FORMAT_MOD_LINEAR:
4638                 break;
4639         case I915_FORMAT_MOD_X_TILED:
4640                 return PLANE_CTL_TILED_X;
4641         case I915_FORMAT_MOD_Y_TILED:
4642                 return PLANE_CTL_TILED_Y;
4643         case I915_FORMAT_MOD_Y_TILED_CCS:
4644                 return PLANE_CTL_TILED_Y | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4645         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
4646                 return PLANE_CTL_TILED_Y |
4647                        PLANE_CTL_RENDER_DECOMPRESSION_ENABLE |
4648                        PLANE_CTL_CLEAR_COLOR_DISABLE;
4649         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
4650                 return PLANE_CTL_TILED_Y | PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE;
4651         case I915_FORMAT_MOD_Yf_TILED:
4652                 return PLANE_CTL_TILED_YF;
4653         case I915_FORMAT_MOD_Yf_TILED_CCS:
4654                 return PLANE_CTL_TILED_YF | PLANE_CTL_RENDER_DECOMPRESSION_ENABLE;
4655         default:
4656                 MISSING_CASE(fb_modifier);
4657         }
4658
4659         return 0;
4660 }
4661
4662 static u32 skl_plane_ctl_rotate(unsigned int rotate)
4663 {
4664         switch (rotate) {
4665         case DRM_MODE_ROTATE_0:
4666                 break;
4667         /*
4668          * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
4669          * while i915 HW rotation is clockwise, thats why this swapping.
4670          */
4671         case DRM_MODE_ROTATE_90:
4672                 return PLANE_CTL_ROTATE_270;
4673         case DRM_MODE_ROTATE_180:
4674                 return PLANE_CTL_ROTATE_180;
4675         case DRM_MODE_ROTATE_270:
4676                 return PLANE_CTL_ROTATE_90;
4677         default:
4678                 MISSING_CASE(rotate);
4679         }
4680
4681         return 0;
4682 }
4683
4684 static u32 cnl_plane_ctl_flip(unsigned int reflect)
4685 {
4686         switch (reflect) {
4687         case 0:
4688                 break;
4689         case DRM_MODE_REFLECT_X:
4690                 return PLANE_CTL_FLIP_HORIZONTAL;
4691         case DRM_MODE_REFLECT_Y:
4692         default:
4693                 MISSING_CASE(reflect);
4694         }
4695
4696         return 0;
4697 }
4698
4699 u32 skl_plane_ctl_crtc(const struct intel_crtc_state *crtc_state)
4700 {
4701         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4702         u32 plane_ctl = 0;
4703
4704         if (crtc_state->uapi.async_flip)
4705                 plane_ctl |= PLANE_CTL_ASYNC_FLIP;
4706
4707         if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
4708                 return plane_ctl;
4709
4710         if (crtc_state->gamma_enable)
4711                 plane_ctl |= PLANE_CTL_PIPE_GAMMA_ENABLE;
4712
4713         if (crtc_state->csc_enable)
4714                 plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
4715
4716         return plane_ctl;
4717 }
4718
4719 u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
4720                   const struct intel_plane_state *plane_state)
4721 {
4722         struct drm_i915_private *dev_priv =
4723                 to_i915(plane_state->uapi.plane->dev);
4724         const struct drm_framebuffer *fb = plane_state->hw.fb;
4725         unsigned int rotation = plane_state->hw.rotation;
4726         const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
4727         u32 plane_ctl;
4728
4729         plane_ctl = PLANE_CTL_ENABLE;
4730
4731         if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
4732                 plane_ctl |= skl_plane_ctl_alpha(plane_state);
4733                 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
4734
4735                 if (plane_state->hw.color_encoding == DRM_COLOR_YCBCR_BT709)
4736                         plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
4737
4738                 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4739                         plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
4740         }
4741
4742         plane_ctl |= skl_plane_ctl_format(fb->format->format);
4743         plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
4744         plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
4745
4746         if (INTEL_GEN(dev_priv) >= 10)
4747                 plane_ctl |= cnl_plane_ctl_flip(rotation &
4748                                                 DRM_MODE_REFLECT_MASK);
4749
4750         if (key->flags & I915_SET_COLORKEY_DESTINATION)
4751                 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
4752         else if (key->flags & I915_SET_COLORKEY_SOURCE)
4753                 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
4754
4755         return plane_ctl;
4756 }
4757
4758 u32 glk_plane_color_ctl_crtc(const struct intel_crtc_state *crtc_state)
4759 {
4760         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4761         u32 plane_color_ctl = 0;
4762
4763         if (INTEL_GEN(dev_priv) >= 11)
4764                 return plane_color_ctl;
4765
4766         if (crtc_state->gamma_enable)
4767                 plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
4768
4769         if (crtc_state->csc_enable)
4770                 plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
4771
4772         return plane_color_ctl;
4773 }
4774
4775 u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
4776                         const struct intel_plane_state *plane_state)
4777 {
4778         struct drm_i915_private *dev_priv =
4779                 to_i915(plane_state->uapi.plane->dev);
4780         const struct drm_framebuffer *fb = plane_state->hw.fb;
4781         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4782         u32 plane_color_ctl = 0;
4783
4784         plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
4785         plane_color_ctl |= glk_plane_color_ctl_alpha(plane_state);
4786
4787         if (fb->format->is_yuv && !icl_is_hdr_plane(dev_priv, plane->id)) {
4788                 switch (plane_state->hw.color_encoding) {
4789                 case DRM_COLOR_YCBCR_BT709:
4790                         plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
4791                         break;
4792                 case DRM_COLOR_YCBCR_BT2020:
4793                         plane_color_ctl |=
4794                                 PLANE_COLOR_CSC_MODE_YUV2020_TO_RGB2020;
4795                         break;
4796                 default:
4797                         plane_color_ctl |=
4798                                 PLANE_COLOR_CSC_MODE_YUV601_TO_RGB601;
4799                 }
4800                 if (plane_state->hw.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
4801                         plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
4802         } else if (fb->format->is_yuv) {
4803                 plane_color_ctl |= PLANE_COLOR_INPUT_CSC_ENABLE;
4804         }
4805
4806         return plane_color_ctl;
4807 }
4808
4809 static int
4810 __intel_display_resume(struct drm_device *dev,
4811                        struct drm_atomic_state *state,
4812                        struct drm_modeset_acquire_ctx *ctx)
4813 {
4814         struct drm_crtc_state *crtc_state;
4815         struct drm_crtc *crtc;
4816         int i, ret;
4817
4818         intel_modeset_setup_hw_state(dev, ctx);
4819         intel_vga_redisable(to_i915(dev));
4820
4821         if (!state)
4822                 return 0;
4823
4824         /*
4825          * We've duplicated the state, pointers to the old state are invalid.
4826          *
4827          * Don't attempt to use the old state until we commit the duplicated state.
4828          */
4829         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4830                 /*
4831                  * Force recalculation even if we restore
4832                  * current state. With fast modeset this may not result
4833                  * in a modeset when the state is compatible.
4834                  */
4835                 crtc_state->mode_changed = true;
4836         }
4837
4838         /* ignore any reset values/BIOS leftovers in the WM registers */
4839         if (!HAS_GMCH(to_i915(dev)))
4840                 to_intel_atomic_state(state)->skip_intermediate_wm = true;
4841
4842         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
4843
4844         drm_WARN_ON(dev, ret == -EDEADLK);
4845         return ret;
4846 }
4847
4848 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
4849 {
4850         return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
4851                 intel_has_gpu_reset(&dev_priv->gt));
4852 }
4853
4854 void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
4855 {
4856         struct drm_device *dev = &dev_priv->drm;
4857         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4858         struct drm_atomic_state *state;
4859         int ret;
4860
4861         if (!HAS_DISPLAY(dev_priv))
4862                 return;
4863
4864         /* reset doesn't touch the display */
4865         if (!dev_priv->params.force_reset_modeset_test &&
4866             !gpu_reset_clobbers_display(dev_priv))
4867                 return;
4868
4869         /* We have a modeset vs reset deadlock, defensively unbreak it. */
4870         set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4871         smp_mb__after_atomic();
4872         wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
4873
4874         if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
4875                 drm_dbg_kms(&dev_priv->drm,
4876                             "Modeset potentially stuck, unbreaking through wedging\n");
4877                 intel_gt_set_wedged(&dev_priv->gt);
4878         }
4879
4880         /*
4881          * Need mode_config.mutex so that we don't
4882          * trample ongoing ->detect() and whatnot.
4883          */
4884         mutex_lock(&dev->mode_config.mutex);
4885         drm_modeset_acquire_init(ctx, 0);
4886         while (1) {
4887                 ret = drm_modeset_lock_all_ctx(dev, ctx);
4888                 if (ret != -EDEADLK)
4889                         break;
4890
4891                 drm_modeset_backoff(ctx);
4892         }
4893         /*
4894          * Disabling the crtcs gracefully seems nicer. Also the
4895          * g33 docs say we should at least disable all the planes.
4896          */
4897         state = drm_atomic_helper_duplicate_state(dev, ctx);
4898         if (IS_ERR(state)) {
4899                 ret = PTR_ERR(state);
4900                 drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
4901                         ret);
4902                 return;
4903         }
4904
4905         ret = drm_atomic_helper_disable_all(dev, ctx);
4906         if (ret) {
4907                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4908                         ret);
4909                 drm_atomic_state_put(state);
4910                 return;
4911         }
4912
4913         dev_priv->modeset_restore_state = state;
4914         state->acquire_ctx = ctx;
4915 }
4916
4917 void intel_display_finish_reset(struct drm_i915_private *dev_priv)
4918 {
4919         struct drm_device *dev = &dev_priv->drm;
4920         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
4921         struct drm_atomic_state *state;
4922         int ret;
4923
4924         if (!HAS_DISPLAY(dev_priv))
4925                 return;
4926
4927         /* reset doesn't touch the display */
4928         if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
4929                 return;
4930
4931         state = fetch_and_zero(&dev_priv->modeset_restore_state);
4932         if (!state)
4933                 goto unlock;
4934
4935         /* reset doesn't touch the display */
4936         if (!gpu_reset_clobbers_display(dev_priv)) {
4937                 /* for testing only restore the display */
4938                 ret = __intel_display_resume(dev, state, ctx);
4939                 if (ret)
4940                         drm_err(&dev_priv->drm,
4941                                 "Restoring old state failed with %i\n", ret);
4942         } else {
4943                 /*
4944                  * The display has been reset as well,
4945                  * so need a full re-initialization.
4946                  */
4947                 intel_pps_unlock_regs_wa(dev_priv);
4948                 intel_modeset_init_hw(dev_priv);
4949                 intel_init_clock_gating(dev_priv);
4950                 intel_hpd_init(dev_priv);
4951
4952                 ret = __intel_display_resume(dev, state, ctx);
4953                 if (ret)
4954                         drm_err(&dev_priv->drm,
4955                                 "Restoring old state failed with %i\n", ret);
4956
4957                 intel_hpd_poll_disable(dev_priv);
4958         }
4959
4960         drm_atomic_state_put(state);
4961 unlock:
4962         drm_modeset_drop_locks(ctx);
4963         drm_modeset_acquire_fini(ctx);
4964         mutex_unlock(&dev->mode_config.mutex);
4965
4966         clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
4967 }
4968
4969 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
4970 {
4971         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4972         enum pipe pipe = crtc->pipe;
4973         u32 tmp;
4974
4975         tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
4976
4977         /*
4978          * Display WA #1153: icl
4979          * enable hardware to bypass the alpha math
4980          * and rounding for per-pixel values 00 and 0xff
4981          */
4982         tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
4983         /*
4984          * Display WA # 1605353570: icl
4985          * Set the pixel rounding bit to 1 for allowing
4986          * passthrough of Frame buffer pixels unmodified
4987          * across pipe
4988          */
4989         tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
4990         intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
4991 }
4992
4993 static void intel_fdi_normal_train(struct intel_crtc *crtc)
4994 {
4995         struct drm_device *dev = crtc->base.dev;
4996         struct drm_i915_private *dev_priv = to_i915(dev);
4997         enum pipe pipe = crtc->pipe;
4998         i915_reg_t reg;
4999         u32 temp;
5000
5001         /* enable normal train */
5002         reg = FDI_TX_CTL(pipe);
5003         temp = intel_de_read(dev_priv, reg);
5004         if (IS_IVYBRIDGE(dev_priv)) {
5005                 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5006                 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
5007         } else {
5008                 temp &= ~FDI_LINK_TRAIN_NONE;
5009                 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
5010         }
5011         intel_de_write(dev_priv, reg, temp);
5012
5013         reg = FDI_RX_CTL(pipe);
5014         temp = intel_de_read(dev_priv, reg);
5015         if (HAS_PCH_CPT(dev_priv)) {
5016                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5017                 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
5018         } else {
5019                 temp &= ~FDI_LINK_TRAIN_NONE;
5020                 temp |= FDI_LINK_TRAIN_NONE;
5021         }
5022         intel_de_write(dev_priv, reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
5023
5024         /* wait one idle pattern time */
5025         intel_de_posting_read(dev_priv, reg);
5026         udelay(1000);
5027
5028         /* IVB wants error correction enabled */
5029         if (IS_IVYBRIDGE(dev_priv))
5030                 intel_de_write(dev_priv, reg,
5031                                intel_de_read(dev_priv, reg) | FDI_FS_ERRC_ENABLE | FDI_FE_ERRC_ENABLE);
5032 }
5033
5034 /* The FDI link training functions for ILK/Ibexpeak. */
5035 static void ilk_fdi_link_train(struct intel_crtc *crtc,
5036                                const struct intel_crtc_state *crtc_state)
5037 {
5038         struct drm_device *dev = crtc->base.dev;
5039         struct drm_i915_private *dev_priv = to_i915(dev);
5040         enum pipe pipe = crtc->pipe;
5041         i915_reg_t reg;
5042         u32 temp, tries;
5043
5044         /* FDI needs bits from pipe first */
5045         assert_pipe_enabled(dev_priv, crtc_state->cpu_transcoder);
5046
5047         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5048            for train result */
5049         reg = FDI_RX_IMR(pipe);
5050         temp = intel_de_read(dev_priv, reg);
5051         temp &= ~FDI_RX_SYMBOL_LOCK;
5052         temp &= ~FDI_RX_BIT_LOCK;
5053         intel_de_write(dev_priv, reg, temp);
5054         intel_de_read(dev_priv, reg);
5055         udelay(150);
5056
5057         /* enable CPU FDI TX and PCH FDI RX */
5058         reg = FDI_TX_CTL(pipe);
5059         temp = intel_de_read(dev_priv, reg);
5060         temp &= ~FDI_DP_PORT_WIDTH_MASK;
5061         temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5062         temp &= ~FDI_LINK_TRAIN_NONE;
5063         temp |= FDI_LINK_TRAIN_PATTERN_1;
5064         intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5065
5066         reg = FDI_RX_CTL(pipe);
5067         temp = intel_de_read(dev_priv, reg);
5068         temp &= ~FDI_LINK_TRAIN_NONE;
5069         temp |= FDI_LINK_TRAIN_PATTERN_1;
5070         intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5071
5072         intel_de_posting_read(dev_priv, reg);
5073         udelay(150);
5074
5075         /* Ironlake workaround, enable clock pointer after FDI enable*/
5076         intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5077                        FDI_RX_PHASE_SYNC_POINTER_OVR);
5078         intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5079                        FDI_RX_PHASE_SYNC_POINTER_OVR | FDI_RX_PHASE_SYNC_POINTER_EN);
5080
5081         reg = FDI_RX_IIR(pipe);
5082         for (tries = 0; tries < 5; tries++) {
5083                 temp = intel_de_read(dev_priv, reg);
5084                 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5085
5086                 if ((temp & FDI_RX_BIT_LOCK)) {
5087                         drm_dbg_kms(&dev_priv->drm, "FDI train 1 done.\n");
5088                         intel_de_write(dev_priv, reg, temp | FDI_RX_BIT_LOCK);
5089                         break;
5090                 }
5091         }
5092         if (tries == 5)
5093                 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5094
5095         /* Train 2 */
5096         reg = FDI_TX_CTL(pipe);
5097         temp = intel_de_read(dev_priv, reg);
5098         temp &= ~FDI_LINK_TRAIN_NONE;
5099         temp |= FDI_LINK_TRAIN_PATTERN_2;
5100         intel_de_write(dev_priv, reg, temp);
5101
5102         reg = FDI_RX_CTL(pipe);
5103         temp = intel_de_read(dev_priv, reg);
5104         temp &= ~FDI_LINK_TRAIN_NONE;
5105         temp |= FDI_LINK_TRAIN_PATTERN_2;
5106         intel_de_write(dev_priv, reg, temp);
5107
5108         intel_de_posting_read(dev_priv, reg);
5109         udelay(150);
5110
5111         reg = FDI_RX_IIR(pipe);
5112         for (tries = 0; tries < 5; tries++) {
5113                 temp = intel_de_read(dev_priv, reg);
5114                 drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5115
5116                 if (temp & FDI_RX_SYMBOL_LOCK) {
5117                         intel_de_write(dev_priv, reg,
5118                                        temp | FDI_RX_SYMBOL_LOCK);
5119                         drm_dbg_kms(&dev_priv->drm, "FDI train 2 done.\n");
5120                         break;
5121                 }
5122         }
5123         if (tries == 5)
5124                 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5125
5126         drm_dbg_kms(&dev_priv->drm, "FDI train done\n");
5127
5128 }
5129
5130 static const int snb_b_fdi_train_param[] = {
5131         FDI_LINK_TRAIN_400MV_0DB_SNB_B,
5132         FDI_LINK_TRAIN_400MV_6DB_SNB_B,
5133         FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
5134         FDI_LINK_TRAIN_800MV_0DB_SNB_B,
5135 };
5136
5137 /* The FDI link training functions for SNB/Cougarpoint. */
5138 static void gen6_fdi_link_train(struct intel_crtc *crtc,
5139                                 const struct intel_crtc_state *crtc_state)
5140 {
5141         struct drm_device *dev = crtc->base.dev;
5142         struct drm_i915_private *dev_priv = to_i915(dev);
5143         enum pipe pipe = crtc->pipe;
5144         i915_reg_t reg;
5145         u32 temp, i, retry;
5146
5147         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5148            for train result */
5149         reg = FDI_RX_IMR(pipe);
5150         temp = intel_de_read(dev_priv, reg);
5151         temp &= ~FDI_RX_SYMBOL_LOCK;
5152         temp &= ~FDI_RX_BIT_LOCK;
5153         intel_de_write(dev_priv, reg, temp);
5154
5155         intel_de_posting_read(dev_priv, reg);
5156         udelay(150);
5157
5158         /* enable CPU FDI TX and PCH FDI RX */
5159         reg = FDI_TX_CTL(pipe);
5160         temp = intel_de_read(dev_priv, reg);
5161         temp &= ~FDI_DP_PORT_WIDTH_MASK;
5162         temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5163         temp &= ~FDI_LINK_TRAIN_NONE;
5164         temp |= FDI_LINK_TRAIN_PATTERN_1;
5165         temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5166         /* SNB-B */
5167         temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5168         intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5169
5170         intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5171                        FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5172
5173         reg = FDI_RX_CTL(pipe);
5174         temp = intel_de_read(dev_priv, reg);
5175         if (HAS_PCH_CPT(dev_priv)) {
5176                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5177                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5178         } else {
5179                 temp &= ~FDI_LINK_TRAIN_NONE;
5180                 temp |= FDI_LINK_TRAIN_PATTERN_1;
5181         }
5182         intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5183
5184         intel_de_posting_read(dev_priv, reg);
5185         udelay(150);
5186
5187         for (i = 0; i < 4; i++) {
5188                 reg = FDI_TX_CTL(pipe);
5189                 temp = intel_de_read(dev_priv, reg);
5190                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5191                 temp |= snb_b_fdi_train_param[i];
5192                 intel_de_write(dev_priv, reg, temp);
5193
5194                 intel_de_posting_read(dev_priv, reg);
5195                 udelay(500);
5196
5197                 for (retry = 0; retry < 5; retry++) {
5198                         reg = FDI_RX_IIR(pipe);
5199                         temp = intel_de_read(dev_priv, reg);
5200                         drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5201                         if (temp & FDI_RX_BIT_LOCK) {
5202                                 intel_de_write(dev_priv, reg,
5203                                                temp | FDI_RX_BIT_LOCK);
5204                                 drm_dbg_kms(&dev_priv->drm,
5205                                             "FDI train 1 done.\n");
5206                                 break;
5207                         }
5208                         udelay(50);
5209                 }
5210                 if (retry < 5)
5211                         break;
5212         }
5213         if (i == 4)
5214                 drm_err(&dev_priv->drm, "FDI train 1 fail!\n");
5215
5216         /* Train 2 */
5217         reg = FDI_TX_CTL(pipe);
5218         temp = intel_de_read(dev_priv, reg);
5219         temp &= ~FDI_LINK_TRAIN_NONE;
5220         temp |= FDI_LINK_TRAIN_PATTERN_2;
5221         if (IS_GEN(dev_priv, 6)) {
5222                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5223                 /* SNB-B */
5224                 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5225         }
5226         intel_de_write(dev_priv, reg, temp);
5227
5228         reg = FDI_RX_CTL(pipe);
5229         temp = intel_de_read(dev_priv, reg);
5230         if (HAS_PCH_CPT(dev_priv)) {
5231                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5232                 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5233         } else {
5234                 temp &= ~FDI_LINK_TRAIN_NONE;
5235                 temp |= FDI_LINK_TRAIN_PATTERN_2;
5236         }
5237         intel_de_write(dev_priv, reg, temp);
5238
5239         intel_de_posting_read(dev_priv, reg);
5240         udelay(150);
5241
5242         for (i = 0; i < 4; i++) {
5243                 reg = FDI_TX_CTL(pipe);
5244                 temp = intel_de_read(dev_priv, reg);
5245                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5246                 temp |= snb_b_fdi_train_param[i];
5247                 intel_de_write(dev_priv, reg, temp);
5248
5249                 intel_de_posting_read(dev_priv, reg);
5250                 udelay(500);
5251
5252                 for (retry = 0; retry < 5; retry++) {
5253                         reg = FDI_RX_IIR(pipe);
5254                         temp = intel_de_read(dev_priv, reg);
5255                         drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5256                         if (temp & FDI_RX_SYMBOL_LOCK) {
5257                                 intel_de_write(dev_priv, reg,
5258                                                temp | FDI_RX_SYMBOL_LOCK);
5259                                 drm_dbg_kms(&dev_priv->drm,
5260                                             "FDI train 2 done.\n");
5261                                 break;
5262                         }
5263                         udelay(50);
5264                 }
5265                 if (retry < 5)
5266                         break;
5267         }
5268         if (i == 4)
5269                 drm_err(&dev_priv->drm, "FDI train 2 fail!\n");
5270
5271         drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5272 }
5273
5274 /* Manual link training for Ivy Bridge A0 parts */
5275 static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
5276                                       const struct intel_crtc_state *crtc_state)
5277 {
5278         struct drm_device *dev = crtc->base.dev;
5279         struct drm_i915_private *dev_priv = to_i915(dev);
5280         enum pipe pipe = crtc->pipe;
5281         i915_reg_t reg;
5282         u32 temp, i, j;
5283
5284         /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
5285            for train result */
5286         reg = FDI_RX_IMR(pipe);
5287         temp = intel_de_read(dev_priv, reg);
5288         temp &= ~FDI_RX_SYMBOL_LOCK;
5289         temp &= ~FDI_RX_BIT_LOCK;
5290         intel_de_write(dev_priv, reg, temp);
5291
5292         intel_de_posting_read(dev_priv, reg);
5293         udelay(150);
5294
5295         drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR before link train 0x%x\n",
5296                     intel_de_read(dev_priv, FDI_RX_IIR(pipe)));
5297
5298         /* Try each vswing and preemphasis setting twice before moving on */
5299         for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
5300                 /* disable first in case we need to retry */
5301                 reg = FDI_TX_CTL(pipe);
5302                 temp = intel_de_read(dev_priv, reg);
5303                 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
5304                 temp &= ~FDI_TX_ENABLE;
5305                 intel_de_write(dev_priv, reg, temp);
5306
5307                 reg = FDI_RX_CTL(pipe);
5308                 temp = intel_de_read(dev_priv, reg);
5309                 temp &= ~FDI_LINK_TRAIN_AUTO;
5310                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5311                 temp &= ~FDI_RX_ENABLE;
5312                 intel_de_write(dev_priv, reg, temp);
5313
5314                 /* enable CPU FDI TX and PCH FDI RX */
5315                 reg = FDI_TX_CTL(pipe);
5316                 temp = intel_de_read(dev_priv, reg);
5317                 temp &= ~FDI_DP_PORT_WIDTH_MASK;
5318                 temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5319                 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
5320                 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
5321                 temp |= snb_b_fdi_train_param[j/2];
5322                 temp |= FDI_COMPOSITE_SYNC;
5323                 intel_de_write(dev_priv, reg, temp | FDI_TX_ENABLE);
5324
5325                 intel_de_write(dev_priv, FDI_RX_MISC(pipe),
5326                                FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
5327
5328                 reg = FDI_RX_CTL(pipe);
5329                 temp = intel_de_read(dev_priv, reg);
5330                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5331                 temp |= FDI_COMPOSITE_SYNC;
5332                 intel_de_write(dev_priv, reg, temp | FDI_RX_ENABLE);
5333
5334                 intel_de_posting_read(dev_priv, reg);
5335                 udelay(1); /* should be 0.5us */
5336
5337                 for (i = 0; i < 4; i++) {
5338                         reg = FDI_RX_IIR(pipe);
5339                         temp = intel_de_read(dev_priv, reg);
5340                         drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5341
5342                         if (temp & FDI_RX_BIT_LOCK ||
5343                             (intel_de_read(dev_priv, reg) & FDI_RX_BIT_LOCK)) {
5344                                 intel_de_write(dev_priv, reg,
5345                                                temp | FDI_RX_BIT_LOCK);
5346                                 drm_dbg_kms(&dev_priv->drm,
5347                                             "FDI train 1 done, level %i.\n",
5348                                             i);
5349                                 break;
5350                         }
5351                         udelay(1); /* should be 0.5us */
5352                 }
5353                 if (i == 4) {
5354                         drm_dbg_kms(&dev_priv->drm,
5355                                     "FDI train 1 fail on vswing %d\n", j / 2);
5356                         continue;
5357                 }
5358
5359                 /* Train 2 */
5360                 reg = FDI_TX_CTL(pipe);
5361                 temp = intel_de_read(dev_priv, reg);
5362                 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
5363                 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
5364                 intel_de_write(dev_priv, reg, temp);
5365
5366                 reg = FDI_RX_CTL(pipe);
5367                 temp = intel_de_read(dev_priv, reg);
5368                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5369                 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
5370                 intel_de_write(dev_priv, reg, temp);
5371
5372                 intel_de_posting_read(dev_priv, reg);
5373                 udelay(2); /* should be 1.5us */
5374
5375                 for (i = 0; i < 4; i++) {
5376                         reg = FDI_RX_IIR(pipe);
5377                         temp = intel_de_read(dev_priv, reg);
5378                         drm_dbg_kms(&dev_priv->drm, "FDI_RX_IIR 0x%x\n", temp);
5379
5380                         if (temp & FDI_RX_SYMBOL_LOCK ||
5381                             (intel_de_read(dev_priv, reg) & FDI_RX_SYMBOL_LOCK)) {
5382                                 intel_de_write(dev_priv, reg,
5383                                                temp | FDI_RX_SYMBOL_LOCK);
5384                                 drm_dbg_kms(&dev_priv->drm,
5385                                             "FDI train 2 done, level %i.\n",
5386                                             i);
5387                                 goto train_done;
5388                         }
5389                         udelay(2); /* should be 1.5us */
5390                 }
5391                 if (i == 4)
5392                         drm_dbg_kms(&dev_priv->drm,
5393                                     "FDI train 2 fail on vswing %d\n", j / 2);
5394         }
5395
5396 train_done:
5397         drm_dbg_kms(&dev_priv->drm, "FDI train done.\n");
5398 }
5399
5400 static void ilk_fdi_pll_enable(const struct intel_crtc_state *crtc_state)
5401 {
5402         struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
5403         struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5404         enum pipe pipe = intel_crtc->pipe;
5405         i915_reg_t reg;
5406         u32 temp;
5407
5408         /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5409         reg = FDI_RX_CTL(pipe);
5410         temp = intel_de_read(dev_priv, reg);
5411         temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
5412         temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
5413         temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5414         intel_de_write(dev_priv, reg, temp | FDI_RX_PLL_ENABLE);
5415
5416         intel_de_posting_read(dev_priv, reg);
5417         udelay(200);
5418
5419         /* Switch from Rawclk to PCDclk */
5420         temp = intel_de_read(dev_priv, reg);
5421         intel_de_write(dev_priv, reg, temp | FDI_PCDCLK);
5422
5423         intel_de_posting_read(dev_priv, reg);
5424         udelay(200);
5425
5426         /* Enable CPU FDI TX PLL, always on for Ironlake */
5427         reg = FDI_TX_CTL(pipe);
5428         temp = intel_de_read(dev_priv, reg);
5429         if ((temp & FDI_TX_PLL_ENABLE) == 0) {
5430                 intel_de_write(dev_priv, reg, temp | FDI_TX_PLL_ENABLE);
5431
5432                 intel_de_posting_read(dev_priv, reg);
5433                 udelay(100);
5434         }
5435 }
5436
5437 static void ilk_fdi_pll_disable(struct intel_crtc *intel_crtc)
5438 {
5439         struct drm_device *dev = intel_crtc->base.dev;
5440         struct drm_i915_private *dev_priv = to_i915(dev);
5441         enum pipe pipe = intel_crtc->pipe;
5442         i915_reg_t reg;
5443         u32 temp;
5444
5445         /* Switch from PCDclk to Rawclk */
5446         reg = FDI_RX_CTL(pipe);
5447         temp = intel_de_read(dev_priv, reg);
5448         intel_de_write(dev_priv, reg, temp & ~FDI_PCDCLK);
5449
5450         /* Disable CPU FDI TX PLL */
5451         reg = FDI_TX_CTL(pipe);
5452         temp = intel_de_read(dev_priv, reg);
5453         intel_de_write(dev_priv, reg, temp & ~FDI_TX_PLL_ENABLE);
5454
5455         intel_de_posting_read(dev_priv, reg);
5456         udelay(100);
5457
5458         reg = FDI_RX_CTL(pipe);
5459         temp = intel_de_read(dev_priv, reg);
5460         intel_de_write(dev_priv, reg, temp & ~FDI_RX_PLL_ENABLE);
5461
5462         /* Wait for the clocks to turn off. */
5463         intel_de_posting_read(dev_priv, reg);
5464         udelay(100);
5465 }
5466
5467 static void ilk_fdi_disable(struct intel_crtc *crtc)
5468 {
5469         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5470         enum pipe pipe = crtc->pipe;
5471         i915_reg_t reg;
5472         u32 temp;
5473
5474         /* disable CPU FDI tx and PCH FDI rx */
5475         reg = FDI_TX_CTL(pipe);
5476         temp = intel_de_read(dev_priv, reg);
5477         intel_de_write(dev_priv, reg, temp & ~FDI_TX_ENABLE);
5478         intel_de_posting_read(dev_priv, reg);
5479
5480         reg = FDI_RX_CTL(pipe);
5481         temp = intel_de_read(dev_priv, reg);
5482         temp &= ~(0x7 << 16);
5483         temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5484         intel_de_write(dev_priv, reg, temp & ~FDI_RX_ENABLE);
5485
5486         intel_de_posting_read(dev_priv, reg);
5487         udelay(100);
5488
5489         /* Ironlake workaround, disable clock pointer after downing FDI */
5490         if (HAS_PCH_IBX(dev_priv))
5491                 intel_de_write(dev_priv, FDI_RX_CHICKEN(pipe),
5492                                FDI_RX_PHASE_SYNC_POINTER_OVR);
5493
5494         /* still set train pattern 1 */
5495         reg = FDI_TX_CTL(pipe);
5496         temp = intel_de_read(dev_priv, reg);
5497         temp &= ~FDI_LINK_TRAIN_NONE;
5498         temp |= FDI_LINK_TRAIN_PATTERN_1;
5499         intel_de_write(dev_priv, reg, temp);
5500
5501         reg = FDI_RX_CTL(pipe);
5502         temp = intel_de_read(dev_priv, reg);
5503         if (HAS_PCH_CPT(dev_priv)) {
5504                 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
5505                 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
5506         } else {
5507                 temp &= ~FDI_LINK_TRAIN_NONE;
5508                 temp |= FDI_LINK_TRAIN_PATTERN_1;
5509         }
5510         /* BPC in FDI rx is consistent with that in PIPECONF */
5511         temp &= ~(0x07 << 16);
5512         temp |= (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5513         intel_de_write(dev_priv, reg, temp);
5514
5515         intel_de_posting_read(dev_priv, reg);
5516         udelay(100);
5517 }
5518
5519 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5520 {
5521         struct drm_crtc *crtc;
5522         bool cleanup_done;
5523
5524         drm_for_each_crtc(crtc, &dev_priv->drm) {
5525                 struct drm_crtc_commit *commit;
5526                 spin_lock(&crtc->commit_lock);
5527                 commit = list_first_entry_or_null(&crtc->commit_list,
5528                                                   struct drm_crtc_commit, commit_entry);
5529                 cleanup_done = commit ?
5530                         try_wait_for_completion(&commit->cleanup_done) : true;
5531                 spin_unlock(&crtc->commit_lock);
5532
5533                 if (cleanup_done)
5534                         continue;
5535
5536                 drm_crtc_wait_one_vblank(crtc);
5537
5538                 return true;
5539         }
5540
5541         return false;
5542 }
5543
5544 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
5545 {
5546         u32 temp;
5547
5548         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
5549
5550         mutex_lock(&dev_priv->sb_lock);
5551
5552         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5553         temp |= SBI_SSCCTL_DISABLE;
5554         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5555
5556         mutex_unlock(&dev_priv->sb_lock);
5557 }
5558
5559 /* Program iCLKIP clock to the desired frequency */
5560 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
5561 {
5562         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5563         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5564         int clock = crtc_state->hw.adjusted_mode.crtc_clock;
5565         u32 divsel, phaseinc, auxdiv, phasedir = 0;
5566         u32 temp;
5567
5568         lpt_disable_iclkip(dev_priv);
5569
5570         /* The iCLK virtual clock root frequency is in MHz,
5571          * but the adjusted_mode->crtc_clock in in KHz. To get the
5572          * divisors, it is necessary to divide one by another, so we
5573          * convert the virtual clock precision to KHz here for higher
5574          * precision.
5575          */
5576         for (auxdiv = 0; auxdiv < 2; auxdiv++) {
5577                 u32 iclk_virtual_root_freq = 172800 * 1000;
5578                 u32 iclk_pi_range = 64;
5579                 u32 desired_divisor;
5580
5581                 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5582                                                     clock << auxdiv);
5583                 divsel = (desired_divisor / iclk_pi_range) - 2;
5584                 phaseinc = desired_divisor % iclk_pi_range;
5585
5586                 /*
5587                  * Near 20MHz is a corner case which is
5588                  * out of range for the 7-bit divisor
5589                  */
5590                 if (divsel <= 0x7f)
5591                         break;
5592         }
5593
5594         /* This should not happen with any sane values */
5595         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
5596                     ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
5597         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
5598                     ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
5599
5600         drm_dbg_kms(&dev_priv->drm,
5601                     "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
5602                     clock, auxdiv, divsel, phasedir, phaseinc);
5603
5604         mutex_lock(&dev_priv->sb_lock);
5605
5606         /* Program SSCDIVINTPHASE6 */
5607         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5608         temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
5609         temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
5610         temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
5611         temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
5612         temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
5613         temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
5614         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
5615
5616         /* Program SSCAUXDIV */
5617         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5618         temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
5619         temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
5620         intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
5621
5622         /* Enable modulator and associated divider */
5623         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5624         temp &= ~SBI_SSCCTL_DISABLE;
5625         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
5626
5627         mutex_unlock(&dev_priv->sb_lock);
5628
5629         /* Wait for initialization time */
5630         udelay(24);
5631
5632         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
5633 }
5634
5635 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
5636 {
5637         u32 divsel, phaseinc, auxdiv;
5638         u32 iclk_virtual_root_freq = 172800 * 1000;
5639         u32 iclk_pi_range = 64;
5640         u32 desired_divisor;
5641         u32 temp;
5642
5643         if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
5644                 return 0;
5645
5646         mutex_lock(&dev_priv->sb_lock);
5647
5648         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
5649         if (temp & SBI_SSCCTL_DISABLE) {
5650                 mutex_unlock(&dev_priv->sb_lock);
5651                 return 0;
5652         }
5653
5654         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
5655         divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
5656                 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
5657         phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
5658                 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
5659
5660         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
5661         auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
5662                 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
5663
5664         mutex_unlock(&dev_priv->sb_lock);
5665
5666         desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
5667
5668         return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
5669                                  desired_divisor << auxdiv);
5670 }
5671
5672 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
5673                                            enum pipe pch_transcoder)
5674 {
5675         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5676         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5677         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5678
5679         intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
5680                        intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
5681         intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
5682                        intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
5683         intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
5684                        intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
5685
5686         intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
5687                        intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5688         intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
5689                        intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
5690         intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
5691                        intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
5692         intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
5693                        intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
5694 }
5695
5696 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
5697 {
5698         u32 temp;
5699
5700         temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
5701         if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
5702                 return;
5703
5704         drm_WARN_ON(&dev_priv->drm,
5705                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
5706                     FDI_RX_ENABLE);
5707         drm_WARN_ON(&dev_priv->drm,
5708                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
5709                     FDI_RX_ENABLE);
5710
5711         temp &= ~FDI_BC_BIFURCATION_SELECT;
5712         if (enable)
5713                 temp |= FDI_BC_BIFURCATION_SELECT;
5714
5715         drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
5716                     enable ? "en" : "dis");
5717         intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
5718         intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
5719 }
5720
5721 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
5722 {
5723         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5724         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5725
5726         switch (crtc->pipe) {
5727         case PIPE_A:
5728                 break;
5729         case PIPE_B:
5730                 if (crtc_state->fdi_lanes > 2)
5731                         cpt_set_fdi_bc_bifurcation(dev_priv, false);
5732                 else
5733                         cpt_set_fdi_bc_bifurcation(dev_priv, true);
5734
5735                 break;
5736         case PIPE_C:
5737                 cpt_set_fdi_bc_bifurcation(dev_priv, true);
5738
5739                 break;
5740         default:
5741                 BUG();
5742         }
5743 }
5744
5745 /*
5746  * Finds the encoder associated with the given CRTC. This can only be
5747  * used when we know that the CRTC isn't feeding multiple encoders!
5748  */
5749 static struct intel_encoder *
5750 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
5751                            const struct intel_crtc_state *crtc_state)
5752 {
5753         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5754         const struct drm_connector_state *connector_state;
5755         const struct drm_connector *connector;
5756         struct intel_encoder *encoder = NULL;
5757         int num_encoders = 0;
5758         int i;
5759
5760         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5761                 if (connector_state->crtc != &crtc->base)
5762                         continue;
5763
5764                 encoder = to_intel_encoder(connector_state->best_encoder);
5765                 num_encoders++;
5766         }
5767
5768         drm_WARN(encoder->base.dev, num_encoders != 1,
5769                  "%d encoders for pipe %c\n",
5770                  num_encoders, pipe_name(crtc->pipe));
5771
5772         return encoder;
5773 }
5774
5775 /*
5776  * Enable PCH resources required for PCH ports:
5777  *   - PCH PLLs
5778  *   - FDI training & RX/TX
5779  *   - update transcoder timings
5780  *   - DP transcoding bits
5781  *   - transcoder
5782  */
5783 static void ilk_pch_enable(const struct intel_atomic_state *state,
5784                            const struct intel_crtc_state *crtc_state)
5785 {
5786         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5787         struct drm_device *dev = crtc->base.dev;
5788         struct drm_i915_private *dev_priv = to_i915(dev);
5789         enum pipe pipe = crtc->pipe;
5790         u32 temp;
5791
5792         assert_pch_transcoder_disabled(dev_priv, pipe);
5793
5794         if (IS_IVYBRIDGE(dev_priv))
5795                 ivb_update_fdi_bc_bifurcation(crtc_state);
5796
5797         /* Write the TU size bits before fdi link training, so that error
5798          * detection works. */
5799         intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
5800                        intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
5801
5802         /* For PCH output, training FDI link */
5803         dev_priv->display.fdi_link_train(crtc, crtc_state);
5804
5805         /* We need to program the right clock selection before writing the pixel
5806          * mutliplier into the DPLL. */
5807         if (HAS_PCH_CPT(dev_priv)) {
5808                 u32 sel;
5809
5810                 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5811                 temp |= TRANS_DPLL_ENABLE(pipe);
5812                 sel = TRANS_DPLLB_SEL(pipe);
5813                 if (crtc_state->shared_dpll ==
5814                     intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
5815                         temp |= sel;
5816                 else
5817                         temp &= ~sel;
5818                 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
5819         }
5820
5821         /* XXX: pch pll's can be enabled any time before we enable the PCH
5822          * transcoder, and we actually should do this to not upset any PCH
5823          * transcoder that already use the clock when we share it.
5824          *
5825          * Note that enable_shared_dpll tries to do the right thing, but
5826          * get_shared_dpll unconditionally resets the pll - we need that to have
5827          * the right LVDS enable sequence. */
5828         intel_enable_shared_dpll(crtc_state);
5829
5830         /* set transcoder timing, panel must allow it */
5831         assert_panel_unlocked(dev_priv, pipe);
5832         ilk_pch_transcoder_set_timings(crtc_state, pipe);
5833
5834         intel_fdi_normal_train(crtc);
5835
5836         /* For PCH DP, enable TRANS_DP_CTL */
5837         if (HAS_PCH_CPT(dev_priv) &&
5838             intel_crtc_has_dp_encoder(crtc_state)) {
5839                 const struct drm_display_mode *adjusted_mode =
5840                         &crtc_state->hw.adjusted_mode;
5841                 u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5842                 i915_reg_t reg = TRANS_DP_CTL(pipe);
5843                 enum port port;
5844
5845                 temp = intel_de_read(dev_priv, reg);
5846                 temp &= ~(TRANS_DP_PORT_SEL_MASK |
5847                           TRANS_DP_SYNC_MASK |
5848                           TRANS_DP_BPC_MASK);
5849                 temp |= TRANS_DP_OUTPUT_ENABLE;
5850                 temp |= bpc << 9; /* same format but at 11:9 */
5851
5852                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5853                         temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
5854                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5855                         temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
5856
5857                 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
5858                 drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
5859                 temp |= TRANS_DP_PORT_SEL(port);
5860
5861                 intel_de_write(dev_priv, reg, temp);
5862         }
5863
5864         ilk_enable_pch_transcoder(crtc_state);
5865 }
5866
5867 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
5868 {
5869         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5870         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5871         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5872
5873         assert_pch_transcoder_disabled(dev_priv, PIPE_A);
5874
5875         lpt_program_iclkip(crtc_state);
5876
5877         /* Set transcoder timing. */
5878         ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
5879
5880         lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
5881 }
5882
5883 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
5884                                enum pipe pipe)
5885 {
5886         i915_reg_t dslreg = PIPEDSL(pipe);
5887         u32 temp;
5888
5889         temp = intel_de_read(dev_priv, dslreg);
5890         udelay(500);
5891         if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
5892                 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
5893                         drm_err(&dev_priv->drm,
5894                                 "mode set failed: pipe %c stuck\n",
5895                                 pipe_name(pipe));
5896         }
5897 }
5898
5899 /*
5900  * The hardware phase 0.0 refers to the center of the pixel.
5901  * We want to start from the top/left edge which is phase
5902  * -0.5. That matches how the hardware calculates the scaling
5903  * factors (from top-left of the first pixel to bottom-right
5904  * of the last pixel, as opposed to the pixel centers).
5905  *
5906  * For 4:2:0 subsampled chroma planes we obviously have to
5907  * adjust that so that the chroma sample position lands in
5908  * the right spot.
5909  *
5910  * Note that for packed YCbCr 4:2:2 formats there is no way to
5911  * control chroma siting. The hardware simply replicates the
5912  * chroma samples for both of the luma samples, and thus we don't
5913  * actually get the expected MPEG2 chroma siting convention :(
5914  * The same behaviour is observed on pre-SKL platforms as well.
5915  *
5916  * Theory behind the formula (note that we ignore sub-pixel
5917  * source coordinates):
5918  * s = source sample position
5919  * d = destination sample position
5920  *
5921  * Downscaling 4:1:
5922  * -0.5
5923  * | 0.0
5924  * | |     1.5 (initial phase)
5925  * | |     |
5926  * v v     v
5927  * | s | s | s | s |
5928  * |       d       |
5929  *
5930  * Upscaling 1:4:
5931  * -0.5
5932  * | -0.375 (initial phase)
5933  * | |     0.0
5934  * | |     |
5935  * v v     v
5936  * |       s       |
5937  * | d | d | d | d |
5938  */
5939 u16 skl_scaler_calc_phase(int sub, int scale, bool chroma_cosited)
5940 {
5941         int phase = -0x8000;
5942         u16 trip = 0;
5943
5944         if (chroma_cosited)
5945                 phase += (sub - 1) * 0x8000 / sub;
5946
5947         phase += scale / (2 * sub);
5948
5949         /*
5950          * Hardware initial phase limited to [-0.5:1.5].
5951          * Since the max hardware scale factor is 3.0, we
5952          * should never actually excdeed 1.0 here.
5953          */
5954         WARN_ON(phase < -0x8000 || phase > 0x18000);
5955
5956         if (phase < 0)
5957                 phase = 0x10000 + phase;
5958         else
5959                 trip = PS_PHASE_TRIP;
5960
5961         return ((phase >> 2) & PS_PHASE_MASK) | trip;
5962 }
5963
5964 #define SKL_MIN_SRC_W 8
5965 #define SKL_MAX_SRC_W 4096
5966 #define SKL_MIN_SRC_H 8
5967 #define SKL_MAX_SRC_H 4096
5968 #define SKL_MIN_DST_W 8
5969 #define SKL_MAX_DST_W 4096
5970 #define SKL_MIN_DST_H 8
5971 #define SKL_MAX_DST_H 4096
5972 #define ICL_MAX_SRC_W 5120
5973 #define ICL_MAX_SRC_H 4096
5974 #define ICL_MAX_DST_W 5120
5975 #define ICL_MAX_DST_H 4096
5976 #define SKL_MIN_YUV_420_SRC_W 16
5977 #define SKL_MIN_YUV_420_SRC_H 16
5978
5979 static int
5980 skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
5981                   unsigned int scaler_user, int *scaler_id,
5982                   int src_w, int src_h, int dst_w, int dst_h,
5983                   const struct drm_format_info *format,
5984                   u64 modifier, bool need_scaler)
5985 {
5986         struct intel_crtc_scaler_state *scaler_state =
5987                 &crtc_state->scaler_state;
5988         struct intel_crtc *intel_crtc =
5989                 to_intel_crtc(crtc_state->uapi.crtc);
5990         struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5991         const struct drm_display_mode *adjusted_mode =
5992                 &crtc_state->hw.adjusted_mode;
5993
5994         /*
5995          * Src coordinates are already rotated by 270 degrees for
5996          * the 90/270 degree plane rotation cases (to match the
5997          * GTT mapping), hence no need to account for rotation here.
5998          */
5999         if (src_w != dst_w || src_h != dst_h)
6000                 need_scaler = true;
6001
6002         /*
6003          * Scaling/fitting not supported in IF-ID mode in GEN9+
6004          * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
6005          * Once NV12 is enabled, handle it here while allocating scaler
6006          * for NV12.
6007          */
6008         if (INTEL_GEN(dev_priv) >= 9 && crtc_state->hw.enable &&
6009             need_scaler && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6010                 drm_dbg_kms(&dev_priv->drm,
6011                             "Pipe/Plane scaling not supported with IF-ID mode\n");
6012                 return -EINVAL;
6013         }
6014
6015         /*
6016          * if plane is being disabled or scaler is no more required or force detach
6017          *  - free scaler binded to this plane/crtc
6018          *  - in order to do this, update crtc->scaler_usage
6019          *
6020          * Here scaler state in crtc_state is set free so that
6021          * scaler can be assigned to other user. Actual register
6022          * update to free the scaler is done in plane/panel-fit programming.
6023          * For this purpose crtc/plane_state->scaler_id isn't reset here.
6024          */
6025         if (force_detach || !need_scaler) {
6026                 if (*scaler_id >= 0) {
6027                         scaler_state->scaler_users &= ~(1 << scaler_user);
6028                         scaler_state->scalers[*scaler_id].in_use = 0;
6029
6030                         drm_dbg_kms(&dev_priv->drm,
6031                                     "scaler_user index %u.%u: "
6032                                     "Staged freeing scaler id %d scaler_users = 0x%x\n",
6033                                     intel_crtc->pipe, scaler_user, *scaler_id,
6034                                     scaler_state->scaler_users);
6035                         *scaler_id = -1;
6036                 }
6037                 return 0;
6038         }
6039
6040         if (format && intel_format_info_is_yuv_semiplanar(format, modifier) &&
6041             (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
6042                 drm_dbg_kms(&dev_priv->drm,
6043                             "Planar YUV: src dimensions not met\n");
6044                 return -EINVAL;
6045         }
6046
6047         /* range checks */
6048         if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
6049             dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
6050             (INTEL_GEN(dev_priv) >= 11 &&
6051              (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
6052               dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
6053             (INTEL_GEN(dev_priv) < 11 &&
6054              (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
6055               dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
6056                 drm_dbg_kms(&dev_priv->drm,
6057                             "scaler_user index %u.%u: src %ux%u dst %ux%u "
6058                             "size is out of scaler range\n",
6059                             intel_crtc->pipe, scaler_user, src_w, src_h,
6060                             dst_w, dst_h);
6061                 return -EINVAL;
6062         }
6063
6064         /* mark this plane as a scaler user in crtc_state */
6065         scaler_state->scaler_users |= (1 << scaler_user);
6066         drm_dbg_kms(&dev_priv->drm, "scaler_user index %u.%u: "
6067                     "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
6068                     intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
6069                     scaler_state->scaler_users);
6070
6071         return 0;
6072 }
6073
6074 static int skl_update_scaler_crtc(struct intel_crtc_state *crtc_state)
6075 {
6076         const struct drm_display_mode *adjusted_mode =
6077                 &crtc_state->hw.adjusted_mode;
6078         int width, height;
6079
6080         if (crtc_state->pch_pfit.enabled) {
6081                 width = drm_rect_width(&crtc_state->pch_pfit.dst);
6082                 height = drm_rect_height(&crtc_state->pch_pfit.dst);
6083         } else {
6084                 width = adjusted_mode->crtc_hdisplay;
6085                 height = adjusted_mode->crtc_vdisplay;
6086         }
6087
6088         return skl_update_scaler(crtc_state, !crtc_state->hw.active,
6089                                  SKL_CRTC_INDEX,
6090                                  &crtc_state->scaler_state.scaler_id,
6091                                  crtc_state->pipe_src_w, crtc_state->pipe_src_h,
6092                                  width, height, NULL, 0,
6093                                  crtc_state->pch_pfit.enabled);
6094 }
6095
6096 /**
6097  * skl_update_scaler_plane - Stages update to scaler state for a given plane.
6098  * @crtc_state: crtc's scaler state
6099  * @plane_state: atomic plane state to update
6100  *
6101  * Return
6102  *     0 - scaler_usage updated successfully
6103  *    error - requested scaling cannot be supported or other error condition
6104  */
6105 static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
6106                                    struct intel_plane_state *plane_state)
6107 {
6108         struct intel_plane *intel_plane =
6109                 to_intel_plane(plane_state->uapi.plane);
6110         struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
6111         struct drm_framebuffer *fb = plane_state->hw.fb;
6112         int ret;
6113         bool force_detach = !fb || !plane_state->uapi.visible;
6114         bool need_scaler = false;
6115
6116         /* Pre-gen11 and SDR planes always need a scaler for planar formats. */
6117         if (!icl_is_hdr_plane(dev_priv, intel_plane->id) &&
6118             fb && intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
6119                 need_scaler = true;
6120
6121         ret = skl_update_scaler(crtc_state, force_detach,
6122                                 drm_plane_index(&intel_plane->base),
6123                                 &plane_state->scaler_id,
6124                                 drm_rect_width(&plane_state->uapi.src) >> 16,
6125                                 drm_rect_height(&plane_state->uapi.src) >> 16,
6126                                 drm_rect_width(&plane_state->uapi.dst),
6127                                 drm_rect_height(&plane_state->uapi.dst),
6128                                 fb ? fb->format : NULL,
6129                                 fb ? fb->modifier : 0,
6130                                 need_scaler);
6131
6132         if (ret || plane_state->scaler_id < 0)
6133                 return ret;
6134
6135         /* check colorkey */
6136         if (plane_state->ckey.flags) {
6137                 drm_dbg_kms(&dev_priv->drm,
6138                             "[PLANE:%d:%s] scaling with color key not allowed",
6139                             intel_plane->base.base.id,
6140                             intel_plane->base.name);
6141                 return -EINVAL;
6142         }
6143
6144         /* Check src format */
6145         switch (fb->format->format) {
6146         case DRM_FORMAT_RGB565:
6147         case DRM_FORMAT_XBGR8888:
6148         case DRM_FORMAT_XRGB8888:
6149         case DRM_FORMAT_ABGR8888:
6150         case DRM_FORMAT_ARGB8888:
6151         case DRM_FORMAT_XRGB2101010:
6152         case DRM_FORMAT_XBGR2101010:
6153         case DRM_FORMAT_ARGB2101010:
6154         case DRM_FORMAT_ABGR2101010:
6155         case DRM_FORMAT_YUYV:
6156         case DRM_FORMAT_YVYU:
6157         case DRM_FORMAT_UYVY:
6158         case DRM_FORMAT_VYUY:
6159         case DRM_FORMAT_NV12:
6160         case DRM_FORMAT_XYUV8888:
6161         case DRM_FORMAT_P010:
6162         case DRM_FORMAT_P012:
6163         case DRM_FORMAT_P016:
6164         case DRM_FORMAT_Y210:
6165         case DRM_FORMAT_Y212:
6166         case DRM_FORMAT_Y216:
6167         case DRM_FORMAT_XVYU2101010:
6168         case DRM_FORMAT_XVYU12_16161616:
6169         case DRM_FORMAT_XVYU16161616:
6170                 break;
6171         case DRM_FORMAT_XBGR16161616F:
6172         case DRM_FORMAT_ABGR16161616F:
6173         case DRM_FORMAT_XRGB16161616F:
6174         case DRM_FORMAT_ARGB16161616F:
6175                 if (INTEL_GEN(dev_priv) >= 11)
6176                         break;
6177                 fallthrough;
6178         default:
6179                 drm_dbg_kms(&dev_priv->drm,
6180                             "[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
6181                             intel_plane->base.base.id, intel_plane->base.name,
6182                             fb->base.id, fb->format->format);
6183                 return -EINVAL;
6184         }
6185
6186         return 0;
6187 }
6188
6189 void skl_scaler_disable(const struct intel_crtc_state *old_crtc_state)
6190 {
6191         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
6192         int i;
6193
6194         for (i = 0; i < crtc->num_scalers; i++)
6195                 skl_detach_scaler(crtc, i);
6196 }
6197
6198 static int cnl_coef_tap(int i)
6199 {
6200         return i % 7;
6201 }
6202
6203 static u16 cnl_nearest_filter_coef(int t)
6204 {
6205         return t == 3 ? 0x0800 : 0x3000;
6206 }
6207
6208 /*
6209  *  Theory behind setting nearest-neighbor integer scaling:
6210  *
6211  *  17 phase of 7 taps requires 119 coefficients in 60 dwords per set.
6212  *  The letter represents the filter tap (D is the center tap) and the number
6213  *  represents the coefficient set for a phase (0-16).
6214  *
6215  *         +------------+------------------------+------------------------+
6216  *         |Index value | Data value coeffient 1 | Data value coeffient 2 |
6217  *         +------------+------------------------+------------------------+
6218  *         |   00h      |          B0            |          A0            |
6219  *         +------------+------------------------+------------------------+
6220  *         |   01h      |          D0            |          C0            |
6221  *         +------------+------------------------+------------------------+
6222  *         |   02h      |          F0            |          E0            |
6223  *         +------------+------------------------+------------------------+
6224  *         |   03h      |          A1            |          G0            |
6225  *         +------------+------------------------+------------------------+
6226  *         |   04h      |          C1            |          B1            |
6227  *         +------------+------------------------+------------------------+
6228  *         |   ...      |          ...           |          ...           |
6229  *         +------------+------------------------+------------------------+
6230  *         |   38h      |          B16           |          A16           |
6231  *         +------------+------------------------+------------------------+
6232  *         |   39h      |          D16           |          C16           |
6233  *         +------------+------------------------+------------------------+
6234  *         |   3Ah      |          F16           |          C16           |
6235  *         +------------+------------------------+------------------------+
6236  *         |   3Bh      |        Reserved        |          G16           |
6237  *         +------------+------------------------+------------------------+
6238  *
6239  *  To enable nearest-neighbor scaling:  program scaler coefficents with
6240  *  the center tap (Dxx) values set to 1 and all other values set to 0 as per
6241  *  SCALER_COEFFICIENT_FORMAT
6242  *
6243  */
6244
6245 static void cnl_program_nearest_filter_coefs(struct drm_i915_private *dev_priv,
6246                                              enum pipe pipe, int id, int set)
6247 {
6248         int i;
6249
6250         intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set),
6251                           PS_COEE_INDEX_AUTO_INC);
6252
6253         for (i = 0; i < 17 * 7; i += 2) {
6254                 u32 tmp;
6255                 int t;
6256
6257                 t = cnl_coef_tap(i);
6258                 tmp = cnl_nearest_filter_coef(t);
6259
6260                 t = cnl_coef_tap(i + 1);
6261                 tmp |= cnl_nearest_filter_coef(t) << 16;
6262
6263                 intel_de_write_fw(dev_priv, CNL_PS_COEF_DATA_SET(pipe, id, set),
6264                                   tmp);
6265         }
6266
6267         intel_de_write_fw(dev_priv, CNL_PS_COEF_INDEX_SET(pipe, id, set), 0);
6268 }
6269
6270 inline u32 skl_scaler_get_filter_select(enum drm_scaling_filter filter, int set)
6271 {
6272         if (filter == DRM_SCALING_FILTER_NEAREST_NEIGHBOR) {
6273                 return (PS_FILTER_PROGRAMMED |
6274                         PS_Y_VERT_FILTER_SELECT(set) |
6275                         PS_Y_HORZ_FILTER_SELECT(set) |
6276                         PS_UV_VERT_FILTER_SELECT(set) |
6277                         PS_UV_HORZ_FILTER_SELECT(set));
6278         }
6279
6280         return PS_FILTER_MEDIUM;
6281 }
6282
6283 void skl_scaler_setup_filter(struct drm_i915_private *dev_priv, enum pipe pipe,
6284                              int id, int set, enum drm_scaling_filter filter)
6285 {
6286         switch (filter) {
6287         case DRM_SCALING_FILTER_DEFAULT:
6288                 break;
6289         case DRM_SCALING_FILTER_NEAREST_NEIGHBOR:
6290                 cnl_program_nearest_filter_coefs(dev_priv, pipe, id, set);
6291                 break;
6292         default:
6293                 MISSING_CASE(filter);
6294         }
6295 }
6296
6297 static void skl_pfit_enable(const struct intel_crtc_state *crtc_state)
6298 {
6299         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6300         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6301         const struct intel_crtc_scaler_state *scaler_state =
6302                 &crtc_state->scaler_state;
6303         struct drm_rect src = {
6304                 .x2 = crtc_state->pipe_src_w << 16,
6305                 .y2 = crtc_state->pipe_src_h << 16,
6306         };
6307         const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6308         u16 uv_rgb_hphase, uv_rgb_vphase;
6309         enum pipe pipe = crtc->pipe;
6310         int width = drm_rect_width(dst);
6311         int height = drm_rect_height(dst);
6312         int x = dst->x1;
6313         int y = dst->y1;
6314         int hscale, vscale;
6315         unsigned long irqflags;
6316         int id;
6317         u32 ps_ctrl;
6318
6319         if (!crtc_state->pch_pfit.enabled)
6320                 return;
6321
6322         if (drm_WARN_ON(&dev_priv->drm,
6323                         crtc_state->scaler_state.scaler_id < 0))
6324                 return;
6325
6326         hscale = drm_rect_calc_hscale(&src, dst, 0, INT_MAX);
6327         vscale = drm_rect_calc_vscale(&src, dst, 0, INT_MAX);
6328
6329         uv_rgb_hphase = skl_scaler_calc_phase(1, hscale, false);
6330         uv_rgb_vphase = skl_scaler_calc_phase(1, vscale, false);
6331
6332         id = scaler_state->scaler_id;
6333
6334         ps_ctrl = skl_scaler_get_filter_select(crtc_state->hw.scaling_filter, 0);
6335         ps_ctrl |=  PS_SCALER_EN | scaler_state->scalers[id].mode;
6336
6337         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6338
6339         skl_scaler_setup_filter(dev_priv, pipe, id, 0,
6340                                 crtc_state->hw.scaling_filter);
6341
6342         intel_de_write_fw(dev_priv, SKL_PS_CTRL(pipe, id), ps_ctrl);
6343
6344         intel_de_write_fw(dev_priv, SKL_PS_VPHASE(pipe, id),
6345                           PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
6346         intel_de_write_fw(dev_priv, SKL_PS_HPHASE(pipe, id),
6347                           PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
6348         intel_de_write_fw(dev_priv, SKL_PS_WIN_POS(pipe, id),
6349                           x << 16 | y);
6350         intel_de_write_fw(dev_priv, SKL_PS_WIN_SZ(pipe, id),
6351                           width << 16 | height);
6352
6353         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6354 }
6355
6356 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
6357 {
6358         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6359         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6360         const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
6361         enum pipe pipe = crtc->pipe;
6362         int width = drm_rect_width(dst);
6363         int height = drm_rect_height(dst);
6364         int x = dst->x1;
6365         int y = dst->y1;
6366
6367         if (!crtc_state->pch_pfit.enabled)
6368                 return;
6369
6370         /* Force use of hard-coded filter coefficients
6371          * as some pre-programmed values are broken,
6372          * e.g. x201.
6373          */
6374         if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
6375                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6376                                PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
6377         else
6378                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
6379                                PF_FILTER_MED_3x3);
6380         intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
6381         intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
6382 }
6383
6384 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
6385 {
6386         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6387         struct drm_device *dev = crtc->base.dev;
6388         struct drm_i915_private *dev_priv = to_i915(dev);
6389
6390         if (!crtc_state->ips_enabled)
6391                 return;
6392
6393         /*
6394          * We can only enable IPS after we enable a plane and wait for a vblank
6395          * This function is called from post_plane_update, which is run after
6396          * a vblank wait.
6397          */
6398         drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
6399
6400         if (IS_BROADWELL(dev_priv)) {
6401                 drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
6402                                                          IPS_ENABLE | IPS_PCODE_CONTROL));
6403                 /* Quoting Art Runyan: "its not safe to expect any particular
6404                  * value in IPS_CTL bit 31 after enabling IPS through the
6405                  * mailbox." Moreover, the mailbox may return a bogus state,
6406                  * so we need to just enable it and continue on.
6407                  */
6408         } else {
6409                 intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
6410                 /* The bit only becomes 1 in the next vblank, so this wait here
6411                  * is essentially intel_wait_for_vblank. If we don't have this
6412                  * and don't wait for vblanks until the end of crtc_enable, then
6413                  * the HW state readout code will complain that the expected
6414                  * IPS_CTL value is not the one we read. */
6415                 if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
6416                         drm_err(&dev_priv->drm,
6417                                 "Timed out waiting for IPS enable\n");
6418         }
6419 }
6420
6421 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
6422 {
6423         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6424         struct drm_device *dev = crtc->base.dev;
6425         struct drm_i915_private *dev_priv = to_i915(dev);
6426
6427         if (!crtc_state->ips_enabled)
6428                 return;
6429
6430         if (IS_BROADWELL(dev_priv)) {
6431                 drm_WARN_ON(dev,
6432                             sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
6433                 /*
6434                  * Wait for PCODE to finish disabling IPS. The BSpec specified
6435                  * 42ms timeout value leads to occasional timeouts so use 100ms
6436                  * instead.
6437                  */
6438                 if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
6439                         drm_err(&dev_priv->drm,
6440                                 "Timed out waiting for IPS disable\n");
6441         } else {
6442                 intel_de_write(dev_priv, IPS_CTL, 0);
6443                 intel_de_posting_read(dev_priv, IPS_CTL);
6444         }
6445
6446         /* We need to wait for a vblank before we can disable the plane. */
6447         intel_wait_for_vblank(dev_priv, crtc->pipe);
6448 }
6449
6450 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
6451 {
6452         if (intel_crtc->overlay)
6453                 (void) intel_overlay_switch_off(intel_crtc->overlay);
6454
6455         /* Let userspace switch the overlay on again. In most cases userspace
6456          * has to recompute where to put it anyway.
6457          */
6458 }
6459
6460 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
6461                                        const struct intel_crtc_state *new_crtc_state)
6462 {
6463         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6464         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6465
6466         if (!old_crtc_state->ips_enabled)
6467                 return false;
6468
6469         if (needs_modeset(new_crtc_state))
6470                 return true;
6471
6472         /*
6473          * Workaround : Do not read or write the pipe palette/gamma data while
6474          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6475          *
6476          * Disable IPS before we program the LUT.
6477          */
6478         if (IS_HASWELL(dev_priv) &&
6479             (new_crtc_state->uapi.color_mgmt_changed ||
6480              new_crtc_state->update_pipe) &&
6481             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6482                 return true;
6483
6484         return !new_crtc_state->ips_enabled;
6485 }
6486
6487 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
6488                                        const struct intel_crtc_state *new_crtc_state)
6489 {
6490         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6491         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6492
6493         if (!new_crtc_state->ips_enabled)
6494                 return false;
6495
6496         if (needs_modeset(new_crtc_state))
6497                 return true;
6498
6499         /*
6500          * Workaround : Do not read or write the pipe palette/gamma data while
6501          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6502          *
6503          * Re-enable IPS after the LUT has been programmed.
6504          */
6505         if (IS_HASWELL(dev_priv) &&
6506             (new_crtc_state->uapi.color_mgmt_changed ||
6507              new_crtc_state->update_pipe) &&
6508             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
6509                 return true;
6510
6511         /*
6512          * We can't read out IPS on broadwell, assume the worst and
6513          * forcibly enable IPS on the first fastset.
6514          */
6515         if (new_crtc_state->update_pipe && old_crtc_state->inherited)
6516                 return true;
6517
6518         return !old_crtc_state->ips_enabled;
6519 }
6520
6521 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
6522 {
6523         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6524
6525         if (!crtc_state->nv12_planes)
6526                 return false;
6527
6528         /* WA Display #0827: Gen9:all */
6529         if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
6530                 return true;
6531
6532         return false;
6533 }
6534
6535 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
6536 {
6537         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
6538
6539         /* Wa_2006604312:icl,ehl */
6540         if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
6541                 return true;
6542
6543         return false;
6544 }
6545
6546 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
6547                             const struct intel_crtc_state *new_crtc_state)
6548 {
6549         return (!old_crtc_state->active_planes || needs_modeset(new_crtc_state)) &&
6550                 new_crtc_state->active_planes;
6551 }
6552
6553 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
6554                              const struct intel_crtc_state *new_crtc_state)
6555 {
6556         return old_crtc_state->active_planes &&
6557                 (!new_crtc_state->active_planes || needs_modeset(new_crtc_state));
6558 }
6559
6560 static void intel_post_plane_update(struct intel_atomic_state *state,
6561                                     struct intel_crtc *crtc)
6562 {
6563         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6564         const struct intel_crtc_state *old_crtc_state =
6565                 intel_atomic_get_old_crtc_state(state, crtc);
6566         const struct intel_crtc_state *new_crtc_state =
6567                 intel_atomic_get_new_crtc_state(state, crtc);
6568         enum pipe pipe = crtc->pipe;
6569
6570         intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
6571
6572         if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
6573                 intel_update_watermarks(crtc);
6574
6575         if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
6576                 hsw_enable_ips(new_crtc_state);
6577
6578         intel_fbc_post_update(state, crtc);
6579
6580         if (needs_nv12_wa(old_crtc_state) &&
6581             !needs_nv12_wa(new_crtc_state))
6582                 skl_wa_827(dev_priv, pipe, false);
6583
6584         if (needs_scalerclk_wa(old_crtc_state) &&
6585             !needs_scalerclk_wa(new_crtc_state))
6586                 icl_wa_scalerclkgating(dev_priv, pipe, false);
6587 }
6588
6589 static void skl_disable_async_flip_wa(struct intel_atomic_state *state,
6590                                       struct intel_crtc *crtc,
6591                                       const struct intel_crtc_state *new_crtc_state)
6592 {
6593         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6594         struct intel_plane *plane;
6595         struct intel_plane_state *new_plane_state;
6596         int i;
6597
6598         for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
6599                 u32 update_mask = new_crtc_state->update_planes;
6600                 u32 plane_ctl, surf_addr;
6601                 enum plane_id plane_id;
6602                 unsigned long irqflags;
6603                 enum pipe pipe;
6604
6605                 if (crtc->pipe != plane->pipe ||
6606                     !(update_mask & BIT(plane->id)))
6607                         continue;
6608
6609                 plane_id = plane->id;
6610                 pipe = plane->pipe;
6611
6612                 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
6613                 plane_ctl = intel_de_read_fw(dev_priv, PLANE_CTL(pipe, plane_id));
6614                 surf_addr = intel_de_read_fw(dev_priv, PLANE_SURF(pipe, plane_id));
6615
6616                 plane_ctl &= ~PLANE_CTL_ASYNC_FLIP;
6617
6618                 intel_de_write_fw(dev_priv, PLANE_CTL(pipe, plane_id), plane_ctl);
6619                 intel_de_write_fw(dev_priv, PLANE_SURF(pipe, plane_id), surf_addr);
6620                 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
6621         }
6622
6623         intel_wait_for_vblank(dev_priv, crtc->pipe);
6624 }
6625
6626 static void intel_pre_plane_update(struct intel_atomic_state *state,
6627                                    struct intel_crtc *crtc)
6628 {
6629         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6630         const struct intel_crtc_state *old_crtc_state =
6631                 intel_atomic_get_old_crtc_state(state, crtc);
6632         const struct intel_crtc_state *new_crtc_state =
6633                 intel_atomic_get_new_crtc_state(state, crtc);
6634         enum pipe pipe = crtc->pipe;
6635
6636         if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
6637                 hsw_disable_ips(old_crtc_state);
6638
6639         if (intel_fbc_pre_update(state, crtc))
6640                 intel_wait_for_vblank(dev_priv, pipe);
6641
6642         /* Display WA 827 */
6643         if (!needs_nv12_wa(old_crtc_state) &&
6644             needs_nv12_wa(new_crtc_state))
6645                 skl_wa_827(dev_priv, pipe, true);
6646
6647         /* Wa_2006604312:icl,ehl */
6648         if (!needs_scalerclk_wa(old_crtc_state) &&
6649             needs_scalerclk_wa(new_crtc_state))
6650                 icl_wa_scalerclkgating(dev_priv, pipe, true);
6651
6652         /*
6653          * Vblank time updates from the shadow to live plane control register
6654          * are blocked if the memory self-refresh mode is active at that
6655          * moment. So to make sure the plane gets truly disabled, disable
6656          * first the self-refresh mode. The self-refresh enable bit in turn
6657          * will be checked/applied by the HW only at the next frame start
6658          * event which is after the vblank start event, so we need to have a
6659          * wait-for-vblank between disabling the plane and the pipe.
6660          */
6661         if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
6662             new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
6663                 intel_wait_for_vblank(dev_priv, pipe);
6664
6665         /*
6666          * IVB workaround: must disable low power watermarks for at least
6667          * one frame before enabling scaling.  LP watermarks can be re-enabled
6668          * when scaling is disabled.
6669          *
6670          * WaCxSRDisabledForSpriteScaling:ivb
6671          */
6672         if (old_crtc_state->hw.active &&
6673             new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
6674                 intel_wait_for_vblank(dev_priv, pipe);
6675
6676         /*
6677          * If we're doing a modeset we don't need to do any
6678          * pre-vblank watermark programming here.
6679          */
6680         if (!needs_modeset(new_crtc_state)) {
6681                 /*
6682                  * For platforms that support atomic watermarks, program the
6683                  * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
6684                  * will be the intermediate values that are safe for both pre- and
6685                  * post- vblank; when vblank happens, the 'active' values will be set
6686                  * to the final 'target' values and we'll do this again to get the
6687                  * optimal watermarks.  For gen9+ platforms, the values we program here
6688                  * will be the final target values which will get automatically latched
6689                  * at vblank time; no further programming will be necessary.
6690                  *
6691                  * If a platform hasn't been transitioned to atomic watermarks yet,
6692                  * we'll continue to update watermarks the old way, if flags tell
6693                  * us to.
6694                  */
6695                 if (dev_priv->display.initial_watermarks)
6696                         dev_priv->display.initial_watermarks(state, crtc);
6697                 else if (new_crtc_state->update_wm_pre)
6698                         intel_update_watermarks(crtc);
6699         }
6700
6701         /*
6702          * Gen2 reports pipe underruns whenever all planes are disabled.
6703          * So disable underrun reporting before all the planes get disabled.
6704          *
6705          * We do this after .initial_watermarks() so that we have a
6706          * chance of catching underruns with the intermediate watermarks
6707          * vs. the old plane configuration.
6708          */
6709         if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
6710                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6711
6712         /*
6713          * WA for platforms where async address update enable bit
6714          * is double buffered and only latched at start of vblank.
6715          */
6716         if (old_crtc_state->uapi.async_flip &&
6717             !new_crtc_state->uapi.async_flip &&
6718             IS_GEN_RANGE(dev_priv, 9, 10))
6719                 skl_disable_async_flip_wa(state, crtc, new_crtc_state);
6720 }
6721
6722 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
6723                                       struct intel_crtc *crtc)
6724 {
6725         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6726         const struct intel_crtc_state *new_crtc_state =
6727                 intel_atomic_get_new_crtc_state(state, crtc);
6728         unsigned int update_mask = new_crtc_state->update_planes;
6729         const struct intel_plane_state *old_plane_state;
6730         struct intel_plane *plane;
6731         unsigned fb_bits = 0;
6732         int i;
6733
6734         intel_crtc_dpms_overlay_disable(crtc);
6735
6736         for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
6737                 if (crtc->pipe != plane->pipe ||
6738                     !(update_mask & BIT(plane->id)))
6739                         continue;
6740
6741                 intel_disable_plane(plane, new_crtc_state);
6742
6743                 if (old_plane_state->uapi.visible)
6744                         fb_bits |= plane->frontbuffer_bit;
6745         }
6746
6747         intel_frontbuffer_flip(dev_priv, fb_bits);
6748 }
6749
6750 /*
6751  * intel_connector_primary_encoder - get the primary encoder for a connector
6752  * @connector: connector for which to return the encoder
6753  *
6754  * Returns the primary encoder for a connector. There is a 1:1 mapping from
6755  * all connectors to their encoder, except for DP-MST connectors which have
6756  * both a virtual and a primary encoder. These DP-MST primary encoders can be
6757  * pointed to by as many DP-MST connectors as there are pipes.
6758  */
6759 static struct intel_encoder *
6760 intel_connector_primary_encoder(struct intel_connector *connector)
6761 {
6762         struct intel_encoder *encoder;
6763
6764         if (connector->mst_port)
6765                 return &dp_to_dig_port(connector->mst_port)->base;
6766
6767         encoder = intel_attached_encoder(connector);
6768         drm_WARN_ON(connector->base.dev, !encoder);
6769
6770         return encoder;
6771 }
6772
6773 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
6774 {
6775         struct drm_connector_state *new_conn_state;
6776         struct drm_connector *connector;
6777         int i;
6778
6779         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6780                                         i) {
6781                 struct intel_connector *intel_connector;
6782                 struct intel_encoder *encoder;
6783                 struct intel_crtc *crtc;
6784
6785                 if (!intel_connector_needs_modeset(state, connector))
6786                         continue;
6787
6788                 intel_connector = to_intel_connector(connector);
6789                 encoder = intel_connector_primary_encoder(intel_connector);
6790                 if (!encoder->update_prepare)
6791                         continue;
6792
6793                 crtc = new_conn_state->crtc ?
6794                         to_intel_crtc(new_conn_state->crtc) : NULL;
6795                 encoder->update_prepare(state, encoder, crtc);
6796         }
6797 }
6798
6799 static void intel_encoders_update_complete(struct intel_atomic_state *state)
6800 {
6801         struct drm_connector_state *new_conn_state;
6802         struct drm_connector *connector;
6803         int i;
6804
6805         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
6806                                         i) {
6807                 struct intel_connector *intel_connector;
6808                 struct intel_encoder *encoder;
6809                 struct intel_crtc *crtc;
6810
6811                 if (!intel_connector_needs_modeset(state, connector))
6812                         continue;
6813
6814                 intel_connector = to_intel_connector(connector);
6815                 encoder = intel_connector_primary_encoder(intel_connector);
6816                 if (!encoder->update_complete)
6817                         continue;
6818
6819                 crtc = new_conn_state->crtc ?
6820                         to_intel_crtc(new_conn_state->crtc) : NULL;
6821                 encoder->update_complete(state, encoder, crtc);
6822         }
6823 }
6824
6825 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
6826                                           struct intel_crtc *crtc)
6827 {
6828         const struct intel_crtc_state *crtc_state =
6829                 intel_atomic_get_new_crtc_state(state, crtc);
6830         const struct drm_connector_state *conn_state;
6831         struct drm_connector *conn;
6832         int i;
6833
6834         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6835                 struct intel_encoder *encoder =
6836                         to_intel_encoder(conn_state->best_encoder);
6837
6838                 if (conn_state->crtc != &crtc->base)
6839                         continue;
6840
6841                 if (encoder->pre_pll_enable)
6842                         encoder->pre_pll_enable(state, encoder,
6843                                                 crtc_state, conn_state);
6844         }
6845 }
6846
6847 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
6848                                       struct intel_crtc *crtc)
6849 {
6850         const struct intel_crtc_state *crtc_state =
6851                 intel_atomic_get_new_crtc_state(state, crtc);
6852         const struct drm_connector_state *conn_state;
6853         struct drm_connector *conn;
6854         int i;
6855
6856         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6857                 struct intel_encoder *encoder =
6858                         to_intel_encoder(conn_state->best_encoder);
6859
6860                 if (conn_state->crtc != &crtc->base)
6861                         continue;
6862
6863                 if (encoder->pre_enable)
6864                         encoder->pre_enable(state, encoder,
6865                                             crtc_state, conn_state);
6866         }
6867 }
6868
6869 static void intel_encoders_enable(struct intel_atomic_state *state,
6870                                   struct intel_crtc *crtc)
6871 {
6872         const struct intel_crtc_state *crtc_state =
6873                 intel_atomic_get_new_crtc_state(state, crtc);
6874         const struct drm_connector_state *conn_state;
6875         struct drm_connector *conn;
6876         int i;
6877
6878         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6879                 struct intel_encoder *encoder =
6880                         to_intel_encoder(conn_state->best_encoder);
6881
6882                 if (conn_state->crtc != &crtc->base)
6883                         continue;
6884
6885                 if (encoder->enable)
6886                         encoder->enable(state, encoder,
6887                                         crtc_state, conn_state);
6888                 intel_opregion_notify_encoder(encoder, true);
6889         }
6890 }
6891
6892 static void intel_encoders_disable(struct intel_atomic_state *state,
6893                                    struct intel_crtc *crtc)
6894 {
6895         const struct intel_crtc_state *old_crtc_state =
6896                 intel_atomic_get_old_crtc_state(state, crtc);
6897         const struct drm_connector_state *old_conn_state;
6898         struct drm_connector *conn;
6899         int i;
6900
6901         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6902                 struct intel_encoder *encoder =
6903                         to_intel_encoder(old_conn_state->best_encoder);
6904
6905                 if (old_conn_state->crtc != &crtc->base)
6906                         continue;
6907
6908                 intel_opregion_notify_encoder(encoder, false);
6909                 if (encoder->disable)
6910                         encoder->disable(state, encoder,
6911                                          old_crtc_state, old_conn_state);
6912         }
6913 }
6914
6915 static void intel_encoders_post_disable(struct intel_atomic_state *state,
6916                                         struct intel_crtc *crtc)
6917 {
6918         const struct intel_crtc_state *old_crtc_state =
6919                 intel_atomic_get_old_crtc_state(state, crtc);
6920         const struct drm_connector_state *old_conn_state;
6921         struct drm_connector *conn;
6922         int i;
6923
6924         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6925                 struct intel_encoder *encoder =
6926                         to_intel_encoder(old_conn_state->best_encoder);
6927
6928                 if (old_conn_state->crtc != &crtc->base)
6929                         continue;
6930
6931                 if (encoder->post_disable)
6932                         encoder->post_disable(state, encoder,
6933                                               old_crtc_state, old_conn_state);
6934         }
6935 }
6936
6937 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
6938                                             struct intel_crtc *crtc)
6939 {
6940         const struct intel_crtc_state *old_crtc_state =
6941                 intel_atomic_get_old_crtc_state(state, crtc);
6942         const struct drm_connector_state *old_conn_state;
6943         struct drm_connector *conn;
6944         int i;
6945
6946         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
6947                 struct intel_encoder *encoder =
6948                         to_intel_encoder(old_conn_state->best_encoder);
6949
6950                 if (old_conn_state->crtc != &crtc->base)
6951                         continue;
6952
6953                 if (encoder->post_pll_disable)
6954                         encoder->post_pll_disable(state, encoder,
6955                                                   old_crtc_state, old_conn_state);
6956         }
6957 }
6958
6959 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
6960                                        struct intel_crtc *crtc)
6961 {
6962         const struct intel_crtc_state *crtc_state =
6963                 intel_atomic_get_new_crtc_state(state, crtc);
6964         const struct drm_connector_state *conn_state;
6965         struct drm_connector *conn;
6966         int i;
6967
6968         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
6969                 struct intel_encoder *encoder =
6970                         to_intel_encoder(conn_state->best_encoder);
6971
6972                 if (conn_state->crtc != &crtc->base)
6973                         continue;
6974
6975                 if (encoder->update_pipe)
6976                         encoder->update_pipe(state, encoder,
6977                                              crtc_state, conn_state);
6978         }
6979 }
6980
6981 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
6982 {
6983         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6984         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6985
6986         plane->disable_plane(plane, crtc_state);
6987 }
6988
6989 static void ilk_crtc_enable(struct intel_atomic_state *state,
6990                             struct intel_crtc *crtc)
6991 {
6992         const struct intel_crtc_state *new_crtc_state =
6993                 intel_atomic_get_new_crtc_state(state, crtc);
6994         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6995         enum pipe pipe = crtc->pipe;
6996
6997         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
6998                 return;
6999
7000         /*
7001          * Sometimes spurious CPU pipe underruns happen during FDI
7002          * training, at least with VGA+HDMI cloning. Suppress them.
7003          *
7004          * On ILK we get an occasional spurious CPU pipe underruns
7005          * between eDP port A enable and vdd enable. Also PCH port
7006          * enable seems to result in the occasional CPU pipe underrun.
7007          *
7008          * Spurious PCH underruns also occur during PCH enabling.
7009          */
7010         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7011         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7012
7013         if (new_crtc_state->has_pch_encoder)
7014                 intel_prepare_shared_dpll(new_crtc_state);
7015
7016         if (intel_crtc_has_dp_encoder(new_crtc_state))
7017                 intel_dp_set_m_n(new_crtc_state, M1_N1);
7018
7019         intel_set_transcoder_timings(new_crtc_state);
7020         intel_set_pipe_src_size(new_crtc_state);
7021
7022         if (new_crtc_state->has_pch_encoder)
7023                 intel_cpu_transcoder_set_m_n(new_crtc_state,
7024                                              &new_crtc_state->fdi_m_n, NULL);
7025
7026         ilk_set_pipeconf(new_crtc_state);
7027
7028         crtc->active = true;
7029
7030         intel_encoders_pre_enable(state, crtc);
7031
7032         if (new_crtc_state->has_pch_encoder) {
7033                 /* Note: FDI PLL enabling _must_ be done before we enable the
7034                  * cpu pipes, hence this is separate from all the other fdi/pch
7035                  * enabling. */
7036                 ilk_fdi_pll_enable(new_crtc_state);
7037         } else {
7038                 assert_fdi_tx_disabled(dev_priv, pipe);
7039                 assert_fdi_rx_disabled(dev_priv, pipe);
7040         }
7041
7042         ilk_pfit_enable(new_crtc_state);
7043
7044         /*
7045          * On ILK+ LUT must be loaded before the pipe is running but with
7046          * clocks enabled
7047          */
7048         intel_color_load_luts(new_crtc_state);
7049         intel_color_commit(new_crtc_state);
7050         /* update DSPCNTR to configure gamma for pipe bottom color */
7051         intel_disable_primary_plane(new_crtc_state);
7052
7053         if (dev_priv->display.initial_watermarks)
7054                 dev_priv->display.initial_watermarks(state, crtc);
7055         intel_enable_pipe(new_crtc_state);
7056
7057         if (new_crtc_state->has_pch_encoder)
7058                 ilk_pch_enable(state, new_crtc_state);
7059
7060         intel_crtc_vblank_on(new_crtc_state);
7061
7062         intel_encoders_enable(state, crtc);
7063
7064         if (HAS_PCH_CPT(dev_priv))
7065                 cpt_verify_modeset(dev_priv, pipe);
7066
7067         /*
7068          * Must wait for vblank to avoid spurious PCH FIFO underruns.
7069          * And a second vblank wait is needed at least on ILK with
7070          * some interlaced HDMI modes. Let's do the double wait always
7071          * in case there are more corner cases we don't know about.
7072          */
7073         if (new_crtc_state->has_pch_encoder) {
7074                 intel_wait_for_vblank(dev_priv, pipe);
7075                 intel_wait_for_vblank(dev_priv, pipe);
7076         }
7077         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7078         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7079 }
7080
7081 /* IPS only exists on ULT machines and is tied to pipe A. */
7082 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
7083 {
7084         return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
7085 }
7086
7087 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
7088                                             enum pipe pipe, bool apply)
7089 {
7090         u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
7091         u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
7092
7093         if (apply)
7094                 val |= mask;
7095         else
7096                 val &= ~mask;
7097
7098         intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
7099 }
7100
7101 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
7102 {
7103         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7104         enum pipe pipe = crtc->pipe;
7105         u32 val;
7106
7107         val = MBUS_DBOX_A_CREDIT(2);
7108
7109         if (INTEL_GEN(dev_priv) >= 12) {
7110                 val |= MBUS_DBOX_BW_CREDIT(2);
7111                 val |= MBUS_DBOX_B_CREDIT(12);
7112         } else {
7113                 val |= MBUS_DBOX_BW_CREDIT(1);
7114                 val |= MBUS_DBOX_B_CREDIT(8);
7115         }
7116
7117         intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
7118 }
7119
7120 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
7121 {
7122         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7123         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7124
7125         intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
7126                        HSW_LINETIME(crtc_state->linetime) |
7127                        HSW_IPS_LINETIME(crtc_state->ips_linetime));
7128 }
7129
7130 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
7131 {
7132         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7133         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7134         i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
7135         u32 val;
7136
7137         val = intel_de_read(dev_priv, reg);
7138         val &= ~HSW_FRAME_START_DELAY_MASK;
7139         val |= HSW_FRAME_START_DELAY(0);
7140         intel_de_write(dev_priv, reg, val);
7141 }
7142
7143 static void hsw_crtc_enable(struct intel_atomic_state *state,
7144                             struct intel_crtc *crtc)
7145 {
7146         const struct intel_crtc_state *new_crtc_state =
7147                 intel_atomic_get_new_crtc_state(state, crtc);
7148         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7149         enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
7150         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
7151         bool psl_clkgate_wa;
7152
7153         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7154                 return;
7155
7156         intel_encoders_pre_pll_enable(state, crtc);
7157
7158         if (new_crtc_state->shared_dpll)
7159                 intel_enable_shared_dpll(new_crtc_state);
7160
7161         intel_encoders_pre_enable(state, crtc);
7162
7163         if (!transcoder_is_dsi(cpu_transcoder))
7164                 intel_set_transcoder_timings(new_crtc_state);
7165
7166         intel_set_pipe_src_size(new_crtc_state);
7167
7168         if (cpu_transcoder != TRANSCODER_EDP &&
7169             !transcoder_is_dsi(cpu_transcoder))
7170                 intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
7171                                new_crtc_state->pixel_multiplier - 1);
7172
7173         if (new_crtc_state->has_pch_encoder)
7174                 intel_cpu_transcoder_set_m_n(new_crtc_state,
7175                                              &new_crtc_state->fdi_m_n, NULL);
7176
7177         if (!transcoder_is_dsi(cpu_transcoder)) {
7178                 hsw_set_frame_start_delay(new_crtc_state);
7179                 hsw_set_pipeconf(new_crtc_state);
7180         }
7181
7182         if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7183                 bdw_set_pipemisc(new_crtc_state);
7184
7185         crtc->active = true;
7186
7187         /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
7188         psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
7189                 new_crtc_state->pch_pfit.enabled;
7190         if (psl_clkgate_wa)
7191                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
7192
7193         if (INTEL_GEN(dev_priv) >= 9)
7194                 skl_pfit_enable(new_crtc_state);
7195         else
7196                 ilk_pfit_enable(new_crtc_state);
7197
7198         /*
7199          * On ILK+ LUT must be loaded before the pipe is running but with
7200          * clocks enabled
7201          */
7202         intel_color_load_luts(new_crtc_state);
7203         intel_color_commit(new_crtc_state);
7204         /* update DSPCNTR to configure gamma/csc for pipe bottom color */
7205         if (INTEL_GEN(dev_priv) < 9)
7206                 intel_disable_primary_plane(new_crtc_state);
7207
7208         hsw_set_linetime_wm(new_crtc_state);
7209
7210         if (INTEL_GEN(dev_priv) >= 11)
7211                 icl_set_pipe_chicken(crtc);
7212
7213         if (dev_priv->display.initial_watermarks)
7214                 dev_priv->display.initial_watermarks(state, crtc);
7215
7216         if (INTEL_GEN(dev_priv) >= 11)
7217                 icl_pipe_mbus_enable(crtc);
7218
7219         intel_encoders_enable(state, crtc);
7220
7221         if (psl_clkgate_wa) {
7222                 intel_wait_for_vblank(dev_priv, pipe);
7223                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
7224         }
7225
7226         /* If we change the relative order between pipe/planes enabling, we need
7227          * to change the workaround. */
7228         hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
7229         if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
7230                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7231                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
7232         }
7233 }
7234
7235 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7236 {
7237         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7238         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7239         enum pipe pipe = crtc->pipe;
7240
7241         /* To avoid upsetting the power well on haswell only disable the pfit if
7242          * it's in use. The hw state code will make sure we get this right. */
7243         if (!old_crtc_state->pch_pfit.enabled)
7244                 return;
7245
7246         intel_de_write(dev_priv, PF_CTL(pipe), 0);
7247         intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
7248         intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
7249 }
7250
7251 static void ilk_crtc_disable(struct intel_atomic_state *state,
7252                              struct intel_crtc *crtc)
7253 {
7254         const struct intel_crtc_state *old_crtc_state =
7255                 intel_atomic_get_old_crtc_state(state, crtc);
7256         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7257         enum pipe pipe = crtc->pipe;
7258
7259         /*
7260          * Sometimes spurious CPU pipe underruns happen when the
7261          * pipe is already disabled, but FDI RX/TX is still enabled.
7262          * Happens at least with VGA+HDMI cloning. Suppress them.
7263          */
7264         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7265         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
7266
7267         intel_encoders_disable(state, crtc);
7268
7269         intel_crtc_vblank_off(old_crtc_state);
7270
7271         intel_disable_pipe(old_crtc_state);
7272
7273         ilk_pfit_disable(old_crtc_state);
7274
7275         if (old_crtc_state->has_pch_encoder)
7276                 ilk_fdi_disable(crtc);
7277
7278         intel_encoders_post_disable(state, crtc);
7279
7280         if (old_crtc_state->has_pch_encoder) {
7281                 ilk_disable_pch_transcoder(dev_priv, pipe);
7282
7283                 if (HAS_PCH_CPT(dev_priv)) {
7284                         i915_reg_t reg;
7285                         u32 temp;
7286
7287                         /* disable TRANS_DP_CTL */
7288                         reg = TRANS_DP_CTL(pipe);
7289                         temp = intel_de_read(dev_priv, reg);
7290                         temp &= ~(TRANS_DP_OUTPUT_ENABLE |
7291                                   TRANS_DP_PORT_SEL_MASK);
7292                         temp |= TRANS_DP_PORT_SEL_NONE;
7293                         intel_de_write(dev_priv, reg, temp);
7294
7295                         /* disable DPLL_SEL */
7296                         temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
7297                         temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
7298                         intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
7299                 }
7300
7301                 ilk_fdi_pll_disable(crtc);
7302         }
7303
7304         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7305         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
7306 }
7307
7308 static void hsw_crtc_disable(struct intel_atomic_state *state,
7309                              struct intel_crtc *crtc)
7310 {
7311         /*
7312          * FIXME collapse everything to one hook.
7313          * Need care with mst->ddi interactions.
7314          */
7315         intel_encoders_disable(state, crtc);
7316         intel_encoders_post_disable(state, crtc);
7317 }
7318
7319 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
7320 {
7321         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7322         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7323
7324         if (!crtc_state->gmch_pfit.control)
7325                 return;
7326
7327         /*
7328          * The panel fitter should only be adjusted whilst the pipe is disabled,
7329          * according to register description and PRM.
7330          */
7331         drm_WARN_ON(&dev_priv->drm,
7332                     intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
7333         assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
7334
7335         intel_de_write(dev_priv, PFIT_PGM_RATIOS,
7336                        crtc_state->gmch_pfit.pgm_ratios);
7337         intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
7338
7339         /* Border color in case we don't scale up to the full screen. Black by
7340          * default, change to something else for debugging. */
7341         intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
7342 }
7343
7344 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
7345 {
7346         if (phy == PHY_NONE)
7347                 return false;
7348         else if (IS_ROCKETLAKE(dev_priv))
7349                 return phy <= PHY_D;
7350         else if (IS_JSL_EHL(dev_priv))
7351                 return phy <= PHY_C;
7352         else if (INTEL_GEN(dev_priv) >= 11)
7353                 return phy <= PHY_B;
7354         else
7355                 return false;
7356 }
7357
7358 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
7359 {
7360         if (IS_ROCKETLAKE(dev_priv))
7361                 return false;
7362         else if (INTEL_GEN(dev_priv) >= 12)
7363                 return phy >= PHY_D && phy <= PHY_I;
7364         else if (INTEL_GEN(dev_priv) >= 11 && !IS_JSL_EHL(dev_priv))
7365                 return phy >= PHY_C && phy <= PHY_F;
7366         else
7367                 return false;
7368 }
7369
7370 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
7371 {
7372         if (IS_ROCKETLAKE(i915) && port >= PORT_TC1)
7373                 return PHY_C + port - PORT_TC1;
7374         else if (IS_JSL_EHL(i915) && port == PORT_D)
7375                 return PHY_A;
7376
7377         return PHY_A + port - PORT_A;
7378 }
7379
7380 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
7381 {
7382         if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
7383                 return TC_PORT_NONE;
7384
7385         if (INTEL_GEN(dev_priv) >= 12)
7386                 return TC_PORT_1 + port - PORT_TC1;
7387         else
7388                 return TC_PORT_1 + port - PORT_C;
7389 }
7390
7391 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
7392 {
7393         switch (port) {
7394         case PORT_A:
7395                 return POWER_DOMAIN_PORT_DDI_A_LANES;
7396         case PORT_B:
7397                 return POWER_DOMAIN_PORT_DDI_B_LANES;
7398         case PORT_C:
7399                 return POWER_DOMAIN_PORT_DDI_C_LANES;
7400         case PORT_D:
7401                 return POWER_DOMAIN_PORT_DDI_D_LANES;
7402         case PORT_E:
7403                 return POWER_DOMAIN_PORT_DDI_E_LANES;
7404         case PORT_F:
7405                 return POWER_DOMAIN_PORT_DDI_F_LANES;
7406         case PORT_G:
7407                 return POWER_DOMAIN_PORT_DDI_G_LANES;
7408         case PORT_H:
7409                 return POWER_DOMAIN_PORT_DDI_H_LANES;
7410         case PORT_I:
7411                 return POWER_DOMAIN_PORT_DDI_I_LANES;
7412         default:
7413                 MISSING_CASE(port);
7414                 return POWER_DOMAIN_PORT_OTHER;
7415         }
7416 }
7417
7418 enum intel_display_power_domain
7419 intel_aux_power_domain(struct intel_digital_port *dig_port)
7420 {
7421         struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
7422         enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
7423
7424         if (intel_phy_is_tc(dev_priv, phy) &&
7425             dig_port->tc_mode == TC_PORT_TBT_ALT) {
7426                 switch (dig_port->aux_ch) {
7427                 case AUX_CH_C:
7428                         return POWER_DOMAIN_AUX_C_TBT;
7429                 case AUX_CH_D:
7430                         return POWER_DOMAIN_AUX_D_TBT;
7431                 case AUX_CH_E:
7432                         return POWER_DOMAIN_AUX_E_TBT;
7433                 case AUX_CH_F:
7434                         return POWER_DOMAIN_AUX_F_TBT;
7435                 case AUX_CH_G:
7436                         return POWER_DOMAIN_AUX_G_TBT;
7437                 case AUX_CH_H:
7438                         return POWER_DOMAIN_AUX_H_TBT;
7439                 case AUX_CH_I:
7440                         return POWER_DOMAIN_AUX_I_TBT;
7441                 default:
7442                         MISSING_CASE(dig_port->aux_ch);
7443                         return POWER_DOMAIN_AUX_C_TBT;
7444                 }
7445         }
7446
7447         return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
7448 }
7449
7450 /*
7451  * Converts aux_ch to power_domain without caring about TBT ports for that use
7452  * intel_aux_power_domain()
7453  */
7454 enum intel_display_power_domain
7455 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
7456 {
7457         switch (aux_ch) {
7458         case AUX_CH_A:
7459                 return POWER_DOMAIN_AUX_A;
7460         case AUX_CH_B:
7461                 return POWER_DOMAIN_AUX_B;
7462         case AUX_CH_C:
7463                 return POWER_DOMAIN_AUX_C;
7464         case AUX_CH_D:
7465                 return POWER_DOMAIN_AUX_D;
7466         case AUX_CH_E:
7467                 return POWER_DOMAIN_AUX_E;
7468         case AUX_CH_F:
7469                 return POWER_DOMAIN_AUX_F;
7470         case AUX_CH_G:
7471                 return POWER_DOMAIN_AUX_G;
7472         case AUX_CH_H:
7473                 return POWER_DOMAIN_AUX_H;
7474         case AUX_CH_I:
7475                 return POWER_DOMAIN_AUX_I;
7476         default:
7477                 MISSING_CASE(aux_ch);
7478                 return POWER_DOMAIN_AUX_A;
7479         }
7480 }
7481
7482 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7483 {
7484         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7485         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7486         struct drm_encoder *encoder;
7487         enum pipe pipe = crtc->pipe;
7488         u64 mask;
7489         enum transcoder transcoder = crtc_state->cpu_transcoder;
7490
7491         if (!crtc_state->hw.active)
7492                 return 0;
7493
7494         mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
7495         mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
7496         if (crtc_state->pch_pfit.enabled ||
7497             crtc_state->pch_pfit.force_thru)
7498                 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
7499
7500         drm_for_each_encoder_mask(encoder, &dev_priv->drm,
7501                                   crtc_state->uapi.encoder_mask) {
7502                 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7503
7504                 mask |= BIT_ULL(intel_encoder->power_domain);
7505         }
7506
7507         if (HAS_DDI(dev_priv) && crtc_state->has_audio)
7508                 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
7509
7510         if (crtc_state->shared_dpll)
7511                 mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
7512
7513         return mask;
7514 }
7515
7516 static u64
7517 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
7518 {
7519         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7520         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7521         enum intel_display_power_domain domain;
7522         u64 domains, new_domains, old_domains;
7523
7524         old_domains = crtc->enabled_power_domains;
7525         crtc->enabled_power_domains = new_domains =
7526                 get_crtc_power_domains(crtc_state);
7527
7528         domains = new_domains & ~old_domains;
7529
7530         for_each_power_domain(domain, domains)
7531                 intel_display_power_get(dev_priv, domain);
7532
7533         return old_domains & ~new_domains;
7534 }
7535
7536 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
7537                                       u64 domains)
7538 {
7539         enum intel_display_power_domain domain;
7540
7541         for_each_power_domain(domain, domains)
7542                 intel_display_power_put_unchecked(dev_priv, domain);
7543 }
7544
7545 static void valleyview_crtc_enable(struct intel_atomic_state *state,
7546                                    struct intel_crtc *crtc)
7547 {
7548         const struct intel_crtc_state *new_crtc_state =
7549                 intel_atomic_get_new_crtc_state(state, crtc);
7550         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7551         enum pipe pipe = crtc->pipe;
7552
7553         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7554                 return;
7555
7556         if (intel_crtc_has_dp_encoder(new_crtc_state))
7557                 intel_dp_set_m_n(new_crtc_state, M1_N1);
7558
7559         intel_set_transcoder_timings(new_crtc_state);
7560         intel_set_pipe_src_size(new_crtc_state);
7561
7562         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
7563                 intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
7564                 intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
7565         }
7566
7567         i9xx_set_pipeconf(new_crtc_state);
7568
7569         crtc->active = true;
7570
7571         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7572
7573         intel_encoders_pre_pll_enable(state, crtc);
7574
7575         if (IS_CHERRYVIEW(dev_priv)) {
7576                 chv_prepare_pll(crtc, new_crtc_state);
7577                 chv_enable_pll(crtc, new_crtc_state);
7578         } else {
7579                 vlv_prepare_pll(crtc, new_crtc_state);
7580                 vlv_enable_pll(crtc, new_crtc_state);
7581         }
7582
7583         intel_encoders_pre_enable(state, crtc);
7584
7585         i9xx_pfit_enable(new_crtc_state);
7586
7587         intel_color_load_luts(new_crtc_state);
7588         intel_color_commit(new_crtc_state);
7589         /* update DSPCNTR to configure gamma for pipe bottom color */
7590         intel_disable_primary_plane(new_crtc_state);
7591
7592         dev_priv->display.initial_watermarks(state, crtc);
7593         intel_enable_pipe(new_crtc_state);
7594
7595         intel_crtc_vblank_on(new_crtc_state);
7596
7597         intel_encoders_enable(state, crtc);
7598 }
7599
7600 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
7601 {
7602         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7603         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7604
7605         intel_de_write(dev_priv, FP0(crtc->pipe),
7606                        crtc_state->dpll_hw_state.fp0);
7607         intel_de_write(dev_priv, FP1(crtc->pipe),
7608                        crtc_state->dpll_hw_state.fp1);
7609 }
7610
7611 static void i9xx_crtc_enable(struct intel_atomic_state *state,
7612                              struct intel_crtc *crtc)
7613 {
7614         const struct intel_crtc_state *new_crtc_state =
7615                 intel_atomic_get_new_crtc_state(state, crtc);
7616         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7617         enum pipe pipe = crtc->pipe;
7618
7619         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
7620                 return;
7621
7622         i9xx_set_pll_dividers(new_crtc_state);
7623
7624         if (intel_crtc_has_dp_encoder(new_crtc_state))
7625                 intel_dp_set_m_n(new_crtc_state, M1_N1);
7626
7627         intel_set_transcoder_timings(new_crtc_state);
7628         intel_set_pipe_src_size(new_crtc_state);
7629
7630         i9xx_set_pipeconf(new_crtc_state);
7631
7632         crtc->active = true;
7633
7634         if (!IS_GEN(dev_priv, 2))
7635                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
7636
7637         intel_encoders_pre_enable(state, crtc);
7638
7639         i9xx_enable_pll(crtc, new_crtc_state);
7640
7641         i9xx_pfit_enable(new_crtc_state);
7642
7643         intel_color_load_luts(new_crtc_state);
7644         intel_color_commit(new_crtc_state);
7645         /* update DSPCNTR to configure gamma for pipe bottom color */
7646         intel_disable_primary_plane(new_crtc_state);
7647
7648         if (dev_priv->display.initial_watermarks)
7649                 dev_priv->display.initial_watermarks(state, crtc);
7650         else
7651                 intel_update_watermarks(crtc);
7652         intel_enable_pipe(new_crtc_state);
7653
7654         intel_crtc_vblank_on(new_crtc_state);
7655
7656         intel_encoders_enable(state, crtc);
7657
7658         /* prevents spurious underruns */
7659         if (IS_GEN(dev_priv, 2))
7660                 intel_wait_for_vblank(dev_priv, pipe);
7661 }
7662
7663 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
7664 {
7665         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
7666         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7667
7668         if (!old_crtc_state->gmch_pfit.control)
7669                 return;
7670
7671         assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
7672
7673         drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
7674                     intel_de_read(dev_priv, PFIT_CONTROL));
7675         intel_de_write(dev_priv, PFIT_CONTROL, 0);
7676 }
7677
7678 static void i9xx_crtc_disable(struct intel_atomic_state *state,
7679                               struct intel_crtc *crtc)
7680 {
7681         struct intel_crtc_state *old_crtc_state =
7682                 intel_atomic_get_old_crtc_state(state, crtc);
7683         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7684         enum pipe pipe = crtc->pipe;
7685
7686         /*
7687          * On gen2 planes are double buffered but the pipe isn't, so we must
7688          * wait for planes to fully turn off before disabling the pipe.
7689          */
7690         if (IS_GEN(dev_priv, 2))
7691                 intel_wait_for_vblank(dev_priv, pipe);
7692
7693         intel_encoders_disable(state, crtc);
7694
7695         intel_crtc_vblank_off(old_crtc_state);
7696
7697         intel_disable_pipe(old_crtc_state);
7698
7699         i9xx_pfit_disable(old_crtc_state);
7700
7701         intel_encoders_post_disable(state, crtc);
7702
7703         if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
7704                 if (IS_CHERRYVIEW(dev_priv))
7705                         chv_disable_pll(dev_priv, pipe);
7706                 else if (IS_VALLEYVIEW(dev_priv))
7707                         vlv_disable_pll(dev_priv, pipe);
7708                 else
7709                         i9xx_disable_pll(old_crtc_state);
7710         }
7711
7712         intel_encoders_post_pll_disable(state, crtc);
7713
7714         if (!IS_GEN(dev_priv, 2))
7715                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
7716
7717         if (!dev_priv->display.initial_watermarks)
7718                 intel_update_watermarks(crtc);
7719
7720         /* clock the pipe down to 640x480@60 to potentially save power */
7721         if (IS_I830(dev_priv))
7722                 i830_enable_pipe(dev_priv, pipe);
7723 }
7724
7725 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
7726                                         struct drm_modeset_acquire_ctx *ctx)
7727 {
7728         struct intel_encoder *encoder;
7729         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7730         struct intel_bw_state *bw_state =
7731                 to_intel_bw_state(dev_priv->bw_obj.state);
7732         struct intel_cdclk_state *cdclk_state =
7733                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
7734         struct intel_dbuf_state *dbuf_state =
7735                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
7736         struct intel_crtc_state *crtc_state =
7737                 to_intel_crtc_state(crtc->base.state);
7738         enum intel_display_power_domain domain;
7739         struct intel_plane *plane;
7740         struct drm_atomic_state *state;
7741         struct intel_crtc_state *temp_crtc_state;
7742         enum pipe pipe = crtc->pipe;
7743         u64 domains;
7744         int ret;
7745
7746         if (!crtc_state->hw.active)
7747                 return;
7748
7749         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7750                 const struct intel_plane_state *plane_state =
7751                         to_intel_plane_state(plane->base.state);
7752
7753                 if (plane_state->uapi.visible)
7754                         intel_plane_disable_noatomic(crtc, plane);
7755         }
7756
7757         state = drm_atomic_state_alloc(&dev_priv->drm);
7758         if (!state) {
7759                 drm_dbg_kms(&dev_priv->drm,
7760                             "failed to disable [CRTC:%d:%s], out of memory",
7761                             crtc->base.base.id, crtc->base.name);
7762                 return;
7763         }
7764
7765         state->acquire_ctx = ctx;
7766
7767         /* Everything's already locked, -EDEADLK can't happen. */
7768         temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
7769         ret = drm_atomic_add_affected_connectors(state, &crtc->base);
7770
7771         drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
7772
7773         dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
7774
7775         drm_atomic_state_put(state);
7776
7777         drm_dbg_kms(&dev_priv->drm,
7778                     "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
7779                     crtc->base.base.id, crtc->base.name);
7780
7781         crtc->active = false;
7782         crtc->base.enabled = false;
7783
7784         drm_WARN_ON(&dev_priv->drm,
7785                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
7786         crtc_state->uapi.active = false;
7787         crtc_state->uapi.connector_mask = 0;
7788         crtc_state->uapi.encoder_mask = 0;
7789         intel_crtc_free_hw_state(crtc_state);
7790         memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
7791
7792         for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
7793                 encoder->base.crtc = NULL;
7794
7795         intel_fbc_disable(crtc);
7796         intel_update_watermarks(crtc);
7797         intel_disable_shared_dpll(crtc_state);
7798
7799         domains = crtc->enabled_power_domains;
7800         for_each_power_domain(domain, domains)
7801                 intel_display_power_put_unchecked(dev_priv, domain);
7802         crtc->enabled_power_domains = 0;
7803
7804         dev_priv->active_pipes &= ~BIT(pipe);
7805         cdclk_state->min_cdclk[pipe] = 0;
7806         cdclk_state->min_voltage_level[pipe] = 0;
7807         cdclk_state->active_pipes &= ~BIT(pipe);
7808
7809         dbuf_state->active_pipes &= ~BIT(pipe);
7810
7811         bw_state->data_rate[pipe] = 0;
7812         bw_state->num_active_planes[pipe] = 0;
7813 }
7814
7815 /*
7816  * turn all crtc's off, but do not adjust state
7817  * This has to be paired with a call to intel_modeset_setup_hw_state.
7818  */
7819 int intel_display_suspend(struct drm_device *dev)
7820 {
7821         struct drm_i915_private *dev_priv = to_i915(dev);
7822         struct drm_atomic_state *state;
7823         int ret;
7824
7825         state = drm_atomic_helper_suspend(dev);
7826         ret = PTR_ERR_OR_ZERO(state);
7827         if (ret)
7828                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
7829                         ret);
7830         else
7831                 dev_priv->modeset_restore_state = state;
7832         return ret;
7833 }
7834
7835 void intel_encoder_destroy(struct drm_encoder *encoder)
7836 {
7837         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
7838
7839         drm_encoder_cleanup(encoder);
7840         kfree(intel_encoder);
7841 }
7842
7843 /* Cross check the actual hw state with our own modeset state tracking (and it's
7844  * internal consistency). */
7845 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
7846                                          struct drm_connector_state *conn_state)
7847 {
7848         struct intel_connector *connector = to_intel_connector(conn_state->connector);
7849         struct drm_i915_private *i915 = to_i915(connector->base.dev);
7850
7851         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
7852                     connector->base.base.id, connector->base.name);
7853
7854         if (connector->get_hw_state(connector)) {
7855                 struct intel_encoder *encoder = intel_attached_encoder(connector);
7856
7857                 I915_STATE_WARN(!crtc_state,
7858                          "connector enabled without attached crtc\n");
7859
7860                 if (!crtc_state)
7861                         return;
7862
7863                 I915_STATE_WARN(!crtc_state->hw.active,
7864                                 "connector is active, but attached crtc isn't\n");
7865
7866                 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
7867                         return;
7868
7869                 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
7870                         "atomic encoder doesn't match attached encoder\n");
7871
7872                 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
7873                         "attached encoder crtc differs from connector crtc\n");
7874         } else {
7875                 I915_STATE_WARN(crtc_state && crtc_state->hw.active,
7876                                 "attached crtc is active, but connector isn't\n");
7877                 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
7878                         "best encoder set without crtc!\n");
7879         }
7880 }
7881
7882 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
7883 {
7884         if (crtc_state->hw.enable && crtc_state->has_pch_encoder)
7885                 return crtc_state->fdi_lanes;
7886
7887         return 0;
7888 }
7889
7890 static int ilk_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
7891                                struct intel_crtc_state *pipe_config)
7892 {
7893         struct drm_i915_private *dev_priv = to_i915(dev);
7894         struct drm_atomic_state *state = pipe_config->uapi.state;
7895         struct intel_crtc *other_crtc;
7896         struct intel_crtc_state *other_crtc_state;
7897
7898         drm_dbg_kms(&dev_priv->drm,
7899                     "checking fdi config on pipe %c, lanes %i\n",
7900                     pipe_name(pipe), pipe_config->fdi_lanes);
7901         if (pipe_config->fdi_lanes > 4) {
7902                 drm_dbg_kms(&dev_priv->drm,
7903                             "invalid fdi lane config on pipe %c: %i lanes\n",
7904                             pipe_name(pipe), pipe_config->fdi_lanes);
7905                 return -EINVAL;
7906         }
7907
7908         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
7909                 if (pipe_config->fdi_lanes > 2) {
7910                         drm_dbg_kms(&dev_priv->drm,
7911                                     "only 2 lanes on haswell, required: %i lanes\n",
7912                                     pipe_config->fdi_lanes);
7913                         return -EINVAL;
7914                 } else {
7915                         return 0;
7916                 }
7917         }
7918
7919         if (INTEL_NUM_PIPES(dev_priv) == 2)
7920                 return 0;
7921
7922         /* Ivybridge 3 pipe is really complicated */
7923         switch (pipe) {
7924         case PIPE_A:
7925                 return 0;
7926         case PIPE_B:
7927                 if (pipe_config->fdi_lanes <= 2)
7928                         return 0;
7929
7930                 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
7931                 other_crtc_state =
7932                         intel_atomic_get_crtc_state(state, other_crtc);
7933                 if (IS_ERR(other_crtc_state))
7934                         return PTR_ERR(other_crtc_state);
7935
7936                 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
7937                         drm_dbg_kms(&dev_priv->drm,
7938                                     "invalid shared fdi lane config on pipe %c: %i lanes\n",
7939                                     pipe_name(pipe), pipe_config->fdi_lanes);
7940                         return -EINVAL;
7941                 }
7942                 return 0;
7943         case PIPE_C:
7944                 if (pipe_config->fdi_lanes > 2) {
7945                         drm_dbg_kms(&dev_priv->drm,
7946                                     "only 2 lanes on pipe %c: required %i lanes\n",
7947                                     pipe_name(pipe), pipe_config->fdi_lanes);
7948                         return -EINVAL;
7949                 }
7950
7951                 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
7952                 other_crtc_state =
7953                         intel_atomic_get_crtc_state(state, other_crtc);
7954                 if (IS_ERR(other_crtc_state))
7955                         return PTR_ERR(other_crtc_state);
7956
7957                 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
7958                         drm_dbg_kms(&dev_priv->drm,
7959                                     "fdi link B uses too many lanes to enable link C\n");
7960                         return -EINVAL;
7961                 }
7962                 return 0;
7963         default:
7964                 BUG();
7965         }
7966 }
7967
7968 #define RETRY 1
7969 static int ilk_fdi_compute_config(struct intel_crtc *intel_crtc,
7970                                   struct intel_crtc_state *pipe_config)
7971 {
7972         struct drm_device *dev = intel_crtc->base.dev;
7973         struct drm_i915_private *i915 = to_i915(dev);
7974         const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
7975         int lane, link_bw, fdi_dotclock, ret;
7976         bool needs_recompute = false;
7977
7978 retry:
7979         /* FDI is a binary signal running at ~2.7GHz, encoding
7980          * each output octet as 10 bits. The actual frequency
7981          * is stored as a divider into a 100MHz clock, and the
7982          * mode pixel clock is stored in units of 1KHz.
7983          * Hence the bw of each lane in terms of the mode signal
7984          * is:
7985          */
7986         link_bw = intel_fdi_link_freq(i915, pipe_config);
7987
7988         fdi_dotclock = adjusted_mode->crtc_clock;
7989
7990         lane = ilk_get_lanes_required(fdi_dotclock, link_bw,
7991                                       pipe_config->pipe_bpp);
7992
7993         pipe_config->fdi_lanes = lane;
7994
7995         intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
7996                                link_bw, &pipe_config->fdi_m_n, false, false);
7997
7998         ret = ilk_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
7999         if (ret == -EDEADLK)
8000                 return ret;
8001
8002         if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
8003                 pipe_config->pipe_bpp -= 2*3;
8004                 drm_dbg_kms(&i915->drm,
8005                             "fdi link bw constraint, reducing pipe bpp to %i\n",
8006                             pipe_config->pipe_bpp);
8007                 needs_recompute = true;
8008                 pipe_config->bw_constrained = true;
8009
8010                 goto retry;
8011         }
8012
8013         if (needs_recompute)
8014                 return RETRY;
8015
8016         return ret;
8017 }
8018
8019 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
8020 {
8021         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8022         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8023
8024         /* IPS only exists on ULT machines and is tied to pipe A. */
8025         if (!hsw_crtc_supports_ips(crtc))
8026                 return false;
8027
8028         if (!dev_priv->params.enable_ips)
8029                 return false;
8030
8031         if (crtc_state->pipe_bpp > 24)
8032                 return false;
8033
8034         /*
8035          * We compare against max which means we must take
8036          * the increased cdclk requirement into account when
8037          * calculating the new cdclk.
8038          *
8039          * Should measure whether using a lower cdclk w/o IPS
8040          */
8041         if (IS_BROADWELL(dev_priv) &&
8042             crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
8043                 return false;
8044
8045         return true;
8046 }
8047
8048 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
8049 {
8050         struct drm_i915_private *dev_priv =
8051                 to_i915(crtc_state->uapi.crtc->dev);
8052         struct intel_atomic_state *state =
8053                 to_intel_atomic_state(crtc_state->uapi.state);
8054
8055         crtc_state->ips_enabled = false;
8056
8057         if (!hsw_crtc_state_ips_capable(crtc_state))
8058                 return 0;
8059
8060         /*
8061          * When IPS gets enabled, the pipe CRC changes. Since IPS gets
8062          * enabled and disabled dynamically based on package C states,
8063          * user space can't make reliable use of the CRCs, so let's just
8064          * completely disable it.
8065          */
8066         if (crtc_state->crc_enabled)
8067                 return 0;
8068
8069         /* IPS should be fine as long as at least one plane is enabled. */
8070         if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
8071                 return 0;
8072
8073         if (IS_BROADWELL(dev_priv)) {
8074                 const struct intel_cdclk_state *cdclk_state;
8075
8076                 cdclk_state = intel_atomic_get_cdclk_state(state);
8077                 if (IS_ERR(cdclk_state))
8078                         return PTR_ERR(cdclk_state);
8079
8080                 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
8081                 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
8082                         return 0;
8083         }
8084
8085         crtc_state->ips_enabled = true;
8086
8087         return 0;
8088 }
8089
8090 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
8091 {
8092         const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8093
8094         /* GDG double wide on either pipe, otherwise pipe A only */
8095         return INTEL_GEN(dev_priv) < 4 &&
8096                 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
8097 }
8098
8099 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
8100 {
8101         u32 pixel_rate = crtc_state->hw.adjusted_mode.crtc_clock;
8102         unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
8103
8104         /*
8105          * We only use IF-ID interlacing. If we ever use
8106          * PF-ID we'll need to adjust the pixel_rate here.
8107          */
8108
8109         if (!crtc_state->pch_pfit.enabled)
8110                 return pixel_rate;
8111
8112         pipe_w = crtc_state->pipe_src_w;
8113         pipe_h = crtc_state->pipe_src_h;
8114
8115         pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
8116         pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
8117
8118         if (pipe_w < pfit_w)
8119                 pipe_w = pfit_w;
8120         if (pipe_h < pfit_h)
8121                 pipe_h = pfit_h;
8122
8123         if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
8124                         !pfit_w || !pfit_h))
8125                 return pixel_rate;
8126
8127         return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
8128                        pfit_w * pfit_h);
8129 }
8130
8131 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
8132                                          const struct drm_display_mode *timings)
8133 {
8134         mode->hdisplay = timings->crtc_hdisplay;
8135         mode->htotal = timings->crtc_htotal;
8136         mode->hsync_start = timings->crtc_hsync_start;
8137         mode->hsync_end = timings->crtc_hsync_end;
8138
8139         mode->vdisplay = timings->crtc_vdisplay;
8140         mode->vtotal = timings->crtc_vtotal;
8141         mode->vsync_start = timings->crtc_vsync_start;
8142         mode->vsync_end = timings->crtc_vsync_end;
8143
8144         mode->flags = timings->flags;
8145         mode->type = DRM_MODE_TYPE_DRIVER;
8146
8147         mode->clock = timings->crtc_clock;
8148
8149         drm_mode_set_name(mode);
8150 }
8151
8152 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
8153 {
8154         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8155
8156         if (HAS_GMCH(dev_priv))
8157                 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
8158                 crtc_state->pixel_rate =
8159                         crtc_state->hw.adjusted_mode.crtc_clock;
8160         else
8161                 crtc_state->pixel_rate =
8162                         ilk_pipe_pixel_rate(crtc_state);
8163 }
8164
8165 static void intel_encoder_get_config(struct intel_encoder *encoder,
8166                                      struct intel_crtc_state *crtc_state)
8167 {
8168         encoder->get_config(encoder, crtc_state);
8169 }
8170
8171 static int intel_crtc_compute_config(struct intel_crtc *crtc,
8172                                      struct intel_crtc_state *pipe_config)
8173 {
8174         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8175         const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
8176         int clock_limit = dev_priv->max_dotclk_freq;
8177
8178         if (INTEL_GEN(dev_priv) < 4) {
8179                 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
8180
8181                 /*
8182                  * Enable double wide mode when the dot clock
8183                  * is > 90% of the (display) core speed.
8184                  */
8185                 if (intel_crtc_supports_double_wide(crtc) &&
8186                     adjusted_mode->crtc_clock > clock_limit) {
8187                         clock_limit = dev_priv->max_dotclk_freq;
8188                         pipe_config->double_wide = true;
8189                 }
8190         }
8191
8192         if (adjusted_mode->crtc_clock > clock_limit) {
8193                 drm_dbg_kms(&dev_priv->drm,
8194                             "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
8195                             adjusted_mode->crtc_clock, clock_limit,
8196                             yesno(pipe_config->double_wide));
8197                 return -EINVAL;
8198         }
8199
8200         if ((pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
8201              pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) &&
8202              pipe_config->hw.ctm) {
8203                 /*
8204                  * There is only one pipe CSC unit per pipe, and we need that
8205                  * for output conversion from RGB->YCBCR. So if CTM is already
8206                  * applied we can't support YCBCR420 output.
8207                  */
8208                 drm_dbg_kms(&dev_priv->drm,
8209                             "YCBCR420 and CTM together are not possible\n");
8210                 return -EINVAL;
8211         }
8212
8213         /*
8214          * Pipe horizontal size must be even in:
8215          * - DVO ganged mode
8216          * - LVDS dual channel mode
8217          * - Double wide pipe
8218          */
8219         if (pipe_config->pipe_src_w & 1) {
8220                 if (pipe_config->double_wide) {
8221                         drm_dbg_kms(&dev_priv->drm,
8222                                     "Odd pipe source width not supported with double wide pipe\n");
8223                         return -EINVAL;
8224                 }
8225
8226                 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
8227                     intel_is_dual_link_lvds(dev_priv)) {
8228                         drm_dbg_kms(&dev_priv->drm,
8229                                     "Odd pipe source width not supported with dual link LVDS\n");
8230                         return -EINVAL;
8231                 }
8232         }
8233
8234         /* Cantiga+ cannot handle modes with a hsync front porch of 0.
8235          * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8236          */
8237         if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8238                 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
8239                 return -EINVAL;
8240
8241         intel_crtc_compute_pixel_rate(pipe_config);
8242
8243         if (pipe_config->has_pch_encoder)
8244                 return ilk_fdi_compute_config(crtc, pipe_config);
8245
8246         return 0;
8247 }
8248
8249 static void
8250 intel_reduce_m_n_ratio(u32 *num, u32 *den)
8251 {
8252         while (*num > DATA_LINK_M_N_MASK ||
8253                *den > DATA_LINK_M_N_MASK) {
8254                 *num >>= 1;
8255                 *den >>= 1;
8256         }
8257 }
8258
8259 static void compute_m_n(unsigned int m, unsigned int n,
8260                         u32 *ret_m, u32 *ret_n,
8261                         bool constant_n)
8262 {
8263         /*
8264          * Several DP dongles in particular seem to be fussy about
8265          * too large link M/N values. Give N value as 0x8000 that
8266          * should be acceptable by specific devices. 0x8000 is the
8267          * specified fixed N value for asynchronous clock mode,
8268          * which the devices expect also in synchronous clock mode.
8269          */
8270         if (constant_n)
8271                 *ret_n = DP_LINK_CONSTANT_N_VALUE;
8272         else
8273                 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
8274
8275         *ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
8276         intel_reduce_m_n_ratio(ret_m, ret_n);
8277 }
8278
8279 void
8280 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
8281                        int pixel_clock, int link_clock,
8282                        struct intel_link_m_n *m_n,
8283                        bool constant_n, bool fec_enable)
8284 {
8285         u32 data_clock = bits_per_pixel * pixel_clock;
8286
8287         if (fec_enable)
8288                 data_clock = intel_dp_mode_to_fec_clock(data_clock);
8289
8290         m_n->tu = 64;
8291         compute_m_n(data_clock,
8292                     link_clock * nlanes * 8,
8293                     &m_n->gmch_m, &m_n->gmch_n,
8294                     constant_n);
8295
8296         compute_m_n(pixel_clock, link_clock,
8297                     &m_n->link_m, &m_n->link_n,
8298                     constant_n);
8299 }
8300
8301 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
8302 {
8303         /*
8304          * There may be no VBT; and if the BIOS enabled SSC we can
8305          * just keep using it to avoid unnecessary flicker.  Whereas if the
8306          * BIOS isn't using it, don't assume it will work even if the VBT
8307          * indicates as much.
8308          */
8309         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
8310                 bool bios_lvds_use_ssc = intel_de_read(dev_priv,
8311                                                        PCH_DREF_CONTROL) &
8312                         DREF_SSC1_ENABLE;
8313
8314                 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
8315                         drm_dbg_kms(&dev_priv->drm,
8316                                     "SSC %s by BIOS, overriding VBT which says %s\n",
8317                                     enableddisabled(bios_lvds_use_ssc),
8318                                     enableddisabled(dev_priv->vbt.lvds_use_ssc));
8319                         dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
8320                 }
8321         }
8322 }
8323
8324 static bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
8325 {
8326         if (dev_priv->params.panel_use_ssc >= 0)
8327                 return dev_priv->params.panel_use_ssc != 0;
8328         return dev_priv->vbt.lvds_use_ssc
8329                 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
8330 }
8331
8332 static u32 pnv_dpll_compute_fp(struct dpll *dpll)
8333 {
8334         return (1 << dpll->n) << 16 | dpll->m2;
8335 }
8336
8337 static u32 i9xx_dpll_compute_fp(struct dpll *dpll)
8338 {
8339         return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
8340 }
8341
8342 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
8343                                      struct intel_crtc_state *crtc_state,
8344                                      struct dpll *reduced_clock)
8345 {
8346         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8347         u32 fp, fp2 = 0;
8348
8349         if (IS_PINEVIEW(dev_priv)) {
8350                 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
8351                 if (reduced_clock)
8352                         fp2 = pnv_dpll_compute_fp(reduced_clock);
8353         } else {
8354                 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8355                 if (reduced_clock)
8356                         fp2 = i9xx_dpll_compute_fp(reduced_clock);
8357         }
8358
8359         crtc_state->dpll_hw_state.fp0 = fp;
8360
8361         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8362             reduced_clock) {
8363                 crtc_state->dpll_hw_state.fp1 = fp2;
8364         } else {
8365                 crtc_state->dpll_hw_state.fp1 = fp;
8366         }
8367 }
8368
8369 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
8370                 pipe)
8371 {
8372         u32 reg_val;
8373
8374         /*
8375          * PLLB opamp always calibrates to max value of 0x3f, force enable it
8376          * and set it to a reasonable value instead.
8377          */
8378         reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8379         reg_val &= 0xffffff00;
8380         reg_val |= 0x00000030;
8381         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8382
8383         reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8384         reg_val &= 0x00ffffff;
8385         reg_val |= 0x8c000000;
8386         vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8387
8388         reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
8389         reg_val &= 0xffffff00;
8390         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
8391
8392         reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
8393         reg_val &= 0x00ffffff;
8394         reg_val |= 0xb0000000;
8395         vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
8396 }
8397
8398 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8399                                          const struct intel_link_m_n *m_n)
8400 {
8401         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8402         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8403         enum pipe pipe = crtc->pipe;
8404
8405         intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
8406                        TU_SIZE(m_n->tu) | m_n->gmch_m);
8407         intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
8408         intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
8409         intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
8410 }
8411
8412 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
8413                                  enum transcoder transcoder)
8414 {
8415         if (IS_HASWELL(dev_priv))
8416                 return transcoder == TRANSCODER_EDP;
8417
8418         /*
8419          * Strictly speaking some registers are available before
8420          * gen7, but we only support DRRS on gen7+
8421          */
8422         return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
8423 }
8424
8425 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
8426                                          const struct intel_link_m_n *m_n,
8427                                          const struct intel_link_m_n *m2_n2)
8428 {
8429         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8430         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8431         enum pipe pipe = crtc->pipe;
8432         enum transcoder transcoder = crtc_state->cpu_transcoder;
8433
8434         if (INTEL_GEN(dev_priv) >= 5) {
8435                 intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
8436                                TU_SIZE(m_n->tu) | m_n->gmch_m);
8437                 intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
8438                                m_n->gmch_n);
8439                 intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
8440                                m_n->link_m);
8441                 intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
8442                                m_n->link_n);
8443                 /*
8444                  *  M2_N2 registers are set only if DRRS is supported
8445                  * (to make sure the registers are not unnecessarily accessed).
8446                  */
8447                 if (m2_n2 && crtc_state->has_drrs &&
8448                     transcoder_has_m2_n2(dev_priv, transcoder)) {
8449                         intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
8450                                        TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
8451                         intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
8452                                        m2_n2->gmch_n);
8453                         intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
8454                                        m2_n2->link_m);
8455                         intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
8456                                        m2_n2->link_n);
8457                 }
8458         } else {
8459                 intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
8460                                TU_SIZE(m_n->tu) | m_n->gmch_m);
8461                 intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
8462                 intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
8463                 intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
8464         }
8465 }
8466
8467 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
8468 {
8469         const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
8470         struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
8471
8472         if (m_n == M1_N1) {
8473                 dp_m_n = &crtc_state->dp_m_n;
8474                 dp_m2_n2 = &crtc_state->dp_m2_n2;
8475         } else if (m_n == M2_N2) {
8476
8477                 /*
8478                  * M2_N2 registers are not supported. Hence m2_n2 divider value
8479                  * needs to be programmed into M1_N1.
8480                  */
8481                 dp_m_n = &crtc_state->dp_m2_n2;
8482         } else {
8483                 drm_err(&i915->drm, "Unsupported divider value\n");
8484                 return;
8485         }
8486
8487         if (crtc_state->has_pch_encoder)
8488                 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
8489         else
8490                 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
8491 }
8492
8493 static void vlv_compute_dpll(struct intel_crtc *crtc,
8494                              struct intel_crtc_state *pipe_config)
8495 {
8496         pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
8497                 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8498         if (crtc->pipe != PIPE_A)
8499                 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8500
8501         /* DPLL not used with DSI, but still need the rest set up */
8502         if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8503                 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
8504                         DPLL_EXT_BUFFER_ENABLE_VLV;
8505
8506         pipe_config->dpll_hw_state.dpll_md =
8507                 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8508 }
8509
8510 static void chv_compute_dpll(struct intel_crtc *crtc,
8511                              struct intel_crtc_state *pipe_config)
8512 {
8513         pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
8514                 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
8515         if (crtc->pipe != PIPE_A)
8516                 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
8517
8518         /* DPLL not used with DSI, but still need the rest set up */
8519         if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
8520                 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
8521
8522         pipe_config->dpll_hw_state.dpll_md =
8523                 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8524 }
8525
8526 static void vlv_prepare_pll(struct intel_crtc *crtc,
8527                             const struct intel_crtc_state *pipe_config)
8528 {
8529         struct drm_device *dev = crtc->base.dev;
8530         struct drm_i915_private *dev_priv = to_i915(dev);
8531         enum pipe pipe = crtc->pipe;
8532         u32 mdiv;
8533         u32 bestn, bestm1, bestm2, bestp1, bestp2;
8534         u32 coreclk, reg_val;
8535
8536         /* Enable Refclk */
8537         intel_de_write(dev_priv, DPLL(pipe),
8538                        pipe_config->dpll_hw_state.dpll & ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
8539
8540         /* No need to actually set up the DPLL with DSI */
8541         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8542                 return;
8543
8544         vlv_dpio_get(dev_priv);
8545
8546         bestn = pipe_config->dpll.n;
8547         bestm1 = pipe_config->dpll.m1;
8548         bestm2 = pipe_config->dpll.m2;
8549         bestp1 = pipe_config->dpll.p1;
8550         bestp2 = pipe_config->dpll.p2;
8551
8552         /* See eDP HDMI DPIO driver vbios notes doc */
8553
8554         /* PLL B needs special handling */
8555         if (pipe == PIPE_B)
8556                 vlv_pllb_recal_opamp(dev_priv, pipe);
8557
8558         /* Set up Tx target for periodic Rcomp update */
8559         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
8560
8561         /* Disable target IRef on PLL */
8562         reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
8563         reg_val &= 0x00ffffff;
8564         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
8565
8566         /* Disable fast lock */
8567         vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
8568
8569         /* Set idtafcrecal before PLL is enabled */
8570         mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
8571         mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
8572         mdiv |= ((bestn << DPIO_N_SHIFT));
8573         mdiv |= (1 << DPIO_K_SHIFT);
8574
8575         /*
8576          * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
8577          * but we don't support that).
8578          * Note: don't use the DAC post divider as it seems unstable.
8579          */
8580         mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
8581         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8582
8583         mdiv |= DPIO_ENABLE_CALIBRATION;
8584         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
8585
8586         /* Set HBR and RBR LPF coefficients */
8587         if (pipe_config->port_clock == 162000 ||
8588             intel_crtc_has_type(pipe_config, INTEL_OUTPUT_ANALOG) ||
8589             intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
8590                 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8591                                  0x009f0003);
8592         else
8593                 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
8594                                  0x00d0000f);
8595
8596         if (intel_crtc_has_dp_encoder(pipe_config)) {
8597                 /* Use SSC source */
8598                 if (pipe == PIPE_A)
8599                         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8600                                          0x0df40000);
8601                 else
8602                         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8603                                          0x0df70000);
8604         } else { /* HDMI or VGA */
8605                 /* Use bend source */
8606                 if (pipe == PIPE_A)
8607                         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8608                                          0x0df70000);
8609                 else
8610                         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
8611                                          0x0df40000);
8612         }
8613
8614         coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
8615         coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
8616         if (intel_crtc_has_dp_encoder(pipe_config))
8617                 coreclk |= 0x01000000;
8618         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
8619
8620         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
8621
8622         vlv_dpio_put(dev_priv);
8623 }
8624
8625 static void chv_prepare_pll(struct intel_crtc *crtc,
8626                             const struct intel_crtc_state *pipe_config)
8627 {
8628         struct drm_device *dev = crtc->base.dev;
8629         struct drm_i915_private *dev_priv = to_i915(dev);
8630         enum pipe pipe = crtc->pipe;
8631         enum dpio_channel port = vlv_pipe_to_channel(pipe);
8632         u32 loopfilter, tribuf_calcntr;
8633         u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
8634         u32 dpio_val;
8635         int vco;
8636
8637         /* Enable Refclk and SSC */
8638         intel_de_write(dev_priv, DPLL(pipe),
8639                        pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8640
8641         /* No need to actually set up the DPLL with DSI */
8642         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8643                 return;
8644
8645         bestn = pipe_config->dpll.n;
8646         bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8647         bestm1 = pipe_config->dpll.m1;
8648         bestm2 = pipe_config->dpll.m2 >> 22;
8649         bestp1 = pipe_config->dpll.p1;
8650         bestp2 = pipe_config->dpll.p2;
8651         vco = pipe_config->dpll.vco;
8652         dpio_val = 0;
8653         loopfilter = 0;
8654
8655         vlv_dpio_get(dev_priv);
8656
8657         /* p1 and p2 divider */
8658         vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8659                         5 << DPIO_CHV_S1_DIV_SHIFT |
8660                         bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8661                         bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8662                         1 << DPIO_CHV_K_DIV_SHIFT);
8663
8664         /* Feedback post-divider - m2 */
8665         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8666
8667         /* Feedback refclk divider - n and m1 */
8668         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8669                         DPIO_CHV_M1_DIV_BY_2 |
8670                         1 << DPIO_CHV_N_DIV_SHIFT);
8671
8672         /* M2 fraction division */
8673         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
8674
8675         /* M2 fraction division enable */
8676         dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8677         dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8678         dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8679         if (bestm2_frac)
8680                 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8681         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
8682
8683         /* Program digital lock detect threshold */
8684         dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8685         dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8686                                         DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8687         dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8688         if (!bestm2_frac)
8689                 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8690         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8691
8692         /* Loop filter */
8693         if (vco == 5400000) {
8694                 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8695                 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8696                 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8697                 tribuf_calcntr = 0x9;
8698         } else if (vco <= 6200000) {
8699                 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8700                 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8701                 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8702                 tribuf_calcntr = 0x9;
8703         } else if (vco <= 6480000) {
8704                 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8705                 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8706                 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8707                 tribuf_calcntr = 0x8;
8708         } else {
8709                 /* Not supported. Apply the same limits as in the max case */
8710                 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8711                 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8712                 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8713                 tribuf_calcntr = 0;
8714         }
8715         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8716
8717         dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
8718         dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8719         dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8720         vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8721
8722         /* AFC Recal */
8723         vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8724                         vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8725                         DPIO_AFC_RECAL);
8726
8727         vlv_dpio_put(dev_priv);
8728 }
8729
8730 /**
8731  * vlv_force_pll_on - forcibly enable just the PLL
8732  * @dev_priv: i915 private structure
8733  * @pipe: pipe PLL to enable
8734  * @dpll: PLL configuration
8735  *
8736  * Enable the PLL for @pipe using the supplied @dpll config. To be used
8737  * in cases where we need the PLL enabled even when @pipe is not going to
8738  * be enabled.
8739  */
8740 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
8741                      const struct dpll *dpll)
8742 {
8743         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
8744         struct intel_crtc_state *pipe_config;
8745
8746         pipe_config = intel_crtc_state_alloc(crtc);
8747         if (!pipe_config)
8748                 return -ENOMEM;
8749
8750         pipe_config->cpu_transcoder = (enum transcoder)pipe;
8751         pipe_config->pixel_multiplier = 1;
8752         pipe_config->dpll = *dpll;
8753
8754         if (IS_CHERRYVIEW(dev_priv)) {
8755                 chv_compute_dpll(crtc, pipe_config);
8756                 chv_prepare_pll(crtc, pipe_config);
8757                 chv_enable_pll(crtc, pipe_config);
8758         } else {
8759                 vlv_compute_dpll(crtc, pipe_config);
8760                 vlv_prepare_pll(crtc, pipe_config);
8761                 vlv_enable_pll(crtc, pipe_config);
8762         }
8763
8764         kfree(pipe_config);
8765
8766         return 0;
8767 }
8768
8769 /**
8770  * vlv_force_pll_off - forcibly disable just the PLL
8771  * @dev_priv: i915 private structure
8772  * @pipe: pipe PLL to disable
8773  *
8774  * Disable the PLL for @pipe. To be used in cases where we need
8775  * the PLL enabled even when @pipe is not going to be enabled.
8776  */
8777 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
8778 {
8779         if (IS_CHERRYVIEW(dev_priv))
8780                 chv_disable_pll(dev_priv, pipe);
8781         else
8782                 vlv_disable_pll(dev_priv, pipe);
8783 }
8784
8785 static void i9xx_compute_dpll(struct intel_crtc *crtc,
8786                               struct intel_crtc_state *crtc_state,
8787                               struct dpll *reduced_clock)
8788 {
8789         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8790         u32 dpll;
8791         struct dpll *clock = &crtc_state->dpll;
8792
8793         i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8794
8795         dpll = DPLL_VGA_MODE_DIS;
8796
8797         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
8798                 dpll |= DPLLB_MODE_LVDS;
8799         else
8800                 dpll |= DPLLB_MODE_DAC_SERIAL;
8801
8802         if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8803             IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
8804                 dpll |= (crtc_state->pixel_multiplier - 1)
8805                         << SDVO_MULTIPLIER_SHIFT_HIRES;
8806         }
8807
8808         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8809             intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
8810                 dpll |= DPLL_SDVO_HIGH_SPEED;
8811
8812         if (intel_crtc_has_dp_encoder(crtc_state))
8813                 dpll |= DPLL_SDVO_HIGH_SPEED;
8814
8815         /* compute bitmask from p1 value */
8816         if (IS_PINEVIEW(dev_priv))
8817                 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8818         else {
8819                 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8820                 if (IS_G4X(dev_priv) && reduced_clock)
8821                         dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8822         }
8823         switch (clock->p2) {
8824         case 5:
8825                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8826                 break;
8827         case 7:
8828                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8829                 break;
8830         case 10:
8831                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8832                 break;
8833         case 14:
8834                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8835                 break;
8836         }
8837         if (INTEL_GEN(dev_priv) >= 4)
8838                 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8839
8840         if (crtc_state->sdvo_tv_clock)
8841                 dpll |= PLL_REF_INPUT_TVCLKINBC;
8842         else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8843                  intel_panel_use_ssc(dev_priv))
8844                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8845         else
8846                 dpll |= PLL_REF_INPUT_DREFCLK;
8847
8848         dpll |= DPLL_VCO_ENABLE;
8849         crtc_state->dpll_hw_state.dpll = dpll;
8850
8851         if (INTEL_GEN(dev_priv) >= 4) {
8852                 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
8853                         << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8854                 crtc_state->dpll_hw_state.dpll_md = dpll_md;
8855         }
8856 }
8857
8858 static void i8xx_compute_dpll(struct intel_crtc *crtc,
8859                               struct intel_crtc_state *crtc_state,
8860                               struct dpll *reduced_clock)
8861 {
8862         struct drm_device *dev = crtc->base.dev;
8863         struct drm_i915_private *dev_priv = to_i915(dev);
8864         u32 dpll;
8865         struct dpll *clock = &crtc_state->dpll;
8866
8867         i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
8868
8869         dpll = DPLL_VGA_MODE_DIS;
8870
8871         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8872                 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8873         } else {
8874                 if (clock->p1 == 2)
8875                         dpll |= PLL_P1_DIVIDE_BY_TWO;
8876                 else
8877                         dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8878                 if (clock->p2 == 4)
8879                         dpll |= PLL_P2_DIVIDE_BY_4;
8880         }
8881
8882         /*
8883          * Bspec:
8884          * "[Almador Errata}: For the correct operation of the muxed DVO pins
8885          *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
8886          *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
8887          *  Enable) must be set to “1” in both the DPLL A Control Register
8888          *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
8889          *
8890          * For simplicity We simply keep both bits always enabled in
8891          * both DPLLS. The spec says we should disable the DVO 2X clock
8892          * when not needed, but this seems to work fine in practice.
8893          */
8894         if (IS_I830(dev_priv) ||
8895             intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
8896                 dpll |= DPLL_DVO_2X_MODE;
8897
8898         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
8899             intel_panel_use_ssc(dev_priv))
8900                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8901         else
8902                 dpll |= PLL_REF_INPUT_DREFCLK;
8903
8904         dpll |= DPLL_VCO_ENABLE;
8905         crtc_state->dpll_hw_state.dpll = dpll;
8906 }
8907
8908 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
8909 {
8910         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8911         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8912         enum pipe pipe = crtc->pipe;
8913         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8914         const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
8915         u32 crtc_vtotal, crtc_vblank_end;
8916         int vsyncshift = 0;
8917
8918         /* We need to be careful not to changed the adjusted mode, for otherwise
8919          * the hw state checker will get angry at the mismatch. */
8920         crtc_vtotal = adjusted_mode->crtc_vtotal;
8921         crtc_vblank_end = adjusted_mode->crtc_vblank_end;
8922
8923         if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
8924                 /* the chip adds 2 halflines automatically */
8925                 crtc_vtotal -= 1;
8926                 crtc_vblank_end -= 1;
8927
8928                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
8929                         vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8930                 else
8931                         vsyncshift = adjusted_mode->crtc_hsync_start -
8932                                 adjusted_mode->crtc_htotal / 2;
8933                 if (vsyncshift < 0)
8934                         vsyncshift += adjusted_mode->crtc_htotal;
8935         }
8936
8937         if (INTEL_GEN(dev_priv) > 3)
8938                 intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
8939                                vsyncshift);
8940
8941         intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
8942                        (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
8943         intel_de_write(dev_priv, HBLANK(cpu_transcoder),
8944                        (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
8945         intel_de_write(dev_priv, HSYNC(cpu_transcoder),
8946                        (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
8947
8948         intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
8949                        (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
8950         intel_de_write(dev_priv, VBLANK(cpu_transcoder),
8951                        (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
8952         intel_de_write(dev_priv, VSYNC(cpu_transcoder),
8953                        (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
8954
8955         /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8956          * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8957          * documented on the DDI_FUNC_CTL register description, EDP Input Select
8958          * bits. */
8959         if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
8960             (pipe == PIPE_B || pipe == PIPE_C))
8961                 intel_de_write(dev_priv, VTOTAL(pipe),
8962                                intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
8963
8964 }
8965
8966 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
8967 {
8968         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8969         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8970         enum pipe pipe = crtc->pipe;
8971
8972         /* pipesrc controls the size that is scaled from, which should
8973          * always be the user's requested size.
8974          */
8975         intel_de_write(dev_priv, PIPESRC(pipe),
8976                        ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
8977 }
8978
8979 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
8980 {
8981         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
8982         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
8983
8984         if (IS_GEN(dev_priv, 2))
8985                 return false;
8986
8987         if (INTEL_GEN(dev_priv) >= 9 ||
8988             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
8989                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
8990         else
8991                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
8992 }
8993
8994 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
8995                                          struct intel_crtc_state *pipe_config)
8996 {
8997         struct drm_device *dev = crtc->base.dev;
8998         struct drm_i915_private *dev_priv = to_i915(dev);
8999         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
9000         u32 tmp;
9001
9002         tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
9003         pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
9004         pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
9005
9006         if (!transcoder_is_dsi(cpu_transcoder)) {
9007                 tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
9008                 pipe_config->hw.adjusted_mode.crtc_hblank_start =
9009                                                         (tmp & 0xffff) + 1;
9010                 pipe_config->hw.adjusted_mode.crtc_hblank_end =
9011                                                 ((tmp >> 16) & 0xffff) + 1;
9012         }
9013         tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
9014         pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
9015         pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
9016
9017         tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
9018         pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
9019         pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
9020
9021         if (!transcoder_is_dsi(cpu_transcoder)) {
9022                 tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
9023                 pipe_config->hw.adjusted_mode.crtc_vblank_start =
9024                                                         (tmp & 0xffff) + 1;
9025                 pipe_config->hw.adjusted_mode.crtc_vblank_end =
9026                                                 ((tmp >> 16) & 0xffff) + 1;
9027         }
9028         tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
9029         pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
9030         pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
9031
9032         if (intel_pipe_is_interlaced(pipe_config)) {
9033                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
9034                 pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
9035                 pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
9036         }
9037 }
9038
9039 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
9040                                     struct intel_crtc_state *pipe_config)
9041 {
9042         struct drm_device *dev = crtc->base.dev;
9043         struct drm_i915_private *dev_priv = to_i915(dev);
9044         u32 tmp;
9045
9046         tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
9047         pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
9048         pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
9049
9050         pipe_config->hw.mode.vdisplay = pipe_config->pipe_src_h;
9051         pipe_config->hw.mode.hdisplay = pipe_config->pipe_src_w;
9052 }
9053
9054 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
9055 {
9056         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9057         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9058         u32 pipeconf;
9059
9060         pipeconf = 0;
9061
9062         /* we keep both pipes enabled on 830 */
9063         if (IS_I830(dev_priv))
9064                 pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
9065
9066         if (crtc_state->double_wide)
9067                 pipeconf |= PIPECONF_DOUBLE_WIDE;
9068
9069         /* only g4x and later have fancy bpc/dither controls */
9070         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9071             IS_CHERRYVIEW(dev_priv)) {
9072                 /* Bspec claims that we can't use dithering for 30bpp pipes. */
9073                 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
9074                         pipeconf |= PIPECONF_DITHER_EN |
9075                                     PIPECONF_DITHER_TYPE_SP;
9076
9077                 switch (crtc_state->pipe_bpp) {
9078                 case 18:
9079                         pipeconf |= PIPECONF_6BPC;
9080                         break;
9081                 case 24:
9082                         pipeconf |= PIPECONF_8BPC;
9083                         break;
9084                 case 30:
9085                         pipeconf |= PIPECONF_10BPC;
9086                         break;
9087                 default:
9088                         /* Case prevented by intel_choose_pipe_bpp_dither. */
9089                         BUG();
9090                 }
9091         }
9092
9093         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
9094                 if (INTEL_GEN(dev_priv) < 4 ||
9095                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
9096                         pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
9097                 else
9098                         pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
9099         } else {
9100                 pipeconf |= PIPECONF_PROGRESSIVE;
9101         }
9102
9103         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9104              crtc_state->limited_color_range)
9105                 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9106
9107         pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
9108
9109         pipeconf |= PIPECONF_FRAME_START_DELAY(0);
9110
9111         intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
9112         intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
9113 }
9114
9115 static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
9116                                    struct intel_crtc_state *crtc_state)
9117 {
9118         struct drm_device *dev = crtc->base.dev;
9119         struct drm_i915_private *dev_priv = to_i915(dev);
9120         const struct intel_limit *limit;
9121         int refclk = 48000;
9122
9123         memset(&crtc_state->dpll_hw_state, 0,
9124                sizeof(crtc_state->dpll_hw_state));
9125
9126         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9127                 if (intel_panel_use_ssc(dev_priv)) {
9128                         refclk = dev_priv->vbt.lvds_ssc_freq;
9129                         drm_dbg_kms(&dev_priv->drm,
9130                                     "using SSC reference clock of %d kHz\n",
9131                                     refclk);
9132                 }
9133
9134                 limit = &intel_limits_i8xx_lvds;
9135         } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
9136                 limit = &intel_limits_i8xx_dvo;
9137         } else {
9138                 limit = &intel_limits_i8xx_dac;
9139         }
9140
9141         if (!crtc_state->clock_set &&
9142             !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9143                                  refclk, NULL, &crtc_state->dpll)) {
9144                 drm_err(&dev_priv->drm,
9145                         "Couldn't find PLL settings for mode!\n");
9146                 return -EINVAL;
9147         }
9148
9149         i8xx_compute_dpll(crtc, crtc_state, NULL);
9150
9151         return 0;
9152 }
9153
9154 static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
9155                                   struct intel_crtc_state *crtc_state)
9156 {
9157         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9158         const struct intel_limit *limit;
9159         int refclk = 96000;
9160
9161         memset(&crtc_state->dpll_hw_state, 0,
9162                sizeof(crtc_state->dpll_hw_state));
9163
9164         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9165                 if (intel_panel_use_ssc(dev_priv)) {
9166                         refclk = dev_priv->vbt.lvds_ssc_freq;
9167                         drm_dbg_kms(&dev_priv->drm,
9168                                     "using SSC reference clock of %d kHz\n",
9169                                     refclk);
9170                 }
9171
9172                 if (intel_is_dual_link_lvds(dev_priv))
9173                         limit = &intel_limits_g4x_dual_channel_lvds;
9174                 else
9175                         limit = &intel_limits_g4x_single_channel_lvds;
9176         } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
9177                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
9178                 limit = &intel_limits_g4x_hdmi;
9179         } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
9180                 limit = &intel_limits_g4x_sdvo;
9181         } else {
9182                 /* The option is for other outputs */
9183                 limit = &intel_limits_i9xx_sdvo;
9184         }
9185
9186         if (!crtc_state->clock_set &&
9187             !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9188                                 refclk, NULL, &crtc_state->dpll)) {
9189                 drm_err(&dev_priv->drm,
9190                         "Couldn't find PLL settings for mode!\n");
9191                 return -EINVAL;
9192         }
9193
9194         i9xx_compute_dpll(crtc, crtc_state, NULL);
9195
9196         return 0;
9197 }
9198
9199 static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
9200                                   struct intel_crtc_state *crtc_state)
9201 {
9202         struct drm_device *dev = crtc->base.dev;
9203         struct drm_i915_private *dev_priv = to_i915(dev);
9204         const struct intel_limit *limit;
9205         int refclk = 96000;
9206
9207         memset(&crtc_state->dpll_hw_state, 0,
9208                sizeof(crtc_state->dpll_hw_state));
9209
9210         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9211                 if (intel_panel_use_ssc(dev_priv)) {
9212                         refclk = dev_priv->vbt.lvds_ssc_freq;
9213                         drm_dbg_kms(&dev_priv->drm,
9214                                     "using SSC reference clock of %d kHz\n",
9215                                     refclk);
9216                 }
9217
9218                 limit = &pnv_limits_lvds;
9219         } else {
9220                 limit = &pnv_limits_sdvo;
9221         }
9222
9223         if (!crtc_state->clock_set &&
9224             !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9225                                 refclk, NULL, &crtc_state->dpll)) {
9226                 drm_err(&dev_priv->drm,
9227                         "Couldn't find PLL settings for mode!\n");
9228                 return -EINVAL;
9229         }
9230
9231         i9xx_compute_dpll(crtc, crtc_state, NULL);
9232
9233         return 0;
9234 }
9235
9236 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
9237                                    struct intel_crtc_state *crtc_state)
9238 {
9239         struct drm_device *dev = crtc->base.dev;
9240         struct drm_i915_private *dev_priv = to_i915(dev);
9241         const struct intel_limit *limit;
9242         int refclk = 96000;
9243
9244         memset(&crtc_state->dpll_hw_state, 0,
9245                sizeof(crtc_state->dpll_hw_state));
9246
9247         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
9248                 if (intel_panel_use_ssc(dev_priv)) {
9249                         refclk = dev_priv->vbt.lvds_ssc_freq;
9250                         drm_dbg_kms(&dev_priv->drm,
9251                                     "using SSC reference clock of %d kHz\n",
9252                                     refclk);
9253                 }
9254
9255                 limit = &intel_limits_i9xx_lvds;
9256         } else {
9257                 limit = &intel_limits_i9xx_sdvo;
9258         }
9259
9260         if (!crtc_state->clock_set &&
9261             !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9262                                  refclk, NULL, &crtc_state->dpll)) {
9263                 drm_err(&dev_priv->drm,
9264                         "Couldn't find PLL settings for mode!\n");
9265                 return -EINVAL;
9266         }
9267
9268         i9xx_compute_dpll(crtc, crtc_state, NULL);
9269
9270         return 0;
9271 }
9272
9273 static int chv_crtc_compute_clock(struct intel_crtc *crtc,
9274                                   struct intel_crtc_state *crtc_state)
9275 {
9276         int refclk = 100000;
9277         const struct intel_limit *limit = &intel_limits_chv;
9278         struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9279
9280         memset(&crtc_state->dpll_hw_state, 0,
9281                sizeof(crtc_state->dpll_hw_state));
9282
9283         if (!crtc_state->clock_set &&
9284             !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9285                                 refclk, NULL, &crtc_state->dpll)) {
9286                 drm_err(&i915->drm, "Couldn't find PLL settings for mode!\n");
9287                 return -EINVAL;
9288         }
9289
9290         chv_compute_dpll(crtc, crtc_state);
9291
9292         return 0;
9293 }
9294
9295 static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
9296                                   struct intel_crtc_state *crtc_state)
9297 {
9298         int refclk = 100000;
9299         const struct intel_limit *limit = &intel_limits_vlv;
9300         struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
9301
9302         memset(&crtc_state->dpll_hw_state, 0,
9303                sizeof(crtc_state->dpll_hw_state));
9304
9305         if (!crtc_state->clock_set &&
9306             !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9307                                 refclk, NULL, &crtc_state->dpll)) {
9308                 drm_err(&i915->drm,  "Couldn't find PLL settings for mode!\n");
9309                 return -EINVAL;
9310         }
9311
9312         vlv_compute_dpll(crtc, crtc_state);
9313
9314         return 0;
9315 }
9316
9317 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
9318 {
9319         if (IS_I830(dev_priv))
9320                 return false;
9321
9322         return INTEL_GEN(dev_priv) >= 4 ||
9323                 IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
9324 }
9325
9326 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
9327 {
9328         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9329         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9330         u32 tmp;
9331
9332         if (!i9xx_has_pfit(dev_priv))
9333                 return;
9334
9335         tmp = intel_de_read(dev_priv, PFIT_CONTROL);
9336         if (!(tmp & PFIT_ENABLE))
9337                 return;
9338
9339         /* Check whether the pfit is attached to our pipe. */
9340         if (INTEL_GEN(dev_priv) < 4) {
9341                 if (crtc->pipe != PIPE_B)
9342                         return;
9343         } else {
9344                 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
9345                         return;
9346         }
9347
9348         crtc_state->gmch_pfit.control = tmp;
9349         crtc_state->gmch_pfit.pgm_ratios =
9350                 intel_de_read(dev_priv, PFIT_PGM_RATIOS);
9351 }
9352
9353 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
9354                                struct intel_crtc_state *pipe_config)
9355 {
9356         struct drm_device *dev = crtc->base.dev;
9357         struct drm_i915_private *dev_priv = to_i915(dev);
9358         enum pipe pipe = crtc->pipe;
9359         struct dpll clock;
9360         u32 mdiv;
9361         int refclk = 100000;
9362
9363         /* In case of DSI, DPLL will not be used */
9364         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9365                 return;
9366
9367         vlv_dpio_get(dev_priv);
9368         mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
9369         vlv_dpio_put(dev_priv);
9370
9371         clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
9372         clock.m2 = mdiv & DPIO_M2DIV_MASK;
9373         clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
9374         clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
9375         clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
9376
9377         pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
9378 }
9379
9380 static void
9381 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
9382                               struct intel_initial_plane_config *plane_config)
9383 {
9384         struct drm_device *dev = crtc->base.dev;
9385         struct drm_i915_private *dev_priv = to_i915(dev);
9386         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9387         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9388         enum pipe pipe;
9389         u32 val, base, offset;
9390         int fourcc, pixel_format;
9391         unsigned int aligned_height;
9392         struct drm_framebuffer *fb;
9393         struct intel_framebuffer *intel_fb;
9394
9395         if (!plane->get_hw_state(plane, &pipe))
9396                 return;
9397
9398         drm_WARN_ON(dev, pipe != crtc->pipe);
9399
9400         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9401         if (!intel_fb) {
9402                 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
9403                 return;
9404         }
9405
9406         fb = &intel_fb->base;
9407
9408         fb->dev = dev;
9409
9410         val = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9411
9412         if (INTEL_GEN(dev_priv) >= 4) {
9413                 if (val & DISPPLANE_TILED) {
9414                         plane_config->tiling = I915_TILING_X;
9415                         fb->modifier = I915_FORMAT_MOD_X_TILED;
9416                 }
9417
9418                 if (val & DISPPLANE_ROTATE_180)
9419                         plane_config->rotation = DRM_MODE_ROTATE_180;
9420         }
9421
9422         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B &&
9423             val & DISPPLANE_MIRROR)
9424                 plane_config->rotation |= DRM_MODE_REFLECT_X;
9425
9426         pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9427         fourcc = i9xx_format_to_fourcc(pixel_format);
9428         fb->format = drm_format_info(fourcc);
9429
9430         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
9431                 offset = intel_de_read(dev_priv, DSPOFFSET(i9xx_plane));
9432                 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9433         } else if (INTEL_GEN(dev_priv) >= 4) {
9434                 if (plane_config->tiling)
9435                         offset = intel_de_read(dev_priv,
9436                                                DSPTILEOFF(i9xx_plane));
9437                 else
9438                         offset = intel_de_read(dev_priv,
9439                                                DSPLINOFF(i9xx_plane));
9440                 base = intel_de_read(dev_priv, DSPSURF(i9xx_plane)) & 0xfffff000;
9441         } else {
9442                 base = intel_de_read(dev_priv, DSPADDR(i9xx_plane));
9443         }
9444         plane_config->base = base;
9445
9446         val = intel_de_read(dev_priv, PIPESRC(pipe));
9447         fb->width = ((val >> 16) & 0xfff) + 1;
9448         fb->height = ((val >> 0) & 0xfff) + 1;
9449
9450         val = intel_de_read(dev_priv, DSPSTRIDE(i9xx_plane));
9451         fb->pitches[0] = val & 0xffffffc0;
9452
9453         aligned_height = intel_fb_align_height(fb, 0, fb->height);
9454
9455         plane_config->size = fb->pitches[0] * aligned_height;
9456
9457         drm_dbg_kms(&dev_priv->drm,
9458                     "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9459                     crtc->base.name, plane->base.name, fb->width, fb->height,
9460                     fb->format->cpp[0] * 8, base, fb->pitches[0],
9461                     plane_config->size);
9462
9463         plane_config->fb = intel_fb;
9464 }
9465
9466 static void chv_crtc_clock_get(struct intel_crtc *crtc,
9467                                struct intel_crtc_state *pipe_config)
9468 {
9469         struct drm_device *dev = crtc->base.dev;
9470         struct drm_i915_private *dev_priv = to_i915(dev);
9471         enum pipe pipe = crtc->pipe;
9472         enum dpio_channel port = vlv_pipe_to_channel(pipe);
9473         struct dpll clock;
9474         u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
9475         int refclk = 100000;
9476
9477         /* In case of DSI, DPLL will not be used */
9478         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
9479                 return;
9480
9481         vlv_dpio_get(dev_priv);
9482         cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
9483         pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
9484         pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
9485         pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
9486         pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
9487         vlv_dpio_put(dev_priv);
9488
9489         clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
9490         clock.m2 = (pll_dw0 & 0xff) << 22;
9491         if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
9492                 clock.m2 |= pll_dw2 & 0x3fffff;
9493         clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
9494         clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
9495         clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
9496
9497         pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
9498 }
9499
9500 static enum intel_output_format
9501 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
9502 {
9503         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9504         u32 tmp;
9505
9506         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
9507
9508         if (tmp & PIPEMISC_YUV420_ENABLE) {
9509                 /* We support 4:2:0 in full blend mode only */
9510                 drm_WARN_ON(&dev_priv->drm,
9511                             (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
9512
9513                 return INTEL_OUTPUT_FORMAT_YCBCR420;
9514         } else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
9515                 return INTEL_OUTPUT_FORMAT_YCBCR444;
9516         } else {
9517                 return INTEL_OUTPUT_FORMAT_RGB;
9518         }
9519 }
9520
9521 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
9522 {
9523         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9524         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
9525         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9526         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
9527         u32 tmp;
9528
9529         tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
9530
9531         if (tmp & DISPPLANE_GAMMA_ENABLE)
9532                 crtc_state->gamma_enable = true;
9533
9534         if (!HAS_GMCH(dev_priv) &&
9535             tmp & DISPPLANE_PIPE_CSC_ENABLE)
9536                 crtc_state->csc_enable = true;
9537 }
9538
9539 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
9540                                  struct intel_crtc_state *pipe_config)
9541 {
9542         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9543         enum intel_display_power_domain power_domain;
9544         intel_wakeref_t wakeref;
9545         u32 tmp;
9546         bool ret;
9547
9548         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9549         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
9550         if (!wakeref)
9551                 return false;
9552
9553         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
9554         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9555         pipe_config->shared_dpll = NULL;
9556
9557         ret = false;
9558
9559         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
9560         if (!(tmp & PIPECONF_ENABLE))
9561                 goto out;
9562
9563         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9564             IS_CHERRYVIEW(dev_priv)) {
9565                 switch (tmp & PIPECONF_BPC_MASK) {
9566                 case PIPECONF_6BPC:
9567                         pipe_config->pipe_bpp = 18;
9568                         break;
9569                 case PIPECONF_8BPC:
9570                         pipe_config->pipe_bpp = 24;
9571                         break;
9572                 case PIPECONF_10BPC:
9573                         pipe_config->pipe_bpp = 30;
9574                         break;
9575                 default:
9576                         break;
9577                 }
9578         }
9579
9580         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
9581             (tmp & PIPECONF_COLOR_RANGE_SELECT))
9582                 pipe_config->limited_color_range = true;
9583
9584         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
9585                 PIPECONF_GAMMA_MODE_SHIFT;
9586
9587         if (IS_CHERRYVIEW(dev_priv))
9588                 pipe_config->cgm_mode = intel_de_read(dev_priv,
9589                                                       CGM_PIPE_MODE(crtc->pipe));
9590
9591         i9xx_get_pipe_color_config(pipe_config);
9592         intel_color_get_config(pipe_config);
9593
9594         if (INTEL_GEN(dev_priv) < 4)
9595                 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
9596
9597         intel_get_transcoder_timings(crtc, pipe_config);
9598         intel_get_pipe_src_size(crtc, pipe_config);
9599
9600         i9xx_get_pfit_config(pipe_config);
9601
9602         if (INTEL_GEN(dev_priv) >= 4) {
9603                 /* No way to read it out on pipes B and C */
9604                 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
9605                         tmp = dev_priv->chv_dpll_md[crtc->pipe];
9606                 else
9607                         tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
9608                 pipe_config->pixel_multiplier =
9609                         ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
9610                          >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
9611                 pipe_config->dpll_hw_state.dpll_md = tmp;
9612         } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
9613                    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
9614                 tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
9615                 pipe_config->pixel_multiplier =
9616                         ((tmp & SDVO_MULTIPLIER_MASK)
9617                          >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
9618         } else {
9619                 /* Note that on i915G/GM the pixel multiplier is in the sdvo
9620                  * port and will be fixed up in the encoder->get_config
9621                  * function. */
9622                 pipe_config->pixel_multiplier = 1;
9623         }
9624         pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
9625                                                         DPLL(crtc->pipe));
9626         if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
9627                 pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
9628                                                                FP0(crtc->pipe));
9629                 pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
9630                                                                FP1(crtc->pipe));
9631         } else {
9632                 /* Mask out read-only status bits. */
9633                 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
9634                                                      DPLL_PORTC_READY_MASK |
9635                                                      DPLL_PORTB_READY_MASK);
9636         }
9637
9638         if (IS_CHERRYVIEW(dev_priv))
9639                 chv_crtc_clock_get(crtc, pipe_config);
9640         else if (IS_VALLEYVIEW(dev_priv))
9641                 vlv_crtc_clock_get(crtc, pipe_config);
9642         else
9643                 i9xx_crtc_clock_get(crtc, pipe_config);
9644
9645         /*
9646          * Normally the dotclock is filled in by the encoder .get_config()
9647          * but in case the pipe is enabled w/o any ports we need a sane
9648          * default.
9649          */
9650         pipe_config->hw.adjusted_mode.crtc_clock =
9651                 pipe_config->port_clock / pipe_config->pixel_multiplier;
9652
9653         ret = true;
9654
9655 out:
9656         intel_display_power_put(dev_priv, power_domain, wakeref);
9657
9658         return ret;
9659 }
9660
9661 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
9662 {
9663         struct intel_encoder *encoder;
9664         int i;
9665         u32 val, final;
9666         bool has_lvds = false;
9667         bool has_cpu_edp = false;
9668         bool has_panel = false;
9669         bool has_ck505 = false;
9670         bool can_ssc = false;
9671         bool using_ssc_source = false;
9672
9673         /* We need to take the global config into account */
9674         for_each_intel_encoder(&dev_priv->drm, encoder) {
9675                 switch (encoder->type) {
9676                 case INTEL_OUTPUT_LVDS:
9677                         has_panel = true;
9678                         has_lvds = true;
9679                         break;
9680                 case INTEL_OUTPUT_EDP:
9681                         has_panel = true;
9682                         if (encoder->port == PORT_A)
9683                                 has_cpu_edp = true;
9684                         break;
9685                 default:
9686                         break;
9687                 }
9688         }
9689
9690         if (HAS_PCH_IBX(dev_priv)) {
9691                 has_ck505 = dev_priv->vbt.display_clock_mode;
9692                 can_ssc = has_ck505;
9693         } else {
9694                 has_ck505 = false;
9695                 can_ssc = true;
9696         }
9697
9698         /* Check if any DPLLs are using the SSC source */
9699         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
9700                 u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
9701
9702                 if (!(temp & DPLL_VCO_ENABLE))
9703                         continue;
9704
9705                 if ((temp & PLL_REF_INPUT_MASK) ==
9706                     PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9707                         using_ssc_source = true;
9708                         break;
9709                 }
9710         }
9711
9712         drm_dbg_kms(&dev_priv->drm,
9713                     "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9714                     has_panel, has_lvds, has_ck505, using_ssc_source);
9715
9716         /* Ironlake: try to setup display ref clock before DPLL
9717          * enabling. This is only under driver's control after
9718          * PCH B stepping, previous chipset stepping should be
9719          * ignoring this setting.
9720          */
9721         val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
9722
9723         /* As we must carefully and slowly disable/enable each source in turn,
9724          * compute the final state we want first and check if we need to
9725          * make any changes at all.
9726          */
9727         final = val;
9728         final &= ~DREF_NONSPREAD_SOURCE_MASK;
9729         if (has_ck505)
9730                 final |= DREF_NONSPREAD_CK505_ENABLE;
9731         else
9732                 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9733
9734         final &= ~DREF_SSC_SOURCE_MASK;
9735         final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9736         final &= ~DREF_SSC1_ENABLE;
9737
9738         if (has_panel) {
9739                 final |= DREF_SSC_SOURCE_ENABLE;
9740
9741                 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9742                         final |= DREF_SSC1_ENABLE;
9743
9744                 if (has_cpu_edp) {
9745                         if (intel_panel_use_ssc(dev_priv) && can_ssc)
9746                                 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9747                         else
9748                                 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9749                 } else
9750                         final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9751         } else if (using_ssc_source) {
9752                 final |= DREF_SSC_SOURCE_ENABLE;
9753                 final |= DREF_SSC1_ENABLE;
9754         }
9755
9756         if (final == val)
9757                 return;
9758
9759         /* Always enable nonspread source */
9760         val &= ~DREF_NONSPREAD_SOURCE_MASK;
9761
9762         if (has_ck505)
9763                 val |= DREF_NONSPREAD_CK505_ENABLE;
9764         else
9765                 val |= DREF_NONSPREAD_SOURCE_ENABLE;
9766
9767         if (has_panel) {
9768                 val &= ~DREF_SSC_SOURCE_MASK;
9769                 val |= DREF_SSC_SOURCE_ENABLE;
9770
9771                 /* SSC must be turned on before enabling the CPU output  */
9772                 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9773                         drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
9774                         val |= DREF_SSC1_ENABLE;
9775                 } else
9776                         val &= ~DREF_SSC1_ENABLE;
9777
9778                 /* Get SSC going before enabling the outputs */
9779                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9780                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9781                 udelay(200);
9782
9783                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9784
9785                 /* Enable CPU source on CPU attached eDP */
9786                 if (has_cpu_edp) {
9787                         if (intel_panel_use_ssc(dev_priv) && can_ssc) {
9788                                 drm_dbg_kms(&dev_priv->drm,
9789                                             "Using SSC on eDP\n");
9790                                 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9791                         } else
9792                                 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9793                 } else
9794                         val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9795
9796                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9797                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9798                 udelay(200);
9799         } else {
9800                 drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
9801
9802                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
9803
9804                 /* Turn off CPU output */
9805                 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
9806
9807                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9808                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9809                 udelay(200);
9810
9811                 if (!using_ssc_source) {
9812                         drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
9813
9814                         /* Turn off the SSC source */
9815                         val &= ~DREF_SSC_SOURCE_MASK;
9816                         val |= DREF_SSC_SOURCE_DISABLE;
9817
9818                         /* Turn off SSC1 */
9819                         val &= ~DREF_SSC1_ENABLE;
9820
9821                         intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
9822                         intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
9823                         udelay(200);
9824                 }
9825         }
9826
9827         BUG_ON(val != final);
9828 }
9829
9830 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
9831 {
9832         u32 tmp;
9833
9834         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9835         tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9836         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9837
9838         if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9839                         FDI_MPHY_IOSFSB_RESET_STATUS, 100))
9840                 drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
9841
9842         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
9843         tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9844         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
9845
9846         if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
9847                          FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
9848                 drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
9849 }
9850
9851 /* WaMPhyProgramming:hsw */
9852 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9853 {
9854         u32 tmp;
9855
9856         tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9857         tmp &= ~(0xFF << 24);
9858         tmp |= (0x12 << 24);
9859         intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9860
9861         tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9862         tmp |= (1 << 11);
9863         intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9864
9865         tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9866         tmp |= (1 << 11);
9867         intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9868
9869         tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9870         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9871         intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9872
9873         tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9874         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9875         intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9876
9877         tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9878         tmp &= ~(7 << 13);
9879         tmp |= (5 << 13);
9880         intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
9881
9882         tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9883         tmp &= ~(7 << 13);
9884         tmp |= (5 << 13);
9885         intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
9886
9887         tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9888         tmp &= ~0xFF;
9889         tmp |= 0x1C;
9890         intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9891
9892         tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9893         tmp &= ~0xFF;
9894         tmp |= 0x1C;
9895         intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9896
9897         tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9898         tmp &= ~(0xFF << 16);
9899         tmp |= (0x1C << 16);
9900         intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9901
9902         tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9903         tmp &= ~(0xFF << 16);
9904         tmp |= (0x1C << 16);
9905         intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9906
9907         tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9908         tmp |= (1 << 27);
9909         intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
9910
9911         tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9912         tmp |= (1 << 27);
9913         intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
9914
9915         tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9916         tmp &= ~(0xF << 28);
9917         tmp |= (4 << 28);
9918         intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
9919
9920         tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9921         tmp &= ~(0xF << 28);
9922         tmp |= (4 << 28);
9923         intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
9924 }
9925
9926 /* Implements 3 different sequences from BSpec chapter "Display iCLK
9927  * Programming" based on the parameters passed:
9928  * - Sequence to enable CLKOUT_DP
9929  * - Sequence to enable CLKOUT_DP without spread
9930  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9931  */
9932 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9933                                  bool with_spread, bool with_fdi)
9934 {
9935         u32 reg, tmp;
9936
9937         if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
9938                      "FDI requires downspread\n"))
9939                 with_spread = true;
9940         if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
9941                      with_fdi, "LP PCH doesn't have FDI\n"))
9942                 with_fdi = false;
9943
9944         mutex_lock(&dev_priv->sb_lock);
9945
9946         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9947         tmp &= ~SBI_SSCCTL_DISABLE;
9948         tmp |= SBI_SSCCTL_PATHALT;
9949         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9950
9951         udelay(24);
9952
9953         if (with_spread) {
9954                 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9955                 tmp &= ~SBI_SSCCTL_PATHALT;
9956                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9957
9958                 if (with_fdi) {
9959                         lpt_reset_fdi_mphy(dev_priv);
9960                         lpt_program_fdi_mphy(dev_priv);
9961                 }
9962         }
9963
9964         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9965         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9966         tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9967         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9968
9969         mutex_unlock(&dev_priv->sb_lock);
9970 }
9971
9972 /* Sequence to disable CLKOUT_DP */
9973 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
9974 {
9975         u32 reg, tmp;
9976
9977         mutex_lock(&dev_priv->sb_lock);
9978
9979         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
9980         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9981         tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9982         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9983
9984         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9985         if (!(tmp & SBI_SSCCTL_DISABLE)) {
9986                 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9987                         tmp |= SBI_SSCCTL_PATHALT;
9988                         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9989                         udelay(32);
9990                 }
9991                 tmp |= SBI_SSCCTL_DISABLE;
9992                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9993         }
9994
9995         mutex_unlock(&dev_priv->sb_lock);
9996 }
9997
9998 #define BEND_IDX(steps) ((50 + (steps)) / 5)
9999
10000 static const u16 sscdivintphase[] = {
10001         [BEND_IDX( 50)] = 0x3B23,
10002         [BEND_IDX( 45)] = 0x3B23,
10003         [BEND_IDX( 40)] = 0x3C23,
10004         [BEND_IDX( 35)] = 0x3C23,
10005         [BEND_IDX( 30)] = 0x3D23,
10006         [BEND_IDX( 25)] = 0x3D23,
10007         [BEND_IDX( 20)] = 0x3E23,
10008         [BEND_IDX( 15)] = 0x3E23,
10009         [BEND_IDX( 10)] = 0x3F23,
10010         [BEND_IDX(  5)] = 0x3F23,
10011         [BEND_IDX(  0)] = 0x0025,
10012         [BEND_IDX( -5)] = 0x0025,
10013         [BEND_IDX(-10)] = 0x0125,
10014         [BEND_IDX(-15)] = 0x0125,
10015         [BEND_IDX(-20)] = 0x0225,
10016         [BEND_IDX(-25)] = 0x0225,
10017         [BEND_IDX(-30)] = 0x0325,
10018         [BEND_IDX(-35)] = 0x0325,
10019         [BEND_IDX(-40)] = 0x0425,
10020         [BEND_IDX(-45)] = 0x0425,
10021         [BEND_IDX(-50)] = 0x0525,
10022 };
10023
10024 /*
10025  * Bend CLKOUT_DP
10026  * steps -50 to 50 inclusive, in steps of 5
10027  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
10028  * change in clock period = -(steps / 10) * 5.787 ps
10029  */
10030 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
10031 {
10032         u32 tmp;
10033         int idx = BEND_IDX(steps);
10034
10035         if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
10036                 return;
10037
10038         if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
10039                 return;
10040
10041         mutex_lock(&dev_priv->sb_lock);
10042
10043         if (steps % 10 != 0)
10044                 tmp = 0xAAAAAAAB;
10045         else
10046                 tmp = 0x00000000;
10047         intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
10048
10049         tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
10050         tmp &= 0xffff0000;
10051         tmp |= sscdivintphase[idx];
10052         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
10053
10054         mutex_unlock(&dev_priv->sb_lock);
10055 }
10056
10057 #undef BEND_IDX
10058
10059 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
10060 {
10061         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10062         u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
10063
10064         if ((ctl & SPLL_PLL_ENABLE) == 0)
10065                 return false;
10066
10067         if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
10068             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10069                 return true;
10070
10071         if (IS_BROADWELL(dev_priv) &&
10072             (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
10073                 return true;
10074
10075         return false;
10076 }
10077
10078 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
10079                                enum intel_dpll_id id)
10080 {
10081         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
10082         u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
10083
10084         if ((ctl & WRPLL_PLL_ENABLE) == 0)
10085                 return false;
10086
10087         if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
10088                 return true;
10089
10090         if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
10091             (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
10092             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
10093                 return true;
10094
10095         return false;
10096 }
10097
10098 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
10099 {
10100         struct intel_encoder *encoder;
10101         bool has_fdi = false;
10102
10103         for_each_intel_encoder(&dev_priv->drm, encoder) {
10104                 switch (encoder->type) {
10105                 case INTEL_OUTPUT_ANALOG:
10106                         has_fdi = true;
10107                         break;
10108                 default:
10109                         break;
10110                 }
10111         }
10112
10113         /*
10114          * The BIOS may have decided to use the PCH SSC
10115          * reference so we must not disable it until the
10116          * relevant PLLs have stopped relying on it. We'll
10117          * just leave the PCH SSC reference enabled in case
10118          * any active PLL is using it. It will get disabled
10119          * after runtime suspend if we don't have FDI.
10120          *
10121          * TODO: Move the whole reference clock handling
10122          * to the modeset sequence proper so that we can
10123          * actually enable/disable/reconfigure these things
10124          * safely. To do that we need to introduce a real
10125          * clock hierarchy. That would also allow us to do
10126          * clock bending finally.
10127          */
10128         dev_priv->pch_ssc_use = 0;
10129
10130         if (spll_uses_pch_ssc(dev_priv)) {
10131                 drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
10132                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
10133         }
10134
10135         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
10136                 drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
10137                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
10138         }
10139
10140         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
10141                 drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
10142                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
10143         }
10144
10145         if (dev_priv->pch_ssc_use)
10146                 return;
10147
10148         if (has_fdi) {
10149                 lpt_bend_clkout_dp(dev_priv, 0);
10150                 lpt_enable_clkout_dp(dev_priv, true, true);
10151         } else {
10152                 lpt_disable_clkout_dp(dev_priv);
10153         }
10154 }
10155
10156 /*
10157  * Initialize reference clocks when the driver loads
10158  */
10159 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
10160 {
10161         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
10162                 ilk_init_pch_refclk(dev_priv);
10163         else if (HAS_PCH_LPT(dev_priv))
10164                 lpt_init_pch_refclk(dev_priv);
10165 }
10166
10167 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
10168 {
10169         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10170         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10171         enum pipe pipe = crtc->pipe;
10172         u32 val;
10173
10174         val = 0;
10175
10176         switch (crtc_state->pipe_bpp) {
10177         case 18:
10178                 val |= PIPECONF_6BPC;
10179                 break;
10180         case 24:
10181                 val |= PIPECONF_8BPC;
10182                 break;
10183         case 30:
10184                 val |= PIPECONF_10BPC;
10185                 break;
10186         case 36:
10187                 val |= PIPECONF_12BPC;
10188                 break;
10189         default:
10190                 /* Case prevented by intel_choose_pipe_bpp_dither. */
10191                 BUG();
10192         }
10193
10194         if (crtc_state->dither)
10195                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10196
10197         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10198                 val |= PIPECONF_INTERLACED_ILK;
10199         else
10200                 val |= PIPECONF_PROGRESSIVE;
10201
10202         /*
10203          * This would end up with an odd purple hue over
10204          * the entire display. Make sure we don't do it.
10205          */
10206         drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
10207                     crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
10208
10209         if (crtc_state->limited_color_range &&
10210             !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
10211                 val |= PIPECONF_COLOR_RANGE_SELECT;
10212
10213         if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10214                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
10215
10216         val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
10217
10218         val |= PIPECONF_FRAME_START_DELAY(0);
10219
10220         intel_de_write(dev_priv, PIPECONF(pipe), val);
10221         intel_de_posting_read(dev_priv, PIPECONF(pipe));
10222 }
10223
10224 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
10225 {
10226         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10227         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10228         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
10229         u32 val = 0;
10230
10231         if (IS_HASWELL(dev_priv) && crtc_state->dither)
10232                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
10233
10234         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
10235                 val |= PIPECONF_INTERLACED_ILK;
10236         else
10237                 val |= PIPECONF_PROGRESSIVE;
10238
10239         if (IS_HASWELL(dev_priv) &&
10240             crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
10241                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
10242
10243         intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
10244         intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
10245 }
10246
10247 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
10248 {
10249         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10250         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10251         u32 val = 0;
10252
10253         switch (crtc_state->pipe_bpp) {
10254         case 18:
10255                 val |= PIPEMISC_DITHER_6_BPC;
10256                 break;
10257         case 24:
10258                 val |= PIPEMISC_DITHER_8_BPC;
10259                 break;
10260         case 30:
10261                 val |= PIPEMISC_DITHER_10_BPC;
10262                 break;
10263         case 36:
10264                 val |= PIPEMISC_DITHER_12_BPC;
10265                 break;
10266         default:
10267                 MISSING_CASE(crtc_state->pipe_bpp);
10268                 break;
10269         }
10270
10271         if (crtc_state->dither)
10272                 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
10273
10274         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
10275             crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
10276                 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
10277
10278         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
10279                 val |= PIPEMISC_YUV420_ENABLE |
10280                         PIPEMISC_YUV420_MODE_FULL_BLEND;
10281
10282         if (INTEL_GEN(dev_priv) >= 11 &&
10283             (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
10284                                            BIT(PLANE_CURSOR))) == 0)
10285                 val |= PIPEMISC_HDR_MODE_PRECISION;
10286
10287         if (INTEL_GEN(dev_priv) >= 12)
10288                 val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
10289
10290         intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
10291 }
10292
10293 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
10294 {
10295         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10296         u32 tmp;
10297
10298         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
10299
10300         switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
10301         case PIPEMISC_DITHER_6_BPC:
10302                 return 18;
10303         case PIPEMISC_DITHER_8_BPC:
10304                 return 24;
10305         case PIPEMISC_DITHER_10_BPC:
10306                 return 30;
10307         case PIPEMISC_DITHER_12_BPC:
10308                 return 36;
10309         default:
10310                 MISSING_CASE(tmp);
10311                 return 0;
10312         }
10313 }
10314
10315 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
10316 {
10317         /*
10318          * Account for spread spectrum to avoid
10319          * oversubscribing the link. Max center spread
10320          * is 2.5%; use 5% for safety's sake.
10321          */
10322         u32 bps = target_clock * bpp * 21 / 20;
10323         return DIV_ROUND_UP(bps, link_bw * 8);
10324 }
10325
10326 static bool ilk_needs_fb_cb_tune(struct dpll *dpll, int factor)
10327 {
10328         return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
10329 }
10330
10331 static void ilk_compute_dpll(struct intel_crtc *crtc,
10332                              struct intel_crtc_state *crtc_state,
10333                              struct dpll *reduced_clock)
10334 {
10335         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10336         u32 dpll, fp, fp2;
10337         int factor;
10338
10339         /* Enable autotuning of the PLL clock (if permissible) */
10340         factor = 21;
10341         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10342                 if ((intel_panel_use_ssc(dev_priv) &&
10343                      dev_priv->vbt.lvds_ssc_freq == 100000) ||
10344                     (HAS_PCH_IBX(dev_priv) &&
10345                      intel_is_dual_link_lvds(dev_priv)))
10346                         factor = 25;
10347         } else if (crtc_state->sdvo_tv_clock) {
10348                 factor = 20;
10349         }
10350
10351         fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
10352
10353         if (ilk_needs_fb_cb_tune(&crtc_state->dpll, factor))
10354                 fp |= FP_CB_TUNE;
10355
10356         if (reduced_clock) {
10357                 fp2 = i9xx_dpll_compute_fp(reduced_clock);
10358
10359                 if (reduced_clock->m < factor * reduced_clock->n)
10360                         fp2 |= FP_CB_TUNE;
10361         } else {
10362                 fp2 = fp;
10363         }
10364
10365         dpll = 0;
10366
10367         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
10368                 dpll |= DPLLB_MODE_LVDS;
10369         else
10370                 dpll |= DPLLB_MODE_DAC_SERIAL;
10371
10372         dpll |= (crtc_state->pixel_multiplier - 1)
10373                 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
10374
10375         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
10376             intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
10377                 dpll |= DPLL_SDVO_HIGH_SPEED;
10378
10379         if (intel_crtc_has_dp_encoder(crtc_state))
10380                 dpll |= DPLL_SDVO_HIGH_SPEED;
10381
10382         /*
10383          * The high speed IO clock is only really required for
10384          * SDVO/HDMI/DP, but we also enable it for CRT to make it
10385          * possible to share the DPLL between CRT and HDMI. Enabling
10386          * the clock needlessly does no real harm, except use up a
10387          * bit of power potentially.
10388          *
10389          * We'll limit this to IVB with 3 pipes, since it has only two
10390          * DPLLs and so DPLL sharing is the only way to get three pipes
10391          * driving PCH ports at the same time. On SNB we could do this,
10392          * and potentially avoid enabling the second DPLL, but it's not
10393          * clear if it''s a win or loss power wise. No point in doing
10394          * this on ILK at all since it has a fixed DPLL<->pipe mapping.
10395          */
10396         if (INTEL_NUM_PIPES(dev_priv) == 3 &&
10397             intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
10398                 dpll |= DPLL_SDVO_HIGH_SPEED;
10399
10400         /* compute bitmask from p1 value */
10401         dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
10402         /* also FPA1 */
10403         dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
10404
10405         switch (crtc_state->dpll.p2) {
10406         case 5:
10407                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
10408                 break;
10409         case 7:
10410                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
10411                 break;
10412         case 10:
10413                 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
10414                 break;
10415         case 14:
10416                 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
10417                 break;
10418         }
10419
10420         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
10421             intel_panel_use_ssc(dev_priv))
10422                 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
10423         else
10424                 dpll |= PLL_REF_INPUT_DREFCLK;
10425
10426         dpll |= DPLL_VCO_ENABLE;
10427
10428         crtc_state->dpll_hw_state.dpll = dpll;
10429         crtc_state->dpll_hw_state.fp0 = fp;
10430         crtc_state->dpll_hw_state.fp1 = fp2;
10431 }
10432
10433 static int ilk_crtc_compute_clock(struct intel_crtc *crtc,
10434                                   struct intel_crtc_state *crtc_state)
10435 {
10436         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10437         struct intel_atomic_state *state =
10438                 to_intel_atomic_state(crtc_state->uapi.state);
10439         const struct intel_limit *limit;
10440         int refclk = 120000;
10441
10442         memset(&crtc_state->dpll_hw_state, 0,
10443                sizeof(crtc_state->dpll_hw_state));
10444
10445         /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
10446         if (!crtc_state->has_pch_encoder)
10447                 return 0;
10448
10449         if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
10450                 if (intel_panel_use_ssc(dev_priv)) {
10451                         drm_dbg_kms(&dev_priv->drm,
10452                                     "using SSC reference clock of %d kHz\n",
10453                                     dev_priv->vbt.lvds_ssc_freq);
10454                         refclk = dev_priv->vbt.lvds_ssc_freq;
10455                 }
10456
10457                 if (intel_is_dual_link_lvds(dev_priv)) {
10458                         if (refclk == 100000)
10459                                 limit = &ilk_limits_dual_lvds_100m;
10460                         else
10461                                 limit = &ilk_limits_dual_lvds;
10462                 } else {
10463                         if (refclk == 100000)
10464                                 limit = &ilk_limits_single_lvds_100m;
10465                         else
10466                                 limit = &ilk_limits_single_lvds;
10467                 }
10468         } else {
10469                 limit = &ilk_limits_dac;
10470         }
10471
10472         if (!crtc_state->clock_set &&
10473             !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
10474                                 refclk, NULL, &crtc_state->dpll)) {
10475                 drm_err(&dev_priv->drm,
10476                         "Couldn't find PLL settings for mode!\n");
10477                 return -EINVAL;
10478         }
10479
10480         ilk_compute_dpll(crtc, crtc_state, NULL);
10481
10482         if (!intel_reserve_shared_dplls(state, crtc, NULL)) {
10483                 drm_dbg_kms(&dev_priv->drm,
10484                             "failed to find PLL for pipe %c\n",
10485                             pipe_name(crtc->pipe));
10486                 return -EINVAL;
10487         }
10488
10489         return 0;
10490 }
10491
10492 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
10493                                          struct intel_link_m_n *m_n)
10494 {
10495         struct drm_device *dev = crtc->base.dev;
10496         struct drm_i915_private *dev_priv = to_i915(dev);
10497         enum pipe pipe = crtc->pipe;
10498
10499         m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
10500         m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
10501         m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10502                 & ~TU_SIZE_MASK;
10503         m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
10504         m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
10505                     & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10506 }
10507
10508 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
10509                                          enum transcoder transcoder,
10510                                          struct intel_link_m_n *m_n,
10511                                          struct intel_link_m_n *m2_n2)
10512 {
10513         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10514         enum pipe pipe = crtc->pipe;
10515
10516         if (INTEL_GEN(dev_priv) >= 5) {
10517                 m_n->link_m = intel_de_read(dev_priv,
10518                                             PIPE_LINK_M1(transcoder));
10519                 m_n->link_n = intel_de_read(dev_priv,
10520                                             PIPE_LINK_N1(transcoder));
10521                 m_n->gmch_m = intel_de_read(dev_priv,
10522                                             PIPE_DATA_M1(transcoder))
10523                         & ~TU_SIZE_MASK;
10524                 m_n->gmch_n = intel_de_read(dev_priv,
10525                                             PIPE_DATA_N1(transcoder));
10526                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
10527                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10528
10529                 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
10530                         m2_n2->link_m = intel_de_read(dev_priv,
10531                                                       PIPE_LINK_M2(transcoder));
10532                         m2_n2->link_n = intel_de_read(dev_priv,
10533                                                              PIPE_LINK_N2(transcoder));
10534                         m2_n2->gmch_m = intel_de_read(dev_priv,
10535                                                              PIPE_DATA_M2(transcoder))
10536                                         & ~TU_SIZE_MASK;
10537                         m2_n2->gmch_n = intel_de_read(dev_priv,
10538                                                              PIPE_DATA_N2(transcoder));
10539                         m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
10540                                         & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10541                 }
10542         } else {
10543                 m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
10544                 m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
10545                 m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10546                         & ~TU_SIZE_MASK;
10547                 m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
10548                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
10549                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
10550         }
10551 }
10552
10553 void intel_dp_get_m_n(struct intel_crtc *crtc,
10554                       struct intel_crtc_state *pipe_config)
10555 {
10556         if (pipe_config->has_pch_encoder)
10557                 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
10558         else
10559                 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10560                                              &pipe_config->dp_m_n,
10561                                              &pipe_config->dp_m2_n2);
10562 }
10563
10564 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
10565                                    struct intel_crtc_state *pipe_config)
10566 {
10567         intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
10568                                      &pipe_config->fdi_m_n, NULL);
10569 }
10570
10571 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
10572                                   u32 pos, u32 size)
10573 {
10574         drm_rect_init(&crtc_state->pch_pfit.dst,
10575                       pos >> 16, pos & 0xffff,
10576                       size >> 16, size & 0xffff);
10577 }
10578
10579 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
10580 {
10581         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10582         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10583         struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
10584         int id = -1;
10585         int i;
10586
10587         /* find scaler attached to this pipe */
10588         for (i = 0; i < crtc->num_scalers; i++) {
10589                 u32 ctl, pos, size;
10590
10591                 ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
10592                 if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
10593                         continue;
10594
10595                 id = i;
10596                 crtc_state->pch_pfit.enabled = true;
10597
10598                 pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
10599                 size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
10600
10601                 ilk_get_pfit_pos_size(crtc_state, pos, size);
10602
10603                 scaler_state->scalers[i].in_use = true;
10604                 break;
10605         }
10606
10607         scaler_state->scaler_id = id;
10608         if (id >= 0)
10609                 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
10610         else
10611                 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
10612 }
10613
10614 static void
10615 skl_get_initial_plane_config(struct intel_crtc *crtc,
10616                              struct intel_initial_plane_config *plane_config)
10617 {
10618         struct drm_device *dev = crtc->base.dev;
10619         struct drm_i915_private *dev_priv = to_i915(dev);
10620         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
10621         enum plane_id plane_id = plane->id;
10622         enum pipe pipe;
10623         u32 val, base, offset, stride_mult, tiling, alpha;
10624         int fourcc, pixel_format;
10625         unsigned int aligned_height;
10626         struct drm_framebuffer *fb;
10627         struct intel_framebuffer *intel_fb;
10628
10629         if (!plane->get_hw_state(plane, &pipe))
10630                 return;
10631
10632         drm_WARN_ON(dev, pipe != crtc->pipe);
10633
10634         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10635         if (!intel_fb) {
10636                 drm_dbg_kms(&dev_priv->drm, "failed to alloc fb\n");
10637                 return;
10638         }
10639
10640         fb = &intel_fb->base;
10641
10642         fb->dev = dev;
10643
10644         val = intel_de_read(dev_priv, PLANE_CTL(pipe, plane_id));
10645
10646         if (INTEL_GEN(dev_priv) >= 11)
10647                 pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
10648         else
10649                 pixel_format = val & PLANE_CTL_FORMAT_MASK;
10650
10651         if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
10652                 alpha = intel_de_read(dev_priv,
10653                                       PLANE_COLOR_CTL(pipe, plane_id));
10654                 alpha &= PLANE_COLOR_ALPHA_MASK;
10655         } else {
10656                 alpha = val & PLANE_CTL_ALPHA_MASK;
10657         }
10658
10659         fourcc = skl_format_to_fourcc(pixel_format,
10660                                       val & PLANE_CTL_ORDER_RGBX, alpha);
10661         fb->format = drm_format_info(fourcc);
10662
10663         tiling = val & PLANE_CTL_TILED_MASK;
10664         switch (tiling) {
10665         case PLANE_CTL_TILED_LINEAR:
10666                 fb->modifier = DRM_FORMAT_MOD_LINEAR;
10667                 break;
10668         case PLANE_CTL_TILED_X:
10669                 plane_config->tiling = I915_TILING_X;
10670                 fb->modifier = I915_FORMAT_MOD_X_TILED;
10671                 break;
10672         case PLANE_CTL_TILED_Y:
10673                 plane_config->tiling = I915_TILING_Y;
10674                 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10675                         fb->modifier = INTEL_GEN(dev_priv) >= 12 ?
10676                                 I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS :
10677                                 I915_FORMAT_MOD_Y_TILED_CCS;
10678                 else if (val & PLANE_CTL_MEDIA_DECOMPRESSION_ENABLE)
10679                         fb->modifier = I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;
10680                 else
10681                         fb->modifier = I915_FORMAT_MOD_Y_TILED;
10682                 break;
10683         case PLANE_CTL_TILED_YF:
10684                 if (val & PLANE_CTL_RENDER_DECOMPRESSION_ENABLE)
10685                         fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
10686                 else
10687                         fb->modifier = I915_FORMAT_MOD_Yf_TILED;
10688                 break;
10689         default:
10690                 MISSING_CASE(tiling);
10691                 goto error;
10692         }
10693
10694         /*
10695          * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
10696          * while i915 HW rotation is clockwise, thats why this swapping.
10697          */
10698         switch (val & PLANE_CTL_ROTATE_MASK) {
10699         case PLANE_CTL_ROTATE_0:
10700                 plane_config->rotation = DRM_MODE_ROTATE_0;
10701                 break;
10702         case PLANE_CTL_ROTATE_90:
10703                 plane_config->rotation = DRM_MODE_ROTATE_270;
10704                 break;
10705         case PLANE_CTL_ROTATE_180:
10706                 plane_config->rotation = DRM_MODE_ROTATE_180;
10707                 break;
10708         case PLANE_CTL_ROTATE_270:
10709                 plane_config->rotation = DRM_MODE_ROTATE_90;
10710                 break;
10711         }
10712
10713         if (INTEL_GEN(dev_priv) >= 10 &&
10714             val & PLANE_CTL_FLIP_HORIZONTAL)
10715                 plane_config->rotation |= DRM_MODE_REFLECT_X;
10716
10717         /* 90/270 degree rotation would require extra work */
10718         if (drm_rotation_90_or_270(plane_config->rotation))
10719                 goto error;
10720
10721         base = intel_de_read(dev_priv, PLANE_SURF(pipe, plane_id)) & 0xfffff000;
10722         plane_config->base = base;
10723
10724         offset = intel_de_read(dev_priv, PLANE_OFFSET(pipe, plane_id));
10725
10726         val = intel_de_read(dev_priv, PLANE_SIZE(pipe, plane_id));
10727         fb->height = ((val >> 16) & 0xffff) + 1;
10728         fb->width = ((val >> 0) & 0xffff) + 1;
10729
10730         val = intel_de_read(dev_priv, PLANE_STRIDE(pipe, plane_id));
10731         stride_mult = skl_plane_stride_mult(fb, 0, DRM_MODE_ROTATE_0);
10732         fb->pitches[0] = (val & 0x3ff) * stride_mult;
10733
10734         aligned_height = intel_fb_align_height(fb, 0, fb->height);
10735
10736         plane_config->size = fb->pitches[0] * aligned_height;
10737
10738         drm_dbg_kms(&dev_priv->drm,
10739                     "%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
10740                     crtc->base.name, plane->base.name, fb->width, fb->height,
10741                     fb->format->cpp[0] * 8, base, fb->pitches[0],
10742                     plane_config->size);
10743
10744         plane_config->fb = intel_fb;
10745         return;
10746
10747 error:
10748         kfree(intel_fb);
10749 }
10750
10751 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
10752 {
10753         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
10754         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10755         u32 ctl, pos, size;
10756
10757         ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
10758         if ((ctl & PF_ENABLE) == 0)
10759                 return;
10760
10761         crtc_state->pch_pfit.enabled = true;
10762
10763         pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
10764         size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
10765
10766         ilk_get_pfit_pos_size(crtc_state, pos, size);
10767
10768         /*
10769          * We currently do not free assignements of panel fitters on
10770          * ivb/hsw (since we don't use the higher upscaling modes which
10771          * differentiates them) so just WARN about this case for now.
10772          */
10773         drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
10774                     (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
10775 }
10776
10777 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
10778                                 struct intel_crtc_state *pipe_config)
10779 {
10780         struct drm_device *dev = crtc->base.dev;
10781         struct drm_i915_private *dev_priv = to_i915(dev);
10782         enum intel_display_power_domain power_domain;
10783         intel_wakeref_t wakeref;
10784         u32 tmp;
10785         bool ret;
10786
10787         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10788         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
10789         if (!wakeref)
10790                 return false;
10791
10792         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10793         pipe_config->shared_dpll = NULL;
10794
10795         ret = false;
10796         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
10797         if (!(tmp & PIPECONF_ENABLE))
10798                 goto out;
10799
10800         switch (tmp & PIPECONF_BPC_MASK) {
10801         case PIPECONF_6BPC:
10802                 pipe_config->pipe_bpp = 18;
10803                 break;
10804         case PIPECONF_8BPC:
10805                 pipe_config->pipe_bpp = 24;
10806                 break;
10807         case PIPECONF_10BPC:
10808                 pipe_config->pipe_bpp = 30;
10809                 break;
10810         case PIPECONF_12BPC:
10811                 pipe_config->pipe_bpp = 36;
10812                 break;
10813         default:
10814                 break;
10815         }
10816
10817         if (tmp & PIPECONF_COLOR_RANGE_SELECT)
10818                 pipe_config->limited_color_range = true;
10819
10820         switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
10821         case PIPECONF_OUTPUT_COLORSPACE_YUV601:
10822         case PIPECONF_OUTPUT_COLORSPACE_YUV709:
10823                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
10824                 break;
10825         default:
10826                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
10827                 break;
10828         }
10829
10830         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
10831                 PIPECONF_GAMMA_MODE_SHIFT;
10832
10833         pipe_config->csc_mode = intel_de_read(dev_priv,
10834                                               PIPE_CSC_MODE(crtc->pipe));
10835
10836         i9xx_get_pipe_color_config(pipe_config);
10837         intel_color_get_config(pipe_config);
10838
10839         if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
10840                 struct intel_shared_dpll *pll;
10841                 enum intel_dpll_id pll_id;
10842
10843                 pipe_config->has_pch_encoder = true;
10844
10845                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
10846                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10847                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
10848
10849                 ilk_get_fdi_m_n_config(crtc, pipe_config);
10850
10851                 if (HAS_PCH_IBX(dev_priv)) {
10852                         /*
10853                          * The pipe->pch transcoder and pch transcoder->pll
10854                          * mapping is fixed.
10855                          */
10856                         pll_id = (enum intel_dpll_id) crtc->pipe;
10857                 } else {
10858                         tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
10859                         if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
10860                                 pll_id = DPLL_ID_PCH_PLL_B;
10861                         else
10862                                 pll_id= DPLL_ID_PCH_PLL_A;
10863                 }
10864
10865                 pipe_config->shared_dpll =
10866                         intel_get_shared_dpll_by_id(dev_priv, pll_id);
10867                 pll = pipe_config->shared_dpll;
10868
10869                 drm_WARN_ON(dev, !pll->info->funcs->get_hw_state(dev_priv, pll,
10870                                                  &pipe_config->dpll_hw_state));
10871
10872                 tmp = pipe_config->dpll_hw_state.dpll;
10873                 pipe_config->pixel_multiplier =
10874                         ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10875                          >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
10876
10877                 ilk_pch_clock_get(crtc, pipe_config);
10878         } else {
10879                 pipe_config->pixel_multiplier = 1;
10880         }
10881
10882         intel_get_transcoder_timings(crtc, pipe_config);
10883         intel_get_pipe_src_size(crtc, pipe_config);
10884
10885         ilk_get_pfit_config(pipe_config);
10886
10887         ret = true;
10888
10889 out:
10890         intel_display_power_put(dev_priv, power_domain, wakeref);
10891
10892         return ret;
10893 }
10894
10895 static int hsw_crtc_compute_clock(struct intel_crtc *crtc,
10896                                   struct intel_crtc_state *crtc_state)
10897 {
10898         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10899         struct intel_atomic_state *state =
10900                 to_intel_atomic_state(crtc_state->uapi.state);
10901
10902         if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) ||
10903             INTEL_GEN(dev_priv) >= 11) {
10904                 struct intel_encoder *encoder =
10905                         intel_get_crtc_new_encoder(state, crtc_state);
10906
10907                 if (!intel_reserve_shared_dplls(state, crtc, encoder)) {
10908                         drm_dbg_kms(&dev_priv->drm,
10909                                     "failed to find PLL for pipe %c\n",
10910                                     pipe_name(crtc->pipe));
10911                         return -EINVAL;
10912                 }
10913         }
10914
10915         return 0;
10916 }
10917
10918 static void dg1_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10919                             struct intel_crtc_state *pipe_config)
10920 {
10921         enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10922         enum phy phy = intel_port_to_phy(dev_priv, port);
10923         enum intel_dpll_id id;
10924         u32 clk_sel;
10925
10926         clk_sel = intel_de_read(dev_priv, DG1_DPCLKA_CFGCR0(phy)) & DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10927         id = DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_DPLL_MAP(clk_sel, phy);
10928
10929         if (WARN_ON(id > DPLL_ID_DG1_DPLL3))
10930                 return;
10931
10932         pipe_config->icl_port_dplls[port_dpll_id].pll =
10933                 intel_get_shared_dpll_by_id(dev_priv, id);
10934
10935         icl_set_active_port_dpll(pipe_config, port_dpll_id);
10936 }
10937
10938 static void cnl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10939                             struct intel_crtc_state *pipe_config)
10940 {
10941         enum intel_dpll_id id;
10942         u32 temp;
10943
10944         temp = intel_de_read(dev_priv, DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
10945         id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
10946
10947         if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL2))
10948                 return;
10949
10950         pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
10951 }
10952
10953 static void icl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
10954                             struct intel_crtc_state *pipe_config)
10955 {
10956         enum phy phy = intel_port_to_phy(dev_priv, port);
10957         enum icl_port_dpll_id port_dpll_id;
10958         enum intel_dpll_id id;
10959         u32 temp;
10960
10961         if (intel_phy_is_combo(dev_priv, phy)) {
10962                 u32 mask, shift;
10963
10964                 if (IS_ROCKETLAKE(dev_priv)) {
10965                         mask = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10966                         shift = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10967                 } else {
10968                         mask = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
10969                         shift = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
10970                 }
10971
10972                 temp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0) & mask;
10973                 id = temp >> shift;
10974                 port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10975         } else if (intel_phy_is_tc(dev_priv, phy)) {
10976                 u32 clk_sel = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
10977
10978                 if (clk_sel == DDI_CLK_SEL_MG) {
10979                         id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
10980                                                                     port));
10981                         port_dpll_id = ICL_PORT_DPLL_MG_PHY;
10982                 } else {
10983                         drm_WARN_ON(&dev_priv->drm,
10984                                     clk_sel < DDI_CLK_SEL_TBT_162);
10985                         id = DPLL_ID_ICL_TBTPLL;
10986                         port_dpll_id = ICL_PORT_DPLL_DEFAULT;
10987                 }
10988         } else {
10989                 drm_WARN(&dev_priv->drm, 1, "Invalid port %x\n", port);
10990                 return;
10991         }
10992
10993         pipe_config->icl_port_dplls[port_dpll_id].pll =
10994                 intel_get_shared_dpll_by_id(dev_priv, id);
10995
10996         icl_set_active_port_dpll(pipe_config, port_dpll_id);
10997 }
10998
10999 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
11000                                 enum port port,
11001                                 struct intel_crtc_state *pipe_config)
11002 {
11003         enum intel_dpll_id id;
11004
11005         switch (port) {
11006         case PORT_A:
11007                 id = DPLL_ID_SKL_DPLL0;
11008                 break;
11009         case PORT_B:
11010                 id = DPLL_ID_SKL_DPLL1;
11011                 break;
11012         case PORT_C:
11013                 id = DPLL_ID_SKL_DPLL2;
11014                 break;
11015         default:
11016                 drm_err(&dev_priv->drm, "Incorrect port type\n");
11017                 return;
11018         }
11019
11020         pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11021 }
11022
11023 static void skl_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11024                             struct intel_crtc_state *pipe_config)
11025 {
11026         enum intel_dpll_id id;
11027         u32 temp;
11028
11029         temp = intel_de_read(dev_priv, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
11030         id = temp >> (port * 3 + 1);
11031
11032         if (drm_WARN_ON(&dev_priv->drm, id < SKL_DPLL0 || id > SKL_DPLL3))
11033                 return;
11034
11035         pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11036 }
11037
11038 static void hsw_get_ddi_pll(struct drm_i915_private *dev_priv, enum port port,
11039                             struct intel_crtc_state *pipe_config)
11040 {
11041         enum intel_dpll_id id;
11042         u32 ddi_pll_sel = intel_de_read(dev_priv, PORT_CLK_SEL(port));
11043
11044         switch (ddi_pll_sel) {
11045         case PORT_CLK_SEL_WRPLL1:
11046                 id = DPLL_ID_WRPLL1;
11047                 break;
11048         case PORT_CLK_SEL_WRPLL2:
11049                 id = DPLL_ID_WRPLL2;
11050                 break;
11051         case PORT_CLK_SEL_SPLL:
11052                 id = DPLL_ID_SPLL;
11053                 break;
11054         case PORT_CLK_SEL_LCPLL_810:
11055                 id = DPLL_ID_LCPLL_810;
11056                 break;
11057         case PORT_CLK_SEL_LCPLL_1350:
11058                 id = DPLL_ID_LCPLL_1350;
11059                 break;
11060         case PORT_CLK_SEL_LCPLL_2700:
11061                 id = DPLL_ID_LCPLL_2700;
11062                 break;
11063         default:
11064                 MISSING_CASE(ddi_pll_sel);
11065                 fallthrough;
11066         case PORT_CLK_SEL_NONE:
11067                 return;
11068         }
11069
11070         pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
11071 }
11072
11073 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
11074                                      struct intel_crtc_state *pipe_config,
11075                                      u64 *power_domain_mask,
11076                                      intel_wakeref_t *wakerefs)
11077 {
11078         struct drm_device *dev = crtc->base.dev;
11079         struct drm_i915_private *dev_priv = to_i915(dev);
11080         enum intel_display_power_domain power_domain;
11081         unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
11082         unsigned long enabled_panel_transcoders = 0;
11083         enum transcoder panel_transcoder;
11084         intel_wakeref_t wf;
11085         u32 tmp;
11086
11087         if (INTEL_GEN(dev_priv) >= 11)
11088                 panel_transcoder_mask |=
11089                         BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
11090
11091         /*
11092          * The pipe->transcoder mapping is fixed with the exception of the eDP
11093          * and DSI transcoders handled below.
11094          */
11095         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
11096
11097         /*
11098          * XXX: Do intel_display_power_get_if_enabled before reading this (for
11099          * consistency and less surprising code; it's in always on power).
11100          */
11101         for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
11102                                        panel_transcoder_mask) {
11103                 bool force_thru = false;
11104                 enum pipe trans_pipe;
11105
11106                 tmp = intel_de_read(dev_priv,
11107                                     TRANS_DDI_FUNC_CTL(panel_transcoder));
11108                 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
11109                         continue;
11110
11111                 /*
11112                  * Log all enabled ones, only use the first one.
11113                  *
11114                  * FIXME: This won't work for two separate DSI displays.
11115                  */
11116                 enabled_panel_transcoders |= BIT(panel_transcoder);
11117                 if (enabled_panel_transcoders != BIT(panel_transcoder))
11118                         continue;
11119
11120                 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
11121                 default:
11122                         drm_WARN(dev, 1,
11123                                  "unknown pipe linked to transcoder %s\n",
11124                                  transcoder_name(panel_transcoder));
11125                         fallthrough;
11126                 case TRANS_DDI_EDP_INPUT_A_ONOFF:
11127                         force_thru = true;
11128                         fallthrough;
11129                 case TRANS_DDI_EDP_INPUT_A_ON:
11130                         trans_pipe = PIPE_A;
11131                         break;
11132                 case TRANS_DDI_EDP_INPUT_B_ONOFF:
11133                         trans_pipe = PIPE_B;
11134                         break;
11135                 case TRANS_DDI_EDP_INPUT_C_ONOFF:
11136                         trans_pipe = PIPE_C;
11137                         break;
11138                 case TRANS_DDI_EDP_INPUT_D_ONOFF:
11139                         trans_pipe = PIPE_D;
11140                         break;
11141                 }
11142
11143                 if (trans_pipe == crtc->pipe) {
11144                         pipe_config->cpu_transcoder = panel_transcoder;
11145                         pipe_config->pch_pfit.force_thru = force_thru;
11146                 }
11147         }
11148
11149         /*
11150          * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
11151          */
11152         drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
11153                     enabled_panel_transcoders != BIT(TRANSCODER_EDP));
11154
11155         power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
11156         drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
11157
11158         wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11159         if (!wf)
11160                 return false;
11161
11162         wakerefs[power_domain] = wf;
11163         *power_domain_mask |= BIT_ULL(power_domain);
11164
11165         tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
11166
11167         return tmp & PIPECONF_ENABLE;
11168 }
11169
11170 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
11171                                          struct intel_crtc_state *pipe_config,
11172                                          u64 *power_domain_mask,
11173                                          intel_wakeref_t *wakerefs)
11174 {
11175         struct drm_device *dev = crtc->base.dev;
11176         struct drm_i915_private *dev_priv = to_i915(dev);
11177         enum intel_display_power_domain power_domain;
11178         enum transcoder cpu_transcoder;
11179         intel_wakeref_t wf;
11180         enum port port;
11181         u32 tmp;
11182
11183         for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
11184                 if (port == PORT_A)
11185                         cpu_transcoder = TRANSCODER_DSI_A;
11186                 else
11187                         cpu_transcoder = TRANSCODER_DSI_C;
11188
11189                 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
11190                 drm_WARN_ON(dev, *power_domain_mask & BIT_ULL(power_domain));
11191
11192                 wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11193                 if (!wf)
11194                         continue;
11195
11196                 wakerefs[power_domain] = wf;
11197                 *power_domain_mask |= BIT_ULL(power_domain);
11198
11199                 /*
11200                  * The PLL needs to be enabled with a valid divider
11201                  * configuration, otherwise accessing DSI registers will hang
11202                  * the machine. See BSpec North Display Engine
11203                  * registers/MIPI[BXT]. We can break out here early, since we
11204                  * need the same DSI PLL to be enabled for both DSI ports.
11205                  */
11206                 if (!bxt_dsi_pll_is_enabled(dev_priv))
11207                         break;
11208
11209                 /* XXX: this works for video mode only */
11210                 tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
11211                 if (!(tmp & DPI_ENABLE))
11212                         continue;
11213
11214                 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
11215                 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
11216                         continue;
11217
11218                 pipe_config->cpu_transcoder = cpu_transcoder;
11219                 break;
11220         }
11221
11222         return transcoder_is_dsi(pipe_config->cpu_transcoder);
11223 }
11224
11225 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
11226                                    struct intel_crtc_state *pipe_config)
11227 {
11228         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11229         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
11230         struct intel_shared_dpll *pll;
11231         enum port port;
11232         u32 tmp;
11233
11234         if (transcoder_is_dsi(cpu_transcoder)) {
11235                 port = (cpu_transcoder == TRANSCODER_DSI_A) ?
11236                                                 PORT_A : PORT_B;
11237         } else {
11238                 tmp = intel_de_read(dev_priv,
11239                                     TRANS_DDI_FUNC_CTL(cpu_transcoder));
11240                 if (INTEL_GEN(dev_priv) >= 12)
11241                         port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11242                 else
11243                         port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
11244         }
11245
11246         if (IS_DG1(dev_priv))
11247                 dg1_get_ddi_pll(dev_priv, port, pipe_config);
11248         else if (INTEL_GEN(dev_priv) >= 11)
11249                 icl_get_ddi_pll(dev_priv, port, pipe_config);
11250         else if (IS_CANNONLAKE(dev_priv))
11251                 cnl_get_ddi_pll(dev_priv, port, pipe_config);
11252         else if (IS_GEN9_BC(dev_priv))
11253                 skl_get_ddi_pll(dev_priv, port, pipe_config);
11254         else if (IS_GEN9_LP(dev_priv))
11255                 bxt_get_ddi_pll(dev_priv, port, pipe_config);
11256         else
11257                 hsw_get_ddi_pll(dev_priv, port, pipe_config);
11258
11259         pll = pipe_config->shared_dpll;
11260         if (pll) {
11261                 drm_WARN_ON(&dev_priv->drm,
11262                             !pll->info->funcs->get_hw_state(dev_priv, pll,
11263                                                 &pipe_config->dpll_hw_state));
11264         }
11265
11266         /*
11267          * Haswell has only FDI/PCH transcoder A. It is which is connected to
11268          * DDI E. So just check whether this pipe is wired to DDI E and whether
11269          * the PCH transcoder is on.
11270          */
11271         if (INTEL_GEN(dev_priv) < 9 &&
11272             (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
11273                 pipe_config->has_pch_encoder = true;
11274
11275                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
11276                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
11277                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
11278
11279                 ilk_get_fdi_m_n_config(crtc, pipe_config);
11280         }
11281 }
11282
11283 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
11284                                 struct intel_crtc_state *pipe_config)
11285 {
11286         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11287         intel_wakeref_t wakerefs[POWER_DOMAIN_NUM], wf;
11288         enum intel_display_power_domain power_domain;
11289         u64 power_domain_mask;
11290         bool active;
11291         u32 tmp;
11292
11293         pipe_config->master_transcoder = INVALID_TRANSCODER;
11294
11295         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
11296         wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11297         if (!wf)
11298                 return false;
11299
11300         wakerefs[power_domain] = wf;
11301         power_domain_mask = BIT_ULL(power_domain);
11302
11303         pipe_config->shared_dpll = NULL;
11304
11305         active = hsw_get_transcoder_state(crtc, pipe_config,
11306                                           &power_domain_mask, wakerefs);
11307
11308         if (IS_GEN9_LP(dev_priv) &&
11309             bxt_get_dsi_transcoder_state(crtc, pipe_config,
11310                                          &power_domain_mask, wakerefs)) {
11311                 drm_WARN_ON(&dev_priv->drm, active);
11312                 active = true;
11313         }
11314
11315         if (!active)
11316                 goto out;
11317
11318         if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
11319             INTEL_GEN(dev_priv) >= 11) {
11320                 hsw_get_ddi_port_state(crtc, pipe_config);
11321                 intel_get_transcoder_timings(crtc, pipe_config);
11322         }
11323
11324         intel_get_pipe_src_size(crtc, pipe_config);
11325
11326         if (IS_HASWELL(dev_priv)) {
11327                 u32 tmp = intel_de_read(dev_priv,
11328                                         PIPECONF(pipe_config->cpu_transcoder));
11329
11330                 if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
11331                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
11332                 else
11333                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
11334         } else {
11335                 pipe_config->output_format =
11336                         bdw_get_pipemisc_output_format(crtc);
11337         }
11338
11339         pipe_config->gamma_mode = intel_de_read(dev_priv,
11340                                                 GAMMA_MODE(crtc->pipe));
11341
11342         pipe_config->csc_mode = intel_de_read(dev_priv,
11343                                               PIPE_CSC_MODE(crtc->pipe));
11344
11345         if (INTEL_GEN(dev_priv) >= 9) {
11346                 tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
11347
11348                 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
11349                         pipe_config->gamma_enable = true;
11350
11351                 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
11352                         pipe_config->csc_enable = true;
11353         } else {
11354                 i9xx_get_pipe_color_config(pipe_config);
11355         }
11356
11357         intel_color_get_config(pipe_config);
11358
11359         tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
11360         pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
11361         if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
11362                 pipe_config->ips_linetime =
11363                         REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
11364
11365         power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
11366         drm_WARN_ON(&dev_priv->drm, power_domain_mask & BIT_ULL(power_domain));
11367
11368         wf = intel_display_power_get_if_enabled(dev_priv, power_domain);
11369         if (wf) {
11370                 wakerefs[power_domain] = wf;
11371                 power_domain_mask |= BIT_ULL(power_domain);
11372
11373                 if (INTEL_GEN(dev_priv) >= 9)
11374                         skl_get_pfit_config(pipe_config);
11375                 else
11376                         ilk_get_pfit_config(pipe_config);
11377         }
11378
11379         if (hsw_crtc_supports_ips(crtc)) {
11380                 if (IS_HASWELL(dev_priv))
11381                         pipe_config->ips_enabled = intel_de_read(dev_priv,
11382                                                                  IPS_CTL) & IPS_ENABLE;
11383                 else {
11384                         /*
11385                          * We cannot readout IPS state on broadwell, set to
11386                          * true so we can set it to a defined state on first
11387                          * commit.
11388                          */
11389                         pipe_config->ips_enabled = true;
11390                 }
11391         }
11392
11393         if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
11394             !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
11395                 pipe_config->pixel_multiplier =
11396                         intel_de_read(dev_priv,
11397                                       PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
11398         } else {
11399                 pipe_config->pixel_multiplier = 1;
11400         }
11401
11402 out:
11403         for_each_power_domain(power_domain, power_domain_mask)
11404                 intel_display_power_put(dev_priv,
11405                                         power_domain, wakerefs[power_domain]);
11406
11407         return active;
11408 }
11409
11410 static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
11411 {
11412         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11413         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
11414
11415         if (!i915->display.get_pipe_config(crtc, crtc_state))
11416                 return false;
11417
11418         crtc_state->hw.active = true;
11419
11420         return true;
11421 }
11422
11423 static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
11424 {
11425         struct drm_i915_private *dev_priv =
11426                 to_i915(plane_state->uapi.plane->dev);
11427         const struct drm_framebuffer *fb = plane_state->hw.fb;
11428         const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11429         u32 base;
11430
11431         if (INTEL_INFO(dev_priv)->display.cursor_needs_physical)
11432                 base = sg_dma_address(obj->mm.pages->sgl);
11433         else
11434                 base = intel_plane_ggtt_offset(plane_state);
11435
11436         return base + plane_state->color_plane[0].offset;
11437 }
11438
11439 static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
11440 {
11441         int x = plane_state->uapi.dst.x1;
11442         int y = plane_state->uapi.dst.y1;
11443         u32 pos = 0;
11444
11445         if (x < 0) {
11446                 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
11447                 x = -x;
11448         }
11449         pos |= x << CURSOR_X_SHIFT;
11450
11451         if (y < 0) {
11452                 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
11453                 y = -y;
11454         }
11455         pos |= y << CURSOR_Y_SHIFT;
11456
11457         return pos;
11458 }
11459
11460 static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
11461 {
11462         const struct drm_mode_config *config =
11463                 &plane_state->uapi.plane->dev->mode_config;
11464         int width = drm_rect_width(&plane_state->uapi.dst);
11465         int height = drm_rect_height(&plane_state->uapi.dst);
11466
11467         return width > 0 && width <= config->cursor_width &&
11468                 height > 0 && height <= config->cursor_height;
11469 }
11470
11471 static int intel_cursor_check_surface(struct intel_plane_state *plane_state)
11472 {
11473         struct drm_i915_private *dev_priv =
11474                 to_i915(plane_state->uapi.plane->dev);
11475         unsigned int rotation = plane_state->hw.rotation;
11476         int src_x, src_y;
11477         u32 offset;
11478         int ret;
11479
11480         ret = intel_plane_compute_gtt(plane_state);
11481         if (ret)
11482                 return ret;
11483
11484         if (!plane_state->uapi.visible)
11485                 return 0;
11486
11487         src_x = plane_state->uapi.src.x1 >> 16;
11488         src_y = plane_state->uapi.src.y1 >> 16;
11489
11490         intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
11491         offset = intel_plane_compute_aligned_offset(&src_x, &src_y,
11492                                                     plane_state, 0);
11493
11494         if (src_x != 0 || src_y != 0) {
11495                 drm_dbg_kms(&dev_priv->drm,
11496                             "Arbitrary cursor panning not supported\n");
11497                 return -EINVAL;
11498         }
11499
11500         /*
11501          * Put the final coordinates back so that the src
11502          * coordinate checks will see the right values.
11503          */
11504         drm_rect_translate_to(&plane_state->uapi.src,
11505                               src_x << 16, src_y << 16);
11506
11507         /* ILK+ do this automagically in hardware */
11508         if (HAS_GMCH(dev_priv) && rotation & DRM_MODE_ROTATE_180) {
11509                 const struct drm_framebuffer *fb = plane_state->hw.fb;
11510                 int src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
11511                 int src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
11512
11513                 offset += (src_h * src_w - 1) * fb->format->cpp[0];
11514         }
11515
11516         plane_state->color_plane[0].offset = offset;
11517         plane_state->color_plane[0].x = src_x;
11518         plane_state->color_plane[0].y = src_y;
11519
11520         return 0;
11521 }
11522
11523 static int intel_check_cursor(struct intel_crtc_state *crtc_state,
11524                               struct intel_plane_state *plane_state)
11525 {
11526         const struct drm_framebuffer *fb = plane_state->hw.fb;
11527         struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11528         int ret;
11529
11530         if (fb && fb->modifier != DRM_FORMAT_MOD_LINEAR) {
11531                 drm_dbg_kms(&i915->drm, "cursor cannot be tiled\n");
11532                 return -EINVAL;
11533         }
11534
11535         ret = drm_atomic_helper_check_plane_state(&plane_state->uapi,
11536                                                   &crtc_state->uapi,
11537                                                   DRM_PLANE_HELPER_NO_SCALING,
11538                                                   DRM_PLANE_HELPER_NO_SCALING,
11539                                                   true, true);
11540         if (ret)
11541                 return ret;
11542
11543         /* Use the unclipped src/dst rectangles, which we program to hw */
11544         plane_state->uapi.src = drm_plane_state_src(&plane_state->uapi);
11545         plane_state->uapi.dst = drm_plane_state_dest(&plane_state->uapi);
11546
11547         ret = intel_cursor_check_surface(plane_state);
11548         if (ret)
11549                 return ret;
11550
11551         if (!plane_state->uapi.visible)
11552                 return 0;
11553
11554         ret = intel_plane_check_src_coordinates(plane_state);
11555         if (ret)
11556                 return ret;
11557
11558         return 0;
11559 }
11560
11561 static unsigned int
11562 i845_cursor_max_stride(struct intel_plane *plane,
11563                        u32 pixel_format, u64 modifier,
11564                        unsigned int rotation)
11565 {
11566         return 2048;
11567 }
11568
11569 static u32 i845_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11570 {
11571         u32 cntl = 0;
11572
11573         if (crtc_state->gamma_enable)
11574                 cntl |= CURSOR_GAMMA_ENABLE;
11575
11576         return cntl;
11577 }
11578
11579 static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
11580                            const struct intel_plane_state *plane_state)
11581 {
11582         return CURSOR_ENABLE |
11583                 CURSOR_FORMAT_ARGB |
11584                 CURSOR_STRIDE(plane_state->color_plane[0].stride);
11585 }
11586
11587 static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
11588 {
11589         int width = drm_rect_width(&plane_state->uapi.dst);
11590
11591         /*
11592          * 845g/865g are only limited by the width of their cursors,
11593          * the height is arbitrary up to the precision of the register.
11594          */
11595         return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
11596 }
11597
11598 static int i845_check_cursor(struct intel_crtc_state *crtc_state,
11599                              struct intel_plane_state *plane_state)
11600 {
11601         const struct drm_framebuffer *fb = plane_state->hw.fb;
11602         struct drm_i915_private *i915 = to_i915(plane_state->uapi.plane->dev);
11603         int ret;
11604
11605         ret = intel_check_cursor(crtc_state, plane_state);
11606         if (ret)
11607                 return ret;
11608
11609         /* if we want to turn off the cursor ignore width and height */
11610         if (!fb)
11611                 return 0;
11612
11613         /* Check for which cursor types we support */
11614         if (!i845_cursor_size_ok(plane_state)) {
11615                 drm_dbg_kms(&i915->drm,
11616                             "Cursor dimension %dx%d not supported\n",
11617                             drm_rect_width(&plane_state->uapi.dst),
11618                             drm_rect_height(&plane_state->uapi.dst));
11619                 return -EINVAL;
11620         }
11621
11622         drm_WARN_ON(&i915->drm, plane_state->uapi.visible &&
11623                     plane_state->color_plane[0].stride != fb->pitches[0]);
11624
11625         switch (fb->pitches[0]) {
11626         case 256:
11627         case 512:
11628         case 1024:
11629         case 2048:
11630                 break;
11631         default:
11632                  drm_dbg_kms(&i915->drm, "Invalid cursor stride (%u)\n",
11633                              fb->pitches[0]);
11634                 return -EINVAL;
11635         }
11636
11637         plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
11638
11639         return 0;
11640 }
11641
11642 static void i845_update_cursor(struct intel_plane *plane,
11643                                const struct intel_crtc_state *crtc_state,
11644                                const struct intel_plane_state *plane_state)
11645 {
11646         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11647         u32 cntl = 0, base = 0, pos = 0, size = 0;
11648         unsigned long irqflags;
11649
11650         if (plane_state && plane_state->uapi.visible) {
11651                 unsigned int width = drm_rect_width(&plane_state->uapi.dst);
11652                 unsigned int height = drm_rect_height(&plane_state->uapi.dst);
11653
11654                 cntl = plane_state->ctl |
11655                         i845_cursor_ctl_crtc(crtc_state);
11656
11657                 size = (height << 12) | width;
11658
11659                 base = intel_cursor_base(plane_state);
11660                 pos = intel_cursor_position(plane_state);
11661         }
11662
11663         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11664
11665         /* On these chipsets we can only modify the base/size/stride
11666          * whilst the cursor is disabled.
11667          */
11668         if (plane->cursor.base != base ||
11669             plane->cursor.size != size ||
11670             plane->cursor.cntl != cntl) {
11671                 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), 0);
11672                 intel_de_write_fw(dev_priv, CURBASE(PIPE_A), base);
11673                 intel_de_write_fw(dev_priv, CURSIZE, size);
11674                 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11675                 intel_de_write_fw(dev_priv, CURCNTR(PIPE_A), cntl);
11676
11677                 plane->cursor.base = base;
11678                 plane->cursor.size = size;
11679                 plane->cursor.cntl = cntl;
11680         } else {
11681                 intel_de_write_fw(dev_priv, CURPOS(PIPE_A), pos);
11682         }
11683
11684         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11685 }
11686
11687 static void i845_disable_cursor(struct intel_plane *plane,
11688                                 const struct intel_crtc_state *crtc_state)
11689 {
11690         i845_update_cursor(plane, crtc_state, NULL);
11691 }
11692
11693 static bool i845_cursor_get_hw_state(struct intel_plane *plane,
11694                                      enum pipe *pipe)
11695 {
11696         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11697         enum intel_display_power_domain power_domain;
11698         intel_wakeref_t wakeref;
11699         bool ret;
11700
11701         power_domain = POWER_DOMAIN_PIPE(PIPE_A);
11702         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11703         if (!wakeref)
11704                 return false;
11705
11706         ret = intel_de_read(dev_priv, CURCNTR(PIPE_A)) & CURSOR_ENABLE;
11707
11708         *pipe = PIPE_A;
11709
11710         intel_display_power_put(dev_priv, power_domain, wakeref);
11711
11712         return ret;
11713 }
11714
11715 static unsigned int
11716 i9xx_cursor_max_stride(struct intel_plane *plane,
11717                        u32 pixel_format, u64 modifier,
11718                        unsigned int rotation)
11719 {
11720         return plane->base.dev->mode_config.cursor_width * 4;
11721 }
11722
11723 static u32 i9xx_cursor_ctl_crtc(const struct intel_crtc_state *crtc_state)
11724 {
11725         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11726         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11727         u32 cntl = 0;
11728
11729         if (INTEL_GEN(dev_priv) >= 11)
11730                 return cntl;
11731
11732         if (crtc_state->gamma_enable)
11733                 cntl = MCURSOR_GAMMA_ENABLE;
11734
11735         if (crtc_state->csc_enable)
11736                 cntl |= MCURSOR_PIPE_CSC_ENABLE;
11737
11738         if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
11739                 cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
11740
11741         return cntl;
11742 }
11743
11744 static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
11745                            const struct intel_plane_state *plane_state)
11746 {
11747         struct drm_i915_private *dev_priv =
11748                 to_i915(plane_state->uapi.plane->dev);
11749         u32 cntl = 0;
11750
11751         if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
11752                 cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
11753
11754         switch (drm_rect_width(&plane_state->uapi.dst)) {
11755         case 64:
11756                 cntl |= MCURSOR_MODE_64_ARGB_AX;
11757                 break;
11758         case 128:
11759                 cntl |= MCURSOR_MODE_128_ARGB_AX;
11760                 break;
11761         case 256:
11762                 cntl |= MCURSOR_MODE_256_ARGB_AX;
11763                 break;
11764         default:
11765                 MISSING_CASE(drm_rect_width(&plane_state->uapi.dst));
11766                 return 0;
11767         }
11768
11769         if (plane_state->hw.rotation & DRM_MODE_ROTATE_180)
11770                 cntl |= MCURSOR_ROTATE_180;
11771
11772         return cntl;
11773 }
11774
11775 static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
11776 {
11777         struct drm_i915_private *dev_priv =
11778                 to_i915(plane_state->uapi.plane->dev);
11779         int width = drm_rect_width(&plane_state->uapi.dst);
11780         int height = drm_rect_height(&plane_state->uapi.dst);
11781
11782         if (!intel_cursor_size_ok(plane_state))
11783                 return false;
11784
11785         /* Cursor width is limited to a few power-of-two sizes */
11786         switch (width) {
11787         case 256:
11788         case 128:
11789         case 64:
11790                 break;
11791         default:
11792                 return false;
11793         }
11794
11795         /*
11796          * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
11797          * height from 8 lines up to the cursor width, when the
11798          * cursor is not rotated. Everything else requires square
11799          * cursors.
11800          */
11801         if (HAS_CUR_FBC(dev_priv) &&
11802             plane_state->hw.rotation & DRM_MODE_ROTATE_0) {
11803                 if (height < 8 || height > width)
11804                         return false;
11805         } else {
11806                 if (height != width)
11807                         return false;
11808         }
11809
11810         return true;
11811 }
11812
11813 static int i9xx_check_cursor(struct intel_crtc_state *crtc_state,
11814                              struct intel_plane_state *plane_state)
11815 {
11816         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11817         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11818         const struct drm_framebuffer *fb = plane_state->hw.fb;
11819         enum pipe pipe = plane->pipe;
11820         int ret;
11821
11822         ret = intel_check_cursor(crtc_state, plane_state);
11823         if (ret)
11824                 return ret;
11825
11826         /* if we want to turn off the cursor ignore width and height */
11827         if (!fb)
11828                 return 0;
11829
11830         /* Check for which cursor types we support */
11831         if (!i9xx_cursor_size_ok(plane_state)) {
11832                 drm_dbg(&dev_priv->drm,
11833                         "Cursor dimension %dx%d not supported\n",
11834                         drm_rect_width(&plane_state->uapi.dst),
11835                         drm_rect_height(&plane_state->uapi.dst));
11836                 return -EINVAL;
11837         }
11838
11839         drm_WARN_ON(&dev_priv->drm, plane_state->uapi.visible &&
11840                     plane_state->color_plane[0].stride != fb->pitches[0]);
11841
11842         if (fb->pitches[0] !=
11843             drm_rect_width(&plane_state->uapi.dst) * fb->format->cpp[0]) {
11844                 drm_dbg_kms(&dev_priv->drm,
11845                             "Invalid cursor stride (%u) (cursor width %d)\n",
11846                             fb->pitches[0],
11847                             drm_rect_width(&plane_state->uapi.dst));
11848                 return -EINVAL;
11849         }
11850
11851         /*
11852          * There's something wrong with the cursor on CHV pipe C.
11853          * If it straddles the left edge of the screen then
11854          * moving it away from the edge or disabling it often
11855          * results in a pipe underrun, and often that can lead to
11856          * dead pipe (constant underrun reported, and it scans
11857          * out just a solid color). To recover from that, the
11858          * display power well must be turned off and on again.
11859          * Refuse the put the cursor into that compromised position.
11860          */
11861         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
11862             plane_state->uapi.visible && plane_state->uapi.dst.x1 < 0) {
11863                 drm_dbg_kms(&dev_priv->drm,
11864                             "CHV cursor C not allowed to straddle the left screen edge\n");
11865                 return -EINVAL;
11866         }
11867
11868         plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
11869
11870         return 0;
11871 }
11872
11873 static void i9xx_update_cursor(struct intel_plane *plane,
11874                                const struct intel_crtc_state *crtc_state,
11875                                const struct intel_plane_state *plane_state)
11876 {
11877         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11878         enum pipe pipe = plane->pipe;
11879         u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
11880         unsigned long irqflags;
11881
11882         if (plane_state && plane_state->uapi.visible) {
11883                 unsigned width = drm_rect_width(&plane_state->uapi.dst);
11884                 unsigned height = drm_rect_height(&plane_state->uapi.dst);
11885
11886                 cntl = plane_state->ctl |
11887                         i9xx_cursor_ctl_crtc(crtc_state);
11888
11889                 if (width != height)
11890                         fbc_ctl = CUR_FBC_CTL_EN | (height - 1);
11891
11892                 base = intel_cursor_base(plane_state);
11893                 pos = intel_cursor_position(plane_state);
11894         }
11895
11896         spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
11897
11898         /*
11899          * On some platforms writing CURCNTR first will also
11900          * cause CURPOS to be armed by the CURBASE write.
11901          * Without the CURCNTR write the CURPOS write would
11902          * arm itself. Thus we always update CURCNTR before
11903          * CURPOS.
11904          *
11905          * On other platforms CURPOS always requires the
11906          * CURBASE write to arm the update. Additonally
11907          * a write to any of the cursor register will cancel
11908          * an already armed cursor update. Thus leaving out
11909          * the CURBASE write after CURPOS could lead to a
11910          * cursor that doesn't appear to move, or even change
11911          * shape. Thus we always write CURBASE.
11912          *
11913          * The other registers are armed by by the CURBASE write
11914          * except when the plane is getting enabled at which time
11915          * the CURCNTR write arms the update.
11916          */
11917
11918         if (INTEL_GEN(dev_priv) >= 9)
11919                 skl_write_cursor_wm(plane, crtc_state);
11920
11921         if (!needs_modeset(crtc_state))
11922                 intel_psr2_program_plane_sel_fetch(plane, crtc_state, plane_state, 0);
11923
11924         if (plane->cursor.base != base ||
11925             plane->cursor.size != fbc_ctl ||
11926             plane->cursor.cntl != cntl) {
11927                 if (HAS_CUR_FBC(dev_priv))
11928                         intel_de_write_fw(dev_priv, CUR_FBC_CTL(pipe),
11929                                           fbc_ctl);
11930                 intel_de_write_fw(dev_priv, CURCNTR(pipe), cntl);
11931                 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11932                 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11933
11934                 plane->cursor.base = base;
11935                 plane->cursor.size = fbc_ctl;
11936                 plane->cursor.cntl = cntl;
11937         } else {
11938                 intel_de_write_fw(dev_priv, CURPOS(pipe), pos);
11939                 intel_de_write_fw(dev_priv, CURBASE(pipe), base);
11940         }
11941
11942         spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
11943 }
11944
11945 static void i9xx_disable_cursor(struct intel_plane *plane,
11946                                 const struct intel_crtc_state *crtc_state)
11947 {
11948         i9xx_update_cursor(plane, crtc_state, NULL);
11949 }
11950
11951 static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
11952                                      enum pipe *pipe)
11953 {
11954         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11955         enum intel_display_power_domain power_domain;
11956         intel_wakeref_t wakeref;
11957         bool ret;
11958         u32 val;
11959
11960         /*
11961          * Not 100% correct for planes that can move between pipes,
11962          * but that's only the case for gen2-3 which don't have any
11963          * display power wells.
11964          */
11965         power_domain = POWER_DOMAIN_PIPE(plane->pipe);
11966         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
11967         if (!wakeref)
11968                 return false;
11969
11970         val = intel_de_read(dev_priv, CURCNTR(plane->pipe));
11971
11972         ret = val & MCURSOR_MODE;
11973
11974         if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
11975                 *pipe = plane->pipe;
11976         else
11977                 *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
11978                         MCURSOR_PIPE_SELECT_SHIFT;
11979
11980         intel_display_power_put(dev_priv, power_domain, wakeref);
11981
11982         return ret;
11983 }
11984
11985 /* VESA 640x480x72Hz mode to set on the pipe */
11986 static const struct drm_display_mode load_detect_mode = {
11987         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
11988                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
11989 };
11990
11991 struct drm_framebuffer *
11992 intel_framebuffer_create(struct drm_i915_gem_object *obj,
11993                          struct drm_mode_fb_cmd2 *mode_cmd)
11994 {
11995         struct intel_framebuffer *intel_fb;
11996         int ret;
11997
11998         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
11999         if (!intel_fb)
12000                 return ERR_PTR(-ENOMEM);
12001
12002         ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
12003         if (ret)
12004                 goto err;
12005
12006         return &intel_fb->base;
12007
12008 err:
12009         kfree(intel_fb);
12010         return ERR_PTR(ret);
12011 }
12012
12013 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
12014                                         struct drm_crtc *crtc)
12015 {
12016         struct drm_plane *plane;
12017         struct drm_plane_state *plane_state;
12018         int ret, i;
12019
12020         ret = drm_atomic_add_affected_planes(state, crtc);
12021         if (ret)
12022                 return ret;
12023
12024         for_each_new_plane_in_state(state, plane, plane_state, i) {
12025                 if (plane_state->crtc != crtc)
12026                         continue;
12027
12028                 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
12029                 if (ret)
12030                         return ret;
12031
12032                 drm_atomic_set_fb_for_plane(plane_state, NULL);
12033         }
12034
12035         return 0;
12036 }
12037
12038 int intel_get_load_detect_pipe(struct drm_connector *connector,
12039                                struct intel_load_detect_pipe *old,
12040                                struct drm_modeset_acquire_ctx *ctx)
12041 {
12042         struct intel_crtc *intel_crtc;
12043         struct intel_encoder *intel_encoder =
12044                 intel_attached_encoder(to_intel_connector(connector));
12045         struct drm_crtc *possible_crtc;
12046         struct drm_encoder *encoder = &intel_encoder->base;
12047         struct drm_crtc *crtc = NULL;
12048         struct drm_device *dev = encoder->dev;
12049         struct drm_i915_private *dev_priv = to_i915(dev);
12050         struct drm_mode_config *config = &dev->mode_config;
12051         struct drm_atomic_state *state = NULL, *restore_state = NULL;
12052         struct drm_connector_state *connector_state;
12053         struct intel_crtc_state *crtc_state;
12054         int ret, i = -1;
12055
12056         drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12057                     connector->base.id, connector->name,
12058                     encoder->base.id, encoder->name);
12059
12060         old->restore_state = NULL;
12061
12062         drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
12063
12064         /*
12065          * Algorithm gets a little messy:
12066          *
12067          *   - if the connector already has an assigned crtc, use it (but make
12068          *     sure it's on first)
12069          *
12070          *   - try to find the first unused crtc that can drive this connector,
12071          *     and use that if we find one
12072          */
12073
12074         /* See if we already have a CRTC for this connector */
12075         if (connector->state->crtc) {
12076                 crtc = connector->state->crtc;
12077
12078                 ret = drm_modeset_lock(&crtc->mutex, ctx);
12079                 if (ret)
12080                         goto fail;
12081
12082                 /* Make sure the crtc and connector are running */
12083                 goto found;
12084         }
12085
12086         /* Find an unused one (if possible) */
12087         for_each_crtc(dev, possible_crtc) {
12088                 i++;
12089                 if (!(encoder->possible_crtcs & (1 << i)))
12090                         continue;
12091
12092                 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
12093                 if (ret)
12094                         goto fail;
12095
12096                 if (possible_crtc->state->enable) {
12097                         drm_modeset_unlock(&possible_crtc->mutex);
12098                         continue;
12099                 }
12100
12101                 crtc = possible_crtc;
12102                 break;
12103         }
12104
12105         /*
12106          * If we didn't find an unused CRTC, don't use any.
12107          */
12108         if (!crtc) {
12109                 drm_dbg_kms(&dev_priv->drm,
12110                             "no pipe available for load-detect\n");
12111                 ret = -ENODEV;
12112                 goto fail;
12113         }
12114
12115 found:
12116         intel_crtc = to_intel_crtc(crtc);
12117
12118         state = drm_atomic_state_alloc(dev);
12119         restore_state = drm_atomic_state_alloc(dev);
12120         if (!state || !restore_state) {
12121                 ret = -ENOMEM;
12122                 goto fail;
12123         }
12124
12125         state->acquire_ctx = ctx;
12126         restore_state->acquire_ctx = ctx;
12127
12128         connector_state = drm_atomic_get_connector_state(state, connector);
12129         if (IS_ERR(connector_state)) {
12130                 ret = PTR_ERR(connector_state);
12131                 goto fail;
12132         }
12133
12134         ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
12135         if (ret)
12136                 goto fail;
12137
12138         crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12139         if (IS_ERR(crtc_state)) {
12140                 ret = PTR_ERR(crtc_state);
12141                 goto fail;
12142         }
12143
12144         crtc_state->uapi.active = true;
12145
12146         ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
12147                                            &load_detect_mode);
12148         if (ret)
12149                 goto fail;
12150
12151         ret = intel_modeset_disable_planes(state, crtc);
12152         if (ret)
12153                 goto fail;
12154
12155         ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
12156         if (!ret)
12157                 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
12158         if (!ret)
12159                 ret = drm_atomic_add_affected_planes(restore_state, crtc);
12160         if (ret) {
12161                 drm_dbg_kms(&dev_priv->drm,
12162                             "Failed to create a copy of old state to restore: %i\n",
12163                             ret);
12164                 goto fail;
12165         }
12166
12167         ret = drm_atomic_commit(state);
12168         if (ret) {
12169                 drm_dbg_kms(&dev_priv->drm,
12170                             "failed to set mode on load-detect pipe\n");
12171                 goto fail;
12172         }
12173
12174         old->restore_state = restore_state;
12175         drm_atomic_state_put(state);
12176
12177         /* let the connector get through one full cycle before testing */
12178         intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
12179         return true;
12180
12181 fail:
12182         if (state) {
12183                 drm_atomic_state_put(state);
12184                 state = NULL;
12185         }
12186         if (restore_state) {
12187                 drm_atomic_state_put(restore_state);
12188                 restore_state = NULL;
12189         }
12190
12191         if (ret == -EDEADLK)
12192                 return ret;
12193
12194         return false;
12195 }
12196
12197 void intel_release_load_detect_pipe(struct drm_connector *connector,
12198                                     struct intel_load_detect_pipe *old,
12199                                     struct drm_modeset_acquire_ctx *ctx)
12200 {
12201         struct intel_encoder *intel_encoder =
12202                 intel_attached_encoder(to_intel_connector(connector));
12203         struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
12204         struct drm_encoder *encoder = &intel_encoder->base;
12205         struct drm_atomic_state *state = old->restore_state;
12206         int ret;
12207
12208         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
12209                     connector->base.id, connector->name,
12210                     encoder->base.id, encoder->name);
12211
12212         if (!state)
12213                 return;
12214
12215         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
12216         if (ret)
12217                 drm_dbg_kms(&i915->drm,
12218                             "Couldn't release load detect pipe: %i\n", ret);
12219         drm_atomic_state_put(state);
12220 }
12221
12222 static int i9xx_pll_refclk(struct drm_device *dev,
12223                            const struct intel_crtc_state *pipe_config)
12224 {
12225         struct drm_i915_private *dev_priv = to_i915(dev);
12226         u32 dpll = pipe_config->dpll_hw_state.dpll;
12227
12228         if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
12229                 return dev_priv->vbt.lvds_ssc_freq;
12230         else if (HAS_PCH_SPLIT(dev_priv))
12231                 return 120000;
12232         else if (!IS_GEN(dev_priv, 2))
12233                 return 96000;
12234         else
12235                 return 48000;
12236 }
12237
12238 /* Returns the clock of the currently programmed mode of the given pipe. */
12239 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
12240                                 struct intel_crtc_state *pipe_config)
12241 {
12242         struct drm_device *dev = crtc->base.dev;
12243         struct drm_i915_private *dev_priv = to_i915(dev);
12244         enum pipe pipe = crtc->pipe;
12245         u32 dpll = pipe_config->dpll_hw_state.dpll;
12246         u32 fp;
12247         struct dpll clock;
12248         int port_clock;
12249         int refclk = i9xx_pll_refclk(dev, pipe_config);
12250
12251         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
12252                 fp = pipe_config->dpll_hw_state.fp0;
12253         else
12254                 fp = pipe_config->dpll_hw_state.fp1;
12255
12256         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
12257         if (IS_PINEVIEW(dev_priv)) {
12258                 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
12259                 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
12260         } else {
12261                 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
12262                 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
12263         }
12264
12265         if (!IS_GEN(dev_priv, 2)) {
12266                 if (IS_PINEVIEW(dev_priv))
12267                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
12268                                 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
12269                 else
12270                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
12271                                DPLL_FPA01_P1_POST_DIV_SHIFT);
12272
12273                 switch (dpll & DPLL_MODE_MASK) {
12274                 case DPLLB_MODE_DAC_SERIAL:
12275                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
12276                                 5 : 10;
12277                         break;
12278                 case DPLLB_MODE_LVDS:
12279                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
12280                                 7 : 14;
12281                         break;
12282                 default:
12283                         drm_dbg_kms(&dev_priv->drm,
12284                                     "Unknown DPLL mode %08x in programmed "
12285                                     "mode\n", (int)(dpll & DPLL_MODE_MASK));
12286                         return;
12287                 }
12288
12289                 if (IS_PINEVIEW(dev_priv))
12290                         port_clock = pnv_calc_dpll_params(refclk, &clock);
12291                 else
12292                         port_clock = i9xx_calc_dpll_params(refclk, &clock);
12293         } else {
12294                 u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
12295                                                                  LVDS);
12296                 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
12297
12298                 if (is_lvds) {
12299                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
12300                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
12301
12302                         if (lvds & LVDS_CLKB_POWER_UP)
12303                                 clock.p2 = 7;
12304                         else
12305                                 clock.p2 = 14;
12306                 } else {
12307                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
12308                                 clock.p1 = 2;
12309                         else {
12310                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
12311                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
12312                         }
12313                         if (dpll & PLL_P2_DIVIDE_BY_4)
12314                                 clock.p2 = 4;
12315                         else
12316                                 clock.p2 = 2;
12317                 }
12318
12319                 port_clock = i9xx_calc_dpll_params(refclk, &clock);
12320         }
12321
12322         /*
12323          * This value includes pixel_multiplier. We will use
12324          * port_clock to compute adjusted_mode.crtc_clock in the
12325          * encoder's get_config() function.
12326          */
12327         pipe_config->port_clock = port_clock;
12328 }
12329
12330 int intel_dotclock_calculate(int link_freq,
12331                              const struct intel_link_m_n *m_n)
12332 {
12333         /*
12334          * The calculation for the data clock is:
12335          * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
12336          * But we want to avoid losing precison if possible, so:
12337          * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
12338          *
12339          * and the link clock is simpler:
12340          * link_clock = (m * link_clock) / n
12341          */
12342
12343         if (!m_n->link_n)
12344                 return 0;
12345
12346         return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
12347 }
12348
12349 static void ilk_pch_clock_get(struct intel_crtc *crtc,
12350                               struct intel_crtc_state *pipe_config)
12351 {
12352         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12353
12354         /* read out port_clock from the DPLL */
12355         i9xx_crtc_clock_get(crtc, pipe_config);
12356
12357         /*
12358          * In case there is an active pipe without active ports,
12359          * we may need some idea for the dotclock anyway.
12360          * Calculate one based on the FDI configuration.
12361          */
12362         pipe_config->hw.adjusted_mode.crtc_clock =
12363                 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
12364                                          &pipe_config->fdi_m_n);
12365 }
12366
12367 static void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
12368                                    struct intel_crtc *crtc)
12369 {
12370         memset(crtc_state, 0, sizeof(*crtc_state));
12371
12372         __drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
12373
12374         crtc_state->cpu_transcoder = INVALID_TRANSCODER;
12375         crtc_state->master_transcoder = INVALID_TRANSCODER;
12376         crtc_state->hsw_workaround_pipe = INVALID_PIPE;
12377         crtc_state->output_format = INTEL_OUTPUT_FORMAT_INVALID;
12378         crtc_state->scaler_state.scaler_id = -1;
12379         crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
12380 }
12381
12382 static struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
12383 {
12384         struct intel_crtc_state *crtc_state;
12385
12386         crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
12387
12388         if (crtc_state)
12389                 intel_crtc_state_reset(crtc_state, crtc);
12390
12391         return crtc_state;
12392 }
12393
12394 /* Returns the currently programmed mode of the given encoder. */
12395 struct drm_display_mode *
12396 intel_encoder_current_mode(struct intel_encoder *encoder)
12397 {
12398         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12399         struct intel_crtc_state *crtc_state;
12400         struct drm_display_mode *mode;
12401         struct intel_crtc *crtc;
12402         enum pipe pipe;
12403
12404         if (!encoder->get_hw_state(encoder, &pipe))
12405                 return NULL;
12406
12407         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12408
12409         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
12410         if (!mode)
12411                 return NULL;
12412
12413         crtc_state = intel_crtc_state_alloc(crtc);
12414         if (!crtc_state) {
12415                 kfree(mode);
12416                 return NULL;
12417         }
12418
12419         if (!intel_crtc_get_pipe_config(crtc_state)) {
12420                 kfree(crtc_state);
12421                 kfree(mode);
12422                 return NULL;
12423         }
12424
12425         intel_encoder_get_config(encoder, crtc_state);
12426
12427         intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
12428
12429         kfree(crtc_state);
12430
12431         return mode;
12432 }
12433
12434 static void intel_crtc_destroy(struct drm_crtc *crtc)
12435 {
12436         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12437
12438         drm_crtc_cleanup(crtc);
12439         kfree(intel_crtc);
12440 }
12441
12442 /**
12443  * intel_wm_need_update - Check whether watermarks need updating
12444  * @cur: current plane state
12445  * @new: new plane state
12446  *
12447  * Check current plane state versus the new one to determine whether
12448  * watermarks need to be recalculated.
12449  *
12450  * Returns true or false.
12451  */
12452 static bool intel_wm_need_update(const struct intel_plane_state *cur,
12453                                  struct intel_plane_state *new)
12454 {
12455         /* Update watermarks on tiling or size changes. */
12456         if (new->uapi.visible != cur->uapi.visible)
12457                 return true;
12458
12459         if (!cur->hw.fb || !new->hw.fb)
12460                 return false;
12461
12462         if (cur->hw.fb->modifier != new->hw.fb->modifier ||
12463             cur->hw.rotation != new->hw.rotation ||
12464             drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
12465             drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
12466             drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
12467             drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
12468                 return true;
12469
12470         return false;
12471 }
12472
12473 static bool needs_scaling(const struct intel_plane_state *state)
12474 {
12475         int src_w = drm_rect_width(&state->uapi.src) >> 16;
12476         int src_h = drm_rect_height(&state->uapi.src) >> 16;
12477         int dst_w = drm_rect_width(&state->uapi.dst);
12478         int dst_h = drm_rect_height(&state->uapi.dst);
12479
12480         return (src_w != dst_w || src_h != dst_h);
12481 }
12482
12483 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
12484                                     struct intel_crtc_state *crtc_state,
12485                                     const struct intel_plane_state *old_plane_state,
12486                                     struct intel_plane_state *plane_state)
12487 {
12488         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12489         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
12490         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12491         bool mode_changed = needs_modeset(crtc_state);
12492         bool was_crtc_enabled = old_crtc_state->hw.active;
12493         bool is_crtc_enabled = crtc_state->hw.active;
12494         bool turn_off, turn_on, visible, was_visible;
12495         int ret;
12496
12497         if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
12498                 ret = skl_update_scaler_plane(crtc_state, plane_state);
12499                 if (ret)
12500                         return ret;
12501         }
12502
12503         was_visible = old_plane_state->uapi.visible;
12504         visible = plane_state->uapi.visible;
12505
12506         if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
12507                 was_visible = false;
12508
12509         /*
12510          * Visibility is calculated as if the crtc was on, but
12511          * after scaler setup everything depends on it being off
12512          * when the crtc isn't active.
12513          *
12514          * FIXME this is wrong for watermarks. Watermarks should also
12515          * be computed as if the pipe would be active. Perhaps move
12516          * per-plane wm computation to the .check_plane() hook, and
12517          * only combine the results from all planes in the current place?
12518          */
12519         if (!is_crtc_enabled) {
12520                 intel_plane_set_invisible(crtc_state, plane_state);
12521                 visible = false;
12522         }
12523
12524         if (!was_visible && !visible)
12525                 return 0;
12526
12527         turn_off = was_visible && (!visible || mode_changed);
12528         turn_on = visible && (!was_visible || mode_changed);
12529
12530         drm_dbg_atomic(&dev_priv->drm,
12531                        "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12532                        crtc->base.base.id, crtc->base.name,
12533                        plane->base.base.id, plane->base.name,
12534                        was_visible, visible,
12535                        turn_off, turn_on, mode_changed);
12536
12537         if (turn_on) {
12538                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12539                         crtc_state->update_wm_pre = true;
12540
12541                 /* must disable cxsr around plane enable/disable */
12542                 if (plane->id != PLANE_CURSOR)
12543                         crtc_state->disable_cxsr = true;
12544         } else if (turn_off) {
12545                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
12546                         crtc_state->update_wm_post = true;
12547
12548                 /* must disable cxsr around plane enable/disable */
12549                 if (plane->id != PLANE_CURSOR)
12550                         crtc_state->disable_cxsr = true;
12551         } else if (intel_wm_need_update(old_plane_state, plane_state)) {
12552                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
12553                         /* FIXME bollocks */
12554                         crtc_state->update_wm_pre = true;
12555                         crtc_state->update_wm_post = true;
12556                 }
12557         }
12558
12559         if (visible || was_visible)
12560                 crtc_state->fb_bits |= plane->frontbuffer_bit;
12561
12562         /*
12563          * ILK/SNB DVSACNTR/Sprite Enable
12564          * IVB SPR_CTL/Sprite Enable
12565          * "When in Self Refresh Big FIFO mode, a write to enable the
12566          *  plane will be internally buffered and delayed while Big FIFO
12567          *  mode is exiting."
12568          *
12569          * Which means that enabling the sprite can take an extra frame
12570          * when we start in big FIFO mode (LP1+). Thus we need to drop
12571          * down to LP0 and wait for vblank in order to make sure the
12572          * sprite gets enabled on the next vblank after the register write.
12573          * Doing otherwise would risk enabling the sprite one frame after
12574          * we've already signalled flip completion. We can resume LP1+
12575          * once the sprite has been enabled.
12576          *
12577          *
12578          * WaCxSRDisabledForSpriteScaling:ivb
12579          * IVB SPR_SCALE/Scaling Enable
12580          * "Low Power watermarks must be disabled for at least one
12581          *  frame before enabling sprite scaling, and kept disabled
12582          *  until sprite scaling is disabled."
12583          *
12584          * ILK/SNB DVSASCALE/Scaling Enable
12585          * "When in Self Refresh Big FIFO mode, scaling enable will be
12586          *  masked off while Big FIFO mode is exiting."
12587          *
12588          * Despite the w/a only being listed for IVB we assume that
12589          * the ILK/SNB note has similar ramifications, hence we apply
12590          * the w/a on all three platforms.
12591          *
12592          * With experimental results seems this is needed also for primary
12593          * plane, not only sprite plane.
12594          */
12595         if (plane->id != PLANE_CURSOR &&
12596             (IS_GEN_RANGE(dev_priv, 5, 6) ||
12597              IS_IVYBRIDGE(dev_priv)) &&
12598             (turn_on || (!needs_scaling(old_plane_state) &&
12599                          needs_scaling(plane_state))))
12600                 crtc_state->disable_lp_wm = true;
12601
12602         return 0;
12603 }
12604
12605 static bool encoders_cloneable(const struct intel_encoder *a,
12606                                const struct intel_encoder *b)
12607 {
12608         /* masks could be asymmetric, so check both ways */
12609         return a == b || (a->cloneable & (1 << b->type) &&
12610                           b->cloneable & (1 << a->type));
12611 }
12612
12613 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12614                                          struct intel_crtc *crtc,
12615                                          struct intel_encoder *encoder)
12616 {
12617         struct intel_encoder *source_encoder;
12618         struct drm_connector *connector;
12619         struct drm_connector_state *connector_state;
12620         int i;
12621
12622         for_each_new_connector_in_state(state, connector, connector_state, i) {
12623                 if (connector_state->crtc != &crtc->base)
12624                         continue;
12625
12626                 source_encoder =
12627                         to_intel_encoder(connector_state->best_encoder);
12628                 if (!encoders_cloneable(encoder, source_encoder))
12629                         return false;
12630         }
12631
12632         return true;
12633 }
12634
12635 static int icl_add_linked_planes(struct intel_atomic_state *state)
12636 {
12637         struct intel_plane *plane, *linked;
12638         struct intel_plane_state *plane_state, *linked_plane_state;
12639         int i;
12640
12641         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12642                 linked = plane_state->planar_linked_plane;
12643
12644                 if (!linked)
12645                         continue;
12646
12647                 linked_plane_state = intel_atomic_get_plane_state(state, linked);
12648                 if (IS_ERR(linked_plane_state))
12649                         return PTR_ERR(linked_plane_state);
12650
12651                 drm_WARN_ON(state->base.dev,
12652                             linked_plane_state->planar_linked_plane != plane);
12653                 drm_WARN_ON(state->base.dev,
12654                             linked_plane_state->planar_slave == plane_state->planar_slave);
12655         }
12656
12657         return 0;
12658 }
12659
12660 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
12661 {
12662         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12663         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12664         struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
12665         struct intel_plane *plane, *linked;
12666         struct intel_plane_state *plane_state;
12667         int i;
12668
12669         if (INTEL_GEN(dev_priv) < 11)
12670                 return 0;
12671
12672         /*
12673          * Destroy all old plane links and make the slave plane invisible
12674          * in the crtc_state->active_planes mask.
12675          */
12676         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12677                 if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
12678                         continue;
12679
12680                 plane_state->planar_linked_plane = NULL;
12681                 if (plane_state->planar_slave && !plane_state->uapi.visible) {
12682                         crtc_state->active_planes &= ~BIT(plane->id);
12683                         crtc_state->update_planes |= BIT(plane->id);
12684                 }
12685
12686                 plane_state->planar_slave = false;
12687         }
12688
12689         if (!crtc_state->nv12_planes)
12690                 return 0;
12691
12692         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
12693                 struct intel_plane_state *linked_state = NULL;
12694
12695                 if (plane->pipe != crtc->pipe ||
12696                     !(crtc_state->nv12_planes & BIT(plane->id)))
12697                         continue;
12698
12699                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
12700                         if (!icl_is_nv12_y_plane(dev_priv, linked->id))
12701                                 continue;
12702
12703                         if (crtc_state->active_planes & BIT(linked->id))
12704                                 continue;
12705
12706                         linked_state = intel_atomic_get_plane_state(state, linked);
12707                         if (IS_ERR(linked_state))
12708                                 return PTR_ERR(linked_state);
12709
12710                         break;
12711                 }
12712
12713                 if (!linked_state) {
12714                         drm_dbg_kms(&dev_priv->drm,
12715                                     "Need %d free Y planes for planar YUV\n",
12716                                     hweight8(crtc_state->nv12_planes));
12717
12718                         return -EINVAL;
12719                 }
12720
12721                 plane_state->planar_linked_plane = linked;
12722
12723                 linked_state->planar_slave = true;
12724                 linked_state->planar_linked_plane = plane;
12725                 crtc_state->active_planes |= BIT(linked->id);
12726                 crtc_state->update_planes |= BIT(linked->id);
12727                 drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
12728                             linked->base.name, plane->base.name);
12729
12730                 /* Copy parameters to slave plane */
12731                 linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
12732                 linked_state->color_ctl = plane_state->color_ctl;
12733                 linked_state->view = plane_state->view;
12734                 memcpy(linked_state->color_plane, plane_state->color_plane,
12735                        sizeof(linked_state->color_plane));
12736
12737                 intel_plane_copy_uapi_to_hw_state(linked_state, plane_state);
12738                 linked_state->uapi.src = plane_state->uapi.src;
12739                 linked_state->uapi.dst = plane_state->uapi.dst;
12740
12741                 if (icl_is_hdr_plane(dev_priv, plane->id)) {
12742                         if (linked->id == PLANE_SPRITE5)
12743                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
12744                         else if (linked->id == PLANE_SPRITE4)
12745                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
12746                         else if (linked->id == PLANE_SPRITE3)
12747                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
12748                         else if (linked->id == PLANE_SPRITE2)
12749                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
12750                         else
12751                                 MISSING_CASE(linked->id);
12752                 }
12753         }
12754
12755         return 0;
12756 }
12757
12758 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
12759 {
12760         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
12761         struct intel_atomic_state *state =
12762                 to_intel_atomic_state(new_crtc_state->uapi.state);
12763         const struct intel_crtc_state *old_crtc_state =
12764                 intel_atomic_get_old_crtc_state(state, crtc);
12765
12766         return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
12767 }
12768
12769 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
12770 {
12771         const struct drm_display_mode *adjusted_mode =
12772                 &crtc_state->hw.adjusted_mode;
12773         int linetime_wm;
12774
12775         if (!crtc_state->hw.enable)
12776                 return 0;
12777
12778         linetime_wm = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12779                                         adjusted_mode->crtc_clock);
12780
12781         return min(linetime_wm, 0x1ff);
12782 }
12783
12784 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
12785                                const struct intel_cdclk_state *cdclk_state)
12786 {
12787         const struct drm_display_mode *adjusted_mode =
12788                 &crtc_state->hw.adjusted_mode;
12789         int linetime_wm;
12790
12791         if (!crtc_state->hw.enable)
12792                 return 0;
12793
12794         linetime_wm = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
12795                                         cdclk_state->logical.cdclk);
12796
12797         return min(linetime_wm, 0x1ff);
12798 }
12799
12800 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
12801 {
12802         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12803         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12804         const struct drm_display_mode *adjusted_mode =
12805                 &crtc_state->hw.adjusted_mode;
12806         int linetime_wm;
12807
12808         if (!crtc_state->hw.enable)
12809                 return 0;
12810
12811         linetime_wm = DIV_ROUND_UP(adjusted_mode->crtc_htotal * 1000 * 8,
12812                                    crtc_state->pixel_rate);
12813
12814         /* Display WA #1135: BXT:ALL GLK:ALL */
12815         if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
12816                 linetime_wm /= 2;
12817
12818         return min(linetime_wm, 0x1ff);
12819 }
12820
12821 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
12822                                    struct intel_crtc *crtc)
12823 {
12824         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12825         struct intel_crtc_state *crtc_state =
12826                 intel_atomic_get_new_crtc_state(state, crtc);
12827         const struct intel_cdclk_state *cdclk_state;
12828
12829         if (INTEL_GEN(dev_priv) >= 9)
12830                 crtc_state->linetime = skl_linetime_wm(crtc_state);
12831         else
12832                 crtc_state->linetime = hsw_linetime_wm(crtc_state);
12833
12834         if (!hsw_crtc_supports_ips(crtc))
12835                 return 0;
12836
12837         cdclk_state = intel_atomic_get_cdclk_state(state);
12838         if (IS_ERR(cdclk_state))
12839                 return PTR_ERR(cdclk_state);
12840
12841         crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
12842                                                        cdclk_state);
12843
12844         return 0;
12845 }
12846
12847 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
12848                                    struct intel_crtc *crtc)
12849 {
12850         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12851         struct intel_crtc_state *crtc_state =
12852                 intel_atomic_get_new_crtc_state(state, crtc);
12853         bool mode_changed = needs_modeset(crtc_state);
12854         int ret;
12855
12856         if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
12857             mode_changed && !crtc_state->hw.active)
12858                 crtc_state->update_wm_post = true;
12859
12860         if (mode_changed && crtc_state->hw.enable &&
12861             dev_priv->display.crtc_compute_clock &&
12862             !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
12863                 ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
12864                 if (ret)
12865                         return ret;
12866         }
12867
12868         /*
12869          * May need to update pipe gamma enable bits
12870          * when C8 planes are getting enabled/disabled.
12871          */
12872         if (c8_planes_changed(crtc_state))
12873                 crtc_state->uapi.color_mgmt_changed = true;
12874
12875         if (mode_changed || crtc_state->update_pipe ||
12876             crtc_state->uapi.color_mgmt_changed) {
12877                 ret = intel_color_check(crtc_state);
12878                 if (ret)
12879                         return ret;
12880         }
12881
12882         if (dev_priv->display.compute_pipe_wm) {
12883                 ret = dev_priv->display.compute_pipe_wm(crtc_state);
12884                 if (ret) {
12885                         drm_dbg_kms(&dev_priv->drm,
12886                                     "Target pipe watermarks are invalid\n");
12887                         return ret;
12888                 }
12889         }
12890
12891         if (dev_priv->display.compute_intermediate_wm) {
12892                 if (drm_WARN_ON(&dev_priv->drm,
12893                                 !dev_priv->display.compute_pipe_wm))
12894                         return 0;
12895
12896                 /*
12897                  * Calculate 'intermediate' watermarks that satisfy both the
12898                  * old state and the new state.  We can program these
12899                  * immediately.
12900                  */
12901                 ret = dev_priv->display.compute_intermediate_wm(crtc_state);
12902                 if (ret) {
12903                         drm_dbg_kms(&dev_priv->drm,
12904                                     "No valid intermediate pipe watermarks are possible\n");
12905                         return ret;
12906                 }
12907         }
12908
12909         if (INTEL_GEN(dev_priv) >= 9) {
12910                 if (mode_changed || crtc_state->update_pipe) {
12911                         ret = skl_update_scaler_crtc(crtc_state);
12912                         if (ret)
12913                                 return ret;
12914                 }
12915
12916                 ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
12917                 if (ret)
12918                         return ret;
12919         }
12920
12921         if (HAS_IPS(dev_priv)) {
12922                 ret = hsw_compute_ips_config(crtc_state);
12923                 if (ret)
12924                         return ret;
12925         }
12926
12927         if (INTEL_GEN(dev_priv) >= 9 ||
12928             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12929                 ret = hsw_compute_linetime_wm(state, crtc);
12930                 if (ret)
12931                         return ret;
12932
12933         }
12934
12935         if (!mode_changed) {
12936                 ret = intel_psr2_sel_fetch_update(state, crtc);
12937                 if (ret)
12938                         return ret;
12939         }
12940
12941         return 0;
12942 }
12943
12944 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12945 {
12946         struct intel_connector *connector;
12947         struct drm_connector_list_iter conn_iter;
12948
12949         drm_connector_list_iter_begin(dev, &conn_iter);
12950         for_each_intel_connector_iter(connector, &conn_iter) {
12951                 if (connector->base.state->crtc)
12952                         drm_connector_put(&connector->base);
12953
12954                 if (connector->base.encoder) {
12955                         connector->base.state->best_encoder =
12956                                 connector->base.encoder;
12957                         connector->base.state->crtc =
12958                                 connector->base.encoder->crtc;
12959
12960                         drm_connector_get(&connector->base);
12961                 } else {
12962                         connector->base.state->best_encoder = NULL;
12963                         connector->base.state->crtc = NULL;
12964                 }
12965         }
12966         drm_connector_list_iter_end(&conn_iter);
12967 }
12968
12969 static int
12970 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
12971                       struct intel_crtc_state *pipe_config)
12972 {
12973         struct drm_connector *connector = conn_state->connector;
12974         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
12975         const struct drm_display_info *info = &connector->display_info;
12976         int bpp;
12977
12978         switch (conn_state->max_bpc) {
12979         case 6 ... 7:
12980                 bpp = 6 * 3;
12981                 break;
12982         case 8 ... 9:
12983                 bpp = 8 * 3;
12984                 break;
12985         case 10 ... 11:
12986                 bpp = 10 * 3;
12987                 break;
12988         case 12:
12989                 bpp = 12 * 3;
12990                 break;
12991         default:
12992                 return -EINVAL;
12993         }
12994
12995         if (bpp < pipe_config->pipe_bpp) {
12996                 drm_dbg_kms(&i915->drm,
12997                             "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
12998                             "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
12999                             connector->base.id, connector->name,
13000                             bpp, 3 * info->bpc,
13001                             3 * conn_state->max_requested_bpc,
13002                             pipe_config->pipe_bpp);
13003
13004                 pipe_config->pipe_bpp = bpp;
13005         }
13006
13007         return 0;
13008 }
13009
13010 static int
13011 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
13012                           struct intel_crtc_state *pipe_config)
13013 {
13014         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13015         struct drm_atomic_state *state = pipe_config->uapi.state;
13016         struct drm_connector *connector;
13017         struct drm_connector_state *connector_state;
13018         int bpp, i;
13019
13020         if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
13021             IS_CHERRYVIEW(dev_priv)))
13022                 bpp = 10*3;
13023         else if (INTEL_GEN(dev_priv) >= 5)
13024                 bpp = 12*3;
13025         else
13026                 bpp = 8*3;
13027
13028         pipe_config->pipe_bpp = bpp;
13029
13030         /* Clamp display bpp to connector max bpp */
13031         for_each_new_connector_in_state(state, connector, connector_state, i) {
13032                 int ret;
13033
13034                 if (connector_state->crtc != &crtc->base)
13035                         continue;
13036
13037                 ret = compute_sink_pipe_bpp(connector_state, pipe_config);
13038                 if (ret)
13039                         return ret;
13040         }
13041
13042         return 0;
13043 }
13044
13045 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
13046                                     const struct drm_display_mode *mode)
13047 {
13048         drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
13049                     "type: 0x%x flags: 0x%x\n",
13050                     mode->crtc_clock,
13051                     mode->crtc_hdisplay, mode->crtc_hsync_start,
13052                     mode->crtc_hsync_end, mode->crtc_htotal,
13053                     mode->crtc_vdisplay, mode->crtc_vsync_start,
13054                     mode->crtc_vsync_end, mode->crtc_vtotal,
13055                     mode->type, mode->flags);
13056 }
13057
13058 static void
13059 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
13060                       const char *id, unsigned int lane_count,
13061                       const struct intel_link_m_n *m_n)
13062 {
13063         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13064
13065         drm_dbg_kms(&i915->drm,
13066                     "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
13067                     id, lane_count,
13068                     m_n->gmch_m, m_n->gmch_n,
13069                     m_n->link_m, m_n->link_n, m_n->tu);
13070 }
13071
13072 static void
13073 intel_dump_infoframe(struct drm_i915_private *dev_priv,
13074                      const union hdmi_infoframe *frame)
13075 {
13076         if (!drm_debug_enabled(DRM_UT_KMS))
13077                 return;
13078
13079         hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
13080 }
13081
13082 static void
13083 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
13084                       const struct drm_dp_vsc_sdp *vsc)
13085 {
13086         if (!drm_debug_enabled(DRM_UT_KMS))
13087                 return;
13088
13089         drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
13090 }
13091
13092 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
13093
13094 static const char * const output_type_str[] = {
13095         OUTPUT_TYPE(UNUSED),
13096         OUTPUT_TYPE(ANALOG),
13097         OUTPUT_TYPE(DVO),
13098         OUTPUT_TYPE(SDVO),
13099         OUTPUT_TYPE(LVDS),
13100         OUTPUT_TYPE(TVOUT),
13101         OUTPUT_TYPE(HDMI),
13102         OUTPUT_TYPE(DP),
13103         OUTPUT_TYPE(EDP),
13104         OUTPUT_TYPE(DSI),
13105         OUTPUT_TYPE(DDI),
13106         OUTPUT_TYPE(DP_MST),
13107 };
13108
13109 #undef OUTPUT_TYPE
13110
13111 static void snprintf_output_types(char *buf, size_t len,
13112                                   unsigned int output_types)
13113 {
13114         char *str = buf;
13115         int i;
13116
13117         str[0] = '\0';
13118
13119         for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
13120                 int r;
13121
13122                 if ((output_types & BIT(i)) == 0)
13123                         continue;
13124
13125                 r = snprintf(str, len, "%s%s",
13126                              str != buf ? "," : "", output_type_str[i]);
13127                 if (r >= len)
13128                         break;
13129                 str += r;
13130                 len -= r;
13131
13132                 output_types &= ~BIT(i);
13133         }
13134
13135         WARN_ON_ONCE(output_types != 0);
13136 }
13137
13138 static const char * const output_format_str[] = {
13139         [INTEL_OUTPUT_FORMAT_INVALID] = "Invalid",
13140         [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
13141         [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
13142         [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
13143 };
13144
13145 static const char *output_formats(enum intel_output_format format)
13146 {
13147         if (format >= ARRAY_SIZE(output_format_str))
13148                 format = INTEL_OUTPUT_FORMAT_INVALID;
13149         return output_format_str[format];
13150 }
13151
13152 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
13153 {
13154         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
13155         struct drm_i915_private *i915 = to_i915(plane->base.dev);
13156         const struct drm_framebuffer *fb = plane_state->hw.fb;
13157         struct drm_format_name_buf format_name;
13158
13159         if (!fb) {
13160                 drm_dbg_kms(&i915->drm,
13161                             "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
13162                             plane->base.base.id, plane->base.name,
13163                             yesno(plane_state->uapi.visible));
13164                 return;
13165         }
13166
13167         drm_dbg_kms(&i915->drm,
13168                     "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s modifier = 0x%llx, visible: %s\n",
13169                     plane->base.base.id, plane->base.name,
13170                     fb->base.id, fb->width, fb->height,
13171                     drm_get_format_name(fb->format->format, &format_name),
13172                     fb->modifier, yesno(plane_state->uapi.visible));
13173         drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
13174                     plane_state->hw.rotation, plane_state->scaler_id);
13175         if (plane_state->uapi.visible)
13176                 drm_dbg_kms(&i915->drm,
13177                             "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
13178                             DRM_RECT_FP_ARG(&plane_state->uapi.src),
13179                             DRM_RECT_ARG(&plane_state->uapi.dst));
13180 }
13181
13182 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
13183                                    struct intel_atomic_state *state,
13184                                    const char *context)
13185 {
13186         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13187         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13188         const struct intel_plane_state *plane_state;
13189         struct intel_plane *plane;
13190         char buf[64];
13191         int i;
13192
13193         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
13194                     crtc->base.base.id, crtc->base.name,
13195                     yesno(pipe_config->hw.enable), context);
13196
13197         if (!pipe_config->hw.enable)
13198                 goto dump_planes;
13199
13200         snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
13201         drm_dbg_kms(&dev_priv->drm,
13202                     "active: %s, output_types: %s (0x%x), output format: %s\n",
13203                     yesno(pipe_config->hw.active),
13204                     buf, pipe_config->output_types,
13205                     output_formats(pipe_config->output_format));
13206
13207         drm_dbg_kms(&dev_priv->drm,
13208                     "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
13209                     transcoder_name(pipe_config->cpu_transcoder),
13210                     pipe_config->pipe_bpp, pipe_config->dither);
13211
13212         drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
13213                     transcoder_name(pipe_config->mst_master_transcoder));
13214
13215         drm_dbg_kms(&dev_priv->drm,
13216                     "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
13217                     transcoder_name(pipe_config->master_transcoder),
13218                     pipe_config->sync_mode_slaves_mask);
13219
13220         if (pipe_config->has_pch_encoder)
13221                 intel_dump_m_n_config(pipe_config, "fdi",
13222                                       pipe_config->fdi_lanes,
13223                                       &pipe_config->fdi_m_n);
13224
13225         if (intel_crtc_has_dp_encoder(pipe_config)) {
13226                 intel_dump_m_n_config(pipe_config, "dp m_n",
13227                                 pipe_config->lane_count, &pipe_config->dp_m_n);
13228                 if (pipe_config->has_drrs)
13229                         intel_dump_m_n_config(pipe_config, "dp m2_n2",
13230                                               pipe_config->lane_count,
13231                                               &pipe_config->dp_m2_n2);
13232         }
13233
13234         drm_dbg_kms(&dev_priv->drm,
13235                     "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
13236                     pipe_config->has_audio, pipe_config->has_infoframe,
13237                     pipe_config->infoframes.enable);
13238
13239         if (pipe_config->infoframes.enable &
13240             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
13241                 drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
13242                             pipe_config->infoframes.gcp);
13243         if (pipe_config->infoframes.enable &
13244             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
13245                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
13246         if (pipe_config->infoframes.enable &
13247             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
13248                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
13249         if (pipe_config->infoframes.enable &
13250             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
13251                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
13252         if (pipe_config->infoframes.enable &
13253             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
13254                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13255         if (pipe_config->infoframes.enable &
13256             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
13257                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
13258         if (pipe_config->infoframes.enable &
13259             intel_hdmi_infoframe_enable(DP_SDP_VSC))
13260                 intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
13261
13262         drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
13263         drm_mode_debug_printmodeline(&pipe_config->hw.mode);
13264         drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
13265         drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
13266         intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
13267         drm_dbg_kms(&dev_priv->drm,
13268                     "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
13269                     pipe_config->port_clock,
13270                     pipe_config->pipe_src_w, pipe_config->pipe_src_h,
13271                     pipe_config->pixel_rate);
13272
13273         drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
13274                     pipe_config->linetime, pipe_config->ips_linetime);
13275
13276         if (INTEL_GEN(dev_priv) >= 9)
13277                 drm_dbg_kms(&dev_priv->drm,
13278                             "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
13279                             crtc->num_scalers,
13280                             pipe_config->scaler_state.scaler_users,
13281                             pipe_config->scaler_state.scaler_id);
13282
13283         if (HAS_GMCH(dev_priv))
13284                 drm_dbg_kms(&dev_priv->drm,
13285                             "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
13286                             pipe_config->gmch_pfit.control,
13287                             pipe_config->gmch_pfit.pgm_ratios,
13288                             pipe_config->gmch_pfit.lvds_border_bits);
13289         else
13290                 drm_dbg_kms(&dev_priv->drm,
13291                             "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
13292                             DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
13293                             enableddisabled(pipe_config->pch_pfit.enabled),
13294                             yesno(pipe_config->pch_pfit.force_thru));
13295
13296         drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
13297                     pipe_config->ips_enabled, pipe_config->double_wide);
13298
13299         intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
13300
13301         if (IS_CHERRYVIEW(dev_priv))
13302                 drm_dbg_kms(&dev_priv->drm,
13303                             "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13304                             pipe_config->cgm_mode, pipe_config->gamma_mode,
13305                             pipe_config->gamma_enable, pipe_config->csc_enable);
13306         else
13307                 drm_dbg_kms(&dev_priv->drm,
13308                             "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
13309                             pipe_config->csc_mode, pipe_config->gamma_mode,
13310                             pipe_config->gamma_enable, pipe_config->csc_enable);
13311
13312         drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
13313                     pipe_config->hw.degamma_lut ?
13314                     drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
13315                     pipe_config->hw.gamma_lut ?
13316                     drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
13317
13318 dump_planes:
13319         if (!state)
13320                 return;
13321
13322         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
13323                 if (plane->pipe == crtc->pipe)
13324                         intel_dump_plane_state(plane_state);
13325         }
13326 }
13327
13328 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
13329 {
13330         struct drm_device *dev = state->base.dev;
13331         struct drm_connector *connector;
13332         struct drm_connector_list_iter conn_iter;
13333         unsigned int used_ports = 0;
13334         unsigned int used_mst_ports = 0;
13335         bool ret = true;
13336
13337         /*
13338          * We're going to peek into connector->state,
13339          * hence connection_mutex must be held.
13340          */
13341         drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
13342
13343         /*
13344          * Walk the connector list instead of the encoder
13345          * list to detect the problem on ddi platforms
13346          * where there's just one encoder per digital port.
13347          */
13348         drm_connector_list_iter_begin(dev, &conn_iter);
13349         drm_for_each_connector_iter(connector, &conn_iter) {
13350                 struct drm_connector_state *connector_state;
13351                 struct intel_encoder *encoder;
13352
13353                 connector_state =
13354                         drm_atomic_get_new_connector_state(&state->base,
13355                                                            connector);
13356                 if (!connector_state)
13357                         connector_state = connector->state;
13358
13359                 if (!connector_state->best_encoder)
13360                         continue;
13361
13362                 encoder = to_intel_encoder(connector_state->best_encoder);
13363
13364                 drm_WARN_ON(dev, !connector_state->crtc);
13365
13366                 switch (encoder->type) {
13367                 case INTEL_OUTPUT_DDI:
13368                         if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
13369                                 break;
13370                         fallthrough;
13371                 case INTEL_OUTPUT_DP:
13372                 case INTEL_OUTPUT_HDMI:
13373                 case INTEL_OUTPUT_EDP:
13374                         /* the same port mustn't appear more than once */
13375                         if (used_ports & BIT(encoder->port))
13376                                 ret = false;
13377
13378                         used_ports |= BIT(encoder->port);
13379                         break;
13380                 case INTEL_OUTPUT_DP_MST:
13381                         used_mst_ports |=
13382                                 1 << encoder->port;
13383                         break;
13384                 default:
13385                         break;
13386                 }
13387         }
13388         drm_connector_list_iter_end(&conn_iter);
13389
13390         /* can't mix MST and SST/HDMI on the same port */
13391         if (used_ports & used_mst_ports)
13392                 return false;
13393
13394         return ret;
13395 }
13396
13397 static void
13398 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_crtc_state *crtc_state)
13399 {
13400         intel_crtc_copy_color_blobs(crtc_state);
13401 }
13402
13403 static void
13404 intel_crtc_copy_uapi_to_hw_state(struct intel_crtc_state *crtc_state)
13405 {
13406         crtc_state->hw.enable = crtc_state->uapi.enable;
13407         crtc_state->hw.active = crtc_state->uapi.active;
13408         crtc_state->hw.mode = crtc_state->uapi.mode;
13409         crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
13410         crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
13411         intel_crtc_copy_uapi_to_hw_state_nomodeset(crtc_state);
13412 }
13413
13414 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
13415 {
13416         crtc_state->uapi.enable = crtc_state->hw.enable;
13417         crtc_state->uapi.active = crtc_state->hw.active;
13418         drm_WARN_ON(crtc_state->uapi.crtc->dev,
13419                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
13420
13421         crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
13422         crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
13423
13424         /* copy color blobs to uapi */
13425         drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
13426                                   crtc_state->hw.degamma_lut);
13427         drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
13428                                   crtc_state->hw.gamma_lut);
13429         drm_property_replace_blob(&crtc_state->uapi.ctm,
13430                                   crtc_state->hw.ctm);
13431 }
13432
13433 static int
13434 intel_crtc_prepare_cleared_state(struct intel_crtc_state *crtc_state)
13435 {
13436         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13437         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
13438         struct intel_crtc_state *saved_state;
13439
13440         saved_state = intel_crtc_state_alloc(crtc);
13441         if (!saved_state)
13442                 return -ENOMEM;
13443
13444         /* free the old crtc_state->hw members */
13445         intel_crtc_free_hw_state(crtc_state);
13446
13447         /* FIXME: before the switch to atomic started, a new pipe_config was
13448          * kzalloc'd. Code that depends on any field being zero should be
13449          * fixed, so that the crtc_state can be safely duplicated. For now,
13450          * only fields that are know to not cause problems are preserved. */
13451
13452         saved_state->uapi = crtc_state->uapi;
13453         saved_state->scaler_state = crtc_state->scaler_state;
13454         saved_state->shared_dpll = crtc_state->shared_dpll;
13455         saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
13456         memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
13457                sizeof(saved_state->icl_port_dplls));
13458         saved_state->crc_enabled = crtc_state->crc_enabled;
13459         if (IS_G4X(dev_priv) ||
13460             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13461                 saved_state->wm = crtc_state->wm;
13462
13463         memcpy(crtc_state, saved_state, sizeof(*crtc_state));
13464         kfree(saved_state);
13465
13466         intel_crtc_copy_uapi_to_hw_state(crtc_state);
13467
13468         return 0;
13469 }
13470
13471 static int
13472 intel_modeset_pipe_config(struct intel_crtc_state *pipe_config)
13473 {
13474         struct drm_crtc *crtc = pipe_config->uapi.crtc;
13475         struct drm_atomic_state *state = pipe_config->uapi.state;
13476         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
13477         struct drm_connector *connector;
13478         struct drm_connector_state *connector_state;
13479         int base_bpp, ret, i;
13480         bool retry = true;
13481
13482         pipe_config->cpu_transcoder =
13483                 (enum transcoder) to_intel_crtc(crtc)->pipe;
13484
13485         /*
13486          * Sanitize sync polarity flags based on requested ones. If neither
13487          * positive or negative polarity is requested, treat this as meaning
13488          * negative polarity.
13489          */
13490         if (!(pipe_config->hw.adjusted_mode.flags &
13491               (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
13492                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
13493
13494         if (!(pipe_config->hw.adjusted_mode.flags &
13495               (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
13496                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
13497
13498         ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13499                                         pipe_config);
13500         if (ret)
13501                 return ret;
13502
13503         base_bpp = pipe_config->pipe_bpp;
13504
13505         /*
13506          * Determine the real pipe dimensions. Note that stereo modes can
13507          * increase the actual pipe size due to the frame doubling and
13508          * insertion of additional space for blanks between the frame. This
13509          * is stored in the crtc timings. We use the requested mode to do this
13510          * computation to clearly distinguish it from the adjusted mode, which
13511          * can be changed by the connectors in the below retry loop.
13512          */
13513         drm_mode_get_hv_timing(&pipe_config->hw.mode,
13514                                &pipe_config->pipe_src_w,
13515                                &pipe_config->pipe_src_h);
13516
13517         for_each_new_connector_in_state(state, connector, connector_state, i) {
13518                 struct intel_encoder *encoder =
13519                         to_intel_encoder(connector_state->best_encoder);
13520
13521                 if (connector_state->crtc != crtc)
13522                         continue;
13523
13524                 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13525                         drm_dbg_kms(&i915->drm,
13526                                     "rejecting invalid cloning configuration\n");
13527                         return -EINVAL;
13528                 }
13529
13530                 /*
13531                  * Determine output_types before calling the .compute_config()
13532                  * hooks so that the hooks can use this information safely.
13533                  */
13534                 if (encoder->compute_output_type)
13535                         pipe_config->output_types |=
13536                                 BIT(encoder->compute_output_type(encoder, pipe_config,
13537                                                                  connector_state));
13538                 else
13539                         pipe_config->output_types |= BIT(encoder->type);
13540         }
13541
13542 encoder_retry:
13543         /* Ensure the port clock defaults are reset when retrying. */
13544         pipe_config->port_clock = 0;
13545         pipe_config->pixel_multiplier = 1;
13546
13547         /* Fill in default crtc timings, allow encoders to overwrite them. */
13548         drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
13549                               CRTC_STEREO_DOUBLE);
13550
13551         /* Pass our mode to the connectors and the CRTC to give them a chance to
13552          * adjust it according to limitations or connector properties, and also
13553          * a chance to reject the mode entirely.
13554          */
13555         for_each_new_connector_in_state(state, connector, connector_state, i) {
13556                 struct intel_encoder *encoder =
13557                         to_intel_encoder(connector_state->best_encoder);
13558
13559                 if (connector_state->crtc != crtc)
13560                         continue;
13561
13562                 ret = encoder->compute_config(encoder, pipe_config,
13563                                               connector_state);
13564                 if (ret < 0) {
13565                         if (ret != -EDEADLK)
13566                                 drm_dbg_kms(&i915->drm,
13567                                             "Encoder config failure: %d\n",
13568                                             ret);
13569                         return ret;
13570                 }
13571         }
13572
13573         /* Set default port clock if not overwritten by the encoder. Needs to be
13574          * done afterwards in case the encoder adjusts the mode. */
13575         if (!pipe_config->port_clock)
13576                 pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
13577                         * pipe_config->pixel_multiplier;
13578
13579         ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
13580         if (ret == -EDEADLK)
13581                 return ret;
13582         if (ret < 0) {
13583                 drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
13584                 return ret;
13585         }
13586
13587         if (ret == RETRY) {
13588                 if (drm_WARN(&i915->drm, !retry,
13589                              "loop in pipe configuration computation\n"))
13590                         return -EINVAL;
13591
13592                 drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
13593                 retry = false;
13594                 goto encoder_retry;
13595         }
13596
13597         /* Dithering seems to not pass-through bits correctly when it should, so
13598          * only enable it on 6bpc panels and when its not a compliance
13599          * test requesting 6bpc video pattern.
13600          */
13601         pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
13602                 !pipe_config->dither_force_disable;
13603         drm_dbg_kms(&i915->drm,
13604                     "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
13605                     base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
13606
13607         return 0;
13608 }
13609
13610 static int
13611 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
13612 {
13613         struct intel_atomic_state *state =
13614                 to_intel_atomic_state(crtc_state->uapi.state);
13615         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13616         struct drm_connector_state *conn_state;
13617         struct drm_connector *connector;
13618         int i;
13619
13620         for_each_new_connector_in_state(&state->base, connector,
13621                                         conn_state, i) {
13622                 struct intel_encoder *encoder =
13623                         to_intel_encoder(conn_state->best_encoder);
13624                 int ret;
13625
13626                 if (conn_state->crtc != &crtc->base ||
13627                     !encoder->compute_config_late)
13628                         continue;
13629
13630                 ret = encoder->compute_config_late(encoder, crtc_state,
13631                                                    conn_state);
13632                 if (ret)
13633                         return ret;
13634         }
13635
13636         return 0;
13637 }
13638
13639 bool intel_fuzzy_clock_check(int clock1, int clock2)
13640 {
13641         int diff;
13642
13643         if (clock1 == clock2)
13644                 return true;
13645
13646         if (!clock1 || !clock2)
13647                 return false;
13648
13649         diff = abs(clock1 - clock2);
13650
13651         if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13652                 return true;
13653
13654         return false;
13655 }
13656
13657 static bool
13658 intel_compare_m_n(unsigned int m, unsigned int n,
13659                   unsigned int m2, unsigned int n2,
13660                   bool exact)
13661 {
13662         if (m == m2 && n == n2)
13663                 return true;
13664
13665         if (exact || !m || !n || !m2 || !n2)
13666                 return false;
13667
13668         BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13669
13670         if (n > n2) {
13671                 while (n > n2) {
13672                         m2 <<= 1;
13673                         n2 <<= 1;
13674                 }
13675         } else if (n < n2) {
13676                 while (n < n2) {
13677                         m <<= 1;
13678                         n <<= 1;
13679                 }
13680         }
13681
13682         if (n != n2)
13683                 return false;
13684
13685         return intel_fuzzy_clock_check(m, m2);
13686 }
13687
13688 static bool
13689 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13690                        const struct intel_link_m_n *m2_n2,
13691                        bool exact)
13692 {
13693         return m_n->tu == m2_n2->tu &&
13694                 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13695                                   m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
13696                 intel_compare_m_n(m_n->link_m, m_n->link_n,
13697                                   m2_n2->link_m, m2_n2->link_n, exact);
13698 }
13699
13700 static bool
13701 intel_compare_infoframe(const union hdmi_infoframe *a,
13702                         const union hdmi_infoframe *b)
13703 {
13704         return memcmp(a, b, sizeof(*a)) == 0;
13705 }
13706
13707 static bool
13708 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
13709                          const struct drm_dp_vsc_sdp *b)
13710 {
13711         return memcmp(a, b, sizeof(*a)) == 0;
13712 }
13713
13714 static void
13715 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
13716                                bool fastset, const char *name,
13717                                const union hdmi_infoframe *a,
13718                                const union hdmi_infoframe *b)
13719 {
13720         if (fastset) {
13721                 if (!drm_debug_enabled(DRM_UT_KMS))
13722                         return;
13723
13724                 drm_dbg_kms(&dev_priv->drm,
13725                             "fastset mismatch in %s infoframe\n", name);
13726                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
13727                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
13728                 drm_dbg_kms(&dev_priv->drm, "found:\n");
13729                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
13730         } else {
13731                 drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
13732                 drm_err(&dev_priv->drm, "expected:\n");
13733                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
13734                 drm_err(&dev_priv->drm, "found:\n");
13735                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
13736         }
13737 }
13738
13739 static void
13740 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
13741                                 bool fastset, const char *name,
13742                                 const struct drm_dp_vsc_sdp *a,
13743                                 const struct drm_dp_vsc_sdp *b)
13744 {
13745         if (fastset) {
13746                 if (!drm_debug_enabled(DRM_UT_KMS))
13747                         return;
13748
13749                 drm_dbg_kms(&dev_priv->drm,
13750                             "fastset mismatch in %s dp sdp\n", name);
13751                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
13752                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
13753                 drm_dbg_kms(&dev_priv->drm, "found:\n");
13754                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
13755         } else {
13756                 drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
13757                 drm_err(&dev_priv->drm, "expected:\n");
13758                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
13759                 drm_err(&dev_priv->drm, "found:\n");
13760                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
13761         }
13762 }
13763
13764 static void __printf(4, 5)
13765 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
13766                      const char *name, const char *format, ...)
13767 {
13768         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
13769         struct va_format vaf;
13770         va_list args;
13771
13772         va_start(args, format);
13773         vaf.fmt = format;
13774         vaf.va = &args;
13775
13776         if (fastset)
13777                 drm_dbg_kms(&i915->drm,
13778                             "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
13779                             crtc->base.base.id, crtc->base.name, name, &vaf);
13780         else
13781                 drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
13782                         crtc->base.base.id, crtc->base.name, name, &vaf);
13783
13784         va_end(args);
13785 }
13786
13787 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
13788 {
13789         if (dev_priv->params.fastboot != -1)
13790                 return dev_priv->params.fastboot;
13791
13792         /* Enable fastboot by default on Skylake and newer */
13793         if (INTEL_GEN(dev_priv) >= 9)
13794                 return true;
13795
13796         /* Enable fastboot by default on VLV and CHV */
13797         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
13798                 return true;
13799
13800         /* Disabled by default on all others */
13801         return false;
13802 }
13803
13804 static bool
13805 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
13806                           const struct intel_crtc_state *pipe_config,
13807                           bool fastset)
13808 {
13809         struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
13810         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
13811         bool ret = true;
13812         u32 bp_gamma = 0;
13813         bool fixup_inherited = fastset &&
13814                 current_config->inherited && !pipe_config->inherited;
13815
13816         if (fixup_inherited && !fastboot_enabled(dev_priv)) {
13817                 drm_dbg_kms(&dev_priv->drm,
13818                             "initial modeset and fastboot not set\n");
13819                 ret = false;
13820         }
13821
13822 #define PIPE_CONF_CHECK_X(name) do { \
13823         if (current_config->name != pipe_config->name) { \
13824                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13825                                      "(expected 0x%08x, found 0x%08x)", \
13826                                      current_config->name, \
13827                                      pipe_config->name); \
13828                 ret = false; \
13829         } \
13830 } while (0)
13831
13832 #define PIPE_CONF_CHECK_I(name) do { \
13833         if (current_config->name != pipe_config->name) { \
13834                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13835                                      "(expected %i, found %i)", \
13836                                      current_config->name, \
13837                                      pipe_config->name); \
13838                 ret = false; \
13839         } \
13840 } while (0)
13841
13842 #define PIPE_CONF_CHECK_BOOL(name) do { \
13843         if (current_config->name != pipe_config->name) { \
13844                 pipe_config_mismatch(fastset, crtc,  __stringify(name), \
13845                                      "(expected %s, found %s)", \
13846                                      yesno(current_config->name), \
13847                                      yesno(pipe_config->name)); \
13848                 ret = false; \
13849         } \
13850 } while (0)
13851
13852 /*
13853  * Checks state where we only read out the enabling, but not the entire
13854  * state itself (like full infoframes or ELD for audio). These states
13855  * require a full modeset on bootup to fix up.
13856  */
13857 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
13858         if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
13859                 PIPE_CONF_CHECK_BOOL(name); \
13860         } else { \
13861                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13862                                      "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
13863                                      yesno(current_config->name), \
13864                                      yesno(pipe_config->name)); \
13865                 ret = false; \
13866         } \
13867 } while (0)
13868
13869 #define PIPE_CONF_CHECK_P(name) do { \
13870         if (current_config->name != pipe_config->name) { \
13871                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13872                                      "(expected %p, found %p)", \
13873                                      current_config->name, \
13874                                      pipe_config->name); \
13875                 ret = false; \
13876         } \
13877 } while (0)
13878
13879 #define PIPE_CONF_CHECK_M_N(name) do { \
13880         if (!intel_compare_link_m_n(&current_config->name, \
13881                                     &pipe_config->name,\
13882                                     !fastset)) { \
13883                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13884                                      "(expected tu %i gmch %i/%i link %i/%i, " \
13885                                      "found tu %i, gmch %i/%i link %i/%i)", \
13886                                      current_config->name.tu, \
13887                                      current_config->name.gmch_m, \
13888                                      current_config->name.gmch_n, \
13889                                      current_config->name.link_m, \
13890                                      current_config->name.link_n, \
13891                                      pipe_config->name.tu, \
13892                                      pipe_config->name.gmch_m, \
13893                                      pipe_config->name.gmch_n, \
13894                                      pipe_config->name.link_m, \
13895                                      pipe_config->name.link_n); \
13896                 ret = false; \
13897         } \
13898 } while (0)
13899
13900 /* This is required for BDW+ where there is only one set of registers for
13901  * switching between high and low RR.
13902  * This macro can be used whenever a comparison has to be made between one
13903  * hw state and multiple sw state variables.
13904  */
13905 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
13906         if (!intel_compare_link_m_n(&current_config->name, \
13907                                     &pipe_config->name, !fastset) && \
13908             !intel_compare_link_m_n(&current_config->alt_name, \
13909                                     &pipe_config->name, !fastset)) { \
13910                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13911                                      "(expected tu %i gmch %i/%i link %i/%i, " \
13912                                      "or tu %i gmch %i/%i link %i/%i, " \
13913                                      "found tu %i, gmch %i/%i link %i/%i)", \
13914                                      current_config->name.tu, \
13915                                      current_config->name.gmch_m, \
13916                                      current_config->name.gmch_n, \
13917                                      current_config->name.link_m, \
13918                                      current_config->name.link_n, \
13919                                      current_config->alt_name.tu, \
13920                                      current_config->alt_name.gmch_m, \
13921                                      current_config->alt_name.gmch_n, \
13922                                      current_config->alt_name.link_m, \
13923                                      current_config->alt_name.link_n, \
13924                                      pipe_config->name.tu, \
13925                                      pipe_config->name.gmch_m, \
13926                                      pipe_config->name.gmch_n, \
13927                                      pipe_config->name.link_m, \
13928                                      pipe_config->name.link_n); \
13929                 ret = false; \
13930         } \
13931 } while (0)
13932
13933 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
13934         if ((current_config->name ^ pipe_config->name) & (mask)) { \
13935                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13936                                      "(%x) (expected %i, found %i)", \
13937                                      (mask), \
13938                                      current_config->name & (mask), \
13939                                      pipe_config->name & (mask)); \
13940                 ret = false; \
13941         } \
13942 } while (0)
13943
13944 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
13945         if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
13946                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
13947                                      "(expected %i, found %i)", \
13948                                      current_config->name, \
13949                                      pipe_config->name); \
13950                 ret = false; \
13951         } \
13952 } while (0)
13953
13954 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
13955         if (!intel_compare_infoframe(&current_config->infoframes.name, \
13956                                      &pipe_config->infoframes.name)) { \
13957                 pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
13958                                                &current_config->infoframes.name, \
13959                                                &pipe_config->infoframes.name); \
13960                 ret = false; \
13961         } \
13962 } while (0)
13963
13964 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
13965         if (!current_config->has_psr && !pipe_config->has_psr && \
13966             !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
13967                                       &pipe_config->infoframes.name)) { \
13968                 pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
13969                                                 &current_config->infoframes.name, \
13970                                                 &pipe_config->infoframes.name); \
13971                 ret = false; \
13972         } \
13973 } while (0)
13974
13975 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
13976         if (current_config->name1 != pipe_config->name1) { \
13977                 pipe_config_mismatch(fastset, crtc, __stringify(name1), \
13978                                 "(expected %i, found %i, won't compare lut values)", \
13979                                 current_config->name1, \
13980                                 pipe_config->name1); \
13981                 ret = false;\
13982         } else { \
13983                 if (!intel_color_lut_equal(current_config->name2, \
13984                                         pipe_config->name2, pipe_config->name1, \
13985                                         bit_precision)) { \
13986                         pipe_config_mismatch(fastset, crtc, __stringify(name2), \
13987                                         "hw_state doesn't match sw_state"); \
13988                         ret = false; \
13989                 } \
13990         } \
13991 } while (0)
13992
13993 #define PIPE_CONF_QUIRK(quirk) \
13994         ((current_config->quirks | pipe_config->quirks) & (quirk))
13995
13996         PIPE_CONF_CHECK_I(cpu_transcoder);
13997
13998         PIPE_CONF_CHECK_BOOL(has_pch_encoder);
13999         PIPE_CONF_CHECK_I(fdi_lanes);
14000         PIPE_CONF_CHECK_M_N(fdi_m_n);
14001
14002         PIPE_CONF_CHECK_I(lane_count);
14003         PIPE_CONF_CHECK_X(lane_lat_optim_mask);
14004
14005         if (INTEL_GEN(dev_priv) < 8) {
14006                 PIPE_CONF_CHECK_M_N(dp_m_n);
14007
14008                 if (current_config->has_drrs)
14009                         PIPE_CONF_CHECK_M_N(dp_m2_n2);
14010         } else
14011                 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
14012
14013         PIPE_CONF_CHECK_X(output_types);
14014
14015         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
14016         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
14017         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
14018         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
14019         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
14020         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
14021
14022         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
14023         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
14024         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
14025         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
14026         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
14027         PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
14028
14029         PIPE_CONF_CHECK_I(pixel_multiplier);
14030         PIPE_CONF_CHECK_I(output_format);
14031         PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
14032         if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
14033             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14034                 PIPE_CONF_CHECK_BOOL(limited_color_range);
14035
14036         PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
14037         PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
14038         PIPE_CONF_CHECK_BOOL(has_infoframe);
14039         PIPE_CONF_CHECK_BOOL(fec_enable);
14040
14041         PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
14042
14043         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14044                               DRM_MODE_FLAG_INTERLACE);
14045
14046         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
14047                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14048                                       DRM_MODE_FLAG_PHSYNC);
14049                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14050                                       DRM_MODE_FLAG_NHSYNC);
14051                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14052                                       DRM_MODE_FLAG_PVSYNC);
14053                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
14054                                       DRM_MODE_FLAG_NVSYNC);
14055         }
14056
14057         PIPE_CONF_CHECK_X(gmch_pfit.control);
14058         /* pfit ratios are autocomputed by the hw on gen4+ */
14059         if (INTEL_GEN(dev_priv) < 4)
14060                 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
14061         PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
14062
14063         /*
14064          * Changing the EDP transcoder input mux
14065          * (A_ONOFF vs. A_ON) requires a full modeset.
14066          */
14067         PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
14068
14069         if (!fastset) {
14070                 PIPE_CONF_CHECK_I(pipe_src_w);
14071                 PIPE_CONF_CHECK_I(pipe_src_h);
14072
14073                 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
14074                 if (current_config->pch_pfit.enabled) {
14075                         PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
14076                         PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
14077                         PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
14078                         PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
14079                 }
14080
14081                 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
14082                 PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
14083
14084                 PIPE_CONF_CHECK_X(gamma_mode);
14085                 if (IS_CHERRYVIEW(dev_priv))
14086                         PIPE_CONF_CHECK_X(cgm_mode);
14087                 else
14088                         PIPE_CONF_CHECK_X(csc_mode);
14089                 PIPE_CONF_CHECK_BOOL(gamma_enable);
14090                 PIPE_CONF_CHECK_BOOL(csc_enable);
14091
14092                 PIPE_CONF_CHECK_I(linetime);
14093                 PIPE_CONF_CHECK_I(ips_linetime);
14094
14095                 bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
14096                 if (bp_gamma)
14097                         PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
14098         }
14099
14100         PIPE_CONF_CHECK_BOOL(double_wide);
14101
14102         PIPE_CONF_CHECK_P(shared_dpll);
14103         PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
14104         PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
14105         PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
14106         PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
14107         PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
14108         PIPE_CONF_CHECK_X(dpll_hw_state.spll);
14109         PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
14110         PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
14111         PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
14112         PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
14113         PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
14114         PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
14115         PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
14116         PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
14117         PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
14118         PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
14119         PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
14120         PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
14121         PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
14122         PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
14123         PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
14124         PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
14125         PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
14126         PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
14127         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
14128         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
14129         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
14130         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
14131         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
14132         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
14133         PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
14134
14135         PIPE_CONF_CHECK_X(dsi_pll.ctrl);
14136         PIPE_CONF_CHECK_X(dsi_pll.div);
14137
14138         if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
14139                 PIPE_CONF_CHECK_I(pipe_bpp);
14140
14141         PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
14142         PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
14143
14144         PIPE_CONF_CHECK_I(min_voltage_level);
14145
14146         PIPE_CONF_CHECK_X(infoframes.enable);
14147         PIPE_CONF_CHECK_X(infoframes.gcp);
14148         PIPE_CONF_CHECK_INFOFRAME(avi);
14149         PIPE_CONF_CHECK_INFOFRAME(spd);
14150         PIPE_CONF_CHECK_INFOFRAME(hdmi);
14151         PIPE_CONF_CHECK_INFOFRAME(drm);
14152         PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
14153
14154         PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
14155         PIPE_CONF_CHECK_I(master_transcoder);
14156
14157         PIPE_CONF_CHECK_I(dsc.compression_enable);
14158         PIPE_CONF_CHECK_I(dsc.dsc_split);
14159         PIPE_CONF_CHECK_I(dsc.compressed_bpp);
14160
14161         PIPE_CONF_CHECK_I(mst_master_transcoder);
14162
14163 #undef PIPE_CONF_CHECK_X
14164 #undef PIPE_CONF_CHECK_I
14165 #undef PIPE_CONF_CHECK_BOOL
14166 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
14167 #undef PIPE_CONF_CHECK_P
14168 #undef PIPE_CONF_CHECK_FLAGS
14169 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
14170 #undef PIPE_CONF_CHECK_COLOR_LUT
14171 #undef PIPE_CONF_QUIRK
14172
14173         return ret;
14174 }
14175
14176 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
14177                                            const struct intel_crtc_state *pipe_config)
14178 {
14179         if (pipe_config->has_pch_encoder) {
14180                 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
14181                                                             &pipe_config->fdi_m_n);
14182                 int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
14183
14184                 /*
14185                  * FDI already provided one idea for the dotclock.
14186                  * Yell if the encoder disagrees.
14187                  */
14188                 drm_WARN(&dev_priv->drm,
14189                          !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
14190                          "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
14191                          fdi_dotclock, dotclock);
14192         }
14193 }
14194
14195 static void verify_wm_state(struct intel_crtc *crtc,
14196                             struct intel_crtc_state *new_crtc_state)
14197 {
14198         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14199         struct skl_hw_state {
14200                 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
14201                 struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
14202                 struct skl_pipe_wm wm;
14203         } *hw;
14204         struct skl_pipe_wm *sw_wm;
14205         struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
14206         u8 hw_enabled_slices;
14207         const enum pipe pipe = crtc->pipe;
14208         int plane, level, max_level = ilk_wm_max_level(dev_priv);
14209
14210         if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
14211                 return;
14212
14213         hw = kzalloc(sizeof(*hw), GFP_KERNEL);
14214         if (!hw)
14215                 return;
14216
14217         skl_pipe_wm_get_hw_state(crtc, &hw->wm);
14218         sw_wm = &new_crtc_state->wm.skl.optimal;
14219
14220         skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
14221
14222         hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
14223
14224         if (INTEL_GEN(dev_priv) >= 11 &&
14225             hw_enabled_slices != dev_priv->dbuf.enabled_slices)
14226                 drm_err(&dev_priv->drm,
14227                         "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
14228                         dev_priv->dbuf.enabled_slices,
14229                         hw_enabled_slices);
14230
14231         /* planes */
14232         for_each_universal_plane(dev_priv, pipe, plane) {
14233                 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14234
14235                 hw_plane_wm = &hw->wm.planes[plane];
14236                 sw_plane_wm = &sw_wm->planes[plane];
14237
14238                 /* Watermarks */
14239                 for (level = 0; level <= max_level; level++) {
14240                         if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14241                                                 &sw_plane_wm->wm[level]) ||
14242                             (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14243                                                                &sw_plane_wm->sagv_wm0)))
14244                                 continue;
14245
14246                         drm_err(&dev_priv->drm,
14247                                 "mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14248                                 pipe_name(pipe), plane + 1, level,
14249                                 sw_plane_wm->wm[level].plane_en,
14250                                 sw_plane_wm->wm[level].plane_res_b,
14251                                 sw_plane_wm->wm[level].plane_res_l,
14252                                 hw_plane_wm->wm[level].plane_en,
14253                                 hw_plane_wm->wm[level].plane_res_b,
14254                                 hw_plane_wm->wm[level].plane_res_l);
14255                 }
14256
14257                 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14258                                          &sw_plane_wm->trans_wm)) {
14259                         drm_err(&dev_priv->drm,
14260                                 "mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14261                                 pipe_name(pipe), plane + 1,
14262                                 sw_plane_wm->trans_wm.plane_en,
14263                                 sw_plane_wm->trans_wm.plane_res_b,
14264                                 sw_plane_wm->trans_wm.plane_res_l,
14265                                 hw_plane_wm->trans_wm.plane_en,
14266                                 hw_plane_wm->trans_wm.plane_res_b,
14267                                 hw_plane_wm->trans_wm.plane_res_l);
14268                 }
14269
14270                 /* DDB */
14271                 hw_ddb_entry = &hw->ddb_y[plane];
14272                 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane];
14273
14274                 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14275                         drm_err(&dev_priv->drm,
14276                                 "mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
14277                                 pipe_name(pipe), plane + 1,
14278                                 sw_ddb_entry->start, sw_ddb_entry->end,
14279                                 hw_ddb_entry->start, hw_ddb_entry->end);
14280                 }
14281         }
14282
14283         /*
14284          * cursor
14285          * If the cursor plane isn't active, we may not have updated it's ddb
14286          * allocation. In that case since the ddb allocation will be updated
14287          * once the plane becomes visible, we can skip this check
14288          */
14289         if (1) {
14290                 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
14291
14292                 hw_plane_wm = &hw->wm.planes[PLANE_CURSOR];
14293                 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
14294
14295                 /* Watermarks */
14296                 for (level = 0; level <= max_level; level++) {
14297                         if (skl_wm_level_equals(&hw_plane_wm->wm[level],
14298                                                 &sw_plane_wm->wm[level]) ||
14299                             (level == 0 && skl_wm_level_equals(&hw_plane_wm->wm[level],
14300                                                                &sw_plane_wm->sagv_wm0)))
14301                                 continue;
14302
14303                         drm_err(&dev_priv->drm,
14304                                 "mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14305                                 pipe_name(pipe), level,
14306                                 sw_plane_wm->wm[level].plane_en,
14307                                 sw_plane_wm->wm[level].plane_res_b,
14308                                 sw_plane_wm->wm[level].plane_res_l,
14309                                 hw_plane_wm->wm[level].plane_en,
14310                                 hw_plane_wm->wm[level].plane_res_b,
14311                                 hw_plane_wm->wm[level].plane_res_l);
14312                 }
14313
14314                 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
14315                                          &sw_plane_wm->trans_wm)) {
14316                         drm_err(&dev_priv->drm,
14317                                 "mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
14318                                 pipe_name(pipe),
14319                                 sw_plane_wm->trans_wm.plane_en,
14320                                 sw_plane_wm->trans_wm.plane_res_b,
14321                                 sw_plane_wm->trans_wm.plane_res_l,
14322                                 hw_plane_wm->trans_wm.plane_en,
14323                                 hw_plane_wm->trans_wm.plane_res_b,
14324                                 hw_plane_wm->trans_wm.plane_res_l);
14325                 }
14326
14327                 /* DDB */
14328                 hw_ddb_entry = &hw->ddb_y[PLANE_CURSOR];
14329                 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR];
14330
14331                 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
14332                         drm_err(&dev_priv->drm,
14333                                 "mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
14334                                 pipe_name(pipe),
14335                                 sw_ddb_entry->start, sw_ddb_entry->end,
14336                                 hw_ddb_entry->start, hw_ddb_entry->end);
14337                 }
14338         }
14339
14340         kfree(hw);
14341 }
14342
14343 static void
14344 verify_connector_state(struct intel_atomic_state *state,
14345                        struct intel_crtc *crtc)
14346 {
14347         struct drm_connector *connector;
14348         struct drm_connector_state *new_conn_state;
14349         int i;
14350
14351         for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
14352                 struct drm_encoder *encoder = connector->encoder;
14353                 struct intel_crtc_state *crtc_state = NULL;
14354
14355                 if (new_conn_state->crtc != &crtc->base)
14356                         continue;
14357
14358                 if (crtc)
14359                         crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
14360
14361                 intel_connector_verify_state(crtc_state, new_conn_state);
14362
14363                 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
14364                      "connector's atomic encoder doesn't match legacy encoder\n");
14365         }
14366 }
14367
14368 static void
14369 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
14370 {
14371         struct intel_encoder *encoder;
14372         struct drm_connector *connector;
14373         struct drm_connector_state *old_conn_state, *new_conn_state;
14374         int i;
14375
14376         for_each_intel_encoder(&dev_priv->drm, encoder) {
14377                 bool enabled = false, found = false;
14378                 enum pipe pipe;
14379
14380                 drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
14381                             encoder->base.base.id,
14382                             encoder->base.name);
14383
14384                 for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
14385                                                    new_conn_state, i) {
14386                         if (old_conn_state->best_encoder == &encoder->base)
14387                                 found = true;
14388
14389                         if (new_conn_state->best_encoder != &encoder->base)
14390                                 continue;
14391                         found = enabled = true;
14392
14393                         I915_STATE_WARN(new_conn_state->crtc !=
14394                                         encoder->base.crtc,
14395                              "connector's crtc doesn't match encoder crtc\n");
14396                 }
14397
14398                 if (!found)
14399                         continue;
14400
14401                 I915_STATE_WARN(!!encoder->base.crtc != enabled,
14402                      "encoder's enabled state mismatch "
14403                      "(expected %i, found %i)\n",
14404                      !!encoder->base.crtc, enabled);
14405
14406                 if (!encoder->base.crtc) {
14407                         bool active;
14408
14409                         active = encoder->get_hw_state(encoder, &pipe);
14410                         I915_STATE_WARN(active,
14411                              "encoder detached but still enabled on pipe %c.\n",
14412                              pipe_name(pipe));
14413                 }
14414         }
14415 }
14416
14417 static void
14418 verify_crtc_state(struct intel_crtc *crtc,
14419                   struct intel_crtc_state *old_crtc_state,
14420                   struct intel_crtc_state *new_crtc_state)
14421 {
14422         struct drm_device *dev = crtc->base.dev;
14423         struct drm_i915_private *dev_priv = to_i915(dev);
14424         struct intel_encoder *encoder;
14425         struct intel_crtc_state *pipe_config = old_crtc_state;
14426         struct drm_atomic_state *state = old_crtc_state->uapi.state;
14427
14428         __drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
14429         intel_crtc_free_hw_state(old_crtc_state);
14430         intel_crtc_state_reset(old_crtc_state, crtc);
14431         old_crtc_state->uapi.state = state;
14432
14433         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
14434                     crtc->base.name);
14435
14436         pipe_config->hw.enable = new_crtc_state->hw.enable;
14437
14438         intel_crtc_get_pipe_config(pipe_config);
14439
14440         /* we keep both pipes enabled on 830 */
14441         if (IS_I830(dev_priv) && pipe_config->hw.active)
14442                 pipe_config->hw.active = new_crtc_state->hw.active;
14443
14444         I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
14445                         "crtc active state doesn't match with hw state "
14446                         "(expected %i, found %i)\n",
14447                         new_crtc_state->hw.active, pipe_config->hw.active);
14448
14449         I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
14450                         "transitional active state does not match atomic hw state "
14451                         "(expected %i, found %i)\n",
14452                         new_crtc_state->hw.active, crtc->active);
14453
14454         for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14455                 enum pipe pipe;
14456                 bool active;
14457
14458                 active = encoder->get_hw_state(encoder, &pipe);
14459                 I915_STATE_WARN(active != new_crtc_state->hw.active,
14460                                 "[ENCODER:%i] active %i with crtc active %i\n",
14461                                 encoder->base.base.id, active,
14462                                 new_crtc_state->hw.active);
14463
14464                 I915_STATE_WARN(active && crtc->pipe != pipe,
14465                                 "Encoder connected to wrong pipe %c\n",
14466                                 pipe_name(pipe));
14467
14468                 if (active)
14469                         intel_encoder_get_config(encoder, pipe_config);
14470         }
14471
14472         intel_crtc_compute_pixel_rate(pipe_config);
14473
14474         if (!new_crtc_state->hw.active)
14475                 return;
14476
14477         intel_pipe_config_sanity_check(dev_priv, pipe_config);
14478
14479         if (!intel_pipe_config_compare(new_crtc_state,
14480                                        pipe_config, false)) {
14481                 I915_STATE_WARN(1, "pipe state doesn't match!\n");
14482                 intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
14483                 intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
14484         }
14485 }
14486
14487 static void
14488 intel_verify_planes(struct intel_atomic_state *state)
14489 {
14490         struct intel_plane *plane;
14491         const struct intel_plane_state *plane_state;
14492         int i;
14493
14494         for_each_new_intel_plane_in_state(state, plane,
14495                                           plane_state, i)
14496                 assert_plane(plane, plane_state->planar_slave ||
14497                              plane_state->uapi.visible);
14498 }
14499
14500 static void
14501 verify_single_dpll_state(struct drm_i915_private *dev_priv,
14502                          struct intel_shared_dpll *pll,
14503                          struct intel_crtc *crtc,
14504                          struct intel_crtc_state *new_crtc_state)
14505 {
14506         struct intel_dpll_hw_state dpll_hw_state;
14507         unsigned int crtc_mask;
14508         bool active;
14509
14510         memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
14511
14512         drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
14513
14514         active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
14515
14516         if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
14517                 I915_STATE_WARN(!pll->on && pll->active_mask,
14518                      "pll in active use but not on in sw tracking\n");
14519                 I915_STATE_WARN(pll->on && !pll->active_mask,
14520                      "pll is on but not used by any active crtc\n");
14521                 I915_STATE_WARN(pll->on != active,
14522                      "pll on state mismatch (expected %i, found %i)\n",
14523                      pll->on, active);
14524         }
14525
14526         if (!crtc) {
14527                 I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
14528                                 "more active pll users than references: %x vs %x\n",
14529                                 pll->active_mask, pll->state.crtc_mask);
14530
14531                 return;
14532         }
14533
14534         crtc_mask = drm_crtc_mask(&crtc->base);
14535
14536         if (new_crtc_state->hw.active)
14537                 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
14538                                 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
14539                                 pipe_name(crtc->pipe), pll->active_mask);
14540         else
14541                 I915_STATE_WARN(pll->active_mask & crtc_mask,
14542                                 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
14543                                 pipe_name(crtc->pipe), pll->active_mask);
14544
14545         I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
14546                         "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
14547                         crtc_mask, pll->state.crtc_mask);
14548
14549         I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
14550                                           &dpll_hw_state,
14551                                           sizeof(dpll_hw_state)),
14552                         "pll hw state mismatch\n");
14553 }
14554
14555 static void
14556 verify_shared_dpll_state(struct intel_crtc *crtc,
14557                          struct intel_crtc_state *old_crtc_state,
14558                          struct intel_crtc_state *new_crtc_state)
14559 {
14560         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14561
14562         if (new_crtc_state->shared_dpll)
14563                 verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
14564
14565         if (old_crtc_state->shared_dpll &&
14566             old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
14567                 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
14568                 struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
14569
14570                 I915_STATE_WARN(pll->active_mask & crtc_mask,
14571                                 "pll active mismatch (didn't expect pipe %c in active mask)\n",
14572                                 pipe_name(crtc->pipe));
14573                 I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
14574                                 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
14575                                 pipe_name(crtc->pipe));
14576         }
14577 }
14578
14579 static void
14580 intel_modeset_verify_crtc(struct intel_crtc *crtc,
14581                           struct intel_atomic_state *state,
14582                           struct intel_crtc_state *old_crtc_state,
14583                           struct intel_crtc_state *new_crtc_state)
14584 {
14585         if (!needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
14586                 return;
14587
14588         verify_wm_state(crtc, new_crtc_state);
14589         verify_connector_state(state, crtc);
14590         verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
14591         verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
14592 }
14593
14594 static void
14595 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
14596 {
14597         int i;
14598
14599         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
14600                 verify_single_dpll_state(dev_priv,
14601                                          &dev_priv->dpll.shared_dplls[i],
14602                                          NULL, NULL);
14603 }
14604
14605 static void
14606 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
14607                               struct intel_atomic_state *state)
14608 {
14609         verify_encoder_state(dev_priv, state);
14610         verify_connector_state(state, NULL);
14611         verify_disabled_dpll_state(dev_priv);
14612 }
14613
14614 static void
14615 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
14616 {
14617         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
14618         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
14619         const struct drm_display_mode *adjusted_mode =
14620                 &crtc_state->hw.adjusted_mode;
14621
14622         drm_calc_timestamping_constants(&crtc->base, adjusted_mode);
14623
14624         crtc->mode_flags = crtc_state->mode_flags;
14625
14626         /*
14627          * The scanline counter increments at the leading edge of hsync.
14628          *
14629          * On most platforms it starts counting from vtotal-1 on the
14630          * first active line. That means the scanline counter value is
14631          * always one less than what we would expect. Ie. just after
14632          * start of vblank, which also occurs at start of hsync (on the
14633          * last active line), the scanline counter will read vblank_start-1.
14634          *
14635          * On gen2 the scanline counter starts counting from 1 instead
14636          * of vtotal-1, so we have to subtract one (or rather add vtotal-1
14637          * to keep the value positive), instead of adding one.
14638          *
14639          * On HSW+ the behaviour of the scanline counter depends on the output
14640          * type. For DP ports it behaves like most other platforms, but on HDMI
14641          * there's an extra 1 line difference. So we need to add two instead of
14642          * one to the value.
14643          *
14644          * On VLV/CHV DSI the scanline counter would appear to increment
14645          * approx. 1/3 of a scanline before start of vblank. Unfortunately
14646          * that means we can't tell whether we're in vblank or not while
14647          * we're on that particular line. We must still set scanline_offset
14648          * to 1 so that the vblank timestamps come out correct when we query
14649          * the scanline counter from within the vblank interrupt handler.
14650          * However if queried just before the start of vblank we'll get an
14651          * answer that's slightly in the future.
14652          */
14653         if (IS_GEN(dev_priv, 2)) {
14654                 int vtotal;
14655
14656                 vtotal = adjusted_mode->crtc_vtotal;
14657                 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
14658                         vtotal /= 2;
14659
14660                 crtc->scanline_offset = vtotal - 1;
14661         } else if (HAS_DDI(dev_priv) &&
14662                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
14663                 crtc->scanline_offset = 2;
14664         } else {
14665                 crtc->scanline_offset = 1;
14666         }
14667 }
14668
14669 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
14670 {
14671         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14672         struct intel_crtc_state *new_crtc_state;
14673         struct intel_crtc *crtc;
14674         int i;
14675
14676         if (!dev_priv->display.crtc_compute_clock)
14677                 return;
14678
14679         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14680                 if (!needs_modeset(new_crtc_state))
14681                         continue;
14682
14683                 intel_release_shared_dplls(state, crtc);
14684         }
14685 }
14686
14687 /*
14688  * This implements the workaround described in the "notes" section of the mode
14689  * set sequence documentation. When going from no pipes or single pipe to
14690  * multiple pipes, and planes are enabled after the pipe, we need to wait at
14691  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
14692  */
14693 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
14694 {
14695         struct intel_crtc_state *crtc_state;
14696         struct intel_crtc *crtc;
14697         struct intel_crtc_state *first_crtc_state = NULL;
14698         struct intel_crtc_state *other_crtc_state = NULL;
14699         enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
14700         int i;
14701
14702         /* look at all crtc's that are going to be enabled in during modeset */
14703         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14704                 if (!crtc_state->hw.active ||
14705                     !needs_modeset(crtc_state))
14706                         continue;
14707
14708                 if (first_crtc_state) {
14709                         other_crtc_state = crtc_state;
14710                         break;
14711                 } else {
14712                         first_crtc_state = crtc_state;
14713                         first_pipe = crtc->pipe;
14714                 }
14715         }
14716
14717         /* No workaround needed? */
14718         if (!first_crtc_state)
14719                 return 0;
14720
14721         /* w/a possibly needed, check how many crtc's are already enabled. */
14722         for_each_intel_crtc(state->base.dev, crtc) {
14723                 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
14724                 if (IS_ERR(crtc_state))
14725                         return PTR_ERR(crtc_state);
14726
14727                 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
14728
14729                 if (!crtc_state->hw.active ||
14730                     needs_modeset(crtc_state))
14731                         continue;
14732
14733                 /* 2 or more enabled crtcs means no need for w/a */
14734                 if (enabled_pipe != INVALID_PIPE)
14735                         return 0;
14736
14737                 enabled_pipe = crtc->pipe;
14738         }
14739
14740         if (enabled_pipe != INVALID_PIPE)
14741                 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
14742         else if (other_crtc_state)
14743                 other_crtc_state->hsw_workaround_pipe = first_pipe;
14744
14745         return 0;
14746 }
14747
14748 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
14749                            u8 active_pipes)
14750 {
14751         const struct intel_crtc_state *crtc_state;
14752         struct intel_crtc *crtc;
14753         int i;
14754
14755         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14756                 if (crtc_state->hw.active)
14757                         active_pipes |= BIT(crtc->pipe);
14758                 else
14759                         active_pipes &= ~BIT(crtc->pipe);
14760         }
14761
14762         return active_pipes;
14763 }
14764
14765 static int intel_modeset_checks(struct intel_atomic_state *state)
14766 {
14767         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14768
14769         state->modeset = true;
14770
14771         if (IS_HASWELL(dev_priv))
14772                 return hsw_mode_set_planes_workaround(state);
14773
14774         return 0;
14775 }
14776
14777 /*
14778  * Handle calculation of various watermark data at the end of the atomic check
14779  * phase.  The code here should be run after the per-crtc and per-plane 'check'
14780  * handlers to ensure that all derived state has been updated.
14781  */
14782 static int calc_watermark_data(struct intel_atomic_state *state)
14783 {
14784         struct drm_device *dev = state->base.dev;
14785         struct drm_i915_private *dev_priv = to_i915(dev);
14786
14787         /* Is there platform-specific watermark information to calculate? */
14788         if (dev_priv->display.compute_global_watermarks)
14789                 return dev_priv->display.compute_global_watermarks(state);
14790
14791         return 0;
14792 }
14793
14794 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
14795                                      struct intel_crtc_state *new_crtc_state)
14796 {
14797         if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
14798                 return;
14799
14800         new_crtc_state->uapi.mode_changed = false;
14801         new_crtc_state->update_pipe = true;
14802 }
14803
14804 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
14805                                     struct intel_crtc_state *new_crtc_state)
14806 {
14807         /*
14808          * If we're not doing the full modeset we want to
14809          * keep the current M/N values as they may be
14810          * sufficiently different to the computed values
14811          * to cause problems.
14812          *
14813          * FIXME: should really copy more fuzzy state here
14814          */
14815         new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
14816         new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
14817         new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
14818         new_crtc_state->has_drrs = old_crtc_state->has_drrs;
14819 }
14820
14821 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
14822                                           struct intel_crtc *crtc,
14823                                           u8 plane_ids_mask)
14824 {
14825         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14826         struct intel_plane *plane;
14827
14828         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
14829                 struct intel_plane_state *plane_state;
14830
14831                 if ((plane_ids_mask & BIT(plane->id)) == 0)
14832                         continue;
14833
14834                 plane_state = intel_atomic_get_plane_state(state, plane);
14835                 if (IS_ERR(plane_state))
14836                         return PTR_ERR(plane_state);
14837         }
14838
14839         return 0;
14840 }
14841
14842 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
14843 {
14844         /* See {hsw,vlv,ivb}_plane_ratio() */
14845         return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
14846                 IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
14847                 IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
14848 }
14849
14850 static int intel_atomic_check_planes(struct intel_atomic_state *state)
14851 {
14852         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14853         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
14854         struct intel_plane_state *plane_state;
14855         struct intel_plane *plane;
14856         struct intel_crtc *crtc;
14857         int i, ret;
14858
14859         ret = icl_add_linked_planes(state);
14860         if (ret)
14861                 return ret;
14862
14863         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14864                 ret = intel_plane_atomic_check(state, plane);
14865                 if (ret) {
14866                         drm_dbg_atomic(&dev_priv->drm,
14867                                        "[PLANE:%d:%s] atomic driver check failed\n",
14868                                        plane->base.base.id, plane->base.name);
14869                         return ret;
14870                 }
14871         }
14872
14873         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
14874                                             new_crtc_state, i) {
14875                 u8 old_active_planes, new_active_planes;
14876
14877                 ret = icl_check_nv12_planes(new_crtc_state);
14878                 if (ret)
14879                         return ret;
14880
14881                 /*
14882                  * On some platforms the number of active planes affects
14883                  * the planes' minimum cdclk calculation. Add such planes
14884                  * to the state before we compute the minimum cdclk.
14885                  */
14886                 if (!active_planes_affects_min_cdclk(dev_priv))
14887                         continue;
14888
14889                 old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14890                 new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
14891
14892                 /*
14893                  * Not only the number of planes, but if the plane configuration had
14894                  * changed might already mean we need to recompute min CDCLK,
14895                  * because different planes might consume different amount of Dbuf bandwidth
14896                  * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
14897                  */
14898                 if (old_active_planes == new_active_planes)
14899                         continue;
14900
14901                 ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
14902                 if (ret)
14903                         return ret;
14904         }
14905
14906         return 0;
14907 }
14908
14909 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
14910                                     bool *need_cdclk_calc)
14911 {
14912         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
14913         const struct intel_cdclk_state *old_cdclk_state;
14914         const struct intel_cdclk_state *new_cdclk_state;
14915         struct intel_plane_state *plane_state;
14916         struct intel_bw_state *new_bw_state;
14917         struct intel_plane *plane;
14918         int min_cdclk = 0;
14919         enum pipe pipe;
14920         int ret;
14921         int i;
14922         /*
14923          * active_planes bitmask has been updated, and potentially
14924          * affected planes are part of the state. We can now
14925          * compute the minimum cdclk for each plane.
14926          */
14927         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
14928                 ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
14929                 if (ret)
14930                         return ret;
14931         }
14932
14933         old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
14934         new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
14935
14936         if (new_cdclk_state &&
14937             old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
14938                 *need_cdclk_calc = true;
14939
14940         ret = dev_priv->display.bw_calc_min_cdclk(state);
14941         if (ret)
14942                 return ret;
14943
14944         new_bw_state = intel_atomic_get_new_bw_state(state);
14945
14946         if (!new_cdclk_state || !new_bw_state)
14947                 return 0;
14948
14949         for_each_pipe(dev_priv, pipe) {
14950                 min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
14951
14952                 /*
14953                  * Currently do this change only if we need to increase
14954                  */
14955                 if (new_bw_state->min_cdclk > min_cdclk)
14956                         *need_cdclk_calc = true;
14957         }
14958
14959         return 0;
14960 }
14961
14962 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
14963 {
14964         struct intel_crtc_state *crtc_state;
14965         struct intel_crtc *crtc;
14966         int i;
14967
14968         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
14969                 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
14970                 int ret;
14971
14972                 ret = intel_crtc_atomic_check(state, crtc);
14973                 if (ret) {
14974                         drm_dbg_atomic(&i915->drm,
14975                                        "[CRTC:%d:%s] atomic driver check failed\n",
14976                                        crtc->base.base.id, crtc->base.name);
14977                         return ret;
14978                 }
14979         }
14980
14981         return 0;
14982 }
14983
14984 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
14985                                                u8 transcoders)
14986 {
14987         const struct intel_crtc_state *new_crtc_state;
14988         struct intel_crtc *crtc;
14989         int i;
14990
14991         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
14992                 if (new_crtc_state->hw.enable &&
14993                     transcoders & BIT(new_crtc_state->cpu_transcoder) &&
14994                     needs_modeset(new_crtc_state))
14995                         return true;
14996         }
14997
14998         return false;
14999 }
15000
15001 /**
15002  * DOC: asynchronous flip implementation
15003  *
15004  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
15005  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
15006  * Correspondingly, support is currently added for primary plane only.
15007  *
15008  * Async flip can only change the plane surface address, so anything else
15009  * changing is rejected from the intel_atomic_check_async() function.
15010  * Once this check is cleared, flip done interrupt is enabled using
15011  * the skl_enable_flip_done() function.
15012  *
15013  * As soon as the surface address register is written, flip done interrupt is
15014  * generated and the requested events are sent to the usersapce in the interrupt
15015  * handler itself. The timestamp and sequence sent during the flip done event
15016  * correspond to the last vblank and have no relation to the actual time when
15017  * the flip done event was sent.
15018  */
15019 static int intel_atomic_check_async(struct intel_atomic_state *state)
15020 {
15021         struct drm_i915_private *i915 = to_i915(state->base.dev);
15022         const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15023         const struct intel_plane_state *new_plane_state, *old_plane_state;
15024         struct intel_crtc *crtc;
15025         struct intel_plane *plane;
15026         int i;
15027
15028         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15029                                             new_crtc_state, i) {
15030                 if (needs_modeset(new_crtc_state)) {
15031                         drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
15032                         return -EINVAL;
15033                 }
15034
15035                 if (!new_crtc_state->hw.active) {
15036                         drm_dbg_kms(&i915->drm, "CRTC inactive\n");
15037                         return -EINVAL;
15038                 }
15039                 if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
15040                         drm_dbg_kms(&i915->drm,
15041                                     "Active planes cannot be changed during async flip\n");
15042                         return -EINVAL;
15043                 }
15044         }
15045
15046         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
15047                                              new_plane_state, i) {
15048                 /*
15049                  * TODO: Async flip is only supported through the page flip IOCTL
15050                  * as of now. So support currently added for primary plane only.
15051                  * Support for other planes on platforms on which supports
15052                  * this(vlv/chv and icl+) should be added when async flip is
15053                  * enabled in the atomic IOCTL path.
15054                  */
15055                 if (plane->id != PLANE_PRIMARY)
15056                         return -EINVAL;
15057
15058                 /*
15059                  * FIXME: This check is kept generic for all platforms.
15060                  * Need to verify this for all gen9 and gen10 platforms to enable
15061                  * this selectively if required.
15062                  */
15063                 switch (new_plane_state->hw.fb->modifier) {
15064                 case I915_FORMAT_MOD_X_TILED:
15065                 case I915_FORMAT_MOD_Y_TILED:
15066                 case I915_FORMAT_MOD_Yf_TILED:
15067                         break;
15068                 default:
15069                         drm_dbg_kms(&i915->drm,
15070                                     "Linear memory/CCS does not support async flips\n");
15071                         return -EINVAL;
15072                 }
15073
15074                 if (old_plane_state->color_plane[0].stride !=
15075                     new_plane_state->color_plane[0].stride) {
15076                         drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
15077                         return -EINVAL;
15078                 }
15079
15080                 if (old_plane_state->hw.fb->modifier !=
15081                     new_plane_state->hw.fb->modifier) {
15082                         drm_dbg_kms(&i915->drm,
15083                                     "Framebuffer modifiers cannot be changed in async flip\n");
15084                         return -EINVAL;
15085                 }
15086
15087                 if (old_plane_state->hw.fb->format !=
15088                     new_plane_state->hw.fb->format) {
15089                         drm_dbg_kms(&i915->drm,
15090                                     "Framebuffer format cannot be changed in async flip\n");
15091                         return -EINVAL;
15092                 }
15093
15094                 if (old_plane_state->hw.rotation !=
15095                     new_plane_state->hw.rotation) {
15096                         drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
15097                         return -EINVAL;
15098                 }
15099
15100                 if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
15101                     !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
15102                         drm_dbg_kms(&i915->drm,
15103                                     "Plane size/co-ordinates cannot be changed in async flip\n");
15104                         return -EINVAL;
15105                 }
15106
15107                 if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
15108                         drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
15109                         return -EINVAL;
15110                 }
15111
15112                 if (old_plane_state->hw.pixel_blend_mode !=
15113                     new_plane_state->hw.pixel_blend_mode) {
15114                         drm_dbg_kms(&i915->drm,
15115                                     "Pixel blend mode cannot be changed in async flip\n");
15116                         return -EINVAL;
15117                 }
15118
15119                 if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
15120                         drm_dbg_kms(&i915->drm,
15121                                     "Color encoding cannot be changed in async flip\n");
15122                         return -EINVAL;
15123                 }
15124
15125                 if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
15126                         drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
15127                         return -EINVAL;
15128                 }
15129         }
15130
15131         return 0;
15132 }
15133
15134 /**
15135  * intel_atomic_check - validate state object
15136  * @dev: drm device
15137  * @_state: state to validate
15138  */
15139 static int intel_atomic_check(struct drm_device *dev,
15140                               struct drm_atomic_state *_state)
15141 {
15142         struct drm_i915_private *dev_priv = to_i915(dev);
15143         struct intel_atomic_state *state = to_intel_atomic_state(_state);
15144         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15145         struct intel_crtc *crtc;
15146         int ret, i;
15147         bool any_ms = false;
15148
15149         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15150                                             new_crtc_state, i) {
15151                 if (new_crtc_state->inherited != old_crtc_state->inherited)
15152                         new_crtc_state->uapi.mode_changed = true;
15153         }
15154
15155         ret = drm_atomic_helper_check_modeset(dev, &state->base);
15156         if (ret)
15157                 goto fail;
15158
15159         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15160                                             new_crtc_state, i) {
15161                 if (!needs_modeset(new_crtc_state)) {
15162                         /* Light copy */
15163                         intel_crtc_copy_uapi_to_hw_state_nomodeset(new_crtc_state);
15164
15165                         continue;
15166                 }
15167
15168                 ret = intel_crtc_prepare_cleared_state(new_crtc_state);
15169                 if (ret)
15170                         goto fail;
15171
15172                 if (!new_crtc_state->hw.enable)
15173                         continue;
15174
15175                 ret = intel_modeset_pipe_config(new_crtc_state);
15176                 if (ret)
15177                         goto fail;
15178         }
15179
15180         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15181                                             new_crtc_state, i) {
15182                 if (!needs_modeset(new_crtc_state))
15183                         continue;
15184
15185                 ret = intel_modeset_pipe_config_late(new_crtc_state);
15186                 if (ret)
15187                         goto fail;
15188
15189                 intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
15190         }
15191
15192         /**
15193          * Check if fastset is allowed by external dependencies like other
15194          * pipes and transcoders.
15195          *
15196          * Right now it only forces a fullmodeset when the MST master
15197          * transcoder did not changed but the pipe of the master transcoder
15198          * needs a fullmodeset so all slaves also needs to do a fullmodeset or
15199          * in case of port synced crtcs, if one of the synced crtcs
15200          * needs a full modeset, all other synced crtcs should be
15201          * forced a full modeset.
15202          */
15203         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15204                 if (!new_crtc_state->hw.enable || needs_modeset(new_crtc_state))
15205                         continue;
15206
15207                 if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
15208                         enum transcoder master = new_crtc_state->mst_master_transcoder;
15209
15210                         if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
15211                                 new_crtc_state->uapi.mode_changed = true;
15212                                 new_crtc_state->update_pipe = false;
15213                         }
15214                 }
15215
15216                 if (is_trans_port_sync_mode(new_crtc_state)) {
15217                         u8 trans = new_crtc_state->sync_mode_slaves_mask;
15218
15219                         if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
15220                                 trans |= BIT(new_crtc_state->master_transcoder);
15221
15222                         if (intel_cpu_transcoders_need_modeset(state, trans)) {
15223                                 new_crtc_state->uapi.mode_changed = true;
15224                                 new_crtc_state->update_pipe = false;
15225                         }
15226                 }
15227         }
15228
15229         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15230                                             new_crtc_state, i) {
15231                 if (needs_modeset(new_crtc_state)) {
15232                         any_ms = true;
15233                         continue;
15234                 }
15235
15236                 if (!new_crtc_state->update_pipe)
15237                         continue;
15238
15239                 intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
15240         }
15241
15242         if (any_ms && !check_digital_port_conflicts(state)) {
15243                 drm_dbg_kms(&dev_priv->drm,
15244                             "rejecting conflicting digital port configuration\n");
15245                 ret = -EINVAL;
15246                 goto fail;
15247         }
15248
15249         ret = drm_dp_mst_atomic_check(&state->base);
15250         if (ret)
15251                 goto fail;
15252
15253         ret = intel_atomic_check_planes(state);
15254         if (ret)
15255                 goto fail;
15256
15257         /*
15258          * distrust_bios_wm will force a full dbuf recomputation
15259          * but the hardware state will only get updated accordingly
15260          * if state->modeset==true. Hence distrust_bios_wm==true &&
15261          * state->modeset==false is an invalid combination which
15262          * would cause the hardware and software dbuf state to get
15263          * out of sync. We must prevent that.
15264          *
15265          * FIXME clean up this mess and introduce better
15266          * state tracking for dbuf.
15267          */
15268         if (dev_priv->wm.distrust_bios_wm)
15269                 any_ms = true;
15270
15271         intel_fbc_choose_crtc(dev_priv, state);
15272         ret = calc_watermark_data(state);
15273         if (ret)
15274                 goto fail;
15275
15276         ret = intel_bw_atomic_check(state);
15277         if (ret)
15278                 goto fail;
15279
15280         ret = intel_atomic_check_cdclk(state, &any_ms);
15281         if (ret)
15282                 goto fail;
15283
15284         if (any_ms) {
15285                 ret = intel_modeset_checks(state);
15286                 if (ret)
15287                         goto fail;
15288
15289                 ret = intel_modeset_calc_cdclk(state);
15290                 if (ret)
15291                         return ret;
15292
15293                 intel_modeset_clear_plls(state);
15294         }
15295
15296         ret = intel_atomic_check_crtcs(state);
15297         if (ret)
15298                 goto fail;
15299
15300         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15301                                             new_crtc_state, i) {
15302                 if (new_crtc_state->uapi.async_flip) {
15303                         ret = intel_atomic_check_async(state);
15304                         if (ret)
15305                                 goto fail;
15306                 }
15307
15308                 if (!needs_modeset(new_crtc_state) &&
15309                     !new_crtc_state->update_pipe)
15310                         continue;
15311
15312                 intel_dump_pipe_config(new_crtc_state, state,
15313                                        needs_modeset(new_crtc_state) ?
15314                                        "[modeset]" : "[fastset]");
15315         }
15316
15317         return 0;
15318
15319  fail:
15320         if (ret == -EDEADLK)
15321                 return ret;
15322
15323         /*
15324          * FIXME would probably be nice to know which crtc specifically
15325          * caused the failure, in cases where we can pinpoint it.
15326          */
15327         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15328                                             new_crtc_state, i)
15329                 intel_dump_pipe_config(new_crtc_state, state, "[failed]");
15330
15331         return ret;
15332 }
15333
15334 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
15335 {
15336         struct intel_crtc_state *crtc_state;
15337         struct intel_crtc *crtc;
15338         int i, ret;
15339
15340         ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
15341         if (ret < 0)
15342                 return ret;
15343
15344         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
15345                 bool mode_changed = needs_modeset(crtc_state);
15346
15347                 if (mode_changed || crtc_state->update_pipe ||
15348                     crtc_state->uapi.color_mgmt_changed) {
15349                         intel_dsb_prepare(crtc_state);
15350                 }
15351         }
15352
15353         return 0;
15354 }
15355
15356 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
15357 {
15358         struct drm_device *dev = crtc->base.dev;
15359         struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
15360
15361         if (!vblank->max_vblank_count)
15362                 return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
15363
15364         return crtc->base.funcs->get_vblank_counter(&crtc->base);
15365 }
15366
15367 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
15368                                   struct intel_crtc_state *crtc_state)
15369 {
15370         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15371
15372         if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
15373                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15374
15375         if (crtc_state->has_pch_encoder) {
15376                 enum pipe pch_transcoder =
15377                         intel_crtc_pch_transcoder(crtc);
15378
15379                 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
15380         }
15381 }
15382
15383 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
15384                                const struct intel_crtc_state *new_crtc_state)
15385 {
15386         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
15387         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
15388
15389         /*
15390          * Update pipe size and adjust fitter if needed: the reason for this is
15391          * that in compute_mode_changes we check the native mode (not the pfit
15392          * mode) to see if we can flip rather than do a full mode set. In the
15393          * fastboot case, we'll flip, but if we don't update the pipesrc and
15394          * pfit state, we'll end up with a big fb scanned out into the wrong
15395          * sized surface.
15396          */
15397         intel_set_pipe_src_size(new_crtc_state);
15398
15399         /* on skylake this is done by detaching scalers */
15400         if (INTEL_GEN(dev_priv) >= 9) {
15401                 skl_detach_scalers(new_crtc_state);
15402
15403                 if (new_crtc_state->pch_pfit.enabled)
15404                         skl_pfit_enable(new_crtc_state);
15405         } else if (HAS_PCH_SPLIT(dev_priv)) {
15406                 if (new_crtc_state->pch_pfit.enabled)
15407                         ilk_pfit_enable(new_crtc_state);
15408                 else if (old_crtc_state->pch_pfit.enabled)
15409                         ilk_pfit_disable(old_crtc_state);
15410         }
15411
15412         /*
15413          * The register is supposedly single buffered so perhaps
15414          * not 100% correct to do this here. But SKL+ calculate
15415          * this based on the adjust pixel rate so pfit changes do
15416          * affect it and so it must be updated for fastsets.
15417          * HSW/BDW only really need this here for fastboot, after
15418          * that the value should not change without a full modeset.
15419          */
15420         if (INTEL_GEN(dev_priv) >= 9 ||
15421             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
15422                 hsw_set_linetime_wm(new_crtc_state);
15423
15424         if (INTEL_GEN(dev_priv) >= 11)
15425                 icl_set_pipe_chicken(crtc);
15426 }
15427
15428 static void commit_pipe_config(struct intel_atomic_state *state,
15429                                struct intel_crtc *crtc)
15430 {
15431         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15432         const struct intel_crtc_state *old_crtc_state =
15433                 intel_atomic_get_old_crtc_state(state, crtc);
15434         const struct intel_crtc_state *new_crtc_state =
15435                 intel_atomic_get_new_crtc_state(state, crtc);
15436         bool modeset = needs_modeset(new_crtc_state);
15437
15438         /*
15439          * During modesets pipe configuration was programmed as the
15440          * CRTC was enabled.
15441          */
15442         if (!modeset) {
15443                 if (new_crtc_state->uapi.color_mgmt_changed ||
15444                     new_crtc_state->update_pipe)
15445                         intel_color_commit(new_crtc_state);
15446
15447                 if (INTEL_GEN(dev_priv) >= 9)
15448                         skl_detach_scalers(new_crtc_state);
15449
15450                 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
15451                         bdw_set_pipemisc(new_crtc_state);
15452
15453                 if (new_crtc_state->update_pipe)
15454                         intel_pipe_fastset(old_crtc_state, new_crtc_state);
15455
15456                 intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
15457         }
15458
15459         if (dev_priv->display.atomic_update_watermarks)
15460                 dev_priv->display.atomic_update_watermarks(state, crtc);
15461 }
15462
15463 static void intel_enable_crtc(struct intel_atomic_state *state,
15464                               struct intel_crtc *crtc)
15465 {
15466         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15467         const struct intel_crtc_state *new_crtc_state =
15468                 intel_atomic_get_new_crtc_state(state, crtc);
15469
15470         if (!needs_modeset(new_crtc_state))
15471                 return;
15472
15473         intel_crtc_update_active_timings(new_crtc_state);
15474
15475         dev_priv->display.crtc_enable(state, crtc);
15476
15477         /* vblanks work again, re-enable pipe CRC. */
15478         intel_crtc_enable_pipe_crc(crtc);
15479 }
15480
15481 static void intel_update_crtc(struct intel_atomic_state *state,
15482                               struct intel_crtc *crtc)
15483 {
15484         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15485         const struct intel_crtc_state *old_crtc_state =
15486                 intel_atomic_get_old_crtc_state(state, crtc);
15487         struct intel_crtc_state *new_crtc_state =
15488                 intel_atomic_get_new_crtc_state(state, crtc);
15489         bool modeset = needs_modeset(new_crtc_state);
15490
15491         if (!modeset) {
15492                 if (new_crtc_state->preload_luts &&
15493                     (new_crtc_state->uapi.color_mgmt_changed ||
15494                      new_crtc_state->update_pipe))
15495                         intel_color_load_luts(new_crtc_state);
15496
15497                 intel_pre_plane_update(state, crtc);
15498
15499                 if (new_crtc_state->update_pipe)
15500                         intel_encoders_update_pipe(state, crtc);
15501         }
15502
15503         if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
15504                 intel_fbc_disable(crtc);
15505         else
15506                 intel_fbc_enable(state, crtc);
15507
15508         /* Perform vblank evasion around commit operation */
15509         intel_pipe_update_start(new_crtc_state);
15510
15511         commit_pipe_config(state, crtc);
15512
15513         if (INTEL_GEN(dev_priv) >= 9)
15514                 skl_update_planes_on_crtc(state, crtc);
15515         else
15516                 i9xx_update_planes_on_crtc(state, crtc);
15517
15518         intel_pipe_update_end(new_crtc_state);
15519
15520         /*
15521          * We usually enable FIFO underrun interrupts as part of the
15522          * CRTC enable sequence during modesets.  But when we inherit a
15523          * valid pipe configuration from the BIOS we need to take care
15524          * of enabling them on the CRTC's first fastset.
15525          */
15526         if (new_crtc_state->update_pipe && !modeset &&
15527             old_crtc_state->inherited)
15528                 intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
15529 }
15530
15531
15532 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
15533                                           struct intel_crtc_state *old_crtc_state,
15534                                           struct intel_crtc_state *new_crtc_state,
15535                                           struct intel_crtc *crtc)
15536 {
15537         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15538
15539         intel_crtc_disable_planes(state, crtc);
15540
15541         /*
15542          * We need to disable pipe CRC before disabling the pipe,
15543          * or we race against vblank off.
15544          */
15545         intel_crtc_disable_pipe_crc(crtc);
15546
15547         dev_priv->display.crtc_disable(state, crtc);
15548         crtc->active = false;
15549         intel_fbc_disable(crtc);
15550         intel_disable_shared_dpll(old_crtc_state);
15551
15552         /* FIXME unify this for all platforms */
15553         if (!new_crtc_state->hw.active &&
15554             !HAS_GMCH(dev_priv) &&
15555             dev_priv->display.initial_watermarks)
15556                 dev_priv->display.initial_watermarks(state, crtc);
15557 }
15558
15559 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
15560 {
15561         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15562         struct intel_crtc *crtc;
15563         u32 handled = 0;
15564         int i;
15565
15566         /* Only disable port sync and MST slaves */
15567         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15568                                             new_crtc_state, i) {
15569                 if (!needs_modeset(new_crtc_state))
15570                         continue;
15571
15572                 if (!old_crtc_state->hw.active)
15573                         continue;
15574
15575                 /* In case of Transcoder port Sync master slave CRTCs can be
15576                  * assigned in any order and we need to make sure that
15577                  * slave CRTCs are disabled first and then master CRTC since
15578                  * Slave vblanks are masked till Master Vblanks.
15579                  */
15580                 if (!is_trans_port_sync_slave(old_crtc_state) &&
15581                     !intel_dp_mst_is_slave_trans(old_crtc_state))
15582                         continue;
15583
15584                 intel_pre_plane_update(state, crtc);
15585                 intel_old_crtc_state_disables(state, old_crtc_state,
15586                                               new_crtc_state, crtc);
15587                 handled |= BIT(crtc->pipe);
15588         }
15589
15590         /* Disable everything else left on */
15591         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15592                                             new_crtc_state, i) {
15593                 if (!needs_modeset(new_crtc_state) ||
15594                     (handled & BIT(crtc->pipe)))
15595                         continue;
15596
15597                 intel_pre_plane_update(state, crtc);
15598                 if (old_crtc_state->hw.active)
15599                         intel_old_crtc_state_disables(state, old_crtc_state,
15600                                                       new_crtc_state, crtc);
15601         }
15602 }
15603
15604 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
15605 {
15606         struct intel_crtc_state *new_crtc_state;
15607         struct intel_crtc *crtc;
15608         int i;
15609
15610         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15611                 if (!new_crtc_state->hw.active)
15612                         continue;
15613
15614                 intel_enable_crtc(state, crtc);
15615                 intel_update_crtc(state, crtc);
15616         }
15617 }
15618
15619 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
15620 {
15621         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
15622         struct intel_crtc *crtc;
15623         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15624         struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
15625         u8 update_pipes = 0, modeset_pipes = 0;
15626         int i;
15627
15628         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15629                 enum pipe pipe = crtc->pipe;
15630
15631                 if (!new_crtc_state->hw.active)
15632                         continue;
15633
15634                 /* ignore allocations for crtc's that have been turned off. */
15635                 if (!needs_modeset(new_crtc_state)) {
15636                         entries[pipe] = old_crtc_state->wm.skl.ddb;
15637                         update_pipes |= BIT(pipe);
15638                 } else {
15639                         modeset_pipes |= BIT(pipe);
15640                 }
15641         }
15642
15643         /*
15644          * Whenever the number of active pipes changes, we need to make sure we
15645          * update the pipes in the right order so that their ddb allocations
15646          * never overlap with each other between CRTC updates. Otherwise we'll
15647          * cause pipe underruns and other bad stuff.
15648          *
15649          * So first lets enable all pipes that do not need a fullmodeset as
15650          * those don't have any external dependency.
15651          */
15652         while (update_pipes) {
15653                 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15654                                                     new_crtc_state, i) {
15655                         enum pipe pipe = crtc->pipe;
15656
15657                         if ((update_pipes & BIT(pipe)) == 0)
15658                                 continue;
15659
15660                         if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15661                                                         entries, I915_MAX_PIPES, pipe))
15662                                 continue;
15663
15664                         entries[pipe] = new_crtc_state->wm.skl.ddb;
15665                         update_pipes &= ~BIT(pipe);
15666
15667                         intel_update_crtc(state, crtc);
15668
15669                         /*
15670                          * If this is an already active pipe, it's DDB changed,
15671                          * and this isn't the last pipe that needs updating
15672                          * then we need to wait for a vblank to pass for the
15673                          * new ddb allocation to take effect.
15674                          */
15675                         if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
15676                                                  &old_crtc_state->wm.skl.ddb) &&
15677                             (update_pipes | modeset_pipes))
15678                                 intel_wait_for_vblank(dev_priv, pipe);
15679                 }
15680         }
15681
15682         update_pipes = modeset_pipes;
15683
15684         /*
15685          * Enable all pipes that needs a modeset and do not depends on other
15686          * pipes
15687          */
15688         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15689                 enum pipe pipe = crtc->pipe;
15690
15691                 if ((modeset_pipes & BIT(pipe)) == 0)
15692                         continue;
15693
15694                 if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
15695                     is_trans_port_sync_master(new_crtc_state))
15696                         continue;
15697
15698                 modeset_pipes &= ~BIT(pipe);
15699
15700                 intel_enable_crtc(state, crtc);
15701         }
15702
15703         /*
15704          * Then we enable all remaining pipes that depend on other
15705          * pipes: MST slaves and port sync masters.
15706          */
15707         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15708                 enum pipe pipe = crtc->pipe;
15709
15710                 if ((modeset_pipes & BIT(pipe)) == 0)
15711                         continue;
15712
15713                 modeset_pipes &= ~BIT(pipe);
15714
15715                 intel_enable_crtc(state, crtc);
15716         }
15717
15718         /*
15719          * Finally we do the plane updates/etc. for all pipes that got enabled.
15720          */
15721         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15722                 enum pipe pipe = crtc->pipe;
15723
15724                 if ((update_pipes & BIT(pipe)) == 0)
15725                         continue;
15726
15727                 drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
15728                                                                         entries, I915_MAX_PIPES, pipe));
15729
15730                 entries[pipe] = new_crtc_state->wm.skl.ddb;
15731                 update_pipes &= ~BIT(pipe);
15732
15733                 intel_update_crtc(state, crtc);
15734         }
15735
15736         drm_WARN_ON(&dev_priv->drm, modeset_pipes);
15737         drm_WARN_ON(&dev_priv->drm, update_pipes);
15738 }
15739
15740 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
15741 {
15742         struct intel_atomic_state *state, *next;
15743         struct llist_node *freed;
15744
15745         freed = llist_del_all(&dev_priv->atomic_helper.free_list);
15746         llist_for_each_entry_safe(state, next, freed, freed)
15747                 drm_atomic_state_put(&state->base);
15748 }
15749
15750 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
15751 {
15752         struct drm_i915_private *dev_priv =
15753                 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
15754
15755         intel_atomic_helper_free_state(dev_priv);
15756 }
15757
15758 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
15759 {
15760         struct wait_queue_entry wait_fence, wait_reset;
15761         struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
15762
15763         init_wait_entry(&wait_fence, 0);
15764         init_wait_entry(&wait_reset, 0);
15765         for (;;) {
15766                 prepare_to_wait(&intel_state->commit_ready.wait,
15767                                 &wait_fence, TASK_UNINTERRUPTIBLE);
15768                 prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15769                                               I915_RESET_MODESET),
15770                                 &wait_reset, TASK_UNINTERRUPTIBLE);
15771
15772
15773                 if (i915_sw_fence_done(&intel_state->commit_ready) ||
15774                     test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
15775                         break;
15776
15777                 schedule();
15778         }
15779         finish_wait(&intel_state->commit_ready.wait, &wait_fence);
15780         finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
15781                                   I915_RESET_MODESET),
15782                     &wait_reset);
15783 }
15784
15785 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
15786 {
15787         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
15788         struct intel_crtc *crtc;
15789         int i;
15790
15791         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15792                                             new_crtc_state, i)
15793                 intel_dsb_cleanup(old_crtc_state);
15794 }
15795
15796 static void intel_atomic_cleanup_work(struct work_struct *work)
15797 {
15798         struct intel_atomic_state *state =
15799                 container_of(work, struct intel_atomic_state, base.commit_work);
15800         struct drm_i915_private *i915 = to_i915(state->base.dev);
15801
15802         intel_cleanup_dsbs(state);
15803         drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
15804         drm_atomic_helper_commit_cleanup_done(&state->base);
15805         drm_atomic_state_put(&state->base);
15806
15807         intel_atomic_helper_free_state(i915);
15808 }
15809
15810 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
15811 {
15812         struct drm_device *dev = state->base.dev;
15813         struct drm_i915_private *dev_priv = to_i915(dev);
15814         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
15815         struct intel_crtc *crtc;
15816         u64 put_domains[I915_MAX_PIPES] = {};
15817         intel_wakeref_t wakeref = 0;
15818         int i;
15819
15820         intel_atomic_commit_fence_wait(state);
15821
15822         drm_atomic_helper_wait_for_dependencies(&state->base);
15823
15824         if (state->modeset)
15825                 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
15826
15827         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15828                                             new_crtc_state, i) {
15829                 if (needs_modeset(new_crtc_state) ||
15830                     new_crtc_state->update_pipe) {
15831
15832                         put_domains[crtc->pipe] =
15833                                 modeset_get_crtc_power_domains(new_crtc_state);
15834                 }
15835         }
15836
15837         intel_commit_modeset_disables(state);
15838
15839         /* FIXME: Eventually get rid of our crtc->config pointer */
15840         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
15841                 crtc->config = new_crtc_state;
15842
15843         if (state->modeset) {
15844                 drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
15845
15846                 intel_set_cdclk_pre_plane_update(state);
15847
15848                 intel_modeset_verify_disabled(dev_priv, state);
15849         }
15850
15851         intel_sagv_pre_plane_update(state);
15852
15853         /* Complete the events for pipes that have now been disabled */
15854         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15855                 bool modeset = needs_modeset(new_crtc_state);
15856
15857                 /* Complete events for now disable pipes here. */
15858                 if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
15859                         spin_lock_irq(&dev->event_lock);
15860                         drm_crtc_send_vblank_event(&crtc->base,
15861                                                    new_crtc_state->uapi.event);
15862                         spin_unlock_irq(&dev->event_lock);
15863
15864                         new_crtc_state->uapi.event = NULL;
15865                 }
15866         }
15867
15868         if (state->modeset)
15869                 intel_encoders_update_prepare(state);
15870
15871         intel_dbuf_pre_plane_update(state);
15872
15873         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15874                 if (new_crtc_state->uapi.async_flip)
15875                         skl_enable_flip_done(crtc);
15876         }
15877
15878         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
15879         dev_priv->display.commit_modeset_enables(state);
15880
15881         if (state->modeset) {
15882                 intel_encoders_update_complete(state);
15883
15884                 intel_set_cdclk_post_plane_update(state);
15885         }
15886
15887         /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
15888          * already, but still need the state for the delayed optimization. To
15889          * fix this:
15890          * - wrap the optimization/post_plane_update stuff into a per-crtc work.
15891          * - schedule that vblank worker _before_ calling hw_done
15892          * - at the start of commit_tail, cancel it _synchrously
15893          * - switch over to the vblank wait helper in the core after that since
15894          *   we don't need out special handling any more.
15895          */
15896         drm_atomic_helper_wait_for_flip_done(dev, &state->base);
15897
15898         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
15899                 if (new_crtc_state->uapi.async_flip)
15900                         skl_disable_flip_done(crtc);
15901
15902                 if (new_crtc_state->hw.active &&
15903                     !needs_modeset(new_crtc_state) &&
15904                     !new_crtc_state->preload_luts &&
15905                     (new_crtc_state->uapi.color_mgmt_changed ||
15906                      new_crtc_state->update_pipe))
15907                         intel_color_load_luts(new_crtc_state);
15908         }
15909
15910         /*
15911          * Now that the vblank has passed, we can go ahead and program the
15912          * optimal watermarks on platforms that need two-step watermark
15913          * programming.
15914          *
15915          * TODO: Move this (and other cleanup) to an async worker eventually.
15916          */
15917         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
15918                                             new_crtc_state, i) {
15919                 /*
15920                  * Gen2 reports pipe underruns whenever all planes are disabled.
15921                  * So re-enable underrun reporting after some planes get enabled.
15922                  *
15923                  * We do this before .optimize_watermarks() so that we have a
15924                  * chance of catching underruns with the intermediate watermarks
15925                  * vs. the new plane configuration.
15926                  */
15927                 if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
15928                         intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
15929
15930                 if (dev_priv->display.optimize_watermarks)
15931                         dev_priv->display.optimize_watermarks(state, crtc);
15932         }
15933
15934         intel_dbuf_post_plane_update(state);
15935
15936         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
15937                 intel_post_plane_update(state, crtc);
15938
15939                 if (put_domains[i])
15940                         modeset_put_power_domains(dev_priv, put_domains[i]);
15941
15942                 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
15943
15944                 /*
15945                  * DSB cleanup is done in cleanup_work aligning with framebuffer
15946                  * cleanup. So copy and reset the dsb structure to sync with
15947                  * commit_done and later do dsb cleanup in cleanup_work.
15948                  */
15949                 old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
15950         }
15951
15952         /* Underruns don't always raise interrupts, so check manually */
15953         intel_check_cpu_fifo_underruns(dev_priv);
15954         intel_check_pch_fifo_underruns(dev_priv);
15955
15956         if (state->modeset)
15957                 intel_verify_planes(state);
15958
15959         intel_sagv_post_plane_update(state);
15960
15961         drm_atomic_helper_commit_hw_done(&state->base);
15962
15963         if (state->modeset) {
15964                 /* As one of the primary mmio accessors, KMS has a high
15965                  * likelihood of triggering bugs in unclaimed access. After we
15966                  * finish modesetting, see if an error has been flagged, and if
15967                  * so enable debugging for the next modeset - and hope we catch
15968                  * the culprit.
15969                  */
15970                 intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
15971                 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
15972         }
15973         intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
15974
15975         /*
15976          * Defer the cleanup of the old state to a separate worker to not
15977          * impede the current task (userspace for blocking modesets) that
15978          * are executed inline. For out-of-line asynchronous modesets/flips,
15979          * deferring to a new worker seems overkill, but we would place a
15980          * schedule point (cond_resched()) here anyway to keep latencies
15981          * down.
15982          */
15983         INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
15984         queue_work(system_highpri_wq, &state->base.commit_work);
15985 }
15986
15987 static void intel_atomic_commit_work(struct work_struct *work)
15988 {
15989         struct intel_atomic_state *state =
15990                 container_of(work, struct intel_atomic_state, base.commit_work);
15991
15992         intel_atomic_commit_tail(state);
15993 }
15994
15995 static int __i915_sw_fence_call
15996 intel_atomic_commit_ready(struct i915_sw_fence *fence,
15997                           enum i915_sw_fence_notify notify)
15998 {
15999         struct intel_atomic_state *state =
16000                 container_of(fence, struct intel_atomic_state, commit_ready);
16001
16002         switch (notify) {
16003         case FENCE_COMPLETE:
16004                 /* we do blocking waits in the worker, nothing to do here */
16005                 break;
16006         case FENCE_FREE:
16007                 {
16008                         struct intel_atomic_helper *helper =
16009                                 &to_i915(state->base.dev)->atomic_helper;
16010
16011                         if (llist_add(&state->freed, &helper->free_list))
16012                                 schedule_work(&helper->free_work);
16013                         break;
16014                 }
16015         }
16016
16017         return NOTIFY_DONE;
16018 }
16019
16020 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
16021 {
16022         struct intel_plane_state *old_plane_state, *new_plane_state;
16023         struct intel_plane *plane;
16024         int i;
16025
16026         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
16027                                              new_plane_state, i)
16028                 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16029                                         to_intel_frontbuffer(new_plane_state->hw.fb),
16030                                         plane->frontbuffer_bit);
16031 }
16032
16033 static int intel_atomic_commit(struct drm_device *dev,
16034                                struct drm_atomic_state *_state,
16035                                bool nonblock)
16036 {
16037         struct intel_atomic_state *state = to_intel_atomic_state(_state);
16038         struct drm_i915_private *dev_priv = to_i915(dev);
16039         int ret = 0;
16040
16041         state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
16042
16043         drm_atomic_state_get(&state->base);
16044         i915_sw_fence_init(&state->commit_ready,
16045                            intel_atomic_commit_ready);
16046
16047         /*
16048          * The intel_legacy_cursor_update() fast path takes care
16049          * of avoiding the vblank waits for simple cursor
16050          * movement and flips. For cursor on/off and size changes,
16051          * we want to perform the vblank waits so that watermark
16052          * updates happen during the correct frames. Gen9+ have
16053          * double buffered watermarks and so shouldn't need this.
16054          *
16055          * Unset state->legacy_cursor_update before the call to
16056          * drm_atomic_helper_setup_commit() because otherwise
16057          * drm_atomic_helper_wait_for_flip_done() is a noop and
16058          * we get FIFO underruns because we didn't wait
16059          * for vblank.
16060          *
16061          * FIXME doing watermarks and fb cleanup from a vblank worker
16062          * (assuming we had any) would solve these problems.
16063          */
16064         if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
16065                 struct intel_crtc_state *new_crtc_state;
16066                 struct intel_crtc *crtc;
16067                 int i;
16068
16069                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16070                         if (new_crtc_state->wm.need_postvbl_update ||
16071                             new_crtc_state->update_wm_post)
16072                                 state->base.legacy_cursor_update = false;
16073         }
16074
16075         ret = intel_atomic_prepare_commit(state);
16076         if (ret) {
16077                 drm_dbg_atomic(&dev_priv->drm,
16078                                "Preparing state failed with %i\n", ret);
16079                 i915_sw_fence_commit(&state->commit_ready);
16080                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
16081                 return ret;
16082         }
16083
16084         ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
16085         if (!ret)
16086                 ret = drm_atomic_helper_swap_state(&state->base, true);
16087         if (!ret)
16088                 intel_atomic_swap_global_state(state);
16089
16090         if (ret) {
16091                 struct intel_crtc_state *new_crtc_state;
16092                 struct intel_crtc *crtc;
16093                 int i;
16094
16095                 i915_sw_fence_commit(&state->commit_ready);
16096
16097                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
16098                         intel_dsb_cleanup(new_crtc_state);
16099
16100                 drm_atomic_helper_cleanup_planes(dev, &state->base);
16101                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
16102                 return ret;
16103         }
16104         dev_priv->wm.distrust_bios_wm = false;
16105         intel_shared_dpll_swap_state(state);
16106         intel_atomic_track_fbs(state);
16107
16108         drm_atomic_state_get(&state->base);
16109         INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
16110
16111         i915_sw_fence_commit(&state->commit_ready);
16112         if (nonblock && state->modeset) {
16113                 queue_work(dev_priv->modeset_wq, &state->base.commit_work);
16114         } else if (nonblock) {
16115                 queue_work(dev_priv->flip_wq, &state->base.commit_work);
16116         } else {
16117                 if (state->modeset)
16118                         flush_workqueue(dev_priv->modeset_wq);
16119                 intel_atomic_commit_tail(state);
16120         }
16121
16122         return 0;
16123 }
16124
16125 struct wait_rps_boost {
16126         struct wait_queue_entry wait;
16127
16128         struct drm_crtc *crtc;
16129         struct i915_request *request;
16130 };
16131
16132 static int do_rps_boost(struct wait_queue_entry *_wait,
16133                         unsigned mode, int sync, void *key)
16134 {
16135         struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
16136         struct i915_request *rq = wait->request;
16137
16138         /*
16139          * If we missed the vblank, but the request is already running it
16140          * is reasonable to assume that it will complete before the next
16141          * vblank without our intervention, so leave RPS alone.
16142          */
16143         if (!i915_request_started(rq))
16144                 intel_rps_boost(rq);
16145         i915_request_put(rq);
16146
16147         drm_crtc_vblank_put(wait->crtc);
16148
16149         list_del(&wait->wait.entry);
16150         kfree(wait);
16151         return 1;
16152 }
16153
16154 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
16155                                        struct dma_fence *fence)
16156 {
16157         struct wait_rps_boost *wait;
16158
16159         if (!dma_fence_is_i915(fence))
16160                 return;
16161
16162         if (INTEL_GEN(to_i915(crtc->dev)) < 6)
16163                 return;
16164
16165         if (drm_crtc_vblank_get(crtc))
16166                 return;
16167
16168         wait = kmalloc(sizeof(*wait), GFP_KERNEL);
16169         if (!wait) {
16170                 drm_crtc_vblank_put(crtc);
16171                 return;
16172         }
16173
16174         wait->request = to_request(dma_fence_get(fence));
16175         wait->crtc = crtc;
16176
16177         wait->wait.func = do_rps_boost;
16178         wait->wait.flags = 0;
16179
16180         add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
16181 }
16182
16183 static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
16184 {
16185         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
16186         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16187         struct drm_framebuffer *fb = plane_state->hw.fb;
16188         struct i915_vma *vma;
16189
16190         if (plane->id == PLANE_CURSOR &&
16191             INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
16192                 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
16193                 const int align = intel_cursor_alignment(dev_priv);
16194                 int err;
16195
16196                 err = i915_gem_object_attach_phys(obj, align);
16197                 if (err)
16198                         return err;
16199         }
16200
16201         vma = intel_pin_and_fence_fb_obj(fb,
16202                                          &plane_state->view,
16203                                          intel_plane_uses_fence(plane_state),
16204                                          &plane_state->flags);
16205         if (IS_ERR(vma))
16206                 return PTR_ERR(vma);
16207
16208         plane_state->vma = vma;
16209
16210         return 0;
16211 }
16212
16213 static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
16214 {
16215         struct i915_vma *vma;
16216
16217         vma = fetch_and_zero(&old_plane_state->vma);
16218         if (vma)
16219                 intel_unpin_fb_vma(vma, old_plane_state->flags);
16220 }
16221
16222 static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
16223 {
16224         struct i915_sched_attr attr = {
16225                 .priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
16226         };
16227
16228         i915_gem_object_wait_priority(obj, 0, &attr);
16229 }
16230
16231 /**
16232  * intel_prepare_plane_fb - Prepare fb for usage on plane
16233  * @_plane: drm plane to prepare for
16234  * @_new_plane_state: the plane state being prepared
16235  *
16236  * Prepares a framebuffer for usage on a display plane.  Generally this
16237  * involves pinning the underlying object and updating the frontbuffer tracking
16238  * bits.  Some older platforms need special physical address handling for
16239  * cursor planes.
16240  *
16241  * Returns 0 on success, negative error code on failure.
16242  */
16243 int
16244 intel_prepare_plane_fb(struct drm_plane *_plane,
16245                        struct drm_plane_state *_new_plane_state)
16246 {
16247         struct intel_plane *plane = to_intel_plane(_plane);
16248         struct intel_plane_state *new_plane_state =
16249                 to_intel_plane_state(_new_plane_state);
16250         struct intel_atomic_state *state =
16251                 to_intel_atomic_state(new_plane_state->uapi.state);
16252         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
16253         const struct intel_plane_state *old_plane_state =
16254                 intel_atomic_get_old_plane_state(state, plane);
16255         struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
16256         struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
16257         int ret;
16258
16259         if (old_obj) {
16260                 const struct intel_crtc_state *crtc_state =
16261                         intel_atomic_get_new_crtc_state(state,
16262                                                         to_intel_crtc(old_plane_state->hw.crtc));
16263
16264                 /* Big Hammer, we also need to ensure that any pending
16265                  * MI_WAIT_FOR_EVENT inside a user batch buffer on the
16266                  * current scanout is retired before unpinning the old
16267                  * framebuffer. Note that we rely on userspace rendering
16268                  * into the buffer attached to the pipe they are waiting
16269                  * on. If not, userspace generates a GPU hang with IPEHR
16270                  * point to the MI_WAIT_FOR_EVENT.
16271                  *
16272                  * This should only fail upon a hung GPU, in which case we
16273                  * can safely continue.
16274                  */
16275                 if (needs_modeset(crtc_state)) {
16276                         ret = i915_sw_fence_await_reservation(&state->commit_ready,
16277                                                               old_obj->base.resv, NULL,
16278                                                               false, 0,
16279                                                               GFP_KERNEL);
16280                         if (ret < 0)
16281                                 return ret;
16282                 }
16283         }
16284
16285         if (new_plane_state->uapi.fence) { /* explicit fencing */
16286                 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
16287                                                     new_plane_state->uapi.fence,
16288                                                     i915_fence_timeout(dev_priv),
16289                                                     GFP_KERNEL);
16290                 if (ret < 0)
16291                         return ret;
16292         }
16293
16294         if (!obj)
16295                 return 0;
16296
16297         ret = i915_gem_object_pin_pages(obj);
16298         if (ret)
16299                 return ret;
16300
16301         ret = intel_plane_pin_fb(new_plane_state);
16302
16303         i915_gem_object_unpin_pages(obj);
16304         if (ret)
16305                 return ret;
16306
16307         fb_obj_bump_render_priority(obj);
16308         i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
16309
16310         if (!new_plane_state->uapi.fence) { /* implicit fencing */
16311                 struct dma_fence *fence;
16312
16313                 ret = i915_sw_fence_await_reservation(&state->commit_ready,
16314                                                       obj->base.resv, NULL,
16315                                                       false,
16316                                                       i915_fence_timeout(dev_priv),
16317                                                       GFP_KERNEL);
16318                 if (ret < 0)
16319                         goto unpin_fb;
16320
16321                 fence = dma_resv_get_excl_rcu(obj->base.resv);
16322                 if (fence) {
16323                         add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16324                                                    fence);
16325                         dma_fence_put(fence);
16326                 }
16327         } else {
16328                 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
16329                                            new_plane_state->uapi.fence);
16330         }
16331
16332         /*
16333          * We declare pageflips to be interactive and so merit a small bias
16334          * towards upclocking to deliver the frame on time. By only changing
16335          * the RPS thresholds to sample more regularly and aim for higher
16336          * clocks we can hopefully deliver low power workloads (like kodi)
16337          * that are not quite steady state without resorting to forcing
16338          * maximum clocks following a vblank miss (see do_rps_boost()).
16339          */
16340         if (!state->rps_interactive) {
16341                 intel_rps_mark_interactive(&dev_priv->gt.rps, true);
16342                 state->rps_interactive = true;
16343         }
16344
16345         return 0;
16346
16347 unpin_fb:
16348         intel_plane_unpin_fb(new_plane_state);
16349
16350         return ret;
16351 }
16352
16353 /**
16354  * intel_cleanup_plane_fb - Cleans up an fb after plane use
16355  * @plane: drm plane to clean up for
16356  * @_old_plane_state: the state from the previous modeset
16357  *
16358  * Cleans up a framebuffer that has just been removed from a plane.
16359  */
16360 void
16361 intel_cleanup_plane_fb(struct drm_plane *plane,
16362                        struct drm_plane_state *_old_plane_state)
16363 {
16364         struct intel_plane_state *old_plane_state =
16365                 to_intel_plane_state(_old_plane_state);
16366         struct intel_atomic_state *state =
16367                 to_intel_atomic_state(old_plane_state->uapi.state);
16368         struct drm_i915_private *dev_priv = to_i915(plane->dev);
16369         struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
16370
16371         if (!obj)
16372                 return;
16373
16374         if (state->rps_interactive) {
16375                 intel_rps_mark_interactive(&dev_priv->gt.rps, false);
16376                 state->rps_interactive = false;
16377         }
16378
16379         /* Should only be called after a successful intel_prepare_plane_fb()! */
16380         intel_plane_unpin_fb(old_plane_state);
16381 }
16382
16383 /**
16384  * intel_plane_destroy - destroy a plane
16385  * @plane: plane to destroy
16386  *
16387  * Common destruction function for all types of planes (primary, cursor,
16388  * sprite).
16389  */
16390 void intel_plane_destroy(struct drm_plane *plane)
16391 {
16392         drm_plane_cleanup(plane);
16393         kfree(to_intel_plane(plane));
16394 }
16395
16396 static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
16397                                             u32 format, u64 modifier)
16398 {
16399         switch (modifier) {
16400         case DRM_FORMAT_MOD_LINEAR:
16401         case I915_FORMAT_MOD_X_TILED:
16402                 break;
16403         default:
16404                 return false;
16405         }
16406
16407         switch (format) {
16408         case DRM_FORMAT_C8:
16409         case DRM_FORMAT_RGB565:
16410         case DRM_FORMAT_XRGB1555:
16411         case DRM_FORMAT_XRGB8888:
16412                 return modifier == DRM_FORMAT_MOD_LINEAR ||
16413                         modifier == I915_FORMAT_MOD_X_TILED;
16414         default:
16415                 return false;
16416         }
16417 }
16418
16419 static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
16420                                             u32 format, u64 modifier)
16421 {
16422         switch (modifier) {
16423         case DRM_FORMAT_MOD_LINEAR:
16424         case I915_FORMAT_MOD_X_TILED:
16425                 break;
16426         default:
16427                 return false;
16428         }
16429
16430         switch (format) {
16431         case DRM_FORMAT_C8:
16432         case DRM_FORMAT_RGB565:
16433         case DRM_FORMAT_XRGB8888:
16434         case DRM_FORMAT_XBGR8888:
16435         case DRM_FORMAT_ARGB8888:
16436         case DRM_FORMAT_ABGR8888:
16437         case DRM_FORMAT_XRGB2101010:
16438         case DRM_FORMAT_XBGR2101010:
16439         case DRM_FORMAT_ARGB2101010:
16440         case DRM_FORMAT_ABGR2101010:
16441         case DRM_FORMAT_XBGR16161616F:
16442                 return modifier == DRM_FORMAT_MOD_LINEAR ||
16443                         modifier == I915_FORMAT_MOD_X_TILED;
16444         default:
16445                 return false;
16446         }
16447 }
16448
16449 static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
16450                                               u32 format, u64 modifier)
16451 {
16452         return modifier == DRM_FORMAT_MOD_LINEAR &&
16453                 format == DRM_FORMAT_ARGB8888;
16454 }
16455
16456 static const struct drm_plane_funcs i965_plane_funcs = {
16457         .update_plane = drm_atomic_helper_update_plane,
16458         .disable_plane = drm_atomic_helper_disable_plane,
16459         .destroy = intel_plane_destroy,
16460         .atomic_duplicate_state = intel_plane_duplicate_state,
16461         .atomic_destroy_state = intel_plane_destroy_state,
16462         .format_mod_supported = i965_plane_format_mod_supported,
16463 };
16464
16465 static const struct drm_plane_funcs i8xx_plane_funcs = {
16466         .update_plane = drm_atomic_helper_update_plane,
16467         .disable_plane = drm_atomic_helper_disable_plane,
16468         .destroy = intel_plane_destroy,
16469         .atomic_duplicate_state = intel_plane_duplicate_state,
16470         .atomic_destroy_state = intel_plane_destroy_state,
16471         .format_mod_supported = i8xx_plane_format_mod_supported,
16472 };
16473
16474 static int
16475 intel_legacy_cursor_update(struct drm_plane *_plane,
16476                            struct drm_crtc *_crtc,
16477                            struct drm_framebuffer *fb,
16478                            int crtc_x, int crtc_y,
16479                            unsigned int crtc_w, unsigned int crtc_h,
16480                            u32 src_x, u32 src_y,
16481                            u32 src_w, u32 src_h,
16482                            struct drm_modeset_acquire_ctx *ctx)
16483 {
16484         struct intel_plane *plane = to_intel_plane(_plane);
16485         struct intel_crtc *crtc = to_intel_crtc(_crtc);
16486         struct intel_plane_state *old_plane_state =
16487                 to_intel_plane_state(plane->base.state);
16488         struct intel_plane_state *new_plane_state;
16489         struct intel_crtc_state *crtc_state =
16490                 to_intel_crtc_state(crtc->base.state);
16491         struct intel_crtc_state *new_crtc_state;
16492         int ret;
16493
16494         /*
16495          * When crtc is inactive or there is a modeset pending,
16496          * wait for it to complete in the slowpath
16497          */
16498         if (!crtc_state->hw.active || needs_modeset(crtc_state) ||
16499             crtc_state->update_pipe)
16500                 goto slow;
16501
16502         /*
16503          * Don't do an async update if there is an outstanding commit modifying
16504          * the plane.  This prevents our async update's changes from getting
16505          * overridden by a previous synchronous update's state.
16506          */
16507         if (old_plane_state->uapi.commit &&
16508             !try_wait_for_completion(&old_plane_state->uapi.commit->hw_done))
16509                 goto slow;
16510
16511         /*
16512          * If any parameters change that may affect watermarks,
16513          * take the slowpath. Only changing fb or position should be
16514          * in the fastpath.
16515          */
16516         if (old_plane_state->uapi.crtc != &crtc->base ||
16517             old_plane_state->uapi.src_w != src_w ||
16518             old_plane_state->uapi.src_h != src_h ||
16519             old_plane_state->uapi.crtc_w != crtc_w ||
16520             old_plane_state->uapi.crtc_h != crtc_h ||
16521             !old_plane_state->uapi.fb != !fb)
16522                 goto slow;
16523
16524         new_plane_state = to_intel_plane_state(intel_plane_duplicate_state(&plane->base));
16525         if (!new_plane_state)
16526                 return -ENOMEM;
16527
16528         new_crtc_state = to_intel_crtc_state(intel_crtc_duplicate_state(&crtc->base));
16529         if (!new_crtc_state) {
16530                 ret = -ENOMEM;
16531                 goto out_free;
16532         }
16533
16534         drm_atomic_set_fb_for_plane(&new_plane_state->uapi, fb);
16535
16536         new_plane_state->uapi.src_x = src_x;
16537         new_plane_state->uapi.src_y = src_y;
16538         new_plane_state->uapi.src_w = src_w;
16539         new_plane_state->uapi.src_h = src_h;
16540         new_plane_state->uapi.crtc_x = crtc_x;
16541         new_plane_state->uapi.crtc_y = crtc_y;
16542         new_plane_state->uapi.crtc_w = crtc_w;
16543         new_plane_state->uapi.crtc_h = crtc_h;
16544
16545         intel_plane_copy_uapi_to_hw_state(new_plane_state, new_plane_state);
16546
16547         ret = intel_plane_atomic_check_with_state(crtc_state, new_crtc_state,
16548                                                   old_plane_state, new_plane_state);
16549         if (ret)
16550                 goto out_free;
16551
16552         ret = intel_plane_pin_fb(new_plane_state);
16553         if (ret)
16554                 goto out_free;
16555
16556         intel_frontbuffer_flush(to_intel_frontbuffer(new_plane_state->hw.fb),
16557                                 ORIGIN_FLIP);
16558         intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
16559                                 to_intel_frontbuffer(new_plane_state->hw.fb),
16560                                 plane->frontbuffer_bit);
16561
16562         /* Swap plane state */
16563         plane->base.state = &new_plane_state->uapi;
16564
16565         /*
16566          * We cannot swap crtc_state as it may be in use by an atomic commit or
16567          * page flip that's running simultaneously. If we swap crtc_state and
16568          * destroy the old state, we will cause a use-after-free there.
16569          *
16570          * Only update active_planes, which is needed for our internal
16571          * bookkeeping. Either value will do the right thing when updating
16572          * planes atomically. If the cursor was part of the atomic update then
16573          * we would have taken the slowpath.
16574          */
16575         crtc_state->active_planes = new_crtc_state->active_planes;
16576
16577         if (new_plane_state->uapi.visible)
16578                 intel_update_plane(plane, crtc_state, new_plane_state);
16579         else
16580                 intel_disable_plane(plane, crtc_state);
16581
16582         intel_plane_unpin_fb(old_plane_state);
16583
16584 out_free:
16585         if (new_crtc_state)
16586                 intel_crtc_destroy_state(&crtc->base, &new_crtc_state->uapi);
16587         if (ret)
16588                 intel_plane_destroy_state(&plane->base, &new_plane_state->uapi);
16589         else
16590                 intel_plane_destroy_state(&plane->base, &old_plane_state->uapi);
16591         return ret;
16592
16593 slow:
16594         return drm_atomic_helper_update_plane(&plane->base, &crtc->base, fb,
16595                                               crtc_x, crtc_y, crtc_w, crtc_h,
16596                                               src_x, src_y, src_w, src_h, ctx);
16597 }
16598
16599 static const struct drm_plane_funcs intel_cursor_plane_funcs = {
16600         .update_plane = intel_legacy_cursor_update,
16601         .disable_plane = drm_atomic_helper_disable_plane,
16602         .destroy = intel_plane_destroy,
16603         .atomic_duplicate_state = intel_plane_duplicate_state,
16604         .atomic_destroy_state = intel_plane_destroy_state,
16605         .format_mod_supported = intel_cursor_format_mod_supported,
16606 };
16607
16608 static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
16609                                enum i9xx_plane_id i9xx_plane)
16610 {
16611         if (!HAS_FBC(dev_priv))
16612                 return false;
16613
16614         if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16615                 return i9xx_plane == PLANE_A; /* tied to pipe A */
16616         else if (IS_IVYBRIDGE(dev_priv))
16617                 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
16618                         i9xx_plane == PLANE_C;
16619         else if (INTEL_GEN(dev_priv) >= 4)
16620                 return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
16621         else
16622                 return i9xx_plane == PLANE_A;
16623 }
16624
16625 static struct intel_plane *
16626 intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
16627 {
16628         struct intel_plane *plane;
16629         const struct drm_plane_funcs *plane_funcs;
16630         unsigned int supported_rotations;
16631         const u32 *formats;
16632         int num_formats;
16633         int ret, zpos;
16634
16635         if (INTEL_GEN(dev_priv) >= 9)
16636                 return skl_universal_plane_create(dev_priv, pipe,
16637                                                   PLANE_PRIMARY);
16638
16639         plane = intel_plane_alloc();
16640         if (IS_ERR(plane))
16641                 return plane;
16642
16643         plane->pipe = pipe;
16644         /*
16645          * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
16646          * port is hooked to pipe B. Hence we want plane A feeding pipe B.
16647          */
16648         if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4 &&
16649             INTEL_NUM_PIPES(dev_priv) == 2)
16650                 plane->i9xx_plane = (enum i9xx_plane_id) !pipe;
16651         else
16652                 plane->i9xx_plane = (enum i9xx_plane_id) pipe;
16653         plane->id = PLANE_PRIMARY;
16654         plane->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, plane->id);
16655
16656         plane->has_fbc = i9xx_plane_has_fbc(dev_priv, plane->i9xx_plane);
16657         if (plane->has_fbc) {
16658                 struct intel_fbc *fbc = &dev_priv->fbc;
16659
16660                 fbc->possible_framebuffer_bits |= plane->frontbuffer_bit;
16661         }
16662
16663         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
16664                 formats = vlv_primary_formats;
16665                 num_formats = ARRAY_SIZE(vlv_primary_formats);
16666         } else if (INTEL_GEN(dev_priv) >= 4) {
16667                 /*
16668                  * WaFP16GammaEnabling:ivb
16669                  * "Workaround : When using the 64-bit format, the plane
16670                  *  output on each color channel has one quarter amplitude.
16671                  *  It can be brought up to full amplitude by using pipe
16672                  *  gamma correction or pipe color space conversion to
16673                  *  multiply the plane output by four."
16674                  *
16675                  * There is no dedicated plane gamma for the primary plane,
16676                  * and using the pipe gamma/csc could conflict with other
16677                  * planes, so we choose not to expose fp16 on IVB primary
16678                  * planes. HSW primary planes no longer have this problem.
16679                  */
16680                 if (IS_IVYBRIDGE(dev_priv)) {
16681                         formats = ivb_primary_formats;
16682                         num_formats = ARRAY_SIZE(ivb_primary_formats);
16683                 } else {
16684                         formats = i965_primary_formats;
16685                         num_formats = ARRAY_SIZE(i965_primary_formats);
16686                 }
16687         } else {
16688                 formats = i8xx_primary_formats;
16689                 num_formats = ARRAY_SIZE(i8xx_primary_formats);
16690         }
16691
16692         if (INTEL_GEN(dev_priv) >= 4)
16693                 plane_funcs = &i965_plane_funcs;
16694         else
16695                 plane_funcs = &i8xx_plane_funcs;
16696
16697         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16698                 plane->min_cdclk = vlv_plane_min_cdclk;
16699         else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
16700                 plane->min_cdclk = hsw_plane_min_cdclk;
16701         else if (IS_IVYBRIDGE(dev_priv))
16702                 plane->min_cdclk = ivb_plane_min_cdclk;
16703         else
16704                 plane->min_cdclk = i9xx_plane_min_cdclk;
16705
16706         plane->max_stride = i9xx_plane_max_stride;
16707         plane->update_plane = i9xx_update_plane;
16708         plane->disable_plane = i9xx_disable_plane;
16709         plane->get_hw_state = i9xx_plane_get_hw_state;
16710         plane->check_plane = i9xx_plane_check;
16711
16712         if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16713                 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16714                                                0, plane_funcs,
16715                                                formats, num_formats,
16716                                                i9xx_format_modifiers,
16717                                                DRM_PLANE_TYPE_PRIMARY,
16718                                                "primary %c", pipe_name(pipe));
16719         else
16720                 ret = drm_universal_plane_init(&dev_priv->drm, &plane->base,
16721                                                0, plane_funcs,
16722                                                formats, num_formats,
16723                                                i9xx_format_modifiers,
16724                                                DRM_PLANE_TYPE_PRIMARY,
16725                                                "plane %c",
16726                                                plane_name(plane->i9xx_plane));
16727         if (ret)
16728                 goto fail;
16729
16730         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
16731                 supported_rotations =
16732                         DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
16733                         DRM_MODE_REFLECT_X;
16734         } else if (INTEL_GEN(dev_priv) >= 4) {
16735                 supported_rotations =
16736                         DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
16737         } else {
16738                 supported_rotations = DRM_MODE_ROTATE_0;
16739         }
16740
16741         if (INTEL_GEN(dev_priv) >= 4)
16742                 drm_plane_create_rotation_property(&plane->base,
16743                                                    DRM_MODE_ROTATE_0,
16744                                                    supported_rotations);
16745
16746         zpos = 0;
16747         drm_plane_create_zpos_immutable_property(&plane->base, zpos);
16748
16749         drm_plane_helper_add(&plane->base, &intel_plane_helper_funcs);
16750
16751         return plane;
16752
16753 fail:
16754         intel_plane_free(plane);
16755
16756         return ERR_PTR(ret);
16757 }
16758
16759 static struct intel_plane *
16760 intel_cursor_plane_create(struct drm_i915_private *dev_priv,
16761                           enum pipe pipe)
16762 {
16763         struct intel_plane *cursor;
16764         int ret, zpos;
16765
16766         cursor = intel_plane_alloc();
16767         if (IS_ERR(cursor))
16768                 return cursor;
16769
16770         cursor->pipe = pipe;
16771         cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
16772         cursor->id = PLANE_CURSOR;
16773         cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
16774
16775         if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
16776                 cursor->max_stride = i845_cursor_max_stride;
16777                 cursor->update_plane = i845_update_cursor;
16778                 cursor->disable_plane = i845_disable_cursor;
16779                 cursor->get_hw_state = i845_cursor_get_hw_state;
16780                 cursor->check_plane = i845_check_cursor;
16781         } else {
16782                 cursor->max_stride = i9xx_cursor_max_stride;
16783                 cursor->update_plane = i9xx_update_cursor;
16784                 cursor->disable_plane = i9xx_disable_cursor;
16785                 cursor->get_hw_state = i9xx_cursor_get_hw_state;
16786                 cursor->check_plane = i9xx_check_cursor;
16787         }
16788
16789         cursor->cursor.base = ~0;
16790         cursor->cursor.cntl = ~0;
16791
16792         if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
16793                 cursor->cursor.size = ~0;
16794
16795         ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
16796                                        0, &intel_cursor_plane_funcs,
16797                                        intel_cursor_formats,
16798                                        ARRAY_SIZE(intel_cursor_formats),
16799                                        cursor_format_modifiers,
16800                                        DRM_PLANE_TYPE_CURSOR,
16801                                        "cursor %c", pipe_name(pipe));
16802         if (ret)
16803                 goto fail;
16804
16805         if (INTEL_GEN(dev_priv) >= 4)
16806                 drm_plane_create_rotation_property(&cursor->base,
16807                                                    DRM_MODE_ROTATE_0,
16808                                                    DRM_MODE_ROTATE_0 |
16809                                                    DRM_MODE_ROTATE_180);
16810
16811         zpos = RUNTIME_INFO(dev_priv)->num_sprites[pipe] + 1;
16812         drm_plane_create_zpos_immutable_property(&cursor->base, zpos);
16813
16814         if (INTEL_GEN(dev_priv) >= 12)
16815                 drm_plane_enable_fb_damage_clips(&cursor->base);
16816
16817         drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
16818
16819         return cursor;
16820
16821 fail:
16822         intel_plane_free(cursor);
16823
16824         return ERR_PTR(ret);
16825 }
16826
16827 #define INTEL_CRTC_FUNCS \
16828         .gamma_set = drm_atomic_helper_legacy_gamma_set, \
16829         .set_config = drm_atomic_helper_set_config, \
16830         .destroy = intel_crtc_destroy, \
16831         .page_flip = drm_atomic_helper_page_flip, \
16832         .atomic_duplicate_state = intel_crtc_duplicate_state, \
16833         .atomic_destroy_state = intel_crtc_destroy_state, \
16834         .set_crc_source = intel_crtc_set_crc_source, \
16835         .verify_crc_source = intel_crtc_verify_crc_source, \
16836         .get_crc_sources = intel_crtc_get_crc_sources
16837
16838 static const struct drm_crtc_funcs bdw_crtc_funcs = {
16839         INTEL_CRTC_FUNCS,
16840
16841         .get_vblank_counter = g4x_get_vblank_counter,
16842         .enable_vblank = bdw_enable_vblank,
16843         .disable_vblank = bdw_disable_vblank,
16844         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16845 };
16846
16847 static const struct drm_crtc_funcs ilk_crtc_funcs = {
16848         INTEL_CRTC_FUNCS,
16849
16850         .get_vblank_counter = g4x_get_vblank_counter,
16851         .enable_vblank = ilk_enable_vblank,
16852         .disable_vblank = ilk_disable_vblank,
16853         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16854 };
16855
16856 static const struct drm_crtc_funcs g4x_crtc_funcs = {
16857         INTEL_CRTC_FUNCS,
16858
16859         .get_vblank_counter = g4x_get_vblank_counter,
16860         .enable_vblank = i965_enable_vblank,
16861         .disable_vblank = i965_disable_vblank,
16862         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16863 };
16864
16865 static const struct drm_crtc_funcs i965_crtc_funcs = {
16866         INTEL_CRTC_FUNCS,
16867
16868         .get_vblank_counter = i915_get_vblank_counter,
16869         .enable_vblank = i965_enable_vblank,
16870         .disable_vblank = i965_disable_vblank,
16871         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16872 };
16873
16874 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
16875         INTEL_CRTC_FUNCS,
16876
16877         .get_vblank_counter = i915_get_vblank_counter,
16878         .enable_vblank = i915gm_enable_vblank,
16879         .disable_vblank = i915gm_disable_vblank,
16880         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16881 };
16882
16883 static const struct drm_crtc_funcs i915_crtc_funcs = {
16884         INTEL_CRTC_FUNCS,
16885
16886         .get_vblank_counter = i915_get_vblank_counter,
16887         .enable_vblank = i8xx_enable_vblank,
16888         .disable_vblank = i8xx_disable_vblank,
16889         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16890 };
16891
16892 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
16893         INTEL_CRTC_FUNCS,
16894
16895         /* no hw vblank counter */
16896         .enable_vblank = i8xx_enable_vblank,
16897         .disable_vblank = i8xx_disable_vblank,
16898         .get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
16899 };
16900
16901 static struct intel_crtc *intel_crtc_alloc(void)
16902 {
16903         struct intel_crtc_state *crtc_state;
16904         struct intel_crtc *crtc;
16905
16906         crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
16907         if (!crtc)
16908                 return ERR_PTR(-ENOMEM);
16909
16910         crtc_state = intel_crtc_state_alloc(crtc);
16911         if (!crtc_state) {
16912                 kfree(crtc);
16913                 return ERR_PTR(-ENOMEM);
16914         }
16915
16916         crtc->base.state = &crtc_state->uapi;
16917         crtc->config = crtc_state;
16918
16919         return crtc;
16920 }
16921
16922 static void intel_crtc_free(struct intel_crtc *crtc)
16923 {
16924         intel_crtc_destroy_state(&crtc->base, crtc->base.state);
16925         kfree(crtc);
16926 }
16927
16928 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
16929 {
16930         struct intel_plane *plane;
16931
16932         for_each_intel_plane(&dev_priv->drm, plane) {
16933                 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
16934                                                                   plane->pipe);
16935
16936                 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
16937         }
16938 }
16939
16940 static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
16941 {
16942         struct intel_plane *primary, *cursor;
16943         const struct drm_crtc_funcs *funcs;
16944         struct intel_crtc *crtc;
16945         int sprite, ret;
16946
16947         crtc = intel_crtc_alloc();
16948         if (IS_ERR(crtc))
16949                 return PTR_ERR(crtc);
16950
16951         crtc->pipe = pipe;
16952         crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
16953
16954         primary = intel_primary_plane_create(dev_priv, pipe);
16955         if (IS_ERR(primary)) {
16956                 ret = PTR_ERR(primary);
16957                 goto fail;
16958         }
16959         crtc->plane_ids_mask |= BIT(primary->id);
16960
16961         for_each_sprite(dev_priv, pipe, sprite) {
16962                 struct intel_plane *plane;
16963
16964                 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
16965                 if (IS_ERR(plane)) {
16966                         ret = PTR_ERR(plane);
16967                         goto fail;
16968                 }
16969                 crtc->plane_ids_mask |= BIT(plane->id);
16970         }
16971
16972         cursor = intel_cursor_plane_create(dev_priv, pipe);
16973         if (IS_ERR(cursor)) {
16974                 ret = PTR_ERR(cursor);
16975                 goto fail;
16976         }
16977         crtc->plane_ids_mask |= BIT(cursor->id);
16978
16979         if (HAS_GMCH(dev_priv)) {
16980                 if (IS_CHERRYVIEW(dev_priv) ||
16981                     IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
16982                         funcs = &g4x_crtc_funcs;
16983                 else if (IS_GEN(dev_priv, 4))
16984                         funcs = &i965_crtc_funcs;
16985                 else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
16986                         funcs = &i915gm_crtc_funcs;
16987                 else if (IS_GEN(dev_priv, 3))
16988                         funcs = &i915_crtc_funcs;
16989                 else
16990                         funcs = &i8xx_crtc_funcs;
16991         } else {
16992                 if (INTEL_GEN(dev_priv) >= 8)
16993                         funcs = &bdw_crtc_funcs;
16994                 else
16995                         funcs = &ilk_crtc_funcs;
16996         }
16997
16998         ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
16999                                         &primary->base, &cursor->base,
17000                                         funcs, "pipe %c", pipe_name(pipe));
17001         if (ret)
17002                 goto fail;
17003
17004         BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
17005                dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
17006         dev_priv->pipe_to_crtc_mapping[pipe] = crtc;
17007
17008         if (INTEL_GEN(dev_priv) < 9) {
17009                 enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
17010
17011                 BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
17012                        dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
17013                 dev_priv->plane_to_crtc_mapping[i9xx_plane] = crtc;
17014         }
17015
17016         if (INTEL_GEN(dev_priv) >= 10)
17017                 drm_crtc_create_scaling_filter_property(&crtc->base,
17018                                                 BIT(DRM_SCALING_FILTER_DEFAULT) |
17019                                                 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
17020
17021         intel_color_init(crtc);
17022
17023         intel_crtc_crc_init(crtc);
17024
17025         drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
17026
17027         return 0;
17028
17029 fail:
17030         intel_crtc_free(crtc);
17031
17032         return ret;
17033 }
17034
17035 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
17036                                       struct drm_file *file)
17037 {
17038         struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
17039         struct drm_crtc *drmmode_crtc;
17040         struct intel_crtc *crtc;
17041
17042         drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
17043         if (!drmmode_crtc)
17044                 return -ENOENT;
17045
17046         crtc = to_intel_crtc(drmmode_crtc);
17047         pipe_from_crtc_id->pipe = crtc->pipe;
17048
17049         return 0;
17050 }
17051
17052 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
17053 {
17054         struct drm_device *dev = encoder->base.dev;
17055         struct intel_encoder *source_encoder;
17056         u32 possible_clones = 0;
17057
17058         for_each_intel_encoder(dev, source_encoder) {
17059                 if (encoders_cloneable(encoder, source_encoder))
17060                         possible_clones |= drm_encoder_mask(&source_encoder->base);
17061         }
17062
17063         return possible_clones;
17064 }
17065
17066 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
17067 {
17068         struct drm_device *dev = encoder->base.dev;
17069         struct intel_crtc *crtc;
17070         u32 possible_crtcs = 0;
17071
17072         for_each_intel_crtc(dev, crtc) {
17073                 if (encoder->pipe_mask & BIT(crtc->pipe))
17074                         possible_crtcs |= drm_crtc_mask(&crtc->base);
17075         }
17076
17077         return possible_crtcs;
17078 }
17079
17080 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
17081 {
17082         if (!IS_MOBILE(dev_priv))
17083                 return false;
17084
17085         if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
17086                 return false;
17087
17088         if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
17089                 return false;
17090
17091         return true;
17092 }
17093
17094 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
17095 {
17096         if (INTEL_GEN(dev_priv) >= 9)
17097                 return false;
17098
17099         if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
17100                 return false;
17101
17102         if (HAS_PCH_LPT_H(dev_priv) &&
17103             intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
17104                 return false;
17105
17106         /* DDI E can't be used if DDI A requires 4 lanes */
17107         if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
17108                 return false;
17109
17110         if (!dev_priv->vbt.int_crt_support)
17111                 return false;
17112
17113         return true;
17114 }
17115
17116 void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
17117 {
17118         int pps_num;
17119         int pps_idx;
17120
17121         if (HAS_DDI(dev_priv))
17122                 return;
17123         /*
17124          * This w/a is needed at least on CPT/PPT, but to be sure apply it
17125          * everywhere where registers can be write protected.
17126          */
17127         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17128                 pps_num = 2;
17129         else
17130                 pps_num = 1;
17131
17132         for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
17133                 u32 val = intel_de_read(dev_priv, PP_CONTROL(pps_idx));
17134
17135                 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
17136                 intel_de_write(dev_priv, PP_CONTROL(pps_idx), val);
17137         }
17138 }
17139
17140 static void intel_pps_init(struct drm_i915_private *dev_priv)
17141 {
17142         if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
17143                 dev_priv->pps_mmio_base = PCH_PPS_BASE;
17144         else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
17145                 dev_priv->pps_mmio_base = VLV_PPS_BASE;
17146         else
17147                 dev_priv->pps_mmio_base = PPS_BASE;
17148
17149         intel_pps_unlock_regs_wa(dev_priv);
17150 }
17151
17152 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
17153 {
17154         struct intel_encoder *encoder;
17155         bool dpd_is_edp = false;
17156
17157         intel_pps_init(dev_priv);
17158
17159         if (!HAS_DISPLAY(dev_priv))
17160                 return;
17161
17162         if (IS_ROCKETLAKE(dev_priv)) {
17163                 intel_ddi_init(dev_priv, PORT_A);
17164                 intel_ddi_init(dev_priv, PORT_B);
17165                 intel_ddi_init(dev_priv, PORT_TC1);
17166                 intel_ddi_init(dev_priv, PORT_TC2);
17167         } else if (INTEL_GEN(dev_priv) >= 12) {
17168                 intel_ddi_init(dev_priv, PORT_A);
17169                 intel_ddi_init(dev_priv, PORT_B);
17170                 intel_ddi_init(dev_priv, PORT_TC1);
17171                 intel_ddi_init(dev_priv, PORT_TC2);
17172                 intel_ddi_init(dev_priv, PORT_TC3);
17173                 intel_ddi_init(dev_priv, PORT_TC4);
17174                 intel_ddi_init(dev_priv, PORT_TC5);
17175                 intel_ddi_init(dev_priv, PORT_TC6);
17176                 icl_dsi_init(dev_priv);
17177         } else if (IS_JSL_EHL(dev_priv)) {
17178                 intel_ddi_init(dev_priv, PORT_A);
17179                 intel_ddi_init(dev_priv, PORT_B);
17180                 intel_ddi_init(dev_priv, PORT_C);
17181                 intel_ddi_init(dev_priv, PORT_D);
17182                 icl_dsi_init(dev_priv);
17183         } else if (IS_GEN(dev_priv, 11)) {
17184                 intel_ddi_init(dev_priv, PORT_A);
17185                 intel_ddi_init(dev_priv, PORT_B);
17186                 intel_ddi_init(dev_priv, PORT_C);
17187                 intel_ddi_init(dev_priv, PORT_D);
17188                 intel_ddi_init(dev_priv, PORT_E);
17189                 /*
17190                  * On some ICL SKUs port F is not present. No strap bits for
17191                  * this, so rely on VBT.
17192                  * Work around broken VBTs on SKUs known to have no port F.
17193                  */
17194                 if (IS_ICL_WITH_PORT_F(dev_priv) &&
17195                     intel_bios_is_port_present(dev_priv, PORT_F))
17196                         intel_ddi_init(dev_priv, PORT_F);
17197
17198                 icl_dsi_init(dev_priv);
17199         } else if (IS_GEN9_LP(dev_priv)) {
17200                 /*
17201                  * FIXME: Broxton doesn't support port detection via the
17202                  * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
17203                  * detect the ports.
17204                  */
17205                 intel_ddi_init(dev_priv, PORT_A);
17206                 intel_ddi_init(dev_priv, PORT_B);
17207                 intel_ddi_init(dev_priv, PORT_C);
17208
17209                 vlv_dsi_init(dev_priv);
17210         } else if (HAS_DDI(dev_priv)) {
17211                 int found;
17212
17213                 if (intel_ddi_crt_present(dev_priv))
17214                         intel_crt_init(dev_priv);
17215
17216                 /*
17217                  * Haswell uses DDI functions to detect digital outputs.
17218                  * On SKL pre-D0 the strap isn't connected, so we assume
17219                  * it's there.
17220                  */
17221                 found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
17222                 /* WaIgnoreDDIAStrap: skl */
17223                 if (found || IS_GEN9_BC(dev_priv))
17224                         intel_ddi_init(dev_priv, PORT_A);
17225
17226                 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
17227                  * register */
17228                 found = intel_de_read(dev_priv, SFUSE_STRAP);
17229
17230                 if (found & SFUSE_STRAP_DDIB_DETECTED)
17231                         intel_ddi_init(dev_priv, PORT_B);
17232                 if (found & SFUSE_STRAP_DDIC_DETECTED)
17233                         intel_ddi_init(dev_priv, PORT_C);
17234                 if (found & SFUSE_STRAP_DDID_DETECTED)
17235                         intel_ddi_init(dev_priv, PORT_D);
17236                 if (found & SFUSE_STRAP_DDIF_DETECTED)
17237                         intel_ddi_init(dev_priv, PORT_F);
17238                 /*
17239                  * On SKL we don't have a way to detect DDI-E so we rely on VBT.
17240                  */
17241                 if (IS_GEN9_BC(dev_priv) &&
17242                     intel_bios_is_port_present(dev_priv, PORT_E))
17243                         intel_ddi_init(dev_priv, PORT_E);
17244
17245         } else if (HAS_PCH_SPLIT(dev_priv)) {
17246                 int found;
17247
17248                 /*
17249                  * intel_edp_init_connector() depends on this completing first,
17250                  * to prevent the registration of both eDP and LVDS and the
17251                  * incorrect sharing of the PPS.
17252                  */
17253                 intel_lvds_init(dev_priv);
17254                 intel_crt_init(dev_priv);
17255
17256                 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
17257
17258                 if (ilk_has_edp_a(dev_priv))
17259                         intel_dp_init(dev_priv, DP_A, PORT_A);
17260
17261                 if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
17262                         /* PCH SDVOB multiplex with HDMIB */
17263                         found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
17264                         if (!found)
17265                                 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
17266                         if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
17267                                 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
17268                 }
17269
17270                 if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
17271                         intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
17272
17273                 if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
17274                         intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
17275
17276                 if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
17277                         intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
17278
17279                 if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
17280                         intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
17281         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
17282                 bool has_edp, has_port;
17283
17284                 if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
17285                         intel_crt_init(dev_priv);
17286
17287                 /*
17288                  * The DP_DETECTED bit is the latched state of the DDC
17289                  * SDA pin at boot. However since eDP doesn't require DDC
17290                  * (no way to plug in a DP->HDMI dongle) the DDC pins for
17291                  * eDP ports may have been muxed to an alternate function.
17292                  * Thus we can't rely on the DP_DETECTED bit alone to detect
17293                  * eDP ports. Consult the VBT as well as DP_DETECTED to
17294                  * detect eDP ports.
17295                  *
17296                  * Sadly the straps seem to be missing sometimes even for HDMI
17297                  * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
17298                  * and VBT for the presence of the port. Additionally we can't
17299                  * trust the port type the VBT declares as we've seen at least
17300                  * HDMI ports that the VBT claim are DP or eDP.
17301                  */
17302                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
17303                 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
17304                 if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
17305                         has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
17306                 if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
17307                         intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
17308
17309                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
17310                 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
17311                 if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
17312                         has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
17313                 if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
17314                         intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
17315
17316                 if (IS_CHERRYVIEW(dev_priv)) {
17317                         /*
17318                          * eDP not supported on port D,
17319                          * so no need to worry about it
17320                          */
17321                         has_port = intel_bios_is_port_present(dev_priv, PORT_D);
17322                         if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
17323                                 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
17324                         if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
17325                                 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
17326                 }
17327
17328                 vlv_dsi_init(dev_priv);
17329         } else if (IS_PINEVIEW(dev_priv)) {
17330                 intel_lvds_init(dev_priv);
17331                 intel_crt_init(dev_priv);
17332         } else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
17333                 bool found = false;
17334
17335                 if (IS_MOBILE(dev_priv))
17336                         intel_lvds_init(dev_priv);
17337
17338                 intel_crt_init(dev_priv);
17339
17340                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17341                         drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
17342                         found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
17343                         if (!found && IS_G4X(dev_priv)) {
17344                                 drm_dbg_kms(&dev_priv->drm,
17345                                             "probing HDMI on SDVOB\n");
17346                                 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
17347                         }
17348
17349                         if (!found && IS_G4X(dev_priv))
17350                                 intel_dp_init(dev_priv, DP_B, PORT_B);
17351                 }
17352
17353                 /* Before G4X SDVOC doesn't have its own detect register */
17354
17355                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
17356                         drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
17357                         found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
17358                 }
17359
17360                 if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
17361
17362                         if (IS_G4X(dev_priv)) {
17363                                 drm_dbg_kms(&dev_priv->drm,
17364                                             "probing HDMI on SDVOC\n");
17365                                 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
17366                         }
17367                         if (IS_G4X(dev_priv))
17368                                 intel_dp_init(dev_priv, DP_C, PORT_C);
17369                 }
17370
17371                 if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
17372                         intel_dp_init(dev_priv, DP_D, PORT_D);
17373
17374                 if (SUPPORTS_TV(dev_priv))
17375                         intel_tv_init(dev_priv);
17376         } else if (IS_GEN(dev_priv, 2)) {
17377                 if (IS_I85X(dev_priv))
17378                         intel_lvds_init(dev_priv);
17379
17380                 intel_crt_init(dev_priv);
17381                 intel_dvo_init(dev_priv);
17382         }
17383
17384         intel_psr_init(dev_priv);
17385
17386         for_each_intel_encoder(&dev_priv->drm, encoder) {
17387                 encoder->base.possible_crtcs =
17388                         intel_encoder_possible_crtcs(encoder);
17389                 encoder->base.possible_clones =
17390                         intel_encoder_possible_clones(encoder);
17391         }
17392
17393         intel_init_pch_refclk(dev_priv);
17394
17395         drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
17396 }
17397
17398 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
17399 {
17400         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
17401
17402         drm_framebuffer_cleanup(fb);
17403         intel_frontbuffer_put(intel_fb->frontbuffer);
17404
17405         kfree(intel_fb);
17406 }
17407
17408 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
17409                                                 struct drm_file *file,
17410                                                 unsigned int *handle)
17411 {
17412         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17413         struct drm_i915_private *i915 = to_i915(obj->base.dev);
17414
17415         if (obj->userptr.mm) {
17416                 drm_dbg(&i915->drm,
17417                         "attempting to use a userptr for a framebuffer, denied\n");
17418                 return -EINVAL;
17419         }
17420
17421         return drm_gem_handle_create(file, &obj->base, handle);
17422 }
17423
17424 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
17425                                         struct drm_file *file,
17426                                         unsigned flags, unsigned color,
17427                                         struct drm_clip_rect *clips,
17428                                         unsigned num_clips)
17429 {
17430         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
17431
17432         i915_gem_object_flush_if_display(obj);
17433         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
17434
17435         return 0;
17436 }
17437
17438 static const struct drm_framebuffer_funcs intel_fb_funcs = {
17439         .destroy = intel_user_framebuffer_destroy,
17440         .create_handle = intel_user_framebuffer_create_handle,
17441         .dirty = intel_user_framebuffer_dirty,
17442 };
17443
17444 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
17445                                   struct drm_i915_gem_object *obj,
17446                                   struct drm_mode_fb_cmd2 *mode_cmd)
17447 {
17448         struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
17449         struct drm_framebuffer *fb = &intel_fb->base;
17450         u32 max_stride;
17451         unsigned int tiling, stride;
17452         int ret = -EINVAL;
17453         int i;
17454
17455         intel_fb->frontbuffer = intel_frontbuffer_get(obj);
17456         if (!intel_fb->frontbuffer)
17457                 return -ENOMEM;
17458
17459         i915_gem_object_lock(obj, NULL);
17460         tiling = i915_gem_object_get_tiling(obj);
17461         stride = i915_gem_object_get_stride(obj);
17462         i915_gem_object_unlock(obj);
17463
17464         if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
17465                 /*
17466                  * If there's a fence, enforce that
17467                  * the fb modifier and tiling mode match.
17468                  */
17469                 if (tiling != I915_TILING_NONE &&
17470                     tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17471                         drm_dbg_kms(&dev_priv->drm,
17472                                     "tiling_mode doesn't match fb modifier\n");
17473                         goto err;
17474                 }
17475         } else {
17476                 if (tiling == I915_TILING_X) {
17477                         mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
17478                 } else if (tiling == I915_TILING_Y) {
17479                         drm_dbg_kms(&dev_priv->drm,
17480                                     "No Y tiling for legacy addfb\n");
17481                         goto err;
17482                 }
17483         }
17484
17485         if (!drm_any_plane_has_format(&dev_priv->drm,
17486                                       mode_cmd->pixel_format,
17487                                       mode_cmd->modifier[0])) {
17488                 struct drm_format_name_buf format_name;
17489
17490                 drm_dbg_kms(&dev_priv->drm,
17491                             "unsupported pixel format %s / modifier 0x%llx\n",
17492                             drm_get_format_name(mode_cmd->pixel_format,
17493                                                 &format_name),
17494                             mode_cmd->modifier[0]);
17495                 goto err;
17496         }
17497
17498         /*
17499          * gen2/3 display engine uses the fence if present,
17500          * so the tiling mode must match the fb modifier exactly.
17501          */
17502         if (INTEL_GEN(dev_priv) < 4 &&
17503             tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
17504                 drm_dbg_kms(&dev_priv->drm,
17505                             "tiling_mode must match fb modifier exactly on gen2/3\n");
17506                 goto err;
17507         }
17508
17509         max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
17510                                          mode_cmd->modifier[0]);
17511         if (mode_cmd->pitches[0] > max_stride) {
17512                 drm_dbg_kms(&dev_priv->drm,
17513                             "%s pitch (%u) must be at most %d\n",
17514                             mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
17515                             "tiled" : "linear",
17516                             mode_cmd->pitches[0], max_stride);
17517                 goto err;
17518         }
17519
17520         /*
17521          * If there's a fence, enforce that
17522          * the fb pitch and fence stride match.
17523          */
17524         if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
17525                 drm_dbg_kms(&dev_priv->drm,
17526                             "pitch (%d) must match tiling stride (%d)\n",
17527                             mode_cmd->pitches[0], stride);
17528                 goto err;
17529         }
17530
17531         /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
17532         if (mode_cmd->offsets[0] != 0) {
17533                 drm_dbg_kms(&dev_priv->drm,
17534                             "plane 0 offset (0x%08x) must be 0\n",
17535                             mode_cmd->offsets[0]);
17536                 goto err;
17537         }
17538
17539         drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
17540
17541         for (i = 0; i < fb->format->num_planes; i++) {
17542                 u32 stride_alignment;
17543
17544                 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
17545                         drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
17546                                     i);
17547                         goto err;
17548                 }
17549
17550                 stride_alignment = intel_fb_stride_alignment(fb, i);
17551                 if (fb->pitches[i] & (stride_alignment - 1)) {
17552                         drm_dbg_kms(&dev_priv->drm,
17553                                     "plane %d pitch (%d) must be at least %u byte aligned\n",
17554                                     i, fb->pitches[i], stride_alignment);
17555                         goto err;
17556                 }
17557
17558                 if (is_gen12_ccs_plane(fb, i)) {
17559                         int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
17560
17561                         if (fb->pitches[i] != ccs_aux_stride) {
17562                                 drm_dbg_kms(&dev_priv->drm,
17563                                             "ccs aux plane %d pitch (%d) must be %d\n",
17564                                             i,
17565                                             fb->pitches[i], ccs_aux_stride);
17566                                 goto err;
17567                         }
17568                 }
17569
17570                 fb->obj[i] = &obj->base;
17571         }
17572
17573         ret = intel_fill_fb_info(dev_priv, fb);
17574         if (ret)
17575                 goto err;
17576
17577         ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
17578         if (ret) {
17579                 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
17580                 goto err;
17581         }
17582
17583         return 0;
17584
17585 err:
17586         intel_frontbuffer_put(intel_fb->frontbuffer);
17587         return ret;
17588 }
17589
17590 static struct drm_framebuffer *
17591 intel_user_framebuffer_create(struct drm_device *dev,
17592                               struct drm_file *filp,
17593                               const struct drm_mode_fb_cmd2 *user_mode_cmd)
17594 {
17595         struct drm_framebuffer *fb;
17596         struct drm_i915_gem_object *obj;
17597         struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
17598
17599         obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
17600         if (!obj)
17601                 return ERR_PTR(-ENOENT);
17602
17603         fb = intel_framebuffer_create(obj, &mode_cmd);
17604         i915_gem_object_put(obj);
17605
17606         return fb;
17607 }
17608
17609 static enum drm_mode_status
17610 intel_mode_valid(struct drm_device *dev,
17611                  const struct drm_display_mode *mode)
17612 {
17613         struct drm_i915_private *dev_priv = to_i915(dev);
17614         int hdisplay_max, htotal_max;
17615         int vdisplay_max, vtotal_max;
17616
17617         /*
17618          * Can't reject DBLSCAN here because Xorg ddxen can add piles
17619          * of DBLSCAN modes to the output's mode list when they detect
17620          * the scaling mode property on the connector. And they don't
17621          * ask the kernel to validate those modes in any way until
17622          * modeset time at which point the client gets a protocol error.
17623          * So in order to not upset those clients we silently ignore the
17624          * DBLSCAN flag on such connectors. For other connectors we will
17625          * reject modes with the DBLSCAN flag in encoder->compute_config().
17626          * And we always reject DBLSCAN modes in connector->mode_valid()
17627          * as we never want such modes on the connector's mode list.
17628          */
17629
17630         if (mode->vscan > 1)
17631                 return MODE_NO_VSCAN;
17632
17633         if (mode->flags & DRM_MODE_FLAG_HSKEW)
17634                 return MODE_H_ILLEGAL;
17635
17636         if (mode->flags & (DRM_MODE_FLAG_CSYNC |
17637                            DRM_MODE_FLAG_NCSYNC |
17638                            DRM_MODE_FLAG_PCSYNC))
17639                 return MODE_HSYNC;
17640
17641         if (mode->flags & (DRM_MODE_FLAG_BCAST |
17642                            DRM_MODE_FLAG_PIXMUX |
17643                            DRM_MODE_FLAG_CLKDIV2))
17644                 return MODE_BAD;
17645
17646         /* Transcoder timing limits */
17647         if (INTEL_GEN(dev_priv) >= 11) {
17648                 hdisplay_max = 16384;
17649                 vdisplay_max = 8192;
17650                 htotal_max = 16384;
17651                 vtotal_max = 8192;
17652         } else if (INTEL_GEN(dev_priv) >= 9 ||
17653                    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
17654                 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
17655                 vdisplay_max = 4096;
17656                 htotal_max = 8192;
17657                 vtotal_max = 8192;
17658         } else if (INTEL_GEN(dev_priv) >= 3) {
17659                 hdisplay_max = 4096;
17660                 vdisplay_max = 4096;
17661                 htotal_max = 8192;
17662                 vtotal_max = 8192;
17663         } else {
17664                 hdisplay_max = 2048;
17665                 vdisplay_max = 2048;
17666                 htotal_max = 4096;
17667                 vtotal_max = 4096;
17668         }
17669
17670         if (mode->hdisplay > hdisplay_max ||
17671             mode->hsync_start > htotal_max ||
17672             mode->hsync_end > htotal_max ||
17673             mode->htotal > htotal_max)
17674                 return MODE_H_ILLEGAL;
17675
17676         if (mode->vdisplay > vdisplay_max ||
17677             mode->vsync_start > vtotal_max ||
17678             mode->vsync_end > vtotal_max ||
17679             mode->vtotal > vtotal_max)
17680                 return MODE_V_ILLEGAL;
17681
17682         if (INTEL_GEN(dev_priv) >= 5) {
17683                 if (mode->hdisplay < 64 ||
17684                     mode->htotal - mode->hdisplay < 32)
17685                         return MODE_H_ILLEGAL;
17686
17687                 if (mode->vtotal - mode->vdisplay < 5)
17688                         return MODE_V_ILLEGAL;
17689         } else {
17690                 if (mode->htotal - mode->hdisplay < 32)
17691                         return MODE_H_ILLEGAL;
17692
17693                 if (mode->vtotal - mode->vdisplay < 3)
17694                         return MODE_V_ILLEGAL;
17695         }
17696
17697         return MODE_OK;
17698 }
17699
17700 enum drm_mode_status
17701 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
17702                                 const struct drm_display_mode *mode)
17703 {
17704         int plane_width_max, plane_height_max;
17705
17706         /*
17707          * intel_mode_valid() should be
17708          * sufficient on older platforms.
17709          */
17710         if (INTEL_GEN(dev_priv) < 9)
17711                 return MODE_OK;
17712
17713         /*
17714          * Most people will probably want a fullscreen
17715          * plane so let's not advertize modes that are
17716          * too big for that.
17717          */
17718         if (INTEL_GEN(dev_priv) >= 11) {
17719                 plane_width_max = 5120;
17720                 plane_height_max = 4320;
17721         } else {
17722                 plane_width_max = 5120;
17723                 plane_height_max = 4096;
17724         }
17725
17726         if (mode->hdisplay > plane_width_max)
17727                 return MODE_H_ILLEGAL;
17728
17729         if (mode->vdisplay > plane_height_max)
17730                 return MODE_V_ILLEGAL;
17731
17732         return MODE_OK;
17733 }
17734
17735 static const struct drm_mode_config_funcs intel_mode_funcs = {
17736         .fb_create = intel_user_framebuffer_create,
17737         .get_format_info = intel_get_format_info,
17738         .output_poll_changed = intel_fbdev_output_poll_changed,
17739         .mode_valid = intel_mode_valid,
17740         .atomic_check = intel_atomic_check,
17741         .atomic_commit = intel_atomic_commit,
17742         .atomic_state_alloc = intel_atomic_state_alloc,
17743         .atomic_state_clear = intel_atomic_state_clear,
17744         .atomic_state_free = intel_atomic_state_free,
17745 };
17746
17747 /**
17748  * intel_init_display_hooks - initialize the display modesetting hooks
17749  * @dev_priv: device private
17750  */
17751 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
17752 {
17753         intel_init_cdclk_hooks(dev_priv);
17754
17755         if (INTEL_GEN(dev_priv) >= 9) {
17756                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17757                 dev_priv->display.get_initial_plane_config =
17758                         skl_get_initial_plane_config;
17759                 dev_priv->display.crtc_compute_clock = hsw_crtc_compute_clock;
17760                 dev_priv->display.crtc_enable = hsw_crtc_enable;
17761                 dev_priv->display.crtc_disable = hsw_crtc_disable;
17762         } else if (HAS_DDI(dev_priv)) {
17763                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
17764                 dev_priv->display.get_initial_plane_config =
17765                         i9xx_get_initial_plane_config;
17766                 dev_priv->display.crtc_compute_clock =
17767                         hsw_crtc_compute_clock;
17768                 dev_priv->display.crtc_enable = hsw_crtc_enable;
17769                 dev_priv->display.crtc_disable = hsw_crtc_disable;
17770         } else if (HAS_PCH_SPLIT(dev_priv)) {
17771                 dev_priv->display.get_pipe_config = ilk_get_pipe_config;
17772                 dev_priv->display.get_initial_plane_config =
17773                         i9xx_get_initial_plane_config;
17774                 dev_priv->display.crtc_compute_clock =
17775                         ilk_crtc_compute_clock;
17776                 dev_priv->display.crtc_enable = ilk_crtc_enable;
17777                 dev_priv->display.crtc_disable = ilk_crtc_disable;
17778         } else if (IS_CHERRYVIEW(dev_priv)) {
17779                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17780                 dev_priv->display.get_initial_plane_config =
17781                         i9xx_get_initial_plane_config;
17782                 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
17783                 dev_priv->display.crtc_enable = valleyview_crtc_enable;
17784                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17785         } else if (IS_VALLEYVIEW(dev_priv)) {
17786                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17787                 dev_priv->display.get_initial_plane_config =
17788                         i9xx_get_initial_plane_config;
17789                 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
17790                 dev_priv->display.crtc_enable = valleyview_crtc_enable;
17791                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17792         } else if (IS_G4X(dev_priv)) {
17793                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17794                 dev_priv->display.get_initial_plane_config =
17795                         i9xx_get_initial_plane_config;
17796                 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
17797                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
17798                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17799         } else if (IS_PINEVIEW(dev_priv)) {
17800                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17801                 dev_priv->display.get_initial_plane_config =
17802                         i9xx_get_initial_plane_config;
17803                 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
17804                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
17805                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17806         } else if (!IS_GEN(dev_priv, 2)) {
17807                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17808                 dev_priv->display.get_initial_plane_config =
17809                         i9xx_get_initial_plane_config;
17810                 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
17811                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
17812                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17813         } else {
17814                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
17815                 dev_priv->display.get_initial_plane_config =
17816                         i9xx_get_initial_plane_config;
17817                 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
17818                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
17819                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
17820         }
17821
17822         if (IS_GEN(dev_priv, 5)) {
17823                 dev_priv->display.fdi_link_train = ilk_fdi_link_train;
17824         } else if (IS_GEN(dev_priv, 6)) {
17825                 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
17826         } else if (IS_IVYBRIDGE(dev_priv)) {
17827                 /* FIXME: detect B0+ stepping and use auto training */
17828                 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
17829         }
17830
17831         if (INTEL_GEN(dev_priv) >= 9)
17832                 dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
17833         else
17834                 dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
17835
17836 }
17837
17838 void intel_modeset_init_hw(struct drm_i915_private *i915)
17839 {
17840         struct intel_cdclk_state *cdclk_state =
17841                 to_intel_cdclk_state(i915->cdclk.obj.state);
17842         struct intel_dbuf_state *dbuf_state =
17843                 to_intel_dbuf_state(i915->dbuf.obj.state);
17844
17845         intel_update_cdclk(i915);
17846         intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
17847         cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
17848
17849         dbuf_state->enabled_slices = i915->dbuf.enabled_slices;
17850 }
17851
17852 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
17853 {
17854         struct drm_plane *plane;
17855         struct intel_crtc *crtc;
17856
17857         for_each_intel_crtc(state->dev, crtc) {
17858                 struct intel_crtc_state *crtc_state;
17859
17860                 crtc_state = intel_atomic_get_crtc_state(state, crtc);
17861                 if (IS_ERR(crtc_state))
17862                         return PTR_ERR(crtc_state);
17863
17864                 if (crtc_state->hw.active) {
17865                         /*
17866                          * Preserve the inherited flag to avoid
17867                          * taking the full modeset path.
17868                          */
17869                         crtc_state->inherited = true;
17870                 }
17871         }
17872
17873         drm_for_each_plane(plane, state->dev) {
17874                 struct drm_plane_state *plane_state;
17875
17876                 plane_state = drm_atomic_get_plane_state(state, plane);
17877                 if (IS_ERR(plane_state))
17878                         return PTR_ERR(plane_state);
17879         }
17880
17881         return 0;
17882 }
17883
17884 /*
17885  * Calculate what we think the watermarks should be for the state we've read
17886  * out of the hardware and then immediately program those watermarks so that
17887  * we ensure the hardware settings match our internal state.
17888  *
17889  * We can calculate what we think WM's should be by creating a duplicate of the
17890  * current state (which was constructed during hardware readout) and running it
17891  * through the atomic check code to calculate new watermark values in the
17892  * state object.
17893  */
17894 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
17895 {
17896         struct drm_atomic_state *state;
17897         struct intel_atomic_state *intel_state;
17898         struct intel_crtc *crtc;
17899         struct intel_crtc_state *crtc_state;
17900         struct drm_modeset_acquire_ctx ctx;
17901         int ret;
17902         int i;
17903
17904         /* Only supported on platforms that use atomic watermark design */
17905         if (!dev_priv->display.optimize_watermarks)
17906                 return;
17907
17908         state = drm_atomic_state_alloc(&dev_priv->drm);
17909         if (drm_WARN_ON(&dev_priv->drm, !state))
17910                 return;
17911
17912         intel_state = to_intel_atomic_state(state);
17913
17914         drm_modeset_acquire_init(&ctx, 0);
17915
17916 retry:
17917         state->acquire_ctx = &ctx;
17918
17919         /*
17920          * Hardware readout is the only time we don't want to calculate
17921          * intermediate watermarks (since we don't trust the current
17922          * watermarks).
17923          */
17924         if (!HAS_GMCH(dev_priv))
17925                 intel_state->skip_intermediate_wm = true;
17926
17927         ret = sanitize_watermarks_add_affected(state);
17928         if (ret)
17929                 goto fail;
17930
17931         ret = intel_atomic_check(&dev_priv->drm, state);
17932         if (ret)
17933                 goto fail;
17934
17935         /* Write calculated watermark values back */
17936         for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
17937                 crtc_state->wm.need_postvbl_update = true;
17938                 dev_priv->display.optimize_watermarks(intel_state, crtc);
17939
17940                 to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
17941         }
17942
17943 fail:
17944         if (ret == -EDEADLK) {
17945                 drm_atomic_state_clear(state);
17946                 drm_modeset_backoff(&ctx);
17947                 goto retry;
17948         }
17949
17950         /*
17951          * If we fail here, it means that the hardware appears to be
17952          * programmed in a way that shouldn't be possible, given our
17953          * understanding of watermark requirements.  This might mean a
17954          * mistake in the hardware readout code or a mistake in the
17955          * watermark calculations for a given platform.  Raise a WARN
17956          * so that this is noticeable.
17957          *
17958          * If this actually happens, we'll have to just leave the
17959          * BIOS-programmed watermarks untouched and hope for the best.
17960          */
17961         drm_WARN(&dev_priv->drm, ret,
17962                  "Could not determine valid watermarks for inherited state\n");
17963
17964         drm_atomic_state_put(state);
17965
17966         drm_modeset_drop_locks(&ctx);
17967         drm_modeset_acquire_fini(&ctx);
17968 }
17969
17970 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
17971 {
17972         if (IS_GEN(dev_priv, 5)) {
17973                 u32 fdi_pll_clk =
17974                         intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
17975
17976                 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
17977         } else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
17978                 dev_priv->fdi_pll_freq = 270000;
17979         } else {
17980                 return;
17981         }
17982
17983         drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
17984 }
17985
17986 static int intel_initial_commit(struct drm_device *dev)
17987 {
17988         struct drm_atomic_state *state = NULL;
17989         struct drm_modeset_acquire_ctx ctx;
17990         struct intel_crtc *crtc;
17991         int ret = 0;
17992
17993         state = drm_atomic_state_alloc(dev);
17994         if (!state)
17995                 return -ENOMEM;
17996
17997         drm_modeset_acquire_init(&ctx, 0);
17998
17999 retry:
18000         state->acquire_ctx = &ctx;
18001
18002         for_each_intel_crtc(dev, crtc) {
18003                 struct intel_crtc_state *crtc_state =
18004                         intel_atomic_get_crtc_state(state, crtc);
18005
18006                 if (IS_ERR(crtc_state)) {
18007                         ret = PTR_ERR(crtc_state);
18008                         goto out;
18009                 }
18010
18011                 if (crtc_state->hw.active) {
18012                         struct intel_encoder *encoder;
18013
18014                         /*
18015                          * We've not yet detected sink capabilities
18016                          * (audio,infoframes,etc.) and thus we don't want to
18017                          * force a full state recomputation yet. We want that to
18018                          * happen only for the first real commit from userspace.
18019                          * So preserve the inherited flag for the time being.
18020                          */
18021                         crtc_state->inherited = true;
18022
18023                         ret = drm_atomic_add_affected_planes(state, &crtc->base);
18024                         if (ret)
18025                                 goto out;
18026
18027                         /*
18028                          * FIXME hack to force a LUT update to avoid the
18029                          * plane update forcing the pipe gamma on without
18030                          * having a proper LUT loaded. Remove once we
18031                          * have readout for pipe gamma enable.
18032                          */
18033                         crtc_state->uapi.color_mgmt_changed = true;
18034
18035                         for_each_intel_encoder_mask(dev, encoder,
18036                                                     crtc_state->uapi.encoder_mask) {
18037                                 if (encoder->initial_fastset_check &&
18038                                     !encoder->initial_fastset_check(encoder, crtc_state)) {
18039                                         ret = drm_atomic_add_affected_connectors(state,
18040                                                                                  &crtc->base);
18041                                         if (ret)
18042                                                 goto out;
18043                                 }
18044                         }
18045                 }
18046         }
18047
18048         ret = drm_atomic_commit(state);
18049
18050 out:
18051         if (ret == -EDEADLK) {
18052                 drm_atomic_state_clear(state);
18053                 drm_modeset_backoff(&ctx);
18054                 goto retry;
18055         }
18056
18057         drm_atomic_state_put(state);
18058
18059         drm_modeset_drop_locks(&ctx);
18060         drm_modeset_acquire_fini(&ctx);
18061
18062         return ret;
18063 }
18064
18065 static void intel_mode_config_init(struct drm_i915_private *i915)
18066 {
18067         struct drm_mode_config *mode_config = &i915->drm.mode_config;
18068
18069         drm_mode_config_init(&i915->drm);
18070         INIT_LIST_HEAD(&i915->global_obj_list);
18071
18072         mode_config->min_width = 0;
18073         mode_config->min_height = 0;
18074
18075         mode_config->preferred_depth = 24;
18076         mode_config->prefer_shadow = 1;
18077
18078         mode_config->allow_fb_modifiers = true;
18079
18080         mode_config->funcs = &intel_mode_funcs;
18081
18082         if (INTEL_GEN(i915) >= 9)
18083                 mode_config->async_page_flip = true;
18084
18085         /*
18086          * Maximum framebuffer dimensions, chosen to match
18087          * the maximum render engine surface size on gen4+.
18088          */
18089         if (INTEL_GEN(i915) >= 7) {
18090                 mode_config->max_width = 16384;
18091                 mode_config->max_height = 16384;
18092         } else if (INTEL_GEN(i915) >= 4) {
18093                 mode_config->max_width = 8192;
18094                 mode_config->max_height = 8192;
18095         } else if (IS_GEN(i915, 3)) {
18096                 mode_config->max_width = 4096;
18097                 mode_config->max_height = 4096;
18098         } else {
18099                 mode_config->max_width = 2048;
18100                 mode_config->max_height = 2048;
18101         }
18102
18103         if (IS_I845G(i915) || IS_I865G(i915)) {
18104                 mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
18105                 mode_config->cursor_height = 1023;
18106         } else if (IS_I830(i915) || IS_I85X(i915) ||
18107                    IS_I915G(i915) || IS_I915GM(i915)) {
18108                 mode_config->cursor_width = 64;
18109                 mode_config->cursor_height = 64;
18110         } else {
18111                 mode_config->cursor_width = 256;
18112                 mode_config->cursor_height = 256;
18113         }
18114 }
18115
18116 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
18117 {
18118         intel_atomic_global_obj_cleanup(i915);
18119         drm_mode_config_cleanup(&i915->drm);
18120 }
18121
18122 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
18123 {
18124         if (plane_config->fb) {
18125                 struct drm_framebuffer *fb = &plane_config->fb->base;
18126
18127                 /* We may only have the stub and not a full framebuffer */
18128                 if (drm_framebuffer_read_refcount(fb))
18129                         drm_framebuffer_put(fb);
18130                 else
18131                         kfree(fb);
18132         }
18133
18134         if (plane_config->vma)
18135                 i915_vma_put(plane_config->vma);
18136 }
18137
18138 /* part #1: call before irq install */
18139 int intel_modeset_init_noirq(struct drm_i915_private *i915)
18140 {
18141         int ret;
18142
18143         if (i915_inject_probe_failure(i915))
18144                 return -ENODEV;
18145
18146         if (HAS_DISPLAY(i915)) {
18147                 ret = drm_vblank_init(&i915->drm,
18148                                       INTEL_NUM_PIPES(i915));
18149                 if (ret)
18150                         return ret;
18151         }
18152
18153         intel_bios_init(i915);
18154
18155         ret = intel_vga_register(i915);
18156         if (ret)
18157                 goto cleanup_bios;
18158
18159         /* FIXME: completely on the wrong abstraction layer */
18160         intel_power_domains_init_hw(i915, false);
18161
18162         intel_csr_ucode_init(i915);
18163
18164         i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
18165         i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
18166                                         WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
18167
18168         intel_mode_config_init(i915);
18169
18170         ret = intel_cdclk_init(i915);
18171         if (ret)
18172                 goto cleanup_vga_client_pw_domain_csr;
18173
18174         ret = intel_dbuf_init(i915);
18175         if (ret)
18176                 goto cleanup_vga_client_pw_domain_csr;
18177
18178         ret = intel_bw_init(i915);
18179         if (ret)
18180                 goto cleanup_vga_client_pw_domain_csr;
18181
18182         init_llist_head(&i915->atomic_helper.free_list);
18183         INIT_WORK(&i915->atomic_helper.free_work,
18184                   intel_atomic_helper_free_state_worker);
18185
18186         intel_init_quirks(i915);
18187
18188         intel_fbc_init(i915);
18189
18190         return 0;
18191
18192 cleanup_vga_client_pw_domain_csr:
18193         intel_csr_ucode_fini(i915);
18194         intel_power_domains_driver_remove(i915);
18195         intel_vga_unregister(i915);
18196 cleanup_bios:
18197         intel_bios_driver_remove(i915);
18198
18199         return ret;
18200 }
18201
18202 /* part #2: call after irq install, but before gem init */
18203 int intel_modeset_init_nogem(struct drm_i915_private *i915)
18204 {
18205         struct drm_device *dev = &i915->drm;
18206         enum pipe pipe;
18207         struct intel_crtc *crtc;
18208         int ret;
18209
18210         intel_init_pm(i915);
18211
18212         intel_panel_sanitize_ssc(i915);
18213
18214         intel_gmbus_setup(i915);
18215
18216         drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
18217                     INTEL_NUM_PIPES(i915),
18218                     INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
18219
18220         if (HAS_DISPLAY(i915)) {
18221                 for_each_pipe(i915, pipe) {
18222                         ret = intel_crtc_init(i915, pipe);
18223                         if (ret) {
18224                                 intel_mode_config_cleanup(i915);
18225                                 return ret;
18226                         }
18227                 }
18228         }
18229
18230         intel_plane_possible_crtcs_init(i915);
18231         intel_shared_dpll_init(dev);
18232         intel_update_fdi_pll_freq(i915);
18233
18234         intel_update_czclk(i915);
18235         intel_modeset_init_hw(i915);
18236
18237         intel_hdcp_component_init(i915);
18238
18239         if (i915->max_cdclk_freq == 0)
18240                 intel_update_max_cdclk(i915);
18241
18242         /*
18243          * If the platform has HTI, we need to find out whether it has reserved
18244          * any display resources before we create our display outputs.
18245          */
18246         if (INTEL_INFO(i915)->display.has_hti)
18247                 i915->hti_state = intel_de_read(i915, HDPORT_STATE);
18248
18249         /* Just disable it once at startup */
18250         intel_vga_disable(i915);
18251         intel_setup_outputs(i915);
18252
18253         drm_modeset_lock_all(dev);
18254         intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
18255         drm_modeset_unlock_all(dev);
18256
18257         for_each_intel_crtc(dev, crtc) {
18258                 struct intel_initial_plane_config plane_config = {};
18259
18260                 if (!crtc->active)
18261                         continue;
18262
18263                 /*
18264                  * Note that reserving the BIOS fb up front prevents us
18265                  * from stuffing other stolen allocations like the ring
18266                  * on top.  This prevents some ugliness at boot time, and
18267                  * can even allow for smooth boot transitions if the BIOS
18268                  * fb is large enough for the active pipe configuration.
18269                  */
18270                 i915->display.get_initial_plane_config(crtc, &plane_config);
18271
18272                 /*
18273                  * If the fb is shared between multiple heads, we'll
18274                  * just get the first one.
18275                  */
18276                 intel_find_initial_plane_obj(crtc, &plane_config);
18277
18278                 plane_config_fini(&plane_config);
18279         }
18280
18281         /*
18282          * Make sure hardware watermarks really match the state we read out.
18283          * Note that we need to do this after reconstructing the BIOS fb's
18284          * since the watermark calculation done here will use pstate->fb.
18285          */
18286         if (!HAS_GMCH(i915))
18287                 sanitize_watermarks(i915);
18288
18289         /*
18290          * Force all active planes to recompute their states. So that on
18291          * mode_setcrtc after probe, all the intel_plane_state variables
18292          * are already calculated and there is no assert_plane warnings
18293          * during bootup.
18294          */
18295         ret = intel_initial_commit(dev);
18296         if (ret)
18297                 drm_dbg_kms(&i915->drm, "Initial commit in probe failed.\n");
18298
18299         return 0;
18300 }
18301
18302 /* part #3: call after gem init */
18303 int intel_modeset_init(struct drm_i915_private *i915)
18304 {
18305         int ret;
18306
18307         if (!HAS_DISPLAY(i915))
18308                 return 0;
18309
18310         intel_overlay_setup(i915);
18311
18312         ret = intel_fbdev_init(&i915->drm);
18313         if (ret)
18314                 return ret;
18315
18316         /* Only enable hotplug handling once the fbdev is fully set up. */
18317         intel_hpd_init(i915);
18318         intel_hpd_poll_disable(i915);
18319
18320         intel_init_ipc(i915);
18321
18322         return 0;
18323 }
18324
18325 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18326 {
18327         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18328         /* 640x480@60Hz, ~25175 kHz */
18329         struct dpll clock = {
18330                 .m1 = 18,
18331                 .m2 = 7,
18332                 .p1 = 13,
18333                 .p2 = 4,
18334                 .n = 2,
18335         };
18336         u32 dpll, fp;
18337         int i;
18338
18339         drm_WARN_ON(&dev_priv->drm,
18340                     i9xx_calc_dpll_params(48000, &clock) != 25154);
18341
18342         drm_dbg_kms(&dev_priv->drm,
18343                     "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
18344                     pipe_name(pipe), clock.vco, clock.dot);
18345
18346         fp = i9xx_dpll_compute_fp(&clock);
18347         dpll = DPLL_DVO_2X_MODE |
18348                 DPLL_VGA_MODE_DIS |
18349                 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
18350                 PLL_P2_DIVIDE_BY_4 |
18351                 PLL_REF_INPUT_DREFCLK |
18352                 DPLL_VCO_ENABLE;
18353
18354         intel_de_write(dev_priv, FP0(pipe), fp);
18355         intel_de_write(dev_priv, FP1(pipe), fp);
18356
18357         intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
18358         intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
18359         intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
18360         intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
18361         intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
18362         intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
18363         intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
18364
18365         /*
18366          * Apparently we need to have VGA mode enabled prior to changing
18367          * the P1/P2 dividers. Otherwise the DPLL will keep using the old
18368          * dividers, even though the register value does change.
18369          */
18370         intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
18371         intel_de_write(dev_priv, DPLL(pipe), dpll);
18372
18373         /* Wait for the clocks to stabilize. */
18374         intel_de_posting_read(dev_priv, DPLL(pipe));
18375         udelay(150);
18376
18377         /* The pixel multiplier can only be updated once the
18378          * DPLL is enabled and the clocks are stable.
18379          *
18380          * So write it again.
18381          */
18382         intel_de_write(dev_priv, DPLL(pipe), dpll);
18383
18384         /* We do this three times for luck */
18385         for (i = 0; i < 3 ; i++) {
18386                 intel_de_write(dev_priv, DPLL(pipe), dpll);
18387                 intel_de_posting_read(dev_priv, DPLL(pipe));
18388                 udelay(150); /* wait for warmup */
18389         }
18390
18391         intel_de_write(dev_priv, PIPECONF(pipe),
18392                        PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
18393         intel_de_posting_read(dev_priv, PIPECONF(pipe));
18394
18395         intel_wait_for_pipe_scanline_moving(crtc);
18396 }
18397
18398 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
18399 {
18400         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18401
18402         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
18403                     pipe_name(pipe));
18404
18405         drm_WARN_ON(&dev_priv->drm,
18406                     intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
18407                     DISPLAY_PLANE_ENABLE);
18408         drm_WARN_ON(&dev_priv->drm,
18409                     intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
18410                     DISPLAY_PLANE_ENABLE);
18411         drm_WARN_ON(&dev_priv->drm,
18412                     intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
18413                     DISPLAY_PLANE_ENABLE);
18414         drm_WARN_ON(&dev_priv->drm,
18415                     intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
18416         drm_WARN_ON(&dev_priv->drm,
18417                     intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
18418
18419         intel_de_write(dev_priv, PIPECONF(pipe), 0);
18420         intel_de_posting_read(dev_priv, PIPECONF(pipe));
18421
18422         intel_wait_for_pipe_scanline_stopped(crtc);
18423
18424         intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
18425         intel_de_posting_read(dev_priv, DPLL(pipe));
18426 }
18427
18428 static void
18429 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
18430 {
18431         struct intel_crtc *crtc;
18432
18433         if (INTEL_GEN(dev_priv) >= 4)
18434                 return;
18435
18436         for_each_intel_crtc(&dev_priv->drm, crtc) {
18437                 struct intel_plane *plane =
18438                         to_intel_plane(crtc->base.primary);
18439                 struct intel_crtc *plane_crtc;
18440                 enum pipe pipe;
18441
18442                 if (!plane->get_hw_state(plane, &pipe))
18443                         continue;
18444
18445                 if (pipe == crtc->pipe)
18446                         continue;
18447
18448                 drm_dbg_kms(&dev_priv->drm,
18449                             "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
18450                             plane->base.base.id, plane->base.name);
18451
18452                 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18453                 intel_plane_disable_noatomic(plane_crtc, plane);
18454         }
18455 }
18456
18457 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
18458 {
18459         struct drm_device *dev = crtc->base.dev;
18460         struct intel_encoder *encoder;
18461
18462         for_each_encoder_on_crtc(dev, &crtc->base, encoder)
18463                 return true;
18464
18465         return false;
18466 }
18467
18468 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
18469 {
18470         struct drm_device *dev = encoder->base.dev;
18471         struct intel_connector *connector;
18472
18473         for_each_connector_on_encoder(dev, &encoder->base, connector)
18474                 return connector;
18475
18476         return NULL;
18477 }
18478
18479 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
18480                               enum pipe pch_transcoder)
18481 {
18482         return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
18483                 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
18484 }
18485
18486 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
18487 {
18488         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
18489         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
18490         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
18491
18492         if (INTEL_GEN(dev_priv) >= 9 ||
18493             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
18494                 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
18495                 u32 val;
18496
18497                 if (transcoder_is_dsi(cpu_transcoder))
18498                         return;
18499
18500                 val = intel_de_read(dev_priv, reg);
18501                 val &= ~HSW_FRAME_START_DELAY_MASK;
18502                 val |= HSW_FRAME_START_DELAY(0);
18503                 intel_de_write(dev_priv, reg, val);
18504         } else {
18505                 i915_reg_t reg = PIPECONF(cpu_transcoder);
18506                 u32 val;
18507
18508                 val = intel_de_read(dev_priv, reg);
18509                 val &= ~PIPECONF_FRAME_START_DELAY_MASK;
18510                 val |= PIPECONF_FRAME_START_DELAY(0);
18511                 intel_de_write(dev_priv, reg, val);
18512         }
18513
18514         if (!crtc_state->has_pch_encoder)
18515                 return;
18516
18517         if (HAS_PCH_IBX(dev_priv)) {
18518                 i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
18519                 u32 val;
18520
18521                 val = intel_de_read(dev_priv, reg);
18522                 val &= ~TRANS_FRAME_START_DELAY_MASK;
18523                 val |= TRANS_FRAME_START_DELAY(0);
18524                 intel_de_write(dev_priv, reg, val);
18525         } else {
18526                 enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
18527                 i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
18528                 u32 val;
18529
18530                 val = intel_de_read(dev_priv, reg);
18531                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
18532                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(0);
18533                 intel_de_write(dev_priv, reg, val);
18534         }
18535 }
18536
18537 static void intel_sanitize_crtc(struct intel_crtc *crtc,
18538                                 struct drm_modeset_acquire_ctx *ctx)
18539 {
18540         struct drm_device *dev = crtc->base.dev;
18541         struct drm_i915_private *dev_priv = to_i915(dev);
18542         struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
18543
18544         if (crtc_state->hw.active) {
18545                 struct intel_plane *plane;
18546
18547                 /* Clear any frame start delays used for debugging left by the BIOS */
18548                 intel_sanitize_frame_start_delay(crtc_state);
18549
18550                 /* Disable everything but the primary plane */
18551                 for_each_intel_plane_on_crtc(dev, crtc, plane) {
18552                         const struct intel_plane_state *plane_state =
18553                                 to_intel_plane_state(plane->base.state);
18554
18555                         if (plane_state->uapi.visible &&
18556                             plane->base.type != DRM_PLANE_TYPE_PRIMARY)
18557                                 intel_plane_disable_noatomic(crtc, plane);
18558                 }
18559
18560                 /*
18561                  * Disable any background color set by the BIOS, but enable the
18562                  * gamma and CSC to match how we program our planes.
18563                  */
18564                 if (INTEL_GEN(dev_priv) >= 9)
18565                         intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
18566                                        SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
18567         }
18568
18569         /* Adjust the state of the output pipe according to whether we
18570          * have active connectors/encoders. */
18571         if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc))
18572                 intel_crtc_disable_noatomic(crtc, ctx);
18573
18574         if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
18575                 /*
18576                  * We start out with underrun reporting disabled to avoid races.
18577                  * For correct bookkeeping mark this on active crtcs.
18578                  *
18579                  * Also on gmch platforms we dont have any hardware bits to
18580                  * disable the underrun reporting. Which means we need to start
18581                  * out with underrun reporting disabled also on inactive pipes,
18582                  * since otherwise we'll complain about the garbage we read when
18583                  * e.g. coming up after runtime pm.
18584                  *
18585                  * No protection against concurrent access is required - at
18586                  * worst a fifo underrun happens which also sets this to false.
18587                  */
18588                 crtc->cpu_fifo_underrun_disabled = true;
18589                 /*
18590                  * We track the PCH trancoder underrun reporting state
18591                  * within the crtc. With crtc for pipe A housing the underrun
18592                  * reporting state for PCH transcoder A, crtc for pipe B housing
18593                  * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
18594                  * and marking underrun reporting as disabled for the non-existing
18595                  * PCH transcoders B and C would prevent enabling the south
18596                  * error interrupt (see cpt_can_enable_serr_int()).
18597                  */
18598                 if (has_pch_trancoder(dev_priv, crtc->pipe))
18599                         crtc->pch_fifo_underrun_disabled = true;
18600         }
18601 }
18602
18603 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
18604 {
18605         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
18606
18607         /*
18608          * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
18609          * the hardware when a high res displays plugged in. DPLL P
18610          * divider is zero, and the pipe timings are bonkers. We'll
18611          * try to disable everything in that case.
18612          *
18613          * FIXME would be nice to be able to sanitize this state
18614          * without several WARNs, but for now let's take the easy
18615          * road.
18616          */
18617         return IS_GEN(dev_priv, 6) &&
18618                 crtc_state->hw.active &&
18619                 crtc_state->shared_dpll &&
18620                 crtc_state->port_clock == 0;
18621 }
18622
18623 static void intel_sanitize_encoder(struct intel_encoder *encoder)
18624 {
18625         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
18626         struct intel_connector *connector;
18627         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
18628         struct intel_crtc_state *crtc_state = crtc ?
18629                 to_intel_crtc_state(crtc->base.state) : NULL;
18630
18631         /* We need to check both for a crtc link (meaning that the
18632          * encoder is active and trying to read from a pipe) and the
18633          * pipe itself being active. */
18634         bool has_active_crtc = crtc_state &&
18635                 crtc_state->hw.active;
18636
18637         if (crtc_state && has_bogus_dpll_config(crtc_state)) {
18638                 drm_dbg_kms(&dev_priv->drm,
18639                             "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
18640                             pipe_name(crtc->pipe));
18641                 has_active_crtc = false;
18642         }
18643
18644         connector = intel_encoder_find_connector(encoder);
18645         if (connector && !has_active_crtc) {
18646                 drm_dbg_kms(&dev_priv->drm,
18647                             "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
18648                             encoder->base.base.id,
18649                             encoder->base.name);
18650
18651                 /* Connector is active, but has no active pipe. This is
18652                  * fallout from our resume register restoring. Disable
18653                  * the encoder manually again. */
18654                 if (crtc_state) {
18655                         struct drm_encoder *best_encoder;
18656
18657                         drm_dbg_kms(&dev_priv->drm,
18658                                     "[ENCODER:%d:%s] manually disabled\n",
18659                                     encoder->base.base.id,
18660                                     encoder->base.name);
18661
18662                         /* avoid oopsing in case the hooks consult best_encoder */
18663                         best_encoder = connector->base.state->best_encoder;
18664                         connector->base.state->best_encoder = &encoder->base;
18665
18666                         /* FIXME NULL atomic state passed! */
18667                         if (encoder->disable)
18668                                 encoder->disable(NULL, encoder, crtc_state,
18669                                                  connector->base.state);
18670                         if (encoder->post_disable)
18671                                 encoder->post_disable(NULL, encoder, crtc_state,
18672                                                       connector->base.state);
18673
18674                         connector->base.state->best_encoder = best_encoder;
18675                 }
18676                 encoder->base.crtc = NULL;
18677
18678                 /* Inconsistent output/port/pipe state happens presumably due to
18679                  * a bug in one of the get_hw_state functions. Or someplace else
18680                  * in our code, like the register restore mess on resume. Clamp
18681                  * things to off as a safer default. */
18682
18683                 connector->base.dpms = DRM_MODE_DPMS_OFF;
18684                 connector->base.encoder = NULL;
18685         }
18686
18687         /* notify opregion of the sanitized encoder state */
18688         intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
18689
18690         if (INTEL_GEN(dev_priv) >= 11)
18691                 icl_sanitize_encoder_pll_mapping(encoder);
18692 }
18693
18694 /* FIXME read out full plane state for all planes */
18695 static void readout_plane_state(struct drm_i915_private *dev_priv)
18696 {
18697         struct intel_plane *plane;
18698         struct intel_crtc *crtc;
18699
18700         for_each_intel_plane(&dev_priv->drm, plane) {
18701                 struct intel_plane_state *plane_state =
18702                         to_intel_plane_state(plane->base.state);
18703                 struct intel_crtc_state *crtc_state;
18704                 enum pipe pipe = PIPE_A;
18705                 bool visible;
18706
18707                 visible = plane->get_hw_state(plane, &pipe);
18708
18709                 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18710                 crtc_state = to_intel_crtc_state(crtc->base.state);
18711
18712                 intel_set_plane_visible(crtc_state, plane_state, visible);
18713
18714                 drm_dbg_kms(&dev_priv->drm,
18715                             "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
18716                             plane->base.base.id, plane->base.name,
18717                             enableddisabled(visible), pipe_name(pipe));
18718         }
18719
18720         for_each_intel_crtc(&dev_priv->drm, crtc) {
18721                 struct intel_crtc_state *crtc_state =
18722                         to_intel_crtc_state(crtc->base.state);
18723
18724                 fixup_active_planes(crtc_state);
18725         }
18726 }
18727
18728 static void intel_modeset_readout_hw_state(struct drm_device *dev)
18729 {
18730         struct drm_i915_private *dev_priv = to_i915(dev);
18731         struct intel_cdclk_state *cdclk_state =
18732                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
18733         struct intel_dbuf_state *dbuf_state =
18734                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
18735         enum pipe pipe;
18736         struct intel_crtc *crtc;
18737         struct intel_encoder *encoder;
18738         struct intel_connector *connector;
18739         struct drm_connector_list_iter conn_iter;
18740         u8 active_pipes = 0;
18741
18742         for_each_intel_crtc(dev, crtc) {
18743                 struct intel_crtc_state *crtc_state =
18744                         to_intel_crtc_state(crtc->base.state);
18745
18746                 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
18747                 intel_crtc_free_hw_state(crtc_state);
18748                 intel_crtc_state_reset(crtc_state, crtc);
18749
18750                 intel_crtc_get_pipe_config(crtc_state);
18751
18752                 crtc_state->hw.enable = crtc_state->hw.active;
18753
18754                 crtc->base.enabled = crtc_state->hw.enable;
18755                 crtc->active = crtc_state->hw.active;
18756
18757                 if (crtc_state->hw.active)
18758                         active_pipes |= BIT(crtc->pipe);
18759
18760                 drm_dbg_kms(&dev_priv->drm,
18761                             "[CRTC:%d:%s] hw state readout: %s\n",
18762                             crtc->base.base.id, crtc->base.name,
18763                             enableddisabled(crtc_state->hw.active));
18764         }
18765
18766         dev_priv->active_pipes = cdclk_state->active_pipes =
18767                 dbuf_state->active_pipes = active_pipes;
18768
18769         readout_plane_state(dev_priv);
18770
18771         intel_dpll_readout_hw_state(dev_priv);
18772
18773         for_each_intel_encoder(dev, encoder) {
18774                 pipe = 0;
18775
18776                 if (encoder->get_hw_state(encoder, &pipe)) {
18777                         struct intel_crtc_state *crtc_state;
18778
18779                         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
18780                         crtc_state = to_intel_crtc_state(crtc->base.state);
18781
18782                         encoder->base.crtc = &crtc->base;
18783                         intel_encoder_get_config(encoder, crtc_state);
18784                         if (encoder->sync_state)
18785                                 encoder->sync_state(encoder, crtc_state);
18786                 } else {
18787                         encoder->base.crtc = NULL;
18788                 }
18789
18790                 drm_dbg_kms(&dev_priv->drm,
18791                             "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
18792                             encoder->base.base.id, encoder->base.name,
18793                             enableddisabled(encoder->base.crtc),
18794                             pipe_name(pipe));
18795         }
18796
18797         drm_connector_list_iter_begin(dev, &conn_iter);
18798         for_each_intel_connector_iter(connector, &conn_iter) {
18799                 if (connector->get_hw_state(connector)) {
18800                         struct intel_crtc_state *crtc_state;
18801                         struct intel_crtc *crtc;
18802
18803                         connector->base.dpms = DRM_MODE_DPMS_ON;
18804
18805                         encoder = intel_attached_encoder(connector);
18806                         connector->base.encoder = &encoder->base;
18807
18808                         crtc = to_intel_crtc(encoder->base.crtc);
18809                         crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
18810
18811                         if (crtc_state && crtc_state->hw.active) {
18812                                 /*
18813                                  * This has to be done during hardware readout
18814                                  * because anything calling .crtc_disable may
18815                                  * rely on the connector_mask being accurate.
18816                                  */
18817                                 crtc_state->uapi.connector_mask |=
18818                                         drm_connector_mask(&connector->base);
18819                                 crtc_state->uapi.encoder_mask |=
18820                                         drm_encoder_mask(&encoder->base);
18821                         }
18822                 } else {
18823                         connector->base.dpms = DRM_MODE_DPMS_OFF;
18824                         connector->base.encoder = NULL;
18825                 }
18826                 drm_dbg_kms(&dev_priv->drm,
18827                             "[CONNECTOR:%d:%s] hw state readout: %s\n",
18828                             connector->base.base.id, connector->base.name,
18829                             enableddisabled(connector->base.encoder));
18830         }
18831         drm_connector_list_iter_end(&conn_iter);
18832
18833         for_each_intel_crtc(dev, crtc) {
18834                 struct intel_bw_state *bw_state =
18835                         to_intel_bw_state(dev_priv->bw_obj.state);
18836                 struct intel_crtc_state *crtc_state =
18837                         to_intel_crtc_state(crtc->base.state);
18838                 struct intel_plane *plane;
18839                 int min_cdclk = 0;
18840
18841                 if (crtc_state->hw.active) {
18842                         struct drm_display_mode *mode = &crtc_state->hw.mode;
18843
18844                         intel_mode_from_crtc_timings(&crtc_state->hw.adjusted_mode,
18845                                                      &crtc_state->hw.adjusted_mode);
18846
18847                         *mode = crtc_state->hw.adjusted_mode;
18848                         mode->hdisplay = crtc_state->pipe_src_w;
18849                         mode->vdisplay = crtc_state->pipe_src_h;
18850
18851                         /*
18852                          * The initial mode needs to be set in order to keep
18853                          * the atomic core happy. It wants a valid mode if the
18854                          * crtc's enabled, so we do the above call.
18855                          *
18856                          * But we don't set all the derived state fully, hence
18857                          * set a flag to indicate that a full recalculation is
18858                          * needed on the next commit.
18859                          */
18860                         crtc_state->inherited = true;
18861
18862                         intel_crtc_compute_pixel_rate(crtc_state);
18863
18864                         intel_crtc_update_active_timings(crtc_state);
18865
18866                         intel_crtc_copy_hw_to_uapi_state(crtc_state);
18867                 }
18868
18869                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
18870                         const struct intel_plane_state *plane_state =
18871                                 to_intel_plane_state(plane->base.state);
18872
18873                         /*
18874                          * FIXME don't have the fb yet, so can't
18875                          * use intel_plane_data_rate() :(
18876                          */
18877                         if (plane_state->uapi.visible)
18878                                 crtc_state->data_rate[plane->id] =
18879                                         4 * crtc_state->pixel_rate;
18880                         /*
18881                          * FIXME don't have the fb yet, so can't
18882                          * use plane->min_cdclk() :(
18883                          */
18884                         if (plane_state->uapi.visible && plane->min_cdclk) {
18885                                 if (crtc_state->double_wide ||
18886                                     INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
18887                                         crtc_state->min_cdclk[plane->id] =
18888                                                 DIV_ROUND_UP(crtc_state->pixel_rate, 2);
18889                                 else
18890                                         crtc_state->min_cdclk[plane->id] =
18891                                                 crtc_state->pixel_rate;
18892                         }
18893                         drm_dbg_kms(&dev_priv->drm,
18894                                     "[PLANE:%d:%s] min_cdclk %d kHz\n",
18895                                     plane->base.base.id, plane->base.name,
18896                                     crtc_state->min_cdclk[plane->id]);
18897                 }
18898
18899                 if (crtc_state->hw.active) {
18900                         min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
18901                         if (drm_WARN_ON(dev, min_cdclk < 0))
18902                                 min_cdclk = 0;
18903                 }
18904
18905                 cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
18906                 cdclk_state->min_voltage_level[crtc->pipe] =
18907                         crtc_state->min_voltage_level;
18908
18909                 intel_bw_crtc_update(bw_state, crtc_state);
18910
18911                 intel_pipe_config_sanity_check(dev_priv, crtc_state);
18912         }
18913 }
18914
18915 static void
18916 get_encoder_power_domains(struct drm_i915_private *dev_priv)
18917 {
18918         struct intel_encoder *encoder;
18919
18920         for_each_intel_encoder(&dev_priv->drm, encoder) {
18921                 struct intel_crtc_state *crtc_state;
18922
18923                 if (!encoder->get_power_domains)
18924                         continue;
18925
18926                 /*
18927                  * MST-primary and inactive encoders don't have a crtc state
18928                  * and neither of these require any power domain references.
18929                  */
18930                 if (!encoder->base.crtc)
18931                         continue;
18932
18933                 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
18934                 encoder->get_power_domains(encoder, crtc_state);
18935         }
18936 }
18937
18938 static void intel_early_display_was(struct drm_i915_private *dev_priv)
18939 {
18940         /*
18941          * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
18942          * Also known as Wa_14010480278.
18943          */
18944         if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
18945                 intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
18946                                intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
18947
18948         if (IS_HASWELL(dev_priv)) {
18949                 /*
18950                  * WaRsPkgCStateDisplayPMReq:hsw
18951                  * System hang if this isn't done before disabling all planes!
18952                  */
18953                 intel_de_write(dev_priv, CHICKEN_PAR1_1,
18954                                intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
18955         }
18956
18957         if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
18958                 /* Display WA #1142:kbl,cfl,cml */
18959                 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
18960                              KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
18961                 intel_de_rmw(dev_priv, CHICKEN_MISC_2,
18962                              KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
18963                              KBL_ARB_FILL_SPARE_14);
18964         }
18965 }
18966
18967 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
18968                                        enum port port, i915_reg_t hdmi_reg)
18969 {
18970         u32 val = intel_de_read(dev_priv, hdmi_reg);
18971
18972         if (val & SDVO_ENABLE ||
18973             (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
18974                 return;
18975
18976         drm_dbg_kms(&dev_priv->drm,
18977                     "Sanitizing transcoder select for HDMI %c\n",
18978                     port_name(port));
18979
18980         val &= ~SDVO_PIPE_SEL_MASK;
18981         val |= SDVO_PIPE_SEL(PIPE_A);
18982
18983         intel_de_write(dev_priv, hdmi_reg, val);
18984 }
18985
18986 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
18987                                      enum port port, i915_reg_t dp_reg)
18988 {
18989         u32 val = intel_de_read(dev_priv, dp_reg);
18990
18991         if (val & DP_PORT_EN ||
18992             (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
18993                 return;
18994
18995         drm_dbg_kms(&dev_priv->drm,
18996                     "Sanitizing transcoder select for DP %c\n",
18997                     port_name(port));
18998
18999         val &= ~DP_PIPE_SEL_MASK;
19000         val |= DP_PIPE_SEL(PIPE_A);
19001
19002         intel_de_write(dev_priv, dp_reg, val);
19003 }
19004
19005 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
19006 {
19007         /*
19008          * The BIOS may select transcoder B on some of the PCH
19009          * ports even it doesn't enable the port. This would trip
19010          * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
19011          * Sanitize the transcoder select bits to prevent that. We
19012          * assume that the BIOS never actually enabled the port,
19013          * because if it did we'd actually have to toggle the port
19014          * on and back off to make the transcoder A select stick
19015          * (see. intel_dp_link_down(), intel_disable_hdmi(),
19016          * intel_disable_sdvo()).
19017          */
19018         ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
19019         ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
19020         ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
19021
19022         /* PCH SDVOB multiplex with HDMIB */
19023         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
19024         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
19025         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
19026 }
19027
19028 /* Scan out the current hw modeset state,
19029  * and sanitizes it to the current state
19030  */
19031 static void
19032 intel_modeset_setup_hw_state(struct drm_device *dev,
19033                              struct drm_modeset_acquire_ctx *ctx)
19034 {
19035         struct drm_i915_private *dev_priv = to_i915(dev);
19036         struct intel_encoder *encoder;
19037         struct intel_crtc *crtc;
19038         intel_wakeref_t wakeref;
19039
19040         wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
19041
19042         intel_early_display_was(dev_priv);
19043         intel_modeset_readout_hw_state(dev);
19044
19045         /* HW state is read out, now we need to sanitize this mess. */
19046
19047         /* Sanitize the TypeC port mode upfront, encoders depend on this */
19048         for_each_intel_encoder(dev, encoder) {
19049                 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
19050
19051                 /* We need to sanitize only the MST primary port. */
19052                 if (encoder->type != INTEL_OUTPUT_DP_MST &&
19053                     intel_phy_is_tc(dev_priv, phy))
19054                         intel_tc_port_sanitize(enc_to_dig_port(encoder));
19055         }
19056
19057         get_encoder_power_domains(dev_priv);
19058
19059         if (HAS_PCH_IBX(dev_priv))
19060                 ibx_sanitize_pch_ports(dev_priv);
19061
19062         /*
19063          * intel_sanitize_plane_mapping() may need to do vblank
19064          * waits, so we need vblank interrupts restored beforehand.
19065          */
19066         for_each_intel_crtc(&dev_priv->drm, crtc) {
19067                 struct intel_crtc_state *crtc_state =
19068                         to_intel_crtc_state(crtc->base.state);
19069
19070                 drm_crtc_vblank_reset(&crtc->base);
19071
19072                 if (crtc_state->hw.active)
19073                         intel_crtc_vblank_on(crtc_state);
19074         }
19075
19076         intel_sanitize_plane_mapping(dev_priv);
19077
19078         for_each_intel_encoder(dev, encoder)
19079                 intel_sanitize_encoder(encoder);
19080
19081         for_each_intel_crtc(&dev_priv->drm, crtc) {
19082                 struct intel_crtc_state *crtc_state =
19083                         to_intel_crtc_state(crtc->base.state);
19084
19085                 intel_sanitize_crtc(crtc, ctx);
19086                 intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
19087         }
19088
19089         intel_modeset_update_connector_atomic_state(dev);
19090
19091         intel_dpll_sanitize_state(dev_priv);
19092
19093         if (IS_G4X(dev_priv)) {
19094                 g4x_wm_get_hw_state(dev_priv);
19095                 g4x_wm_sanitize(dev_priv);
19096         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
19097                 vlv_wm_get_hw_state(dev_priv);
19098                 vlv_wm_sanitize(dev_priv);
19099         } else if (INTEL_GEN(dev_priv) >= 9) {
19100                 skl_wm_get_hw_state(dev_priv);
19101         } else if (HAS_PCH_SPLIT(dev_priv)) {
19102                 ilk_wm_get_hw_state(dev_priv);
19103         }
19104
19105         for_each_intel_crtc(dev, crtc) {
19106                 struct intel_crtc_state *crtc_state =
19107                         to_intel_crtc_state(crtc->base.state);
19108                 u64 put_domains;
19109
19110                 put_domains = modeset_get_crtc_power_domains(crtc_state);
19111                 if (drm_WARN_ON(dev, put_domains))
19112                         modeset_put_power_domains(dev_priv, put_domains);
19113         }
19114
19115         intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
19116 }
19117
19118 void intel_display_resume(struct drm_device *dev)
19119 {
19120         struct drm_i915_private *dev_priv = to_i915(dev);
19121         struct drm_atomic_state *state = dev_priv->modeset_restore_state;
19122         struct drm_modeset_acquire_ctx ctx;
19123         int ret;
19124
19125         dev_priv->modeset_restore_state = NULL;
19126         if (state)
19127                 state->acquire_ctx = &ctx;
19128
19129         drm_modeset_acquire_init(&ctx, 0);
19130
19131         while (1) {
19132                 ret = drm_modeset_lock_all_ctx(dev, &ctx);
19133                 if (ret != -EDEADLK)
19134                         break;
19135
19136                 drm_modeset_backoff(&ctx);
19137         }
19138
19139         if (!ret)
19140                 ret = __intel_display_resume(dev, state, &ctx);
19141
19142         intel_enable_ipc(dev_priv);
19143         drm_modeset_drop_locks(&ctx);
19144         drm_modeset_acquire_fini(&ctx);
19145
19146         if (ret)
19147                 drm_err(&dev_priv->drm,
19148                         "Restoring old state failed with %i\n", ret);
19149         if (state)
19150                 drm_atomic_state_put(state);
19151 }
19152
19153 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
19154 {
19155         struct intel_connector *connector;
19156         struct drm_connector_list_iter conn_iter;
19157
19158         /* Kill all the work that may have been queued by hpd. */
19159         drm_connector_list_iter_begin(&i915->drm, &conn_iter);
19160         for_each_intel_connector_iter(connector, &conn_iter) {
19161                 if (connector->modeset_retry_work.func)
19162                         cancel_work_sync(&connector->modeset_retry_work);
19163                 if (connector->hdcp.shim) {
19164                         cancel_delayed_work_sync(&connector->hdcp.check_work);
19165                         cancel_work_sync(&connector->hdcp.prop_work);
19166                 }
19167         }
19168         drm_connector_list_iter_end(&conn_iter);
19169 }
19170
19171 /* part #1: call before irq uninstall */
19172 void intel_modeset_driver_remove(struct drm_i915_private *i915)
19173 {
19174         flush_workqueue(i915->flip_wq);
19175         flush_workqueue(i915->modeset_wq);
19176
19177         flush_work(&i915->atomic_helper.free_work);
19178         drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
19179 }
19180
19181 /* part #2: call after irq uninstall */
19182 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
19183 {
19184         /*
19185          * Due to the hpd irq storm handling the hotplug work can re-arm the
19186          * poll handlers. Hence disable polling after hpd handling is shut down.
19187          */
19188         intel_hpd_poll_fini(i915);
19189
19190         /*
19191          * MST topology needs to be suspended so we don't have any calls to
19192          * fbdev after it's finalized. MST will be destroyed later as part of
19193          * drm_mode_config_cleanup()
19194          */
19195         intel_dp_mst_suspend(i915);
19196
19197         /* poll work can call into fbdev, hence clean that up afterwards */
19198         intel_fbdev_fini(i915);
19199
19200         intel_unregister_dsm_handler();
19201
19202         intel_fbc_global_disable(i915);
19203
19204         /* flush any delayed tasks or pending work */
19205         flush_scheduled_work();
19206
19207         intel_hdcp_component_fini(i915);
19208
19209         intel_mode_config_cleanup(i915);
19210
19211         intel_overlay_cleanup(i915);
19212
19213         intel_gmbus_teardown(i915);
19214
19215         destroy_workqueue(i915->flip_wq);
19216         destroy_workqueue(i915->modeset_wq);
19217
19218         intel_fbc_cleanup_cfb(i915);
19219 }
19220
19221 /* part #3: call after gem init */
19222 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
19223 {
19224         intel_csr_ucode_fini(i915);
19225
19226         intel_power_domains_driver_remove(i915);
19227
19228         intel_vga_unregister(i915);
19229
19230         intel_bios_driver_remove(i915);
19231 }
19232
19233 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
19234
19235 struct intel_display_error_state {
19236
19237         u32 power_well_driver;
19238
19239         struct intel_cursor_error_state {
19240                 u32 control;
19241                 u32 position;
19242                 u32 base;
19243                 u32 size;
19244         } cursor[I915_MAX_PIPES];
19245
19246         struct intel_pipe_error_state {
19247                 bool power_domain_on;
19248                 u32 source;
19249                 u32 stat;
19250         } pipe[I915_MAX_PIPES];
19251
19252         struct intel_plane_error_state {
19253                 u32 control;
19254                 u32 stride;
19255                 u32 size;
19256                 u32 pos;
19257                 u32 addr;
19258                 u32 surface;
19259                 u32 tile_offset;
19260         } plane[I915_MAX_PIPES];
19261
19262         struct intel_transcoder_error_state {
19263                 bool available;
19264                 bool power_domain_on;
19265                 enum transcoder cpu_transcoder;
19266
19267                 u32 conf;
19268
19269                 u32 htotal;
19270                 u32 hblank;
19271                 u32 hsync;
19272                 u32 vtotal;
19273                 u32 vblank;
19274                 u32 vsync;
19275         } transcoder[5];
19276 };
19277
19278 struct intel_display_error_state *
19279 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
19280 {
19281         struct intel_display_error_state *error;
19282         int transcoders[] = {
19283                 TRANSCODER_A,
19284                 TRANSCODER_B,
19285                 TRANSCODER_C,
19286                 TRANSCODER_D,
19287                 TRANSCODER_EDP,
19288         };
19289         int i;
19290
19291         BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
19292
19293         if (!HAS_DISPLAY(dev_priv))
19294                 return NULL;
19295
19296         error = kzalloc(sizeof(*error), GFP_ATOMIC);
19297         if (error == NULL)
19298                 return NULL;
19299
19300         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
19301                 error->power_well_driver = intel_de_read(dev_priv,
19302                                                          HSW_PWR_WELL_CTL2);
19303
19304         for_each_pipe(dev_priv, i) {
19305                 error->pipe[i].power_domain_on =
19306                         __intel_display_power_is_enabled(dev_priv,
19307                                                          POWER_DOMAIN_PIPE(i));
19308                 if (!error->pipe[i].power_domain_on)
19309                         continue;
19310
19311                 error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
19312                 error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
19313                 error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
19314
19315                 error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
19316                 error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
19317                 if (INTEL_GEN(dev_priv) <= 3) {
19318                         error->plane[i].size = intel_de_read(dev_priv,
19319                                                              DSPSIZE(i));
19320                         error->plane[i].pos = intel_de_read(dev_priv,
19321                                                             DSPPOS(i));
19322                 }
19323                 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
19324                         error->plane[i].addr = intel_de_read(dev_priv,
19325                                                              DSPADDR(i));
19326                 if (INTEL_GEN(dev_priv) >= 4) {
19327                         error->plane[i].surface = intel_de_read(dev_priv,
19328                                                                 DSPSURF(i));
19329                         error->plane[i].tile_offset = intel_de_read(dev_priv,
19330                                                                     DSPTILEOFF(i));
19331                 }
19332
19333                 error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
19334
19335                 if (HAS_GMCH(dev_priv))
19336                         error->pipe[i].stat = intel_de_read(dev_priv,
19337                                                             PIPESTAT(i));
19338         }
19339
19340         for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19341                 enum transcoder cpu_transcoder = transcoders[i];
19342
19343                 if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
19344                         continue;
19345
19346                 error->transcoder[i].available = true;
19347                 error->transcoder[i].power_domain_on =
19348                         __intel_display_power_is_enabled(dev_priv,
19349                                 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
19350                 if (!error->transcoder[i].power_domain_on)
19351                         continue;
19352
19353                 error->transcoder[i].cpu_transcoder = cpu_transcoder;
19354
19355                 error->transcoder[i].conf = intel_de_read(dev_priv,
19356                                                           PIPECONF(cpu_transcoder));
19357                 error->transcoder[i].htotal = intel_de_read(dev_priv,
19358                                                             HTOTAL(cpu_transcoder));
19359                 error->transcoder[i].hblank = intel_de_read(dev_priv,
19360                                                             HBLANK(cpu_transcoder));
19361                 error->transcoder[i].hsync = intel_de_read(dev_priv,
19362                                                            HSYNC(cpu_transcoder));
19363                 error->transcoder[i].vtotal = intel_de_read(dev_priv,
19364                                                             VTOTAL(cpu_transcoder));
19365                 error->transcoder[i].vblank = intel_de_read(dev_priv,
19366                                                             VBLANK(cpu_transcoder));
19367                 error->transcoder[i].vsync = intel_de_read(dev_priv,
19368                                                            VSYNC(cpu_transcoder));
19369         }
19370
19371         return error;
19372 }
19373
19374 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
19375
19376 void
19377 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
19378                                 struct intel_display_error_state *error)
19379 {
19380         struct drm_i915_private *dev_priv = m->i915;
19381         int i;
19382
19383         if (!error)
19384                 return;
19385
19386         err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
19387         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
19388                 err_printf(m, "PWR_WELL_CTL2: %08x\n",
19389                            error->power_well_driver);
19390         for_each_pipe(dev_priv, i) {
19391                 err_printf(m, "Pipe [%d]:\n", i);
19392                 err_printf(m, "  Power: %s\n",
19393                            onoff(error->pipe[i].power_domain_on));
19394                 err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
19395                 err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
19396
19397                 err_printf(m, "Plane [%d]:\n", i);
19398                 err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
19399                 err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
19400                 if (INTEL_GEN(dev_priv) <= 3) {
19401                         err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
19402                         err_printf(m, "  POS: %08x\n", error->plane[i].pos);
19403                 }
19404                 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
19405                         err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
19406                 if (INTEL_GEN(dev_priv) >= 4) {
19407                         err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
19408                         err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
19409                 }
19410
19411                 err_printf(m, "Cursor [%d]:\n", i);
19412                 err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
19413                 err_printf(m, "  POS: %08x\n", error->cursor[i].position);
19414                 err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
19415         }
19416
19417         for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
19418                 if (!error->transcoder[i].available)
19419                         continue;
19420
19421                 err_printf(m, "CPU transcoder: %s\n",
19422                            transcoder_name(error->transcoder[i].cpu_transcoder));
19423                 err_printf(m, "  Power: %s\n",
19424                            onoff(error->transcoder[i].power_domain_on));
19425                 err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
19426                 err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
19427                 err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
19428                 err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
19429                 err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
19430                 err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
19431                 err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
19432         }
19433 }
19434
19435 #endif