]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Validate mode against max. link data rate for DP MST
[linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
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 <[email protected]>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
5d723d7a 37#include "intel_frontbuffer.h"
760285e7 38#include <drm/i915_drm.h>
79e53945 39#include "i915_drv.h"
db18b6a6 40#include "intel_dsi.h"
e5510fac 41#include "i915_trace.h"
319c1d42 42#include <drm/drm_atomic.h>
c196e1d6 43#include <drm/drm_atomic_helper.h>
760285e7
DH
44#include <drm/drm_dp_helper.h>
45#include <drm/drm_crtc_helper.h>
465c120c
MR
46#include <drm/drm_plane_helper.h>
47#include <drm/drm_rect.h>
c0f372b3 48#include <linux/dma_remapping.h>
fd8e058a 49#include <linux/reservation.h>
79e53945 50
5a21b665
SV
51static bool is_mmio_work(struct intel_flip_work *work)
52{
53 return work->mmio_work.func;
54}
55
465c120c 56/* Primary plane formats for gen <= 3 */
568db4f2 57static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
465c120c 60 DRM_FORMAT_XRGB1555,
67fe7dc5 61 DRM_FORMAT_XRGB8888,
465c120c
MR
62};
63
64/* Primary plane formats for gen >= 4 */
568db4f2 65static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
66 DRM_FORMAT_C8,
67 DRM_FORMAT_RGB565,
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_XBGR8888,
70 DRM_FORMAT_XRGB2101010,
71 DRM_FORMAT_XBGR2101010,
72};
73
74static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
75 DRM_FORMAT_C8,
76 DRM_FORMAT_RGB565,
77 DRM_FORMAT_XRGB8888,
465c120c 78 DRM_FORMAT_XBGR8888,
67fe7dc5 79 DRM_FORMAT_ARGB8888,
465c120c
MR
80 DRM_FORMAT_ABGR8888,
81 DRM_FORMAT_XRGB2101010,
465c120c 82 DRM_FORMAT_XBGR2101010,
ea916ea0
KM
83 DRM_FORMAT_YUYV,
84 DRM_FORMAT_YVYU,
85 DRM_FORMAT_UYVY,
86 DRM_FORMAT_VYUY,
465c120c
MR
87};
88
3d7d6510
MR
89/* Cursor formats */
90static const uint32_t intel_cursor_formats[] = {
91 DRM_FORMAT_ARGB8888,
92};
93
f1f644dc 94static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 95 struct intel_crtc_state *pipe_config);
18442d08 96static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 97 struct intel_crtc_state *pipe_config);
f1f644dc 98
eb1bfe80
JB
99static int intel_framebuffer_init(struct drm_device *dev,
100 struct intel_framebuffer *ifb,
101 struct drm_mode_fb_cmd2 *mode_cmd,
102 struct drm_i915_gem_object *obj);
5b18e57c
SV
103static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
104static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
bc58be60 105static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
29407aab 106static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
107 struct intel_link_m_n *m_n,
108 struct intel_link_m_n *m2_n2);
29407aab 109static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97 110static void haswell_set_pipeconf(struct drm_crtc *crtc);
391bf048 111static void haswell_set_pipemisc(struct drm_crtc *crtc);
d288f65f 112static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 113 const struct intel_crtc_state *pipe_config);
d288f65f 114static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 115 const struct intel_crtc_state *pipe_config);
5a21b665
SV
116static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
117static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
65edccce
VS
118static void skl_init_scalers(struct drm_i915_private *dev_priv,
119 struct intel_crtc *crtc,
120 struct intel_crtc_state *crtc_state);
bfd16b2a
ML
121static void skylake_pfit_enable(struct intel_crtc *crtc);
122static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
123static void ironlake_pfit_enable(struct intel_crtc *crtc);
043e9bda 124static void intel_modeset_setup_hw_state(struct drm_device *dev);
2622a081 125static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
4e5ca60f 126static int ilk_max_pixel_rate(struct drm_atomic_state *state);
89b3c3c7 127static int glk_calc_cdclk(int max_pixclk);
324513c0 128static int bxt_calc_cdclk(int max_pixclk);
e7457a9a 129
d4906093 130struct intel_limit {
4c5def93
ACO
131 struct {
132 int min, max;
133 } dot, vco, n, m, m1, m2, p, p1;
134
135 struct {
136 int dot_limit;
137 int p2_slow, p2_fast;
138 } p2;
d4906093 139};
79e53945 140
bfa7df01
VS
141/* returns HPLL frequency in kHz */
142static int valleyview_get_vco(struct drm_i915_private *dev_priv)
143{
144 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
145
146 /* Obtain SKU information */
147 mutex_lock(&dev_priv->sb_lock);
148 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
149 CCK_FUSE_HPLL_FREQ_MASK;
150 mutex_unlock(&dev_priv->sb_lock);
151
152 return vco_freq[hpll_freq] * 1000;
153}
154
c30fec65
VS
155int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
156 const char *name, u32 reg, int ref_freq)
bfa7df01
VS
157{
158 u32 val;
159 int divider;
160
bfa7df01
VS
161 mutex_lock(&dev_priv->sb_lock);
162 val = vlv_cck_read(dev_priv, reg);
163 mutex_unlock(&dev_priv->sb_lock);
164
165 divider = val & CCK_FREQUENCY_VALUES;
166
167 WARN((val & CCK_FREQUENCY_STATUS) !=
168 (divider << CCK_FREQUENCY_STATUS_SHIFT),
169 "%s change in progress\n", name);
170
c30fec65
VS
171 return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
172}
173
174static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
175 const char *name, u32 reg)
176{
177 if (dev_priv->hpll_freq == 0)
178 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
179
180 return vlv_get_cck_clock(dev_priv, name, reg,
181 dev_priv->hpll_freq);
bfa7df01
VS
182}
183
e7dc33f3
VS
184static int
185intel_pch_rawclk(struct drm_i915_private *dev_priv)
d2acd215 186{
e7dc33f3
VS
187 return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
188}
d2acd215 189
e7dc33f3
VS
190static int
191intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
192{
19ab4ed3 193 /* RAWCLK_FREQ_VLV register updated from power well code */
35d38d1f
VS
194 return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
195 CCK_DISPLAY_REF_CLOCK_CONTROL);
d2acd215
SV
196}
197
e7dc33f3
VS
198static int
199intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
79e50a4f 200{
79e50a4f
JN
201 uint32_t clkcfg;
202
e7dc33f3 203 /* hrawclock is 1/4 the FSB frequency */
79e50a4f
JN
204 clkcfg = I915_READ(CLKCFG);
205 switch (clkcfg & CLKCFG_FSB_MASK) {
206 case CLKCFG_FSB_400:
e7dc33f3 207 return 100000;
79e50a4f 208 case CLKCFG_FSB_533:
e7dc33f3 209 return 133333;
79e50a4f 210 case CLKCFG_FSB_667:
e7dc33f3 211 return 166667;
79e50a4f 212 case CLKCFG_FSB_800:
e7dc33f3 213 return 200000;
79e50a4f 214 case CLKCFG_FSB_1067:
e7dc33f3 215 return 266667;
79e50a4f 216 case CLKCFG_FSB_1333:
e7dc33f3 217 return 333333;
79e50a4f
JN
218 /* these two are just a guess; one of them might be right */
219 case CLKCFG_FSB_1600:
220 case CLKCFG_FSB_1600_ALT:
e7dc33f3 221 return 400000;
79e50a4f 222 default:
e7dc33f3 223 return 133333;
79e50a4f
JN
224 }
225}
226
19ab4ed3 227void intel_update_rawclk(struct drm_i915_private *dev_priv)
e7dc33f3
VS
228{
229 if (HAS_PCH_SPLIT(dev_priv))
230 dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
231 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
232 dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
233 else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
234 dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
235 else
236 return; /* no rawclk on other platforms, or no need to know it */
237
238 DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
239}
240
bfa7df01
VS
241static void intel_update_czclk(struct drm_i915_private *dev_priv)
242{
666a4537 243 if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
bfa7df01
VS
244 return;
245
246 dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
247 CCK_CZ_CLOCK_CONTROL);
248
249 DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
250}
251
021357ac 252static inline u32 /* units of 100MHz */
21a727b3
VS
253intel_fdi_link_freq(struct drm_i915_private *dev_priv,
254 const struct intel_crtc_state *pipe_config)
021357ac 255{
21a727b3
VS
256 if (HAS_DDI(dev_priv))
257 return pipe_config->port_clock; /* SPLL */
258 else if (IS_GEN5(dev_priv))
259 return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
e3b247da 260 else
21a727b3 261 return 270000;
021357ac
CW
262}
263
1b6f4958 264static const struct intel_limit intel_limits_i8xx_dac = {
0206e353 265 .dot = { .min = 25000, .max = 350000 },
9c333719 266 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 267 .n = { .min = 2, .max = 16 },
0206e353
AJ
268 .m = { .min = 96, .max = 140 },
269 .m1 = { .min = 18, .max = 26 },
270 .m2 = { .min = 6, .max = 16 },
271 .p = { .min = 4, .max = 128 },
272 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
273 .p2 = { .dot_limit = 165000,
274 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
275};
276
1b6f4958 277static const struct intel_limit intel_limits_i8xx_dvo = {
5d536e28 278 .dot = { .min = 25000, .max = 350000 },
9c333719 279 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 280 .n = { .min = 2, .max = 16 },
5d536e28
SV
281 .m = { .min = 96, .max = 140 },
282 .m1 = { .min = 18, .max = 26 },
283 .m2 = { .min = 6, .max = 16 },
284 .p = { .min = 4, .max = 128 },
285 .p1 = { .min = 2, .max = 33 },
286 .p2 = { .dot_limit = 165000,
287 .p2_slow = 4, .p2_fast = 4 },
288};
289
1b6f4958 290static const struct intel_limit intel_limits_i8xx_lvds = {
0206e353 291 .dot = { .min = 25000, .max = 350000 },
9c333719 292 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 293 .n = { .min = 2, .max = 16 },
0206e353
AJ
294 .m = { .min = 96, .max = 140 },
295 .m1 = { .min = 18, .max = 26 },
296 .m2 = { .min = 6, .max = 16 },
297 .p = { .min = 4, .max = 128 },
298 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
299 .p2 = { .dot_limit = 165000,
300 .p2_slow = 14, .p2_fast = 7 },
e4b36699 301};
273e27ca 302
1b6f4958 303static const struct intel_limit intel_limits_i9xx_sdvo = {
0206e353
AJ
304 .dot = { .min = 20000, .max = 400000 },
305 .vco = { .min = 1400000, .max = 2800000 },
306 .n = { .min = 1, .max = 6 },
307 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
308 .m1 = { .min = 8, .max = 18 },
309 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
310 .p = { .min = 5, .max = 80 },
311 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
312 .p2 = { .dot_limit = 200000,
313 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
314};
315
1b6f4958 316static const struct intel_limit intel_limits_i9xx_lvds = {
0206e353
AJ
317 .dot = { .min = 20000, .max = 400000 },
318 .vco = { .min = 1400000, .max = 2800000 },
319 .n = { .min = 1, .max = 6 },
320 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
321 .m1 = { .min = 8, .max = 18 },
322 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
323 .p = { .min = 7, .max = 98 },
324 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
325 .p2 = { .dot_limit = 112000,
326 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
327};
328
273e27ca 329
1b6f4958 330static const struct intel_limit intel_limits_g4x_sdvo = {
273e27ca
EA
331 .dot = { .min = 25000, .max = 270000 },
332 .vco = { .min = 1750000, .max = 3500000},
333 .n = { .min = 1, .max = 4 },
334 .m = { .min = 104, .max = 138 },
335 .m1 = { .min = 17, .max = 23 },
336 .m2 = { .min = 5, .max = 11 },
337 .p = { .min = 10, .max = 30 },
338 .p1 = { .min = 1, .max = 3},
339 .p2 = { .dot_limit = 270000,
340 .p2_slow = 10,
341 .p2_fast = 10
044c7c41 342 },
e4b36699
KP
343};
344
1b6f4958 345static const struct intel_limit intel_limits_g4x_hdmi = {
273e27ca
EA
346 .dot = { .min = 22000, .max = 400000 },
347 .vco = { .min = 1750000, .max = 3500000},
348 .n = { .min = 1, .max = 4 },
349 .m = { .min = 104, .max = 138 },
350 .m1 = { .min = 16, .max = 23 },
351 .m2 = { .min = 5, .max = 11 },
352 .p = { .min = 5, .max = 80 },
353 .p1 = { .min = 1, .max = 8},
354 .p2 = { .dot_limit = 165000,
355 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
356};
357
1b6f4958 358static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
359 .dot = { .min = 20000, .max = 115000 },
360 .vco = { .min = 1750000, .max = 3500000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 104, .max = 138 },
363 .m1 = { .min = 17, .max = 23 },
364 .m2 = { .min = 5, .max = 11 },
365 .p = { .min = 28, .max = 112 },
366 .p1 = { .min = 2, .max = 8 },
367 .p2 = { .dot_limit = 0,
368 .p2_slow = 14, .p2_fast = 14
044c7c41 369 },
e4b36699
KP
370};
371
1b6f4958 372static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
373 .dot = { .min = 80000, .max = 224000 },
374 .vco = { .min = 1750000, .max = 3500000 },
375 .n = { .min = 1, .max = 3 },
376 .m = { .min = 104, .max = 138 },
377 .m1 = { .min = 17, .max = 23 },
378 .m2 = { .min = 5, .max = 11 },
379 .p = { .min = 14, .max = 42 },
380 .p1 = { .min = 2, .max = 6 },
381 .p2 = { .dot_limit = 0,
382 .p2_slow = 7, .p2_fast = 7
044c7c41 383 },
e4b36699
KP
384};
385
1b6f4958 386static const struct intel_limit intel_limits_pineview_sdvo = {
0206e353
AJ
387 .dot = { .min = 20000, .max = 400000},
388 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 389 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
390 .n = { .min = 3, .max = 6 },
391 .m = { .min = 2, .max = 256 },
273e27ca 392 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
393 .m1 = { .min = 0, .max = 0 },
394 .m2 = { .min = 0, .max = 254 },
395 .p = { .min = 5, .max = 80 },
396 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
397 .p2 = { .dot_limit = 200000,
398 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
399};
400
1b6f4958 401static const struct intel_limit intel_limits_pineview_lvds = {
0206e353
AJ
402 .dot = { .min = 20000, .max = 400000 },
403 .vco = { .min = 1700000, .max = 3500000 },
404 .n = { .min = 3, .max = 6 },
405 .m = { .min = 2, .max = 256 },
406 .m1 = { .min = 0, .max = 0 },
407 .m2 = { .min = 0, .max = 254 },
408 .p = { .min = 7, .max = 112 },
409 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
410 .p2 = { .dot_limit = 112000,
411 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
412};
413
273e27ca
EA
414/* Ironlake / Sandybridge
415 *
416 * We calculate clock using (register_value + 2) for N/M1/M2, so here
417 * the range value for them is (actual_value - 2).
418 */
1b6f4958 419static const struct intel_limit intel_limits_ironlake_dac = {
273e27ca
EA
420 .dot = { .min = 25000, .max = 350000 },
421 .vco = { .min = 1760000, .max = 3510000 },
422 .n = { .min = 1, .max = 5 },
423 .m = { .min = 79, .max = 127 },
424 .m1 = { .min = 12, .max = 22 },
425 .m2 = { .min = 5, .max = 9 },
426 .p = { .min = 5, .max = 80 },
427 .p1 = { .min = 1, .max = 8 },
428 .p2 = { .dot_limit = 225000,
429 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
430};
431
1b6f4958 432static const struct intel_limit intel_limits_ironlake_single_lvds = {
273e27ca
EA
433 .dot = { .min = 25000, .max = 350000 },
434 .vco = { .min = 1760000, .max = 3510000 },
435 .n = { .min = 1, .max = 3 },
436 .m = { .min = 79, .max = 118 },
437 .m1 = { .min = 12, .max = 22 },
438 .m2 = { .min = 5, .max = 9 },
439 .p = { .min = 28, .max = 112 },
440 .p1 = { .min = 2, .max = 8 },
441 .p2 = { .dot_limit = 225000,
442 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
443};
444
1b6f4958 445static const struct intel_limit intel_limits_ironlake_dual_lvds = {
273e27ca
EA
446 .dot = { .min = 25000, .max = 350000 },
447 .vco = { .min = 1760000, .max = 3510000 },
448 .n = { .min = 1, .max = 3 },
449 .m = { .min = 79, .max = 127 },
450 .m1 = { .min = 12, .max = 22 },
451 .m2 = { .min = 5, .max = 9 },
452 .p = { .min = 14, .max = 56 },
453 .p1 = { .min = 2, .max = 8 },
454 .p2 = { .dot_limit = 225000,
455 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
456};
457
273e27ca 458/* LVDS 100mhz refclk limits. */
1b6f4958 459static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
460 .dot = { .min = 25000, .max = 350000 },
461 .vco = { .min = 1760000, .max = 3510000 },
462 .n = { .min = 1, .max = 2 },
463 .m = { .min = 79, .max = 126 },
464 .m1 = { .min = 12, .max = 22 },
465 .m2 = { .min = 5, .max = 9 },
466 .p = { .min = 28, .max = 112 },
0206e353 467 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
468 .p2 = { .dot_limit = 225000,
469 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
470};
471
1b6f4958 472static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
473 .dot = { .min = 25000, .max = 350000 },
474 .vco = { .min = 1760000, .max = 3510000 },
475 .n = { .min = 1, .max = 3 },
476 .m = { .min = 79, .max = 126 },
477 .m1 = { .min = 12, .max = 22 },
478 .m2 = { .min = 5, .max = 9 },
479 .p = { .min = 14, .max = 42 },
0206e353 480 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
481 .p2 = { .dot_limit = 225000,
482 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
483};
484
1b6f4958 485static const struct intel_limit intel_limits_vlv = {
f01b7962
VS
486 /*
487 * These are the data rate limits (measured in fast clocks)
488 * since those are the strictest limits we have. The fast
489 * clock and actual rate limits are more relaxed, so checking
490 * them would make no difference.
491 */
492 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 493 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 494 .n = { .min = 1, .max = 7 },
a0c4da24
JB
495 .m1 = { .min = 2, .max = 3 },
496 .m2 = { .min = 11, .max = 156 },
b99ab663 497 .p1 = { .min = 2, .max = 3 },
5fdc9c49 498 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
499};
500
1b6f4958 501static const struct intel_limit intel_limits_chv = {
ef9348c8
CML
502 /*
503 * These are the data rate limits (measured in fast clocks)
504 * since those are the strictest limits we have. The fast
505 * clock and actual rate limits are more relaxed, so checking
506 * them would make no difference.
507 */
508 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 509 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
510 .n = { .min = 1, .max = 1 },
511 .m1 = { .min = 2, .max = 2 },
512 .m2 = { .min = 24 << 22, .max = 175 << 22 },
513 .p1 = { .min = 2, .max = 4 },
514 .p2 = { .p2_slow = 1, .p2_fast = 14 },
515};
516
1b6f4958 517static const struct intel_limit intel_limits_bxt = {
5ab7b0b7
ID
518 /* FIXME: find real dot limits */
519 .dot = { .min = 0, .max = INT_MAX },
e6292556 520 .vco = { .min = 4800000, .max = 6700000 },
5ab7b0b7
ID
521 .n = { .min = 1, .max = 1 },
522 .m1 = { .min = 2, .max = 2 },
523 /* FIXME: find real m2 limits */
524 .m2 = { .min = 2 << 22, .max = 255 << 22 },
525 .p1 = { .min = 2, .max = 4 },
526 .p2 = { .p2_slow = 1, .p2_fast = 20 },
527};
528
cdba954e
ACO
529static bool
530needs_modeset(struct drm_crtc_state *state)
531{
fc596660 532 return drm_atomic_crtc_needs_modeset(state);
cdba954e
ACO
533}
534
dccbea3b
ID
535/*
536 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
537 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
538 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
539 * The helpers' return value is the rate of the clock that is fed to the
540 * display engine's pipe which can be the above fast dot clock rate or a
541 * divided-down version of it.
542 */
f2b115e6 543/* m1 is reserved as 0 in Pineview, n is a ring counter */
9e2c8475 544static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
79e53945 545{
2177832f
SL
546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
ed5ca77e 548 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 549 return 0;
fb03ac01
VS
550 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
551 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
552
553 return clock->dot;
2177832f
SL
554}
555
7429e9d4
SV
556static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
557{
558 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
559}
560
9e2c8475 561static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
2177832f 562{
7429e9d4 563 clock->m = i9xx_dpll_compute_m(clock);
79e53945 564 clock->p = clock->p1 * clock->p2;
ed5ca77e 565 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
dccbea3b 566 return 0;
fb03ac01
VS
567 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
568 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
569
570 return clock->dot;
79e53945
JB
571}
572
9e2c8475 573static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
589eca67
ID
574{
575 clock->m = clock->m1 * clock->m2;
576 clock->p = clock->p1 * clock->p2;
577 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 578 return 0;
589eca67
ID
579 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
580 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
581
582 return clock->dot / 5;
589eca67
ID
583}
584
9e2c8475 585int chv_calc_dpll_params(int refclk, struct dpll *clock)
ef9348c8
CML
586{
587 clock->m = clock->m1 * clock->m2;
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n == 0 || clock->p == 0))
dccbea3b 590 return 0;
ef9348c8
CML
591 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
592 clock->n << 22);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
dccbea3b
ID
594
595 return clock->dot / 5;
ef9348c8
CML
596}
597
7c04d1d9 598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
e2d214ae 604static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
1b6f4958 605 const struct intel_limit *limit,
9e2c8475 606 const struct dpll *clock)
79e53945 607{
f01b7962
VS
608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
79e53945 610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 611 INTELPllInvalid("p1 out of range\n");
79e53945 612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 613 INTELPllInvalid("m2 out of range\n");
79e53945 614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 615 INTELPllInvalid("m1 out of range\n");
f01b7962 616
e2d214ae 617 if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
cc3f90f0 618 !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
f01b7962
VS
619 if (clock->m1 <= clock->m2)
620 INTELPllInvalid("m1 <= m2\n");
621
e2d214ae 622 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
cc3f90f0 623 !IS_GEN9_LP(dev_priv)) {
f01b7962
VS
624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
79e53945 630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 631 INTELPllInvalid("vco out of range\n");
79e53945
JB
632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 636 INTELPllInvalid("dot out of range\n");
79e53945
JB
637
638 return true;
639}
640
3b1429d9 641static int
1b6f4958 642i9xx_select_p2_div(const struct intel_limit *limit,
3b1429d9
VS
643 const struct intel_crtc_state *crtc_state,
644 int target)
79e53945 645{
3b1429d9 646 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 647
2d84d2b3 648 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 649 /*
a210b028
SV
650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
79e53945 653 */
1974cad0 654 if (intel_is_dual_link_lvds(dev))
3b1429d9 655 return limit->p2.p2_fast;
79e53945 656 else
3b1429d9 657 return limit->p2.p2_slow;
79e53945
JB
658 } else {
659 if (target < limit->p2.dot_limit)
3b1429d9 660 return limit->p2.p2_slow;
79e53945 661 else
3b1429d9 662 return limit->p2.p2_fast;
79e53945 663 }
3b1429d9
VS
664}
665
70e8aa21
ACO
666/*
667 * Returns a set of divisors for the desired target clock with the given
668 * refclk, or FALSE. The returned values represent the clock equation:
669 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
670 *
671 * Target and reference clocks are specified in kHz.
672 *
673 * If match_clock is provided, then best_clock P divider must match the P
674 * divider from @match_clock used for LVDS downclocking.
675 */
3b1429d9 676static bool
1b6f4958 677i9xx_find_best_dpll(const struct intel_limit *limit,
3b1429d9 678 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
679 int target, int refclk, struct dpll *match_clock,
680 struct dpll *best_clock)
3b1429d9
VS
681{
682 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 683 struct dpll clock;
3b1429d9 684 int err = target;
79e53945 685
0206e353 686 memset(best_clock, 0, sizeof(*best_clock));
79e53945 687
3b1429d9
VS
688 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
689
42158660
ZY
690 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
691 clock.m1++) {
692 for (clock.m2 = limit->m2.min;
693 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 694 if (clock.m2 >= clock.m1)
42158660
ZY
695 break;
696 for (clock.n = limit->n.min;
697 clock.n <= limit->n.max; clock.n++) {
698 for (clock.p1 = limit->p1.min;
699 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
700 int this_err;
701
dccbea3b 702 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
703 if (!intel_PLL_is_valid(to_i915(dev),
704 limit,
ac58c3f0
SV
705 &clock))
706 continue;
707 if (match_clock &&
708 clock.p != match_clock->p)
709 continue;
710
711 this_err = abs(clock.dot - target);
712 if (this_err < err) {
713 *best_clock = clock;
714 err = this_err;
715 }
716 }
717 }
718 }
719 }
720
721 return (err != target);
722}
723
70e8aa21
ACO
724/*
725 * Returns a set of divisors for the desired target clock with the given
726 * refclk, or FALSE. The returned values represent the clock equation:
727 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
728 *
729 * Target and reference clocks are specified in kHz.
730 *
731 * If match_clock is provided, then best_clock P divider must match the P
732 * divider from @match_clock used for LVDS downclocking.
733 */
ac58c3f0 734static bool
1b6f4958 735pnv_find_best_dpll(const struct intel_limit *limit,
a93e255f 736 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
737 int target, int refclk, struct dpll *match_clock,
738 struct dpll *best_clock)
79e53945 739{
3b1429d9 740 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 741 struct dpll clock;
79e53945
JB
742 int err = target;
743
0206e353 744 memset(best_clock, 0, sizeof(*best_clock));
79e53945 745
3b1429d9
VS
746 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
747
42158660
ZY
748 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
749 clock.m1++) {
750 for (clock.m2 = limit->m2.min;
751 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
752 for (clock.n = limit->n.min;
753 clock.n <= limit->n.max; clock.n++) {
754 for (clock.p1 = limit->p1.min;
755 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
756 int this_err;
757
dccbea3b 758 pnv_calc_dpll_params(refclk, &clock);
e2d214ae
TU
759 if (!intel_PLL_is_valid(to_i915(dev),
760 limit,
1b894b59 761 &clock))
79e53945 762 continue;
cec2f356
SP
763 if (match_clock &&
764 clock.p != match_clock->p)
765 continue;
79e53945
JB
766
767 this_err = abs(clock.dot - target);
768 if (this_err < err) {
769 *best_clock = clock;
770 err = this_err;
771 }
772 }
773 }
774 }
775 }
776
777 return (err != target);
778}
779
997c030c
ACO
780/*
781 * Returns a set of divisors for the desired target clock with the given
782 * refclk, or FALSE. The returned values represent the clock equation:
783 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
70e8aa21
ACO
784 *
785 * Target and reference clocks are specified in kHz.
786 *
787 * If match_clock is provided, then best_clock P divider must match the P
788 * divider from @match_clock used for LVDS downclocking.
997c030c 789 */
d4906093 790static bool
1b6f4958 791g4x_find_best_dpll(const struct intel_limit *limit,
a93e255f 792 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
793 int target, int refclk, struct dpll *match_clock,
794 struct dpll *best_clock)
d4906093 795{
3b1429d9 796 struct drm_device *dev = crtc_state->base.crtc->dev;
9e2c8475 797 struct dpll clock;
d4906093 798 int max_n;
3b1429d9 799 bool found = false;
6ba770dc
AJ
800 /* approximately equals target * 0.00585 */
801 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
802
803 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
804
805 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
806
d4906093 807 max_n = limit->n.max;
f77f13e2 808 /* based on hardware requirement, prefer smaller n to precision */
d4906093 809 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 810 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
811 for (clock.m1 = limit->m1.max;
812 clock.m1 >= limit->m1.min; clock.m1--) {
813 for (clock.m2 = limit->m2.max;
814 clock.m2 >= limit->m2.min; clock.m2--) {
815 for (clock.p1 = limit->p1.max;
816 clock.p1 >= limit->p1.min; clock.p1--) {
817 int this_err;
818
dccbea3b 819 i9xx_calc_dpll_params(refclk, &clock);
e2d214ae
TU
820 if (!intel_PLL_is_valid(to_i915(dev),
821 limit,
1b894b59 822 &clock))
d4906093 823 continue;
1b894b59
CW
824
825 this_err = abs(clock.dot - target);
d4906093
ML
826 if (this_err < err_most) {
827 *best_clock = clock;
828 err_most = this_err;
829 max_n = clock.n;
830 found = true;
831 }
832 }
833 }
834 }
835 }
2c07245f
ZW
836 return found;
837}
838
d5dd62bd
ID
839/*
840 * Check if the calculated PLL configuration is more optimal compared to the
841 * best configuration and error found so far. Return the calculated error.
842 */
843static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
9e2c8475
ACO
844 const struct dpll *calculated_clock,
845 const struct dpll *best_clock,
d5dd62bd
ID
846 unsigned int best_error_ppm,
847 unsigned int *error_ppm)
848{
9ca3ba01
ID
849 /*
850 * For CHV ignore the error and consider only the P value.
851 * Prefer a bigger P value based on HW requirements.
852 */
920a14b2 853 if (IS_CHERRYVIEW(to_i915(dev))) {
9ca3ba01
ID
854 *error_ppm = 0;
855
856 return calculated_clock->p > best_clock->p;
857 }
858
24be4e46
ID
859 if (WARN_ON_ONCE(!target_freq))
860 return false;
861
d5dd62bd
ID
862 *error_ppm = div_u64(1000000ULL *
863 abs(target_freq - calculated_clock->dot),
864 target_freq);
865 /*
866 * Prefer a better P value over a better (smaller) error if the error
867 * is small. Ensure this preference for future configurations too by
868 * setting the error to 0.
869 */
870 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
871 *error_ppm = 0;
872
873 return true;
874 }
875
876 return *error_ppm + 10 < best_error_ppm;
877}
878
65b3d6a9
ACO
879/*
880 * Returns a set of divisors for the desired target clock with the given
881 * refclk, or FALSE. The returned values represent the clock equation:
882 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
883 */
a0c4da24 884static bool
1b6f4958 885vlv_find_best_dpll(const struct intel_limit *limit,
a93e255f 886 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
887 int target, int refclk, struct dpll *match_clock,
888 struct dpll *best_clock)
a0c4da24 889{
a93e255f 890 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 891 struct drm_device *dev = crtc->base.dev;
9e2c8475 892 struct dpll clock;
69e4f900 893 unsigned int bestppm = 1000000;
27e639bf
VS
894 /* min update 19.2 MHz */
895 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 896 bool found = false;
a0c4da24 897
6b4bf1c4
VS
898 target *= 5; /* fast clock */
899
900 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
901
902 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 903 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 904 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 905 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 906 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 907 clock.p = clock.p1 * clock.p2;
a0c4da24 908 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 909 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 910 unsigned int ppm;
69e4f900 911
6b4bf1c4
VS
912 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
913 refclk * clock.m1);
914
dccbea3b 915 vlv_calc_dpll_params(refclk, &clock);
43b0ac53 916
e2d214ae
TU
917 if (!intel_PLL_is_valid(to_i915(dev),
918 limit,
f01b7962 919 &clock))
43b0ac53
VS
920 continue;
921
d5dd62bd
ID
922 if (!vlv_PLL_is_optimal(dev, target,
923 &clock,
924 best_clock,
925 bestppm, &ppm))
926 continue;
6b4bf1c4 927
d5dd62bd
ID
928 *best_clock = clock;
929 bestppm = ppm;
930 found = true;
a0c4da24
JB
931 }
932 }
933 }
934 }
a0c4da24 935
49e497ef 936 return found;
a0c4da24 937}
a4fc5ed6 938
65b3d6a9
ACO
939/*
940 * Returns a set of divisors for the desired target clock with the given
941 * refclk, or FALSE. The returned values represent the clock equation:
942 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
943 */
ef9348c8 944static bool
1b6f4958 945chv_find_best_dpll(const struct intel_limit *limit,
a93e255f 946 struct intel_crtc_state *crtc_state,
9e2c8475
ACO
947 int target, int refclk, struct dpll *match_clock,
948 struct dpll *best_clock)
ef9348c8 949{
a93e255f 950 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 951 struct drm_device *dev = crtc->base.dev;
9ca3ba01 952 unsigned int best_error_ppm;
9e2c8475 953 struct dpll clock;
ef9348c8
CML
954 uint64_t m2;
955 int found = false;
956
957 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 958 best_error_ppm = 1000000;
ef9348c8
CML
959
960 /*
961 * Based on hardware doc, the n always set to 1, and m1 always
962 * set to 2. If requires to support 200Mhz refclk, we need to
963 * revisit this because n may not 1 anymore.
964 */
965 clock.n = 1, clock.m1 = 2;
966 target *= 5; /* fast clock */
967
968 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
969 for (clock.p2 = limit->p2.p2_fast;
970 clock.p2 >= limit->p2.p2_slow;
971 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 972 unsigned int error_ppm;
ef9348c8
CML
973
974 clock.p = clock.p1 * clock.p2;
975
976 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
977 clock.n) << 22, refclk * clock.m1);
978
979 if (m2 > INT_MAX/clock.m1)
980 continue;
981
982 clock.m2 = m2;
983
dccbea3b 984 chv_calc_dpll_params(refclk, &clock);
ef9348c8 985
e2d214ae 986 if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
ef9348c8
CML
987 continue;
988
9ca3ba01
ID
989 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
990 best_error_ppm, &error_ppm))
991 continue;
992
993 *best_clock = clock;
994 best_error_ppm = error_ppm;
995 found = true;
ef9348c8
CML
996 }
997 }
998
999 return found;
1000}
1001
5ab7b0b7 1002bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
9e2c8475 1003 struct dpll *best_clock)
5ab7b0b7 1004{
65b3d6a9 1005 int refclk = 100000;
1b6f4958 1006 const struct intel_limit *limit = &intel_limits_bxt;
5ab7b0b7 1007
65b3d6a9 1008 return chv_find_best_dpll(limit, crtc_state,
5ab7b0b7
ID
1009 target_clock, refclk, NULL, best_clock);
1010}
1011
525b9311 1012bool intel_crtc_active(struct intel_crtc *crtc)
20ddf665 1013{
20ddf665
VS
1014 /* Be paranoid as we can arrive here with only partial
1015 * state retrieved from the hardware during setup.
1016 *
241bfc38 1017 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
1018 * as Haswell has gained clock readout/fastboot support.
1019 *
66e514c1 1020 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1021 * properly reconstruct framebuffers.
c3d1f436
MR
1022 *
1023 * FIXME: The intel_crtc->active here should be switched to
1024 * crtc->state->active once we have proper CRTC states wired up
1025 * for atomic.
20ddf665 1026 */
525b9311
VS
1027 return crtc->active && crtc->base.primary->state->fb &&
1028 crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1029}
1030
a5c961d1
PZ
1031enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1032 enum pipe pipe)
1033{
98187836 1034 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
a5c961d1 1035
e2af48c6 1036 return crtc->config->cpu_transcoder;
a5c961d1
PZ
1037}
1038
6315b5d3 1039static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
fbf49ea2 1040{
f0f59a00 1041 i915_reg_t reg = PIPEDSL(pipe);
fbf49ea2
VS
1042 u32 line1, line2;
1043 u32 line_mask;
1044
5db94019 1045 if (IS_GEN2(dev_priv))
fbf49ea2
VS
1046 line_mask = DSL_LINEMASK_GEN2;
1047 else
1048 line_mask = DSL_LINEMASK_GEN3;
1049
1050 line1 = I915_READ(reg) & line_mask;
6adfb1ef 1051 msleep(5);
fbf49ea2
VS
1052 line2 = I915_READ(reg) & line_mask;
1053
1054 return line1 == line2;
1055}
1056
ab7ad7f6
KP
1057/*
1058 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1059 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1060 *
1061 * After disabling a pipe, we can't wait for vblank in the usual way,
1062 * spinning on the vblank interrupt status bit, since we won't actually
1063 * see an interrupt when the pipe is disabled.
1064 *
ab7ad7f6
KP
1065 * On Gen4 and above:
1066 * wait for the pipe register state bit to turn off
1067 *
1068 * Otherwise:
1069 * wait for the display line value to settle (it usually
1070 * ends up stopping at the start of the next frame).
58e10eb9 1071 *
9d0498a2 1072 */
575f7ab7 1073static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1074{
6315b5d3 1075 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 1076 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1077 enum pipe pipe = crtc->pipe;
ab7ad7f6 1078
6315b5d3 1079 if (INTEL_GEN(dev_priv) >= 4) {
f0f59a00 1080 i915_reg_t reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1081
1082 /* Wait for the Pipe State to go off */
b8511f53
CW
1083 if (intel_wait_for_register(dev_priv,
1084 reg, I965_PIPECONF_ACTIVE, 0,
1085 100))
284637d9 1086 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1087 } else {
ab7ad7f6 1088 /* Wait for the display line to settle */
6315b5d3 1089 if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
284637d9 1090 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1091 }
79e53945
JB
1092}
1093
b24e7179 1094/* Only for pre-ILK configs */
55607e8a
SV
1095void assert_pll(struct drm_i915_private *dev_priv,
1096 enum pipe pipe, bool state)
b24e7179 1097{
b24e7179
JB
1098 u32 val;
1099 bool cur_state;
1100
649636ef 1101 val = I915_READ(DPLL(pipe));
b24e7179 1102 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1103 I915_STATE_WARN(cur_state != state,
b24e7179 1104 "PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1105 onoff(state), onoff(cur_state));
b24e7179 1106}
b24e7179 1107
23538ef1 1108/* XXX: the dsi pll is shared between MIPI DSI ports */
8563b1e8 1109void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
23538ef1
JN
1110{
1111 u32 val;
1112 bool cur_state;
1113
a580516d 1114 mutex_lock(&dev_priv->sb_lock);
23538ef1 1115 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1116 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1117
1118 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1119 I915_STATE_WARN(cur_state != state,
23538ef1 1120 "DSI PLL state assertion failure (expected %s, current %s)\n",
87ad3212 1121 onoff(state), onoff(cur_state));
23538ef1 1122}
23538ef1 1123
040484af
JB
1124static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
1126{
040484af 1127 bool cur_state;
ad80a810
PZ
1128 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1129 pipe);
040484af 1130
2d1fe073 1131 if (HAS_DDI(dev_priv)) {
affa9354 1132 /* DDI does not have a specific FDI_TX register */
649636ef 1133 u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
ad80a810 1134 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7 1135 } else {
649636ef 1136 u32 val = I915_READ(FDI_TX_CTL(pipe));
bf507ef7
ED
1137 cur_state = !!(val & FDI_TX_ENABLE);
1138 }
e2c719b7 1139 I915_STATE_WARN(cur_state != state,
040484af 1140 "FDI TX state assertion failure (expected %s, current %s)\n",
87ad3212 1141 onoff(state), onoff(cur_state));
040484af
JB
1142}
1143#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1144#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1145
1146static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1147 enum pipe pipe, bool state)
1148{
040484af
JB
1149 u32 val;
1150 bool cur_state;
1151
649636ef 1152 val = I915_READ(FDI_RX_CTL(pipe));
d63fa0dc 1153 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1154 I915_STATE_WARN(cur_state != state,
040484af 1155 "FDI RX state assertion failure (expected %s, current %s)\n",
87ad3212 1156 onoff(state), onoff(cur_state));
040484af
JB
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
040484af
JB
1164 u32 val;
1165
1166 /* ILK FDI PLL is always enabled */
7e22dbbb 1167 if (IS_GEN5(dev_priv))
040484af
JB
1168 return;
1169
bf507ef7 1170 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
2d1fe073 1171 if (HAS_DDI(dev_priv))
bf507ef7
ED
1172 return;
1173
649636ef 1174 val = I915_READ(FDI_TX_CTL(pipe));
e2c719b7 1175 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1176}
1177
55607e8a
SV
1178void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1179 enum pipe pipe, bool state)
040484af 1180{
040484af 1181 u32 val;
55607e8a 1182 bool cur_state;
040484af 1183
649636ef 1184 val = I915_READ(FDI_RX_CTL(pipe));
55607e8a 1185 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1186 I915_STATE_WARN(cur_state != state,
55607e8a 1187 "FDI RX PLL assertion failure (expected %s, current %s)\n",
87ad3212 1188 onoff(state), onoff(cur_state));
040484af
JB
1189}
1190
4f8036a2 1191void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
ea0760cf 1192{
f0f59a00 1193 i915_reg_t pp_reg;
ea0760cf
JB
1194 u32 val;
1195 enum pipe panel_pipe = PIPE_A;
0de3b485 1196 bool locked = true;
ea0760cf 1197
4f8036a2 1198 if (WARN_ON(HAS_DDI(dev_priv)))
bedd4dba
JN
1199 return;
1200
4f8036a2 1201 if (HAS_PCH_SPLIT(dev_priv)) {
bedd4dba
JN
1202 u32 port_sel;
1203
44cb734c
ID
1204 pp_reg = PP_CONTROL(0);
1205 port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
bedd4dba
JN
1206
1207 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1208 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1209 panel_pipe = PIPE_B;
1210 /* XXX: else fix for eDP */
4f8036a2 1211 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
bedd4dba 1212 /* presumably write lock depends on pipe, not port select */
44cb734c 1213 pp_reg = PP_CONTROL(pipe);
bedd4dba 1214 panel_pipe = pipe;
ea0760cf 1215 } else {
44cb734c 1216 pp_reg = PP_CONTROL(0);
bedd4dba
JN
1217 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1218 panel_pipe = PIPE_B;
ea0760cf
JB
1219 }
1220
1221 val = I915_READ(pp_reg);
1222 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1223 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1224 locked = false;
1225
e2c719b7 1226 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1227 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1228 pipe_name(pipe));
ea0760cf
JB
1229}
1230
93ce0ba6
JN
1231static void assert_cursor(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
1233{
93ce0ba6
JN
1234 bool cur_state;
1235
50a0bc90 1236 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
0b87c24e 1237 cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
d9d82081 1238 else
5efb3e28 1239 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1240
e2c719b7 1241 I915_STATE_WARN(cur_state != state,
93ce0ba6 1242 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1243 pipe_name(pipe), onoff(state), onoff(cur_state));
93ce0ba6
JN
1244}
1245#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1246#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1247
b840d907
JB
1248void assert_pipe(struct drm_i915_private *dev_priv,
1249 enum pipe pipe, bool state)
b24e7179 1250{
63d7bbe9 1251 bool cur_state;
702e7a56
PZ
1252 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1253 pipe);
4feed0eb 1254 enum intel_display_power_domain power_domain;
b24e7179 1255
b6b5d049
VS
1256 /* if we need the pipe quirk it must be always on */
1257 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1258 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
SV
1259 state = true;
1260
4feed0eb
ID
1261 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
1262 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
649636ef 1263 u32 val = I915_READ(PIPECONF(cpu_transcoder));
69310161 1264 cur_state = !!(val & PIPECONF_ENABLE);
4feed0eb
ID
1265
1266 intel_display_power_put(dev_priv, power_domain);
1267 } else {
1268 cur_state = false;
69310161
PZ
1269 }
1270
e2c719b7 1271 I915_STATE_WARN(cur_state != state,
63d7bbe9 1272 "pipe %c assertion failure (expected %s, current %s)\n",
87ad3212 1273 pipe_name(pipe), onoff(state), onoff(cur_state));
b24e7179
JB
1274}
1275
931872fc
CW
1276static void assert_plane(struct drm_i915_private *dev_priv,
1277 enum plane plane, bool state)
b24e7179 1278{
b24e7179 1279 u32 val;
931872fc 1280 bool cur_state;
b24e7179 1281
649636ef 1282 val = I915_READ(DSPCNTR(plane));
931872fc 1283 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1284 I915_STATE_WARN(cur_state != state,
931872fc 1285 "plane %c assertion failure (expected %s, current %s)\n",
87ad3212 1286 plane_name(plane), onoff(state), onoff(cur_state));
b24e7179
JB
1287}
1288
931872fc
CW
1289#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1290#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1291
b24e7179
JB
1292static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1293 enum pipe pipe)
1294{
649636ef 1295 int i;
b24e7179 1296
653e1026 1297 /* Primary planes are fixed to pipes on gen4+ */
6315b5d3 1298 if (INTEL_GEN(dev_priv) >= 4) {
649636ef 1299 u32 val = I915_READ(DSPCNTR(pipe));
e2c719b7 1300 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1301 "plane %c assertion failure, should be disabled but not\n",
1302 plane_name(pipe));
19ec1358 1303 return;
28c05794 1304 }
19ec1358 1305
b24e7179 1306 /* Need to check both planes against the pipe */
055e393f 1307 for_each_pipe(dev_priv, i) {
649636ef
VS
1308 u32 val = I915_READ(DSPCNTR(i));
1309 enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
b24e7179 1310 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1311 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1312 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1313 plane_name(i), pipe_name(pipe));
b24e7179
JB
1314 }
1315}
1316
19332d7a
JB
1317static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe)
1319{
649636ef 1320 int sprite;
19332d7a 1321
6315b5d3 1322 if (INTEL_GEN(dev_priv) >= 9) {
3bdcfc0c 1323 for_each_sprite(dev_priv, pipe, sprite) {
649636ef 1324 u32 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1325 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1326 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1327 sprite, pipe_name(pipe));
1328 }
920a14b2 1329 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3bdcfc0c 1330 for_each_sprite(dev_priv, pipe, sprite) {
83c04a62 1331 u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
e2c719b7 1332 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1333 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1334 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef 1335 }
6315b5d3 1336 } else if (INTEL_GEN(dev_priv) >= 7) {
649636ef 1337 u32 val = I915_READ(SPRCTL(pipe));
e2c719b7 1338 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1339 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1340 plane_name(pipe), pipe_name(pipe));
6315b5d3 1341 } else if (INTEL_GEN(dev_priv) >= 5) {
649636ef 1342 u32 val = I915_READ(DVSCNTR(pipe));
e2c719b7 1343 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1344 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1345 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1346 }
1347}
1348
08c71e5e
VS
1349static void assert_vblank_disabled(struct drm_crtc *crtc)
1350{
e2c719b7 1351 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1352 drm_crtc_vblank_put(crtc);
1353}
1354
7abd4b35
ACO
1355void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1356 enum pipe pipe)
92f2584a 1357{
92f2584a
JB
1358 u32 val;
1359 bool enabled;
1360
649636ef 1361 val = I915_READ(PCH_TRANSCONF(pipe));
92f2584a 1362 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1363 I915_STATE_WARN(enabled,
9db4a9c7
JB
1364 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1365 pipe_name(pipe));
92f2584a
JB
1366}
1367
4e634389
KP
1368static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1370{
1371 if ((val & DP_PORT_EN) == 0)
1372 return false;
1373
2d1fe073 1374 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00 1375 u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
f0575e92
KP
1376 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1377 return false;
2d1fe073 1378 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1379 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1380 return false;
f0575e92
KP
1381 } else {
1382 if ((val & DP_PIPE_MASK) != (pipe << 30))
1383 return false;
1384 }
1385 return true;
1386}
1387
1519b995
KP
1388static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1389 enum pipe pipe, u32 val)
1390{
dc0fa718 1391 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1392 return false;
1393
2d1fe073 1394 if (HAS_PCH_CPT(dev_priv)) {
dc0fa718 1395 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1396 return false;
2d1fe073 1397 } else if (IS_CHERRYVIEW(dev_priv)) {
44f37d1f
CML
1398 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1399 return false;
1519b995 1400 } else {
dc0fa718 1401 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1402 return false;
1403 }
1404 return true;
1405}
1406
1407static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1408 enum pipe pipe, u32 val)
1409{
1410 if ((val & LVDS_PORT_EN) == 0)
1411 return false;
1412
2d1fe073 1413 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1414 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1415 return false;
1416 } else {
1417 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1418 return false;
1419 }
1420 return true;
1421}
1422
1423static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1424 enum pipe pipe, u32 val)
1425{
1426 if ((val & ADPA_DAC_ENABLE) == 0)
1427 return false;
2d1fe073 1428 if (HAS_PCH_CPT(dev_priv)) {
1519b995
KP
1429 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1430 return false;
1431 } else {
1432 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1433 return false;
1434 }
1435 return true;
1436}
1437
291906f1 1438static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0f59a00
VS
1439 enum pipe pipe, i915_reg_t reg,
1440 u32 port_sel)
291906f1 1441{
47a05eca 1442 u32 val = I915_READ(reg);
e2c719b7 1443 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1444 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1445 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1446
2d1fe073 1447 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
75c5da27 1448 && (val & DP_PIPEB_SELECT),
de9a35ab 1449 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1450}
1451
1452static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
f0f59a00 1453 enum pipe pipe, i915_reg_t reg)
291906f1 1454{
47a05eca 1455 u32 val = I915_READ(reg);
e2c719b7 1456 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1457 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
f0f59a00 1458 i915_mmio_reg_offset(reg), pipe_name(pipe));
de9a35ab 1459
2d1fe073 1460 I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
75c5da27 1461 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1462 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1463}
1464
1465static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
1467{
291906f1 1468 u32 val;
291906f1 1469
f0575e92
KP
1470 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1 1473
649636ef 1474 val = I915_READ(PCH_ADPA);
e2c719b7 1475 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1476 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1477 pipe_name(pipe));
291906f1 1478
649636ef 1479 val = I915_READ(PCH_LVDS);
e2c719b7 1480 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1481 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1482 pipe_name(pipe));
291906f1 1483
e2debe91
PZ
1484 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1485 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1486 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1487}
1488
cd2d34d9
VS
1489static void _vlv_enable_pll(struct intel_crtc *crtc,
1490 const struct intel_crtc_state *pipe_config)
1491{
1492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1493 enum pipe pipe = crtc->pipe;
1494
1495 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1496 POSTING_READ(DPLL(pipe));
1497 udelay(150);
1498
2c30b43b
CW
1499 if (intel_wait_for_register(dev_priv,
1500 DPLL(pipe),
1501 DPLL_LOCK_VLV,
1502 DPLL_LOCK_VLV,
1503 1))
cd2d34d9
VS
1504 DRM_ERROR("DPLL %d failed to lock\n", pipe);
1505}
1506
d288f65f 1507static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1508 const struct intel_crtc_state *pipe_config)
87442f73 1509{
cd2d34d9 1510 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1511 enum pipe pipe = crtc->pipe;
87442f73 1512
8bd3f301 1513 assert_pipe_disabled(dev_priv, pipe);
87442f73 1514
87442f73 1515 /* PLL is protected by panel, make sure we can write it */
7d1a83cb 1516 assert_panel_unlocked(dev_priv, pipe);
87442f73 1517
cd2d34d9
VS
1518 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1519 _vlv_enable_pll(crtc, pipe_config);
426115cf 1520
8bd3f301
VS
1521 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1522 POSTING_READ(DPLL_MD(pipe));
87442f73
SV
1523}
1524
cd2d34d9
VS
1525
1526static void _chv_enable_pll(struct intel_crtc *crtc,
1527 const struct intel_crtc_state *pipe_config)
9d556c99 1528{
cd2d34d9 1529 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8bd3f301 1530 enum pipe pipe = crtc->pipe;
9d556c99 1531 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1532 u32 tmp;
1533
a580516d 1534 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1535
1536 /* Enable back the 10bit clock to display controller */
1537 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1538 tmp |= DPIO_DCLKP_EN;
1539 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1540
54433e91
VS
1541 mutex_unlock(&dev_priv->sb_lock);
1542
9d556c99
CML
1543 /*
1544 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1545 */
1546 udelay(1);
1547
1548 /* Enable PLL */
d288f65f 1549 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1550
1551 /* Check PLL is locked */
6b18826a
CW
1552 if (intel_wait_for_register(dev_priv,
1553 DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
1554 1))
9d556c99 1555 DRM_ERROR("PLL %d failed to lock\n", pipe);
cd2d34d9
VS
1556}
1557
1558static void chv_enable_pll(struct intel_crtc *crtc,
1559 const struct intel_crtc_state *pipe_config)
1560{
1561 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1562 enum pipe pipe = crtc->pipe;
1563
1564 assert_pipe_disabled(dev_priv, pipe);
1565
1566 /* PLL is protected by panel, make sure we can write it */
1567 assert_panel_unlocked(dev_priv, pipe);
1568
1569 if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
1570 _chv_enable_pll(crtc, pipe_config);
9d556c99 1571
c231775c
VS
1572 if (pipe != PIPE_A) {
1573 /*
1574 * WaPixelRepeatModeFixForC0:chv
1575 *
1576 * DPLLCMD is AWOL. Use chicken bits to propagate
1577 * the value from DPLLBMD to either pipe B or C.
1578 */
1579 I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
1580 I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
1581 I915_WRITE(CBR4_VLV, 0);
1582 dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
1583
1584 /*
1585 * DPLLB VGA mode also seems to cause problems.
1586 * We should always have it disabled.
1587 */
1588 WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
1589 } else {
1590 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1591 POSTING_READ(DPLL_MD(pipe));
1592 }
9d556c99
CML
1593}
1594
6315b5d3 1595static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
1c4e0274
VS
1596{
1597 struct intel_crtc *crtc;
1598 int count = 0;
1599
6315b5d3 1600 for_each_intel_crtc(&dev_priv->drm, crtc) {
3538b9df 1601 count += crtc->base.state->active &&
2d84d2b3
VS
1602 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
1603 }
1c4e0274
VS
1604
1605 return count;
1606}
1607
66e3d5c0 1608static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1609{
6315b5d3 1610 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
f0f59a00 1611 i915_reg_t reg = DPLL(crtc->pipe);
6e3c9717 1612 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1613
66e3d5c0 1614 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1615
63d7bbe9 1616 /* PLL is protected by panel, make sure we can write it */
50a0bc90 1617 if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
66e3d5c0 1618 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1619
1c4e0274 1620 /* Enable DVO 2x clock on both PLLs if necessary */
6315b5d3 1621 if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
1c4e0274
VS
1622 /*
1623 * It appears to be important that we don't enable this
1624 * for the current pipe before otherwise configuring the
1625 * PLL. No idea how this should be handled if multiple
1626 * DVO outputs are enabled simultaneosly.
1627 */
1628 dpll |= DPLL_DVO_2X_MODE;
1629 I915_WRITE(DPLL(!crtc->pipe),
1630 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1631 }
66e3d5c0 1632
c2b63374
VS
1633 /*
1634 * Apparently we need to have VGA mode enabled prior to changing
1635 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1636 * dividers, even though the register value does change.
1637 */
1638 I915_WRITE(reg, 0);
1639
8e7a65aa
VS
1640 I915_WRITE(reg, dpll);
1641
66e3d5c0
SV
1642 /* Wait for the clocks to stabilize. */
1643 POSTING_READ(reg);
1644 udelay(150);
1645
6315b5d3 1646 if (INTEL_GEN(dev_priv) >= 4) {
66e3d5c0 1647 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1648 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
SV
1649 } else {
1650 /* The pixel multiplier can only be updated once the
1651 * DPLL is enabled and the clocks are stable.
1652 *
1653 * So write it again.
1654 */
1655 I915_WRITE(reg, dpll);
1656 }
63d7bbe9
JB
1657
1658 /* We do this three times for luck */
66e3d5c0 1659 I915_WRITE(reg, dpll);
63d7bbe9
JB
1660 POSTING_READ(reg);
1661 udelay(150); /* wait for warmup */
66e3d5c0 1662 I915_WRITE(reg, dpll);
63d7bbe9
JB
1663 POSTING_READ(reg);
1664 udelay(150); /* wait for warmup */
66e3d5c0 1665 I915_WRITE(reg, dpll);
63d7bbe9
JB
1666 POSTING_READ(reg);
1667 udelay(150); /* wait for warmup */
1668}
1669
1670/**
50b44a44 1671 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1672 * @dev_priv: i915 private structure
1673 * @pipe: pipe PLL to disable
1674 *
1675 * Disable the PLL for @pipe, making sure the pipe is off first.
1676 *
1677 * Note! This is for pre-ILK only.
1678 */
1c4e0274 1679static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1680{
6315b5d3 1681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1c4e0274
VS
1682 enum pipe pipe = crtc->pipe;
1683
1684 /* Disable DVO 2x clock on both PLLs if necessary */
50a0bc90 1685 if (IS_I830(dev_priv) &&
2d84d2b3 1686 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
6315b5d3 1687 !intel_num_dvo_pipes(dev_priv)) {
1c4e0274
VS
1688 I915_WRITE(DPLL(PIPE_B),
1689 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1690 I915_WRITE(DPLL(PIPE_A),
1691 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1692 }
1693
b6b5d049
VS
1694 /* Don't disable pipe or pipe PLLs if needed */
1695 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1696 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1697 return;
1698
1699 /* Make sure the pipe isn't still relying on us */
1700 assert_pipe_disabled(dev_priv, pipe);
1701
b8afb911 1702 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
50b44a44 1703 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1704}
1705
f6071166
JB
1706static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1707{
b8afb911 1708 u32 val;
f6071166
JB
1709
1710 /* Make sure the pipe isn't still relying on us */
1711 assert_pipe_disabled(dev_priv, pipe);
1712
03ed5cbf
VS
1713 val = DPLL_INTEGRATED_REF_CLK_VLV |
1714 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1715 if (pipe != PIPE_A)
1716 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1717
f6071166
JB
1718 I915_WRITE(DPLL(pipe), val);
1719 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1720}
1721
1722static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1723{
d752048d 1724 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1725 u32 val;
1726
a11b0703
VS
1727 /* Make sure the pipe isn't still relying on us */
1728 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1729
60bfe44f
VS
1730 val = DPLL_SSC_REF_CLK_CHV |
1731 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
a11b0703
VS
1732 if (pipe != PIPE_A)
1733 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
03ed5cbf 1734
a11b0703
VS
1735 I915_WRITE(DPLL(pipe), val);
1736 POSTING_READ(DPLL(pipe));
d752048d 1737
a580516d 1738 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1739
1740 /* Disable 10bit clock to display controller */
1741 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1742 val &= ~DPIO_DCLKP_EN;
1743 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1744
a580516d 1745 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1746}
1747
e4607fcf 1748void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1749 struct intel_digital_port *dport,
1750 unsigned int expected_mask)
89b667f8
JB
1751{
1752 u32 port_mask;
f0f59a00 1753 i915_reg_t dpll_reg;
89b667f8 1754
e4607fcf
CML
1755 switch (dport->port) {
1756 case PORT_B:
89b667f8 1757 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1758 dpll_reg = DPLL(0);
e4607fcf
CML
1759 break;
1760 case PORT_C:
89b667f8 1761 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1762 dpll_reg = DPLL(0);
9b6de0a1 1763 expected_mask <<= 4;
00fc31b7
CML
1764 break;
1765 case PORT_D:
1766 port_mask = DPLL_PORTD_READY_MASK;
1767 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1768 break;
1769 default:
1770 BUG();
1771 }
89b667f8 1772
370004d3
CW
1773 if (intel_wait_for_register(dev_priv,
1774 dpll_reg, port_mask, expected_mask,
1775 1000))
9b6de0a1
VS
1776 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1777 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1778}
1779
b8a4f404
PZ
1780static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1781 enum pipe pipe)
040484af 1782{
98187836
VS
1783 struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
1784 pipe);
f0f59a00
VS
1785 i915_reg_t reg;
1786 uint32_t val, pipeconf_val;
040484af 1787
040484af 1788 /* Make sure PCH DPLL is enabled */
8106ddbd 1789 assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
040484af
JB
1790
1791 /* FDI must be feeding us bits for PCH ports */
1792 assert_fdi_tx_enabled(dev_priv, pipe);
1793 assert_fdi_rx_enabled(dev_priv, pipe);
1794
6e266956 1795 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1796 /* Workaround: Set the timing override bit before enabling the
1797 * pch transcoder. */
1798 reg = TRANS_CHICKEN2(pipe);
1799 val = I915_READ(reg);
1800 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1801 I915_WRITE(reg, val);
59c859d6 1802 }
23670b32 1803
ab9412ba 1804 reg = PCH_TRANSCONF(pipe);
040484af 1805 val = I915_READ(reg);
5f7f726d 1806 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c 1807
2d1fe073 1808 if (HAS_PCH_IBX(dev_priv)) {
e9bcff5c 1809 /*
c5de7c6f
VS
1810 * Make the BPC in transcoder be consistent with
1811 * that in pipeconf reg. For HDMI we must use 8bpc
1812 * here for both 8bpc and 12bpc.
e9bcff5c 1813 */
dfd07d72 1814 val &= ~PIPECONF_BPC_MASK;
2d84d2b3 1815 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
c5de7c6f
VS
1816 val |= PIPECONF_8BPC;
1817 else
1818 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1819 }
5f7f726d
PZ
1820
1821 val &= ~TRANS_INTERLACE_MASK;
1822 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2d1fe073 1823 if (HAS_PCH_IBX(dev_priv) &&
2d84d2b3 1824 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
1825 val |= TRANS_LEGACY_INTERLACED_ILK;
1826 else
1827 val |= TRANS_INTERLACED;
5f7f726d
PZ
1828 else
1829 val |= TRANS_PROGRESSIVE;
1830
040484af 1831 I915_WRITE(reg, val | TRANS_ENABLE);
650fbd84
CW
1832 if (intel_wait_for_register(dev_priv,
1833 reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
1834 100))
4bb6f1f3 1835 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1836}
1837
8fb033d7 1838static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1839 enum transcoder cpu_transcoder)
040484af 1840{
8fb033d7 1841 u32 val, pipeconf_val;
8fb033d7 1842
8fb033d7 1843 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1844 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1845 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1846
223a6fdf 1847 /* Workaround: set timing override bit. */
36c0d0cf 1848 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1849 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1850 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
223a6fdf 1851
25f3ef11 1852 val = TRANS_ENABLE;
937bb610 1853 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1854
9a76b1c6
PZ
1855 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1856 PIPECONF_INTERLACED_ILK)
a35f2679 1857 val |= TRANS_INTERLACED;
8fb033d7
PZ
1858 else
1859 val |= TRANS_PROGRESSIVE;
1860
ab9412ba 1861 I915_WRITE(LPT_TRANSCONF, val);
d9f96244
CW
1862 if (intel_wait_for_register(dev_priv,
1863 LPT_TRANSCONF,
1864 TRANS_STATE_ENABLE,
1865 TRANS_STATE_ENABLE,
1866 100))
937bb610 1867 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1868}
1869
b8a4f404
PZ
1870static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1871 enum pipe pipe)
040484af 1872{
f0f59a00
VS
1873 i915_reg_t reg;
1874 uint32_t val;
040484af
JB
1875
1876 /* FDI relies on the transcoder */
1877 assert_fdi_tx_disabled(dev_priv, pipe);
1878 assert_fdi_rx_disabled(dev_priv, pipe);
1879
291906f1
JB
1880 /* Ports must be off as well */
1881 assert_pch_ports_disabled(dev_priv, pipe);
1882
ab9412ba 1883 reg = PCH_TRANSCONF(pipe);
040484af
JB
1884 val = I915_READ(reg);
1885 val &= ~TRANS_ENABLE;
1886 I915_WRITE(reg, val);
1887 /* wait for PCH transcoder off, transcoder state */
a7d04662
CW
1888 if (intel_wait_for_register(dev_priv,
1889 reg, TRANS_STATE_ENABLE, 0,
1890 50))
4bb6f1f3 1891 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32 1892
6e266956 1893 if (HAS_PCH_CPT(dev_priv)) {
23670b32
SV
1894 /* Workaround: Clear the timing override chicken bit again. */
1895 reg = TRANS_CHICKEN2(pipe);
1896 val = I915_READ(reg);
1897 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1898 I915_WRITE(reg, val);
1899 }
040484af
JB
1900}
1901
b7076546 1902void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1903{
8fb033d7
PZ
1904 u32 val;
1905
ab9412ba 1906 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1907 val &= ~TRANS_ENABLE;
ab9412ba 1908 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1909 /* wait for PCH transcoder off, transcoder state */
dfdb4749
CW
1910 if (intel_wait_for_register(dev_priv,
1911 LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
1912 50))
8a52fd9f 1913 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1914
1915 /* Workaround: clear timing override bit. */
36c0d0cf 1916 val = I915_READ(TRANS_CHICKEN2(PIPE_A));
23670b32 1917 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
36c0d0cf 1918 I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
040484af
JB
1919}
1920
65f2130c
VS
1921enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
1922{
1923 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1924
1925 WARN_ON(!crtc->config->has_pch_encoder);
1926
1927 if (HAS_PCH_LPT(dev_priv))
1928 return TRANSCODER_A;
1929 else
1930 return (enum transcoder) crtc->pipe;
1931}
1932
b24e7179 1933/**
309cfea8 1934 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1935 * @crtc: crtc responsible for the pipe
b24e7179 1936 *
0372264a 1937 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1938 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1939 */
e1fdc473 1940static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1941{
0372264a 1942 struct drm_device *dev = crtc->base.dev;
fac5e23e 1943 struct drm_i915_private *dev_priv = to_i915(dev);
0372264a 1944 enum pipe pipe = crtc->pipe;
1a70a728 1945 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
f0f59a00 1946 i915_reg_t reg;
b24e7179
JB
1947 u32 val;
1948
9e2ee2dd
VS
1949 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
1950
58c6eaa2 1951 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1952 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
SV
1953 assert_sprites_disabled(dev_priv, pipe);
1954
b24e7179
JB
1955 /*
1956 * A pipe without a PLL won't actually be able to drive bits from
1957 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1958 * need the check.
1959 */
09fa8bb9 1960 if (HAS_GMCH_DISPLAY(dev_priv)) {
d7edc4e5 1961 if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
23538ef1
JN
1962 assert_dsi_pll_enabled(dev_priv);
1963 else
1964 assert_pll_enabled(dev_priv, pipe);
09fa8bb9 1965 } else {
6e3c9717 1966 if (crtc->config->has_pch_encoder) {
040484af 1967 /* if driving the PCH, we need FDI enabled */
65f2130c
VS
1968 assert_fdi_rx_pll_enabled(dev_priv,
1969 (enum pipe) intel_crtc_pch_transcoder(crtc));
1a240d4d
SV
1970 assert_fdi_tx_pll_enabled(dev_priv,
1971 (enum pipe) cpu_transcoder);
040484af
JB
1972 }
1973 /* FIXME: assert CPU port conditions for SNB+ */
1974 }
b24e7179 1975
702e7a56 1976 reg = PIPECONF(cpu_transcoder);
b24e7179 1977 val = I915_READ(reg);
7ad25d48 1978 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
1979 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1980 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 1981 return;
7ad25d48 1982 }
00d70b15
CW
1983
1984 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1985 POSTING_READ(reg);
b7792d8b
VS
1986
1987 /*
1988 * Until the pipe starts DSL will read as 0, which would cause
1989 * an apparent vblank timestamp jump, which messes up also the
1990 * frame count when it's derived from the timestamps. So let's
1991 * wait for the pipe to start properly before we call
1992 * drm_crtc_vblank_on()
1993 */
1994 if (dev->max_vblank_count == 0 &&
1995 wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
1996 DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
b24e7179
JB
1997}
1998
1999/**
309cfea8 2000 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2001 * @crtc: crtc whose pipes is to be disabled
b24e7179 2002 *
575f7ab7
VS
2003 * Disable the pipe of @crtc, making sure that various hardware
2004 * specific requirements are met, if applicable, e.g. plane
2005 * disabled, panel fitter off, etc.
b24e7179
JB
2006 *
2007 * Will wait until the pipe has shut down before returning.
2008 */
575f7ab7 2009static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2010{
fac5e23e 2011 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6e3c9717 2012 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2013 enum pipe pipe = crtc->pipe;
f0f59a00 2014 i915_reg_t reg;
b24e7179
JB
2015 u32 val;
2016
9e2ee2dd
VS
2017 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2018
b24e7179
JB
2019 /*
2020 * Make sure planes won't keep trying to pump pixels to us,
2021 * or we might hang the display.
2022 */
2023 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2024 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2025 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2026
702e7a56 2027 reg = PIPECONF(cpu_transcoder);
b24e7179 2028 val = I915_READ(reg);
00d70b15
CW
2029 if ((val & PIPECONF_ENABLE) == 0)
2030 return;
2031
67adc644
VS
2032 /*
2033 * Double wide has implications for planes
2034 * so best keep it disabled when not needed.
2035 */
6e3c9717 2036 if (crtc->config->double_wide)
67adc644
VS
2037 val &= ~PIPECONF_DOUBLE_WIDE;
2038
2039 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2040 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2041 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2042 val &= ~PIPECONF_ENABLE;
2043
2044 I915_WRITE(reg, val);
2045 if ((val & PIPECONF_ENABLE) == 0)
2046 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2047}
2048
832be82f
VS
2049static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
2050{
2051 return IS_GEN2(dev_priv) ? 2048 : 4096;
2052}
2053
27ba3910
VS
2054static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
2055 uint64_t fb_modifier, unsigned int cpp)
7b49f948
VS
2056{
2057 switch (fb_modifier) {
2058 case DRM_FORMAT_MOD_NONE:
2059 return cpp;
2060 case I915_FORMAT_MOD_X_TILED:
2061 if (IS_GEN2(dev_priv))
2062 return 128;
2063 else
2064 return 512;
2065 case I915_FORMAT_MOD_Y_TILED:
2066 if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
2067 return 128;
2068 else
2069 return 512;
2070 case I915_FORMAT_MOD_Yf_TILED:
2071 switch (cpp) {
2072 case 1:
2073 return 64;
2074 case 2:
2075 case 4:
2076 return 128;
2077 case 8:
2078 case 16:
2079 return 256;
2080 default:
2081 MISSING_CASE(cpp);
2082 return cpp;
2083 }
2084 break;
2085 default:
2086 MISSING_CASE(fb_modifier);
2087 return cpp;
2088 }
2089}
2090
832be82f
VS
2091unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
2092 uint64_t fb_modifier, unsigned int cpp)
a57ce0b2 2093{
832be82f
VS
2094 if (fb_modifier == DRM_FORMAT_MOD_NONE)
2095 return 1;
2096 else
2097 return intel_tile_size(dev_priv) /
27ba3910 2098 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
6761dd31
TU
2099}
2100
8d0deca8
VS
2101/* Return the tile dimensions in pixel units */
2102static void intel_tile_dims(const struct drm_i915_private *dev_priv,
2103 unsigned int *tile_width,
2104 unsigned int *tile_height,
2105 uint64_t fb_modifier,
2106 unsigned int cpp)
2107{
2108 unsigned int tile_width_bytes =
2109 intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
2110
2111 *tile_width = tile_width_bytes / cpp;
2112 *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
2113}
2114
6761dd31
TU
2115unsigned int
2116intel_fb_align_height(struct drm_device *dev, unsigned int height,
832be82f 2117 uint32_t pixel_format, uint64_t fb_modifier)
6761dd31 2118{
832be82f
VS
2119 unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
2120 unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
2121
2122 return ALIGN(height, tile_height);
a57ce0b2
JB
2123}
2124
1663b9d6
VS
2125unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
2126{
2127 unsigned int size = 0;
2128 int i;
2129
2130 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
2131 size += rot_info->plane[i].width * rot_info->plane[i].height;
2132
2133 return size;
2134}
2135
75c82a53 2136static void
3465c580
VS
2137intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
2138 const struct drm_framebuffer *fb,
2139 unsigned int rotation)
f64b98cd 2140{
bd2ef25d 2141 if (drm_rotation_90_or_270(rotation)) {
2d7a215f
VS
2142 *view = i915_ggtt_view_rotated;
2143 view->params.rotated = to_intel_framebuffer(fb)->rot_info;
2144 } else {
2145 *view = i915_ggtt_view_normal;
2146 }
2147}
50470bb0 2148
603525d7 2149static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
4e9a86b6
VS
2150{
2151 if (INTEL_INFO(dev_priv)->gen >= 9)
2152 return 256 * 1024;
985b8bb4 2153 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
666a4537 2154 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4e9a86b6
VS
2155 return 128 * 1024;
2156 else if (INTEL_INFO(dev_priv)->gen >= 4)
2157 return 4 * 1024;
2158 else
44c5905e 2159 return 0;
4e9a86b6
VS
2160}
2161
603525d7
VS
2162static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
2163 uint64_t fb_modifier)
2164{
2165 switch (fb_modifier) {
2166 case DRM_FORMAT_MOD_NONE:
2167 return intel_linear_alignment(dev_priv);
2168 case I915_FORMAT_MOD_X_TILED:
2169 if (INTEL_INFO(dev_priv)->gen >= 9)
2170 return 256 * 1024;
2171 return 0;
2172 case I915_FORMAT_MOD_Y_TILED:
2173 case I915_FORMAT_MOD_Yf_TILED:
2174 return 1 * 1024 * 1024;
2175 default:
2176 MISSING_CASE(fb_modifier);
2177 return 0;
2178 }
2179}
2180
058d88c4
CW
2181struct i915_vma *
2182intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
6b95a207 2183{
850c4cdc 2184 struct drm_device *dev = fb->dev;
fac5e23e 2185 struct drm_i915_private *dev_priv = to_i915(dev);
850c4cdc 2186 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2187 struct i915_ggtt_view view;
058d88c4 2188 struct i915_vma *vma;
6b95a207 2189 u32 alignment;
6b95a207 2190
ebcdd39e
MR
2191 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2192
bae781b2 2193 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6b95a207 2194
3465c580 2195 intel_fill_fb_ggtt_view(&view, fb, rotation);
f64b98cd 2196
693db184
CW
2197 /* Note that the w/a also requires 64 PTE of padding following the
2198 * bo. We currently fill all unused PTE with the shadow page and so
2199 * we should always have valid PTE following the scanout preventing
2200 * the VT-d warning.
2201 */
48f112fe 2202 if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
693db184
CW
2203 alignment = 256 * 1024;
2204
d6dd6843
PZ
2205 /*
2206 * Global gtt pte registers are special registers which actually forward
2207 * writes to a chunk of system memory. Which means that there is no risk
2208 * that the register values disappear as soon as we call
2209 * intel_runtime_pm_put(), so it is correct to wrap only the
2210 * pin/unpin/fence and not more.
2211 */
2212 intel_runtime_pm_get(dev_priv);
2213
058d88c4 2214 vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
49ef5294
CW
2215 if (IS_ERR(vma))
2216 goto err;
6b95a207 2217
05a20d09 2218 if (i915_vma_is_map_and_fenceable(vma)) {
49ef5294
CW
2219 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2220 * fence, whereas 965+ only requires a fence if using
2221 * framebuffer compression. For simplicity, we always, when
2222 * possible, install a fence as the cost is not that onerous.
2223 *
2224 * If we fail to fence the tiled scanout, then either the
2225 * modeset will reject the change (which is highly unlikely as
2226 * the affected systems, all but one, do not have unmappable
2227 * space) or we will not be able to enable full powersaving
2228 * techniques (also likely not to apply due to various limits
2229 * FBC and the like impose on the size of the buffer, which
2230 * presumably we violated anyway with this unmappable buffer).
2231 * Anyway, it is presumably better to stumble onwards with
2232 * something and try to run the system in a "less than optimal"
2233 * mode that matches the user configuration.
2234 */
2235 if (i915_vma_get_fence(vma) == 0)
2236 i915_vma_pin_fence(vma);
9807216f 2237 }
6b95a207 2238
49ef5294 2239err:
d6dd6843 2240 intel_runtime_pm_put(dev_priv);
058d88c4 2241 return vma;
6b95a207
KH
2242}
2243
fb4b8ce1 2244void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
1690e1eb 2245{
82bc3b2d 2246 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2247 struct i915_ggtt_view view;
058d88c4 2248 struct i915_vma *vma;
82bc3b2d 2249
ebcdd39e
MR
2250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
3465c580 2252 intel_fill_fb_ggtt_view(&view, fb, rotation);
05a20d09 2253 vma = i915_gem_object_to_ggtt(obj, &view);
f64b98cd 2254
49ef5294 2255 i915_vma_unpin_fence(vma);
058d88c4 2256 i915_gem_object_unpin_from_display_plane(vma);
1690e1eb
CW
2257}
2258
ef78ec94
VS
2259static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
2260 unsigned int rotation)
2261{
bd2ef25d 2262 if (drm_rotation_90_or_270(rotation))
ef78ec94
VS
2263 return to_intel_framebuffer(fb)->rotated[plane].pitch;
2264 else
2265 return fb->pitches[plane];
2266}
2267
6687c906
VS
2268/*
2269 * Convert the x/y offsets into a linear offset.
2270 * Only valid with 0/180 degree rotation, which is fine since linear
2271 * offset is only used with linear buffers on pre-hsw and tiled buffers
2272 * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
2273 */
2274u32 intel_fb_xy_to_linear(int x, int y,
2949056c
VS
2275 const struct intel_plane_state *state,
2276 int plane)
6687c906 2277{
2949056c 2278 const struct drm_framebuffer *fb = state->base.fb;
6687c906
VS
2279 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2280 unsigned int pitch = fb->pitches[plane];
2281
2282 return y * pitch + x * cpp;
2283}
2284
2285/*
2286 * Add the x/y offsets derived from fb->offsets[] to the user
2287 * specified plane src x/y offsets. The resulting x/y offsets
2288 * specify the start of scanout from the beginning of the gtt mapping.
2289 */
2290void intel_add_fb_offsets(int *x, int *y,
2949056c
VS
2291 const struct intel_plane_state *state,
2292 int plane)
6687c906
VS
2293
2294{
2949056c
VS
2295 const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
2296 unsigned int rotation = state->base.rotation;
6687c906 2297
bd2ef25d 2298 if (drm_rotation_90_or_270(rotation)) {
6687c906
VS
2299 *x += intel_fb->rotated[plane].x;
2300 *y += intel_fb->rotated[plane].y;
2301 } else {
2302 *x += intel_fb->normal[plane].x;
2303 *y += intel_fb->normal[plane].y;
2304 }
2305}
2306
29cf9491 2307/*
29cf9491
VS
2308 * Input tile dimensions and pitch must already be
2309 * rotated to match x and y, and in pixel units.
2310 */
66a2d927
VS
2311static u32 _intel_adjust_tile_offset(int *x, int *y,
2312 unsigned int tile_width,
2313 unsigned int tile_height,
2314 unsigned int tile_size,
2315 unsigned int pitch_tiles,
2316 u32 old_offset,
2317 u32 new_offset)
29cf9491 2318{
b9b24038 2319 unsigned int pitch_pixels = pitch_tiles * tile_width;
29cf9491
VS
2320 unsigned int tiles;
2321
2322 WARN_ON(old_offset & (tile_size - 1));
2323 WARN_ON(new_offset & (tile_size - 1));
2324 WARN_ON(new_offset > old_offset);
2325
2326 tiles = (old_offset - new_offset) / tile_size;
2327
2328 *y += tiles / pitch_tiles * tile_height;
2329 *x += tiles % pitch_tiles * tile_width;
2330
b9b24038
VS
2331 /* minimize x in case it got needlessly big */
2332 *y += *x / pitch_pixels * tile_height;
2333 *x %= pitch_pixels;
2334
29cf9491
VS
2335 return new_offset;
2336}
2337
66a2d927
VS
2338/*
2339 * Adjust the tile offset by moving the difference into
2340 * the x/y offsets.
2341 */
2342static u32 intel_adjust_tile_offset(int *x, int *y,
2343 const struct intel_plane_state *state, int plane,
2344 u32 old_offset, u32 new_offset)
2345{
2346 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2347 const struct drm_framebuffer *fb = state->base.fb;
2348 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2349 unsigned int rotation = state->base.rotation;
2350 unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
2351
2352 WARN_ON(new_offset > old_offset);
2353
bae781b2 2354 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
66a2d927
VS
2355 unsigned int tile_size, tile_width, tile_height;
2356 unsigned int pitch_tiles;
2357
2358 tile_size = intel_tile_size(dev_priv);
2359 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2360 fb->modifier, cpp);
66a2d927 2361
bd2ef25d 2362 if (drm_rotation_90_or_270(rotation)) {
66a2d927
VS
2363 pitch_tiles = pitch / tile_height;
2364 swap(tile_width, tile_height);
2365 } else {
2366 pitch_tiles = pitch / (tile_width * cpp);
2367 }
2368
2369 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2370 tile_size, pitch_tiles,
2371 old_offset, new_offset);
2372 } else {
2373 old_offset += *y * pitch + *x * cpp;
2374
2375 *y = (old_offset - new_offset) / pitch;
2376 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
2377 }
2378
2379 return new_offset;
2380}
2381
8d0deca8
VS
2382/*
2383 * Computes the linear offset to the base tile and adjusts
2384 * x, y. bytes per pixel is assumed to be a power-of-two.
2385 *
2386 * In the 90/270 rotated case, x and y are assumed
2387 * to be already rotated to match the rotated GTT view, and
2388 * pitch is the tile_height aligned framebuffer height.
6687c906
VS
2389 *
2390 * This function is used when computing the derived information
2391 * under intel_framebuffer, so using any of that information
2392 * here is not allowed. Anything under drm_framebuffer can be
2393 * used. This is why the user has to pass in the pitch since it
2394 * is specified in the rotated orientation.
8d0deca8 2395 */
6687c906
VS
2396static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
2397 int *x, int *y,
2398 const struct drm_framebuffer *fb, int plane,
2399 unsigned int pitch,
2400 unsigned int rotation,
2401 u32 alignment)
c2c75131 2402{
bae781b2 2403 uint64_t fb_modifier = fb->modifier;
4f2d9934 2404 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
6687c906 2405 u32 offset, offset_aligned;
29cf9491 2406
29cf9491
VS
2407 if (alignment)
2408 alignment--;
2409
b5c65338 2410 if (fb_modifier != DRM_FORMAT_MOD_NONE) {
8d0deca8
VS
2411 unsigned int tile_size, tile_width, tile_height;
2412 unsigned int tile_rows, tiles, pitch_tiles;
c2c75131 2413
d843310d 2414 tile_size = intel_tile_size(dev_priv);
8d0deca8
VS
2415 intel_tile_dims(dev_priv, &tile_width, &tile_height,
2416 fb_modifier, cpp);
2417
bd2ef25d 2418 if (drm_rotation_90_or_270(rotation)) {
8d0deca8
VS
2419 pitch_tiles = pitch / tile_height;
2420 swap(tile_width, tile_height);
2421 } else {
2422 pitch_tiles = pitch / (tile_width * cpp);
2423 }
d843310d
VS
2424
2425 tile_rows = *y / tile_height;
2426 *y %= tile_height;
c2c75131 2427
8d0deca8
VS
2428 tiles = *x / tile_width;
2429 *x %= tile_width;
bc752862 2430
29cf9491
VS
2431 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
2432 offset_aligned = offset & ~alignment;
bc752862 2433
66a2d927
VS
2434 _intel_adjust_tile_offset(x, y, tile_width, tile_height,
2435 tile_size, pitch_tiles,
2436 offset, offset_aligned);
29cf9491 2437 } else {
bc752862 2438 offset = *y * pitch + *x * cpp;
29cf9491
VS
2439 offset_aligned = offset & ~alignment;
2440
4e9a86b6
VS
2441 *y = (offset & alignment) / pitch;
2442 *x = ((offset & alignment) - *y * pitch) / cpp;
bc752862 2443 }
29cf9491
VS
2444
2445 return offset_aligned;
c2c75131
SV
2446}
2447
6687c906 2448u32 intel_compute_tile_offset(int *x, int *y,
2949056c
VS
2449 const struct intel_plane_state *state,
2450 int plane)
6687c906 2451{
2949056c
VS
2452 const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
2453 const struct drm_framebuffer *fb = state->base.fb;
2454 unsigned int rotation = state->base.rotation;
ef78ec94 2455 int pitch = intel_fb_pitch(fb, plane, rotation);
8d970654
VS
2456 u32 alignment;
2457
2458 /* AUX_DIST needs only 4K alignment */
2459 if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
2460 alignment = 4096;
2461 else
bae781b2 2462 alignment = intel_surf_alignment(dev_priv, fb->modifier);
6687c906
VS
2463
2464 return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
2465 rotation, alignment);
2466}
2467
2468/* Convert the fb->offset[] linear offset into x/y offsets */
2469static void intel_fb_offset_to_xy(int *x, int *y,
2470 const struct drm_framebuffer *fb, int plane)
2471{
2472 unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2473 unsigned int pitch = fb->pitches[plane];
2474 u32 linear_offset = fb->offsets[plane];
2475
2476 *y = linear_offset / pitch;
2477 *x = linear_offset % pitch / cpp;
2478}
2479
72618ebf
VS
2480static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
2481{
2482 switch (fb_modifier) {
2483 case I915_FORMAT_MOD_X_TILED:
2484 return I915_TILING_X;
2485 case I915_FORMAT_MOD_Y_TILED:
2486 return I915_TILING_Y;
2487 default:
2488 return I915_TILING_NONE;
2489 }
2490}
2491
6687c906
VS
2492static int
2493intel_fill_fb_info(struct drm_i915_private *dev_priv,
2494 struct drm_framebuffer *fb)
2495{
2496 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2497 struct intel_rotation_info *rot_info = &intel_fb->rot_info;
2498 u32 gtt_offset_rotated = 0;
2499 unsigned int max_size = 0;
2500 uint32_t format = fb->pixel_format;
2501 int i, num_planes = drm_format_num_planes(format);
2502 unsigned int tile_size = intel_tile_size(dev_priv);
2503
2504 for (i = 0; i < num_planes; i++) {
2505 unsigned int width, height;
2506 unsigned int cpp, size;
2507 u32 offset;
2508 int x, y;
2509
2510 cpp = drm_format_plane_cpp(format, i);
2511 width = drm_format_plane_width(fb->width, format, i);
2512 height = drm_format_plane_height(fb->height, format, i);
2513
2514 intel_fb_offset_to_xy(&x, &y, fb, i);
2515
60d5f2a4
VS
2516 /*
2517 * The fence (if used) is aligned to the start of the object
2518 * so having the framebuffer wrap around across the edge of the
2519 * fenced region doesn't really work. We have no API to configure
2520 * the fence start offset within the object (nor could we probably
2521 * on gen2/3). So it's just easier if we just require that the
2522 * fb layout agrees with the fence layout. We already check that the
2523 * fb stride matches the fence stride elsewhere.
2524 */
2525 if (i915_gem_object_is_tiled(intel_fb->obj) &&
2526 (x + width) * cpp > fb->pitches[i]) {
2527 DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
2528 i, fb->offsets[i]);
2529 return -EINVAL;
2530 }
2531
6687c906
VS
2532 /*
2533 * First pixel of the framebuffer from
2534 * the start of the normal gtt mapping.
2535 */
2536 intel_fb->normal[i].x = x;
2537 intel_fb->normal[i].y = y;
2538
2539 offset = _intel_compute_tile_offset(dev_priv, &x, &y,
2540 fb, 0, fb->pitches[i],
cc926387 2541 DRM_ROTATE_0, tile_size);
6687c906
VS
2542 offset /= tile_size;
2543
bae781b2 2544 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
6687c906
VS
2545 unsigned int tile_width, tile_height;
2546 unsigned int pitch_tiles;
2547 struct drm_rect r;
2548
2549 intel_tile_dims(dev_priv, &tile_width, &tile_height,
bae781b2 2550 fb->modifier, cpp);
6687c906
VS
2551
2552 rot_info->plane[i].offset = offset;
2553 rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
2554 rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2555 rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2556
2557 intel_fb->rotated[i].pitch =
2558 rot_info->plane[i].height * tile_height;
2559
2560 /* how many tiles does this plane need */
2561 size = rot_info->plane[i].stride * rot_info->plane[i].height;
2562 /*
2563 * If the plane isn't horizontally tile aligned,
2564 * we need one more tile.
2565 */
2566 if (x != 0)
2567 size++;
2568
2569 /* rotate the x/y offsets to match the GTT view */
2570 r.x1 = x;
2571 r.y1 = y;
2572 r.x2 = x + width;
2573 r.y2 = y + height;
2574 drm_rect_rotate(&r,
2575 rot_info->plane[i].width * tile_width,
2576 rot_info->plane[i].height * tile_height,
cc926387 2577 DRM_ROTATE_270);
6687c906
VS
2578 x = r.x1;
2579 y = r.y1;
2580
2581 /* rotate the tile dimensions to match the GTT view */
2582 pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
2583 swap(tile_width, tile_height);
2584
2585 /*
2586 * We only keep the x/y offsets, so push all of the
2587 * gtt offset into the x/y offsets.
2588 */
66a2d927
VS
2589 _intel_adjust_tile_offset(&x, &y, tile_size,
2590 tile_width, tile_height, pitch_tiles,
2591 gtt_offset_rotated * tile_size, 0);
6687c906
VS
2592
2593 gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
2594
2595 /*
2596 * First pixel of the framebuffer from
2597 * the start of the rotated gtt mapping.
2598 */
2599 intel_fb->rotated[i].x = x;
2600 intel_fb->rotated[i].y = y;
2601 } else {
2602 size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2603 x * cpp, tile_size);
2604 }
2605
2606 /* how many tiles in total needed in the bo */
2607 max_size = max(max_size, offset + size);
2608 }
2609
2610 if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
2611 DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
2612 max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
2613 return -EINVAL;
2614 }
2615
2616 return 0;
2617}
2618
b35d63fa 2619static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2620{
2621 switch (format) {
2622 case DISPPLANE_8BPP:
2623 return DRM_FORMAT_C8;
2624 case DISPPLANE_BGRX555:
2625 return DRM_FORMAT_XRGB1555;
2626 case DISPPLANE_BGRX565:
2627 return DRM_FORMAT_RGB565;
2628 default:
2629 case DISPPLANE_BGRX888:
2630 return DRM_FORMAT_XRGB8888;
2631 case DISPPLANE_RGBX888:
2632 return DRM_FORMAT_XBGR8888;
2633 case DISPPLANE_BGRX101010:
2634 return DRM_FORMAT_XRGB2101010;
2635 case DISPPLANE_RGBX101010:
2636 return DRM_FORMAT_XBGR2101010;
2637 }
2638}
2639
bc8d7dff
DL
2640static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2641{
2642 switch (format) {
2643 case PLANE_CTL_FORMAT_RGB_565:
2644 return DRM_FORMAT_RGB565;
2645 default:
2646 case PLANE_CTL_FORMAT_XRGB_8888:
2647 if (rgb_order) {
2648 if (alpha)
2649 return DRM_FORMAT_ABGR8888;
2650 else
2651 return DRM_FORMAT_XBGR8888;
2652 } else {
2653 if (alpha)
2654 return DRM_FORMAT_ARGB8888;
2655 else
2656 return DRM_FORMAT_XRGB8888;
2657 }
2658 case PLANE_CTL_FORMAT_XRGB_2101010:
2659 if (rgb_order)
2660 return DRM_FORMAT_XBGR2101010;
2661 else
2662 return DRM_FORMAT_XRGB2101010;
2663 }
2664}
2665
5724dbd1 2666static bool
f6936e29
SV
2667intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2668 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2669{
2670 struct drm_device *dev = crtc->base.dev;
3badb49f 2671 struct drm_i915_private *dev_priv = to_i915(dev);
72e96d64 2672 struct i915_ggtt *ggtt = &dev_priv->ggtt;
46f297fb
JB
2673 struct drm_i915_gem_object *obj = NULL;
2674 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2675 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
SV
2676 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2677 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2678 PAGE_SIZE);
2679
2680 size_aligned -= base_aligned;
46f297fb 2681
ff2652ea
CW
2682 if (plane_config->size == 0)
2683 return false;
2684
3badb49f
PZ
2685 /* If the FB is too big, just don't use it since fbdev is not very
2686 * important and we should probably use that space with FBC or other
2687 * features. */
72e96d64 2688 if (size_aligned * 2 > ggtt->stolen_usable_size)
3badb49f
PZ
2689 return false;
2690
12c83d99
TU
2691 mutex_lock(&dev->struct_mutex);
2692
187685cb 2693 obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
f37b5c2b
SV
2694 base_aligned,
2695 base_aligned,
2696 size_aligned);
12c83d99
TU
2697 if (!obj) {
2698 mutex_unlock(&dev->struct_mutex);
484b41dd 2699 return false;
12c83d99 2700 }
46f297fb 2701
3e510a8e
CW
2702 if (plane_config->tiling == I915_TILING_X)
2703 obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
46f297fb 2704
6bf129df
DL
2705 mode_cmd.pixel_format = fb->pixel_format;
2706 mode_cmd.width = fb->width;
2707 mode_cmd.height = fb->height;
2708 mode_cmd.pitches[0] = fb->pitches[0];
bae781b2 2709 mode_cmd.modifier[0] = fb->modifier;
18c5247e 2710 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb 2711
6bf129df 2712 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2713 &mode_cmd, obj)) {
46f297fb
JB
2714 DRM_DEBUG_KMS("intel fb init failed\n");
2715 goto out_unref_obj;
2716 }
12c83d99 2717
46f297fb 2718 mutex_unlock(&dev->struct_mutex);
484b41dd 2719
f6936e29 2720 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2721 return true;
46f297fb
JB
2722
2723out_unref_obj:
f8c417cd 2724 i915_gem_object_put(obj);
46f297fb 2725 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2726 return false;
2727}
2728
5a21b665
SV
2729/* Update plane->state->fb to match plane->fb after driver-internal updates */
2730static void
2731update_state_fb(struct drm_plane *plane)
2732{
2733 if (plane->fb == plane->state->fb)
2734 return;
2735
2736 if (plane->state->fb)
2737 drm_framebuffer_unreference(plane->state->fb);
2738 plane->state->fb = plane->fb;
2739 if (plane->state->fb)
2740 drm_framebuffer_reference(plane->state->fb);
2741}
2742
5724dbd1 2743static void
f6936e29
SV
2744intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2745 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2746{
2747 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 2748 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd
JB
2749 struct drm_crtc *c;
2750 struct intel_crtc *i;
2ff8fde1 2751 struct drm_i915_gem_object *obj;
88595ac9 2752 struct drm_plane *primary = intel_crtc->base.primary;
be5651f2 2753 struct drm_plane_state *plane_state = primary->state;
200757f5
MR
2754 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
2755 struct intel_plane *intel_plane = to_intel_plane(primary);
0a8d8a86
MR
2756 struct intel_plane_state *intel_state =
2757 to_intel_plane_state(plane_state);
88595ac9 2758 struct drm_framebuffer *fb;
484b41dd 2759
2d14030b 2760 if (!plane_config->fb)
484b41dd
JB
2761 return;
2762
f6936e29 2763 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
SV
2764 fb = &plane_config->fb->base;
2765 goto valid_fb;
f55548b5 2766 }
484b41dd 2767
2d14030b 2768 kfree(plane_config->fb);
484b41dd
JB
2769
2770 /*
2771 * Failed to alloc the obj, check to see if we should share
2772 * an fb with another CRTC instead
2773 */
70e1e0ec 2774 for_each_crtc(dev, c) {
484b41dd
JB
2775 i = to_intel_crtc(c);
2776
2777 if (c == &intel_crtc->base)
2778 continue;
2779
2ff8fde1
MR
2780 if (!i->active)
2781 continue;
2782
88595ac9
SV
2783 fb = c->primary->fb;
2784 if (!fb)
484b41dd
JB
2785 continue;
2786
88595ac9 2787 obj = intel_fb_obj(fb);
058d88c4 2788 if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
88595ac9
SV
2789 drm_framebuffer_reference(fb);
2790 goto valid_fb;
484b41dd
JB
2791 }
2792 }
88595ac9 2793
200757f5
MR
2794 /*
2795 * We've failed to reconstruct the BIOS FB. Current display state
2796 * indicates that the primary plane is visible, but has a NULL FB,
2797 * which will lead to problems later if we don't fix it up. The
2798 * simplest solution is to just disable the primary plane now and
2799 * pretend the BIOS never had it enabled.
2800 */
936e71e3 2801 to_intel_plane_state(plane_state)->base.visible = false;
200757f5 2802 crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
2622a081 2803 intel_pre_disable_primary_noatomic(&intel_crtc->base);
200757f5
MR
2804 intel_plane->disable_plane(primary, &intel_crtc->base);
2805
88595ac9
SV
2806 return;
2807
2808valid_fb:
f44e2659
VS
2809 plane_state->src_x = 0;
2810 plane_state->src_y = 0;
be5651f2
ML
2811 plane_state->src_w = fb->width << 16;
2812 plane_state->src_h = fb->height << 16;
2813
f44e2659
VS
2814 plane_state->crtc_x = 0;
2815 plane_state->crtc_y = 0;
be5651f2
ML
2816 plane_state->crtc_w = fb->width;
2817 plane_state->crtc_h = fb->height;
2818
1638d30c
RC
2819 intel_state->base.src = drm_plane_state_src(plane_state);
2820 intel_state->base.dst = drm_plane_state_dest(plane_state);
0a8d8a86 2821
88595ac9 2822 obj = intel_fb_obj(fb);
3e510a8e 2823 if (i915_gem_object_is_tiled(obj))
88595ac9
SV
2824 dev_priv->preserve_bios_swizzle = true;
2825
be5651f2
ML
2826 drm_framebuffer_reference(fb);
2827 primary->fb = primary->state->fb = fb;
36750f28 2828 primary->crtc = primary->state->crtc = &intel_crtc->base;
36750f28 2829 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
faf5bf0a
CW
2830 atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2831 &obj->frontbuffer_bits);
46f297fb
JB
2832}
2833
b63a16f6
VS
2834static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
2835 unsigned int rotation)
2836{
2837 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
2838
bae781b2 2839 switch (fb->modifier) {
b63a16f6
VS
2840 case DRM_FORMAT_MOD_NONE:
2841 case I915_FORMAT_MOD_X_TILED:
2842 switch (cpp) {
2843 case 8:
2844 return 4096;
2845 case 4:
2846 case 2:
2847 case 1:
2848 return 8192;
2849 default:
2850 MISSING_CASE(cpp);
2851 break;
2852 }
2853 break;
2854 case I915_FORMAT_MOD_Y_TILED:
2855 case I915_FORMAT_MOD_Yf_TILED:
2856 switch (cpp) {
2857 case 8:
2858 return 2048;
2859 case 4:
2860 return 4096;
2861 case 2:
2862 case 1:
2863 return 8192;
2864 default:
2865 MISSING_CASE(cpp);
2866 break;
2867 }
2868 break;
2869 default:
bae781b2 2870 MISSING_CASE(fb->modifier);
b63a16f6
VS
2871 }
2872
2873 return 2048;
2874}
2875
2876static int skl_check_main_surface(struct intel_plane_state *plane_state)
2877{
2878 const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
2879 const struct drm_framebuffer *fb = plane_state->base.fb;
2880 unsigned int rotation = plane_state->base.rotation;
cc926387
SV
2881 int x = plane_state->base.src.x1 >> 16;
2882 int y = plane_state->base.src.y1 >> 16;
2883 int w = drm_rect_width(&plane_state->base.src) >> 16;
2884 int h = drm_rect_height(&plane_state->base.src) >> 16;
b63a16f6
VS
2885 int max_width = skl_max_plane_width(fb, 0, rotation);
2886 int max_height = 4096;
8d970654 2887 u32 alignment, offset, aux_offset = plane_state->aux.offset;
b63a16f6
VS
2888
2889 if (w > max_width || h > max_height) {
2890 DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
2891 w, h, max_width, max_height);
2892 return -EINVAL;
2893 }
2894
2895 intel_add_fb_offsets(&x, &y, plane_state, 0);
2896 offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
2897
bae781b2 2898 alignment = intel_surf_alignment(dev_priv, fb->modifier);
b63a16f6 2899
8d970654
VS
2900 /*
2901 * AUX surface offset is specified as the distance from the
2902 * main surface offset, and it must be non-negative. Make
2903 * sure that is what we will get.
2904 */
2905 if (offset > aux_offset)
2906 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2907 offset, aux_offset & ~(alignment - 1));
2908
b63a16f6
VS
2909 /*
2910 * When using an X-tiled surface, the plane blows up
2911 * if the x offset + width exceed the stride.
2912 *
2913 * TODO: linear and Y-tiled seem fine, Yf untested,
2914 */
bae781b2 2915 if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
b63a16f6
VS
2916 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
2917
2918 while ((x + w) * cpp > fb->pitches[0]) {
2919 if (offset == 0) {
2920 DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
2921 return -EINVAL;
2922 }
2923
2924 offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
2925 offset, offset - alignment);
2926 }
2927 }
2928
2929 plane_state->main.offset = offset;
2930 plane_state->main.x = x;
2931 plane_state->main.y = y;
2932
2933 return 0;
2934}
2935
8d970654
VS
2936static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
2937{
2938 const struct drm_framebuffer *fb = plane_state->base.fb;
2939 unsigned int rotation = plane_state->base.rotation;
2940 int max_width = skl_max_plane_width(fb, 1, rotation);
2941 int max_height = 4096;
cc926387
SV
2942 int x = plane_state->base.src.x1 >> 17;
2943 int y = plane_state->base.src.y1 >> 17;
2944 int w = drm_rect_width(&plane_state->base.src) >> 17;
2945 int h = drm_rect_height(&plane_state->base.src) >> 17;
8d970654
VS
2946 u32 offset;
2947
2948 intel_add_fb_offsets(&x, &y, plane_state, 1);
2949 offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
2950
2951 /* FIXME not quite sure how/if these apply to the chroma plane */
2952 if (w > max_width || h > max_height) {
2953 DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
2954 w, h, max_width, max_height);
2955 return -EINVAL;
2956 }
2957
2958 plane_state->aux.offset = offset;
2959 plane_state->aux.x = x;
2960 plane_state->aux.y = y;
2961
2962 return 0;
2963}
2964
b63a16f6
VS
2965int skl_check_plane_surface(struct intel_plane_state *plane_state)
2966{
2967 const struct drm_framebuffer *fb = plane_state->base.fb;
2968 unsigned int rotation = plane_state->base.rotation;
2969 int ret;
2970
2971 /* Rotate src coordinates to match rotated GTT view */
bd2ef25d 2972 if (drm_rotation_90_or_270(rotation))
cc926387 2973 drm_rect_rotate(&plane_state->base.src,
da064b47
VS
2974 fb->width << 16, fb->height << 16,
2975 DRM_ROTATE_270);
b63a16f6 2976
8d970654
VS
2977 /*
2978 * Handle the AUX surface first since
2979 * the main surface setup depends on it.
2980 */
2981 if (fb->pixel_format == DRM_FORMAT_NV12) {
2982 ret = skl_check_nv12_aux_surface(plane_state);
2983 if (ret)
2984 return ret;
2985 } else {
2986 plane_state->aux.offset = ~0xfff;
2987 plane_state->aux.x = 0;
2988 plane_state->aux.y = 0;
2989 }
2990
b63a16f6
VS
2991 ret = skl_check_main_surface(plane_state);
2992 if (ret)
2993 return ret;
2994
2995 return 0;
2996}
2997
a8d201af
ML
2998static void i9xx_update_primary_plane(struct drm_plane *primary,
2999 const struct intel_crtc_state *crtc_state,
3000 const struct intel_plane_state *plane_state)
81255565 3001{
6315b5d3 3002 struct drm_i915_private *dev_priv = to_i915(primary->dev);
a8d201af
ML
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3004 struct drm_framebuffer *fb = plane_state->base.fb;
81255565 3005 int plane = intel_crtc->plane;
54ea9da8 3006 u32 linear_offset;
81255565 3007 u32 dspcntr;
f0f59a00 3008 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3009 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3010 int x = plane_state->base.src.x1 >> 16;
3011 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3012
f45651ba
VS
3013 dspcntr = DISPPLANE_GAMMA_ENABLE;
3014
fdd508a6 3015 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3016
6315b5d3 3017 if (INTEL_GEN(dev_priv) < 4) {
f45651ba
VS
3018 if (intel_crtc->pipe == PIPE_B)
3019 dspcntr |= DISPPLANE_SEL_PIPE_B;
3020
3021 /* pipesrc and dspsize control the size that is scaled from,
3022 * which should always be the user's requested size.
3023 */
3024 I915_WRITE(DSPSIZE(plane),
a8d201af
ML
3025 ((crtc_state->pipe_src_h - 1) << 16) |
3026 (crtc_state->pipe_src_w - 1));
f45651ba 3027 I915_WRITE(DSPPOS(plane), 0);
920a14b2 3028 } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
c14b0485 3029 I915_WRITE(PRIMSIZE(plane),
a8d201af
ML
3030 ((crtc_state->pipe_src_h - 1) << 16) |
3031 (crtc_state->pipe_src_w - 1));
c14b0485
VS
3032 I915_WRITE(PRIMPOS(plane), 0);
3033 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 3034 }
81255565 3035
57779d06
VS
3036 switch (fb->pixel_format) {
3037 case DRM_FORMAT_C8:
81255565
JB
3038 dspcntr |= DISPPLANE_8BPP;
3039 break;
57779d06 3040 case DRM_FORMAT_XRGB1555:
57779d06 3041 dspcntr |= DISPPLANE_BGRX555;
81255565 3042 break;
57779d06
VS
3043 case DRM_FORMAT_RGB565:
3044 dspcntr |= DISPPLANE_BGRX565;
3045 break;
3046 case DRM_FORMAT_XRGB8888:
57779d06
VS
3047 dspcntr |= DISPPLANE_BGRX888;
3048 break;
3049 case DRM_FORMAT_XBGR8888:
57779d06
VS
3050 dspcntr |= DISPPLANE_RGBX888;
3051 break;
3052 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3053 dspcntr |= DISPPLANE_BGRX101010;
3054 break;
3055 case DRM_FORMAT_XBGR2101010:
57779d06 3056 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
3057 break;
3058 default:
baba133a 3059 BUG();
81255565 3060 }
57779d06 3061
72618ebf 3062 if (INTEL_GEN(dev_priv) >= 4 &&
bae781b2 3063 fb->modifier == I915_FORMAT_MOD_X_TILED)
f45651ba 3064 dspcntr |= DISPPLANE_TILED;
81255565 3065
df0cd455
VS
3066 if (rotation & DRM_ROTATE_180)
3067 dspcntr |= DISPPLANE_ROTATE_180;
3068
4ea7be2b
VS
3069 if (rotation & DRM_REFLECT_X)
3070 dspcntr |= DISPPLANE_MIRROR;
3071
9beb5fea 3072 if (IS_G4X(dev_priv))
de1aa629
VS
3073 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
3074
2949056c 3075 intel_add_fb_offsets(&x, &y, plane_state, 0);
81255565 3076
6315b5d3 3077 if (INTEL_GEN(dev_priv) >= 4)
c2c75131 3078 intel_crtc->dspaddr_offset =
2949056c 3079 intel_compute_tile_offset(&x, &y, plane_state, 0);
e506a0c6 3080
f22aa143 3081 if (rotation & DRM_ROTATE_180) {
df0cd455
VS
3082 x += crtc_state->pipe_src_w - 1;
3083 y += crtc_state->pipe_src_h - 1;
4ea7be2b
VS
3084 } else if (rotation & DRM_REFLECT_X) {
3085 x += crtc_state->pipe_src_w - 1;
48404c1e
SJ
3086 }
3087
2949056c 3088 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3089
6315b5d3 3090 if (INTEL_GEN(dev_priv) < 4)
6687c906
VS
3091 intel_crtc->dspaddr_offset = linear_offset;
3092
2db3366b
PZ
3093 intel_crtc->adjusted_x = x;
3094 intel_crtc->adjusted_y = y;
3095
48404c1e
SJ
3096 I915_WRITE(reg, dspcntr);
3097
01f2c773 3098 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
6315b5d3 3099 if (INTEL_GEN(dev_priv) >= 4) {
85ba7b7d 3100 I915_WRITE(DSPSURF(plane),
6687c906
VS
3101 intel_fb_gtt_offset(fb, rotation) +
3102 intel_crtc->dspaddr_offset);
5eddb70b 3103 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 3104 I915_WRITE(DSPLINOFF(plane), linear_offset);
bfb81049
VS
3105 } else {
3106 I915_WRITE(DSPADDR(plane),
3107 intel_fb_gtt_offset(fb, rotation) +
3108 intel_crtc->dspaddr_offset);
3109 }
5eddb70b 3110 POSTING_READ(reg);
17638cd6
JB
3111}
3112
a8d201af
ML
3113static void i9xx_disable_primary_plane(struct drm_plane *primary,
3114 struct drm_crtc *crtc)
17638cd6
JB
3115{
3116 struct drm_device *dev = crtc->dev;
fac5e23e 3117 struct drm_i915_private *dev_priv = to_i915(dev);
17638cd6 3118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
17638cd6 3119 int plane = intel_crtc->plane;
f45651ba 3120
a8d201af
ML
3121 I915_WRITE(DSPCNTR(plane), 0);
3122 if (INTEL_INFO(dev_priv)->gen >= 4)
fdd508a6 3123 I915_WRITE(DSPSURF(plane), 0);
a8d201af
ML
3124 else
3125 I915_WRITE(DSPADDR(plane), 0);
3126 POSTING_READ(DSPCNTR(plane));
3127}
c9ba6fad 3128
a8d201af
ML
3129static void ironlake_update_primary_plane(struct drm_plane *primary,
3130 const struct intel_crtc_state *crtc_state,
3131 const struct intel_plane_state *plane_state)
3132{
3133 struct drm_device *dev = primary->dev;
fac5e23e 3134 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3136 struct drm_framebuffer *fb = plane_state->base.fb;
a8d201af 3137 int plane = intel_crtc->plane;
54ea9da8 3138 u32 linear_offset;
a8d201af
ML
3139 u32 dspcntr;
3140 i915_reg_t reg = DSPCNTR(plane);
8d0deca8 3141 unsigned int rotation = plane_state->base.rotation;
936e71e3
VS
3142 int x = plane_state->base.src.x1 >> 16;
3143 int y = plane_state->base.src.y1 >> 16;
c9ba6fad 3144
f45651ba 3145 dspcntr = DISPPLANE_GAMMA_ENABLE;
fdd508a6 3146 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba 3147
8652744b 3148 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
f45651ba 3149 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 3150
57779d06
VS
3151 switch (fb->pixel_format) {
3152 case DRM_FORMAT_C8:
17638cd6
JB
3153 dspcntr |= DISPPLANE_8BPP;
3154 break;
57779d06
VS
3155 case DRM_FORMAT_RGB565:
3156 dspcntr |= DISPPLANE_BGRX565;
17638cd6 3157 break;
57779d06 3158 case DRM_FORMAT_XRGB8888:
57779d06
VS
3159 dspcntr |= DISPPLANE_BGRX888;
3160 break;
3161 case DRM_FORMAT_XBGR8888:
57779d06
VS
3162 dspcntr |= DISPPLANE_RGBX888;
3163 break;
3164 case DRM_FORMAT_XRGB2101010:
57779d06
VS
3165 dspcntr |= DISPPLANE_BGRX101010;
3166 break;
3167 case DRM_FORMAT_XBGR2101010:
57779d06 3168 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
3169 break;
3170 default:
baba133a 3171 BUG();
17638cd6
JB
3172 }
3173
bae781b2 3174 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
17638cd6 3175 dspcntr |= DISPPLANE_TILED;
17638cd6 3176
df0cd455
VS
3177 if (rotation & DRM_ROTATE_180)
3178 dspcntr |= DISPPLANE_ROTATE_180;
3179
8652744b 3180 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
1f5d76db 3181 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 3182
2949056c 3183 intel_add_fb_offsets(&x, &y, plane_state, 0);
6687c906 3184
c2c75131 3185 intel_crtc->dspaddr_offset =
2949056c 3186 intel_compute_tile_offset(&x, &y, plane_state, 0);
6687c906 3187
df0cd455
VS
3188 /* HSW+ does this automagically in hardware */
3189 if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
3190 rotation & DRM_ROTATE_180) {
3191 x += crtc_state->pipe_src_w - 1;
3192 y += crtc_state->pipe_src_h - 1;
48404c1e
SJ
3193 }
3194
2949056c 3195 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 3196
2db3366b
PZ
3197 intel_crtc->adjusted_x = x;
3198 intel_crtc->adjusted_y = y;
3199
48404c1e 3200 I915_WRITE(reg, dspcntr);
17638cd6 3201
01f2c773 3202 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d 3203 I915_WRITE(DSPSURF(plane),
6687c906
VS
3204 intel_fb_gtt_offset(fb, rotation) +
3205 intel_crtc->dspaddr_offset);
8652744b 3206 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
bc1c91eb
DL
3207 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
3208 } else {
3209 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
3210 I915_WRITE(DSPLINOFF(plane), linear_offset);
3211 }
17638cd6 3212 POSTING_READ(reg);
17638cd6
JB
3213}
3214
7b49f948
VS
3215u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
3216 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 3217{
7b49f948 3218 if (fb_modifier == DRM_FORMAT_MOD_NONE) {
b321803d 3219 return 64;
7b49f948
VS
3220 } else {
3221 int cpp = drm_format_plane_cpp(pixel_format, 0);
3222
27ba3910 3223 return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
b321803d
DL
3224 }
3225}
3226
6687c906
VS
3227u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
3228 unsigned int rotation)
121920fa 3229{
6687c906 3230 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ce7f1728 3231 struct i915_ggtt_view view;
058d88c4 3232 struct i915_vma *vma;
121920fa 3233
6687c906 3234 intel_fill_fb_ggtt_view(&view, fb, rotation);
dedf278c 3235
058d88c4
CW
3236 vma = i915_gem_object_to_ggtt(obj, &view);
3237 if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
3238 view.type))
3239 return -1;
3240
bde13ebd 3241 return i915_ggtt_offset(vma);
121920fa
TU
3242}
3243
e435d6e5
ML
3244static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
3245{
3246 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 3247 struct drm_i915_private *dev_priv = to_i915(dev);
e435d6e5
ML
3248
3249 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
3250 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
3251 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
e435d6e5
ML
3252}
3253
a1b2278e
CK
3254/*
3255 * This function detaches (aka. unbinds) unused scalers in hardware
3256 */
0583236e 3257static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e 3258{
a1b2278e
CK
3259 struct intel_crtc_scaler_state *scaler_state;
3260 int i;
3261
a1b2278e
CK
3262 scaler_state = &intel_crtc->config->scaler_state;
3263
3264 /* loop through and disable scalers that aren't in use */
3265 for (i = 0; i < intel_crtc->num_scalers; i++) {
e435d6e5
ML
3266 if (!scaler_state->scalers[i].in_use)
3267 skl_detach_scaler(intel_crtc, i);
a1b2278e
CK
3268 }
3269}
3270
d2196774
VS
3271u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
3272 unsigned int rotation)
3273{
3274 const struct drm_i915_private *dev_priv = to_i915(fb->dev);
3275 u32 stride = intel_fb_pitch(fb, plane, rotation);
3276
3277 /*
3278 * The stride is either expressed as a multiple of 64 bytes chunks for
3279 * linear buffers or in number of tiles for tiled buffers.
3280 */
bd2ef25d 3281 if (drm_rotation_90_or_270(rotation)) {
d2196774
VS
3282 int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
3283
bae781b2 3284 stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
d2196774 3285 } else {
bae781b2 3286 stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
d2196774
VS
3287 fb->pixel_format);
3288 }
3289
3290 return stride;
3291}
3292
6156a456 3293u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 3294{
6156a456 3295 switch (pixel_format) {
d161cf7a 3296 case DRM_FORMAT_C8:
c34ce3d1 3297 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 3298 case DRM_FORMAT_RGB565:
c34ce3d1 3299 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 3300 case DRM_FORMAT_XBGR8888:
c34ce3d1 3301 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 3302 case DRM_FORMAT_XRGB8888:
c34ce3d1 3303 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
3304 /*
3305 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
3306 * to be already pre-multiplied. We need to add a knob (or a different
3307 * DRM_FORMAT) for user-space to configure that.
3308 */
f75fb42a 3309 case DRM_FORMAT_ABGR8888:
c34ce3d1 3310 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 3311 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 3312 case DRM_FORMAT_ARGB8888:
c34ce3d1 3313 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 3314 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 3315 case DRM_FORMAT_XRGB2101010:
c34ce3d1 3316 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 3317 case DRM_FORMAT_XBGR2101010:
c34ce3d1 3318 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 3319 case DRM_FORMAT_YUYV:
c34ce3d1 3320 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 3321 case DRM_FORMAT_YVYU:
c34ce3d1 3322 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 3323 case DRM_FORMAT_UYVY:
c34ce3d1 3324 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 3325 case DRM_FORMAT_VYUY:
c34ce3d1 3326 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 3327 default:
4249eeef 3328 MISSING_CASE(pixel_format);
70d21f0e 3329 }
8cfcba41 3330
c34ce3d1 3331 return 0;
6156a456 3332}
70d21f0e 3333
6156a456
CK
3334u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3335{
6156a456 3336 switch (fb_modifier) {
30af77c4 3337 case DRM_FORMAT_MOD_NONE:
70d21f0e 3338 break;
30af77c4 3339 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3340 return PLANE_CTL_TILED_X;
b321803d 3341 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3342 return PLANE_CTL_TILED_Y;
b321803d 3343 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3344 return PLANE_CTL_TILED_YF;
70d21f0e 3345 default:
6156a456 3346 MISSING_CASE(fb_modifier);
70d21f0e 3347 }
8cfcba41 3348
c34ce3d1 3349 return 0;
6156a456 3350}
70d21f0e 3351
6156a456
CK
3352u32 skl_plane_ctl_rotation(unsigned int rotation)
3353{
3b7a5119 3354 switch (rotation) {
31ad61e4 3355 case DRM_ROTATE_0:
6156a456 3356 break;
1e8df167
SJ
3357 /*
3358 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3359 * while i915 HW rotation is clockwise, thats why this swapping.
3360 */
31ad61e4 3361 case DRM_ROTATE_90:
1e8df167 3362 return PLANE_CTL_ROTATE_270;
31ad61e4 3363 case DRM_ROTATE_180:
c34ce3d1 3364 return PLANE_CTL_ROTATE_180;
31ad61e4 3365 case DRM_ROTATE_270:
1e8df167 3366 return PLANE_CTL_ROTATE_90;
6156a456
CK
3367 default:
3368 MISSING_CASE(rotation);
3369 }
3370
c34ce3d1 3371 return 0;
6156a456
CK
3372}
3373
a8d201af
ML
3374static void skylake_update_primary_plane(struct drm_plane *plane,
3375 const struct intel_crtc_state *crtc_state,
3376 const struct intel_plane_state *plane_state)
6156a456 3377{
a8d201af 3378 struct drm_device *dev = plane->dev;
fac5e23e 3379 struct drm_i915_private *dev_priv = to_i915(dev);
a8d201af
ML
3380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
3381 struct drm_framebuffer *fb = plane_state->base.fb;
8e816bb4
VS
3382 enum plane_id plane_id = to_intel_plane(plane)->id;
3383 enum pipe pipe = to_intel_plane(plane)->pipe;
d2196774 3384 u32 plane_ctl;
a8d201af 3385 unsigned int rotation = plane_state->base.rotation;
d2196774 3386 u32 stride = skl_plane_stride(fb, 0, rotation);
b63a16f6 3387 u32 surf_addr = plane_state->main.offset;
a8d201af 3388 int scaler_id = plane_state->scaler_id;
b63a16f6
VS
3389 int src_x = plane_state->main.x;
3390 int src_y = plane_state->main.y;
936e71e3
VS
3391 int src_w = drm_rect_width(&plane_state->base.src) >> 16;
3392 int src_h = drm_rect_height(&plane_state->base.src) >> 16;
3393 int dst_x = plane_state->base.dst.x1;
3394 int dst_y = plane_state->base.dst.y1;
3395 int dst_w = drm_rect_width(&plane_state->base.dst);
3396 int dst_h = drm_rect_height(&plane_state->base.dst);
70d21f0e 3397
6156a456
CK
3398 plane_ctl = PLANE_CTL_ENABLE |
3399 PLANE_CTL_PIPE_GAMMA_ENABLE |
3400 PLANE_CTL_PIPE_CSC_ENABLE;
3401
3402 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
bae781b2 3403 plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
6156a456 3404 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
6156a456
CK
3405 plane_ctl |= skl_plane_ctl_rotation(rotation);
3406
6687c906
VS
3407 /* Sizes are 0 based */
3408 src_w--;
3409 src_h--;
3410 dst_w--;
3411 dst_h--;
3412
4c0b8a8b
PZ
3413 intel_crtc->dspaddr_offset = surf_addr;
3414
6687c906
VS
3415 intel_crtc->adjusted_x = src_x;
3416 intel_crtc->adjusted_y = src_y;
2db3366b 3417
8e816bb4
VS
3418 I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
3419 I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
3420 I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
3421 I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
6156a456
CK
3422
3423 if (scaler_id >= 0) {
3424 uint32_t ps_ctrl = 0;
3425
3426 WARN_ON(!dst_w || !dst_h);
8e816bb4 3427 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
6156a456
CK
3428 crtc_state->scaler_state.scalers[scaler_id].mode;
3429 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3430 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3431 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3432 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
8e816bb4 3433 I915_WRITE(PLANE_POS(pipe, plane_id), 0);
6156a456 3434 } else {
8e816bb4 3435 I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
6156a456
CK
3436 }
3437
8e816bb4 3438 I915_WRITE(PLANE_SURF(pipe, plane_id),
6687c906 3439 intel_fb_gtt_offset(fb, rotation) + surf_addr);
70d21f0e 3440
8e816bb4 3441 POSTING_READ(PLANE_SURF(pipe, plane_id));
70d21f0e
DL
3442}
3443
a8d201af
ML
3444static void skylake_disable_primary_plane(struct drm_plane *primary,
3445 struct drm_crtc *crtc)
17638cd6
JB
3446{
3447 struct drm_device *dev = crtc->dev;
fac5e23e 3448 struct drm_i915_private *dev_priv = to_i915(dev);
8e816bb4
VS
3449 enum plane_id plane_id = to_intel_plane(primary)->id;
3450 enum pipe pipe = to_intel_plane(primary)->pipe;
62e0fb88 3451
8e816bb4
VS
3452 I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
3453 I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
3454 POSTING_READ(PLANE_SURF(pipe, plane_id));
a8d201af 3455}
29b9bde6 3456
a8d201af
ML
3457/* Assume fb object is pinned & idle & fenced and just update base pointers */
3458static int
3459intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3460 int x, int y, enum mode_set_atomic state)
3461{
3462 /* Support for kgdboc is disabled, this needs a major rework. */
3463 DRM_ERROR("legacy panic handler not supported any more.\n");
3464
3465 return -ENODEV;
81255565
JB
3466}
3467
5a21b665
SV
3468static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
3469{
3470 struct intel_crtc *crtc;
3471
91c8a326 3472 for_each_intel_crtc(&dev_priv->drm, crtc)
5a21b665
SV
3473 intel_finish_page_flip_cs(dev_priv, crtc->pipe);
3474}
3475
7514747d
VS
3476static void intel_update_primary_planes(struct drm_device *dev)
3477{
7514747d 3478 struct drm_crtc *crtc;
96a02917 3479
70e1e0ec 3480 for_each_crtc(dev, crtc) {
11c22da6 3481 struct intel_plane *plane = to_intel_plane(crtc->primary);
73974893
ML
3482 struct intel_plane_state *plane_state =
3483 to_intel_plane_state(plane->base.state);
11c22da6 3484
936e71e3 3485 if (plane_state->base.visible)
a8d201af
ML
3486 plane->update_plane(&plane->base,
3487 to_intel_crtc_state(crtc->state),
3488 plane_state);
73974893
ML
3489 }
3490}
3491
3492static int
3493__intel_display_resume(struct drm_device *dev,
3494 struct drm_atomic_state *state)
3495{
3496 struct drm_crtc_state *crtc_state;
3497 struct drm_crtc *crtc;
3498 int i, ret;
11c22da6 3499
73974893 3500 intel_modeset_setup_hw_state(dev);
29b74b7f 3501 i915_redisable_vga(to_i915(dev));
73974893
ML
3502
3503 if (!state)
3504 return 0;
3505
3506 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3507 /*
3508 * Force recalculation even if we restore
3509 * current state. With fast modeset this may not result
3510 * in a modeset when the state is compatible.
3511 */
3512 crtc_state->mode_changed = true;
96a02917 3513 }
73974893
ML
3514
3515 /* ignore any reset values/BIOS leftovers in the WM registers */
3516 to_intel_atomic_state(state)->skip_intermediate_wm = true;
3517
3518 ret = drm_atomic_commit(state);
3519
3520 WARN_ON(ret == -EDEADLK);
3521 return ret;
96a02917
VS
3522}
3523
4ac2ba2f
VS
3524static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
3525{
ae98104b
VS
3526 return intel_has_gpu_reset(dev_priv) &&
3527 INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
4ac2ba2f
VS
3528}
3529
c033666a 3530void intel_prepare_reset(struct drm_i915_private *dev_priv)
7514747d 3531{
73974893
ML
3532 struct drm_device *dev = &dev_priv->drm;
3533 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3534 struct drm_atomic_state *state;
3535 int ret;
3536
73974893
ML
3537 /*
3538 * Need mode_config.mutex so that we don't
3539 * trample ongoing ->detect() and whatnot.
3540 */
3541 mutex_lock(&dev->mode_config.mutex);
3542 drm_modeset_acquire_init(ctx, 0);
3543 while (1) {
3544 ret = drm_modeset_lock_all_ctx(dev, ctx);
3545 if (ret != -EDEADLK)
3546 break;
3547
3548 drm_modeset_backoff(ctx);
3549 }
3550
3551 /* reset doesn't touch the display, but flips might get nuked anyway, */
522a63de 3552 if (!i915.force_reset_modeset_test &&
4ac2ba2f 3553 !gpu_reset_clobbers_display(dev_priv))
7514747d
VS
3554 return;
3555
f98ce92f
VS
3556 /*
3557 * Disabling the crtcs gracefully seems nicer. Also the
3558 * g33 docs say we should at least disable all the planes.
3559 */
73974893
ML
3560 state = drm_atomic_helper_duplicate_state(dev, ctx);
3561 if (IS_ERR(state)) {
3562 ret = PTR_ERR(state);
3563 state = NULL;
3564 DRM_ERROR("Duplicating state failed with %i\n", ret);
3565 goto err;
3566 }
3567
3568 ret = drm_atomic_helper_disable_all(dev, ctx);
3569 if (ret) {
3570 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
3571 goto err;
3572 }
3573
3574 dev_priv->modeset_restore_state = state;
3575 state->acquire_ctx = ctx;
3576 return;
3577
3578err:
0853695c 3579 drm_atomic_state_put(state);
7514747d
VS
3580}
3581
c033666a 3582void intel_finish_reset(struct drm_i915_private *dev_priv)
7514747d 3583{
73974893
ML
3584 struct drm_device *dev = &dev_priv->drm;
3585 struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
3586 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
3587 int ret;
3588
5a21b665
SV
3589 /*
3590 * Flips in the rings will be nuked by the reset,
3591 * so complete all pending flips so that user space
3592 * will get its events and not get stuck.
3593 */
3594 intel_complete_page_flips(dev_priv);
3595
73974893
ML
3596 dev_priv->modeset_restore_state = NULL;
3597
7514747d 3598 /* reset doesn't touch the display */
4ac2ba2f 3599 if (!gpu_reset_clobbers_display(dev_priv)) {
522a63de
ML
3600 if (!state) {
3601 /*
3602 * Flips in the rings have been nuked by the reset,
3603 * so update the base address of all primary
3604 * planes to the the last fb to make sure we're
3605 * showing the correct fb after a reset.
3606 *
3607 * FIXME: Atomic will make this obsolete since we won't schedule
3608 * CS-based flips (which might get lost in gpu resets) any more.
3609 */
3610 intel_update_primary_planes(dev);
3611 } else {
3612 ret = __intel_display_resume(dev, state);
3613 if (ret)
3614 DRM_ERROR("Restoring old state failed with %i\n", ret);
3615 }
73974893
ML
3616 } else {
3617 /*
3618 * The display has been reset as well,
3619 * so need a full re-initialization.
3620 */
3621 intel_runtime_pm_disable_interrupts(dev_priv);
3622 intel_runtime_pm_enable_interrupts(dev_priv);
7514747d 3623
51f59205 3624 intel_pps_unlock_regs_wa(dev_priv);
73974893 3625 intel_modeset_init_hw(dev);
7514747d 3626
73974893
ML
3627 spin_lock_irq(&dev_priv->irq_lock);
3628 if (dev_priv->display.hpd_irq_setup)
3629 dev_priv->display.hpd_irq_setup(dev_priv);
3630 spin_unlock_irq(&dev_priv->irq_lock);
7514747d 3631
73974893
ML
3632 ret = __intel_display_resume(dev, state);
3633 if (ret)
3634 DRM_ERROR("Restoring old state failed with %i\n", ret);
7514747d 3635
73974893
ML
3636 intel_hpd_init(dev_priv);
3637 }
7514747d 3638
0853695c
CW
3639 if (state)
3640 drm_atomic_state_put(state);
73974893
ML
3641 drm_modeset_drop_locks(ctx);
3642 drm_modeset_acquire_fini(ctx);
3643 mutex_unlock(&dev->mode_config.mutex);
7514747d
VS
3644}
3645
8af29b0c
CW
3646static bool abort_flip_on_reset(struct intel_crtc *crtc)
3647{
3648 struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
3649
3650 if (i915_reset_in_progress(error))
3651 return true;
3652
3653 if (crtc->reset_count != i915_reset_count(error))
3654 return true;
3655
3656 return false;
3657}
3658
7d5e3799
CW
3659static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3660{
5a21b665
SV
3661 struct drm_device *dev = crtc->dev;
3662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5a21b665
SV
3663 bool pending;
3664
8af29b0c 3665 if (abort_flip_on_reset(intel_crtc))
5a21b665
SV
3666 return false;
3667
3668 spin_lock_irq(&dev->event_lock);
3669 pending = to_intel_crtc(crtc)->flip_work != NULL;
3670 spin_unlock_irq(&dev->event_lock);
3671
3672 return pending;
7d5e3799
CW
3673}
3674
bfd16b2a
ML
3675static void intel_update_pipe_config(struct intel_crtc *crtc,
3676 struct intel_crtc_state *old_crtc_state)
e30e8f75 3677{
6315b5d3 3678 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
bfd16b2a
ML
3679 struct intel_crtc_state *pipe_config =
3680 to_intel_crtc_state(crtc->base.state);
e30e8f75 3681
bfd16b2a
ML
3682 /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
3683 crtc->base.mode = crtc->base.state->mode;
3684
3685 DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
3686 old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
3687 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
e30e8f75
GP
3688
3689 /*
3690 * Update pipe size and adjust fitter if needed: the reason for this is
3691 * that in compute_mode_changes we check the native mode (not the pfit
3692 * mode) to see if we can flip rather than do a full mode set. In the
3693 * fastboot case, we'll flip, but if we don't update the pipesrc and
3694 * pfit state, we'll end up with a big fb scanned out into the wrong
3695 * sized surface.
e30e8f75
GP
3696 */
3697
e30e8f75 3698 I915_WRITE(PIPESRC(crtc->pipe),
bfd16b2a
ML
3699 ((pipe_config->pipe_src_w - 1) << 16) |
3700 (pipe_config->pipe_src_h - 1));
3701
3702 /* on skylake this is done by detaching scalers */
6315b5d3 3703 if (INTEL_GEN(dev_priv) >= 9) {
bfd16b2a
ML
3704 skl_detach_scalers(crtc);
3705
3706 if (pipe_config->pch_pfit.enabled)
3707 skylake_pfit_enable(crtc);
6e266956 3708 } else if (HAS_PCH_SPLIT(dev_priv)) {
bfd16b2a
ML
3709 if (pipe_config->pch_pfit.enabled)
3710 ironlake_pfit_enable(crtc);
3711 else if (old_crtc_state->pch_pfit.enabled)
3712 ironlake_pfit_disable(crtc, true);
e30e8f75 3713 }
e30e8f75
GP
3714}
3715
5e84e1a4
ZW
3716static void intel_fdi_normal_train(struct drm_crtc *crtc)
3717{
3718 struct drm_device *dev = crtc->dev;
fac5e23e 3719 struct drm_i915_private *dev_priv = to_i915(dev);
5e84e1a4
ZW
3720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3721 int pipe = intel_crtc->pipe;
f0f59a00
VS
3722 i915_reg_t reg;
3723 u32 temp;
5e84e1a4
ZW
3724
3725 /* enable normal train */
3726 reg = FDI_TX_CTL(pipe);
3727 temp = I915_READ(reg);
fd6b8f43 3728 if (IS_IVYBRIDGE(dev_priv)) {
357555c0
JB
3729 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3730 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3731 } else {
3732 temp &= ~FDI_LINK_TRAIN_NONE;
3733 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3734 }
5e84e1a4
ZW
3735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
6e266956 3739 if (HAS_PCH_CPT(dev_priv)) {
5e84e1a4
ZW
3740 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3741 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3742 } else {
3743 temp &= ~FDI_LINK_TRAIN_NONE;
3744 temp |= FDI_LINK_TRAIN_NONE;
3745 }
3746 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3747
3748 /* wait one idle pattern time */
3749 POSTING_READ(reg);
3750 udelay(1000);
357555c0
JB
3751
3752 /* IVB wants error correction enabled */
fd6b8f43 3753 if (IS_IVYBRIDGE(dev_priv))
357555c0
JB
3754 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3755 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3756}
3757
8db9d77b
ZW
3758/* The FDI link training functions for ILK/Ibexpeak. */
3759static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3760{
3761 struct drm_device *dev = crtc->dev;
fac5e23e 3762 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3764 int pipe = intel_crtc->pipe;
f0f59a00
VS
3765 i915_reg_t reg;
3766 u32 temp, tries;
8db9d77b 3767
1c8562f6 3768 /* FDI needs bits from pipe first */
0fc932b8 3769 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3770
e1a44743
AJ
3771 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3772 for train result */
5eddb70b
CW
3773 reg = FDI_RX_IMR(pipe);
3774 temp = I915_READ(reg);
e1a44743
AJ
3775 temp &= ~FDI_RX_SYMBOL_LOCK;
3776 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3777 I915_WRITE(reg, temp);
3778 I915_READ(reg);
e1a44743
AJ
3779 udelay(150);
3780
8db9d77b 3781 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3782 reg = FDI_TX_CTL(pipe);
3783 temp = I915_READ(reg);
627eb5a3 3784 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3785 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3786 temp &= ~FDI_LINK_TRAIN_NONE;
3787 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3788 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3789
5eddb70b
CW
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
8db9d77b
ZW
3792 temp &= ~FDI_LINK_TRAIN_NONE;
3793 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3794 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
8db9d77b
ZW
3797 udelay(150);
3798
5b2adf89 3799 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
SV
3800 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3802 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3803
5eddb70b 3804 reg = FDI_RX_IIR(pipe);
e1a44743 3805 for (tries = 0; tries < 5; tries++) {
5eddb70b 3806 temp = I915_READ(reg);
8db9d77b
ZW
3807 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3808
3809 if ((temp & FDI_RX_BIT_LOCK)) {
3810 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3811 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3812 break;
3813 }
8db9d77b 3814 }
e1a44743 3815 if (tries == 5)
5eddb70b 3816 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3817
3818 /* Train 2 */
5eddb70b
CW
3819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
8db9d77b
ZW
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3823 I915_WRITE(reg, temp);
8db9d77b 3824
5eddb70b
CW
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
8db9d77b
ZW
3827 temp &= ~FDI_LINK_TRAIN_NONE;
3828 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3829 I915_WRITE(reg, temp);
8db9d77b 3830
5eddb70b
CW
3831 POSTING_READ(reg);
3832 udelay(150);
8db9d77b 3833
5eddb70b 3834 reg = FDI_RX_IIR(pipe);
e1a44743 3835 for (tries = 0; tries < 5; tries++) {
5eddb70b 3836 temp = I915_READ(reg);
8db9d77b
ZW
3837 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3838
3839 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3840 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3841 DRM_DEBUG_KMS("FDI train 2 done.\n");
3842 break;
3843 }
8db9d77b 3844 }
e1a44743 3845 if (tries == 5)
5eddb70b 3846 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3847
3848 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3849
8db9d77b
ZW
3850}
3851
0206e353 3852static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3853 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3854 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3855 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3856 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3857};
3858
3859/* The FDI link training functions for SNB/Cougarpoint. */
3860static void gen6_fdi_link_train(struct drm_crtc *crtc)
3861{
3862 struct drm_device *dev = crtc->dev;
fac5e23e 3863 struct drm_i915_private *dev_priv = to_i915(dev);
8db9d77b
ZW
3864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3865 int pipe = intel_crtc->pipe;
f0f59a00
VS
3866 i915_reg_t reg;
3867 u32 temp, i, retry;
8db9d77b 3868
e1a44743
AJ
3869 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3870 for train result */
5eddb70b
CW
3871 reg = FDI_RX_IMR(pipe);
3872 temp = I915_READ(reg);
e1a44743
AJ
3873 temp &= ~FDI_RX_SYMBOL_LOCK;
3874 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3875 I915_WRITE(reg, temp);
3876
3877 POSTING_READ(reg);
e1a44743
AJ
3878 udelay(150);
3879
8db9d77b 3880 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3881 reg = FDI_TX_CTL(pipe);
3882 temp = I915_READ(reg);
627eb5a3 3883 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3884 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3885 temp &= ~FDI_LINK_TRAIN_NONE;
3886 temp |= FDI_LINK_TRAIN_PATTERN_1;
3887 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3888 /* SNB-B */
3889 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3890 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3891
d74cf324
SV
3892 I915_WRITE(FDI_RX_MISC(pipe),
3893 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3894
5eddb70b
CW
3895 reg = FDI_RX_CTL(pipe);
3896 temp = I915_READ(reg);
6e266956 3897 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3898 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3899 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3900 } else {
3901 temp &= ~FDI_LINK_TRAIN_NONE;
3902 temp |= FDI_LINK_TRAIN_PATTERN_1;
3903 }
5eddb70b
CW
3904 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3905
3906 POSTING_READ(reg);
8db9d77b
ZW
3907 udelay(150);
3908
0206e353 3909 for (i = 0; i < 4; i++) {
5eddb70b
CW
3910 reg = FDI_TX_CTL(pipe);
3911 temp = I915_READ(reg);
8db9d77b
ZW
3912 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3913 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3914 I915_WRITE(reg, temp);
3915
3916 POSTING_READ(reg);
8db9d77b
ZW
3917 udelay(500);
3918
fa37d39e
SP
3919 for (retry = 0; retry < 5; retry++) {
3920 reg = FDI_RX_IIR(pipe);
3921 temp = I915_READ(reg);
3922 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3923 if (temp & FDI_RX_BIT_LOCK) {
3924 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3925 DRM_DEBUG_KMS("FDI train 1 done.\n");
3926 break;
3927 }
3928 udelay(50);
8db9d77b 3929 }
fa37d39e
SP
3930 if (retry < 5)
3931 break;
8db9d77b
ZW
3932 }
3933 if (i == 4)
5eddb70b 3934 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3935
3936 /* Train 2 */
5eddb70b
CW
3937 reg = FDI_TX_CTL(pipe);
3938 temp = I915_READ(reg);
8db9d77b
ZW
3939 temp &= ~FDI_LINK_TRAIN_NONE;
3940 temp |= FDI_LINK_TRAIN_PATTERN_2;
5db94019 3941 if (IS_GEN6(dev_priv)) {
8db9d77b
ZW
3942 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3943 /* SNB-B */
3944 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3945 }
5eddb70b 3946 I915_WRITE(reg, temp);
8db9d77b 3947
5eddb70b
CW
3948 reg = FDI_RX_CTL(pipe);
3949 temp = I915_READ(reg);
6e266956 3950 if (HAS_PCH_CPT(dev_priv)) {
8db9d77b
ZW
3951 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3952 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3953 } else {
3954 temp &= ~FDI_LINK_TRAIN_NONE;
3955 temp |= FDI_LINK_TRAIN_PATTERN_2;
3956 }
5eddb70b
CW
3957 I915_WRITE(reg, temp);
3958
3959 POSTING_READ(reg);
8db9d77b
ZW
3960 udelay(150);
3961
0206e353 3962 for (i = 0; i < 4; i++) {
5eddb70b
CW
3963 reg = FDI_TX_CTL(pipe);
3964 temp = I915_READ(reg);
8db9d77b
ZW
3965 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3966 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3967 I915_WRITE(reg, temp);
3968
3969 POSTING_READ(reg);
8db9d77b
ZW
3970 udelay(500);
3971
fa37d39e
SP
3972 for (retry = 0; retry < 5; retry++) {
3973 reg = FDI_RX_IIR(pipe);
3974 temp = I915_READ(reg);
3975 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3976 if (temp & FDI_RX_SYMBOL_LOCK) {
3977 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3978 DRM_DEBUG_KMS("FDI train 2 done.\n");
3979 break;
3980 }
3981 udelay(50);
8db9d77b 3982 }
fa37d39e
SP
3983 if (retry < 5)
3984 break;
8db9d77b
ZW
3985 }
3986 if (i == 4)
5eddb70b 3987 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3988
3989 DRM_DEBUG_KMS("FDI train done.\n");
3990}
3991
357555c0
JB
3992/* Manual link training for Ivy Bridge A0 parts */
3993static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3994{
3995 struct drm_device *dev = crtc->dev;
fac5e23e 3996 struct drm_i915_private *dev_priv = to_i915(dev);
357555c0
JB
3997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3998 int pipe = intel_crtc->pipe;
f0f59a00
VS
3999 i915_reg_t reg;
4000 u32 temp, i, j;
357555c0
JB
4001
4002 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
4003 for train result */
4004 reg = FDI_RX_IMR(pipe);
4005 temp = I915_READ(reg);
4006 temp &= ~FDI_RX_SYMBOL_LOCK;
4007 temp &= ~FDI_RX_BIT_LOCK;
4008 I915_WRITE(reg, temp);
4009
4010 POSTING_READ(reg);
4011 udelay(150);
4012
01a415fd
SV
4013 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
4014 I915_READ(FDI_RX_IIR(pipe)));
4015
139ccd3f
JB
4016 /* Try each vswing and preemphasis setting twice before moving on */
4017 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
4018 /* disable first in case we need to retry */
4019 reg = FDI_TX_CTL(pipe);
4020 temp = I915_READ(reg);
4021 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
4022 temp &= ~FDI_TX_ENABLE;
4023 I915_WRITE(reg, temp);
357555c0 4024
139ccd3f
JB
4025 reg = FDI_RX_CTL(pipe);
4026 temp = I915_READ(reg);
4027 temp &= ~FDI_LINK_TRAIN_AUTO;
4028 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4029 temp &= ~FDI_RX_ENABLE;
4030 I915_WRITE(reg, temp);
357555c0 4031
139ccd3f 4032 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
4033 reg = FDI_TX_CTL(pipe);
4034 temp = I915_READ(reg);
139ccd3f 4035 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 4036 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 4037 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 4038 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
4039 temp |= snb_b_fdi_train_param[j/2];
4040 temp |= FDI_COMPOSITE_SYNC;
4041 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 4042
139ccd3f
JB
4043 I915_WRITE(FDI_RX_MISC(pipe),
4044 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 4045
139ccd3f 4046 reg = FDI_RX_CTL(pipe);
357555c0 4047 temp = I915_READ(reg);
139ccd3f
JB
4048 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4049 temp |= FDI_COMPOSITE_SYNC;
4050 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 4051
139ccd3f
JB
4052 POSTING_READ(reg);
4053 udelay(1); /* should be 0.5us */
357555c0 4054
139ccd3f
JB
4055 for (i = 0; i < 4; i++) {
4056 reg = FDI_RX_IIR(pipe);
4057 temp = I915_READ(reg);
4058 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4059
139ccd3f
JB
4060 if (temp & FDI_RX_BIT_LOCK ||
4061 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
4062 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
4063 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
4064 i);
4065 break;
4066 }
4067 udelay(1); /* should be 0.5us */
4068 }
4069 if (i == 4) {
4070 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
4071 continue;
4072 }
357555c0 4073
139ccd3f 4074 /* Train 2 */
357555c0
JB
4075 reg = FDI_TX_CTL(pipe);
4076 temp = I915_READ(reg);
139ccd3f
JB
4077 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
4078 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
4079 I915_WRITE(reg, temp);
4080
4081 reg = FDI_RX_CTL(pipe);
4082 temp = I915_READ(reg);
4083 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4084 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
4085 I915_WRITE(reg, temp);
4086
4087 POSTING_READ(reg);
139ccd3f 4088 udelay(2); /* should be 1.5us */
357555c0 4089
139ccd3f
JB
4090 for (i = 0; i < 4; i++) {
4091 reg = FDI_RX_IIR(pipe);
4092 temp = I915_READ(reg);
4093 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 4094
139ccd3f
JB
4095 if (temp & FDI_RX_SYMBOL_LOCK ||
4096 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
4097 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
4098 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
4099 i);
4100 goto train_done;
4101 }
4102 udelay(2); /* should be 1.5us */
357555c0 4103 }
139ccd3f
JB
4104 if (i == 4)
4105 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 4106 }
357555c0 4107
139ccd3f 4108train_done:
357555c0
JB
4109 DRM_DEBUG_KMS("FDI train done.\n");
4110}
4111
88cefb6c 4112static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 4113{
88cefb6c 4114 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4115 struct drm_i915_private *dev_priv = to_i915(dev);
2c07245f 4116 int pipe = intel_crtc->pipe;
f0f59a00
VS
4117 i915_reg_t reg;
4118 u32 temp;
c64e311e 4119
c98e9dcf 4120 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
4121 reg = FDI_RX_CTL(pipe);
4122 temp = I915_READ(reg);
627eb5a3 4123 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 4124 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 4125 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
4126 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
4127
4128 POSTING_READ(reg);
c98e9dcf
JB
4129 udelay(200);
4130
4131 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
4132 temp = I915_READ(reg);
4133 I915_WRITE(reg, temp | FDI_PCDCLK);
4134
4135 POSTING_READ(reg);
c98e9dcf
JB
4136 udelay(200);
4137
20749730
PZ
4138 /* Enable CPU FDI TX PLL, always on for Ironlake */
4139 reg = FDI_TX_CTL(pipe);
4140 temp = I915_READ(reg);
4141 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
4142 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 4143
20749730
PZ
4144 POSTING_READ(reg);
4145 udelay(100);
6be4a607 4146 }
0e23b99d
JB
4147}
4148
88cefb6c
SV
4149static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
4150{
4151 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4152 struct drm_i915_private *dev_priv = to_i915(dev);
88cefb6c 4153 int pipe = intel_crtc->pipe;
f0f59a00
VS
4154 i915_reg_t reg;
4155 u32 temp;
88cefb6c
SV
4156
4157 /* Switch from PCDclk to Rawclk */
4158 reg = FDI_RX_CTL(pipe);
4159 temp = I915_READ(reg);
4160 I915_WRITE(reg, temp & ~FDI_PCDCLK);
4161
4162 /* Disable CPU FDI TX PLL */
4163 reg = FDI_TX_CTL(pipe);
4164 temp = I915_READ(reg);
4165 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
4166
4167 POSTING_READ(reg);
4168 udelay(100);
4169
4170 reg = FDI_RX_CTL(pipe);
4171 temp = I915_READ(reg);
4172 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
4173
4174 /* Wait for the clocks to turn off. */
4175 POSTING_READ(reg);
4176 udelay(100);
4177}
4178
0fc932b8
JB
4179static void ironlake_fdi_disable(struct drm_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->dev;
fac5e23e 4182 struct drm_i915_private *dev_priv = to_i915(dev);
0fc932b8
JB
4183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4184 int pipe = intel_crtc->pipe;
f0f59a00
VS
4185 i915_reg_t reg;
4186 u32 temp;
0fc932b8
JB
4187
4188 /* disable CPU FDI tx and PCH FDI rx */
4189 reg = FDI_TX_CTL(pipe);
4190 temp = I915_READ(reg);
4191 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
4192 POSTING_READ(reg);
4193
4194 reg = FDI_RX_CTL(pipe);
4195 temp = I915_READ(reg);
4196 temp &= ~(0x7 << 16);
dfd07d72 4197 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4198 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
4199
4200 POSTING_READ(reg);
4201 udelay(100);
4202
4203 /* Ironlake workaround, disable clock pointer after downing FDI */
6e266956 4204 if (HAS_PCH_IBX(dev_priv))
6f06ce18 4205 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
4206
4207 /* still set train pattern 1 */
4208 reg = FDI_TX_CTL(pipe);
4209 temp = I915_READ(reg);
4210 temp &= ~FDI_LINK_TRAIN_NONE;
4211 temp |= FDI_LINK_TRAIN_PATTERN_1;
4212 I915_WRITE(reg, temp);
4213
4214 reg = FDI_RX_CTL(pipe);
4215 temp = I915_READ(reg);
6e266956 4216 if (HAS_PCH_CPT(dev_priv)) {
0fc932b8
JB
4217 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
4218 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
4219 } else {
4220 temp &= ~FDI_LINK_TRAIN_NONE;
4221 temp |= FDI_LINK_TRAIN_PATTERN_1;
4222 }
4223 /* BPC in FDI rx is consistent with that in PIPECONF */
4224 temp &= ~(0x07 << 16);
dfd07d72 4225 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
4226 I915_WRITE(reg, temp);
4227
4228 POSTING_READ(reg);
4229 udelay(100);
4230}
4231
49d73912 4232bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
5dce5b93
CW
4233{
4234 struct intel_crtc *crtc;
4235
4236 /* Note that we don't need to be called with mode_config.lock here
4237 * as our list of CRTC objects is static for the lifetime of the
4238 * device and so cannot disappear as we iterate. Similarly, we can
4239 * happily treat the predicates as racy, atomic checks as userspace
4240 * cannot claim and pin a new fb without at least acquring the
4241 * struct_mutex and so serialising with us.
4242 */
49d73912 4243 for_each_intel_crtc(&dev_priv->drm, crtc) {
5dce5b93
CW
4244 if (atomic_read(&crtc->unpin_work_count) == 0)
4245 continue;
4246
5a21b665 4247 if (crtc->flip_work)
0f0f74bc 4248 intel_wait_for_vblank(dev_priv, crtc->pipe);
5dce5b93
CW
4249
4250 return true;
4251 }
4252
4253 return false;
4254}
4255
5a21b665 4256static void page_flip_completed(struct intel_crtc *intel_crtc)
d6bbafa1
CW
4257{
4258 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
5a21b665
SV
4259 struct intel_flip_work *work = intel_crtc->flip_work;
4260
4261 intel_crtc->flip_work = NULL;
d6bbafa1
CW
4262
4263 if (work->event)
560ce1dc 4264 drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
d6bbafa1
CW
4265
4266 drm_crtc_vblank_put(&intel_crtc->base);
4267
5a21b665 4268 wake_up_all(&dev_priv->pending_flip_queue);
143f73b3 4269 queue_work(dev_priv->wq, &work->unpin_work);
5a21b665
SV
4270
4271 trace_i915_flip_complete(intel_crtc->plane,
4272 work->pending_flip_obj);
d6bbafa1
CW
4273}
4274
5008e874 4275static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 4276{
0f91128d 4277 struct drm_device *dev = crtc->dev;
fac5e23e 4278 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874 4279 long ret;
e6c3a2a6 4280
2c10d571 4281 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
5008e874
ML
4282
4283 ret = wait_event_interruptible_timeout(
4284 dev_priv->pending_flip_queue,
4285 !intel_crtc_has_pending_flip(crtc),
4286 60*HZ);
4287
4288 if (ret < 0)
4289 return ret;
4290
5a21b665
SV
4291 if (ret == 0) {
4292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4293 struct intel_flip_work *work;
4294
4295 spin_lock_irq(&dev->event_lock);
4296 work = intel_crtc->flip_work;
4297 if (work && !is_mmio_work(work)) {
4298 WARN_ONCE(1, "Removing stuck page flip\n");
4299 page_flip_completed(intel_crtc);
4300 }
4301 spin_unlock_irq(&dev->event_lock);
4302 }
5bb61643 4303
5008e874 4304 return 0;
e6c3a2a6
CW
4305}
4306
b7076546 4307void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
060f02d8
VS
4308{
4309 u32 temp;
4310
4311 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
4312
4313 mutex_lock(&dev_priv->sb_lock);
4314
4315 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4316 temp |= SBI_SSCCTL_DISABLE;
4317 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
4318
4319 mutex_unlock(&dev_priv->sb_lock);
4320}
4321
e615efe4
ED
4322/* Program iCLKIP clock to the desired frequency */
4323static void lpt_program_iclkip(struct drm_crtc *crtc)
4324{
64b46a06 4325 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6e3c9717 4326 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
4327 u32 divsel, phaseinc, auxdiv, phasedir = 0;
4328 u32 temp;
4329
060f02d8 4330 lpt_disable_iclkip(dev_priv);
e615efe4 4331
64b46a06
VS
4332 /* The iCLK virtual clock root frequency is in MHz,
4333 * but the adjusted_mode->crtc_clock in in KHz. To get the
4334 * divisors, it is necessary to divide one by another, so we
4335 * convert the virtual clock precision to KHz here for higher
4336 * precision.
4337 */
4338 for (auxdiv = 0; auxdiv < 2; auxdiv++) {
e615efe4
ED
4339 u32 iclk_virtual_root_freq = 172800 * 1000;
4340 u32 iclk_pi_range = 64;
64b46a06 4341 u32 desired_divisor;
e615efe4 4342
64b46a06
VS
4343 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4344 clock << auxdiv);
4345 divsel = (desired_divisor / iclk_pi_range) - 2;
4346 phaseinc = desired_divisor % iclk_pi_range;
e615efe4 4347
64b46a06
VS
4348 /*
4349 * Near 20MHz is a corner case which is
4350 * out of range for the 7-bit divisor
4351 */
4352 if (divsel <= 0x7f)
4353 break;
e615efe4
ED
4354 }
4355
4356 /* This should not happen with any sane values */
4357 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4358 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4359 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4360 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4361
4362 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4363 clock,
e615efe4
ED
4364 auxdiv,
4365 divsel,
4366 phasedir,
4367 phaseinc);
4368
060f02d8
VS
4369 mutex_lock(&dev_priv->sb_lock);
4370
e615efe4 4371 /* Program SSCDIVINTPHASE6 */
988d6ee8 4372 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4373 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4374 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4375 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4376 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4377 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4378 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4379 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4380
4381 /* Program SSCAUXDIV */
988d6ee8 4382 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4383 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4384 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4385 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4386
4387 /* Enable modulator and associated divider */
988d6ee8 4388 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4389 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4390 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4 4391
060f02d8
VS
4392 mutex_unlock(&dev_priv->sb_lock);
4393
e615efe4
ED
4394 /* Wait for initialization time */
4395 udelay(24);
4396
4397 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4398}
4399
8802e5b6
VS
4400int lpt_get_iclkip(struct drm_i915_private *dev_priv)
4401{
4402 u32 divsel, phaseinc, auxdiv;
4403 u32 iclk_virtual_root_freq = 172800 * 1000;
4404 u32 iclk_pi_range = 64;
4405 u32 desired_divisor;
4406 u32 temp;
4407
4408 if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
4409 return 0;
4410
4411 mutex_lock(&dev_priv->sb_lock);
4412
4413 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
4414 if (temp & SBI_SSCCTL_DISABLE) {
4415 mutex_unlock(&dev_priv->sb_lock);
4416 return 0;
4417 }
4418
4419 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
4420 divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
4421 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
4422 phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
4423 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
4424
4425 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
4426 auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
4427 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
4428
4429 mutex_unlock(&dev_priv->sb_lock);
4430
4431 desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
4432
4433 return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
4434 desired_divisor << auxdiv);
4435}
4436
275f01b2
SV
4437static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4438 enum pipe pch_transcoder)
4439{
4440 struct drm_device *dev = crtc->base.dev;
fac5e23e 4441 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 4442 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
SV
4443
4444 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4445 I915_READ(HTOTAL(cpu_transcoder)));
4446 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4447 I915_READ(HBLANK(cpu_transcoder)));
4448 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4449 I915_READ(HSYNC(cpu_transcoder)));
4450
4451 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4452 I915_READ(VTOTAL(cpu_transcoder)));
4453 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4454 I915_READ(VBLANK(cpu_transcoder)));
4455 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4456 I915_READ(VSYNC(cpu_transcoder)));
4457 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4458 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4459}
4460
003632d9 4461static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78 4462{
fac5e23e 4463 struct drm_i915_private *dev_priv = to_i915(dev);
1fbc0d78
SV
4464 uint32_t temp;
4465
4466 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4467 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
SV
4468 return;
4469
4470 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4471 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4472
003632d9
ACO
4473 temp &= ~FDI_BC_BIFURCATION_SELECT;
4474 if (enable)
4475 temp |= FDI_BC_BIFURCATION_SELECT;
4476
4477 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
SV
4478 I915_WRITE(SOUTH_CHICKEN1, temp);
4479 POSTING_READ(SOUTH_CHICKEN1);
4480}
4481
4482static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4483{
4484 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
SV
4485
4486 switch (intel_crtc->pipe) {
4487 case PIPE_A:
4488 break;
4489 case PIPE_B:
6e3c9717 4490 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4491 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4492 else
003632d9 4493 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4494
4495 break;
4496 case PIPE_C:
003632d9 4497 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
SV
4498
4499 break;
4500 default:
4501 BUG();
4502 }
4503}
4504
c48b5305
VS
4505/* Return which DP Port should be selected for Transcoder DP control */
4506static enum port
4507intel_trans_dp_port_sel(struct drm_crtc *crtc)
4508{
4509 struct drm_device *dev = crtc->dev;
4510 struct intel_encoder *encoder;
4511
4512 for_each_encoder_on_crtc(dev, crtc, encoder) {
cca0502b 4513 if (encoder->type == INTEL_OUTPUT_DP ||
c48b5305
VS
4514 encoder->type == INTEL_OUTPUT_EDP)
4515 return enc_to_dig_port(&encoder->base)->port;
4516 }
4517
4518 return -1;
4519}
4520
f67a559d
JB
4521/*
4522 * Enable PCH resources required for PCH ports:
4523 * - PCH PLLs
4524 * - FDI training & RX/TX
4525 * - update transcoder timings
4526 * - DP transcoding bits
4527 * - transcoder
4528 */
4529static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4530{
4531 struct drm_device *dev = crtc->dev;
fac5e23e 4532 struct drm_i915_private *dev_priv = to_i915(dev);
0e23b99d
JB
4533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4534 int pipe = intel_crtc->pipe;
f0f59a00 4535 u32 temp;
2c07245f 4536
ab9412ba 4537 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4538
fd6b8f43 4539 if (IS_IVYBRIDGE(dev_priv))
1fbc0d78
SV
4540 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4541
cd986abb
SV
4542 /* Write the TU size bits before fdi link training, so that error
4543 * detection works. */
4544 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4545 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4546
c98e9dcf 4547 /* For PCH output, training FDI link */
674cf967 4548 dev_priv->display.fdi_link_train(crtc);
2c07245f 4549
3ad8a208
SV
4550 /* We need to program the right clock selection before writing the pixel
4551 * mutliplier into the DPLL. */
6e266956 4552 if (HAS_PCH_CPT(dev_priv)) {
ee7b9f93 4553 u32 sel;
4b645f14 4554
c98e9dcf 4555 temp = I915_READ(PCH_DPLL_SEL);
11887397
SV
4556 temp |= TRANS_DPLL_ENABLE(pipe);
4557 sel = TRANS_DPLLB_SEL(pipe);
8106ddbd
ACO
4558 if (intel_crtc->config->shared_dpll ==
4559 intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
ee7b9f93
JB
4560 temp |= sel;
4561 else
4562 temp &= ~sel;
c98e9dcf 4563 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4564 }
5eddb70b 4565
3ad8a208
SV
4566 /* XXX: pch pll's can be enabled any time before we enable the PCH
4567 * transcoder, and we actually should do this to not upset any PCH
4568 * transcoder that already use the clock when we share it.
4569 *
4570 * Note that enable_shared_dpll tries to do the right thing, but
4571 * get_shared_dpll unconditionally resets the pll - we need that to have
4572 * the right LVDS enable sequence. */
85b3894f 4573 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4574
d9b6cb56
JB
4575 /* set transcoder timing, panel must allow it */
4576 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4577 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4578
303b81e0 4579 intel_fdi_normal_train(crtc);
5e84e1a4 4580
c98e9dcf 4581 /* For PCH DP, enable TRANS_DP_CTL */
6e266956
TU
4582 if (HAS_PCH_CPT(dev_priv) &&
4583 intel_crtc_has_dp_encoder(intel_crtc->config)) {
9c4edaee
VS
4584 const struct drm_display_mode *adjusted_mode =
4585 &intel_crtc->config->base.adjusted_mode;
dfd07d72 4586 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
f0f59a00 4587 i915_reg_t reg = TRANS_DP_CTL(pipe);
5eddb70b
CW
4588 temp = I915_READ(reg);
4589 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4590 TRANS_DP_SYNC_MASK |
4591 TRANS_DP_BPC_MASK);
e3ef4479 4592 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4593 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf 4594
9c4edaee 4595 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4596 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
9c4edaee 4597 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4598 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4599
4600 switch (intel_trans_dp_port_sel(crtc)) {
c48b5305 4601 case PORT_B:
5eddb70b 4602 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf 4603 break;
c48b5305 4604 case PORT_C:
5eddb70b 4605 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf 4606 break;
c48b5305 4607 case PORT_D:
5eddb70b 4608 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4609 break;
4610 default:
e95d41e1 4611 BUG();
32f9d658 4612 }
2c07245f 4613
5eddb70b 4614 I915_WRITE(reg, temp);
6be4a607 4615 }
b52eb4dc 4616
b8a4f404 4617 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4618}
4619
1507e5bd
PZ
4620static void lpt_pch_enable(struct drm_crtc *crtc)
4621{
4622 struct drm_device *dev = crtc->dev;
fac5e23e 4623 struct drm_i915_private *dev_priv = to_i915(dev);
1507e5bd 4624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4625 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4626
ab9412ba 4627 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4628
8c52b5e8 4629 lpt_program_iclkip(crtc);
1507e5bd 4630
0540e488 4631 /* Set transcoder timing. */
275f01b2 4632 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4633
937bb610 4634 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4635}
4636
a1520318 4637static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57 4638{
fac5e23e 4639 struct drm_i915_private *dev_priv = to_i915(dev);
f0f59a00 4640 i915_reg_t dslreg = PIPEDSL(pipe);
d4270e57
JB
4641 u32 temp;
4642
4643 temp = I915_READ(dslreg);
4644 udelay(500);
4645 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4646 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4647 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4648 }
4649}
4650
86adf9d7
ML
4651static int
4652skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4653 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4654 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4655{
86adf9d7
ML
4656 struct intel_crtc_scaler_state *scaler_state =
4657 &crtc_state->scaler_state;
4658 struct intel_crtc *intel_crtc =
4659 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4660 int need_scaling;
6156a456 4661
bd2ef25d 4662 need_scaling = drm_rotation_90_or_270(rotation) ?
6156a456
CK
4663 (src_h != dst_w || src_w != dst_h):
4664 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4665
4666 /*
4667 * if plane is being disabled or scaler is no more required or force detach
4668 * - free scaler binded to this plane/crtc
4669 * - in order to do this, update crtc->scaler_usage
4670 *
4671 * Here scaler state in crtc_state is set free so that
4672 * scaler can be assigned to other user. Actual register
4673 * update to free the scaler is done in plane/panel-fit programming.
4674 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4675 */
86adf9d7 4676 if (force_detach || !need_scaling) {
a1b2278e 4677 if (*scaler_id >= 0) {
86adf9d7 4678 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4679 scaler_state->scalers[*scaler_id].in_use = 0;
4680
86adf9d7
ML
4681 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4682 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4683 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4684 scaler_state->scaler_users);
4685 *scaler_id = -1;
4686 }
4687 return 0;
4688 }
4689
4690 /* range checks */
4691 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4692 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4693
4694 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4695 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4696 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4697 "size is out of scaler range\n",
86adf9d7 4698 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4699 return -EINVAL;
4700 }
4701
86adf9d7
ML
4702 /* mark this plane as a scaler user in crtc_state */
4703 scaler_state->scaler_users |= (1 << scaler_user);
4704 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4705 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4706 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4707 scaler_state->scaler_users);
4708
4709 return 0;
4710}
4711
4712/**
4713 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4714 *
4715 * @state: crtc's scaler state
86adf9d7
ML
4716 *
4717 * Return
4718 * 0 - scaler_usage updated successfully
4719 * error - requested scaling cannot be supported or other error condition
4720 */
e435d6e5 4721int skl_update_scaler_crtc(struct intel_crtc_state *state)
86adf9d7 4722{
7c5f93b0 4723 const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
86adf9d7 4724
e435d6e5 4725 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
31ad61e4 4726 &state->scaler_state.scaler_id, DRM_ROTATE_0,
86adf9d7 4727 state->pipe_src_w, state->pipe_src_h,
aad941d5 4728 adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
86adf9d7
ML
4729}
4730
4731/**
4732 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4733 *
4734 * @state: crtc's scaler state
86adf9d7
ML
4735 * @plane_state: atomic plane state to update
4736 *
4737 * Return
4738 * 0 - scaler_usage updated successfully
4739 * error - requested scaling cannot be supported or other error condition
4740 */
da20eabd
ML
4741static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4742 struct intel_plane_state *plane_state)
86adf9d7
ML
4743{
4744
da20eabd
ML
4745 struct intel_plane *intel_plane =
4746 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4747 struct drm_framebuffer *fb = plane_state->base.fb;
4748 int ret;
4749
936e71e3 4750 bool force_detach = !fb || !plane_state->base.visible;
86adf9d7 4751
86adf9d7
ML
4752 ret = skl_update_scaler(crtc_state, force_detach,
4753 drm_plane_index(&intel_plane->base),
4754 &plane_state->scaler_id,
4755 plane_state->base.rotation,
936e71e3
VS
4756 drm_rect_width(&plane_state->base.src) >> 16,
4757 drm_rect_height(&plane_state->base.src) >> 16,
4758 drm_rect_width(&plane_state->base.dst),
4759 drm_rect_height(&plane_state->base.dst));
86adf9d7
ML
4760
4761 if (ret || plane_state->scaler_id < 0)
4762 return ret;
4763
a1b2278e 4764 /* check colorkey */
818ed961 4765 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
72660ce0
VS
4766 DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
4767 intel_plane->base.base.id,
4768 intel_plane->base.name);
a1b2278e
CK
4769 return -EINVAL;
4770 }
4771
4772 /* Check src format */
86adf9d7
ML
4773 switch (fb->pixel_format) {
4774 case DRM_FORMAT_RGB565:
4775 case DRM_FORMAT_XBGR8888:
4776 case DRM_FORMAT_XRGB8888:
4777 case DRM_FORMAT_ABGR8888:
4778 case DRM_FORMAT_ARGB8888:
4779 case DRM_FORMAT_XRGB2101010:
4780 case DRM_FORMAT_XBGR2101010:
4781 case DRM_FORMAT_YUYV:
4782 case DRM_FORMAT_YVYU:
4783 case DRM_FORMAT_UYVY:
4784 case DRM_FORMAT_VYUY:
4785 break;
4786 default:
72660ce0
VS
4787 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
4788 intel_plane->base.base.id, intel_plane->base.name,
4789 fb->base.id, fb->pixel_format);
86adf9d7 4790 return -EINVAL;
a1b2278e
CK
4791 }
4792
a1b2278e
CK
4793 return 0;
4794}
4795
e435d6e5
ML
4796static void skylake_scaler_disable(struct intel_crtc *crtc)
4797{
4798 int i;
4799
4800 for (i = 0; i < crtc->num_scalers; i++)
4801 skl_detach_scaler(crtc, i);
4802}
4803
4804static void skylake_pfit_enable(struct intel_crtc *crtc)
bd2e244f
JB
4805{
4806 struct drm_device *dev = crtc->base.dev;
fac5e23e 4807 struct drm_i915_private *dev_priv = to_i915(dev);
bd2e244f 4808 int pipe = crtc->pipe;
a1b2278e
CK
4809 struct intel_crtc_scaler_state *scaler_state =
4810 &crtc->config->scaler_state;
4811
4812 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4813
6e3c9717 4814 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4815 int id;
4816
4817 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4818 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4819 return;
4820 }
4821
4822 id = scaler_state->scaler_id;
4823 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4824 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4825 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4826 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4827
4828 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4829 }
4830}
4831
b074cec8
JB
4832static void ironlake_pfit_enable(struct intel_crtc *crtc)
4833{
4834 struct drm_device *dev = crtc->base.dev;
fac5e23e 4835 struct drm_i915_private *dev_priv = to_i915(dev);
b074cec8
JB
4836 int pipe = crtc->pipe;
4837
6e3c9717 4838 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4839 /* Force use of hard-coded filter coefficients
4840 * as some pre-programmed values are broken,
4841 * e.g. x201.
4842 */
fd6b8f43 4843 if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
b074cec8
JB
4844 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4845 PF_PIPE_SEL_IVB(pipe));
4846 else
4847 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4848 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4849 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4850 }
4851}
4852
20bc8673 4853void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4854{
cea165c3 4855 struct drm_device *dev = crtc->base.dev;
fac5e23e 4856 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4857
6e3c9717 4858 if (!crtc->config->ips_enabled)
d77e4531
PZ
4859 return;
4860
307e4498
ML
4861 /*
4862 * We can only enable IPS after we enable a plane and wait for a vblank
4863 * This function is called from post_plane_update, which is run after
4864 * a vblank wait.
4865 */
cea165c3 4866
d77e4531 4867 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4868 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4869 mutex_lock(&dev_priv->rps.hw_lock);
4870 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4871 mutex_unlock(&dev_priv->rps.hw_lock);
4872 /* Quoting Art Runyan: "its not safe to expect any particular
4873 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4874 * mailbox." Moreover, the mailbox may return a bogus state,
4875 * so we need to just enable it and continue on.
2a114cc1
BW
4876 */
4877 } else {
4878 I915_WRITE(IPS_CTL, IPS_ENABLE);
4879 /* The bit only becomes 1 in the next vblank, so this wait here
4880 * is essentially intel_wait_for_vblank. If we don't have this
4881 * and don't wait for vblanks until the end of crtc_enable, then
4882 * the HW state readout code will complain that the expected
4883 * IPS_CTL value is not the one we read. */
2ec9ba3c
CW
4884 if (intel_wait_for_register(dev_priv,
4885 IPS_CTL, IPS_ENABLE, IPS_ENABLE,
4886 50))
2a114cc1
BW
4887 DRM_ERROR("Timed out waiting for IPS enable\n");
4888 }
d77e4531
PZ
4889}
4890
20bc8673 4891void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4892{
4893 struct drm_device *dev = crtc->base.dev;
fac5e23e 4894 struct drm_i915_private *dev_priv = to_i915(dev);
d77e4531 4895
6e3c9717 4896 if (!crtc->config->ips_enabled)
d77e4531
PZ
4897 return;
4898
4899 assert_plane_enabled(dev_priv, crtc->plane);
8652744b 4900 if (IS_BROADWELL(dev_priv)) {
2a114cc1
BW
4901 mutex_lock(&dev_priv->rps.hw_lock);
4902 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4903 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130 4904 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
b85c1ecf
CW
4905 if (intel_wait_for_register(dev_priv,
4906 IPS_CTL, IPS_ENABLE, 0,
4907 42))
23d0b130 4908 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4909 } else {
2a114cc1 4910 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4911 POSTING_READ(IPS_CTL);
4912 }
d77e4531
PZ
4913
4914 /* We need to wait for a vblank before we can disable the plane. */
0f0f74bc 4915 intel_wait_for_vblank(dev_priv, crtc->pipe);
d77e4531
PZ
4916}
4917
7cac945f 4918static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4919{
7cac945f 4920 if (intel_crtc->overlay) {
d3eedb1a 4921 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 4922 struct drm_i915_private *dev_priv = to_i915(dev);
d3eedb1a
VS
4923
4924 mutex_lock(&dev->struct_mutex);
4925 dev_priv->mm.interruptible = false;
4926 (void) intel_overlay_switch_off(intel_crtc->overlay);
4927 dev_priv->mm.interruptible = true;
4928 mutex_unlock(&dev->struct_mutex);
4929 }
4930
4931 /* Let userspace switch the overlay on again. In most cases userspace
4932 * has to recompute where to put it anyway.
4933 */
4934}
4935
87d4300a
ML
4936/**
4937 * intel_post_enable_primary - Perform operations after enabling primary plane
4938 * @crtc: the CRTC whose primary plane was just enabled
4939 *
4940 * Performs potentially sleeping operations that must be done after the primary
4941 * plane is enabled, such as updating FBC and IPS. Note that this may be
4942 * called due to an explicit primary plane update, or due to an implicit
4943 * re-enable that is caused when a sprite plane is updated to no longer
4944 * completely hide the primary plane.
4945 */
4946static void
4947intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4948{
4949 struct drm_device *dev = crtc->dev;
fac5e23e 4950 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4952 int pipe = intel_crtc->pipe;
a5c4d7bc 4953
87d4300a
ML
4954 /*
4955 * FIXME IPS should be fine as long as one plane is
4956 * enabled, but in practice it seems to have problems
4957 * when going from primary only to sprite only and vice
4958 * versa.
4959 */
a5c4d7bc
VS
4960 hsw_enable_ips(intel_crtc);
4961
f99d7069 4962 /*
87d4300a
ML
4963 * Gen2 reports pipe underruns whenever all planes are disabled.
4964 * So don't enable underrun reporting before at least some planes
4965 * are enabled.
4966 * FIXME: Need to fix the logic to work when we turn off all planes
4967 * but leave the pipe running.
f99d7069 4968 */
5db94019 4969 if (IS_GEN2(dev_priv))
87d4300a
ML
4970 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4971
aca7b684
VS
4972 /* Underruns don't always raise interrupts, so check manually. */
4973 intel_check_cpu_fifo_underruns(dev_priv);
4974 intel_check_pch_fifo_underruns(dev_priv);
a5c4d7bc
VS
4975}
4976
2622a081 4977/* FIXME move all this to pre_plane_update() with proper state tracking */
87d4300a
ML
4978static void
4979intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4980{
4981 struct drm_device *dev = crtc->dev;
fac5e23e 4982 struct drm_i915_private *dev_priv = to_i915(dev);
a5c4d7bc
VS
4983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 int pipe = intel_crtc->pipe;
a5c4d7bc 4985
87d4300a
ML
4986 /*
4987 * Gen2 reports pipe underruns whenever all planes are disabled.
4988 * So diasble underrun reporting before all the planes get disabled.
4989 * FIXME: Need to fix the logic to work when we turn off all planes
4990 * but leave the pipe running.
4991 */
5db94019 4992 if (IS_GEN2(dev_priv))
87d4300a 4993 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4994
2622a081
VS
4995 /*
4996 * FIXME IPS should be fine as long as one plane is
4997 * enabled, but in practice it seems to have problems
4998 * when going from primary only to sprite only and vice
4999 * versa.
5000 */
5001 hsw_disable_ips(intel_crtc);
5002}
5003
5004/* FIXME get rid of this and use pre_plane_update */
5005static void
5006intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
5007{
5008 struct drm_device *dev = crtc->dev;
fac5e23e 5009 struct drm_i915_private *dev_priv = to_i915(dev);
2622a081
VS
5010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5011 int pipe = intel_crtc->pipe;
5012
5013 intel_pre_disable_primary(crtc);
5014
87d4300a
ML
5015 /*
5016 * Vblank time updates from the shadow to live plane control register
5017 * are blocked if the memory self-refresh mode is active at that
5018 * moment. So to make sure the plane gets truly disabled, disable
5019 * first the self-refresh mode. The self-refresh enable bit in turn
5020 * will be checked/applied by the HW only at the next frame start
5021 * event which is after the vblank start event, so we need to have a
5022 * wait-for-vblank between disabling the plane and the pipe.
5023 */
49cff963 5024 if (HAS_GMCH_DISPLAY(dev_priv)) {
87d4300a 5025 intel_set_memory_cxsr(dev_priv, false);
262cd2e1 5026 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5027 intel_wait_for_vblank(dev_priv, pipe);
262cd2e1 5028 }
87d4300a
ML
5029}
5030
5a21b665
SV
5031static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
5032{
5033 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
5034 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5035 struct intel_crtc_state *pipe_config =
5036 to_intel_crtc_state(crtc->base.state);
5a21b665
SV
5037 struct drm_plane *primary = crtc->base.primary;
5038 struct drm_plane_state *old_pri_state =
5039 drm_atomic_get_existing_plane_state(old_state, primary);
5040
5748b6a1 5041 intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
5a21b665
SV
5042
5043 crtc->wm.cxsr_allowed = true;
5044
5045 if (pipe_config->update_wm_post && pipe_config->base.active)
432081bc 5046 intel_update_watermarks(crtc);
5a21b665
SV
5047
5048 if (old_pri_state) {
5049 struct intel_plane_state *primary_state =
5050 to_intel_plane_state(primary->state);
5051 struct intel_plane_state *old_primary_state =
5052 to_intel_plane_state(old_pri_state);
5053
5054 intel_fbc_post_update(crtc);
5055
936e71e3 5056 if (primary_state->base.visible &&
5a21b665 5057 (needs_modeset(&pipe_config->base) ||
936e71e3 5058 !old_primary_state->base.visible))
5a21b665
SV
5059 intel_post_enable_primary(&crtc->base);
5060 }
5061}
5062
5c74cd73 5063static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
ac21b225 5064{
5c74cd73 5065 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
ac21b225 5066 struct drm_device *dev = crtc->base.dev;
fac5e23e 5067 struct drm_i915_private *dev_priv = to_i915(dev);
ab1d3a0e
ML
5068 struct intel_crtc_state *pipe_config =
5069 to_intel_crtc_state(crtc->base.state);
5c74cd73
ML
5070 struct drm_atomic_state *old_state = old_crtc_state->base.state;
5071 struct drm_plane *primary = crtc->base.primary;
5072 struct drm_plane_state *old_pri_state =
5073 drm_atomic_get_existing_plane_state(old_state, primary);
5074 bool modeset = needs_modeset(&pipe_config->base);
ccf010fb
ML
5075 struct intel_atomic_state *old_intel_state =
5076 to_intel_atomic_state(old_state);
ac21b225 5077
5c74cd73
ML
5078 if (old_pri_state) {
5079 struct intel_plane_state *primary_state =
5080 to_intel_plane_state(primary->state);
5081 struct intel_plane_state *old_primary_state =
5082 to_intel_plane_state(old_pri_state);
5083
faf68d92 5084 intel_fbc_pre_update(crtc, pipe_config, primary_state);
31ae71fc 5085
936e71e3
VS
5086 if (old_primary_state->base.visible &&
5087 (modeset || !primary_state->base.visible))
5c74cd73
ML
5088 intel_pre_disable_primary(&crtc->base);
5089 }
852eb00d 5090
49cff963 5091 if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
852eb00d 5092 crtc->wm.cxsr_allowed = false;
2dfd178d 5093
2622a081
VS
5094 /*
5095 * Vblank time updates from the shadow to live plane control register
5096 * are blocked if the memory self-refresh mode is active at that
5097 * moment. So to make sure the plane gets truly disabled, disable
5098 * first the self-refresh mode. The self-refresh enable bit in turn
5099 * will be checked/applied by the HW only at the next frame start
5100 * event which is after the vblank start event, so we need to have a
5101 * wait-for-vblank between disabling the plane and the pipe.
5102 */
5103 if (old_crtc_state->base.active) {
2dfd178d 5104 intel_set_memory_cxsr(dev_priv, false);
2622a081 5105 dev_priv->wm.vlv.cxsr = false;
0f0f74bc 5106 intel_wait_for_vblank(dev_priv, crtc->pipe);
2622a081 5107 }
852eb00d 5108 }
92826fcd 5109
ed4a6a7c
MR
5110 /*
5111 * IVB workaround: must disable low power watermarks for at least
5112 * one frame before enabling scaling. LP watermarks can be re-enabled
5113 * when scaling is disabled.
5114 *
5115 * WaCxSRDisabledForSpriteScaling:ivb
5116 */
5117 if (pipe_config->disable_lp_wm) {
5118 ilk_disable_lp_wm(dev);
0f0f74bc 5119 intel_wait_for_vblank(dev_priv, crtc->pipe);
ed4a6a7c
MR
5120 }
5121
5122 /*
5123 * If we're doing a modeset, we're done. No need to do any pre-vblank
5124 * watermark programming here.
5125 */
5126 if (needs_modeset(&pipe_config->base))
5127 return;
5128
5129 /*
5130 * For platforms that support atomic watermarks, program the
5131 * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
5132 * will be the intermediate values that are safe for both pre- and
5133 * post- vblank; when vblank happens, the 'active' values will be set
5134 * to the final 'target' values and we'll do this again to get the
5135 * optimal watermarks. For gen9+ platforms, the values we program here
5136 * will be the final target values which will get automatically latched
5137 * at vblank time; no further programming will be necessary.
5138 *
5139 * If a platform hasn't been transitioned to atomic watermarks yet,
5140 * we'll continue to update watermarks the old way, if flags tell
5141 * us to.
5142 */
5143 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5144 dev_priv->display.initial_watermarks(old_intel_state,
5145 pipe_config);
caed361d 5146 else if (pipe_config->update_wm_pre)
432081bc 5147 intel_update_watermarks(crtc);
ac21b225
ML
5148}
5149
d032ffa0 5150static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
5151{
5152 struct drm_device *dev = crtc->dev;
5153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 5154 struct drm_plane *p;
87d4300a
ML
5155 int pipe = intel_crtc->pipe;
5156
7cac945f 5157 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 5158
d032ffa0
ML
5159 drm_for_each_plane_mask(p, dev, plane_mask)
5160 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 5161
f99d7069
SV
5162 /*
5163 * FIXME: Once we grow proper nuclear flip support out of this we need
5164 * to compute the mask of flip planes precisely. For the time being
5165 * consider this a flip to a NULL plane.
5166 */
5748b6a1 5167 intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
5168}
5169
fb1c98b1 5170static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
fd6bbda9 5171 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5172 struct drm_atomic_state *old_state)
5173{
5174 struct drm_connector_state *old_conn_state;
5175 struct drm_connector *conn;
5176 int i;
5177
5178 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5179 struct drm_connector_state *conn_state = conn->state;
5180 struct intel_encoder *encoder =
5181 to_intel_encoder(conn_state->best_encoder);
5182
5183 if (conn_state->crtc != crtc)
5184 continue;
5185
5186 if (encoder->pre_pll_enable)
fd6bbda9 5187 encoder->pre_pll_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5188 }
5189}
5190
5191static void intel_encoders_pre_enable(struct drm_crtc *crtc,
fd6bbda9 5192 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5193 struct drm_atomic_state *old_state)
5194{
5195 struct drm_connector_state *old_conn_state;
5196 struct drm_connector *conn;
5197 int i;
5198
5199 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5200 struct drm_connector_state *conn_state = conn->state;
5201 struct intel_encoder *encoder =
5202 to_intel_encoder(conn_state->best_encoder);
5203
5204 if (conn_state->crtc != crtc)
5205 continue;
5206
5207 if (encoder->pre_enable)
fd6bbda9 5208 encoder->pre_enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5209 }
5210}
5211
5212static void intel_encoders_enable(struct drm_crtc *crtc,
fd6bbda9 5213 struct intel_crtc_state *crtc_state,
fb1c98b1
ML
5214 struct drm_atomic_state *old_state)
5215{
5216 struct drm_connector_state *old_conn_state;
5217 struct drm_connector *conn;
5218 int i;
5219
5220 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5221 struct drm_connector_state *conn_state = conn->state;
5222 struct intel_encoder *encoder =
5223 to_intel_encoder(conn_state->best_encoder);
5224
5225 if (conn_state->crtc != crtc)
5226 continue;
5227
fd6bbda9 5228 encoder->enable(encoder, crtc_state, conn_state);
fb1c98b1
ML
5229 intel_opregion_notify_encoder(encoder, true);
5230 }
5231}
5232
5233static void intel_encoders_disable(struct drm_crtc *crtc,
fd6bbda9 5234 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5235 struct drm_atomic_state *old_state)
5236{
5237 struct drm_connector_state *old_conn_state;
5238 struct drm_connector *conn;
5239 int i;
5240
5241 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5242 struct intel_encoder *encoder =
5243 to_intel_encoder(old_conn_state->best_encoder);
5244
5245 if (old_conn_state->crtc != crtc)
5246 continue;
5247
5248 intel_opregion_notify_encoder(encoder, false);
fd6bbda9 5249 encoder->disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5250 }
5251}
5252
5253static void intel_encoders_post_disable(struct drm_crtc *crtc,
fd6bbda9 5254 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5255 struct drm_atomic_state *old_state)
5256{
5257 struct drm_connector_state *old_conn_state;
5258 struct drm_connector *conn;
5259 int i;
5260
5261 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5262 struct intel_encoder *encoder =
5263 to_intel_encoder(old_conn_state->best_encoder);
5264
5265 if (old_conn_state->crtc != crtc)
5266 continue;
5267
5268 if (encoder->post_disable)
fd6bbda9 5269 encoder->post_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5270 }
5271}
5272
5273static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
fd6bbda9 5274 struct intel_crtc_state *old_crtc_state,
fb1c98b1
ML
5275 struct drm_atomic_state *old_state)
5276{
5277 struct drm_connector_state *old_conn_state;
5278 struct drm_connector *conn;
5279 int i;
5280
5281 for_each_connector_in_state(old_state, conn, old_conn_state, i) {
5282 struct intel_encoder *encoder =
5283 to_intel_encoder(old_conn_state->best_encoder);
5284
5285 if (old_conn_state->crtc != crtc)
5286 continue;
5287
5288 if (encoder->post_pll_disable)
fd6bbda9 5289 encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
fb1c98b1
ML
5290 }
5291}
5292
4a806558
ML
5293static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
5294 struct drm_atomic_state *old_state)
f67a559d 5295{
4a806558 5296 struct drm_crtc *crtc = pipe_config->base.crtc;
f67a559d 5297 struct drm_device *dev = crtc->dev;
fac5e23e 5298 struct drm_i915_private *dev_priv = to_i915(dev);
f67a559d
JB
5299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
ccf010fb
ML
5301 struct intel_atomic_state *old_intel_state =
5302 to_intel_atomic_state(old_state);
f67a559d 5303
53d9f4e9 5304 if (WARN_ON(intel_crtc->active))
f67a559d
JB
5305 return;
5306
b2c0593a
VS
5307 /*
5308 * Sometimes spurious CPU pipe underruns happen during FDI
5309 * training, at least with VGA+HDMI cloning. Suppress them.
5310 *
5311 * On ILK we get an occasional spurious CPU pipe underruns
5312 * between eDP port A enable and vdd enable. Also PCH port
5313 * enable seems to result in the occasional CPU pipe underrun.
5314 *
5315 * Spurious PCH underruns also occur during PCH enabling.
5316 */
5317 if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
5318 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
81b088ca
VS
5319 if (intel_crtc->config->has_pch_encoder)
5320 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5321
6e3c9717 5322 if (intel_crtc->config->has_pch_encoder)
b14b1055
SV
5323 intel_prepare_shared_dpll(intel_crtc);
5324
37a5650b 5325 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5326 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
SV
5327
5328 intel_set_pipe_timings(intel_crtc);
bc58be60 5329 intel_set_pipe_src_size(intel_crtc);
29407aab 5330
6e3c9717 5331 if (intel_crtc->config->has_pch_encoder) {
29407aab 5332 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5333 &intel_crtc->config->fdi_m_n, NULL);
29407aab
SV
5334 }
5335
5336 ironlake_set_pipeconf(crtc);
5337
f67a559d 5338 intel_crtc->active = true;
8664281b 5339
fd6bbda9 5340 intel_encoders_pre_enable(crtc, pipe_config, old_state);
f67a559d 5341
6e3c9717 5342 if (intel_crtc->config->has_pch_encoder) {
fff367c7
SV
5343 /* Note: FDI PLL enabling _must_ be done before we enable the
5344 * cpu pipes, hence this is separate from all the other fdi/pch
5345 * enabling. */
88cefb6c 5346 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
SV
5347 } else {
5348 assert_fdi_tx_disabled(dev_priv, pipe);
5349 assert_fdi_rx_disabled(dev_priv, pipe);
5350 }
f67a559d 5351
b074cec8 5352 ironlake_pfit_enable(intel_crtc);
f67a559d 5353
9c54c0dd
JB
5354 /*
5355 * On ILK+ LUT must be loaded before the pipe is running but with
5356 * clocks enabled
5357 */
b95c5321 5358 intel_color_load_luts(&pipe_config->base);
9c54c0dd 5359
1d5bf5d9 5360 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb 5361 dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
e1fdc473 5362 intel_enable_pipe(intel_crtc);
f67a559d 5363
6e3c9717 5364 if (intel_crtc->config->has_pch_encoder)
f67a559d 5365 ironlake_pch_enable(crtc);
c98e9dcf 5366
f9b61ff6
SV
5367 assert_vblank_disabled(crtc);
5368 drm_crtc_vblank_on(crtc);
5369
fd6bbda9 5370 intel_encoders_enable(crtc, pipe_config, old_state);
61b77ddd 5371
6e266956 5372 if (HAS_PCH_CPT(dev_priv))
a1520318 5373 cpt_verify_modeset(dev, intel_crtc->pipe);
37ca8d4c
VS
5374
5375 /* Must wait for vblank to avoid spurious PCH FIFO underruns */
5376 if (intel_crtc->config->has_pch_encoder)
0f0f74bc 5377 intel_wait_for_vblank(dev_priv, pipe);
b2c0593a 5378 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
37ca8d4c 5379 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607
JB
5380}
5381
42db64ef
PZ
5382/* IPS only exists on ULT machines and is tied to pipe A. */
5383static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
5384{
50a0bc90 5385 return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
42db64ef
PZ
5386}
5387
4a806558
ML
5388static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
5389 struct drm_atomic_state *old_state)
4f771f10 5390{
4a806558 5391 struct drm_crtc *crtc = pipe_config->base.crtc;
6315b5d3 5392 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
4f771f10 5393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99d736a2 5394 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4d1de975 5395 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ccf010fb
ML
5396 struct intel_atomic_state *old_intel_state =
5397 to_intel_atomic_state(old_state);
4f771f10 5398
53d9f4e9 5399 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
5400 return;
5401
81b088ca
VS
5402 if (intel_crtc->config->has_pch_encoder)
5403 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5404 false);
5405
fd6bbda9 5406 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
95a7a2ae 5407
8106ddbd 5408 if (intel_crtc->config->shared_dpll)
df8ad70c
SV
5409 intel_enable_shared_dpll(intel_crtc);
5410
37a5650b 5411 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 5412 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97 5413
d7edc4e5 5414 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5415 intel_set_pipe_timings(intel_crtc);
5416
bc58be60 5417 intel_set_pipe_src_size(intel_crtc);
229fca97 5418
4d1de975
JN
5419 if (cpu_transcoder != TRANSCODER_EDP &&
5420 !transcoder_is_dsi(cpu_transcoder)) {
5421 I915_WRITE(PIPE_MULT(cpu_transcoder),
6e3c9717 5422 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5423 }
5424
6e3c9717 5425 if (intel_crtc->config->has_pch_encoder) {
229fca97 5426 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5427 &intel_crtc->config->fdi_m_n, NULL);
229fca97
SV
5428 }
5429
d7edc4e5 5430 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975
JN
5431 haswell_set_pipeconf(crtc);
5432
391bf048 5433 haswell_set_pipemisc(crtc);
229fca97 5434
b95c5321 5435 intel_color_set_csc(&pipe_config->base);
229fca97 5436
4f771f10 5437 intel_crtc->active = true;
8664281b 5438
6b698516
SV
5439 if (intel_crtc->config->has_pch_encoder)
5440 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5441 else
5442 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5443
fd6bbda9 5444 intel_encoders_pre_enable(crtc, pipe_config, old_state);
4f771f10 5445
d2d65408 5446 if (intel_crtc->config->has_pch_encoder)
4fe9467d 5447 dev_priv->display.fdi_link_train(crtc);
4fe9467d 5448
d7edc4e5 5449 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5450 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5451
6315b5d3 5452 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5453 skylake_pfit_enable(intel_crtc);
ff6d9f55 5454 else
1c132b44 5455 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
5456
5457 /*
5458 * On ILK+ LUT must be loaded before the pipe is running but with
5459 * clocks enabled
5460 */
b95c5321 5461 intel_color_load_luts(&pipe_config->base);
4f771f10 5462
1f544388 5463 intel_ddi_set_pipe_settings(crtc);
d7edc4e5 5464 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5465 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5466
1d5bf5d9 5467 if (dev_priv->display.initial_watermarks != NULL)
ccf010fb
ML
5468 dev_priv->display.initial_watermarks(old_intel_state,
5469 pipe_config);
1d5bf5d9 5470 else
432081bc 5471 intel_update_watermarks(intel_crtc);
4d1de975
JN
5472
5473 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5474 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5475 intel_enable_pipe(intel_crtc);
42db64ef 5476
6e3c9717 5477 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5478 lpt_pch_enable(crtc);
4f771f10 5479
0037071d 5480 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
0e32b39c
DA
5481 intel_ddi_set_vc_payload_alloc(crtc, true);
5482
f9b61ff6
SV
5483 assert_vblank_disabled(crtc);
5484 drm_crtc_vblank_on(crtc);
5485
fd6bbda9 5486 intel_encoders_enable(crtc, pipe_config, old_state);
4f771f10 5487
6b698516 5488 if (intel_crtc->config->has_pch_encoder) {
0f0f74bc
VS
5489 intel_wait_for_vblank(dev_priv, pipe);
5490 intel_wait_for_vblank(dev_priv, pipe);
6b698516 5491 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
d2d65408
VS
5492 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5493 true);
6b698516 5494 }
d2d65408 5495
e4916946
PZ
5496 /* If we change the relative order between pipe/planes enabling, we need
5497 * to change the workaround. */
99d736a2 5498 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
772c2a51 5499 if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
0f0f74bc
VS
5500 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
5501 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
99d736a2 5502 }
4f771f10
PZ
5503}
5504
bfd16b2a 5505static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
3f8dce3a
SV
5506{
5507 struct drm_device *dev = crtc->base.dev;
fac5e23e 5508 struct drm_i915_private *dev_priv = to_i915(dev);
3f8dce3a
SV
5509 int pipe = crtc->pipe;
5510
5511 /* To avoid upsetting the power well on haswell only disable the pfit if
5512 * it's in use. The hw state code will make sure we get this right. */
bfd16b2a 5513 if (force || crtc->config->pch_pfit.enabled) {
3f8dce3a
SV
5514 I915_WRITE(PF_CTL(pipe), 0);
5515 I915_WRITE(PF_WIN_POS(pipe), 0);
5516 I915_WRITE(PF_WIN_SZ(pipe), 0);
5517 }
5518}
5519
4a806558
ML
5520static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
5521 struct drm_atomic_state *old_state)
6be4a607 5522{
4a806558 5523 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6be4a607 5524 struct drm_device *dev = crtc->dev;
fac5e23e 5525 struct drm_i915_private *dev_priv = to_i915(dev);
6be4a607
JB
5526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5527 int pipe = intel_crtc->pipe;
b52eb4dc 5528
b2c0593a
VS
5529 /*
5530 * Sometimes spurious CPU pipe underruns happen when the
5531 * pipe is already disabled, but FDI RX/TX is still enabled.
5532 * Happens at least with VGA+HDMI cloning. Suppress them.
5533 */
5534 if (intel_crtc->config->has_pch_encoder) {
5535 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
37ca8d4c 5536 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
b2c0593a 5537 }
37ca8d4c 5538
fd6bbda9 5539 intel_encoders_disable(crtc, old_crtc_state, old_state);
ea9d758d 5540
f9b61ff6
SV
5541 drm_crtc_vblank_off(crtc);
5542 assert_vblank_disabled(crtc);
5543
575f7ab7 5544 intel_disable_pipe(intel_crtc);
32f9d658 5545
bfd16b2a 5546 ironlake_pfit_disable(intel_crtc, false);
2c07245f 5547
b2c0593a 5548 if (intel_crtc->config->has_pch_encoder)
5a74f70a
VS
5549 ironlake_fdi_disable(crtc);
5550
fd6bbda9 5551 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
2c07245f 5552
6e3c9717 5553 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5554 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5555
6e266956 5556 if (HAS_PCH_CPT(dev_priv)) {
f0f59a00
VS
5557 i915_reg_t reg;
5558 u32 temp;
5559
d925c59a
SV
5560 /* disable TRANS_DP_CTL */
5561 reg = TRANS_DP_CTL(pipe);
5562 temp = I915_READ(reg);
5563 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5564 TRANS_DP_PORT_SEL_MASK);
5565 temp |= TRANS_DP_PORT_SEL_NONE;
5566 I915_WRITE(reg, temp);
5567
5568 /* disable DPLL_SEL */
5569 temp = I915_READ(PCH_DPLL_SEL);
11887397 5570 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5571 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5572 }
e3421a18 5573
d925c59a
SV
5574 ironlake_fdi_pll_disable(intel_crtc);
5575 }
81b088ca 5576
b2c0593a 5577 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
81b088ca 5578 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
6be4a607 5579}
1b3c7a47 5580
4a806558
ML
5581static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
5582 struct drm_atomic_state *old_state)
ee7b9f93 5583{
4a806558 5584 struct drm_crtc *crtc = old_crtc_state->base.crtc;
6315b5d3 5585 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee7b9f93 5586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 5587 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5588
d2d65408
VS
5589 if (intel_crtc->config->has_pch_encoder)
5590 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5591 false);
5592
fd6bbda9 5593 intel_encoders_disable(crtc, old_crtc_state, old_state);
4f771f10 5594
f9b61ff6
SV
5595 drm_crtc_vblank_off(crtc);
5596 assert_vblank_disabled(crtc);
5597
4d1de975 5598 /* XXX: Do the pipe assertions at the right place for BXT DSI. */
d7edc4e5 5599 if (!transcoder_is_dsi(cpu_transcoder))
4d1de975 5600 intel_disable_pipe(intel_crtc);
4f771f10 5601
0037071d 5602 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
a4bf214f
VS
5603 intel_ddi_set_vc_payload_alloc(crtc, false);
5604
d7edc4e5 5605 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5606 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5607
6315b5d3 5608 if (INTEL_GEN(dev_priv) >= 9)
e435d6e5 5609 skylake_scaler_disable(intel_crtc);
ff6d9f55 5610 else
bfd16b2a 5611 ironlake_pfit_disable(intel_crtc, false);
4f771f10 5612
d7edc4e5 5613 if (!transcoder_is_dsi(cpu_transcoder))
7d4aefd0 5614 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5615
fd6bbda9 5616 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
81b088ca 5617
b7076546 5618 if (old_crtc_state->has_pch_encoder)
81b088ca
VS
5619 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5620 true);
4f771f10
PZ
5621}
5622
2dd24552
JB
5623static void i9xx_pfit_enable(struct intel_crtc *crtc)
5624{
5625 struct drm_device *dev = crtc->base.dev;
fac5e23e 5626 struct drm_i915_private *dev_priv = to_i915(dev);
6e3c9717 5627 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5628
681a8504 5629 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5630 return;
5631
2dd24552 5632 /*
c0b03411
SV
5633 * The panel fitter should only be adjusted whilst the pipe is disabled,
5634 * according to register description and PRM.
2dd24552 5635 */
c0b03411
SV
5636 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5637 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5638
b074cec8
JB
5639 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5640 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
SV
5641
5642 /* Border color in case we don't scale up to the full screen. Black by
5643 * default, change to something else for debugging. */
5644 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5645}
5646
d05410f9
DA
5647static enum intel_display_power_domain port_to_power_domain(enum port port)
5648{
5649 switch (port) {
5650 case PORT_A:
6331a704 5651 return POWER_DOMAIN_PORT_DDI_A_LANES;
d05410f9 5652 case PORT_B:
6331a704 5653 return POWER_DOMAIN_PORT_DDI_B_LANES;
d05410f9 5654 case PORT_C:
6331a704 5655 return POWER_DOMAIN_PORT_DDI_C_LANES;
d05410f9 5656 case PORT_D:
6331a704 5657 return POWER_DOMAIN_PORT_DDI_D_LANES;
d8e19f99 5658 case PORT_E:
6331a704 5659 return POWER_DOMAIN_PORT_DDI_E_LANES;
d05410f9 5660 default:
b9fec167 5661 MISSING_CASE(port);
d05410f9
DA
5662 return POWER_DOMAIN_PORT_OTHER;
5663 }
5664}
5665
25f78f58
VS
5666static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
5667{
5668 switch (port) {
5669 case PORT_A:
5670 return POWER_DOMAIN_AUX_A;
5671 case PORT_B:
5672 return POWER_DOMAIN_AUX_B;
5673 case PORT_C:
5674 return POWER_DOMAIN_AUX_C;
5675 case PORT_D:
5676 return POWER_DOMAIN_AUX_D;
5677 case PORT_E:
5678 /* FIXME: Check VBT for actual wiring of PORT E */
5679 return POWER_DOMAIN_AUX_D;
5680 default:
b9fec167 5681 MISSING_CASE(port);
25f78f58
VS
5682 return POWER_DOMAIN_AUX_A;
5683 }
5684}
5685
319be8ae
ID
5686enum intel_display_power_domain
5687intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5688{
4f8036a2 5689 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
319be8ae
ID
5690 struct intel_digital_port *intel_dig_port;
5691
5692 switch (intel_encoder->type) {
5693 case INTEL_OUTPUT_UNKNOWN:
5694 /* Only DDI platforms should ever use this output type */
4f8036a2 5695 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5696 case INTEL_OUTPUT_DP:
319be8ae
ID
5697 case INTEL_OUTPUT_HDMI:
5698 case INTEL_OUTPUT_EDP:
5699 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5700 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5701 case INTEL_OUTPUT_DP_MST:
5702 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5703 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5704 case INTEL_OUTPUT_ANALOG:
5705 return POWER_DOMAIN_PORT_CRT;
5706 case INTEL_OUTPUT_DSI:
5707 return POWER_DOMAIN_PORT_DSI;
5708 default:
5709 return POWER_DOMAIN_PORT_OTHER;
5710 }
5711}
5712
25f78f58
VS
5713enum intel_display_power_domain
5714intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
5715{
4f8036a2 5716 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
25f78f58
VS
5717 struct intel_digital_port *intel_dig_port;
5718
5719 switch (intel_encoder->type) {
5720 case INTEL_OUTPUT_UNKNOWN:
651174a4
ID
5721 case INTEL_OUTPUT_HDMI:
5722 /*
5723 * Only DDI platforms should ever use these output types.
5724 * We can get here after the HDMI detect code has already set
5725 * the type of the shared encoder. Since we can't be sure
5726 * what's the status of the given connectors, play safe and
5727 * run the DP detection too.
5728 */
4f8036a2 5729 WARN_ON_ONCE(!HAS_DDI(dev_priv));
cca0502b 5730 case INTEL_OUTPUT_DP:
25f78f58
VS
5731 case INTEL_OUTPUT_EDP:
5732 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5733 return port_to_aux_power_domain(intel_dig_port->port);
5734 case INTEL_OUTPUT_DP_MST:
5735 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5736 return port_to_aux_power_domain(intel_dig_port->port);
5737 default:
b9fec167 5738 MISSING_CASE(intel_encoder->type);
25f78f58
VS
5739 return POWER_DOMAIN_AUX_A;
5740 }
5741}
5742
74bff5f9
ML
5743static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
5744 struct intel_crtc_state *crtc_state)
77d22dca 5745{
319be8ae 5746 struct drm_device *dev = crtc->dev;
74bff5f9 5747 struct drm_encoder *encoder;
319be8ae
ID
5748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5749 enum pipe pipe = intel_crtc->pipe;
77d22dca 5750 unsigned long mask;
74bff5f9 5751 enum transcoder transcoder = crtc_state->cpu_transcoder;
77d22dca 5752
74bff5f9 5753 if (!crtc_state->base.active)
292b990e
ML
5754 return 0;
5755
77d22dca
ID
5756 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5757 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
74bff5f9
ML
5758 if (crtc_state->pch_pfit.enabled ||
5759 crtc_state->pch_pfit.force_thru)
77d22dca
ID
5760 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5761
74bff5f9
ML
5762 drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
5763 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5764
319be8ae 5765 mask |= BIT(intel_display_port_power_domain(intel_encoder));
74bff5f9 5766 }
319be8ae 5767
15e7ec29
ML
5768 if (crtc_state->shared_dpll)
5769 mask |= BIT(POWER_DOMAIN_PLLS);
5770
77d22dca
ID
5771 return mask;
5772}
5773
74bff5f9
ML
5774static unsigned long
5775modeset_get_crtc_power_domains(struct drm_crtc *crtc,
5776 struct intel_crtc_state *crtc_state)
77d22dca 5777{
fac5e23e 5778 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
292b990e
ML
5779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5780 enum intel_display_power_domain domain;
5a21b665 5781 unsigned long domains, new_domains, old_domains;
77d22dca 5782
292b990e 5783 old_domains = intel_crtc->enabled_power_domains;
74bff5f9
ML
5784 intel_crtc->enabled_power_domains = new_domains =
5785 get_crtc_power_domains(crtc, crtc_state);
77d22dca 5786
5a21b665 5787 domains = new_domains & ~old_domains;
292b990e
ML
5788
5789 for_each_power_domain(domain, domains)
5790 intel_display_power_get(dev_priv, domain);
5791
5a21b665 5792 return old_domains & ~new_domains;
292b990e
ML
5793}
5794
5795static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5796 unsigned long domains)
5797{
5798 enum intel_display_power_domain domain;
5799
5800 for_each_power_domain(domain, domains)
5801 intel_display_power_put(dev_priv, domain);
5802}
77d22dca 5803
adafdc6f
MK
5804static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5805{
5806 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5807
09d09386
ACO
5808 if (IS_GEMINILAKE(dev_priv))
5809 return 2 * max_cdclk_freq;
5810 else if (INTEL_INFO(dev_priv)->gen >= 9 ||
5811 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
adafdc6f
MK
5812 return max_cdclk_freq;
5813 else if (IS_CHERRYVIEW(dev_priv))
5814 return max_cdclk_freq*95/100;
5815 else if (INTEL_INFO(dev_priv)->gen < 4)
5816 return 2*max_cdclk_freq*90/100;
5817 else
5818 return max_cdclk_freq*90/100;
5819}
5820
b2045352
VS
5821static int skl_calc_cdclk(int max_pixclk, int vco);
5822
4c75b940 5823static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5824{
0853723b 5825 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
560a7ae4 5826 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
b2045352
VS
5827 int max_cdclk, vco;
5828
5829 vco = dev_priv->skl_preferred_vco_freq;
63911d72 5830 WARN_ON(vco != 8100000 && vco != 8640000);
560a7ae4 5831
b2045352
VS
5832 /*
5833 * Use the lower (vco 8640) cdclk values as a
5834 * first guess. skl_calc_cdclk() will correct it
5835 * if the preferred vco is 8100 instead.
5836 */
560a7ae4 5837 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
487ed2e4 5838 max_cdclk = 617143;
560a7ae4 5839 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
b2045352 5840 max_cdclk = 540000;
560a7ae4 5841 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
b2045352 5842 max_cdclk = 432000;
560a7ae4 5843 else
487ed2e4 5844 max_cdclk = 308571;
b2045352
VS
5845
5846 dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
89b3c3c7
ACO
5847 } else if (IS_GEMINILAKE(dev_priv)) {
5848 dev_priv->max_cdclk_freq = 316800;
e2d214ae 5849 } else if (IS_BROXTON(dev_priv)) {
281c114f 5850 dev_priv->max_cdclk_freq = 624000;
8652744b 5851 } else if (IS_BROADWELL(dev_priv)) {
560a7ae4
DL
5852 /*
5853 * FIXME with extra cooling we can allow
5854 * 540 MHz for ULX and 675 Mhz for ULT.
5855 * How can we know if extra cooling is
5856 * available? PCI ID, VTB, something else?
5857 */
5858 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5859 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5860 else if (IS_BDW_ULX(dev_priv))
560a7ae4 5861 dev_priv->max_cdclk_freq = 450000;
50a0bc90 5862 else if (IS_BDW_ULT(dev_priv))
560a7ae4
DL
5863 dev_priv->max_cdclk_freq = 540000;
5864 else
5865 dev_priv->max_cdclk_freq = 675000;
920a14b2 5866 } else if (IS_CHERRYVIEW(dev_priv)) {
0904deaf 5867 dev_priv->max_cdclk_freq = 320000;
11a914c2 5868 } else if (IS_VALLEYVIEW(dev_priv)) {
560a7ae4
DL
5869 dev_priv->max_cdclk_freq = 400000;
5870 } else {
5871 /* otherwise assume cdclk is fixed */
5872 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5873 }
5874
adafdc6f
MK
5875 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5876
560a7ae4
DL
5877 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5878 dev_priv->max_cdclk_freq);
adafdc6f
MK
5879
5880 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5881 dev_priv->max_dotclk_freq);
560a7ae4
DL
5882}
5883
4c75b940 5884static void intel_update_cdclk(struct drm_i915_private *dev_priv)
560a7ae4 5885{
1353c4fb 5886 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
2f2a121a 5887
83d7c81f 5888 if (INTEL_GEN(dev_priv) >= 9)
709e05c3
VS
5889 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
5890 dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
5891 dev_priv->cdclk_pll.ref);
2f2a121a
VS
5892 else
5893 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5894 dev_priv->cdclk_freq);
560a7ae4
DL
5895
5896 /*
b5d99ff9
VS
5897 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
5898 * Programmng [sic] note: bit[9:2] should be programmed to the number
5899 * of cdclk that generates 4MHz reference clock freq which is used to
5900 * generate GMBus clock. This will vary with the cdclk freq.
560a7ae4 5901 */
b5d99ff9 5902 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
560a7ae4 5903 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
560a7ae4
DL
5904}
5905
92891e45
VS
5906/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5907static int skl_cdclk_decimal(int cdclk)
5908{
5909 return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
5910}
5911
5f199dfa
VS
5912static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5913{
5914 int ratio;
5915
5916 if (cdclk == dev_priv->cdclk_pll.ref)
5917 return 0;
5918
5919 switch (cdclk) {
5920 default:
5921 MISSING_CASE(cdclk);
5922 case 144000:
5923 case 288000:
5924 case 384000:
5925 case 576000:
5926 ratio = 60;
5927 break;
5928 case 624000:
5929 ratio = 65;
5930 break;
5931 }
5932
5933 return dev_priv->cdclk_pll.ref * ratio;
5934}
5935
89b3c3c7
ACO
5936static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
5937{
5938 int ratio;
5939
5940 if (cdclk == dev_priv->cdclk_pll.ref)
5941 return 0;
5942
5943 switch (cdclk) {
5944 default:
5945 MISSING_CASE(cdclk);
5946 case 79200:
5947 case 158400:
5948 case 316800:
5949 ratio = 33;
5950 break;
5951 }
5952
5953 return dev_priv->cdclk_pll.ref * ratio;
5954}
5955
2b73001e
VS
5956static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
5957{
5958 I915_WRITE(BXT_DE_PLL_ENABLE, 0);
5959
5960 /* Timeout 200us */
95cac283
CW
5961 if (intel_wait_for_register(dev_priv,
5962 BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
5963 1))
2b73001e 5964 DRM_ERROR("timeout waiting for DE PLL unlock\n");
83d7c81f
VS
5965
5966 dev_priv->cdclk_pll.vco = 0;
2b73001e
VS
5967}
5968
5f199dfa 5969static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
2b73001e 5970{
5f199dfa 5971 int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
2b73001e
VS
5972 u32 val;
5973
5974 val = I915_READ(BXT_DE_PLL_CTL);
5975 val &= ~BXT_DE_PLL_RATIO_MASK;
5f199dfa 5976 val |= BXT_DE_PLL_RATIO(ratio);
2b73001e
VS
5977 I915_WRITE(BXT_DE_PLL_CTL, val);
5978
5979 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5980
5981 /* Timeout 200us */
e084e1b9
CW
5982 if (intel_wait_for_register(dev_priv,
5983 BXT_DE_PLL_ENABLE,
5984 BXT_DE_PLL_LOCK,
5985 BXT_DE_PLL_LOCK,
5986 1))
2b73001e 5987 DRM_ERROR("timeout waiting for DE PLL lock\n");
83d7c81f 5988
5f199dfa 5989 dev_priv->cdclk_pll.vco = vco;
2b73001e
VS
5990}
5991
324513c0 5992static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
f8437dd1 5993{
5f199dfa
VS
5994 u32 val, divider;
5995 int vco, ret;
f8437dd1 5996
89b3c3c7
ACO
5997 if (IS_GEMINILAKE(dev_priv))
5998 vco = glk_de_pll_vco(dev_priv, cdclk);
5999 else
6000 vco = bxt_de_pll_vco(dev_priv, cdclk);
5f199dfa
VS
6001
6002 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
6003
6004 /* cdclk = vco / 2 / div{1,1.5,2,4} */
6005 switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
6006 case 8:
f8437dd1 6007 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
f8437dd1 6008 break;
5f199dfa 6009 case 4:
f8437dd1 6010 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
f8437dd1 6011 break;
5f199dfa 6012 case 3:
89b3c3c7 6013 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f8437dd1 6014 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
f8437dd1 6015 break;
5f199dfa 6016 case 2:
f8437dd1 6017 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
f8437dd1
VK
6018 break;
6019 default:
5f199dfa
VS
6020 WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
6021 WARN_ON(vco != 0);
f8437dd1 6022
5f199dfa
VS
6023 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
6024 break;
f8437dd1
VK
6025 }
6026
f8437dd1 6027 /* Inform power controller of upcoming frequency change */
5f199dfa 6028 mutex_lock(&dev_priv->rps.hw_lock);
f8437dd1
VK
6029 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
6030 0x80000000);
6031 mutex_unlock(&dev_priv->rps.hw_lock);
6032
6033 if (ret) {
6034 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
9ef56154 6035 ret, cdclk);
f8437dd1
VK
6036 return;
6037 }
6038
5f199dfa
VS
6039 if (dev_priv->cdclk_pll.vco != 0 &&
6040 dev_priv->cdclk_pll.vco != vco)
2b73001e 6041 bxt_de_pll_disable(dev_priv);
f8437dd1 6042
5f199dfa
VS
6043 if (dev_priv->cdclk_pll.vco != vco)
6044 bxt_de_pll_enable(dev_priv, vco);
f8437dd1 6045
5f199dfa
VS
6046 val = divider | skl_cdclk_decimal(cdclk);
6047 /*
6048 * FIXME if only the cd2x divider needs changing, it could be done
6049 * without shutting off the pipe (if only one pipe is active).
6050 */
6051 val |= BXT_CDCLK_CD2X_PIPE_NONE;
6052 /*
6053 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6054 * enable otherwise.
6055 */
6056 if (cdclk >= 500000)
6057 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6058 I915_WRITE(CDCLK_CTL, val);
f8437dd1
VK
6059
6060 mutex_lock(&dev_priv->rps.hw_lock);
6061 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
9ef56154 6062 DIV_ROUND_UP(cdclk, 25000));
f8437dd1
VK
6063 mutex_unlock(&dev_priv->rps.hw_lock);
6064
6065 if (ret) {
6066 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
9ef56154 6067 ret, cdclk);
f8437dd1
VK
6068 return;
6069 }
6070
4c75b940 6071 intel_update_cdclk(dev_priv);
f8437dd1
VK
6072}
6073
d66a2194 6074static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6075{
d66a2194
ID
6076 u32 cdctl, expected;
6077
4c75b940 6078 intel_update_cdclk(dev_priv);
f8437dd1 6079
d66a2194
ID
6080 if (dev_priv->cdclk_pll.vco == 0 ||
6081 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
6082 goto sanitize;
6083
6084 /* DPLL okay; verify the cdclock
6085 *
6086 * Some BIOS versions leave an incorrect decimal frequency value and
6087 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
6088 * so sanitize this register.
6089 */
6090 cdctl = I915_READ(CDCLK_CTL);
6091 /*
6092 * Let's ignore the pipe field, since BIOS could have configured the
6093 * dividers both synching to an active pipe, or asynchronously
6094 * (PIPE_NONE).
6095 */
6096 cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
6097
6098 expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
6099 skl_cdclk_decimal(dev_priv->cdclk_freq);
6100 /*
6101 * Disable SSA Precharge when CD clock frequency < 500 MHz,
6102 * enable otherwise.
6103 */
6104 if (dev_priv->cdclk_freq >= 500000)
6105 expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
6106
6107 if (cdctl == expected)
6108 /* All well; nothing to sanitize */
6109 return;
6110
6111sanitize:
6112 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
6113
6114 /* force cdclk programming */
6115 dev_priv->cdclk_freq = 0;
6116
6117 /* force full PLL disable + enable */
6118 dev_priv->cdclk_pll.vco = -1;
6119}
6120
324513c0 6121void bxt_init_cdclk(struct drm_i915_private *dev_priv)
d66a2194 6122{
89b3c3c7
ACO
6123 int cdclk;
6124
d66a2194
ID
6125 bxt_sanitize_cdclk(dev_priv);
6126
6127 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
089c6fd5 6128 return;
c2e001ef 6129
f8437dd1
VK
6130 /*
6131 * FIXME:
6132 * - The initial CDCLK needs to be read from VBT.
6133 * Need to make this change after VBT has changes for BXT.
f8437dd1 6134 */
89b3c3c7
ACO
6135 if (IS_GEMINILAKE(dev_priv))
6136 cdclk = glk_calc_cdclk(0);
6137 else
6138 cdclk = bxt_calc_cdclk(0);
6139
6140 bxt_set_cdclk(dev_priv, cdclk);
f8437dd1
VK
6141}
6142
324513c0 6143void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
f8437dd1 6144{
324513c0 6145 bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
f8437dd1
VK
6146}
6147
a8ca4934
VS
6148static int skl_calc_cdclk(int max_pixclk, int vco)
6149{
63911d72 6150 if (vco == 8640000) {
a8ca4934 6151 if (max_pixclk > 540000)
487ed2e4 6152 return 617143;
a8ca4934
VS
6153 else if (max_pixclk > 432000)
6154 return 540000;
487ed2e4 6155 else if (max_pixclk > 308571)
a8ca4934
VS
6156 return 432000;
6157 else
487ed2e4 6158 return 308571;
a8ca4934 6159 } else {
a8ca4934
VS
6160 if (max_pixclk > 540000)
6161 return 675000;
6162 else if (max_pixclk > 450000)
6163 return 540000;
6164 else if (max_pixclk > 337500)
6165 return 450000;
6166 else
6167 return 337500;
6168 }
6169}
6170
ea61791e
VS
6171static void
6172skl_dpll0_update(struct drm_i915_private *dev_priv)
5d96d8af 6173{
ea61791e 6174 u32 val;
5d96d8af 6175
709e05c3 6176 dev_priv->cdclk_pll.ref = 24000;
1c3f7700 6177 dev_priv->cdclk_pll.vco = 0;
709e05c3 6178
ea61791e 6179 val = I915_READ(LCPLL1_CTL);
1c3f7700 6180 if ((val & LCPLL_PLL_ENABLE) == 0)
ea61791e 6181 return;
5d96d8af 6182
1c3f7700
ID
6183 if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
6184 return;
9f7eb31a 6185
ea61791e
VS
6186 val = I915_READ(DPLL_CTRL1);
6187
1c3f7700
ID
6188 if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
6189 DPLL_CTRL1_SSC(SKL_DPLL0) |
6190 DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
6191 DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
6192 return;
9f7eb31a 6193
ea61791e
VS
6194 switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
6195 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
6196 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
6197 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
6198 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
63911d72 6199 dev_priv->cdclk_pll.vco = 8100000;
ea61791e
VS
6200 break;
6201 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
6202 case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
63911d72 6203 dev_priv->cdclk_pll.vco = 8640000;
ea61791e
VS
6204 break;
6205 default:
6206 MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
ea61791e
VS
6207 break;
6208 }
5d96d8af
DL
6209}
6210
b2045352
VS
6211void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
6212{
6213 bool changed = dev_priv->skl_preferred_vco_freq != vco;
6214
6215 dev_priv->skl_preferred_vco_freq = vco;
6216
6217 if (changed)
4c75b940 6218 intel_update_max_cdclk(dev_priv);
b2045352
VS
6219}
6220
5d96d8af 6221static void
3861fc60 6222skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
5d96d8af 6223{
a8ca4934 6224 int min_cdclk = skl_calc_cdclk(0, vco);
5d96d8af
DL
6225 u32 val;
6226
63911d72 6227 WARN_ON(vco != 8100000 && vco != 8640000);
b2045352 6228
5d96d8af 6229 /* select the minimum CDCLK before enabling DPLL 0 */
9ef56154 6230 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
5d96d8af
DL
6231 I915_WRITE(CDCLK_CTL, val);
6232 POSTING_READ(CDCLK_CTL);
6233
6234 /*
6235 * We always enable DPLL0 with the lowest link rate possible, but still
6236 * taking into account the VCO required to operate the eDP panel at the
6237 * desired frequency. The usual DP link rates operate with a VCO of
6238 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
6239 * The modeset code is responsible for the selection of the exact link
6240 * rate later on, with the constraint of choosing a frequency that
a8ca4934 6241 * works with vco.
5d96d8af
DL
6242 */
6243 val = I915_READ(DPLL_CTRL1);
6244
6245 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
6246 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
6247 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
63911d72 6248 if (vco == 8640000)
5d96d8af
DL
6249 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
6250 SKL_DPLL0);
6251 else
6252 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
6253 SKL_DPLL0);
6254
6255 I915_WRITE(DPLL_CTRL1, val);
6256 POSTING_READ(DPLL_CTRL1);
6257
6258 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
6259
e24ca054
CW
6260 if (intel_wait_for_register(dev_priv,
6261 LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
6262 5))
5d96d8af 6263 DRM_ERROR("DPLL0 not locked\n");
1cd593e0 6264
63911d72 6265 dev_priv->cdclk_pll.vco = vco;
b2045352
VS
6266
6267 /* We'll want to keep using the current vco from now on. */
6268 skl_set_preferred_cdclk_vco(dev_priv, vco);
5d96d8af
DL
6269}
6270
430e05de
VS
6271static void
6272skl_dpll0_disable(struct drm_i915_private *dev_priv)
6273{
6274 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
8ad32a05
CW
6275 if (intel_wait_for_register(dev_priv,
6276 LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
6277 1))
430e05de 6278 DRM_ERROR("Couldn't disable DPLL0\n");
1cd593e0 6279
63911d72 6280 dev_priv->cdclk_pll.vco = 0;
430e05de
VS
6281}
6282
5d96d8af
DL
6283static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
6284{
6285 int ret;
6286 u32 val;
6287
6288 /* inform PCU we want to change CDCLK */
6289 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
6290 mutex_lock(&dev_priv->rps.hw_lock);
6291 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
6292 mutex_unlock(&dev_priv->rps.hw_lock);
6293
6294 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
6295}
6296
6297static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
6298{
848496e5 6299 return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
5d96d8af
DL
6300}
6301
1cd593e0 6302static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
5d96d8af
DL
6303{
6304 u32 freq_select, pcu_ack;
6305
1cd593e0
VS
6306 WARN_ON((cdclk == 24000) != (vco == 0));
6307
63911d72 6308 DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
5d96d8af
DL
6309
6310 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
6311 DRM_ERROR("failed to inform PCU about cdclk change\n");
6312 return;
6313 }
6314
6315 /* set CDCLK_CTL */
9ef56154 6316 switch (cdclk) {
5d96d8af
DL
6317 case 450000:
6318 case 432000:
6319 freq_select = CDCLK_FREQ_450_432;
6320 pcu_ack = 1;
6321 break;
6322 case 540000:
6323 freq_select = CDCLK_FREQ_540;
6324 pcu_ack = 2;
6325 break;
487ed2e4 6326 case 308571:
5d96d8af
DL
6327 case 337500:
6328 default:
6329 freq_select = CDCLK_FREQ_337_308;
6330 pcu_ack = 0;
6331 break;
487ed2e4 6332 case 617143:
5d96d8af
DL
6333 case 675000:
6334 freq_select = CDCLK_FREQ_675_617;
6335 pcu_ack = 3;
6336 break;
6337 }
6338
63911d72
VS
6339 if (dev_priv->cdclk_pll.vco != 0 &&
6340 dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6341 skl_dpll0_disable(dev_priv);
6342
63911d72 6343 if (dev_priv->cdclk_pll.vco != vco)
1cd593e0
VS
6344 skl_dpll0_enable(dev_priv, vco);
6345
9ef56154 6346 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
5d96d8af
DL
6347 POSTING_READ(CDCLK_CTL);
6348
6349 /* inform PCU of the change */
6350 mutex_lock(&dev_priv->rps.hw_lock);
6351 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
6352 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4 6353
4c75b940 6354 intel_update_cdclk(dev_priv);
5d96d8af
DL
6355}
6356
9f7eb31a
VS
6357static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
6358
5d96d8af
DL
6359void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
6360{
709e05c3 6361 skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
5d96d8af
DL
6362}
6363
6364void skl_init_cdclk(struct drm_i915_private *dev_priv)
6365{
9f7eb31a
VS
6366 int cdclk, vco;
6367
6368 skl_sanitize_cdclk(dev_priv);
5d96d8af 6369
63911d72 6370 if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
9f7eb31a
VS
6371 /*
6372 * Use the current vco as our initial
6373 * guess as to what the preferred vco is.
6374 */
6375 if (dev_priv->skl_preferred_vco_freq == 0)
6376 skl_set_preferred_cdclk_vco(dev_priv,
63911d72 6377 dev_priv->cdclk_pll.vco);
70c2c184 6378 return;
1cd593e0 6379 }
5d96d8af 6380
70c2c184
VS
6381 vco = dev_priv->skl_preferred_vco_freq;
6382 if (vco == 0)
63911d72 6383 vco = 8100000;
70c2c184 6384 cdclk = skl_calc_cdclk(0, vco);
5d96d8af 6385
70c2c184 6386 skl_set_cdclk(dev_priv, cdclk, vco);
5d96d8af
DL
6387}
6388
9f7eb31a 6389static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
c73666f3 6390{
09492498 6391 uint32_t cdctl, expected;
c73666f3 6392
f1b391a5
SK
6393 /*
6394 * check if the pre-os intialized the display
6395 * There is SWF18 scratchpad register defined which is set by the
6396 * pre-os which can be used by the OS drivers to check the status
6397 */
6398 if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
6399 goto sanitize;
6400
4c75b940 6401 intel_update_cdclk(dev_priv);
c73666f3 6402 /* Is PLL enabled and locked ? */
1c3f7700
ID
6403 if (dev_priv->cdclk_pll.vco == 0 ||
6404 dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
c73666f3
SK
6405 goto sanitize;
6406
6407 /* DPLL okay; verify the cdclock
6408 *
6409 * Noticed in some instances that the freq selection is correct but
6410 * decimal part is programmed wrong from BIOS where pre-os does not
6411 * enable display. Verify the same as well.
6412 */
09492498
VS
6413 cdctl = I915_READ(CDCLK_CTL);
6414 expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
6415 skl_cdclk_decimal(dev_priv->cdclk_freq);
6416 if (cdctl == expected)
c73666f3 6417 /* All well; nothing to sanitize */
9f7eb31a 6418 return;
c89e39f3 6419
9f7eb31a
VS
6420sanitize:
6421 DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
c73666f3 6422
9f7eb31a
VS
6423 /* force cdclk programming */
6424 dev_priv->cdclk_freq = 0;
6425 /* force full PLL disable + enable */
63911d72 6426 dev_priv->cdclk_pll.vco = -1;
c73666f3
SK
6427}
6428
30a970c6
JB
6429/* Adjust CDclk dividers to allow high res or save power if possible */
6430static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
6431{
fac5e23e 6432 struct drm_i915_private *dev_priv = to_i915(dev);
30a970c6
JB
6433 u32 val, cmd;
6434
1353c4fb 6435 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6436 != dev_priv->cdclk_freq);
d60c4473 6437
dfcab17e 6438 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 6439 cmd = 2;
dfcab17e 6440 else if (cdclk == 266667)
30a970c6
JB
6441 cmd = 1;
6442 else
6443 cmd = 0;
6444
6445 mutex_lock(&dev_priv->rps.hw_lock);
6446 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6447 val &= ~DSPFREQGUAR_MASK;
6448 val |= (cmd << DSPFREQGUAR_SHIFT);
6449 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6450 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6451 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
6452 50)) {
6453 DRM_ERROR("timed out waiting for CDclk change\n");
6454 }
6455 mutex_unlock(&dev_priv->rps.hw_lock);
6456
54433e91
VS
6457 mutex_lock(&dev_priv->sb_lock);
6458
dfcab17e 6459 if (cdclk == 400000) {
6bcda4f0 6460 u32 divider;
30a970c6 6461
6bcda4f0 6462 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 6463
30a970c6
JB
6464 /* adjust cdclk divider */
6465 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
87d5d259 6466 val &= ~CCK_FREQUENCY_VALUES;
30a970c6
JB
6467 val |= divider;
6468 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
6469
6470 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
87d5d259 6471 CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
a877e801
VS
6472 50))
6473 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
6474 }
6475
30a970c6
JB
6476 /* adjust self-refresh exit latency value */
6477 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
6478 val &= ~0x7f;
6479
6480 /*
6481 * For high bandwidth configs, we set a higher latency in the bunit
6482 * so that the core display fetch happens in time to avoid underruns.
6483 */
dfcab17e 6484 if (cdclk == 400000)
30a970c6
JB
6485 val |= 4500 / 250; /* 4.5 usec */
6486 else
6487 val |= 3000 / 250; /* 3.0 usec */
6488 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 6489
a580516d 6490 mutex_unlock(&dev_priv->sb_lock);
30a970c6 6491
4c75b940 6492 intel_update_cdclk(dev_priv);
30a970c6
JB
6493}
6494
383c5a6a
VS
6495static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
6496{
fac5e23e 6497 struct drm_i915_private *dev_priv = to_i915(dev);
383c5a6a
VS
6498 u32 val, cmd;
6499
1353c4fb 6500 WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
164dfd28 6501 != dev_priv->cdclk_freq);
383c5a6a
VS
6502
6503 switch (cdclk) {
383c5a6a
VS
6504 case 333333:
6505 case 320000:
383c5a6a 6506 case 266667:
383c5a6a 6507 case 200000:
383c5a6a
VS
6508 break;
6509 default:
5f77eeb0 6510 MISSING_CASE(cdclk);
383c5a6a
VS
6511 return;
6512 }
6513
9d0d3fda
VS
6514 /*
6515 * Specs are full of misinformation, but testing on actual
6516 * hardware has shown that we just need to write the desired
6517 * CCK divider into the Punit register.
6518 */
6519 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
6520
383c5a6a
VS
6521 mutex_lock(&dev_priv->rps.hw_lock);
6522 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6523 val &= ~DSPFREQGUAR_MASK_CHV;
6524 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
6525 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
6526 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
6527 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
6528 50)) {
6529 DRM_ERROR("timed out waiting for CDclk change\n");
6530 }
6531 mutex_unlock(&dev_priv->rps.hw_lock);
6532
4c75b940 6533 intel_update_cdclk(dev_priv);
383c5a6a
VS
6534}
6535
30a970c6
JB
6536static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
6537 int max_pixclk)
6538{
6bcda4f0 6539 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 6540 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 6541
30a970c6
JB
6542 /*
6543 * Really only a few cases to deal with, as only 4 CDclks are supported:
6544 * 200MHz
6545 * 267MHz
29dc7ef3 6546 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
6547 * 400MHz (VLV only)
6548 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
6549 * of the lower bin and adjust if needed.
e37c67a1
VS
6550 *
6551 * We seem to get an unstable or solid color picture at 200MHz.
6552 * Not sure what's wrong. For now use 200MHz only when all pipes
6553 * are off.
30a970c6 6554 */
6cca3195
VS
6555 if (!IS_CHERRYVIEW(dev_priv) &&
6556 max_pixclk > freq_320*limit/100)
dfcab17e 6557 return 400000;
6cca3195 6558 else if (max_pixclk > 266667*limit/100)
29dc7ef3 6559 return freq_320;
e37c67a1 6560 else if (max_pixclk > 0)
dfcab17e 6561 return 266667;
e37c67a1
VS
6562 else
6563 return 200000;
30a970c6
JB
6564}
6565
89b3c3c7
ACO
6566static int glk_calc_cdclk(int max_pixclk)
6567{
09d09386 6568 if (max_pixclk > 2 * 158400)
89b3c3c7 6569 return 316800;
09d09386 6570 else if (max_pixclk > 2 * 79200)
89b3c3c7
ACO
6571 return 158400;
6572 else
6573 return 79200;
6574}
6575
324513c0 6576static int bxt_calc_cdclk(int max_pixclk)
f8437dd1 6577{
760e1477 6578 if (max_pixclk > 576000)
f8437dd1 6579 return 624000;
760e1477 6580 else if (max_pixclk > 384000)
f8437dd1 6581 return 576000;
760e1477 6582 else if (max_pixclk > 288000)
f8437dd1 6583 return 384000;
760e1477 6584 else if (max_pixclk > 144000)
f8437dd1
VK
6585 return 288000;
6586 else
6587 return 144000;
6588}
6589
e8788cbc 6590/* Compute the max pixel clock for new configuration. */
a821fc46
ACO
6591static int intel_mode_max_pixclk(struct drm_device *dev,
6592 struct drm_atomic_state *state)
30a970c6 6593{
565602d7 6594 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 6595 struct drm_i915_private *dev_priv = to_i915(dev);
565602d7
ML
6596 struct drm_crtc *crtc;
6597 struct drm_crtc_state *crtc_state;
6598 unsigned max_pixclk = 0, i;
6599 enum pipe pipe;
30a970c6 6600
565602d7
ML
6601 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
6602 sizeof(intel_state->min_pixclk));
304603f4 6603
565602d7
ML
6604 for_each_crtc_in_state(state, crtc, crtc_state, i) {
6605 int pixclk = 0;
6606
6607 if (crtc_state->enable)
6608 pixclk = crtc_state->adjusted_mode.crtc_clock;
304603f4 6609
565602d7 6610 intel_state->min_pixclk[i] = pixclk;
30a970c6
JB
6611 }
6612
565602d7
ML
6613 for_each_pipe(dev_priv, pipe)
6614 max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
6615
30a970c6
JB
6616 return max_pixclk;
6617}
6618
27c329ed 6619static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 6620{
27c329ed 6621 struct drm_device *dev = state->dev;
fac5e23e 6622 struct drm_i915_private *dev_priv = to_i915(dev);
27c329ed 6623 int max_pixclk = intel_mode_max_pixclk(dev, state);
1a617b77
ML
6624 struct intel_atomic_state *intel_state =
6625 to_intel_atomic_state(state);
30a970c6 6626
1a617b77 6627 intel_state->cdclk = intel_state->dev_cdclk =
27c329ed 6628 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 6629
1a617b77
ML
6630 if (!intel_state->active_crtcs)
6631 intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
6632
27c329ed
ML
6633 return 0;
6634}
304603f4 6635
324513c0 6636static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
27c329ed 6637{
89b3c3c7 6638 struct drm_i915_private *dev_priv = to_i915(state->dev);
4e5ca60f 6639 int max_pixclk = ilk_max_pixel_rate(state);
1a617b77
ML
6640 struct intel_atomic_state *intel_state =
6641 to_intel_atomic_state(state);
89b3c3c7 6642 int cdclk;
85a96e7a 6643
89b3c3c7
ACO
6644 if (IS_GEMINILAKE(dev_priv))
6645 cdclk = glk_calc_cdclk(max_pixclk);
6646 else
6647 cdclk = bxt_calc_cdclk(max_pixclk);
85a96e7a 6648
89b3c3c7
ACO
6649 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
6650
6651 if (!intel_state->active_crtcs) {
6652 if (IS_GEMINILAKE(dev_priv))
6653 cdclk = glk_calc_cdclk(0);
6654 else
6655 cdclk = bxt_calc_cdclk(0);
6656
6657 intel_state->dev_cdclk = cdclk;
6658 }
1a617b77 6659
27c329ed 6660 return 0;
30a970c6
JB
6661}
6662
1e69cd74
VS
6663static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6664{
6665 unsigned int credits, default_credits;
6666
6667 if (IS_CHERRYVIEW(dev_priv))
6668 default_credits = PFI_CREDIT(12);
6669 else
6670 default_credits = PFI_CREDIT(8);
6671
bfa7df01 6672 if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
1e69cd74
VS
6673 /* CHV suggested value is 31 or 63 */
6674 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 6675 credits = PFI_CREDIT_63;
1e69cd74
VS
6676 else
6677 credits = PFI_CREDIT(15);
6678 } else {
6679 credits = default_credits;
6680 }
6681
6682 /*
6683 * WA - write default credits before re-programming
6684 * FIXME: should we also set the resend bit here?
6685 */
6686 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6687 default_credits);
6688
6689 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6690 credits | PFI_CREDIT_RESEND);
6691
6692 /*
6693 * FIXME is this guaranteed to clear
6694 * immediately or should we poll for it?
6695 */
6696 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6697}
6698
27c329ed 6699static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 6700{
a821fc46 6701 struct drm_device *dev = old_state->dev;
fac5e23e 6702 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77
ML
6703 struct intel_atomic_state *old_intel_state =
6704 to_intel_atomic_state(old_state);
6705 unsigned req_cdclk = old_intel_state->dev_cdclk;
30a970c6 6706
27c329ed
ML
6707 /*
6708 * FIXME: We can end up here with all power domains off, yet
6709 * with a CDCLK frequency other than the minimum. To account
6710 * for this take the PIPE-A power domain, which covers the HW
6711 * blocks needed for the following programming. This can be
6712 * removed once it's guaranteed that we get here either with
6713 * the minimum CDCLK set, or the required power domains
6714 * enabled.
6715 */
6716 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 6717
920a14b2 6718 if (IS_CHERRYVIEW(dev_priv))
27c329ed
ML
6719 cherryview_set_cdclk(dev, req_cdclk);
6720 else
6721 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6722
27c329ed 6723 vlv_program_pfi_credits(dev_priv);
1e69cd74 6724
27c329ed 6725 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
6726}
6727
4a806558
ML
6728static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
6729 struct drm_atomic_state *old_state)
89b667f8 6730{
4a806558 6731 struct drm_crtc *crtc = pipe_config->base.crtc;
89b667f8 6732 struct drm_device *dev = crtc->dev;
a72e4c9f 6733 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8 6734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
89b667f8 6735 int pipe = intel_crtc->pipe;
89b667f8 6736
53d9f4e9 6737 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6738 return;
6739
37a5650b 6740 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6741 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6742
6743 intel_set_pipe_timings(intel_crtc);
bc58be60 6744 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6745
920a14b2 6746 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
fac5e23e 6747 struct drm_i915_private *dev_priv = to_i915(dev);
c14b0485
VS
6748
6749 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6750 I915_WRITE(CHV_CANVAS(pipe), 0);
6751 }
6752
5b18e57c
SV
6753 i9xx_set_pipeconf(intel_crtc);
6754
89b667f8 6755 intel_crtc->active = true;
89b667f8 6756
a72e4c9f 6757 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6758
fd6bbda9 6759 intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
89b667f8 6760
920a14b2 6761 if (IS_CHERRYVIEW(dev_priv)) {
cd2d34d9
VS
6762 chv_prepare_pll(intel_crtc, intel_crtc->config);
6763 chv_enable_pll(intel_crtc, intel_crtc->config);
6764 } else {
6765 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6766 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6767 }
89b667f8 6768
fd6bbda9 6769 intel_encoders_pre_enable(crtc, pipe_config, old_state);
89b667f8 6770
2dd24552
JB
6771 i9xx_pfit_enable(intel_crtc);
6772
b95c5321 6773 intel_color_load_luts(&pipe_config->base);
63cbb074 6774
432081bc 6775 intel_update_watermarks(intel_crtc);
e1fdc473 6776 intel_enable_pipe(intel_crtc);
be6a6f8e 6777
4b3a9526
VS
6778 assert_vblank_disabled(crtc);
6779 drm_crtc_vblank_on(crtc);
6780
fd6bbda9 6781 intel_encoders_enable(crtc, pipe_config, old_state);
89b667f8
JB
6782}
6783
f13c2ef3
SV
6784static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6785{
6786 struct drm_device *dev = crtc->base.dev;
fac5e23e 6787 struct drm_i915_private *dev_priv = to_i915(dev);
f13c2ef3 6788
6e3c9717
ACO
6789 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6790 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
SV
6791}
6792
4a806558
ML
6793static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
6794 struct drm_atomic_state *old_state)
79e53945 6795{
4a806558 6796 struct drm_crtc *crtc = pipe_config->base.crtc;
79e53945 6797 struct drm_device *dev = crtc->dev;
a72e4c9f 6798 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6799 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cd2d34d9 6800 enum pipe pipe = intel_crtc->pipe;
79e53945 6801
53d9f4e9 6802 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6803 return;
6804
f13c2ef3
SV
6805 i9xx_set_pll_dividers(intel_crtc);
6806
37a5650b 6807 if (intel_crtc_has_dp_encoder(intel_crtc->config))
fe3cd48d 6808 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
SV
6809
6810 intel_set_pipe_timings(intel_crtc);
bc58be60 6811 intel_set_pipe_src_size(intel_crtc);
5b18e57c 6812
5b18e57c
SV
6813 i9xx_set_pipeconf(intel_crtc);
6814
f7abfe8b 6815 intel_crtc->active = true;
6b383a7f 6816
5db94019 6817 if (!IS_GEN2(dev_priv))
a72e4c9f 6818 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6819
fd6bbda9 6820 intel_encoders_pre_enable(crtc, pipe_config, old_state);
9d6d9f19 6821
f6736a1a
SV
6822 i9xx_enable_pll(intel_crtc);
6823
2dd24552
JB
6824 i9xx_pfit_enable(intel_crtc);
6825
b95c5321 6826 intel_color_load_luts(&pipe_config->base);
63cbb074 6827
432081bc 6828 intel_update_watermarks(intel_crtc);
e1fdc473 6829 intel_enable_pipe(intel_crtc);
be6a6f8e 6830
4b3a9526
VS
6831 assert_vblank_disabled(crtc);
6832 drm_crtc_vblank_on(crtc);
6833
fd6bbda9 6834 intel_encoders_enable(crtc, pipe_config, old_state);
0b8765c6 6835}
79e53945 6836
87476d63
SV
6837static void i9xx_pfit_disable(struct intel_crtc *crtc)
6838{
6839 struct drm_device *dev = crtc->base.dev;
fac5e23e 6840 struct drm_i915_private *dev_priv = to_i915(dev);
87476d63 6841
6e3c9717 6842 if (!crtc->config->gmch_pfit.control)
328d8e82 6843 return;
87476d63 6844
328d8e82 6845 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6846
328d8e82
SV
6847 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6848 I915_READ(PFIT_CONTROL));
6849 I915_WRITE(PFIT_CONTROL, 0);
87476d63
SV
6850}
6851
4a806558
ML
6852static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
6853 struct drm_atomic_state *old_state)
0b8765c6 6854{
4a806558 6855 struct drm_crtc *crtc = old_crtc_state->base.crtc;
0b8765c6 6856 struct drm_device *dev = crtc->dev;
fac5e23e 6857 struct drm_i915_private *dev_priv = to_i915(dev);
0b8765c6
JB
6858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6859 int pipe = intel_crtc->pipe;
ef9c3aee 6860
6304cd91
VS
6861 /*
6862 * On gen2 planes are double buffered but the pipe isn't, so we must
6863 * wait for planes to fully turn off before disabling the pipe.
6864 */
5db94019 6865 if (IS_GEN2(dev_priv))
0f0f74bc 6866 intel_wait_for_vblank(dev_priv, pipe);
6304cd91 6867
fd6bbda9 6868 intel_encoders_disable(crtc, old_crtc_state, old_state);
4b3a9526 6869
f9b61ff6
SV
6870 drm_crtc_vblank_off(crtc);
6871 assert_vblank_disabled(crtc);
6872
575f7ab7 6873 intel_disable_pipe(intel_crtc);
24a1f16d 6874
87476d63 6875 i9xx_pfit_disable(intel_crtc);
24a1f16d 6876
fd6bbda9 6877 intel_encoders_post_disable(crtc, old_crtc_state, old_state);
89b667f8 6878
d7edc4e5 6879 if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
920a14b2 6880 if (IS_CHERRYVIEW(dev_priv))
076ed3b2 6881 chv_disable_pll(dev_priv, pipe);
11a914c2 6882 else if (IS_VALLEYVIEW(dev_priv))
076ed3b2
CML
6883 vlv_disable_pll(dev_priv, pipe);
6884 else
1c4e0274 6885 i9xx_disable_pll(intel_crtc);
076ed3b2 6886 }
0b8765c6 6887
fd6bbda9 6888 intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
d6db995f 6889
5db94019 6890 if (!IS_GEN2(dev_priv))
a72e4c9f 6891 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6892}
6893
b17d48e2
ML
6894static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6895{
842e0307 6896 struct intel_encoder *encoder;
b17d48e2
ML
6897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6898 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6899 enum intel_display_power_domain domain;
6900 unsigned long domains;
4a806558
ML
6901 struct drm_atomic_state *state;
6902 struct intel_crtc_state *crtc_state;
6903 int ret;
b17d48e2
ML
6904
6905 if (!intel_crtc->active)
6906 return;
6907
936e71e3 6908 if (to_intel_plane_state(crtc->primary->state)->base.visible) {
5a21b665 6909 WARN_ON(intel_crtc->flip_work);
fc32b1fd 6910
2622a081 6911 intel_pre_disable_primary_noatomic(crtc);
54a41961
ML
6912
6913 intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
936e71e3 6914 to_intel_plane_state(crtc->primary->state)->base.visible = false;
a539205a
ML
6915 }
6916
4a806558
ML
6917 state = drm_atomic_state_alloc(crtc->dev);
6918 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
6919
6920 /* Everything's already locked, -EDEADLK can't happen. */
6921 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6922 ret = drm_atomic_add_affected_connectors(state, crtc);
6923
6924 WARN_ON(IS_ERR(crtc_state) || ret);
6925
6926 dev_priv->display.crtc_disable(crtc_state, state);
6927
0853695c 6928 drm_atomic_state_put(state);
842e0307 6929
78108b7c
VS
6930 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
6931 crtc->base.id, crtc->name);
842e0307
ML
6932
6933 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
6934 crtc->state->active = false;
37d9078b 6935 intel_crtc->active = false;
842e0307
ML
6936 crtc->enabled = false;
6937 crtc->state->connector_mask = 0;
6938 crtc->state->encoder_mask = 0;
6939
6940 for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
6941 encoder->base.crtc = NULL;
6942
58f9c0bc 6943 intel_fbc_disable(intel_crtc);
432081bc 6944 intel_update_watermarks(intel_crtc);
1f7457b1 6945 intel_disable_shared_dpll(intel_crtc);
b17d48e2
ML
6946
6947 domains = intel_crtc->enabled_power_domains;
6948 for_each_power_domain(domain, domains)
6949 intel_display_power_put(dev_priv, domain);
6950 intel_crtc->enabled_power_domains = 0;
565602d7
ML
6951
6952 dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
6953 dev_priv->min_pixclk[intel_crtc->pipe] = 0;
b17d48e2
ML
6954}
6955
6b72d486
ML
6956/*
6957 * turn all crtc's off, but do not adjust state
6958 * This has to be paired with a call to intel_modeset_setup_hw_state.
6959 */
70e0bd74 6960int intel_display_suspend(struct drm_device *dev)
ee7b9f93 6961{
e2c8b870 6962 struct drm_i915_private *dev_priv = to_i915(dev);
70e0bd74 6963 struct drm_atomic_state *state;
e2c8b870 6964 int ret;
70e0bd74 6965
e2c8b870
ML
6966 state = drm_atomic_helper_suspend(dev);
6967 ret = PTR_ERR_OR_ZERO(state);
70e0bd74
ML
6968 if (ret)
6969 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
e2c8b870
ML
6970 else
6971 dev_priv->modeset_restore_state = state;
70e0bd74 6972 return ret;
ee7b9f93
JB
6973}
6974
ea5b213a 6975void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6976{
4ef69c7a 6977 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6978
ea5b213a
CW
6979 drm_encoder_cleanup(encoder);
6980 kfree(intel_encoder);
7e7d76c3
JB
6981}
6982
0a91ca29
SV
6983/* Cross check the actual hw state with our own modeset state tracking (and it's
6984 * internal consistency). */
5a21b665 6985static void intel_connector_verify_state(struct intel_connector *connector)
79e53945 6986{
5a21b665 6987 struct drm_crtc *crtc = connector->base.state->crtc;
35dd3c64
ML
6988
6989 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6990 connector->base.base.id,
6991 connector->base.name);
6992
0a91ca29 6993 if (connector->get_hw_state(connector)) {
e85376cb 6994 struct intel_encoder *encoder = connector->encoder;
5a21b665 6995 struct drm_connector_state *conn_state = connector->base.state;
0a91ca29 6996
35dd3c64
ML
6997 I915_STATE_WARN(!crtc,
6998 "connector enabled without attached crtc\n");
0a91ca29 6999
35dd3c64
ML
7000 if (!crtc)
7001 return;
7002
7003 I915_STATE_WARN(!crtc->state->active,
7004 "connector is active, but attached crtc isn't\n");
7005
e85376cb 7006 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
35dd3c64
ML
7007 return;
7008
e85376cb 7009 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
35dd3c64
ML
7010 "atomic encoder doesn't match attached encoder\n");
7011
e85376cb 7012 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
35dd3c64
ML
7013 "attached encoder crtc differs from connector crtc\n");
7014 } else {
4d688a2a
ML
7015 I915_STATE_WARN(crtc && crtc->state->active,
7016 "attached crtc is active, but connector isn't\n");
5a21b665 7017 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
35dd3c64 7018 "best encoder set without crtc!\n");
0a91ca29 7019 }
79e53945
JB
7020}
7021
08d9bc92
ACO
7022int intel_connector_init(struct intel_connector *connector)
7023{
5350a031 7024 drm_atomic_helper_connector_reset(&connector->base);
08d9bc92 7025
5350a031 7026 if (!connector->base.state)
08d9bc92
ACO
7027 return -ENOMEM;
7028
08d9bc92
ACO
7029 return 0;
7030}
7031
7032struct intel_connector *intel_connector_alloc(void)
7033{
7034 struct intel_connector *connector;
7035
7036 connector = kzalloc(sizeof *connector, GFP_KERNEL);
7037 if (!connector)
7038 return NULL;
7039
7040 if (intel_connector_init(connector) < 0) {
7041 kfree(connector);
7042 return NULL;
7043 }
7044
7045 return connector;
7046}
7047
f0947c37
SV
7048/* Simple connector->get_hw_state implementation for encoders that support only
7049 * one connector and no cloning and hence the encoder state determines the state
7050 * of the connector. */
7051bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 7052{
24929352 7053 enum pipe pipe = 0;
f0947c37 7054 struct intel_encoder *encoder = connector->encoder;
ea5b213a 7055
f0947c37 7056 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
7057}
7058
6d293983 7059static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 7060{
6d293983
ACO
7061 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
7062 return crtc_state->fdi_lanes;
d272ddfa
VS
7063
7064 return 0;
7065}
7066
6d293983 7067static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 7068 struct intel_crtc_state *pipe_config)
1857e1da 7069{
8652744b 7070 struct drm_i915_private *dev_priv = to_i915(dev);
6d293983
ACO
7071 struct drm_atomic_state *state = pipe_config->base.state;
7072 struct intel_crtc *other_crtc;
7073 struct intel_crtc_state *other_crtc_state;
7074
1857e1da
SV
7075 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
7076 pipe_name(pipe), pipe_config->fdi_lanes);
7077 if (pipe_config->fdi_lanes > 4) {
7078 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
7079 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7080 return -EINVAL;
1857e1da
SV
7081 }
7082
8652744b 7083 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
1857e1da
SV
7084 if (pipe_config->fdi_lanes > 2) {
7085 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
7086 pipe_config->fdi_lanes);
6d293983 7087 return -EINVAL;
1857e1da 7088 } else {
6d293983 7089 return 0;
1857e1da
SV
7090 }
7091 }
7092
b7f05d4a 7093 if (INTEL_INFO(dev_priv)->num_pipes == 2)
6d293983 7094 return 0;
1857e1da
SV
7095
7096 /* Ivybridge 3 pipe is really complicated */
7097 switch (pipe) {
7098 case PIPE_A:
6d293983 7099 return 0;
1857e1da 7100 case PIPE_B:
6d293983
ACO
7101 if (pipe_config->fdi_lanes <= 2)
7102 return 0;
7103
b91eb5cc 7104 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
6d293983
ACO
7105 other_crtc_state =
7106 intel_atomic_get_crtc_state(state, other_crtc);
7107 if (IS_ERR(other_crtc_state))
7108 return PTR_ERR(other_crtc_state);
7109
7110 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
SV
7111 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
7112 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7113 return -EINVAL;
1857e1da 7114 }
6d293983 7115 return 0;
1857e1da 7116 case PIPE_C:
251cc67c
VS
7117 if (pipe_config->fdi_lanes > 2) {
7118 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
7119 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 7120 return -EINVAL;
251cc67c 7121 }
6d293983 7122
b91eb5cc 7123 other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
6d293983
ACO
7124 other_crtc_state =
7125 intel_atomic_get_crtc_state(state, other_crtc);
7126 if (IS_ERR(other_crtc_state))
7127 return PTR_ERR(other_crtc_state);
7128
7129 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 7130 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 7131 return -EINVAL;
1857e1da 7132 }
6d293983 7133 return 0;
1857e1da
SV
7134 default:
7135 BUG();
7136 }
7137}
7138
e29c22c0
SV
7139#define RETRY 1
7140static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 7141 struct intel_crtc_state *pipe_config)
877d48d5 7142{
1857e1da 7143 struct drm_device *dev = intel_crtc->base.dev;
7c5f93b0 7144 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
7145 int lane, link_bw, fdi_dotclock, ret;
7146 bool needs_recompute = false;
877d48d5 7147
e29c22c0 7148retry:
877d48d5
SV
7149 /* FDI is a binary signal running at ~2.7GHz, encoding
7150 * each output octet as 10 bits. The actual frequency
7151 * is stored as a divider into a 100MHz clock, and the
7152 * mode pixel clock is stored in units of 1KHz.
7153 * Hence the bw of each lane in terms of the mode signal
7154 * is:
7155 */
21a727b3 7156 link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
877d48d5 7157
241bfc38 7158 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 7159
2bd89a07 7160 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
SV
7161 pipe_config->pipe_bpp);
7162
7163 pipe_config->fdi_lanes = lane;
7164
2bd89a07 7165 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 7166 link_bw, &pipe_config->fdi_m_n);
1857e1da 7167
e3b247da 7168 ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
6d293983 7169 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
SV
7170 pipe_config->pipe_bpp -= 2*3;
7171 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
7172 pipe_config->pipe_bpp);
7173 needs_recompute = true;
7174 pipe_config->bw_constrained = true;
7175
7176 goto retry;
7177 }
7178
7179 if (needs_recompute)
7180 return RETRY;
7181
6d293983 7182 return ret;
877d48d5
SV
7183}
7184
8cfb3407
VS
7185static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
7186 struct intel_crtc_state *pipe_config)
7187{
7188 if (pipe_config->pipe_bpp > 24)
7189 return false;
7190
7191 /* HSW can handle pixel rate up to cdclk? */
2d1fe073 7192 if (IS_HASWELL(dev_priv))
8cfb3407
VS
7193 return true;
7194
7195 /*
b432e5cf
VS
7196 * We compare against max which means we must take
7197 * the increased cdclk requirement into account when
7198 * calculating the new cdclk.
7199 *
7200 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
7201 */
7202 return ilk_pipe_pixel_rate(pipe_config) <=
7203 dev_priv->max_cdclk_freq * 95 / 100;
7204}
7205
42db64ef 7206static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 7207 struct intel_crtc_state *pipe_config)
42db64ef 7208{
8cfb3407 7209 struct drm_device *dev = crtc->base.dev;
fac5e23e 7210 struct drm_i915_private *dev_priv = to_i915(dev);
8cfb3407 7211
d330a953 7212 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
7213 hsw_crtc_supports_ips(crtc) &&
7214 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
7215}
7216
39acb4aa
VS
7217static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
7218{
7219 const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7220
7221 /* GDG double wide on either pipe, otherwise pipe A only */
7222 return INTEL_INFO(dev_priv)->gen < 4 &&
7223 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
7224}
7225
a43f6e0f 7226static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 7227 struct intel_crtc_state *pipe_config)
79e53945 7228{
a43f6e0f 7229 struct drm_device *dev = crtc->base.dev;
fac5e23e 7230 struct drm_i915_private *dev_priv = to_i915(dev);
7c5f93b0 7231 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
f3261156 7232 int clock_limit = dev_priv->max_dotclk_freq;
89749350 7233
6315b5d3 7234 if (INTEL_GEN(dev_priv) < 4) {
f3261156 7235 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
cf532bb2
VS
7236
7237 /*
39acb4aa 7238 * Enable double wide mode when the dot clock
cf532bb2 7239 * is > 90% of the (display) core speed.
cf532bb2 7240 */
39acb4aa
VS
7241 if (intel_crtc_supports_double_wide(crtc) &&
7242 adjusted_mode->crtc_clock > clock_limit) {
f3261156 7243 clock_limit = dev_priv->max_dotclk_freq;
cf532bb2 7244 pipe_config->double_wide = true;
ad3a4479 7245 }
f3261156 7246 }
ad3a4479 7247
f3261156
VS
7248 if (adjusted_mode->crtc_clock > clock_limit) {
7249 DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
7250 adjusted_mode->crtc_clock, clock_limit,
7251 yesno(pipe_config->double_wide));
7252 return -EINVAL;
2c07245f 7253 }
89749350 7254
1d1d0e27
VS
7255 /*
7256 * Pipe horizontal size must be even in:
7257 * - DVO ganged mode
7258 * - LVDS dual channel mode
7259 * - Double wide pipe
7260 */
2d84d2b3 7261 if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
7262 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
7263 pipe_config->pipe_src_w &= ~1;
7264
8693a824
DL
7265 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
7266 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42 7267 */
9beb5fea 7268 if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
aad941d5 7269 adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
e29c22c0 7270 return -EINVAL;
44f46b42 7271
50a0bc90 7272 if (HAS_IPS(dev_priv))
a43f6e0f
SV
7273 hsw_compute_ips_config(crtc, pipe_config);
7274
877d48d5 7275 if (pipe_config->has_pch_encoder)
a43f6e0f 7276 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 7277
cf5a15be 7278 return 0;
79e53945
JB
7279}
7280
1353c4fb 7281static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7282{
1353c4fb 7283 u32 cdctl;
1652d19e 7284
ea61791e 7285 skl_dpll0_update(dev_priv);
1652d19e 7286
63911d72 7287 if (dev_priv->cdclk_pll.vco == 0)
709e05c3 7288 return dev_priv->cdclk_pll.ref;
1652d19e 7289
ea61791e 7290 cdctl = I915_READ(CDCLK_CTL);
1652d19e 7291
63911d72 7292 if (dev_priv->cdclk_pll.vco == 8640000) {
1652d19e
VS
7293 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7294 case CDCLK_FREQ_450_432:
7295 return 432000;
7296 case CDCLK_FREQ_337_308:
487ed2e4 7297 return 308571;
ea61791e
VS
7298 case CDCLK_FREQ_540:
7299 return 540000;
1652d19e 7300 case CDCLK_FREQ_675_617:
487ed2e4 7301 return 617143;
1652d19e 7302 default:
ea61791e 7303 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7304 }
7305 } else {
1652d19e
VS
7306 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
7307 case CDCLK_FREQ_450_432:
7308 return 450000;
7309 case CDCLK_FREQ_337_308:
7310 return 337500;
ea61791e
VS
7311 case CDCLK_FREQ_540:
7312 return 540000;
1652d19e
VS
7313 case CDCLK_FREQ_675_617:
7314 return 675000;
7315 default:
ea61791e 7316 MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1652d19e
VS
7317 }
7318 }
7319
709e05c3 7320 return dev_priv->cdclk_pll.ref;
1652d19e
VS
7321}
7322
83d7c81f
VS
7323static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
7324{
7325 u32 val;
7326
7327 dev_priv->cdclk_pll.ref = 19200;
1c3f7700 7328 dev_priv->cdclk_pll.vco = 0;
83d7c81f
VS
7329
7330 val = I915_READ(BXT_DE_PLL_ENABLE);
1c3f7700 7331 if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
83d7c81f 7332 return;
83d7c81f 7333
1c3f7700
ID
7334 if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
7335 return;
83d7c81f
VS
7336
7337 val = I915_READ(BXT_DE_PLL_CTL);
7338 dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
7339 dev_priv->cdclk_pll.ref;
7340}
7341
1353c4fb 7342static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
acd3f3d3 7343{
f5986242
VS
7344 u32 divider;
7345 int div, vco;
acd3f3d3 7346
83d7c81f
VS
7347 bxt_de_pll_update(dev_priv);
7348
f5986242
VS
7349 vco = dev_priv->cdclk_pll.vco;
7350 if (vco == 0)
7351 return dev_priv->cdclk_pll.ref;
acd3f3d3 7352
f5986242 7353 divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
acd3f3d3 7354
f5986242 7355 switch (divider) {
acd3f3d3 7356 case BXT_CDCLK_CD2X_DIV_SEL_1:
f5986242
VS
7357 div = 2;
7358 break;
acd3f3d3 7359 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
89b3c3c7 7360 WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
f5986242
VS
7361 div = 3;
7362 break;
acd3f3d3 7363 case BXT_CDCLK_CD2X_DIV_SEL_2:
f5986242
VS
7364 div = 4;
7365 break;
acd3f3d3 7366 case BXT_CDCLK_CD2X_DIV_SEL_4:
f5986242
VS
7367 div = 8;
7368 break;
7369 default:
7370 MISSING_CASE(divider);
7371 return dev_priv->cdclk_pll.ref;
acd3f3d3
BP
7372 }
7373
f5986242 7374 return DIV_ROUND_CLOSEST(vco, div);
acd3f3d3
BP
7375}
7376
1353c4fb 7377static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7378{
1652d19e
VS
7379 uint32_t lcpll = I915_READ(LCPLL_CTL);
7380 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7381
7382 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7383 return 800000;
7384 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7385 return 450000;
7386 else if (freq == LCPLL_CLK_FREQ_450)
7387 return 450000;
7388 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
7389 return 540000;
7390 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
7391 return 337500;
7392 else
7393 return 675000;
7394}
7395
1353c4fb 7396static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
1652d19e 7397{
1652d19e
VS
7398 uint32_t lcpll = I915_READ(LCPLL_CTL);
7399 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
7400
7401 if (lcpll & LCPLL_CD_SOURCE_FCLK)
7402 return 800000;
7403 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
7404 return 450000;
7405 else if (freq == LCPLL_CLK_FREQ_450)
7406 return 450000;
50a0bc90 7407 else if (IS_HSW_ULT(dev_priv))
1652d19e
VS
7408 return 337500;
7409 else
7410 return 540000;
79e53945
JB
7411}
7412
1353c4fb 7413static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
25eb05fc 7414{
1353c4fb 7415 return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
bfa7df01 7416 CCK_DISPLAY_CLOCK_CONTROL);
25eb05fc
JB
7417}
7418
1353c4fb 7419static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
b37a6434
VS
7420{
7421 return 450000;
7422}
7423
1353c4fb 7424static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7425{
7426 return 400000;
7427}
79e53945 7428
1353c4fb 7429static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
79e53945 7430{
e907f170 7431 return 333333;
e70236a8 7432}
79e53945 7433
1353c4fb 7434static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8
JB
7435{
7436 return 200000;
7437}
79e53945 7438
1353c4fb 7439static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
257a7ffc 7440{
1353c4fb 7441 struct pci_dev *pdev = dev_priv->drm.pdev;
257a7ffc
SV
7442 u16 gcfgc = 0;
7443
52a05c30 7444 pci_read_config_word(pdev, GCFGC, &gcfgc);
257a7ffc
SV
7445
7446 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7447 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 7448 return 266667;
257a7ffc 7449 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 7450 return 333333;
257a7ffc 7451 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 7452 return 444444;
257a7ffc
SV
7453 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
7454 return 200000;
7455 default:
7456 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
7457 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 7458 return 133333;
257a7ffc 7459 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 7460 return 166667;
257a7ffc
SV
7461 }
7462}
7463
1353c4fb 7464static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7465{
1353c4fb 7466 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7467 u16 gcfgc = 0;
79e53945 7468
52a05c30 7469 pci_read_config_word(pdev, GCFGC, &gcfgc);
e70236a8
JB
7470
7471 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 7472 return 133333;
e70236a8
JB
7473 else {
7474 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
7475 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 7476 return 333333;
e70236a8
JB
7477 default:
7478 case GC_DISPLAY_CLOCK_190_200_MHZ:
7479 return 190000;
79e53945 7480 }
e70236a8
JB
7481 }
7482}
7483
1353c4fb 7484static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7485{
e907f170 7486 return 266667;
e70236a8
JB
7487}
7488
1353c4fb 7489static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7490{
1353c4fb 7491 struct pci_dev *pdev = dev_priv->drm.pdev;
e70236a8 7492 u16 hpllcc = 0;
1b1d2716 7493
65cd2b3f
VS
7494 /*
7495 * 852GM/852GMV only supports 133 MHz and the HPLLCC
7496 * encoding is different :(
7497 * FIXME is this the right way to detect 852GM/852GMV?
7498 */
52a05c30 7499 if (pdev->revision == 0x1)
65cd2b3f
VS
7500 return 133333;
7501
52a05c30 7502 pci_bus_read_config_word(pdev->bus,
1b1d2716
VS
7503 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
7504
e70236a8
JB
7505 /* Assume that the hardware is in the high speed state. This
7506 * should be the default.
7507 */
7508 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
7509 case GC_CLOCK_133_200:
1b1d2716 7510 case GC_CLOCK_133_200_2:
e70236a8
JB
7511 case GC_CLOCK_100_200:
7512 return 200000;
7513 case GC_CLOCK_166_250:
7514 return 250000;
7515 case GC_CLOCK_100_133:
e907f170 7516 return 133333;
1b1d2716
VS
7517 case GC_CLOCK_133_266:
7518 case GC_CLOCK_133_266_2:
7519 case GC_CLOCK_166_266:
7520 return 266667;
e70236a8 7521 }
79e53945 7522
e70236a8
JB
7523 /* Shouldn't happen */
7524 return 0;
7525}
79e53945 7526
1353c4fb 7527static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
e70236a8 7528{
e907f170 7529 return 133333;
79e53945
JB
7530}
7531
1353c4fb 7532static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
34edce2f 7533{
34edce2f
VS
7534 static const unsigned int blb_vco[8] = {
7535 [0] = 3200000,
7536 [1] = 4000000,
7537 [2] = 5333333,
7538 [3] = 4800000,
7539 [4] = 6400000,
7540 };
7541 static const unsigned int pnv_vco[8] = {
7542 [0] = 3200000,
7543 [1] = 4000000,
7544 [2] = 5333333,
7545 [3] = 4800000,
7546 [4] = 2666667,
7547 };
7548 static const unsigned int cl_vco[8] = {
7549 [0] = 3200000,
7550 [1] = 4000000,
7551 [2] = 5333333,
7552 [3] = 6400000,
7553 [4] = 3333333,
7554 [5] = 3566667,
7555 [6] = 4266667,
7556 };
7557 static const unsigned int elk_vco[8] = {
7558 [0] = 3200000,
7559 [1] = 4000000,
7560 [2] = 5333333,
7561 [3] = 4800000,
7562 };
7563 static const unsigned int ctg_vco[8] = {
7564 [0] = 3200000,
7565 [1] = 4000000,
7566 [2] = 5333333,
7567 [3] = 6400000,
7568 [4] = 2666667,
7569 [5] = 4266667,
7570 };
7571 const unsigned int *vco_table;
7572 unsigned int vco;
7573 uint8_t tmp = 0;
7574
7575 /* FIXME other chipsets? */
50a0bc90 7576 if (IS_GM45(dev_priv))
34edce2f 7577 vco_table = ctg_vco;
9beb5fea 7578 else if (IS_G4X(dev_priv))
34edce2f 7579 vco_table = elk_vco;
1353c4fb 7580 else if (IS_CRESTLINE(dev_priv))
34edce2f 7581 vco_table = cl_vco;
1353c4fb 7582 else if (IS_PINEVIEW(dev_priv))
34edce2f 7583 vco_table = pnv_vco;
1353c4fb 7584 else if (IS_G33(dev_priv))
34edce2f
VS
7585 vco_table = blb_vco;
7586 else
7587 return 0;
7588
1353c4fb 7589 tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
34edce2f
VS
7590
7591 vco = vco_table[tmp & 0x7];
7592 if (vco == 0)
7593 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7594 else
7595 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7596
7597 return vco;
7598}
7599
1353c4fb 7600static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7601{
1353c4fb
VS
7602 struct pci_dev *pdev = dev_priv->drm.pdev;
7603 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7604 uint16_t tmp = 0;
7605
52a05c30 7606 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7607
7608 cdclk_sel = (tmp >> 12) & 0x1;
7609
7610 switch (vco) {
7611 case 2666667:
7612 case 4000000:
7613 case 5333333:
7614 return cdclk_sel ? 333333 : 222222;
7615 case 3200000:
7616 return cdclk_sel ? 320000 : 228571;
7617 default:
7618 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7619 return 222222;
7620 }
7621}
7622
1353c4fb 7623static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7624{
1353c4fb 7625 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7626 static const uint8_t div_3200[] = { 16, 10, 8 };
7627 static const uint8_t div_4000[] = { 20, 12, 10 };
7628 static const uint8_t div_5333[] = { 24, 16, 14 };
7629 const uint8_t *div_table;
1353c4fb 7630 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7631 uint16_t tmp = 0;
7632
52a05c30 7633 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7634
7635 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7636
7637 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7638 goto fail;
7639
7640 switch (vco) {
7641 case 3200000:
7642 div_table = div_3200;
7643 break;
7644 case 4000000:
7645 div_table = div_4000;
7646 break;
7647 case 5333333:
7648 div_table = div_5333;
7649 break;
7650 default:
7651 goto fail;
7652 }
7653
7654 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7655
caf4e252 7656fail:
34edce2f
VS
7657 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7658 return 200000;
7659}
7660
1353c4fb 7661static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
34edce2f 7662{
1353c4fb 7663 struct pci_dev *pdev = dev_priv->drm.pdev;
34edce2f
VS
7664 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7665 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7666 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7667 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7668 const uint8_t *div_table;
1353c4fb 7669 unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
34edce2f
VS
7670 uint16_t tmp = 0;
7671
52a05c30 7672 pci_read_config_word(pdev, GCFGC, &tmp);
34edce2f
VS
7673
7674 cdclk_sel = (tmp >> 4) & 0x7;
7675
7676 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7677 goto fail;
7678
7679 switch (vco) {
7680 case 3200000:
7681 div_table = div_3200;
7682 break;
7683 case 4000000:
7684 div_table = div_4000;
7685 break;
7686 case 4800000:
7687 div_table = div_4800;
7688 break;
7689 case 5333333:
7690 div_table = div_5333;
7691 break;
7692 default:
7693 goto fail;
7694 }
7695
7696 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7697
caf4e252 7698fail:
34edce2f
VS
7699 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7700 return 190476;
7701}
7702
2c07245f 7703static void
a65851af 7704intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7705{
a65851af
VS
7706 while (*num > DATA_LINK_M_N_MASK ||
7707 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7708 *num >>= 1;
7709 *den >>= 1;
7710 }
7711}
7712
a65851af
VS
7713static void compute_m_n(unsigned int m, unsigned int n,
7714 uint32_t *ret_m, uint32_t *ret_n)
7715{
7716 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7717 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7718 intel_reduce_m_n_ratio(ret_m, ret_n);
7719}
7720
e69d0bc1
SV
7721void
7722intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7723 int pixel_clock, int link_clock,
7724 struct intel_link_m_n *m_n)
2c07245f 7725{
e69d0bc1 7726 m_n->tu = 64;
a65851af
VS
7727
7728 compute_m_n(bits_per_pixel * pixel_clock,
7729 link_clock * nlanes * 8,
7730 &m_n->gmch_m, &m_n->gmch_n);
7731
7732 compute_m_n(pixel_clock, link_clock,
7733 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7734}
7735
a7615030
CW
7736static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7737{
d330a953
JN
7738 if (i915.panel_use_ssc >= 0)
7739 return i915.panel_use_ssc != 0;
41aa3448 7740 return dev_priv->vbt.lvds_use_ssc
435793df 7741 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7742}
7743
7429e9d4 7744static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7745{
7df00d7a 7746 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7747}
f47709a9 7748
7429e9d4
SV
7749static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7750{
7751 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7752}
7753
f47709a9 7754static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7755 struct intel_crtc_state *crtc_state,
9e2c8475 7756 struct dpll *reduced_clock)
a7516a05 7757{
9b1e14f4 7758 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
a7516a05
JB
7759 u32 fp, fp2 = 0;
7760
9b1e14f4 7761 if (IS_PINEVIEW(dev_priv)) {
190f68c5 7762 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7763 if (reduced_clock)
7429e9d4 7764 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7765 } else {
190f68c5 7766 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7767 if (reduced_clock)
7429e9d4 7768 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7769 }
7770
190f68c5 7771 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7772
f47709a9 7773 crtc->lowfreq_avail = false;
2d84d2b3 7774 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7775 reduced_clock) {
190f68c5 7776 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7777 crtc->lowfreq_avail = true;
a7516a05 7778 } else {
190f68c5 7779 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7780 }
7781}
7782
5e69f97f
CML
7783static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7784 pipe)
89b667f8
JB
7785{
7786 u32 reg_val;
7787
7788 /*
7789 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7790 * and set it to a reasonable value instead.
7791 */
ab3c759a 7792 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7793 reg_val &= 0xffffff00;
7794 reg_val |= 0x00000030;
ab3c759a 7795 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7796
ab3c759a 7797 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7798 reg_val &= 0x8cffffff;
7799 reg_val = 0x8c000000;
ab3c759a 7800 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7801
ab3c759a 7802 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7803 reg_val &= 0xffffff00;
ab3c759a 7804 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7805
ab3c759a 7806 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7807 reg_val &= 0x00ffffff;
7808 reg_val |= 0xb0000000;
ab3c759a 7809 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7810}
7811
b551842d
SV
7812static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7813 struct intel_link_m_n *m_n)
7814{
7815 struct drm_device *dev = crtc->base.dev;
fac5e23e 7816 struct drm_i915_private *dev_priv = to_i915(dev);
b551842d
SV
7817 int pipe = crtc->pipe;
7818
e3b95f1e
SV
7819 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7820 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7821 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7822 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
SV
7823}
7824
7825static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7826 struct intel_link_m_n *m_n,
7827 struct intel_link_m_n *m2_n2)
b551842d 7828{
6315b5d3 7829 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
b551842d 7830 int pipe = crtc->pipe;
6e3c9717 7831 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d 7832
6315b5d3 7833 if (INTEL_GEN(dev_priv) >= 5) {
b551842d
SV
7834 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7835 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7836 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7837 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7838 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7839 * for gen < 8) and if DRRS is supported (to make sure the
7840 * registers are not unnecessarily accessed).
7841 */
920a14b2
TU
7842 if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
7843 INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
f769cd24
VK
7844 I915_WRITE(PIPE_DATA_M2(transcoder),
7845 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7846 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7847 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7848 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7849 }
b551842d 7850 } else {
e3b95f1e
SV
7851 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7852 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7853 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7854 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
SV
7855 }
7856}
7857
fe3cd48d 7858void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7859{
fe3cd48d
R
7860 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7861
7862 if (m_n == M1_N1) {
7863 dp_m_n = &crtc->config->dp_m_n;
7864 dp_m2_n2 = &crtc->config->dp_m2_n2;
7865 } else if (m_n == M2_N2) {
7866
7867 /*
7868 * M2_N2 registers are not supported. Hence m2_n2 divider value
7869 * needs to be programmed into M1_N1.
7870 */
7871 dp_m_n = &crtc->config->dp_m2_n2;
7872 } else {
7873 DRM_ERROR("Unsupported divider value\n");
7874 return;
7875 }
7876
6e3c9717
ACO
7877 if (crtc->config->has_pch_encoder)
7878 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7879 else
fe3cd48d 7880 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
SV
7881}
7882
251ac862
SV
7883static void vlv_compute_dpll(struct intel_crtc *crtc,
7884 struct intel_crtc_state *pipe_config)
bdd4b6a6 7885{
03ed5cbf 7886 pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
cd2d34d9 7887 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7888 if (crtc->pipe != PIPE_A)
7889 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
bdd4b6a6 7890
cd2d34d9 7891 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7892 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7893 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
7894 DPLL_EXT_BUFFER_ENABLE_VLV;
7895
03ed5cbf
VS
7896 pipe_config->dpll_hw_state.dpll_md =
7897 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7898}
bdd4b6a6 7899
03ed5cbf
VS
7900static void chv_compute_dpll(struct intel_crtc *crtc,
7901 struct intel_crtc_state *pipe_config)
7902{
7903 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
cd2d34d9 7904 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
03ed5cbf
VS
7905 if (crtc->pipe != PIPE_A)
7906 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7907
cd2d34d9 7908 /* DPLL not used with DSI, but still need the rest set up */
d7edc4e5 7909 if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
cd2d34d9
VS
7910 pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
7911
03ed5cbf
VS
7912 pipe_config->dpll_hw_state.dpll_md =
7913 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
bdd4b6a6
SV
7914}
7915
d288f65f 7916static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7917 const struct intel_crtc_state *pipe_config)
a0c4da24 7918{
f47709a9 7919 struct drm_device *dev = crtc->base.dev;
fac5e23e 7920 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 7921 enum pipe pipe = crtc->pipe;
bdd4b6a6 7922 u32 mdiv;
a0c4da24 7923 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7924 u32 coreclk, reg_val;
a0c4da24 7925
cd2d34d9
VS
7926 /* Enable Refclk */
7927 I915_WRITE(DPLL(pipe),
7928 pipe_config->dpll_hw_state.dpll &
7929 ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
7930
7931 /* No need to actually set up the DPLL with DSI */
7932 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
7933 return;
7934
a580516d 7935 mutex_lock(&dev_priv->sb_lock);
09153000 7936
d288f65f
VS
7937 bestn = pipe_config->dpll.n;
7938 bestm1 = pipe_config->dpll.m1;
7939 bestm2 = pipe_config->dpll.m2;
7940 bestp1 = pipe_config->dpll.p1;
7941 bestp2 = pipe_config->dpll.p2;
a0c4da24 7942
89b667f8
JB
7943 /* See eDP HDMI DPIO driver vbios notes doc */
7944
7945 /* PLL B needs special handling */
bdd4b6a6 7946 if (pipe == PIPE_B)
5e69f97f 7947 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7948
7949 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7950 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7951
7952 /* Disable target IRef on PLL */
ab3c759a 7953 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7954 reg_val &= 0x00ffffff;
ab3c759a 7955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7956
7957 /* Disable fast lock */
ab3c759a 7958 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7959
7960 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7961 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7962 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7963 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7964 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7965
7966 /*
7967 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7968 * but we don't support that).
7969 * Note: don't use the DAC post divider as it seems unstable.
7970 */
7971 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7973
a0c4da24 7974 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7975 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7976
89b667f8 7977 /* Set HBR and RBR LPF coefficients */
d288f65f 7978 if (pipe_config->port_clock == 162000 ||
2d84d2b3
VS
7979 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
7980 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
ab3c759a 7981 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7982 0x009f0003);
89b667f8 7983 else
ab3c759a 7984 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7985 0x00d0000f);
7986
37a5650b 7987 if (intel_crtc_has_dp_encoder(pipe_config)) {
89b667f8 7988 /* Use SSC source */
bdd4b6a6 7989 if (pipe == PIPE_A)
ab3c759a 7990 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7991 0x0df40000);
7992 else
ab3c759a 7993 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7994 0x0df70000);
7995 } else { /* HDMI or VGA */
7996 /* Use bend source */
bdd4b6a6 7997 if (pipe == PIPE_A)
ab3c759a 7998 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7999 0x0df70000);
8000 else
ab3c759a 8001 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
8002 0x0df40000);
8003 }
a0c4da24 8004
ab3c759a 8005 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 8006 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
2210ce7f 8007 if (intel_crtc_has_dp_encoder(crtc->config))
89b667f8 8008 coreclk |= 0x01000000;
ab3c759a 8009 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 8010
ab3c759a 8011 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 8012 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
8013}
8014
d288f65f 8015static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 8016 const struct intel_crtc_state *pipe_config)
9d556c99
CML
8017{
8018 struct drm_device *dev = crtc->base.dev;
fac5e23e 8019 struct drm_i915_private *dev_priv = to_i915(dev);
cd2d34d9 8020 enum pipe pipe = crtc->pipe;
9d556c99 8021 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 8022 u32 loopfilter, tribuf_calcntr;
9d556c99 8023 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 8024 u32 dpio_val;
9cbe40c1 8025 int vco;
9d556c99 8026
cd2d34d9
VS
8027 /* Enable Refclk and SSC */
8028 I915_WRITE(DPLL(pipe),
8029 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
8030
8031 /* No need to actually set up the DPLL with DSI */
8032 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8033 return;
8034
d288f65f
VS
8035 bestn = pipe_config->dpll.n;
8036 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
8037 bestm1 = pipe_config->dpll.m1;
8038 bestm2 = pipe_config->dpll.m2 >> 22;
8039 bestp1 = pipe_config->dpll.p1;
8040 bestp2 = pipe_config->dpll.p2;
9cbe40c1 8041 vco = pipe_config->dpll.vco;
a945ce7e 8042 dpio_val = 0;
9cbe40c1 8043 loopfilter = 0;
9d556c99 8044
a580516d 8045 mutex_lock(&dev_priv->sb_lock);
9d556c99 8046
9d556c99
CML
8047 /* p1 and p2 divider */
8048 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
8049 5 << DPIO_CHV_S1_DIV_SHIFT |
8050 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
8051 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
8052 1 << DPIO_CHV_K_DIV_SHIFT);
8053
8054 /* Feedback post-divider - m2 */
8055 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
8056
8057 /* Feedback refclk divider - n and m1 */
8058 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
8059 DPIO_CHV_M1_DIV_BY_2 |
8060 1 << DPIO_CHV_N_DIV_SHIFT);
8061
8062 /* M2 fraction division */
25a25dfc 8063 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
8064
8065 /* M2 fraction division enable */
a945ce7e
VP
8066 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
8067 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
8068 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
8069 if (bestm2_frac)
8070 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
8071 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 8072
de3a0fde
VP
8073 /* Program digital lock detect threshold */
8074 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
8075 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
8076 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
8077 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
8078 if (!bestm2_frac)
8079 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
8080 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
8081
9d556c99 8082 /* Loop filter */
9cbe40c1
VP
8083 if (vco == 5400000) {
8084 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
8085 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
8086 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
8087 tribuf_calcntr = 0x9;
8088 } else if (vco <= 6200000) {
8089 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
8090 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
8091 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8092 tribuf_calcntr = 0x9;
8093 } else if (vco <= 6480000) {
8094 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8095 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8096 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8097 tribuf_calcntr = 0x8;
8098 } else {
8099 /* Not supported. Apply the same limits as in the max case */
8100 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
8101 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
8102 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
8103 tribuf_calcntr = 0;
8104 }
9d556c99
CML
8105 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
8106
968040b2 8107 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
8108 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
8109 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
8110 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
8111
9d556c99
CML
8112 /* AFC Recal */
8113 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
8114 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
8115 DPIO_AFC_RECAL);
8116
a580516d 8117 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
8118}
8119
d288f65f
VS
8120/**
8121 * vlv_force_pll_on - forcibly enable just the PLL
8122 * @dev_priv: i915 private structure
8123 * @pipe: pipe PLL to enable
8124 * @dpll: PLL configuration
8125 *
8126 * Enable the PLL for @pipe using the supplied @dpll config. To be used
8127 * in cases where we need the PLL enabled even when @pipe is not going to
8128 * be enabled.
8129 */
30ad9814 8130int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
3f36b937 8131 const struct dpll *dpll)
d288f65f 8132{
b91eb5cc 8133 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
3f36b937
TU
8134 struct intel_crtc_state *pipe_config;
8135
8136 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
8137 if (!pipe_config)
8138 return -ENOMEM;
8139
8140 pipe_config->base.crtc = &crtc->base;
8141 pipe_config->pixel_multiplier = 1;
8142 pipe_config->dpll = *dpll;
d288f65f 8143
30ad9814 8144 if (IS_CHERRYVIEW(dev_priv)) {
3f36b937
TU
8145 chv_compute_dpll(crtc, pipe_config);
8146 chv_prepare_pll(crtc, pipe_config);
8147 chv_enable_pll(crtc, pipe_config);
d288f65f 8148 } else {
3f36b937
TU
8149 vlv_compute_dpll(crtc, pipe_config);
8150 vlv_prepare_pll(crtc, pipe_config);
8151 vlv_enable_pll(crtc, pipe_config);
d288f65f 8152 }
3f36b937
TU
8153
8154 kfree(pipe_config);
8155
8156 return 0;
d288f65f
VS
8157}
8158
8159/**
8160 * vlv_force_pll_off - forcibly disable just the PLL
8161 * @dev_priv: i915 private structure
8162 * @pipe: pipe PLL to disable
8163 *
8164 * Disable the PLL for @pipe. To be used in cases where we need
8165 * the PLL enabled even when @pipe is not going to be enabled.
8166 */
30ad9814 8167void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
d288f65f 8168{
30ad9814
VS
8169 if (IS_CHERRYVIEW(dev_priv))
8170 chv_disable_pll(dev_priv, pipe);
d288f65f 8171 else
30ad9814 8172 vlv_disable_pll(dev_priv, pipe);
d288f65f
VS
8173}
8174
251ac862
SV
8175static void i9xx_compute_dpll(struct intel_crtc *crtc,
8176 struct intel_crtc_state *crtc_state,
9e2c8475 8177 struct dpll *reduced_clock)
eb1cbe48 8178{
9b1e14f4 8179 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb1cbe48 8180 u32 dpll;
190f68c5 8181 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8182
190f68c5 8183 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8184
eb1cbe48
SV
8185 dpll = DPLL_VGA_MODE_DIS;
8186
2d84d2b3 8187 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
SV
8188 dpll |= DPLLB_MODE_LVDS;
8189 else
8190 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 8191
50a0bc90 8192 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
190f68c5 8193 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 8194 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 8195 }
198a037f 8196
3d6e9ee0
VS
8197 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
8198 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 8199 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 8200
37a5650b 8201 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 8202 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
SV
8203
8204 /* compute bitmask from p1 value */
9b1e14f4 8205 if (IS_PINEVIEW(dev_priv))
eb1cbe48
SV
8206 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
8207 else {
8208 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
9beb5fea 8209 if (IS_G4X(dev_priv) && reduced_clock)
eb1cbe48
SV
8210 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8211 }
8212 switch (clock->p2) {
8213 case 5:
8214 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8215 break;
8216 case 7:
8217 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8218 break;
8219 case 10:
8220 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8221 break;
8222 case 14:
8223 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8224 break;
8225 }
9b1e14f4 8226 if (INTEL_GEN(dev_priv) >= 4)
eb1cbe48
SV
8227 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
8228
190f68c5 8229 if (crtc_state->sdvo_tv_clock)
eb1cbe48 8230 dpll |= PLL_REF_INPUT_TVCLKINBC;
2d84d2b3 8231 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8232 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8233 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8234 else
8235 dpll |= PLL_REF_INPUT_DREFCLK;
8236
8237 dpll |= DPLL_VCO_ENABLE;
190f68c5 8238 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 8239
9b1e14f4 8240 if (INTEL_GEN(dev_priv) >= 4) {
190f68c5 8241 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 8242 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 8243 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
SV
8244 }
8245}
8246
251ac862
SV
8247static void i8xx_compute_dpll(struct intel_crtc *crtc,
8248 struct intel_crtc_state *crtc_state,
9e2c8475 8249 struct dpll *reduced_clock)
eb1cbe48 8250{
f47709a9 8251 struct drm_device *dev = crtc->base.dev;
fac5e23e 8252 struct drm_i915_private *dev_priv = to_i915(dev);
eb1cbe48 8253 u32 dpll;
190f68c5 8254 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 8255
190f68c5 8256 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 8257
eb1cbe48
SV
8258 dpll = DPLL_VGA_MODE_DIS;
8259
2d84d2b3 8260 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
SV
8261 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8262 } else {
8263 if (clock->p1 == 2)
8264 dpll |= PLL_P1_DIVIDE_BY_TWO;
8265 else
8266 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8267 if (clock->p2 == 4)
8268 dpll |= PLL_P2_DIVIDE_BY_4;
8269 }
8270
50a0bc90
TU
8271 if (!IS_I830(dev_priv) &&
8272 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
SV
8273 dpll |= DPLL_DVO_2X_MODE;
8274
2d84d2b3 8275 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ceb41007 8276 intel_panel_use_ssc(dev_priv))
eb1cbe48
SV
8277 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8278 else
8279 dpll |= PLL_REF_INPUT_DREFCLK;
8280
8281 dpll |= DPLL_VCO_ENABLE;
190f68c5 8282 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
SV
8283}
8284
8a654f3b 8285static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c 8286{
6315b5d3 8287 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
b0e77b9c 8288 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8289 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7c5f93b0 8290 const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
8291 uint32_t crtc_vtotal, crtc_vblank_end;
8292 int vsyncshift = 0;
4d8a62ea
SV
8293
8294 /* We need to be careful not to changed the adjusted mode, for otherwise
8295 * the hw state checker will get angry at the mismatch. */
8296 crtc_vtotal = adjusted_mode->crtc_vtotal;
8297 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 8298
609aeaca 8299 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 8300 /* the chip adds 2 halflines automatically */
4d8a62ea
SV
8301 crtc_vtotal -= 1;
8302 crtc_vblank_end -= 1;
609aeaca 8303
2d84d2b3 8304 if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
609aeaca
VS
8305 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
8306 else
8307 vsyncshift = adjusted_mode->crtc_hsync_start -
8308 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
8309 if (vsyncshift < 0)
8310 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
8311 }
8312
6315b5d3 8313 if (INTEL_GEN(dev_priv) > 3)
fe2b8f9d 8314 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 8315
fe2b8f9d 8316 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
8317 (adjusted_mode->crtc_hdisplay - 1) |
8318 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 8319 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
8320 (adjusted_mode->crtc_hblank_start - 1) |
8321 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 8322 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
8323 (adjusted_mode->crtc_hsync_start - 1) |
8324 ((adjusted_mode->crtc_hsync_end - 1) << 16));
8325
fe2b8f9d 8326 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 8327 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 8328 ((crtc_vtotal - 1) << 16));
fe2b8f9d 8329 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 8330 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 8331 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 8332 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
8333 (adjusted_mode->crtc_vsync_start - 1) |
8334 ((adjusted_mode->crtc_vsync_end - 1) << 16));
8335
b5e508d4
PZ
8336 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
8337 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
8338 * documented on the DDI_FUNC_CTL register description, EDP Input Select
8339 * bits. */
772c2a51 8340 if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
b5e508d4
PZ
8341 (pipe == PIPE_B || pipe == PIPE_C))
8342 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
8343
bc58be60
JN
8344}
8345
8346static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
8347{
8348 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 8349 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60
JN
8350 enum pipe pipe = intel_crtc->pipe;
8351
b0e77b9c
PZ
8352 /* pipesrc controls the size that is scaled from, which should
8353 * always be the user's requested size.
8354 */
8355 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
8356 ((intel_crtc->config->pipe_src_w - 1) << 16) |
8357 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
8358}
8359
1bd1bd80 8360static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 8361 struct intel_crtc_state *pipe_config)
1bd1bd80
SV
8362{
8363 struct drm_device *dev = crtc->base.dev;
fac5e23e 8364 struct drm_i915_private *dev_priv = to_i915(dev);
1bd1bd80
SV
8365 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
8366 uint32_t tmp;
8367
8368 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
8369 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
8370 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8371 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
8372 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
8373 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8374 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
8375 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
8376 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8377
8378 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
8379 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
8380 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8381 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
8382 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
8383 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 8384 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
8385 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
8386 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
SV
8387
8388 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
8389 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
8390 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
8391 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80 8392 }
bc58be60
JN
8393}
8394
8395static void intel_get_pipe_src_size(struct intel_crtc *crtc,
8396 struct intel_crtc_state *pipe_config)
8397{
8398 struct drm_device *dev = crtc->base.dev;
fac5e23e 8399 struct drm_i915_private *dev_priv = to_i915(dev);
bc58be60 8400 u32 tmp;
1bd1bd80
SV
8401
8402 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
8403 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
8404 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
8405
2d112de7
ACO
8406 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
8407 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
SV
8408}
8409
f6a83288 8410void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 8411 struct intel_crtc_state *pipe_config)
babea61d 8412{
2d112de7
ACO
8413 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
8414 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
8415 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
8416 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 8417
2d112de7
ACO
8418 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
8419 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
8420 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
8421 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 8422
2d112de7 8423 mode->flags = pipe_config->base.adjusted_mode.flags;
cd13f5ab 8424 mode->type = DRM_MODE_TYPE_DRIVER;
babea61d 8425
2d112de7
ACO
8426 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
8427 mode->flags |= pipe_config->base.adjusted_mode.flags;
cd13f5ab
ML
8428
8429 mode->hsync = drm_mode_hsync(mode);
8430 mode->vrefresh = drm_mode_vrefresh(mode);
8431 drm_mode_set_name(mode);
babea61d
JB
8432}
8433
84b046f3
SV
8434static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
8435{
6315b5d3 8436 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
84b046f3
SV
8437 uint32_t pipeconf;
8438
9f11a9e4 8439 pipeconf = 0;
84b046f3 8440
b6b5d049
VS
8441 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
8442 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8443 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 8444
6e3c9717 8445 if (intel_crtc->config->double_wide)
cf532bb2 8446 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 8447
ff9ce46e 8448 /* only g4x and later have fancy bpc/dither controls */
9beb5fea
TU
8449 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8450 IS_CHERRYVIEW(dev_priv)) {
ff9ce46e 8451 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 8452 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 8453 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 8454 PIPECONF_DITHER_TYPE_SP;
84b046f3 8455
6e3c9717 8456 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
SV
8457 case 18:
8458 pipeconf |= PIPECONF_6BPC;
8459 break;
8460 case 24:
8461 pipeconf |= PIPECONF_8BPC;
8462 break;
8463 case 30:
8464 pipeconf |= PIPECONF_10BPC;
8465 break;
8466 default:
8467 /* Case prevented by intel_choose_pipe_bpp_dither. */
8468 BUG();
84b046f3
SV
8469 }
8470 }
8471
56b857a5 8472 if (HAS_PIPE_CXSR(dev_priv)) {
84b046f3
SV
8473 if (intel_crtc->lowfreq_avail) {
8474 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
8475 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
8476 } else {
8477 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
SV
8478 }
8479 }
8480
6e3c9717 8481 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6315b5d3 8482 if (INTEL_GEN(dev_priv) < 4 ||
2d84d2b3 8483 intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
efc2cfff
VS
8484 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
8485 else
8486 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
8487 } else
84b046f3
SV
8488 pipeconf |= PIPECONF_PROGRESSIVE;
8489
920a14b2 8490 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8491 intel_crtc->config->limited_color_range)
9f11a9e4 8492 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 8493
84b046f3
SV
8494 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
8495 POSTING_READ(PIPECONF(intel_crtc->pipe));
8496}
8497
81c97f52
ACO
8498static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
8499 struct intel_crtc_state *crtc_state)
8500{
8501 struct drm_device *dev = crtc->base.dev;
fac5e23e 8502 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8503 const struct intel_limit *limit;
81c97f52
ACO
8504 int refclk = 48000;
8505
8506 memset(&crtc_state->dpll_hw_state, 0,
8507 sizeof(crtc_state->dpll_hw_state));
8508
2d84d2b3 8509 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
81c97f52
ACO
8510 if (intel_panel_use_ssc(dev_priv)) {
8511 refclk = dev_priv->vbt.lvds_ssc_freq;
8512 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8513 }
8514
8515 limit = &intel_limits_i8xx_lvds;
2d84d2b3 8516 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
81c97f52
ACO
8517 limit = &intel_limits_i8xx_dvo;
8518 } else {
8519 limit = &intel_limits_i8xx_dac;
8520 }
8521
8522 if (!crtc_state->clock_set &&
8523 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8524 refclk, NULL, &crtc_state->dpll)) {
8525 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8526 return -EINVAL;
8527 }
8528
8529 i8xx_compute_dpll(crtc, crtc_state, NULL);
8530
8531 return 0;
8532}
8533
19ec6693
ACO
8534static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
8535 struct intel_crtc_state *crtc_state)
8536{
8537 struct drm_device *dev = crtc->base.dev;
fac5e23e 8538 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8539 const struct intel_limit *limit;
19ec6693
ACO
8540 int refclk = 96000;
8541
8542 memset(&crtc_state->dpll_hw_state, 0,
8543 sizeof(crtc_state->dpll_hw_state));
8544
2d84d2b3 8545 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
19ec6693
ACO
8546 if (intel_panel_use_ssc(dev_priv)) {
8547 refclk = dev_priv->vbt.lvds_ssc_freq;
8548 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8549 }
8550
8551 if (intel_is_dual_link_lvds(dev))
8552 limit = &intel_limits_g4x_dual_channel_lvds;
8553 else
8554 limit = &intel_limits_g4x_single_channel_lvds;
2d84d2b3
VS
8555 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
8556 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
19ec6693 8557 limit = &intel_limits_g4x_hdmi;
2d84d2b3 8558 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
19ec6693
ACO
8559 limit = &intel_limits_g4x_sdvo;
8560 } else {
8561 /* The option is for other outputs */
8562 limit = &intel_limits_i9xx_sdvo;
8563 }
8564
8565 if (!crtc_state->clock_set &&
8566 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8567 refclk, NULL, &crtc_state->dpll)) {
8568 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8569 return -EINVAL;
8570 }
8571
8572 i9xx_compute_dpll(crtc, crtc_state, NULL);
8573
8574 return 0;
8575}
8576
70e8aa21
ACO
8577static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
8578 struct intel_crtc_state *crtc_state)
8579{
8580 struct drm_device *dev = crtc->base.dev;
fac5e23e 8581 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8582 const struct intel_limit *limit;
70e8aa21
ACO
8583 int refclk = 96000;
8584
8585 memset(&crtc_state->dpll_hw_state, 0,
8586 sizeof(crtc_state->dpll_hw_state));
8587
2d84d2b3 8588 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8589 if (intel_panel_use_ssc(dev_priv)) {
8590 refclk = dev_priv->vbt.lvds_ssc_freq;
8591 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8592 }
8593
8594 limit = &intel_limits_pineview_lvds;
8595 } else {
8596 limit = &intel_limits_pineview_sdvo;
8597 }
8598
8599 if (!crtc_state->clock_set &&
8600 !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8601 refclk, NULL, &crtc_state->dpll)) {
8602 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8603 return -EINVAL;
8604 }
8605
8606 i9xx_compute_dpll(crtc, crtc_state, NULL);
8607
8608 return 0;
8609}
8610
190f68c5
ACO
8611static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
8612 struct intel_crtc_state *crtc_state)
79e53945 8613{
c7653199 8614 struct drm_device *dev = crtc->base.dev;
fac5e23e 8615 struct drm_i915_private *dev_priv = to_i915(dev);
1b6f4958 8616 const struct intel_limit *limit;
81c97f52 8617 int refclk = 96000;
79e53945 8618
dd3cd74a
ACO
8619 memset(&crtc_state->dpll_hw_state, 0,
8620 sizeof(crtc_state->dpll_hw_state));
8621
2d84d2b3 8622 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
70e8aa21
ACO
8623 if (intel_panel_use_ssc(dev_priv)) {
8624 refclk = dev_priv->vbt.lvds_ssc_freq;
8625 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
8626 }
43565a06 8627
70e8aa21
ACO
8628 limit = &intel_limits_i9xx_lvds;
8629 } else {
8630 limit = &intel_limits_i9xx_sdvo;
81c97f52 8631 }
79e53945 8632
70e8aa21
ACO
8633 if (!crtc_state->clock_set &&
8634 !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8635 refclk, NULL, &crtc_state->dpll)) {
8636 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8637 return -EINVAL;
f47709a9 8638 }
7026d4ac 8639
81c97f52 8640 i9xx_compute_dpll(crtc, crtc_state, NULL);
79e53945 8641
c8f7a0db 8642 return 0;
f564048e
EA
8643}
8644
65b3d6a9
ACO
8645static int chv_crtc_compute_clock(struct intel_crtc *crtc,
8646 struct intel_crtc_state *crtc_state)
8647{
8648 int refclk = 100000;
1b6f4958 8649 const struct intel_limit *limit = &intel_limits_chv;
65b3d6a9
ACO
8650
8651 memset(&crtc_state->dpll_hw_state, 0,
8652 sizeof(crtc_state->dpll_hw_state));
8653
65b3d6a9
ACO
8654 if (!crtc_state->clock_set &&
8655 !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8656 refclk, NULL, &crtc_state->dpll)) {
8657 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8658 return -EINVAL;
8659 }
8660
8661 chv_compute_dpll(crtc, crtc_state);
8662
8663 return 0;
8664}
8665
8666static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
8667 struct intel_crtc_state *crtc_state)
8668{
8669 int refclk = 100000;
1b6f4958 8670 const struct intel_limit *limit = &intel_limits_vlv;
65b3d6a9
ACO
8671
8672 memset(&crtc_state->dpll_hw_state, 0,
8673 sizeof(crtc_state->dpll_hw_state));
8674
65b3d6a9
ACO
8675 if (!crtc_state->clock_set &&
8676 !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
8677 refclk, NULL, &crtc_state->dpll)) {
8678 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8679 return -EINVAL;
8680 }
8681
8682 vlv_compute_dpll(crtc, crtc_state);
8683
8684 return 0;
8685}
8686
2fa2fe9a 8687static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8688 struct intel_crtc_state *pipe_config)
2fa2fe9a 8689{
6315b5d3 8690 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2fa2fe9a
SV
8691 uint32_t tmp;
8692
50a0bc90
TU
8693 if (INTEL_GEN(dev_priv) <= 3 &&
8694 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
dc9e7dec
VS
8695 return;
8696
2fa2fe9a 8697 tmp = I915_READ(PFIT_CONTROL);
06922821
SV
8698 if (!(tmp & PFIT_ENABLE))
8699 return;
2fa2fe9a 8700
06922821 8701 /* Check whether the pfit is attached to our pipe. */
6315b5d3 8702 if (INTEL_GEN(dev_priv) < 4) {
2fa2fe9a
SV
8703 if (crtc->pipe != PIPE_B)
8704 return;
2fa2fe9a
SV
8705 } else {
8706 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8707 return;
8708 }
8709
06922821 8710 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a 8711 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
2fa2fe9a
SV
8712}
8713
acbec814 8714static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8715 struct intel_crtc_state *pipe_config)
acbec814
JB
8716{
8717 struct drm_device *dev = crtc->base.dev;
fac5e23e 8718 struct drm_i915_private *dev_priv = to_i915(dev);
acbec814 8719 int pipe = pipe_config->cpu_transcoder;
9e2c8475 8720 struct dpll clock;
acbec814 8721 u32 mdiv;
662c6ecb 8722 int refclk = 100000;
acbec814 8723
b521973b
VS
8724 /* In case of DSI, DPLL will not be used */
8725 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
f573de5a
SK
8726 return;
8727
a580516d 8728 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8729 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8730 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8731
8732 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8733 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8734 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8735 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8736 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8737
dccbea3b 8738 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
acbec814
JB
8739}
8740
5724dbd1
DL
8741static void
8742i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8743 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8744{
8745 struct drm_device *dev = crtc->base.dev;
fac5e23e 8746 struct drm_i915_private *dev_priv = to_i915(dev);
1ad292b5
JB
8747 u32 val, base, offset;
8748 int pipe = crtc->pipe, plane = crtc->plane;
8749 int fourcc, pixel_format;
6761dd31 8750 unsigned int aligned_height;
b113d5ee 8751 struct drm_framebuffer *fb;
1b842c89 8752 struct intel_framebuffer *intel_fb;
1ad292b5 8753
42a7b088
DL
8754 val = I915_READ(DSPCNTR(plane));
8755 if (!(val & DISPLAY_PLANE_ENABLE))
8756 return;
8757
d9806c9f 8758 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8759 if (!intel_fb) {
1ad292b5
JB
8760 DRM_DEBUG_KMS("failed to alloc fb\n");
8761 return;
8762 }
8763
1b842c89
DL
8764 fb = &intel_fb->base;
8765
6315b5d3 8766 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 8767 if (val & DISPPLANE_TILED) {
49af449b 8768 plane_config->tiling = I915_TILING_X;
bae781b2 8769 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
SV
8770 }
8771 }
1ad292b5
JB
8772
8773 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8774 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8775 fb->pixel_format = fourcc;
8776 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5 8777
6315b5d3 8778 if (INTEL_GEN(dev_priv) >= 4) {
49af449b 8779 if (plane_config->tiling)
1ad292b5
JB
8780 offset = I915_READ(DSPTILEOFF(plane));
8781 else
8782 offset = I915_READ(DSPLINOFF(plane));
8783 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8784 } else {
8785 base = I915_READ(DSPADDR(plane));
8786 }
8787 plane_config->base = base;
8788
8789 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8790 fb->width = ((val >> 16) & 0xfff) + 1;
8791 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8792
8793 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8794 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8795
b113d5ee 8796 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 8797 fb->pixel_format,
bae781b2 8798 fb->modifier);
1ad292b5 8799
f37b5c2b 8800 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8801
2844a921
DL
8802 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8803 pipe_name(pipe), plane, fb->width, fb->height,
8804 fb->bits_per_pixel, base, fb->pitches[0],
8805 plane_config->size);
1ad292b5 8806
2d14030b 8807 plane_config->fb = intel_fb;
1ad292b5
JB
8808}
8809
70b23a98 8810static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8811 struct intel_crtc_state *pipe_config)
70b23a98
VS
8812{
8813 struct drm_device *dev = crtc->base.dev;
fac5e23e 8814 struct drm_i915_private *dev_priv = to_i915(dev);
70b23a98
VS
8815 int pipe = pipe_config->cpu_transcoder;
8816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9e2c8475 8817 struct dpll clock;
0d7b6b11 8818 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
70b23a98
VS
8819 int refclk = 100000;
8820
b521973b
VS
8821 /* In case of DSI, DPLL will not be used */
8822 if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
8823 return;
8824
a580516d 8825 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8826 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8827 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8828 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8829 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
0d7b6b11 8830 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
a580516d 8831 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8832
8833 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
0d7b6b11
ID
8834 clock.m2 = (pll_dw0 & 0xff) << 22;
8835 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
8836 clock.m2 |= pll_dw2 & 0x3fffff;
70b23a98
VS
8837 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8838 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8839 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8840
dccbea3b 8841 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
70b23a98
VS
8842}
8843
0e8ffe1b 8844static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8845 struct intel_crtc_state *pipe_config)
0e8ffe1b 8846{
6315b5d3 8847 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e 8848 enum intel_display_power_domain power_domain;
0e8ffe1b 8849 uint32_t tmp;
1729050e 8850 bool ret;
0e8ffe1b 8851
1729050e
ID
8852 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
8853 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0
ID
8854 return false;
8855
e143a21c 8856 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 8857 pipe_config->shared_dpll = NULL;
eccb140b 8858
1729050e
ID
8859 ret = false;
8860
0e8ffe1b
SV
8861 tmp = I915_READ(PIPECONF(crtc->pipe));
8862 if (!(tmp & PIPECONF_ENABLE))
1729050e 8863 goto out;
0e8ffe1b 8864
9beb5fea
TU
8865 if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8866 IS_CHERRYVIEW(dev_priv)) {
42571aef
VS
8867 switch (tmp & PIPECONF_BPC_MASK) {
8868 case PIPECONF_6BPC:
8869 pipe_config->pipe_bpp = 18;
8870 break;
8871 case PIPECONF_8BPC:
8872 pipe_config->pipe_bpp = 24;
8873 break;
8874 case PIPECONF_10BPC:
8875 pipe_config->pipe_bpp = 30;
8876 break;
8877 default:
8878 break;
8879 }
8880 }
8881
920a14b2 8882 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
666a4537 8883 (tmp & PIPECONF_COLOR_RANGE_SELECT))
b5a9fa09
SV
8884 pipe_config->limited_color_range = true;
8885
6315b5d3 8886 if (INTEL_GEN(dev_priv) < 4)
282740f7
VS
8887 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8888
1bd1bd80 8889 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 8890 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 8891
2fa2fe9a
SV
8892 i9xx_get_pfit_config(crtc, pipe_config);
8893
6315b5d3 8894 if (INTEL_GEN(dev_priv) >= 4) {
c231775c 8895 /* No way to read it out on pipes B and C */
920a14b2 8896 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
c231775c
VS
8897 tmp = dev_priv->chv_dpll_md[crtc->pipe];
8898 else
8899 tmp = I915_READ(DPLL_MD(crtc->pipe));
6c49f241
SV
8900 pipe_config->pixel_multiplier =
8901 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8902 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8903 pipe_config->dpll_hw_state.dpll_md = tmp;
50a0bc90
TU
8904 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
8905 IS_G33(dev_priv)) {
6c49f241
SV
8906 tmp = I915_READ(DPLL(crtc->pipe));
8907 pipe_config->pixel_multiplier =
8908 ((tmp & SDVO_MULTIPLIER_MASK)
8909 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8910 } else {
8911 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8912 * port and will be fixed up in the encoder->get_config
8913 * function. */
8914 pipe_config->pixel_multiplier = 1;
8915 }
8bcc2795 8916 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
920a14b2 8917 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1c4e0274
VS
8918 /*
8919 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8920 * on 830. Filter it out here so that we don't
8921 * report errors due to that.
8922 */
50a0bc90 8923 if (IS_I830(dev_priv))
1c4e0274
VS
8924 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8925
8bcc2795
SV
8926 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8927 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8928 } else {
8929 /* Mask out read-only status bits. */
8930 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8931 DPLL_PORTC_READY_MASK |
8932 DPLL_PORTB_READY_MASK);
8bcc2795 8933 }
6c49f241 8934
920a14b2 8935 if (IS_CHERRYVIEW(dev_priv))
70b23a98 8936 chv_crtc_clock_get(crtc, pipe_config);
11a914c2 8937 else if (IS_VALLEYVIEW(dev_priv))
acbec814
JB
8938 vlv_crtc_clock_get(crtc, pipe_config);
8939 else
8940 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8941
0f64614d
VS
8942 /*
8943 * Normally the dotclock is filled in by the encoder .get_config()
8944 * but in case the pipe is enabled w/o any ports we need a sane
8945 * default.
8946 */
8947 pipe_config->base.adjusted_mode.crtc_clock =
8948 pipe_config->port_clock / pipe_config->pixel_multiplier;
8949
1729050e
ID
8950 ret = true;
8951
8952out:
8953 intel_display_power_put(dev_priv, power_domain);
8954
8955 return ret;
0e8ffe1b
SV
8956}
8957
c39055b0 8958static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
13d83a67 8959{
13d83a67 8960 struct intel_encoder *encoder;
1c1a24d2 8961 int i;
74cfd7ac 8962 u32 val, final;
13d83a67 8963 bool has_lvds = false;
199e5d79 8964 bool has_cpu_edp = false;
199e5d79 8965 bool has_panel = false;
99eb6a01
KP
8966 bool has_ck505 = false;
8967 bool can_ssc = false;
1c1a24d2 8968 bool using_ssc_source = false;
13d83a67
JB
8969
8970 /* We need to take the global config into account */
c39055b0 8971 for_each_intel_encoder(&dev_priv->drm, encoder) {
199e5d79
KP
8972 switch (encoder->type) {
8973 case INTEL_OUTPUT_LVDS:
8974 has_panel = true;
8975 has_lvds = true;
8976 break;
8977 case INTEL_OUTPUT_EDP:
8978 has_panel = true;
2de6905f 8979 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8980 has_cpu_edp = true;
8981 break;
6847d71b
PZ
8982 default:
8983 break;
13d83a67
JB
8984 }
8985 }
8986
6e266956 8987 if (HAS_PCH_IBX(dev_priv)) {
41aa3448 8988 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8989 can_ssc = has_ck505;
8990 } else {
8991 has_ck505 = false;
8992 can_ssc = true;
8993 }
8994
1c1a24d2
L
8995 /* Check if any DPLLs are using the SSC source */
8996 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8997 u32 temp = I915_READ(PCH_DPLL(i));
8998
8999 if (!(temp & DPLL_VCO_ENABLE))
9000 continue;
9001
9002 if ((temp & PLL_REF_INPUT_MASK) ==
9003 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
9004 using_ssc_source = true;
9005 break;
9006 }
9007 }
9008
9009 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
9010 has_panel, has_lvds, has_ck505, using_ssc_source);
13d83a67
JB
9011
9012 /* Ironlake: try to setup display ref clock before DPLL
9013 * enabling. This is only under driver's control after
9014 * PCH B stepping, previous chipset stepping should be
9015 * ignoring this setting.
9016 */
74cfd7ac
CW
9017 val = I915_READ(PCH_DREF_CONTROL);
9018
9019 /* As we must carefully and slowly disable/enable each source in turn,
9020 * compute the final state we want first and check if we need to
9021 * make any changes at all.
9022 */
9023 final = val;
9024 final &= ~DREF_NONSPREAD_SOURCE_MASK;
9025 if (has_ck505)
9026 final |= DREF_NONSPREAD_CK505_ENABLE;
9027 else
9028 final |= DREF_NONSPREAD_SOURCE_ENABLE;
9029
8c07eb68 9030 final &= ~DREF_SSC_SOURCE_MASK;
74cfd7ac 9031 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8c07eb68 9032 final &= ~DREF_SSC1_ENABLE;
74cfd7ac
CW
9033
9034 if (has_panel) {
9035 final |= DREF_SSC_SOURCE_ENABLE;
9036
9037 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9038 final |= DREF_SSC1_ENABLE;
9039
9040 if (has_cpu_edp) {
9041 if (intel_panel_use_ssc(dev_priv) && can_ssc)
9042 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
9043 else
9044 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
9045 } else
9046 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
1c1a24d2
L
9047 } else if (using_ssc_source) {
9048 final |= DREF_SSC_SOURCE_ENABLE;
9049 final |= DREF_SSC1_ENABLE;
74cfd7ac
CW
9050 }
9051
9052 if (final == val)
9053 return;
9054
13d83a67 9055 /* Always enable nonspread source */
74cfd7ac 9056 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 9057
99eb6a01 9058 if (has_ck505)
74cfd7ac 9059 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 9060 else
74cfd7ac 9061 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 9062
199e5d79 9063 if (has_panel) {
74cfd7ac
CW
9064 val &= ~DREF_SSC_SOURCE_MASK;
9065 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 9066
199e5d79 9067 /* SSC must be turned on before enabling the CPU output */
99eb6a01 9068 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9069 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 9070 val |= DREF_SSC1_ENABLE;
e77166b5 9071 } else
74cfd7ac 9072 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
9073
9074 /* Get SSC going before enabling the outputs */
74cfd7ac 9075 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9076 POSTING_READ(PCH_DREF_CONTROL);
9077 udelay(200);
9078
74cfd7ac 9079 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
9080
9081 /* Enable CPU source on CPU attached eDP */
199e5d79 9082 if (has_cpu_edp) {
99eb6a01 9083 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 9084 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 9085 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 9086 } else
74cfd7ac 9087 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 9088 } else
74cfd7ac 9089 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9090
74cfd7ac 9091 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9092 POSTING_READ(PCH_DREF_CONTROL);
9093 udelay(200);
9094 } else {
1c1a24d2 9095 DRM_DEBUG_KMS("Disabling CPU source output\n");
199e5d79 9096
74cfd7ac 9097 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
9098
9099 /* Turn off CPU output */
74cfd7ac 9100 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 9101
74cfd7ac 9102 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
9103 POSTING_READ(PCH_DREF_CONTROL);
9104 udelay(200);
9105
1c1a24d2
L
9106 if (!using_ssc_source) {
9107 DRM_DEBUG_KMS("Disabling SSC source\n");
199e5d79 9108
1c1a24d2
L
9109 /* Turn off the SSC source */
9110 val &= ~DREF_SSC_SOURCE_MASK;
9111 val |= DREF_SSC_SOURCE_DISABLE;
f165d283 9112
1c1a24d2
L
9113 /* Turn off SSC1 */
9114 val &= ~DREF_SSC1_ENABLE;
9115
9116 I915_WRITE(PCH_DREF_CONTROL, val);
9117 POSTING_READ(PCH_DREF_CONTROL);
9118 udelay(200);
9119 }
13d83a67 9120 }
74cfd7ac
CW
9121
9122 BUG_ON(val != final);
13d83a67
JB
9123}
9124
f31f2d55 9125static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 9126{
f31f2d55 9127 uint32_t tmp;
dde86e2d 9128
0ff066a9
PZ
9129 tmp = I915_READ(SOUTH_CHICKEN2);
9130 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
9131 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9132
cf3598c2
ID
9133 if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
9134 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
0ff066a9 9135 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 9136
0ff066a9
PZ
9137 tmp = I915_READ(SOUTH_CHICKEN2);
9138 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
9139 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 9140
cf3598c2
ID
9141 if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
9142 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
0ff066a9 9143 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
9144}
9145
9146/* WaMPhyProgramming:hsw */
9147static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
9148{
9149 uint32_t tmp;
dde86e2d
PZ
9150
9151 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
9152 tmp &= ~(0xFF << 24);
9153 tmp |= (0x12 << 24);
9154 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
9155
dde86e2d
PZ
9156 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
9157 tmp |= (1 << 11);
9158 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
9159
9160 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
9161 tmp |= (1 << 11);
9162 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
9163
dde86e2d
PZ
9164 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
9165 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9166 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
9167
9168 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
9169 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
9170 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
9171
0ff066a9
PZ
9172 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
9173 tmp &= ~(7 << 13);
9174 tmp |= (5 << 13);
9175 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 9176
0ff066a9
PZ
9177 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
9178 tmp &= ~(7 << 13);
9179 tmp |= (5 << 13);
9180 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
9181
9182 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
9183 tmp &= ~0xFF;
9184 tmp |= 0x1C;
9185 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
9186
9187 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
9188 tmp &= ~0xFF;
9189 tmp |= 0x1C;
9190 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
9191
9192 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
9193 tmp &= ~(0xFF << 16);
9194 tmp |= (0x1C << 16);
9195 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
9196
9197 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
9198 tmp &= ~(0xFF << 16);
9199 tmp |= (0x1C << 16);
9200 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
9201
0ff066a9
PZ
9202 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
9203 tmp |= (1 << 27);
9204 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 9205
0ff066a9
PZ
9206 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
9207 tmp |= (1 << 27);
9208 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 9209
0ff066a9
PZ
9210 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
9211 tmp &= ~(0xF << 28);
9212 tmp |= (4 << 28);
9213 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 9214
0ff066a9
PZ
9215 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
9216 tmp &= ~(0xF << 28);
9217 tmp |= (4 << 28);
9218 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
9219}
9220
2fa86a1f
PZ
9221/* Implements 3 different sequences from BSpec chapter "Display iCLK
9222 * Programming" based on the parameters passed:
9223 * - Sequence to enable CLKOUT_DP
9224 * - Sequence to enable CLKOUT_DP without spread
9225 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
9226 */
c39055b0
ACO
9227static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
9228 bool with_spread, bool with_fdi)
f31f2d55 9229{
2fa86a1f
PZ
9230 uint32_t reg, tmp;
9231
9232 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
9233 with_spread = true;
4f8036a2
TU
9234 if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
9235 with_fdi, "LP PCH doesn't have FDI\n"))
2fa86a1f 9236 with_fdi = false;
f31f2d55 9237
a580516d 9238 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
9239
9240 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9241 tmp &= ~SBI_SSCCTL_DISABLE;
9242 tmp |= SBI_SSCCTL_PATHALT;
9243 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9244
9245 udelay(24);
9246
2fa86a1f
PZ
9247 if (with_spread) {
9248 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9249 tmp &= ~SBI_SSCCTL_PATHALT;
9250 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 9251
2fa86a1f
PZ
9252 if (with_fdi) {
9253 lpt_reset_fdi_mphy(dev_priv);
9254 lpt_program_fdi_mphy(dev_priv);
9255 }
9256 }
dde86e2d 9257
4f8036a2 9258 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
2fa86a1f
PZ
9259 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9260 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9261 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 9262
a580516d 9263 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
9264}
9265
47701c3b 9266/* Sequence to disable CLKOUT_DP */
c39055b0 9267static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
47701c3b 9268{
47701c3b
PZ
9269 uint32_t reg, tmp;
9270
a580516d 9271 mutex_lock(&dev_priv->sb_lock);
47701c3b 9272
4f8036a2 9273 reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
47701c3b
PZ
9274 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
9275 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
9276 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
9277
9278 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
9279 if (!(tmp & SBI_SSCCTL_DISABLE)) {
9280 if (!(tmp & SBI_SSCCTL_PATHALT)) {
9281 tmp |= SBI_SSCCTL_PATHALT;
9282 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9283 udelay(32);
9284 }
9285 tmp |= SBI_SSCCTL_DISABLE;
9286 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
9287 }
9288
a580516d 9289 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
9290}
9291
f7be2c21
VS
9292#define BEND_IDX(steps) ((50 + (steps)) / 5)
9293
9294static const uint16_t sscdivintphase[] = {
9295 [BEND_IDX( 50)] = 0x3B23,
9296 [BEND_IDX( 45)] = 0x3B23,
9297 [BEND_IDX( 40)] = 0x3C23,
9298 [BEND_IDX( 35)] = 0x3C23,
9299 [BEND_IDX( 30)] = 0x3D23,
9300 [BEND_IDX( 25)] = 0x3D23,
9301 [BEND_IDX( 20)] = 0x3E23,
9302 [BEND_IDX( 15)] = 0x3E23,
9303 [BEND_IDX( 10)] = 0x3F23,
9304 [BEND_IDX( 5)] = 0x3F23,
9305 [BEND_IDX( 0)] = 0x0025,
9306 [BEND_IDX( -5)] = 0x0025,
9307 [BEND_IDX(-10)] = 0x0125,
9308 [BEND_IDX(-15)] = 0x0125,
9309 [BEND_IDX(-20)] = 0x0225,
9310 [BEND_IDX(-25)] = 0x0225,
9311 [BEND_IDX(-30)] = 0x0325,
9312 [BEND_IDX(-35)] = 0x0325,
9313 [BEND_IDX(-40)] = 0x0425,
9314 [BEND_IDX(-45)] = 0x0425,
9315 [BEND_IDX(-50)] = 0x0525,
9316};
9317
9318/*
9319 * Bend CLKOUT_DP
9320 * steps -50 to 50 inclusive, in steps of 5
9321 * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
9322 * change in clock period = -(steps / 10) * 5.787 ps
9323 */
9324static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
9325{
9326 uint32_t tmp;
9327 int idx = BEND_IDX(steps);
9328
9329 if (WARN_ON(steps % 5 != 0))
9330 return;
9331
9332 if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
9333 return;
9334
9335 mutex_lock(&dev_priv->sb_lock);
9336
9337 if (steps % 10 != 0)
9338 tmp = 0xAAAAAAAB;
9339 else
9340 tmp = 0x00000000;
9341 intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
9342
9343 tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
9344 tmp &= 0xffff0000;
9345 tmp |= sscdivintphase[idx];
9346 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
9347
9348 mutex_unlock(&dev_priv->sb_lock);
9349}
9350
9351#undef BEND_IDX
9352
c39055b0 9353static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
bf8fa3d3 9354{
bf8fa3d3
PZ
9355 struct intel_encoder *encoder;
9356 bool has_vga = false;
9357
c39055b0 9358 for_each_intel_encoder(&dev_priv->drm, encoder) {
bf8fa3d3
PZ
9359 switch (encoder->type) {
9360 case INTEL_OUTPUT_ANALOG:
9361 has_vga = true;
9362 break;
6847d71b
PZ
9363 default:
9364 break;
bf8fa3d3
PZ
9365 }
9366 }
9367
f7be2c21 9368 if (has_vga) {
c39055b0
ACO
9369 lpt_bend_clkout_dp(dev_priv, 0);
9370 lpt_enable_clkout_dp(dev_priv, true, true);
f7be2c21 9371 } else {
c39055b0 9372 lpt_disable_clkout_dp(dev_priv);
f7be2c21 9373 }
bf8fa3d3
PZ
9374}
9375
dde86e2d
PZ
9376/*
9377 * Initialize reference clocks when the driver loads
9378 */
c39055b0 9379void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
dde86e2d 9380{
6e266956 9381 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
c39055b0 9382 ironlake_init_pch_refclk(dev_priv);
6e266956 9383 else if (HAS_PCH_LPT(dev_priv))
c39055b0 9384 lpt_init_pch_refclk(dev_priv);
dde86e2d
PZ
9385}
9386
6ff93609 9387static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 9388{
fac5e23e 9389 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
79e53945
JB
9390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9391 int pipe = intel_crtc->pipe;
c8203565
PZ
9392 uint32_t val;
9393
78114071 9394 val = 0;
c8203565 9395
6e3c9717 9396 switch (intel_crtc->config->pipe_bpp) {
c8203565 9397 case 18:
dfd07d72 9398 val |= PIPECONF_6BPC;
c8203565
PZ
9399 break;
9400 case 24:
dfd07d72 9401 val |= PIPECONF_8BPC;
c8203565
PZ
9402 break;
9403 case 30:
dfd07d72 9404 val |= PIPECONF_10BPC;
c8203565
PZ
9405 break;
9406 case 36:
dfd07d72 9407 val |= PIPECONF_12BPC;
c8203565
PZ
9408 break;
9409 default:
cc769b62
PZ
9410 /* Case prevented by intel_choose_pipe_bpp_dither. */
9411 BUG();
c8203565
PZ
9412 }
9413
6e3c9717 9414 if (intel_crtc->config->dither)
c8203565
PZ
9415 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9416
6e3c9717 9417 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
9418 val |= PIPECONF_INTERLACED_ILK;
9419 else
9420 val |= PIPECONF_PROGRESSIVE;
9421
6e3c9717 9422 if (intel_crtc->config->limited_color_range)
3685a8f3 9423 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 9424
c8203565
PZ
9425 I915_WRITE(PIPECONF(pipe), val);
9426 POSTING_READ(PIPECONF(pipe));
9427}
9428
6ff93609 9429static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 9430{
fac5e23e 9431 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
ee2b0b38 9432 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 9433 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
391bf048 9434 u32 val = 0;
ee2b0b38 9435
391bf048 9436 if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
ee2b0b38
PZ
9437 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
9438
6e3c9717 9439 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
9440 val |= PIPECONF_INTERLACED_ILK;
9441 else
9442 val |= PIPECONF_PROGRESSIVE;
9443
702e7a56
PZ
9444 I915_WRITE(PIPECONF(cpu_transcoder), val);
9445 POSTING_READ(PIPECONF(cpu_transcoder));
391bf048
JN
9446}
9447
391bf048
JN
9448static void haswell_set_pipemisc(struct drm_crtc *crtc)
9449{
fac5e23e 9450 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
391bf048 9451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 9452
391bf048
JN
9453 if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
9454 u32 val = 0;
756f85cf 9455
6e3c9717 9456 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
9457 case 18:
9458 val |= PIPEMISC_DITHER_6_BPC;
9459 break;
9460 case 24:
9461 val |= PIPEMISC_DITHER_8_BPC;
9462 break;
9463 case 30:
9464 val |= PIPEMISC_DITHER_10_BPC;
9465 break;
9466 case 36:
9467 val |= PIPEMISC_DITHER_12_BPC;
9468 break;
9469 default:
9470 /* Case prevented by pipe_config_set_bpp. */
9471 BUG();
9472 }
9473
6e3c9717 9474 if (intel_crtc->config->dither)
756f85cf
PZ
9475 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
9476
391bf048 9477 I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
756f85cf 9478 }
ee2b0b38
PZ
9479}
9480
d4b1931c
PZ
9481int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
9482{
9483 /*
9484 * Account for spread spectrum to avoid
9485 * oversubscribing the link. Max center spread
9486 * is 2.5%; use 5% for safety's sake.
9487 */
9488 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 9489 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
9490}
9491
7429e9d4 9492static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 9493{
7429e9d4 9494 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
9495}
9496
b75ca6f6
ACO
9497static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
9498 struct intel_crtc_state *crtc_state,
9e2c8475 9499 struct dpll *reduced_clock)
79e53945 9500{
de13a2e3 9501 struct drm_crtc *crtc = &intel_crtc->base;
79e53945 9502 struct drm_device *dev = crtc->dev;
fac5e23e 9503 struct drm_i915_private *dev_priv = to_i915(dev);
b75ca6f6 9504 u32 dpll, fp, fp2;
3d6e9ee0 9505 int factor;
79e53945 9506
c1858123 9507 /* Enable autotuning of the PLL clock (if permissible) */
8febb297 9508 factor = 21;
3d6e9ee0 9509 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
8febb297 9510 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 9511 dev_priv->vbt.lvds_ssc_freq == 100000) ||
6e266956 9512 (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
8febb297 9513 factor = 25;
190f68c5 9514 } else if (crtc_state->sdvo_tv_clock)
8febb297 9515 factor = 20;
c1858123 9516
b75ca6f6
ACO
9517 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
9518
190f68c5 9519 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
b75ca6f6
ACO
9520 fp |= FP_CB_TUNE;
9521
9522 if (reduced_clock) {
9523 fp2 = i9xx_dpll_compute_fp(reduced_clock);
2c07245f 9524
b75ca6f6
ACO
9525 if (reduced_clock->m < factor * reduced_clock->n)
9526 fp2 |= FP_CB_TUNE;
9527 } else {
9528 fp2 = fp;
9529 }
9a7c7890 9530
5eddb70b 9531 dpll = 0;
2c07245f 9532
3d6e9ee0 9533 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
a07d6787
EA
9534 dpll |= DPLLB_MODE_LVDS;
9535 else
9536 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 9537
190f68c5 9538 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 9539 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f 9540
3d6e9ee0
VS
9541 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
9542 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4a33e48d 9543 dpll |= DPLL_SDVO_HIGH_SPEED;
3d6e9ee0 9544
37a5650b 9545 if (intel_crtc_has_dp_encoder(crtc_state))
4a33e48d 9546 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 9547
7d7f8633
VS
9548 /*
9549 * The high speed IO clock is only really required for
9550 * SDVO/HDMI/DP, but we also enable it for CRT to make it
9551 * possible to share the DPLL between CRT and HDMI. Enabling
9552 * the clock needlessly does no real harm, except use up a
9553 * bit of power potentially.
9554 *
9555 * We'll limit this to IVB with 3 pipes, since it has only two
9556 * DPLLs and so DPLL sharing is the only way to get three pipes
9557 * driving PCH ports at the same time. On SNB we could do this,
9558 * and potentially avoid enabling the second DPLL, but it's not
9559 * clear if it''s a win or loss power wise. No point in doing
9560 * this on ILK at all since it has a fixed DPLL<->pipe mapping.
9561 */
9562 if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
9563 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
9564 dpll |= DPLL_SDVO_HIGH_SPEED;
9565
a07d6787 9566 /* compute bitmask from p1 value */
190f68c5 9567 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 9568 /* also FPA1 */
190f68c5 9569 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 9570
190f68c5 9571 switch (crtc_state->dpll.p2) {
a07d6787
EA
9572 case 5:
9573 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
9574 break;
9575 case 7:
9576 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
9577 break;
9578 case 10:
9579 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
9580 break;
9581 case 14:
9582 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
9583 break;
79e53945
JB
9584 }
9585
3d6e9ee0
VS
9586 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
9587 intel_panel_use_ssc(dev_priv))
43565a06 9588 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
9589 else
9590 dpll |= PLL_REF_INPUT_DREFCLK;
9591
b75ca6f6
ACO
9592 dpll |= DPLL_VCO_ENABLE;
9593
9594 crtc_state->dpll_hw_state.dpll = dpll;
9595 crtc_state->dpll_hw_state.fp0 = fp;
9596 crtc_state->dpll_hw_state.fp1 = fp2;
de13a2e3
PZ
9597}
9598
190f68c5
ACO
9599static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
9600 struct intel_crtc_state *crtc_state)
de13a2e3 9601{
997c030c 9602 struct drm_device *dev = crtc->base.dev;
fac5e23e 9603 struct drm_i915_private *dev_priv = to_i915(dev);
9e2c8475 9604 struct dpll reduced_clock;
7ed9f894 9605 bool has_reduced_clock = false;
e2b78267 9606 struct intel_shared_dpll *pll;
1b6f4958 9607 const struct intel_limit *limit;
997c030c 9608 int refclk = 120000;
de13a2e3 9609
dd3cd74a
ACO
9610 memset(&crtc_state->dpll_hw_state, 0,
9611 sizeof(crtc_state->dpll_hw_state));
9612
ded220e2
ACO
9613 crtc->lowfreq_avail = false;
9614
9615 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
9616 if (!crtc_state->has_pch_encoder)
9617 return 0;
79e53945 9618
2d84d2b3 9619 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
997c030c
ACO
9620 if (intel_panel_use_ssc(dev_priv)) {
9621 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
9622 dev_priv->vbt.lvds_ssc_freq);
9623 refclk = dev_priv->vbt.lvds_ssc_freq;
9624 }
9625
9626 if (intel_is_dual_link_lvds(dev)) {
9627 if (refclk == 100000)
9628 limit = &intel_limits_ironlake_dual_lvds_100m;
9629 else
9630 limit = &intel_limits_ironlake_dual_lvds;
9631 } else {
9632 if (refclk == 100000)
9633 limit = &intel_limits_ironlake_single_lvds_100m;
9634 else
9635 limit = &intel_limits_ironlake_single_lvds;
9636 }
9637 } else {
9638 limit = &intel_limits_ironlake_dac;
9639 }
9640
364ee29d 9641 if (!crtc_state->clock_set &&
997c030c
ACO
9642 !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
9643 refclk, NULL, &crtc_state->dpll)) {
364ee29d
ACO
9644 DRM_ERROR("Couldn't find PLL settings for mode!\n");
9645 return -EINVAL;
f47709a9 9646 }
79e53945 9647
b75ca6f6
ACO
9648 ironlake_compute_dpll(crtc, crtc_state,
9649 has_reduced_clock ? &reduced_clock : NULL);
66e985c0 9650
ded220e2
ACO
9651 pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
9652 if (pll == NULL) {
9653 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
9654 pipe_name(crtc->pipe));
9655 return -EINVAL;
3fb37703 9656 }
79e53945 9657
2d84d2b3 9658 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ded220e2 9659 has_reduced_clock)
c7653199 9660 crtc->lowfreq_avail = true;
e2b78267 9661
c8f7a0db 9662 return 0;
79e53945
JB
9663}
9664
eb14cb74
VS
9665static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9666 struct intel_link_m_n *m_n)
9667{
9668 struct drm_device *dev = crtc->base.dev;
fac5e23e 9669 struct drm_i915_private *dev_priv = to_i915(dev);
eb14cb74
VS
9670 enum pipe pipe = crtc->pipe;
9671
9672 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9673 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9674 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9675 & ~TU_SIZE_MASK;
9676 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9677 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9678 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9679}
9680
9681static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9682 enum transcoder transcoder,
b95af8be
VK
9683 struct intel_link_m_n *m_n,
9684 struct intel_link_m_n *m2_n2)
72419203 9685{
6315b5d3 9686 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
eb14cb74 9687 enum pipe pipe = crtc->pipe;
72419203 9688
6315b5d3 9689 if (INTEL_GEN(dev_priv) >= 5) {
eb14cb74
VS
9690 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9691 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9692 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9693 & ~TU_SIZE_MASK;
9694 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9695 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9696 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9697 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9698 * gen < 8) and if DRRS is supported (to make sure the
9699 * registers are not unnecessarily read).
9700 */
6315b5d3 9701 if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
6e3c9717 9702 crtc->config->has_drrs) {
b95af8be
VK
9703 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9704 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9705 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9706 & ~TU_SIZE_MASK;
9707 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9708 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9709 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9710 }
eb14cb74
VS
9711 } else {
9712 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9713 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9714 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9715 & ~TU_SIZE_MASK;
9716 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9717 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9718 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9719 }
9720}
9721
9722void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9723 struct intel_crtc_state *pipe_config)
eb14cb74 9724{
681a8504 9725 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9726 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9727 else
9728 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9729 &pipe_config->dp_m_n,
9730 &pipe_config->dp_m2_n2);
eb14cb74 9731}
72419203 9732
eb14cb74 9733static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9734 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9735{
9736 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9737 &pipe_config->fdi_m_n, NULL);
72419203
SV
9738}
9739
bd2e244f 9740static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9741 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9742{
9743 struct drm_device *dev = crtc->base.dev;
fac5e23e 9744 struct drm_i915_private *dev_priv = to_i915(dev);
a1b2278e
CK
9745 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9746 uint32_t ps_ctrl = 0;
9747 int id = -1;
9748 int i;
bd2e244f 9749
a1b2278e
CK
9750 /* find scaler attached to this pipe */
9751 for (i = 0; i < crtc->num_scalers; i++) {
9752 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9753 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9754 id = i;
9755 pipe_config->pch_pfit.enabled = true;
9756 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9757 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9758 break;
9759 }
9760 }
bd2e244f 9761
a1b2278e
CK
9762 scaler_state->scaler_id = id;
9763 if (id >= 0) {
9764 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9765 } else {
9766 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9767 }
9768}
9769
5724dbd1
DL
9770static void
9771skylake_get_initial_plane_config(struct intel_crtc *crtc,
9772 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9773{
9774 struct drm_device *dev = crtc->base.dev;
fac5e23e 9775 struct drm_i915_private *dev_priv = to_i915(dev);
40f46283 9776 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9777 int pipe = crtc->pipe;
9778 int fourcc, pixel_format;
6761dd31 9779 unsigned int aligned_height;
bc8d7dff 9780 struct drm_framebuffer *fb;
1b842c89 9781 struct intel_framebuffer *intel_fb;
bc8d7dff 9782
d9806c9f 9783 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9784 if (!intel_fb) {
bc8d7dff
DL
9785 DRM_DEBUG_KMS("failed to alloc fb\n");
9786 return;
9787 }
9788
1b842c89
DL
9789 fb = &intel_fb->base;
9790
bc8d7dff 9791 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9792 if (!(val & PLANE_CTL_ENABLE))
9793 goto error;
9794
bc8d7dff
DL
9795 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9796 fourcc = skl_format_to_fourcc(pixel_format,
9797 val & PLANE_CTL_ORDER_RGBX,
9798 val & PLANE_CTL_ALPHA_MASK);
9799 fb->pixel_format = fourcc;
9800 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9801
40f46283
DL
9802 tiling = val & PLANE_CTL_TILED_MASK;
9803 switch (tiling) {
9804 case PLANE_CTL_TILED_LINEAR:
bae781b2 9805 fb->modifier = DRM_FORMAT_MOD_NONE;
40f46283
DL
9806 break;
9807 case PLANE_CTL_TILED_X:
9808 plane_config->tiling = I915_TILING_X;
bae781b2 9809 fb->modifier = I915_FORMAT_MOD_X_TILED;
40f46283
DL
9810 break;
9811 case PLANE_CTL_TILED_Y:
bae781b2 9812 fb->modifier = I915_FORMAT_MOD_Y_TILED;
40f46283
DL
9813 break;
9814 case PLANE_CTL_TILED_YF:
bae781b2 9815 fb->modifier = I915_FORMAT_MOD_Yf_TILED;
40f46283
DL
9816 break;
9817 default:
9818 MISSING_CASE(tiling);
9819 goto error;
9820 }
9821
bc8d7dff
DL
9822 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9823 plane_config->base = base;
9824
9825 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9826
9827 val = I915_READ(PLANE_SIZE(pipe, 0));
9828 fb->height = ((val >> 16) & 0xfff) + 1;
9829 fb->width = ((val >> 0) & 0x1fff) + 1;
9830
9831 val = I915_READ(PLANE_STRIDE(pipe, 0));
bae781b2 9832 stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
40f46283 9833 fb->pixel_format);
bc8d7dff
DL
9834 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9835
9836 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 9837 fb->pixel_format,
bae781b2 9838 fb->modifier);
bc8d7dff 9839
f37b5c2b 9840 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9841
9842 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9843 pipe_name(pipe), fb->width, fb->height,
9844 fb->bits_per_pixel, base, fb->pitches[0],
9845 plane_config->size);
9846
2d14030b 9847 plane_config->fb = intel_fb;
bc8d7dff
DL
9848 return;
9849
9850error:
d1a3a036 9851 kfree(intel_fb);
bc8d7dff
DL
9852}
9853
2fa2fe9a 9854static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9855 struct intel_crtc_state *pipe_config)
2fa2fe9a
SV
9856{
9857 struct drm_device *dev = crtc->base.dev;
fac5e23e 9858 struct drm_i915_private *dev_priv = to_i915(dev);
2fa2fe9a
SV
9859 uint32_t tmp;
9860
9861 tmp = I915_READ(PF_CTL(crtc->pipe));
9862
9863 if (tmp & PF_ENABLE) {
fd4daa9c 9864 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
SV
9865 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9866 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
SV
9867
9868 /* We currently do not free assignements of panel fitters on
9869 * ivb/hsw (since we don't use the higher upscaling modes which
9870 * differentiates them) so just WARN about this case for now. */
5db94019 9871 if (IS_GEN7(dev_priv)) {
cb8b2a30
SV
9872 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9873 PF_PIPE_SEL_IVB(crtc->pipe));
9874 }
2fa2fe9a 9875 }
79e53945
JB
9876}
9877
5724dbd1
DL
9878static void
9879ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9880 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9881{
9882 struct drm_device *dev = crtc->base.dev;
fac5e23e 9883 struct drm_i915_private *dev_priv = to_i915(dev);
4c6baa59 9884 u32 val, base, offset;
aeee5a49 9885 int pipe = crtc->pipe;
4c6baa59 9886 int fourcc, pixel_format;
6761dd31 9887 unsigned int aligned_height;
b113d5ee 9888 struct drm_framebuffer *fb;
1b842c89 9889 struct intel_framebuffer *intel_fb;
4c6baa59 9890
42a7b088
DL
9891 val = I915_READ(DSPCNTR(pipe));
9892 if (!(val & DISPLAY_PLANE_ENABLE))
9893 return;
9894
d9806c9f 9895 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9896 if (!intel_fb) {
4c6baa59
JB
9897 DRM_DEBUG_KMS("failed to alloc fb\n");
9898 return;
9899 }
9900
1b842c89
DL
9901 fb = &intel_fb->base;
9902
6315b5d3 9903 if (INTEL_GEN(dev_priv) >= 4) {
18c5247e 9904 if (val & DISPPLANE_TILED) {
49af449b 9905 plane_config->tiling = I915_TILING_X;
bae781b2 9906 fb->modifier = I915_FORMAT_MOD_X_TILED;
18c5247e
SV
9907 }
9908 }
4c6baa59
JB
9909
9910 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9911 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9912 fb->pixel_format = fourcc;
9913 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9914
aeee5a49 9915 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8652744b 9916 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
aeee5a49 9917 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9918 } else {
49af449b 9919 if (plane_config->tiling)
aeee5a49 9920 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9921 else
aeee5a49 9922 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9923 }
9924 plane_config->base = base;
9925
9926 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9927 fb->width = ((val >> 16) & 0xfff) + 1;
9928 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9929
9930 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9931 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9932
b113d5ee 9933 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb 9934 fb->pixel_format,
bae781b2 9935 fb->modifier);
4c6baa59 9936
f37b5c2b 9937 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9938
2844a921
DL
9939 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9940 pipe_name(pipe), fb->width, fb->height,
9941 fb->bits_per_pixel, base, fb->pitches[0],
9942 plane_config->size);
b113d5ee 9943
2d14030b 9944 plane_config->fb = intel_fb;
4c6baa59
JB
9945}
9946
0e8ffe1b 9947static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9948 struct intel_crtc_state *pipe_config)
0e8ffe1b
SV
9949{
9950 struct drm_device *dev = crtc->base.dev;
fac5e23e 9951 struct drm_i915_private *dev_priv = to_i915(dev);
1729050e 9952 enum intel_display_power_domain power_domain;
0e8ffe1b 9953 uint32_t tmp;
1729050e 9954 bool ret;
0e8ffe1b 9955
1729050e
ID
9956 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
9957 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
930e8c9e
PZ
9958 return false;
9959
e143a21c 9960 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8106ddbd 9961 pipe_config->shared_dpll = NULL;
eccb140b 9962
1729050e 9963 ret = false;
0e8ffe1b
SV
9964 tmp = I915_READ(PIPECONF(crtc->pipe));
9965 if (!(tmp & PIPECONF_ENABLE))
1729050e 9966 goto out;
0e8ffe1b 9967
42571aef
VS
9968 switch (tmp & PIPECONF_BPC_MASK) {
9969 case PIPECONF_6BPC:
9970 pipe_config->pipe_bpp = 18;
9971 break;
9972 case PIPECONF_8BPC:
9973 pipe_config->pipe_bpp = 24;
9974 break;
9975 case PIPECONF_10BPC:
9976 pipe_config->pipe_bpp = 30;
9977 break;
9978 case PIPECONF_12BPC:
9979 pipe_config->pipe_bpp = 36;
9980 break;
9981 default:
9982 break;
9983 }
9984
b5a9fa09
SV
9985 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9986 pipe_config->limited_color_range = true;
9987
ab9412ba 9988 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0 9989 struct intel_shared_dpll *pll;
8106ddbd 9990 enum intel_dpll_id pll_id;
66e985c0 9991
88adfff1
SV
9992 pipe_config->has_pch_encoder = true;
9993
627eb5a3
SV
9994 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9995 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9996 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
SV
9997
9998 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9999
2d1fe073 10000 if (HAS_PCH_IBX(dev_priv)) {
d9a7bc67
ID
10001 /*
10002 * The pipe->pch transcoder and pch transcoder->pll
10003 * mapping is fixed.
10004 */
8106ddbd 10005 pll_id = (enum intel_dpll_id) crtc->pipe;
c0d43d62
SV
10006 } else {
10007 tmp = I915_READ(PCH_DPLL_SEL);
10008 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8106ddbd 10009 pll_id = DPLL_ID_PCH_PLL_B;
c0d43d62 10010 else
8106ddbd 10011 pll_id= DPLL_ID_PCH_PLL_A;
c0d43d62 10012 }
66e985c0 10013
8106ddbd
ACO
10014 pipe_config->shared_dpll =
10015 intel_get_shared_dpll_by_id(dev_priv, pll_id);
10016 pll = pipe_config->shared_dpll;
66e985c0 10017
2edd6443
ACO
10018 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10019 &pipe_config->dpll_hw_state));
c93f54cf
SV
10020
10021 tmp = pipe_config->dpll_hw_state.dpll;
10022 pipe_config->pixel_multiplier =
10023 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
10024 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
10025
10026 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
SV
10027 } else {
10028 pipe_config->pixel_multiplier = 1;
627eb5a3
SV
10029 }
10030
1bd1bd80 10031 intel_get_pipe_timings(crtc, pipe_config);
bc58be60 10032 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10033
2fa2fe9a
SV
10034 ironlake_get_pfit_config(crtc, pipe_config);
10035
1729050e
ID
10036 ret = true;
10037
10038out:
10039 intel_display_power_put(dev_priv, power_domain);
10040
10041 return ret;
0e8ffe1b
SV
10042}
10043
be256dc7
PZ
10044static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
10045{
91c8a326 10046 struct drm_device *dev = &dev_priv->drm;
be256dc7 10047 struct intel_crtc *crtc;
be256dc7 10048
d3fcc808 10049 for_each_intel_crtc(dev, crtc)
e2c719b7 10050 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
10051 pipe_name(crtc->pipe));
10052
e2c719b7
RC
10053 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
10054 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
01403de3
VS
10055 I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
10056 I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
44cb734c 10057 I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
e2c719b7 10058 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 10059 "CPU PWM1 enabled\n");
772c2a51 10060 if (IS_HASWELL(dev_priv))
e2c719b7 10061 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 10062 "CPU PWM2 enabled\n");
e2c719b7 10063 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 10064 "PCH PWM1 enabled\n");
e2c719b7 10065 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 10066 "Utility pin enabled\n");
e2c719b7 10067 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 10068
9926ada1
PZ
10069 /*
10070 * In theory we can still leave IRQs enabled, as long as only the HPD
10071 * interrupts remain enabled. We used to check for that, but since it's
10072 * gen-specific and since we only disable LCPLL after we fully disable
10073 * the interrupts, the check below should be enough.
10074 */
e2c719b7 10075 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
10076}
10077
9ccd5aeb
PZ
10078static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
10079{
772c2a51 10080 if (IS_HASWELL(dev_priv))
9ccd5aeb
PZ
10081 return I915_READ(D_COMP_HSW);
10082 else
10083 return I915_READ(D_COMP_BDW);
10084}
10085
3c4c9b81
PZ
10086static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
10087{
772c2a51 10088 if (IS_HASWELL(dev_priv)) {
3c4c9b81
PZ
10089 mutex_lock(&dev_priv->rps.hw_lock);
10090 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
10091 val))
79cf219a 10092 DRM_DEBUG_KMS("Failed to write to D_COMP\n");
3c4c9b81
PZ
10093 mutex_unlock(&dev_priv->rps.hw_lock);
10094 } else {
9ccd5aeb
PZ
10095 I915_WRITE(D_COMP_BDW, val);
10096 POSTING_READ(D_COMP_BDW);
3c4c9b81 10097 }
be256dc7
PZ
10098}
10099
10100/*
10101 * This function implements pieces of two sequences from BSpec:
10102 * - Sequence for display software to disable LCPLL
10103 * - Sequence for display software to allow package C8+
10104 * The steps implemented here are just the steps that actually touch the LCPLL
10105 * register. Callers should take care of disabling all the display engine
10106 * functions, doing the mode unset, fixing interrupts, etc.
10107 */
6ff58d53
PZ
10108static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
10109 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
10110{
10111 uint32_t val;
10112
10113 assert_can_disable_lcpll(dev_priv);
10114
10115 val = I915_READ(LCPLL_CTL);
10116
10117 if (switch_to_fclk) {
10118 val |= LCPLL_CD_SOURCE_FCLK;
10119 I915_WRITE(LCPLL_CTL, val);
10120
f53dd63f
ID
10121 if (wait_for_us(I915_READ(LCPLL_CTL) &
10122 LCPLL_CD_SOURCE_FCLK_DONE, 1))
be256dc7
PZ
10123 DRM_ERROR("Switching to FCLK failed\n");
10124
10125 val = I915_READ(LCPLL_CTL);
10126 }
10127
10128 val |= LCPLL_PLL_DISABLE;
10129 I915_WRITE(LCPLL_CTL, val);
10130 POSTING_READ(LCPLL_CTL);
10131
24d8441d 10132 if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
be256dc7
PZ
10133 DRM_ERROR("LCPLL still locked\n");
10134
9ccd5aeb 10135 val = hsw_read_dcomp(dev_priv);
be256dc7 10136 val |= D_COMP_COMP_DISABLE;
3c4c9b81 10137 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10138 ndelay(100);
10139
9ccd5aeb
PZ
10140 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
10141 1))
be256dc7
PZ
10142 DRM_ERROR("D_COMP RCOMP still in progress\n");
10143
10144 if (allow_power_down) {
10145 val = I915_READ(LCPLL_CTL);
10146 val |= LCPLL_POWER_DOWN_ALLOW;
10147 I915_WRITE(LCPLL_CTL, val);
10148 POSTING_READ(LCPLL_CTL);
10149 }
10150}
10151
10152/*
10153 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
10154 * source.
10155 */
6ff58d53 10156static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
10157{
10158 uint32_t val;
10159
10160 val = I915_READ(LCPLL_CTL);
10161
10162 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
10163 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
10164 return;
10165
a8a8bd54
PZ
10166 /*
10167 * Make sure we're not on PC8 state before disabling PC8, otherwise
10168 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 10169 */
59bad947 10170 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 10171
be256dc7
PZ
10172 if (val & LCPLL_POWER_DOWN_ALLOW) {
10173 val &= ~LCPLL_POWER_DOWN_ALLOW;
10174 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 10175 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
10176 }
10177
9ccd5aeb 10178 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
10179 val |= D_COMP_COMP_FORCE;
10180 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 10181 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
10182
10183 val = I915_READ(LCPLL_CTL);
10184 val &= ~LCPLL_PLL_DISABLE;
10185 I915_WRITE(LCPLL_CTL, val);
10186
93220c08
CW
10187 if (intel_wait_for_register(dev_priv,
10188 LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
10189 5))
be256dc7
PZ
10190 DRM_ERROR("LCPLL not locked yet\n");
10191
10192 if (val & LCPLL_CD_SOURCE_FCLK) {
10193 val = I915_READ(LCPLL_CTL);
10194 val &= ~LCPLL_CD_SOURCE_FCLK;
10195 I915_WRITE(LCPLL_CTL, val);
10196
f53dd63f
ID
10197 if (wait_for_us((I915_READ(LCPLL_CTL) &
10198 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
be256dc7
PZ
10199 DRM_ERROR("Switching back to LCPLL failed\n");
10200 }
215733fa 10201
59bad947 10202 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4c75b940 10203 intel_update_cdclk(dev_priv);
be256dc7
PZ
10204}
10205
765dab67
PZ
10206/*
10207 * Package states C8 and deeper are really deep PC states that can only be
10208 * reached when all the devices on the system allow it, so even if the graphics
10209 * device allows PC8+, it doesn't mean the system will actually get to these
10210 * states. Our driver only allows PC8+ when going into runtime PM.
10211 *
10212 * The requirements for PC8+ are that all the outputs are disabled, the power
10213 * well is disabled and most interrupts are disabled, and these are also
10214 * requirements for runtime PM. When these conditions are met, we manually do
10215 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
10216 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
10217 * hang the machine.
10218 *
10219 * When we really reach PC8 or deeper states (not just when we allow it) we lose
10220 * the state of some registers, so when we come back from PC8+ we need to
10221 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
10222 * need to take care of the registers kept by RC6. Notice that this happens even
10223 * if we don't put the device in PCI D3 state (which is what currently happens
10224 * because of the runtime PM support).
10225 *
10226 * For more, read "Display Sequences for Package C8" on the hardware
10227 * documentation.
10228 */
a14cb6fc 10229void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10230{
c67a470b
PZ
10231 uint32_t val;
10232
c67a470b
PZ
10233 DRM_DEBUG_KMS("Enabling package C8+\n");
10234
4f8036a2 10235 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10236 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10237 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
10238 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10239 }
10240
c39055b0 10241 lpt_disable_clkout_dp(dev_priv);
c67a470b
PZ
10242 hsw_disable_lcpll(dev_priv, true, true);
10243}
10244
a14cb6fc 10245void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b 10246{
c67a470b
PZ
10247 uint32_t val;
10248
c67a470b
PZ
10249 DRM_DEBUG_KMS("Disabling package C8+\n");
10250
10251 hsw_restore_lcpll(dev_priv);
c39055b0 10252 lpt_init_pch_refclk(dev_priv);
c67a470b 10253
4f8036a2 10254 if (HAS_PCH_LPT_LP(dev_priv)) {
c67a470b
PZ
10255 val = I915_READ(SOUTH_DSPCLK_GATE_D);
10256 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
10257 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
10258 }
c67a470b
PZ
10259}
10260
324513c0 10261static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 10262{
a821fc46 10263 struct drm_device *dev = old_state->dev;
1a617b77
ML
10264 struct intel_atomic_state *old_intel_state =
10265 to_intel_atomic_state(old_state);
10266 unsigned int req_cdclk = old_intel_state->dev_cdclk;
f8437dd1 10267
324513c0 10268 bxt_set_cdclk(to_i915(dev), req_cdclk);
f8437dd1
VK
10269}
10270
b30ce9e0
DP
10271static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
10272 int pixel_rate)
10273{
9c754024
DP
10274 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
10275
b30ce9e0 10276 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9c754024 10277 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
b30ce9e0
DP
10278 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
10279
10280 /* BSpec says "Do not use DisplayPort with CDCLK less than
10281 * 432 MHz, audio enabled, port width x4, and link rate
10282 * HBR2 (5.4 GHz), or else there may be audio corruption or
10283 * screen corruption."
10284 */
10285 if (intel_crtc_has_dp_encoder(crtc_state) &&
10286 crtc_state->has_audio &&
10287 crtc_state->port_clock >= 540000 &&
10288 crtc_state->lane_count == 4)
10289 pixel_rate = max(432000, pixel_rate);
10290
10291 return pixel_rate;
10292}
10293
b432e5cf 10294/* compute the max rate for new configuration */
27c329ed 10295static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 10296{
565602d7 10297 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 10298 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
10299 struct drm_crtc *crtc;
10300 struct drm_crtc_state *cstate;
27c329ed 10301 struct intel_crtc_state *crtc_state;
565602d7
ML
10302 unsigned max_pixel_rate = 0, i;
10303 enum pipe pipe;
b432e5cf 10304
565602d7
ML
10305 memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
10306 sizeof(intel_state->min_pixclk));
27c329ed 10307
565602d7
ML
10308 for_each_crtc_in_state(state, crtc, cstate, i) {
10309 int pixel_rate;
27c329ed 10310
565602d7
ML
10311 crtc_state = to_intel_crtc_state(cstate);
10312 if (!crtc_state->base.enable) {
10313 intel_state->min_pixclk[i] = 0;
b432e5cf 10314 continue;
565602d7 10315 }
b432e5cf 10316
27c329ed 10317 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf 10318
9c754024 10319 if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
b30ce9e0
DP
10320 pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
10321 pixel_rate);
b432e5cf 10322
565602d7 10323 intel_state->min_pixclk[i] = pixel_rate;
b432e5cf
VS
10324 }
10325
565602d7
ML
10326 for_each_pipe(dev_priv, pipe)
10327 max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
10328
b432e5cf
VS
10329 return max_pixel_rate;
10330}
10331
10332static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
10333{
fac5e23e 10334 struct drm_i915_private *dev_priv = to_i915(dev);
b432e5cf
VS
10335 uint32_t val, data;
10336 int ret;
10337
10338 if (WARN((I915_READ(LCPLL_CTL) &
10339 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
10340 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
10341 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
10342 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
10343 "trying to change cdclk frequency with cdclk not enabled\n"))
10344 return;
10345
10346 mutex_lock(&dev_priv->rps.hw_lock);
10347 ret = sandybridge_pcode_write(dev_priv,
10348 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
10349 mutex_unlock(&dev_priv->rps.hw_lock);
10350 if (ret) {
10351 DRM_ERROR("failed to inform pcode about cdclk change\n");
10352 return;
10353 }
10354
10355 val = I915_READ(LCPLL_CTL);
10356 val |= LCPLL_CD_SOURCE_FCLK;
10357 I915_WRITE(LCPLL_CTL, val);
10358
5ba00178
TU
10359 if (wait_for_us(I915_READ(LCPLL_CTL) &
10360 LCPLL_CD_SOURCE_FCLK_DONE, 1))
b432e5cf
VS
10361 DRM_ERROR("Switching to FCLK failed\n");
10362
10363 val = I915_READ(LCPLL_CTL);
10364 val &= ~LCPLL_CLK_FREQ_MASK;
10365
10366 switch (cdclk) {
10367 case 450000:
10368 val |= LCPLL_CLK_FREQ_450;
10369 data = 0;
10370 break;
10371 case 540000:
10372 val |= LCPLL_CLK_FREQ_54O_BDW;
10373 data = 1;
10374 break;
10375 case 337500:
10376 val |= LCPLL_CLK_FREQ_337_5_BDW;
10377 data = 2;
10378 break;
10379 case 675000:
10380 val |= LCPLL_CLK_FREQ_675_BDW;
10381 data = 3;
10382 break;
10383 default:
10384 WARN(1, "invalid cdclk frequency\n");
10385 return;
10386 }
10387
10388 I915_WRITE(LCPLL_CTL, val);
10389
10390 val = I915_READ(LCPLL_CTL);
10391 val &= ~LCPLL_CD_SOURCE_FCLK;
10392 I915_WRITE(LCPLL_CTL, val);
10393
5ba00178
TU
10394 if (wait_for_us((I915_READ(LCPLL_CTL) &
10395 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
b432e5cf
VS
10396 DRM_ERROR("Switching back to LCPLL failed\n");
10397
10398 mutex_lock(&dev_priv->rps.hw_lock);
10399 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
10400 mutex_unlock(&dev_priv->rps.hw_lock);
10401
7f1052a8
VS
10402 I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
10403
4c75b940 10404 intel_update_cdclk(dev_priv);
b432e5cf
VS
10405
10406 WARN(cdclk != dev_priv->cdclk_freq,
10407 "cdclk requested %d kHz but got %d kHz\n",
10408 cdclk, dev_priv->cdclk_freq);
10409}
10410
587c7914
VS
10411static int broadwell_calc_cdclk(int max_pixclk)
10412{
10413 if (max_pixclk > 540000)
10414 return 675000;
10415 else if (max_pixclk > 450000)
10416 return 540000;
10417 else if (max_pixclk > 337500)
10418 return 450000;
10419 else
10420 return 337500;
10421}
10422
27c329ed 10423static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 10424{
27c329ed 10425 struct drm_i915_private *dev_priv = to_i915(state->dev);
1a617b77 10426 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
27c329ed 10427 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
10428 int cdclk;
10429
10430 /*
10431 * FIXME should also account for plane ratio
10432 * once 64bpp pixel formats are supported.
10433 */
587c7914 10434 cdclk = broadwell_calc_cdclk(max_pixclk);
b432e5cf 10435
b432e5cf 10436 if (cdclk > dev_priv->max_cdclk_freq) {
63ba534e
ML
10437 DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10438 cdclk, dev_priv->max_cdclk_freq);
10439 return -EINVAL;
b432e5cf
VS
10440 }
10441
1a617b77
ML
10442 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10443 if (!intel_state->active_crtcs)
587c7914 10444 intel_state->dev_cdclk = broadwell_calc_cdclk(0);
b432e5cf
VS
10445
10446 return 0;
10447}
10448
27c329ed 10449static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 10450{
27c329ed 10451 struct drm_device *dev = old_state->dev;
1a617b77
ML
10452 struct intel_atomic_state *old_intel_state =
10453 to_intel_atomic_state(old_state);
10454 unsigned req_cdclk = old_intel_state->dev_cdclk;
b432e5cf 10455
27c329ed 10456 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
10457}
10458
c89e39f3
CT
10459static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
10460{
10461 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
10462 struct drm_i915_private *dev_priv = to_i915(state->dev);
10463 const int max_pixclk = ilk_max_pixel_rate(state);
a8ca4934 10464 int vco = intel_state->cdclk_pll_vco;
c89e39f3
CT
10465 int cdclk;
10466
10467 /*
10468 * FIXME should also account for plane ratio
10469 * once 64bpp pixel formats are supported.
10470 */
a8ca4934 10471 cdclk = skl_calc_cdclk(max_pixclk, vco);
c89e39f3
CT
10472
10473 /*
10474 * FIXME move the cdclk caclulation to
10475 * compute_config() so we can fail gracegully.
10476 */
10477 if (cdclk > dev_priv->max_cdclk_freq) {
10478 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
10479 cdclk, dev_priv->max_cdclk_freq);
10480 cdclk = dev_priv->max_cdclk_freq;
10481 }
10482
10483 intel_state->cdclk = intel_state->dev_cdclk = cdclk;
10484 if (!intel_state->active_crtcs)
a8ca4934 10485 intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
c89e39f3
CT
10486
10487 return 0;
10488}
10489
10490static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
10491{
1cd593e0
VS
10492 struct drm_i915_private *dev_priv = to_i915(old_state->dev);
10493 struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
10494 unsigned int req_cdclk = intel_state->dev_cdclk;
10495 unsigned int req_vco = intel_state->cdclk_pll_vco;
c89e39f3 10496
1cd593e0 10497 skl_set_cdclk(dev_priv, req_cdclk, req_vco);
c89e39f3
CT
10498}
10499
190f68c5
ACO
10500static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
10501 struct intel_crtc_state *crtc_state)
09b4ddf9 10502{
d7edc4e5 10503 if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
af3997b5
MK
10504 if (!intel_ddi_pll_select(crtc, crtc_state))
10505 return -EINVAL;
10506 }
716c2e55 10507
c7653199 10508 crtc->lowfreq_avail = false;
644cef34 10509
c8f7a0db 10510 return 0;
79e53945
JB
10511}
10512
3760b59c
S
10513static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
10514 enum port port,
10515 struct intel_crtc_state *pipe_config)
10516{
8106ddbd
ACO
10517 enum intel_dpll_id id;
10518
3760b59c
S
10519 switch (port) {
10520 case PORT_A:
08250c4b 10521 id = DPLL_ID_SKL_DPLL0;
3760b59c
S
10522 break;
10523 case PORT_B:
08250c4b 10524 id = DPLL_ID_SKL_DPLL1;
3760b59c
S
10525 break;
10526 case PORT_C:
08250c4b 10527 id = DPLL_ID_SKL_DPLL2;
3760b59c
S
10528 break;
10529 default:
10530 DRM_ERROR("Incorrect port type\n");
8106ddbd 10531 return;
3760b59c 10532 }
8106ddbd
ACO
10533
10534 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
3760b59c
S
10535}
10536
96b7dfb7
S
10537static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
10538 enum port port,
5cec258b 10539 struct intel_crtc_state *pipe_config)
96b7dfb7 10540{
8106ddbd 10541 enum intel_dpll_id id;
a3c988ea 10542 u32 temp;
96b7dfb7
S
10543
10544 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
c856052a 10545 id = temp >> (port * 3 + 1);
96b7dfb7 10546
c856052a 10547 if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
8106ddbd 10548 return;
8106ddbd
ACO
10549
10550 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
96b7dfb7
S
10551}
10552
7d2c8175
DL
10553static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
10554 enum port port,
5cec258b 10555 struct intel_crtc_state *pipe_config)
7d2c8175 10556{
8106ddbd 10557 enum intel_dpll_id id;
c856052a 10558 uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8106ddbd 10559
c856052a 10560 switch (ddi_pll_sel) {
7d2c8175 10561 case PORT_CLK_SEL_WRPLL1:
8106ddbd 10562 id = DPLL_ID_WRPLL1;
7d2c8175
DL
10563 break;
10564 case PORT_CLK_SEL_WRPLL2:
8106ddbd 10565 id = DPLL_ID_WRPLL2;
7d2c8175 10566 break;
00490c22 10567 case PORT_CLK_SEL_SPLL:
8106ddbd 10568 id = DPLL_ID_SPLL;
79bd23da 10569 break;
9d16da65
ACO
10570 case PORT_CLK_SEL_LCPLL_810:
10571 id = DPLL_ID_LCPLL_810;
10572 break;
10573 case PORT_CLK_SEL_LCPLL_1350:
10574 id = DPLL_ID_LCPLL_1350;
10575 break;
10576 case PORT_CLK_SEL_LCPLL_2700:
10577 id = DPLL_ID_LCPLL_2700;
10578 break;
8106ddbd 10579 default:
c856052a 10580 MISSING_CASE(ddi_pll_sel);
8106ddbd
ACO
10581 /* fall through */
10582 case PORT_CLK_SEL_NONE:
8106ddbd 10583 return;
7d2c8175 10584 }
8106ddbd
ACO
10585
10586 pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
7d2c8175
DL
10587}
10588
cf30429e
JN
10589static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
10590 struct intel_crtc_state *pipe_config,
10591 unsigned long *power_domain_mask)
10592{
10593 struct drm_device *dev = crtc->base.dev;
fac5e23e 10594 struct drm_i915_private *dev_priv = to_i915(dev);
cf30429e
JN
10595 enum intel_display_power_domain power_domain;
10596 u32 tmp;
10597
d9a7bc67
ID
10598 /*
10599 * The pipe->transcoder mapping is fixed with the exception of the eDP
10600 * transcoder handled below.
10601 */
cf30429e
JN
10602 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
10603
10604 /*
10605 * XXX: Do intel_display_power_get_if_enabled before reading this (for
10606 * consistency and less surprising code; it's in always on power).
10607 */
10608 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
10609 if (tmp & TRANS_DDI_FUNC_ENABLE) {
10610 enum pipe trans_edp_pipe;
10611 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
10612 default:
10613 WARN(1, "unknown pipe linked to edp transcoder\n");
10614 case TRANS_DDI_EDP_INPUT_A_ONOFF:
10615 case TRANS_DDI_EDP_INPUT_A_ON:
10616 trans_edp_pipe = PIPE_A;
10617 break;
10618 case TRANS_DDI_EDP_INPUT_B_ONOFF:
10619 trans_edp_pipe = PIPE_B;
10620 break;
10621 case TRANS_DDI_EDP_INPUT_C_ONOFF:
10622 trans_edp_pipe = PIPE_C;
10623 break;
10624 }
10625
10626 if (trans_edp_pipe == crtc->pipe)
10627 pipe_config->cpu_transcoder = TRANSCODER_EDP;
10628 }
10629
10630 power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
10631 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10632 return false;
10633 *power_domain_mask |= BIT(power_domain);
10634
10635 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
10636
10637 return tmp & PIPECONF_ENABLE;
10638}
10639
4d1de975
JN
10640static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
10641 struct intel_crtc_state *pipe_config,
10642 unsigned long *power_domain_mask)
10643{
10644 struct drm_device *dev = crtc->base.dev;
fac5e23e 10645 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975
JN
10646 enum intel_display_power_domain power_domain;
10647 enum port port;
10648 enum transcoder cpu_transcoder;
10649 u32 tmp;
10650
4d1de975
JN
10651 for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
10652 if (port == PORT_A)
10653 cpu_transcoder = TRANSCODER_DSI_A;
10654 else
10655 cpu_transcoder = TRANSCODER_DSI_C;
10656
10657 power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
10658 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
10659 continue;
10660 *power_domain_mask |= BIT(power_domain);
10661
db18b6a6
ID
10662 /*
10663 * The PLL needs to be enabled with a valid divider
10664 * configuration, otherwise accessing DSI registers will hang
10665 * the machine. See BSpec North Display Engine
10666 * registers/MIPI[BXT]. We can break out here early, since we
10667 * need the same DSI PLL to be enabled for both DSI ports.
10668 */
10669 if (!intel_dsi_pll_is_enabled(dev_priv))
10670 break;
10671
4d1de975
JN
10672 /* XXX: this works for video mode only */
10673 tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
10674 if (!(tmp & DPI_ENABLE))
10675 continue;
10676
10677 tmp = I915_READ(MIPI_CTRL(port));
10678 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
10679 continue;
10680
10681 pipe_config->cpu_transcoder = cpu_transcoder;
4d1de975
JN
10682 break;
10683 }
10684
d7edc4e5 10685 return transcoder_is_dsi(pipe_config->cpu_transcoder);
4d1de975
JN
10686}
10687
26804afd 10688static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 10689 struct intel_crtc_state *pipe_config)
26804afd 10690{
6315b5d3 10691 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d452c5b6 10692 struct intel_shared_dpll *pll;
26804afd
SV
10693 enum port port;
10694 uint32_t tmp;
10695
10696 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
10697
10698 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
10699
0853723b 10700 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
96b7dfb7 10701 skylake_get_ddi_pll(dev_priv, port, pipe_config);
cc3f90f0 10702 else if (IS_GEN9_LP(dev_priv))
3760b59c 10703 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
10704 else
10705 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 10706
8106ddbd
ACO
10707 pll = pipe_config->shared_dpll;
10708 if (pll) {
2edd6443
ACO
10709 WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
10710 &pipe_config->dpll_hw_state));
d452c5b6
SV
10711 }
10712
26804afd
SV
10713 /*
10714 * Haswell has only FDI/PCH transcoder A. It is which is connected to
10715 * DDI E. So just check whether this pipe is wired to DDI E and whether
10716 * the PCH transcoder is on.
10717 */
6315b5d3 10718 if (INTEL_GEN(dev_priv) < 9 &&
ca370455 10719 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
SV
10720 pipe_config->has_pch_encoder = true;
10721
10722 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
10723 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
10724 FDI_DP_PORT_WIDTH_SHIFT) + 1;
10725
10726 ironlake_get_fdi_m_n_config(crtc, pipe_config);
10727 }
10728}
10729
0e8ffe1b 10730static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 10731 struct intel_crtc_state *pipe_config)
0e8ffe1b 10732{
6315b5d3 10733 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1729050e
ID
10734 enum intel_display_power_domain power_domain;
10735 unsigned long power_domain_mask;
cf30429e 10736 bool active;
0e8ffe1b 10737
1729050e
ID
10738 power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
10739 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b5482bd0 10740 return false;
1729050e
ID
10741 power_domain_mask = BIT(power_domain);
10742
8106ddbd 10743 pipe_config->shared_dpll = NULL;
c0d43d62 10744
cf30429e 10745 active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
eccb140b 10746
cc3f90f0 10747 if (IS_GEN9_LP(dev_priv) &&
d7edc4e5
VS
10748 bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
10749 WARN_ON(active);
10750 active = true;
4d1de975
JN
10751 }
10752
cf30429e 10753 if (!active)
1729050e 10754 goto out;
0e8ffe1b 10755
d7edc4e5 10756 if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
4d1de975
JN
10757 haswell_get_ddi_port_state(crtc, pipe_config);
10758 intel_get_pipe_timings(crtc, pipe_config);
10759 }
627eb5a3 10760
bc58be60 10761 intel_get_pipe_src_size(crtc, pipe_config);
1bd1bd80 10762
05dc698c
LL
10763 pipe_config->gamma_mode =
10764 I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
10765
6315b5d3 10766 if (INTEL_GEN(dev_priv) >= 9) {
65edccce 10767 skl_init_scalers(dev_priv, crtc, pipe_config);
a1b2278e 10768
af99ceda
CK
10769 pipe_config->scaler_state.scaler_id = -1;
10770 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
10771 }
10772
1729050e
ID
10773 power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
10774 if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
10775 power_domain_mask |= BIT(power_domain);
6315b5d3 10776 if (INTEL_GEN(dev_priv) >= 9)
bd2e244f 10777 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 10778 else
1c132b44 10779 ironlake_get_pfit_config(crtc, pipe_config);
bd2e244f 10780 }
88adfff1 10781
772c2a51 10782 if (IS_HASWELL(dev_priv))
e59150dc
JB
10783 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10784 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 10785
4d1de975
JN
10786 if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
10787 !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
ebb69c95
CT
10788 pipe_config->pixel_multiplier =
10789 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10790 } else {
10791 pipe_config->pixel_multiplier = 1;
10792 }
6c49f241 10793
1729050e
ID
10794out:
10795 for_each_power_domain(power_domain, power_domain_mask)
10796 intel_display_power_put(dev_priv, power_domain);
10797
cf30429e 10798 return active;
0e8ffe1b
SV
10799}
10800
55a08b3f
ML
10801static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
10802 const struct intel_plane_state *plane_state)
560b85bb
CW
10803{
10804 struct drm_device *dev = crtc->dev;
fac5e23e 10805 struct drm_i915_private *dev_priv = to_i915(dev);
560b85bb 10806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 10807 uint32_t cntl = 0, size = 0;
560b85bb 10808
936e71e3 10809 if (plane_state && plane_state->base.visible) {
55a08b3f
ML
10810 unsigned int width = plane_state->base.crtc_w;
10811 unsigned int height = plane_state->base.crtc_h;
dc41c154
VS
10812 unsigned int stride = roundup_pow_of_two(width) * 4;
10813
10814 switch (stride) {
10815 default:
10816 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10817 width, stride);
10818 stride = 256;
10819 /* fallthrough */
10820 case 256:
10821 case 512:
10822 case 1024:
10823 case 2048:
10824 break;
4b0e333e
CW
10825 }
10826
dc41c154
VS
10827 cntl |= CURSOR_ENABLE |
10828 CURSOR_GAMMA_ENABLE |
10829 CURSOR_FORMAT_ARGB |
10830 CURSOR_STRIDE(stride);
10831
10832 size = (height << 12) | width;
4b0e333e 10833 }
560b85bb 10834
dc41c154
VS
10835 if (intel_crtc->cursor_cntl != 0 &&
10836 (intel_crtc->cursor_base != base ||
10837 intel_crtc->cursor_size != size ||
10838 intel_crtc->cursor_cntl != cntl)) {
10839 /* On these chipsets we can only modify the base/size/stride
10840 * whilst the cursor is disabled.
10841 */
0b87c24e
VS
10842 I915_WRITE(CURCNTR(PIPE_A), 0);
10843 POSTING_READ(CURCNTR(PIPE_A));
dc41c154 10844 intel_crtc->cursor_cntl = 0;
4b0e333e 10845 }
560b85bb 10846
99d1f387 10847 if (intel_crtc->cursor_base != base) {
0b87c24e 10848 I915_WRITE(CURBASE(PIPE_A), base);
99d1f387
VS
10849 intel_crtc->cursor_base = base;
10850 }
4726e0b0 10851
dc41c154
VS
10852 if (intel_crtc->cursor_size != size) {
10853 I915_WRITE(CURSIZE, size);
10854 intel_crtc->cursor_size = size;
4b0e333e 10855 }
560b85bb 10856
4b0e333e 10857 if (intel_crtc->cursor_cntl != cntl) {
0b87c24e
VS
10858 I915_WRITE(CURCNTR(PIPE_A), cntl);
10859 POSTING_READ(CURCNTR(PIPE_A));
4b0e333e 10860 intel_crtc->cursor_cntl = cntl;
560b85bb 10861 }
560b85bb
CW
10862}
10863
55a08b3f
ML
10864static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
10865 const struct intel_plane_state *plane_state)
65a21cd6
JB
10866{
10867 struct drm_device *dev = crtc->dev;
fac5e23e 10868 struct drm_i915_private *dev_priv = to_i915(dev);
65a21cd6
JB
10869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10870 int pipe = intel_crtc->pipe;
663f3122 10871 uint32_t cntl = 0;
4b0e333e 10872
936e71e3 10873 if (plane_state && plane_state->base.visible) {
4b0e333e 10874 cntl = MCURSOR_GAMMA_ENABLE;
55a08b3f 10875 switch (plane_state->base.crtc_w) {
4726e0b0
SK
10876 case 64:
10877 cntl |= CURSOR_MODE_64_ARGB_AX;
10878 break;
10879 case 128:
10880 cntl |= CURSOR_MODE_128_ARGB_AX;
10881 break;
10882 case 256:
10883 cntl |= CURSOR_MODE_256_ARGB_AX;
10884 break;
10885 default:
55a08b3f 10886 MISSING_CASE(plane_state->base.crtc_w);
4726e0b0 10887 return;
65a21cd6 10888 }
4b0e333e 10889 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7 10890
4f8036a2 10891 if (HAS_DDI(dev_priv))
47bf17a7 10892 cntl |= CURSOR_PIPE_CSC_ENABLE;
65a21cd6 10893
f22aa143 10894 if (plane_state->base.rotation & DRM_ROTATE_180)
55a08b3f
ML
10895 cntl |= CURSOR_ROTATE_180;
10896 }
4398ad45 10897
4b0e333e
CW
10898 if (intel_crtc->cursor_cntl != cntl) {
10899 I915_WRITE(CURCNTR(pipe), cntl);
10900 POSTING_READ(CURCNTR(pipe));
10901 intel_crtc->cursor_cntl = cntl;
65a21cd6 10902 }
4b0e333e 10903
65a21cd6 10904 /* and commit changes on next vblank */
5efb3e28
VS
10905 I915_WRITE(CURBASE(pipe), base);
10906 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10907
10908 intel_crtc->cursor_base = base;
65a21cd6
JB
10909}
10910
cda4b7d3 10911/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f 10912static void intel_crtc_update_cursor(struct drm_crtc *crtc,
55a08b3f 10913 const struct intel_plane_state *plane_state)
cda4b7d3
CW
10914{
10915 struct drm_device *dev = crtc->dev;
fac5e23e 10916 struct drm_i915_private *dev_priv = to_i915(dev);
cda4b7d3
CW
10917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10918 int pipe = intel_crtc->pipe;
55a08b3f
ML
10919 u32 base = intel_crtc->cursor_addr;
10920 u32 pos = 0;
cda4b7d3 10921
55a08b3f
ML
10922 if (plane_state) {
10923 int x = plane_state->base.crtc_x;
10924 int y = plane_state->base.crtc_y;
cda4b7d3 10925
55a08b3f
ML
10926 if (x < 0) {
10927 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10928 x = -x;
10929 }
10930 pos |= x << CURSOR_X_SHIFT;
cda4b7d3 10931
55a08b3f
ML
10932 if (y < 0) {
10933 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10934 y = -y;
10935 }
10936 pos |= y << CURSOR_Y_SHIFT;
10937
10938 /* ILK+ do this automagically */
49cff963 10939 if (HAS_GMCH_DISPLAY(dev_priv) &&
f22aa143 10940 plane_state->base.rotation & DRM_ROTATE_180) {
55a08b3f
ML
10941 base += (plane_state->base.crtc_h *
10942 plane_state->base.crtc_w - 1) * 4;
10943 }
cda4b7d3 10944 }
cda4b7d3 10945
5efb3e28
VS
10946 I915_WRITE(CURPOS(pipe), pos);
10947
50a0bc90 10948 if (IS_845G(dev_priv) || IS_I865G(dev_priv))
55a08b3f 10949 i845_update_cursor(crtc, base, plane_state);
5efb3e28 10950 else
55a08b3f 10951 i9xx_update_cursor(crtc, base, plane_state);
cda4b7d3
CW
10952}
10953
50a0bc90 10954static bool cursor_size_ok(struct drm_i915_private *dev_priv,
dc41c154
VS
10955 uint32_t width, uint32_t height)
10956{
10957 if (width == 0 || height == 0)
10958 return false;
10959
10960 /*
10961 * 845g/865g are special in that they are only limited by
10962 * the width of their cursors, the height is arbitrary up to
10963 * the precision of the register. Everything else requires
10964 * square cursors, limited to a few power-of-two sizes.
10965 */
50a0bc90 10966 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
dc41c154
VS
10967 if ((width & 63) != 0)
10968 return false;
10969
50a0bc90 10970 if (width > (IS_845G(dev_priv) ? 64 : 512))
dc41c154
VS
10971 return false;
10972
10973 if (height > 1023)
10974 return false;
10975 } else {
10976 switch (width | height) {
10977 case 256:
10978 case 128:
50a0bc90 10979 if (IS_GEN2(dev_priv))
dc41c154
VS
10980 return false;
10981 case 64:
10982 break;
10983 default:
10984 return false;
10985 }
10986 }
10987
10988 return true;
10989}
10990
79e53945
JB
10991/* VESA 640x480x72Hz mode to set on the pipe */
10992static struct drm_display_mode load_detect_mode = {
10993 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10994 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10995};
10996
a8bb6818
SV
10997struct drm_framebuffer *
10998__intel_framebuffer_create(struct drm_device *dev,
10999 struct drm_mode_fb_cmd2 *mode_cmd,
11000 struct drm_i915_gem_object *obj)
d2dff872
CW
11001{
11002 struct intel_framebuffer *intel_fb;
11003 int ret;
11004
11005 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
dcb1394e 11006 if (!intel_fb)
d2dff872 11007 return ERR_PTR(-ENOMEM);
d2dff872
CW
11008
11009 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
SV
11010 if (ret)
11011 goto err;
d2dff872
CW
11012
11013 return &intel_fb->base;
dcb1394e 11014
dd4916c5 11015err:
dd4916c5 11016 kfree(intel_fb);
dd4916c5 11017 return ERR_PTR(ret);
d2dff872
CW
11018}
11019
b5ea642a 11020static struct drm_framebuffer *
a8bb6818
SV
11021intel_framebuffer_create(struct drm_device *dev,
11022 struct drm_mode_fb_cmd2 *mode_cmd,
11023 struct drm_i915_gem_object *obj)
11024{
11025 struct drm_framebuffer *fb;
11026 int ret;
11027
11028 ret = i915_mutex_lock_interruptible(dev);
11029 if (ret)
11030 return ERR_PTR(ret);
11031 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
11032 mutex_unlock(&dev->struct_mutex);
11033
11034 return fb;
11035}
11036
d2dff872
CW
11037static u32
11038intel_framebuffer_pitch_for_width(int width, int bpp)
11039{
11040 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
11041 return ALIGN(pitch, 64);
11042}
11043
11044static u32
11045intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
11046{
11047 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 11048 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
11049}
11050
11051static struct drm_framebuffer *
11052intel_framebuffer_create_for_mode(struct drm_device *dev,
11053 struct drm_display_mode *mode,
11054 int depth, int bpp)
11055{
dcb1394e 11056 struct drm_framebuffer *fb;
d2dff872 11057 struct drm_i915_gem_object *obj;
0fed39bd 11058 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872 11059
12d79d78 11060 obj = i915_gem_object_create(to_i915(dev),
d2dff872 11061 intel_framebuffer_size_for_mode(mode, bpp));
fe3db79b
CW
11062 if (IS_ERR(obj))
11063 return ERR_CAST(obj);
d2dff872
CW
11064
11065 mode_cmd.width = mode->hdisplay;
11066 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
11067 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
11068 bpp);
5ca0c34a 11069 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872 11070
dcb1394e
LW
11071 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
11072 if (IS_ERR(fb))
f0cd5182 11073 i915_gem_object_put(obj);
dcb1394e
LW
11074
11075 return fb;
d2dff872
CW
11076}
11077
11078static struct drm_framebuffer *
11079mode_fits_in_fbdev(struct drm_device *dev,
11080 struct drm_display_mode *mode)
11081{
0695726e 11082#ifdef CONFIG_DRM_FBDEV_EMULATION
fac5e23e 11083 struct drm_i915_private *dev_priv = to_i915(dev);
d2dff872
CW
11084 struct drm_i915_gem_object *obj;
11085 struct drm_framebuffer *fb;
11086
4c0e5528 11087 if (!dev_priv->fbdev)
d2dff872
CW
11088 return NULL;
11089
4c0e5528 11090 if (!dev_priv->fbdev->fb)
d2dff872
CW
11091 return NULL;
11092
4c0e5528
SV
11093 obj = dev_priv->fbdev->fb->obj;
11094 BUG_ON(!obj);
11095
8bcd4553 11096 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
11097 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
11098 fb->bits_per_pixel))
d2dff872
CW
11099 return NULL;
11100
01f2c773 11101 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
11102 return NULL;
11103
edde3617 11104 drm_framebuffer_reference(fb);
d2dff872 11105 return fb;
4520f53a
SV
11106#else
11107 return NULL;
11108#endif
d2dff872
CW
11109}
11110
d3a40d1b
ACO
11111static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
11112 struct drm_crtc *crtc,
11113 struct drm_display_mode *mode,
11114 struct drm_framebuffer *fb,
11115 int x, int y)
11116{
11117 struct drm_plane_state *plane_state;
11118 int hdisplay, vdisplay;
11119 int ret;
11120
11121 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
11122 if (IS_ERR(plane_state))
11123 return PTR_ERR(plane_state);
11124
11125 if (mode)
11126 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11127 else
11128 hdisplay = vdisplay = 0;
11129
11130 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
11131 if (ret)
11132 return ret;
11133 drm_atomic_set_fb_for_plane(plane_state, fb);
11134 plane_state->crtc_x = 0;
11135 plane_state->crtc_y = 0;
11136 plane_state->crtc_w = hdisplay;
11137 plane_state->crtc_h = vdisplay;
11138 plane_state->src_x = x << 16;
11139 plane_state->src_y = y << 16;
11140 plane_state->src_w = hdisplay << 16;
11141 plane_state->src_h = vdisplay << 16;
11142
11143 return 0;
11144}
11145
d2434ab7 11146bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 11147 struct drm_display_mode *mode,
51fd371b
RC
11148 struct intel_load_detect_pipe *old,
11149 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
11150{
11151 struct intel_crtc *intel_crtc;
d2434ab7
SV
11152 struct intel_encoder *intel_encoder =
11153 intel_attached_encoder(connector);
79e53945 11154 struct drm_crtc *possible_crtc;
4ef69c7a 11155 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
11156 struct drm_crtc *crtc = NULL;
11157 struct drm_device *dev = encoder->dev;
0f0f74bc 11158 struct drm_i915_private *dev_priv = to_i915(dev);
94352cf9 11159 struct drm_framebuffer *fb;
51fd371b 11160 struct drm_mode_config *config = &dev->mode_config;
edde3617 11161 struct drm_atomic_state *state = NULL, *restore_state = NULL;
944b0c76 11162 struct drm_connector_state *connector_state;
4be07317 11163 struct intel_crtc_state *crtc_state;
51fd371b 11164 int ret, i = -1;
79e53945 11165
d2dff872 11166 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11167 connector->base.id, connector->name,
8e329a03 11168 encoder->base.id, encoder->name);
d2dff872 11169
edde3617
ML
11170 old->restore_state = NULL;
11171
51fd371b
RC
11172retry:
11173 ret = drm_modeset_lock(&config->connection_mutex, ctx);
11174 if (ret)
ad3c558f 11175 goto fail;
6e9f798d 11176
79e53945
JB
11177 /*
11178 * Algorithm gets a little messy:
7a5e4805 11179 *
79e53945
JB
11180 * - if the connector already has an assigned crtc, use it (but make
11181 * sure it's on first)
7a5e4805 11182 *
79e53945
JB
11183 * - try to find the first unused crtc that can drive this connector,
11184 * and use that if we find one
79e53945
JB
11185 */
11186
11187 /* See if we already have a CRTC for this connector */
edde3617
ML
11188 if (connector->state->crtc) {
11189 crtc = connector->state->crtc;
8261b191 11190
51fd371b 11191 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 11192 if (ret)
ad3c558f 11193 goto fail;
8261b191
CW
11194
11195 /* Make sure the crtc and connector are running */
edde3617 11196 goto found;
79e53945
JB
11197 }
11198
11199 /* Find an unused one (if possible) */
70e1e0ec 11200 for_each_crtc(dev, possible_crtc) {
79e53945
JB
11201 i++;
11202 if (!(encoder->possible_crtcs & (1 << i)))
11203 continue;
edde3617
ML
11204
11205 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
11206 if (ret)
11207 goto fail;
11208
11209 if (possible_crtc->state->enable) {
11210 drm_modeset_unlock(&possible_crtc->mutex);
a459249c 11211 continue;
edde3617 11212 }
a459249c
VS
11213
11214 crtc = possible_crtc;
11215 break;
79e53945
JB
11216 }
11217
11218 /*
11219 * If we didn't find an unused CRTC, don't use any.
11220 */
11221 if (!crtc) {
7173188d 11222 DRM_DEBUG_KMS("no pipe available for load-detect\n");
ad3c558f 11223 goto fail;
79e53945
JB
11224 }
11225
edde3617
ML
11226found:
11227 intel_crtc = to_intel_crtc(crtc);
11228
4d02e2de
SV
11229 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
11230 if (ret)
ad3c558f 11231 goto fail;
79e53945 11232
83a57153 11233 state = drm_atomic_state_alloc(dev);
edde3617
ML
11234 restore_state = drm_atomic_state_alloc(dev);
11235 if (!state || !restore_state) {
11236 ret = -ENOMEM;
11237 goto fail;
11238 }
83a57153
ACO
11239
11240 state->acquire_ctx = ctx;
edde3617 11241 restore_state->acquire_ctx = ctx;
83a57153 11242
944b0c76
ACO
11243 connector_state = drm_atomic_get_connector_state(state, connector);
11244 if (IS_ERR(connector_state)) {
11245 ret = PTR_ERR(connector_state);
11246 goto fail;
11247 }
11248
edde3617
ML
11249 ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
11250 if (ret)
11251 goto fail;
944b0c76 11252
4be07317
ACO
11253 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
11254 if (IS_ERR(crtc_state)) {
11255 ret = PTR_ERR(crtc_state);
11256 goto fail;
11257 }
11258
49d6fa21 11259 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 11260
6492711d
CW
11261 if (!mode)
11262 mode = &load_detect_mode;
79e53945 11263
d2dff872
CW
11264 /* We need a framebuffer large enough to accommodate all accesses
11265 * that the plane may generate whilst we perform load detection.
11266 * We can not rely on the fbcon either being present (we get called
11267 * during its initialisation to detect all boot displays, or it may
11268 * not even exist) or that it is large enough to satisfy the
11269 * requested mode.
11270 */
94352cf9
SV
11271 fb = mode_fits_in_fbdev(dev, mode);
11272 if (fb == NULL) {
d2dff872 11273 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9 11274 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
d2dff872
CW
11275 } else
11276 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 11277 if (IS_ERR(fb)) {
d2dff872 11278 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 11279 goto fail;
79e53945 11280 }
79e53945 11281
d3a40d1b
ACO
11282 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
11283 if (ret)
11284 goto fail;
11285
edde3617
ML
11286 drm_framebuffer_unreference(fb);
11287
11288 ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
11289 if (ret)
11290 goto fail;
11291
11292 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
11293 if (!ret)
11294 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
11295 if (!ret)
11296 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
11297 if (ret) {
11298 DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
11299 goto fail;
11300 }
8c7b5ccb 11301
3ba86073
ML
11302 ret = drm_atomic_commit(state);
11303 if (ret) {
6492711d 11304 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
412b61d8 11305 goto fail;
79e53945 11306 }
edde3617
ML
11307
11308 old->restore_state = restore_state;
7173188d 11309
79e53945 11310 /* let the connector get through one full cycle before testing */
0f0f74bc 11311 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7173188d 11312 return true;
412b61d8 11313
ad3c558f 11314fail:
7fb71c8f
CW
11315 if (state) {
11316 drm_atomic_state_put(state);
11317 state = NULL;
11318 }
11319 if (restore_state) {
11320 drm_atomic_state_put(restore_state);
11321 restore_state = NULL;
11322 }
83a57153 11323
51fd371b
RC
11324 if (ret == -EDEADLK) {
11325 drm_modeset_backoff(ctx);
11326 goto retry;
11327 }
11328
412b61d8 11329 return false;
79e53945
JB
11330}
11331
d2434ab7 11332void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
11333 struct intel_load_detect_pipe *old,
11334 struct drm_modeset_acquire_ctx *ctx)
79e53945 11335{
d2434ab7
SV
11336 struct intel_encoder *intel_encoder =
11337 intel_attached_encoder(connector);
4ef69c7a 11338 struct drm_encoder *encoder = &intel_encoder->base;
edde3617 11339 struct drm_atomic_state *state = old->restore_state;
d3a40d1b 11340 int ret;
79e53945 11341
d2dff872 11342 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 11343 connector->base.id, connector->name,
8e329a03 11344 encoder->base.id, encoder->name);
d2dff872 11345
edde3617 11346 if (!state)
0622a53c 11347 return;
79e53945 11348
edde3617 11349 ret = drm_atomic_commit(state);
0853695c 11350 if (ret)
edde3617 11351 DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
0853695c 11352 drm_atomic_state_put(state);
79e53945
JB
11353}
11354
da4a1efa 11355static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 11356 const struct intel_crtc_state *pipe_config)
da4a1efa 11357{
fac5e23e 11358 struct drm_i915_private *dev_priv = to_i915(dev);
da4a1efa
VS
11359 u32 dpll = pipe_config->dpll_hw_state.dpll;
11360
11361 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 11362 return dev_priv->vbt.lvds_ssc_freq;
6e266956 11363 else if (HAS_PCH_SPLIT(dev_priv))
da4a1efa 11364 return 120000;
5db94019 11365 else if (!IS_GEN2(dev_priv))
da4a1efa
VS
11366 return 96000;
11367 else
11368 return 48000;
11369}
11370
79e53945 11371/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 11372static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 11373 struct intel_crtc_state *pipe_config)
79e53945 11374{
f1f644dc 11375 struct drm_device *dev = crtc->base.dev;
fac5e23e 11376 struct drm_i915_private *dev_priv = to_i915(dev);
f1f644dc 11377 int pipe = pipe_config->cpu_transcoder;
293623f7 11378 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945 11379 u32 fp;
9e2c8475 11380 struct dpll clock;
dccbea3b 11381 int port_clock;
da4a1efa 11382 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
11383
11384 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 11385 fp = pipe_config->dpll_hw_state.fp0;
79e53945 11386 else
293623f7 11387 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
11388
11389 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9b1e14f4 11390 if (IS_PINEVIEW(dev_priv)) {
f2b115e6
AJ
11391 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
11392 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
11393 } else {
11394 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
11395 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
11396 }
11397
5db94019 11398 if (!IS_GEN2(dev_priv)) {
9b1e14f4 11399 if (IS_PINEVIEW(dev_priv))
f2b115e6
AJ
11400 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
11401 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
11402 else
11403 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
11404 DPLL_FPA01_P1_POST_DIV_SHIFT);
11405
11406 switch (dpll & DPLL_MODE_MASK) {
11407 case DPLLB_MODE_DAC_SERIAL:
11408 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
11409 5 : 10;
11410 break;
11411 case DPLLB_MODE_LVDS:
11412 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
11413 7 : 14;
11414 break;
11415 default:
28c97730 11416 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 11417 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 11418 return;
79e53945
JB
11419 }
11420
9b1e14f4 11421 if (IS_PINEVIEW(dev_priv))
dccbea3b 11422 port_clock = pnv_calc_dpll_params(refclk, &clock);
ac58c3f0 11423 else
dccbea3b 11424 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945 11425 } else {
50a0bc90 11426 u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
b1c560d1 11427 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
11428
11429 if (is_lvds) {
11430 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
11431 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
11432
11433 if (lvds & LVDS_CLKB_POWER_UP)
11434 clock.p2 = 7;
11435 else
11436 clock.p2 = 14;
79e53945
JB
11437 } else {
11438 if (dpll & PLL_P1_DIVIDE_BY_TWO)
11439 clock.p1 = 2;
11440 else {
11441 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
11442 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
11443 }
11444 if (dpll & PLL_P2_DIVIDE_BY_4)
11445 clock.p2 = 4;
11446 else
11447 clock.p2 = 2;
79e53945 11448 }
da4a1efa 11449
dccbea3b 11450 port_clock = i9xx_calc_dpll_params(refclk, &clock);
79e53945
JB
11451 }
11452
18442d08
VS
11453 /*
11454 * This value includes pixel_multiplier. We will use
241bfc38 11455 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
11456 * encoder's get_config() function.
11457 */
dccbea3b 11458 pipe_config->port_clock = port_clock;
f1f644dc
JB
11459}
11460
6878da05
VS
11461int intel_dotclock_calculate(int link_freq,
11462 const struct intel_link_m_n *m_n)
f1f644dc 11463{
f1f644dc
JB
11464 /*
11465 * The calculation for the data clock is:
1041a02f 11466 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 11467 * But we want to avoid losing precison if possible, so:
1041a02f 11468 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
11469 *
11470 * and the link clock is simpler:
1041a02f 11471 * link_clock = (m * link_clock) / n
f1f644dc
JB
11472 */
11473
6878da05
VS
11474 if (!m_n->link_n)
11475 return 0;
f1f644dc 11476
6878da05
VS
11477 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
11478}
f1f644dc 11479
18442d08 11480static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 11481 struct intel_crtc_state *pipe_config)
6878da05 11482{
e3b247da 11483 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
79e53945 11484
18442d08
VS
11485 /* read out port_clock from the DPLL */
11486 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 11487
f1f644dc 11488 /*
e3b247da
VS
11489 * In case there is an active pipe without active ports,
11490 * we may need some idea for the dotclock anyway.
11491 * Calculate one based on the FDI configuration.
79e53945 11492 */
2d112de7 11493 pipe_config->base.adjusted_mode.crtc_clock =
21a727b3 11494 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
18442d08 11495 &pipe_config->fdi_m_n);
79e53945
JB
11496}
11497
11498/** Returns the currently programmed mode of the given pipe. */
11499struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
11500 struct drm_crtc *crtc)
11501{
fac5e23e 11502 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 11503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 11504 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 11505 struct drm_display_mode *mode;
3f36b937 11506 struct intel_crtc_state *pipe_config;
fe2b8f9d
PZ
11507 int htot = I915_READ(HTOTAL(cpu_transcoder));
11508 int hsync = I915_READ(HSYNC(cpu_transcoder));
11509 int vtot = I915_READ(VTOTAL(cpu_transcoder));
11510 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 11511 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
11512
11513 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
11514 if (!mode)
11515 return NULL;
11516
3f36b937
TU
11517 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
11518 if (!pipe_config) {
11519 kfree(mode);
11520 return NULL;
11521 }
11522
f1f644dc
JB
11523 /*
11524 * Construct a pipe_config sufficient for getting the clock info
11525 * back out of crtc_clock_get.
11526 *
11527 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
11528 * to use a real value here instead.
11529 */
3f36b937
TU
11530 pipe_config->cpu_transcoder = (enum transcoder) pipe;
11531 pipe_config->pixel_multiplier = 1;
11532 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
11533 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
11534 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
11535 i9xx_crtc_clock_get(intel_crtc, pipe_config);
11536
11537 mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
79e53945
JB
11538 mode->hdisplay = (htot & 0xffff) + 1;
11539 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
11540 mode->hsync_start = (hsync & 0xffff) + 1;
11541 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
11542 mode->vdisplay = (vtot & 0xffff) + 1;
11543 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
11544 mode->vsync_start = (vsync & 0xffff) + 1;
11545 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
11546
11547 drm_mode_set_name(mode);
79e53945 11548
3f36b937
TU
11549 kfree(pipe_config);
11550
79e53945
JB
11551 return mode;
11552}
11553
11554static void intel_crtc_destroy(struct drm_crtc *crtc)
11555{
11556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a 11557 struct drm_device *dev = crtc->dev;
51cbaf01 11558 struct intel_flip_work *work;
67e77c5a 11559
5e2d7afc 11560 spin_lock_irq(&dev->event_lock);
5a21b665
SV
11561 work = intel_crtc->flip_work;
11562 intel_crtc->flip_work = NULL;
11563 spin_unlock_irq(&dev->event_lock);
67e77c5a 11564
5a21b665 11565 if (work) {
51cbaf01
ML
11566 cancel_work_sync(&work->mmio_work);
11567 cancel_work_sync(&work->unpin_work);
5a21b665 11568 kfree(work);
67e77c5a 11569 }
79e53945
JB
11570
11571 drm_crtc_cleanup(crtc);
67e77c5a 11572
79e53945
JB
11573 kfree(intel_crtc);
11574}
11575
6b95a207
KH
11576static void intel_unpin_work_fn(struct work_struct *__work)
11577{
51cbaf01
ML
11578 struct intel_flip_work *work =
11579 container_of(__work, struct intel_flip_work, unpin_work);
5a21b665
SV
11580 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
11581 struct drm_device *dev = crtc->base.dev;
11582 struct drm_plane *primary = crtc->base.primary;
03f476e1 11583
5a21b665
SV
11584 if (is_mmio_work(work))
11585 flush_work(&work->mmio_work);
03f476e1 11586
5a21b665
SV
11587 mutex_lock(&dev->struct_mutex);
11588 intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
f8c417cd 11589 i915_gem_object_put(work->pending_flip_obj);
5a21b665 11590 mutex_unlock(&dev->struct_mutex);
143f73b3 11591
e8a261ea
CW
11592 i915_gem_request_put(work->flip_queued_req);
11593
5748b6a1
CW
11594 intel_frontbuffer_flip_complete(to_i915(dev),
11595 to_intel_plane(primary)->frontbuffer_bit);
5a21b665
SV
11596 intel_fbc_post_update(crtc);
11597 drm_framebuffer_unreference(work->old_fb);
143f73b3 11598
5a21b665
SV
11599 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
11600 atomic_dec(&crtc->unpin_work_count);
a6747b73 11601
5a21b665
SV
11602 kfree(work);
11603}
d9e86c0e 11604
5a21b665
SV
11605/* Is 'a' after or equal to 'b'? */
11606static bool g4x_flip_count_after_eq(u32 a, u32 b)
11607{
11608 return !((a - b) & 0x80000000);
11609}
143f73b3 11610
5a21b665
SV
11611static bool __pageflip_finished_cs(struct intel_crtc *crtc,
11612 struct intel_flip_work *work)
11613{
11614 struct drm_device *dev = crtc->base.dev;
fac5e23e 11615 struct drm_i915_private *dev_priv = to_i915(dev);
143f73b3 11616
8af29b0c 11617 if (abort_flip_on_reset(crtc))
5a21b665 11618 return true;
143f73b3 11619
5a21b665
SV
11620 /*
11621 * The relevant registers doen't exist on pre-ctg.
11622 * As the flip done interrupt doesn't trigger for mmio
11623 * flips on gmch platforms, a flip count check isn't
11624 * really needed there. But since ctg has the registers,
11625 * include it in the check anyway.
11626 */
9beb5fea 11627 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
5a21b665 11628 return true;
b4a98e57 11629
5a21b665
SV
11630 /*
11631 * BDW signals flip done immediately if the plane
11632 * is disabled, even if the plane enable is already
11633 * armed to occur at the next vblank :(
11634 */
f99d7069 11635
5a21b665
SV
11636 /*
11637 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11638 * used the same base address. In that case the mmio flip might
11639 * have completed, but the CS hasn't even executed the flip yet.
11640 *
11641 * A flip count check isn't enough as the CS might have updated
11642 * the base address just after start of vblank, but before we
11643 * managed to process the interrupt. This means we'd complete the
11644 * CS flip too soon.
11645 *
11646 * Combining both checks should get us a good enough result. It may
11647 * still happen that the CS flip has been executed, but has not
11648 * yet actually completed. But in case the base address is the same
11649 * anyway, we don't really care.
11650 */
11651 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11652 crtc->flip_work->gtt_offset &&
11653 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
11654 crtc->flip_work->flip_count);
11655}
b4a98e57 11656
5a21b665
SV
11657static bool
11658__pageflip_finished_mmio(struct intel_crtc *crtc,
11659 struct intel_flip_work *work)
11660{
11661 /*
11662 * MMIO work completes when vblank is different from
11663 * flip_queued_vblank.
11664 *
11665 * Reset counter value doesn't matter, this is handled by
11666 * i915_wait_request finishing early, so no need to handle
11667 * reset here.
11668 */
11669 return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
6b95a207
KH
11670}
11671
51cbaf01
ML
11672
11673static bool pageflip_finished(struct intel_crtc *crtc,
11674 struct intel_flip_work *work)
11675{
11676 if (!atomic_read(&work->pending))
11677 return false;
11678
11679 smp_rmb();
11680
5a21b665
SV
11681 if (is_mmio_work(work))
11682 return __pageflip_finished_mmio(crtc, work);
11683 else
11684 return __pageflip_finished_cs(crtc, work);
11685}
11686
11687void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
11688{
91c8a326 11689 struct drm_device *dev = &dev_priv->drm;
98187836 11690 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
SV
11691 struct intel_flip_work *work;
11692 unsigned long flags;
11693
11694 /* Ignore early vblank irqs */
11695 if (!crtc)
11696 return;
11697
51cbaf01 11698 /*
5a21b665
SV
11699 * This is called both by irq handlers and the reset code (to complete
11700 * lost pageflips) so needs the full irqsave spinlocks.
51cbaf01 11701 */
5a21b665 11702 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11703 work = crtc->flip_work;
5a21b665
SV
11704
11705 if (work != NULL &&
11706 !is_mmio_work(work) &&
e2af48c6
VS
11707 pageflip_finished(crtc, work))
11708 page_flip_completed(crtc);
5a21b665
SV
11709
11710 spin_unlock_irqrestore(&dev->event_lock, flags);
75f7f3ec
VS
11711}
11712
51cbaf01 11713void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
6b95a207 11714{
91c8a326 11715 struct drm_device *dev = &dev_priv->drm;
98187836 11716 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
51cbaf01 11717 struct intel_flip_work *work;
6b95a207
KH
11718 unsigned long flags;
11719
5251f04e
ML
11720 /* Ignore early vblank irqs */
11721 if (!crtc)
11722 return;
f326038a
SV
11723
11724 /*
11725 * This is called both by irq handlers and the reset code (to complete
11726 * lost pageflips) so needs the full irqsave spinlocks.
e7d841ca 11727 */
6b95a207 11728 spin_lock_irqsave(&dev->event_lock, flags);
e2af48c6 11729 work = crtc->flip_work;
5251f04e 11730
5a21b665
SV
11731 if (work != NULL &&
11732 is_mmio_work(work) &&
e2af48c6
VS
11733 pageflip_finished(crtc, work))
11734 page_flip_completed(crtc);
5251f04e 11735
6b95a207
KH
11736 spin_unlock_irqrestore(&dev->event_lock, flags);
11737}
11738
5a21b665
SV
11739static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
11740 struct intel_flip_work *work)
84c33a64 11741{
5a21b665 11742 work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
84c33a64 11743
5a21b665
SV
11744 /* Ensure that the work item is consistent when activating it ... */
11745 smp_mb__before_atomic();
11746 atomic_set(&work->pending, 1);
11747}
a6747b73 11748
5a21b665
SV
11749static int intel_gen2_queue_flip(struct drm_device *dev,
11750 struct drm_crtc *crtc,
11751 struct drm_framebuffer *fb,
11752 struct drm_i915_gem_object *obj,
11753 struct drm_i915_gem_request *req,
11754 uint32_t flags)
11755{
7e37f889 11756 struct intel_ring *ring = req->ring;
5a21b665
SV
11757 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11758 u32 flip_mask;
11759 int ret;
143f73b3 11760
5a21b665
SV
11761 ret = intel_ring_begin(req, 6);
11762 if (ret)
11763 return ret;
143f73b3 11764
5a21b665
SV
11765 /* Can't queue multiple flips, so wait for the previous
11766 * one to finish before executing the next.
11767 */
11768 if (intel_crtc->plane)
11769 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11770 else
11771 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11772 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11773 intel_ring_emit(ring, MI_NOOP);
11774 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11775 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11776 intel_ring_emit(ring, fb->pitches[0]);
11777 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11778 intel_ring_emit(ring, 0); /* aux display base address, unused */
143f73b3 11779
5a21b665
SV
11780 return 0;
11781}
84c33a64 11782
5a21b665
SV
11783static int intel_gen3_queue_flip(struct drm_device *dev,
11784 struct drm_crtc *crtc,
11785 struct drm_framebuffer *fb,
11786 struct drm_i915_gem_object *obj,
11787 struct drm_i915_gem_request *req,
11788 uint32_t flags)
11789{
7e37f889 11790 struct intel_ring *ring = req->ring;
5a21b665
SV
11791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11792 u32 flip_mask;
11793 int ret;
d55dbd06 11794
5a21b665
SV
11795 ret = intel_ring_begin(req, 6);
11796 if (ret)
11797 return ret;
d55dbd06 11798
5a21b665
SV
11799 if (intel_crtc->plane)
11800 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11801 else
11802 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
b5321f30
CW
11803 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11804 intel_ring_emit(ring, MI_NOOP);
11805 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5a21b665 11806 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11807 intel_ring_emit(ring, fb->pitches[0]);
11808 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11809 intel_ring_emit(ring, MI_NOOP);
fd8e058a 11810
5a21b665
SV
11811 return 0;
11812}
84c33a64 11813
5a21b665
SV
11814static int intel_gen4_queue_flip(struct drm_device *dev,
11815 struct drm_crtc *crtc,
11816 struct drm_framebuffer *fb,
11817 struct drm_i915_gem_object *obj,
11818 struct drm_i915_gem_request *req,
11819 uint32_t flags)
11820{
7e37f889 11821 struct intel_ring *ring = req->ring;
fac5e23e 11822 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11824 uint32_t pf, pipesrc;
11825 int ret;
143f73b3 11826
5a21b665
SV
11827 ret = intel_ring_begin(req, 4);
11828 if (ret)
11829 return ret;
143f73b3 11830
5a21b665
SV
11831 /* i965+ uses the linear or tiled offsets from the
11832 * Display Registers (which do not change across a page-flip)
11833 * so we need only reprogram the base address.
11834 */
b5321f30 11835 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11836 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
b5321f30
CW
11837 intel_ring_emit(ring, fb->pitches[0]);
11838 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
bae781b2 11839 intel_fb_modifier_to_tiling(fb->modifier));
5a21b665
SV
11840
11841 /* XXX Enabling the panel-fitter across page-flip is so far
11842 * untested on non-native modes, so ignore it for now.
11843 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11844 */
11845 pf = 0;
11846 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11847 intel_ring_emit(ring, pf | pipesrc);
143f73b3 11848
5a21b665 11849 return 0;
8c9f3aaf
JB
11850}
11851
5a21b665
SV
11852static int intel_gen6_queue_flip(struct drm_device *dev,
11853 struct drm_crtc *crtc,
11854 struct drm_framebuffer *fb,
11855 struct drm_i915_gem_object *obj,
11856 struct drm_i915_gem_request *req,
11857 uint32_t flags)
da20eabd 11858{
7e37f889 11859 struct intel_ring *ring = req->ring;
fac5e23e 11860 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
11861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11862 uint32_t pf, pipesrc;
11863 int ret;
d21fbe87 11864
5a21b665
SV
11865 ret = intel_ring_begin(req, 4);
11866 if (ret)
11867 return ret;
92826fcd 11868
b5321f30 11869 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5a21b665 11870 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
72618ebf 11871 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11872 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30 11873 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
92826fcd 11874
5a21b665
SV
11875 /* Contrary to the suggestions in the documentation,
11876 * "Enable Panel Fitter" does not seem to be required when page
11877 * flipping with a non-native mode, and worse causes a normal
11878 * modeset to fail.
11879 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11880 */
11881 pf = 0;
11882 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
b5321f30 11883 intel_ring_emit(ring, pf | pipesrc);
7809e5ae 11884
5a21b665 11885 return 0;
7809e5ae
MR
11886}
11887
5a21b665
SV
11888static int intel_gen7_queue_flip(struct drm_device *dev,
11889 struct drm_crtc *crtc,
11890 struct drm_framebuffer *fb,
11891 struct drm_i915_gem_object *obj,
11892 struct drm_i915_gem_request *req,
11893 uint32_t flags)
d21fbe87 11894{
5db94019 11895 struct drm_i915_private *dev_priv = to_i915(dev);
7e37f889 11896 struct intel_ring *ring = req->ring;
5a21b665
SV
11897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11898 uint32_t plane_bit = 0;
11899 int len, ret;
d21fbe87 11900
5a21b665
SV
11901 switch (intel_crtc->plane) {
11902 case PLANE_A:
11903 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11904 break;
11905 case PLANE_B:
11906 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11907 break;
11908 case PLANE_C:
11909 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11910 break;
11911 default:
11912 WARN_ONCE(1, "unknown plane in flip command\n");
11913 return -ENODEV;
11914 }
11915
11916 len = 4;
b5321f30 11917 if (req->engine->id == RCS) {
5a21b665
SV
11918 len += 6;
11919 /*
11920 * On Gen 8, SRM is now taking an extra dword to accommodate
11921 * 48bits addresses, and we need a NOOP for the batch size to
11922 * stay even.
11923 */
5db94019 11924 if (IS_GEN8(dev_priv))
5a21b665
SV
11925 len += 2;
11926 }
11927
11928 /*
11929 * BSpec MI_DISPLAY_FLIP for IVB:
11930 * "The full packet must be contained within the same cache line."
11931 *
11932 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11933 * cacheline, if we ever start emitting more commands before
11934 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11935 * then do the cacheline alignment, and finally emit the
11936 * MI_DISPLAY_FLIP.
11937 */
11938 ret = intel_ring_cacheline_align(req);
11939 if (ret)
11940 return ret;
11941
11942 ret = intel_ring_begin(req, len);
11943 if (ret)
11944 return ret;
11945
11946 /* Unmask the flip-done completion message. Note that the bspec says that
11947 * we should do this for both the BCS and RCS, and that we must not unmask
11948 * more than one flip event at any time (or ensure that one flip message
11949 * can be sent by waiting for flip-done prior to queueing new flips).
11950 * Experimentation says that BCS works despite DERRMR masking all
11951 * flip-done completion events and that unmasking all planes at once
11952 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11953 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11954 */
b5321f30
CW
11955 if (req->engine->id == RCS) {
11956 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11957 intel_ring_emit_reg(ring, DERRMR);
11958 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
5a21b665
SV
11959 DERRMR_PIPEB_PRI_FLIP_DONE |
11960 DERRMR_PIPEC_PRI_FLIP_DONE));
5db94019 11961 if (IS_GEN8(dev_priv))
b5321f30 11962 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
5a21b665
SV
11963 MI_SRM_LRM_GLOBAL_GTT);
11964 else
b5321f30 11965 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
5a21b665 11966 MI_SRM_LRM_GLOBAL_GTT);
b5321f30 11967 intel_ring_emit_reg(ring, DERRMR);
bde13ebd
CW
11968 intel_ring_emit(ring,
11969 i915_ggtt_offset(req->engine->scratch) + 256);
5db94019 11970 if (IS_GEN8(dev_priv)) {
b5321f30
CW
11971 intel_ring_emit(ring, 0);
11972 intel_ring_emit(ring, MI_NOOP);
5a21b665
SV
11973 }
11974 }
11975
b5321f30 11976 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
72618ebf 11977 intel_ring_emit(ring, fb->pitches[0] |
bae781b2 11978 intel_fb_modifier_to_tiling(fb->modifier));
b5321f30
CW
11979 intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
11980 intel_ring_emit(ring, (MI_NOOP));
5a21b665
SV
11981
11982 return 0;
11983}
11984
11985static bool use_mmio_flip(struct intel_engine_cs *engine,
11986 struct drm_i915_gem_object *obj)
11987{
11988 /*
11989 * This is not being used for older platforms, because
11990 * non-availability of flip done interrupt forces us to use
11991 * CS flips. Older platforms derive flip done using some clever
11992 * tricks involving the flip_pending status bits and vblank irqs.
11993 * So using MMIO flips there would disrupt this mechanism.
11994 */
11995
11996 if (engine == NULL)
11997 return true;
11998
11999 if (INTEL_GEN(engine->i915) < 5)
12000 return false;
12001
12002 if (i915.use_mmio_flip < 0)
12003 return false;
12004 else if (i915.use_mmio_flip > 0)
12005 return true;
12006 else if (i915.enable_execlists)
12007 return true;
c37efb99 12008
d07f0e59 12009 return engine != i915_gem_object_last_write_engine(obj);
5a21b665
SV
12010}
12011
12012static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
12013 unsigned int rotation,
12014 struct intel_flip_work *work)
12015{
12016 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12017 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
12018 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
12019 const enum pipe pipe = intel_crtc->pipe;
d2196774 12020 u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
5a21b665
SV
12021
12022 ctl = I915_READ(PLANE_CTL(pipe, 0));
12023 ctl &= ~PLANE_CTL_TILED_MASK;
bae781b2 12024 switch (fb->modifier) {
5a21b665
SV
12025 case DRM_FORMAT_MOD_NONE:
12026 break;
12027 case I915_FORMAT_MOD_X_TILED:
12028 ctl |= PLANE_CTL_TILED_X;
12029 break;
12030 case I915_FORMAT_MOD_Y_TILED:
12031 ctl |= PLANE_CTL_TILED_Y;
12032 break;
12033 case I915_FORMAT_MOD_Yf_TILED:
12034 ctl |= PLANE_CTL_TILED_YF;
12035 break;
12036 default:
bae781b2 12037 MISSING_CASE(fb->modifier);
5a21b665
SV
12038 }
12039
5a21b665
SV
12040 /*
12041 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
12042 * PLANE_SURF updates, the update is then guaranteed to be atomic.
12043 */
12044 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
12045 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
12046
12047 I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
12048 POSTING_READ(PLANE_SURF(pipe, 0));
12049}
12050
12051static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
12052 struct intel_flip_work *work)
12053{
12054 struct drm_device *dev = intel_crtc->base.dev;
fac5e23e 12055 struct drm_i915_private *dev_priv = to_i915(dev);
72618ebf 12056 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
5a21b665
SV
12057 i915_reg_t reg = DSPCNTR(intel_crtc->plane);
12058 u32 dspcntr;
12059
12060 dspcntr = I915_READ(reg);
12061
bae781b2 12062 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
5a21b665
SV
12063 dspcntr |= DISPPLANE_TILED;
12064 else
12065 dspcntr &= ~DISPPLANE_TILED;
12066
12067 I915_WRITE(reg, dspcntr);
12068
12069 I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
12070 POSTING_READ(DSPSURF(intel_crtc->plane));
12071}
12072
12073static void intel_mmio_flip_work_func(struct work_struct *w)
12074{
12075 struct intel_flip_work *work =
12076 container_of(w, struct intel_flip_work, mmio_work);
12077 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
12078 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12079 struct intel_framebuffer *intel_fb =
12080 to_intel_framebuffer(crtc->base.primary->fb);
12081 struct drm_i915_gem_object *obj = intel_fb->obj;
12082
d07f0e59 12083 WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
5a21b665
SV
12084
12085 intel_pipe_update_start(crtc);
12086
12087 if (INTEL_GEN(dev_priv) >= 9)
12088 skl_do_mmio_flip(crtc, work->rotation, work);
12089 else
12090 /* use_mmio_flip() retricts MMIO flips to ilk+ */
12091 ilk_do_mmio_flip(crtc, work);
12092
12093 intel_pipe_update_end(crtc, work);
12094}
12095
12096static int intel_default_queue_flip(struct drm_device *dev,
12097 struct drm_crtc *crtc,
12098 struct drm_framebuffer *fb,
12099 struct drm_i915_gem_object *obj,
12100 struct drm_i915_gem_request *req,
12101 uint32_t flags)
12102{
12103 return -ENODEV;
12104}
12105
12106static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
12107 struct intel_crtc *intel_crtc,
12108 struct intel_flip_work *work)
12109{
12110 u32 addr, vblank;
12111
12112 if (!atomic_read(&work->pending))
12113 return false;
12114
12115 smp_rmb();
12116
12117 vblank = intel_crtc_get_vblank_counter(intel_crtc);
12118 if (work->flip_ready_vblank == 0) {
12119 if (work->flip_queued_req &&
f69a02c9 12120 !i915_gem_request_completed(work->flip_queued_req))
5a21b665
SV
12121 return false;
12122
12123 work->flip_ready_vblank = vblank;
12124 }
12125
12126 if (vblank - work->flip_ready_vblank < 3)
12127 return false;
12128
12129 /* Potential stall - if we see that the flip has happened,
12130 * assume a missed interrupt. */
12131 if (INTEL_GEN(dev_priv) >= 4)
12132 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
12133 else
12134 addr = I915_READ(DSPADDR(intel_crtc->plane));
12135
12136 /* There is a potential issue here with a false positive after a flip
12137 * to the same address. We could address this by checking for a
12138 * non-incrementing frame counter.
12139 */
12140 return addr == work->gtt_offset;
12141}
12142
12143void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
12144{
91c8a326 12145 struct drm_device *dev = &dev_priv->drm;
98187836 12146 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5a21b665
SV
12147 struct intel_flip_work *work;
12148
12149 WARN_ON(!in_interrupt());
12150
12151 if (crtc == NULL)
12152 return;
12153
12154 spin_lock(&dev->event_lock);
e2af48c6 12155 work = crtc->flip_work;
5a21b665
SV
12156
12157 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12158 __pageflip_stall_check_cs(dev_priv, crtc, work)) {
5a21b665
SV
12159 WARN_ONCE(1,
12160 "Kicking stuck page flip: queued at %d, now %d\n",
e2af48c6
VS
12161 work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
12162 page_flip_completed(crtc);
5a21b665
SV
12163 work = NULL;
12164 }
12165
12166 if (work != NULL && !is_mmio_work(work) &&
e2af48c6 12167 intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
5a21b665
SV
12168 intel_queue_rps_boost_for_request(work->flip_queued_req);
12169 spin_unlock(&dev->event_lock);
12170}
12171
12172static int intel_crtc_page_flip(struct drm_crtc *crtc,
12173 struct drm_framebuffer *fb,
12174 struct drm_pending_vblank_event *event,
12175 uint32_t page_flip_flags)
12176{
12177 struct drm_device *dev = crtc->dev;
fac5e23e 12178 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665
SV
12179 struct drm_framebuffer *old_fb = crtc->primary->fb;
12180 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12181 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12182 struct drm_plane *primary = crtc->primary;
12183 enum pipe pipe = intel_crtc->pipe;
12184 struct intel_flip_work *work;
12185 struct intel_engine_cs *engine;
12186 bool mmio_flip;
8e637178 12187 struct drm_i915_gem_request *request;
058d88c4 12188 struct i915_vma *vma;
5a21b665
SV
12189 int ret;
12190
12191 /*
12192 * drm_mode_page_flip_ioctl() should already catch this, but double
12193 * check to be safe. In the future we may enable pageflipping from
12194 * a disabled primary plane.
12195 */
12196 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
12197 return -EBUSY;
12198
12199 /* Can't change pixel format via MI display flips. */
12200 if (fb->pixel_format != crtc->primary->fb->pixel_format)
12201 return -EINVAL;
12202
12203 /*
12204 * TILEOFF/LINOFF registers can't be changed via MI display flips.
12205 * Note that pitch changes could also affect these register.
12206 */
6315b5d3 12207 if (INTEL_GEN(dev_priv) > 3 &&
5a21b665
SV
12208 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
12209 fb->pitches[0] != crtc->primary->fb->pitches[0]))
12210 return -EINVAL;
12211
12212 if (i915_terminally_wedged(&dev_priv->gpu_error))
12213 goto out_hang;
12214
12215 work = kzalloc(sizeof(*work), GFP_KERNEL);
12216 if (work == NULL)
12217 return -ENOMEM;
12218
12219 work->event = event;
12220 work->crtc = crtc;
12221 work->old_fb = old_fb;
12222 INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
12223
12224 ret = drm_crtc_vblank_get(crtc);
12225 if (ret)
12226 goto free_work;
12227
12228 /* We borrow the event spin lock for protecting flip_work */
12229 spin_lock_irq(&dev->event_lock);
12230 if (intel_crtc->flip_work) {
12231 /* Before declaring the flip queue wedged, check if
12232 * the hardware completed the operation behind our backs.
12233 */
12234 if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
12235 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
12236 page_flip_completed(intel_crtc);
12237 } else {
12238 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
12239 spin_unlock_irq(&dev->event_lock);
12240
12241 drm_crtc_vblank_put(crtc);
12242 kfree(work);
12243 return -EBUSY;
12244 }
12245 }
12246 intel_crtc->flip_work = work;
12247 spin_unlock_irq(&dev->event_lock);
12248
12249 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
12250 flush_workqueue(dev_priv->wq);
12251
12252 /* Reference the objects for the scheduled work. */
12253 drm_framebuffer_reference(work->old_fb);
5a21b665
SV
12254
12255 crtc->primary->fb = fb;
12256 update_state_fb(crtc->primary);
faf68d92 12257
25dc556a 12258 work->pending_flip_obj = i915_gem_object_get(obj);
5a21b665
SV
12259
12260 ret = i915_mutex_lock_interruptible(dev);
12261 if (ret)
12262 goto cleanup;
12263
8af29b0c
CW
12264 intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
12265 if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
5a21b665 12266 ret = -EIO;
ddbb271a 12267 goto unlock;
5a21b665
SV
12268 }
12269
12270 atomic_inc(&intel_crtc->unpin_work_count);
12271
9beb5fea 12272 if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
5a21b665
SV
12273 work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
12274
920a14b2 12275 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
3b3f1650 12276 engine = dev_priv->engine[BCS];
bae781b2 12277 if (fb->modifier != old_fb->modifier)
5a21b665
SV
12278 /* vlv: DISPLAY_FLIP fails to change tiling */
12279 engine = NULL;
fd6b8f43 12280 } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
3b3f1650 12281 engine = dev_priv->engine[BCS];
6315b5d3 12282 } else if (INTEL_GEN(dev_priv) >= 7) {
d07f0e59 12283 engine = i915_gem_object_last_write_engine(obj);
5a21b665 12284 if (engine == NULL || engine->id != RCS)
3b3f1650 12285 engine = dev_priv->engine[BCS];
5a21b665 12286 } else {
3b3f1650 12287 engine = dev_priv->engine[RCS];
5a21b665
SV
12288 }
12289
12290 mmio_flip = use_mmio_flip(engine, obj);
12291
058d88c4
CW
12292 vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
12293 if (IS_ERR(vma)) {
12294 ret = PTR_ERR(vma);
5a21b665 12295 goto cleanup_pending;
058d88c4 12296 }
5a21b665 12297
6687c906 12298 work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
5a21b665
SV
12299 work->gtt_offset += intel_crtc->dspaddr_offset;
12300 work->rotation = crtc->primary->state->rotation;
12301
1f061316
PZ
12302 /*
12303 * There's the potential that the next frame will not be compatible with
12304 * FBC, so we want to call pre_update() before the actual page flip.
12305 * The problem is that pre_update() caches some information about the fb
12306 * object, so we want to do this only after the object is pinned. Let's
12307 * be on the safe side and do this immediately before scheduling the
12308 * flip.
12309 */
12310 intel_fbc_pre_update(intel_crtc, intel_crtc->config,
12311 to_intel_plane_state(primary->state));
12312
5a21b665
SV
12313 if (mmio_flip) {
12314 INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
6277c8d0 12315 queue_work(system_unbound_wq, &work->mmio_work);
5a21b665 12316 } else {
8e637178
CW
12317 request = i915_gem_request_alloc(engine, engine->last_context);
12318 if (IS_ERR(request)) {
12319 ret = PTR_ERR(request);
12320 goto cleanup_unpin;
12321 }
12322
a2bc4695 12323 ret = i915_gem_request_await_object(request, obj, false);
8e637178
CW
12324 if (ret)
12325 goto cleanup_request;
12326
5a21b665
SV
12327 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
12328 page_flip_flags);
12329 if (ret)
8e637178 12330 goto cleanup_request;
5a21b665
SV
12331
12332 intel_mark_page_flip_active(intel_crtc, work);
12333
8e637178 12334 work->flip_queued_req = i915_gem_request_get(request);
5a21b665
SV
12335 i915_add_request_no_flush(request);
12336 }
12337
92117f0b 12338 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
5a21b665
SV
12339 i915_gem_track_fb(intel_fb_obj(old_fb), obj,
12340 to_intel_plane(primary)->frontbuffer_bit);
12341 mutex_unlock(&dev->struct_mutex);
12342
5748b6a1 12343 intel_frontbuffer_flip_prepare(to_i915(dev),
5a21b665
SV
12344 to_intel_plane(primary)->frontbuffer_bit);
12345
12346 trace_i915_flip_request(intel_crtc->plane, obj);
12347
12348 return 0;
12349
8e637178
CW
12350cleanup_request:
12351 i915_add_request_no_flush(request);
5a21b665
SV
12352cleanup_unpin:
12353 intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
12354cleanup_pending:
5a21b665 12355 atomic_dec(&intel_crtc->unpin_work_count);
ddbb271a 12356unlock:
5a21b665
SV
12357 mutex_unlock(&dev->struct_mutex);
12358cleanup:
12359 crtc->primary->fb = old_fb;
12360 update_state_fb(crtc->primary);
12361
f0cd5182 12362 i915_gem_object_put(obj);
5a21b665
SV
12363 drm_framebuffer_unreference(work->old_fb);
12364
12365 spin_lock_irq(&dev->event_lock);
12366 intel_crtc->flip_work = NULL;
12367 spin_unlock_irq(&dev->event_lock);
12368
12369 drm_crtc_vblank_put(crtc);
12370free_work:
12371 kfree(work);
12372
12373 if (ret == -EIO) {
12374 struct drm_atomic_state *state;
12375 struct drm_plane_state *plane_state;
12376
12377out_hang:
12378 state = drm_atomic_state_alloc(dev);
12379 if (!state)
12380 return -ENOMEM;
12381 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
12382
12383retry:
12384 plane_state = drm_atomic_get_plane_state(state, primary);
12385 ret = PTR_ERR_OR_ZERO(plane_state);
12386 if (!ret) {
12387 drm_atomic_set_fb_for_plane(plane_state, fb);
12388
12389 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
12390 if (!ret)
12391 ret = drm_atomic_commit(state);
12392 }
12393
12394 if (ret == -EDEADLK) {
12395 drm_modeset_backoff(state->acquire_ctx);
12396 drm_atomic_state_clear(state);
12397 goto retry;
12398 }
12399
0853695c 12400 drm_atomic_state_put(state);
5a21b665
SV
12401
12402 if (ret == 0 && event) {
12403 spin_lock_irq(&dev->event_lock);
12404 drm_crtc_send_vblank_event(crtc, event);
12405 spin_unlock_irq(&dev->event_lock);
12406 }
12407 }
12408 return ret;
12409}
12410
12411
12412/**
12413 * intel_wm_need_update - Check whether watermarks need updating
12414 * @plane: drm plane
12415 * @state: new plane state
12416 *
12417 * Check current plane state versus the new one to determine whether
12418 * watermarks need to be recalculated.
12419 *
12420 * Returns true or false.
12421 */
12422static bool intel_wm_need_update(struct drm_plane *plane,
12423 struct drm_plane_state *state)
12424{
12425 struct intel_plane_state *new = to_intel_plane_state(state);
12426 struct intel_plane_state *cur = to_intel_plane_state(plane->state);
12427
12428 /* Update watermarks on tiling or size changes. */
936e71e3 12429 if (new->base.visible != cur->base.visible)
5a21b665
SV
12430 return true;
12431
12432 if (!cur->base.fb || !new->base.fb)
12433 return false;
12434
bae781b2 12435 if (cur->base.fb->modifier != new->base.fb->modifier ||
5a21b665 12436 cur->base.rotation != new->base.rotation ||
936e71e3
VS
12437 drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
12438 drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
12439 drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
12440 drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
5a21b665
SV
12441 return true;
12442
12443 return false;
12444}
12445
12446static bool needs_scaling(struct intel_plane_state *state)
12447{
936e71e3
VS
12448 int src_w = drm_rect_width(&state->base.src) >> 16;
12449 int src_h = drm_rect_height(&state->base.src) >> 16;
12450 int dst_w = drm_rect_width(&state->base.dst);
12451 int dst_h = drm_rect_height(&state->base.dst);
5a21b665
SV
12452
12453 return (src_w != dst_w || src_h != dst_h);
12454}
d21fbe87 12455
da20eabd
ML
12456int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
12457 struct drm_plane_state *plane_state)
12458{
ab1d3a0e 12459 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
da20eabd
ML
12460 struct drm_crtc *crtc = crtc_state->crtc;
12461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12462 struct drm_plane *plane = plane_state->plane;
12463 struct drm_device *dev = crtc->dev;
ed4a6a7c 12464 struct drm_i915_private *dev_priv = to_i915(dev);
da20eabd
ML
12465 struct intel_plane_state *old_plane_state =
12466 to_intel_plane_state(plane->state);
da20eabd
ML
12467 bool mode_changed = needs_modeset(crtc_state);
12468 bool was_crtc_enabled = crtc->state->active;
12469 bool is_crtc_enabled = crtc_state->active;
da20eabd
ML
12470 bool turn_off, turn_on, visible, was_visible;
12471 struct drm_framebuffer *fb = plane_state->fb;
78108b7c 12472 int ret;
da20eabd 12473
55b8f2a7 12474 if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
da20eabd
ML
12475 ret = skl_update_scaler_plane(
12476 to_intel_crtc_state(crtc_state),
12477 to_intel_plane_state(plane_state));
12478 if (ret)
12479 return ret;
12480 }
12481
936e71e3
VS
12482 was_visible = old_plane_state->base.visible;
12483 visible = to_intel_plane_state(plane_state)->base.visible;
da20eabd
ML
12484
12485 if (!was_crtc_enabled && WARN_ON(was_visible))
12486 was_visible = false;
12487
35c08f43
ML
12488 /*
12489 * Visibility is calculated as if the crtc was on, but
12490 * after scaler setup everything depends on it being off
12491 * when the crtc isn't active.
f818ffea
VS
12492 *
12493 * FIXME this is wrong for watermarks. Watermarks should also
12494 * be computed as if the pipe would be active. Perhaps move
12495 * per-plane wm computation to the .check_plane() hook, and
12496 * only combine the results from all planes in the current place?
35c08f43
ML
12497 */
12498 if (!is_crtc_enabled)
936e71e3 12499 to_intel_plane_state(plane_state)->base.visible = visible = false;
da20eabd
ML
12500
12501 if (!was_visible && !visible)
12502 return 0;
12503
e8861675
ML
12504 if (fb != old_plane_state->base.fb)
12505 pipe_config->fb_changed = true;
12506
da20eabd
ML
12507 turn_off = was_visible && (!visible || mode_changed);
12508 turn_on = visible && (!was_visible || mode_changed);
12509
72660ce0 12510 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
78108b7c
VS
12511 intel_crtc->base.base.id,
12512 intel_crtc->base.name,
72660ce0
VS
12513 plane->base.id, plane->name,
12514 fb ? fb->base.id : -1);
da20eabd 12515
72660ce0
VS
12516 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
12517 plane->base.id, plane->name,
12518 was_visible, visible,
da20eabd
ML
12519 turn_off, turn_on, mode_changed);
12520
caed361d
VS
12521 if (turn_on) {
12522 pipe_config->update_wm_pre = true;
12523
12524 /* must disable cxsr around plane enable/disable */
12525 if (plane->type != DRM_PLANE_TYPE_CURSOR)
12526 pipe_config->disable_cxsr = true;
12527 } else if (turn_off) {
12528 pipe_config->update_wm_post = true;
92826fcd 12529
852eb00d 12530 /* must disable cxsr around plane enable/disable */
e8861675 12531 if (plane->type != DRM_PLANE_TYPE_CURSOR)
ab1d3a0e 12532 pipe_config->disable_cxsr = true;
852eb00d 12533 } else if (intel_wm_need_update(plane, plane_state)) {
caed361d
VS
12534 /* FIXME bollocks */
12535 pipe_config->update_wm_pre = true;
12536 pipe_config->update_wm_post = true;
852eb00d 12537 }
da20eabd 12538
ed4a6a7c 12539 /* Pre-gen9 platforms need two-step watermark updates */
caed361d 12540 if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
6315b5d3 12541 INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
ed4a6a7c
MR
12542 to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
12543
8be6ca85 12544 if (visible || was_visible)
cd202f69 12545 pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
a9ff8714 12546
31ae71fc
ML
12547 /*
12548 * WaCxSRDisabledForSpriteScaling:ivb
12549 *
12550 * cstate->update_wm was already set above, so this flag will
12551 * take effect when we commit and program watermarks.
12552 */
fd6b8f43 12553 if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
31ae71fc
ML
12554 needs_scaling(to_intel_plane_state(plane_state)) &&
12555 !needs_scaling(old_plane_state))
12556 pipe_config->disable_lp_wm = true;
d21fbe87 12557
da20eabd
ML
12558 return 0;
12559}
12560
6d3a1ce7
ML
12561static bool encoders_cloneable(const struct intel_encoder *a,
12562 const struct intel_encoder *b)
12563{
12564 /* masks could be asymmetric, so check both ways */
12565 return a == b || (a->cloneable & (1 << b->type) &&
12566 b->cloneable & (1 << a->type));
12567}
12568
12569static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12570 struct intel_crtc *crtc,
12571 struct intel_encoder *encoder)
12572{
12573 struct intel_encoder *source_encoder;
12574 struct drm_connector *connector;
12575 struct drm_connector_state *connector_state;
12576 int i;
12577
12578 for_each_connector_in_state(state, connector, connector_state, i) {
12579 if (connector_state->crtc != &crtc->base)
12580 continue;
12581
12582 source_encoder =
12583 to_intel_encoder(connector_state->best_encoder);
12584 if (!encoders_cloneable(encoder, source_encoder))
12585 return false;
12586 }
12587
12588 return true;
12589}
12590
6d3a1ce7
ML
12591static int intel_crtc_atomic_check(struct drm_crtc *crtc,
12592 struct drm_crtc_state *crtc_state)
12593{
cf5a15be 12594 struct drm_device *dev = crtc->dev;
fac5e23e 12595 struct drm_i915_private *dev_priv = to_i915(dev);
6d3a1ce7 12596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
12597 struct intel_crtc_state *pipe_config =
12598 to_intel_crtc_state(crtc_state);
6d3a1ce7 12599 struct drm_atomic_state *state = crtc_state->state;
4d20cd86 12600 int ret;
6d3a1ce7
ML
12601 bool mode_changed = needs_modeset(crtc_state);
12602
852eb00d 12603 if (mode_changed && !crtc_state->active)
caed361d 12604 pipe_config->update_wm_post = true;
eddfcbcd 12605
ad421372
ML
12606 if (mode_changed && crtc_state->enable &&
12607 dev_priv->display.crtc_compute_clock &&
8106ddbd 12608 !WARN_ON(pipe_config->shared_dpll)) {
ad421372
ML
12609 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12610 pipe_config);
12611 if (ret)
12612 return ret;
12613 }
12614
82cf435b
LL
12615 if (crtc_state->color_mgmt_changed) {
12616 ret = intel_color_check(crtc, crtc_state);
12617 if (ret)
12618 return ret;
e7852a4b
LL
12619
12620 /*
12621 * Changing color management on Intel hardware is
12622 * handled as part of planes update.
12623 */
12624 crtc_state->planes_changed = true;
82cf435b
LL
12625 }
12626
e435d6e5 12627 ret = 0;
86c8bbbe 12628 if (dev_priv->display.compute_pipe_wm) {
e3bddded 12629 ret = dev_priv->display.compute_pipe_wm(pipe_config);
ed4a6a7c
MR
12630 if (ret) {
12631 DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
12632 return ret;
12633 }
12634 }
12635
12636 if (dev_priv->display.compute_intermediate_wm &&
12637 !to_intel_atomic_state(state)->skip_intermediate_wm) {
12638 if (WARN_ON(!dev_priv->display.compute_pipe_wm))
12639 return 0;
12640
12641 /*
12642 * Calculate 'intermediate' watermarks that satisfy both the
12643 * old state and the new state. We can program these
12644 * immediately.
12645 */
6315b5d3 12646 ret = dev_priv->display.compute_intermediate_wm(dev,
ed4a6a7c
MR
12647 intel_crtc,
12648 pipe_config);
12649 if (ret) {
12650 DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
86c8bbbe 12651 return ret;
ed4a6a7c 12652 }
e3d5457c
VS
12653 } else if (dev_priv->display.compute_intermediate_wm) {
12654 if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
12655 pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
86c8bbbe
MR
12656 }
12657
6315b5d3 12658 if (INTEL_GEN(dev_priv) >= 9) {
e435d6e5
ML
12659 if (mode_changed)
12660 ret = skl_update_scaler_crtc(pipe_config);
12661
12662 if (!ret)
12663 ret = intel_atomic_setup_scalers(dev, intel_crtc,
12664 pipe_config);
12665 }
12666
12667 return ret;
6d3a1ce7
ML
12668}
12669
65b38e0d 12670static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160 12671 .mode_set_base_atomic = intel_pipe_set_base_atomic,
5a21b665
SV
12672 .atomic_begin = intel_begin_crtc_commit,
12673 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 12674 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
12675};
12676
d29b2f9d
ACO
12677static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
12678{
12679 struct intel_connector *connector;
12680
12681 for_each_intel_connector(dev, connector) {
8863dc7f
SV
12682 if (connector->base.state->crtc)
12683 drm_connector_unreference(&connector->base);
12684
d29b2f9d
ACO
12685 if (connector->base.encoder) {
12686 connector->base.state->best_encoder =
12687 connector->base.encoder;
12688 connector->base.state->crtc =
12689 connector->base.encoder->crtc;
8863dc7f
SV
12690
12691 drm_connector_reference(&connector->base);
d29b2f9d
ACO
12692 } else {
12693 connector->base.state->best_encoder = NULL;
12694 connector->base.state->crtc = NULL;
12695 }
12696 }
12697}
12698
050f7aeb 12699static void
eba905b2 12700connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 12701 struct intel_crtc_state *pipe_config)
050f7aeb 12702{
6a2a5c5d 12703 const struct drm_display_info *info = &connector->base.display_info;
050f7aeb
SV
12704 int bpp = pipe_config->pipe_bpp;
12705
12706 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
6a2a5c5d
VS
12707 connector->base.base.id,
12708 connector->base.name);
050f7aeb
SV
12709
12710 /* Don't use an invalid EDID bpc value */
6a2a5c5d 12711 if (info->bpc != 0 && info->bpc * 3 < bpp) {
050f7aeb 12712 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
6a2a5c5d
VS
12713 bpp, info->bpc * 3);
12714 pipe_config->pipe_bpp = info->bpc * 3;
050f7aeb
SV
12715 }
12716
196f954e 12717 /* Clamp bpp to 8 on screens without EDID 1.4 */
6a2a5c5d 12718 if (info->bpc == 0 && bpp > 24) {
196f954e
MK
12719 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
12720 bpp);
12721 pipe_config->pipe_bpp = 24;
050f7aeb
SV
12722 }
12723}
12724
4e53c2e0 12725static int
050f7aeb 12726compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 12727 struct intel_crtc_state *pipe_config)
4e53c2e0 12728{
9beb5fea 12729 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1486017f 12730 struct drm_atomic_state *state;
da3ced29
ACO
12731 struct drm_connector *connector;
12732 struct drm_connector_state *connector_state;
1486017f 12733 int bpp, i;
4e53c2e0 12734
9beb5fea
TU
12735 if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
12736 IS_CHERRYVIEW(dev_priv)))
4e53c2e0 12737 bpp = 10*3;
9beb5fea 12738 else if (INTEL_GEN(dev_priv) >= 5)
d328c9d7
SV
12739 bpp = 12*3;
12740 else
12741 bpp = 8*3;
12742
4e53c2e0 12743
4e53c2e0
SV
12744 pipe_config->pipe_bpp = bpp;
12745
1486017f
ACO
12746 state = pipe_config->base.state;
12747
4e53c2e0 12748 /* Clamp display bpp to EDID value */
da3ced29
ACO
12749 for_each_connector_in_state(state, connector, connector_state, i) {
12750 if (connector_state->crtc != &crtc->base)
4e53c2e0
SV
12751 continue;
12752
da3ced29
ACO
12753 connected_sink_compute_bpp(to_intel_connector(connector),
12754 pipe_config);
4e53c2e0
SV
12755 }
12756
12757 return bpp;
12758}
12759
644db711
SV
12760static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12761{
12762 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12763 "type: 0x%x flags: 0x%x\n",
1342830c 12764 mode->crtc_clock,
644db711
SV
12765 mode->crtc_hdisplay, mode->crtc_hsync_start,
12766 mode->crtc_hsync_end, mode->crtc_htotal,
12767 mode->crtc_vdisplay, mode->crtc_vsync_start,
12768 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12769}
12770
f6982332
TU
12771static inline void
12772intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
a4309657 12773 unsigned int lane_count, struct intel_link_m_n *m_n)
f6982332 12774{
a4309657
TU
12775 DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12776 id, lane_count,
f6982332
TU
12777 m_n->gmch_m, m_n->gmch_n,
12778 m_n->link_m, m_n->link_n, m_n->tu);
12779}
12780
c0b03411 12781static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 12782 struct intel_crtc_state *pipe_config,
c0b03411
SV
12783 const char *context)
12784{
6a60cd87 12785 struct drm_device *dev = crtc->base.dev;
4f8036a2 12786 struct drm_i915_private *dev_priv = to_i915(dev);
6a60cd87
CK
12787 struct drm_plane *plane;
12788 struct intel_plane *intel_plane;
12789 struct intel_plane_state *state;
12790 struct drm_framebuffer *fb;
12791
66766e4f
TU
12792 DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
12793 crtc->base.base.id, crtc->base.name, context);
c0b03411 12794
2c89429e
TU
12795 DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
12796 transcoder_name(pipe_config->cpu_transcoder),
c0b03411 12797 pipe_config->pipe_bpp, pipe_config->dither);
a4309657
TU
12798
12799 if (pipe_config->has_pch_encoder)
12800 intel_dump_m_n_config(pipe_config, "fdi",
12801 pipe_config->fdi_lanes,
12802 &pipe_config->fdi_m_n);
f6982332
TU
12803
12804 if (intel_crtc_has_dp_encoder(pipe_config)) {
a4309657
TU
12805 intel_dump_m_n_config(pipe_config, "dp m_n",
12806 pipe_config->lane_count, &pipe_config->dp_m_n);
d806e682
TU
12807 if (pipe_config->has_drrs)
12808 intel_dump_m_n_config(pipe_config, "dp m2_n2",
12809 pipe_config->lane_count,
12810 &pipe_config->dp_m2_n2);
f6982332 12811 }
b95af8be 12812
55072d19 12813 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
2c89429e 12814 pipe_config->has_audio, pipe_config->has_infoframe);
55072d19 12815
c0b03411 12816 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12817 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12818 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12819 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12820 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
2c89429e
TU
12821 DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
12822 pipe_config->port_clock,
37327abd 12823 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
dd2f616d
TU
12824
12825 if (INTEL_GEN(dev_priv) >= 9)
12826 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12827 crtc->num_scalers,
12828 pipe_config->scaler_state.scaler_users,
12829 pipe_config->scaler_state.scaler_id);
a74f8375
TU
12830
12831 if (HAS_GMCH_DISPLAY(dev_priv))
12832 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12833 pipe_config->gmch_pfit.control,
12834 pipe_config->gmch_pfit.pgm_ratios,
12835 pipe_config->gmch_pfit.lvds_border_bits);
12836 else
12837 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
12838 pipe_config->pch_pfit.pos,
12839 pipe_config->pch_pfit.size,
08c4d7fc 12840 enableddisabled(pipe_config->pch_pfit.enabled));
a74f8375 12841
2c89429e
TU
12842 DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
12843 pipe_config->ips_enabled, pipe_config->double_wide);
6a60cd87 12844
cc3f90f0 12845 if (IS_GEN9_LP(dev_priv)) {
c856052a 12846 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
415ff0f6 12847 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
c8453338 12848 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
415ff0f6 12849 pipe_config->dpll_hw_state.ebb0,
05712c15 12850 pipe_config->dpll_hw_state.ebb4,
415ff0f6
TU
12851 pipe_config->dpll_hw_state.pll0,
12852 pipe_config->dpll_hw_state.pll1,
12853 pipe_config->dpll_hw_state.pll2,
12854 pipe_config->dpll_hw_state.pll3,
12855 pipe_config->dpll_hw_state.pll6,
12856 pipe_config->dpll_hw_state.pll8,
05712c15 12857 pipe_config->dpll_hw_state.pll9,
c8453338 12858 pipe_config->dpll_hw_state.pll10,
415ff0f6 12859 pipe_config->dpll_hw_state.pcsdw12);
0853723b 12860 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
c856052a 12861 DRM_DEBUG_KMS("dpll_hw_state: "
415ff0f6 12862 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
415ff0f6
TU
12863 pipe_config->dpll_hw_state.ctrl1,
12864 pipe_config->dpll_hw_state.cfgcr1,
12865 pipe_config->dpll_hw_state.cfgcr2);
4f8036a2 12866 } else if (HAS_DDI(dev_priv)) {
c856052a 12867 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
00490c22
ML
12868 pipe_config->dpll_hw_state.wrpll,
12869 pipe_config->dpll_hw_state.spll);
415ff0f6
TU
12870 } else {
12871 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12872 "fp0: 0x%x, fp1: 0x%x\n",
12873 pipe_config->dpll_hw_state.dpll,
12874 pipe_config->dpll_hw_state.dpll_md,
12875 pipe_config->dpll_hw_state.fp0,
12876 pipe_config->dpll_hw_state.fp1);
12877 }
12878
6a60cd87
CK
12879 DRM_DEBUG_KMS("planes on this crtc\n");
12880 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
b3c11ac2 12881 struct drm_format_name_buf format_name;
6a60cd87
CK
12882 intel_plane = to_intel_plane(plane);
12883 if (intel_plane->pipe != crtc->pipe)
12884 continue;
12885
12886 state = to_intel_plane_state(plane->state);
12887 fb = state->base.fb;
12888 if (!fb) {
1d577e02
VS
12889 DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
12890 plane->base.id, plane->name, state->scaler_id);
6a60cd87
CK
12891 continue;
12892 }
12893
dd2f616d
TU
12894 DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
12895 plane->base.id, plane->name,
b3c11ac2
EE
12896 fb->base.id, fb->width, fb->height,
12897 drm_get_format_name(fb->pixel_format, &format_name));
dd2f616d
TU
12898 if (INTEL_GEN(dev_priv) >= 9)
12899 DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
12900 state->scaler_id,
12901 state->base.src.x1 >> 16,
12902 state->base.src.y1 >> 16,
12903 drm_rect_width(&state->base.src) >> 16,
12904 drm_rect_height(&state->base.src) >> 16,
12905 state->base.dst.x1, state->base.dst.y1,
12906 drm_rect_width(&state->base.dst),
12907 drm_rect_height(&state->base.dst));
6a60cd87 12908 }
c0b03411
SV
12909}
12910
5448a00d 12911static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12912{
5448a00d 12913 struct drm_device *dev = state->dev;
da3ced29 12914 struct drm_connector *connector;
00f0b378 12915 unsigned int used_ports = 0;
477321e0 12916 unsigned int used_mst_ports = 0;
00f0b378
VS
12917
12918 /*
12919 * Walk the connector list instead of the encoder
12920 * list to detect the problem on ddi platforms
12921 * where there's just one encoder per digital port.
12922 */
0bff4858
VS
12923 drm_for_each_connector(connector, dev) {
12924 struct drm_connector_state *connector_state;
12925 struct intel_encoder *encoder;
12926
12927 connector_state = drm_atomic_get_existing_connector_state(state, connector);
12928 if (!connector_state)
12929 connector_state = connector->state;
12930
5448a00d 12931 if (!connector_state->best_encoder)
00f0b378
VS
12932 continue;
12933
5448a00d
ACO
12934 encoder = to_intel_encoder(connector_state->best_encoder);
12935
12936 WARN_ON(!connector_state->crtc);
00f0b378
VS
12937
12938 switch (encoder->type) {
12939 unsigned int port_mask;
12940 case INTEL_OUTPUT_UNKNOWN:
4f8036a2 12941 if (WARN_ON(!HAS_DDI(to_i915(dev))))
00f0b378 12942 break;
cca0502b 12943 case INTEL_OUTPUT_DP:
00f0b378
VS
12944 case INTEL_OUTPUT_HDMI:
12945 case INTEL_OUTPUT_EDP:
12946 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12947
12948 /* the same port mustn't appear more than once */
12949 if (used_ports & port_mask)
12950 return false;
12951
12952 used_ports |= port_mask;
477321e0
VS
12953 break;
12954 case INTEL_OUTPUT_DP_MST:
12955 used_mst_ports |=
12956 1 << enc_to_mst(&encoder->base)->primary->port;
12957 break;
00f0b378
VS
12958 default:
12959 break;
12960 }
12961 }
12962
477321e0
VS
12963 /* can't mix MST and SST/HDMI on the same port */
12964 if (used_ports & used_mst_ports)
12965 return false;
12966
00f0b378
VS
12967 return true;
12968}
12969
83a57153
ACO
12970static void
12971clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12972{
12973 struct drm_crtc_state tmp_state;
663a3640 12974 struct intel_crtc_scaler_state scaler_state;
4978cc93 12975 struct intel_dpll_hw_state dpll_hw_state;
8106ddbd 12976 struct intel_shared_dpll *shared_dpll;
c4e2d043 12977 bool force_thru;
83a57153 12978
7546a384
ACO
12979 /* FIXME: before the switch to atomic started, a new pipe_config was
12980 * kzalloc'd. Code that depends on any field being zero should be
12981 * fixed, so that the crtc_state can be safely duplicated. For now,
12982 * only fields that are know to not cause problems are preserved. */
12983
83a57153 12984 tmp_state = crtc_state->base;
663a3640 12985 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12986 shared_dpll = crtc_state->shared_dpll;
12987 dpll_hw_state = crtc_state->dpll_hw_state;
c4e2d043 12988 force_thru = crtc_state->pch_pfit.force_thru;
4978cc93 12989
83a57153 12990 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12991
83a57153 12992 crtc_state->base = tmp_state;
663a3640 12993 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12994 crtc_state->shared_dpll = shared_dpll;
12995 crtc_state->dpll_hw_state = dpll_hw_state;
c4e2d043 12996 crtc_state->pch_pfit.force_thru = force_thru;
83a57153
ACO
12997}
12998
548ee15b 12999static int
b8cecdf5 13000intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 13001 struct intel_crtc_state *pipe_config)
ee7b9f93 13002{
b359283a 13003 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 13004 struct intel_encoder *encoder;
da3ced29 13005 struct drm_connector *connector;
0b901879 13006 struct drm_connector_state *connector_state;
d328c9d7 13007 int base_bpp, ret = -EINVAL;
0b901879 13008 int i;
e29c22c0 13009 bool retry = true;
ee7b9f93 13010
83a57153 13011 clear_intel_crtc_state(pipe_config);
7758a113 13012
e143a21c
SV
13013 pipe_config->cpu_transcoder =
13014 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 13015
2960bc9c
ID
13016 /*
13017 * Sanitize sync polarity flags based on requested ones. If neither
13018 * positive or negative polarity is requested, treat this as meaning
13019 * negative polarity.
13020 */
2d112de7 13021 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13022 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 13023 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 13024
2d112de7 13025 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 13026 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 13027 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 13028
d328c9d7
SV
13029 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
13030 pipe_config);
13031 if (base_bpp < 0)
4e53c2e0
SV
13032 goto fail;
13033
e41a56be
VS
13034 /*
13035 * Determine the real pipe dimensions. Note that stereo modes can
13036 * increase the actual pipe size due to the frame doubling and
13037 * insertion of additional space for blanks between the frame. This
13038 * is stored in the crtc timings. We use the requested mode to do this
13039 * computation to clearly distinguish it from the adjusted mode, which
13040 * can be changed by the connectors in the below retry loop.
13041 */
2d112de7 13042 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
13043 &pipe_config->pipe_src_w,
13044 &pipe_config->pipe_src_h);
e41a56be 13045
253c84c8
VS
13046 for_each_connector_in_state(state, connector, connector_state, i) {
13047 if (connector_state->crtc != crtc)
13048 continue;
13049
13050 encoder = to_intel_encoder(connector_state->best_encoder);
13051
e25148d0
VS
13052 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
13053 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
13054 goto fail;
13055 }
13056
253c84c8
VS
13057 /*
13058 * Determine output_types before calling the .compute_config()
13059 * hooks so that the hooks can use this information safely.
13060 */
13061 pipe_config->output_types |= 1 << encoder->type;
13062 }
13063
e29c22c0 13064encoder_retry:
ef1b460d 13065 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 13066 pipe_config->port_clock = 0;
ef1b460d 13067 pipe_config->pixel_multiplier = 1;
ff9a6750 13068
135c81b8 13069 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
13070 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
13071 CRTC_STEREO_DOUBLE);
135c81b8 13072
7758a113
SV
13073 /* Pass our mode to the connectors and the CRTC to give them a chance to
13074 * adjust it according to limitations or connector properties, and also
13075 * a chance to reject the mode entirely.
47f1c6c9 13076 */
da3ced29 13077 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 13078 if (connector_state->crtc != crtc)
7758a113 13079 continue;
7ae89233 13080
0b901879
ACO
13081 encoder = to_intel_encoder(connector_state->best_encoder);
13082
0a478c27 13083 if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
efea6e8e 13084 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
SV
13085 goto fail;
13086 }
ee7b9f93 13087 }
47f1c6c9 13088
ff9a6750
SV
13089 /* Set default port clock if not overwritten by the encoder. Needs to be
13090 * done afterwards in case the encoder adjusts the mode. */
13091 if (!pipe_config->port_clock)
2d112de7 13092 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 13093 * pipe_config->pixel_multiplier;
ff9a6750 13094
a43f6e0f 13095 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 13096 if (ret < 0) {
7758a113
SV
13097 DRM_DEBUG_KMS("CRTC fixup failed\n");
13098 goto fail;
ee7b9f93 13099 }
e29c22c0
SV
13100
13101 if (ret == RETRY) {
13102 if (WARN(!retry, "loop in pipe configuration computation\n")) {
13103 ret = -EINVAL;
13104 goto fail;
13105 }
13106
13107 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
13108 retry = false;
13109 goto encoder_retry;
13110 }
13111
e8fa4270
SV
13112 /* Dithering seems to not pass-through bits correctly when it should, so
13113 * only enable it on 6bpc panels. */
13114 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
62f0ace5 13115 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 13116 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 13117
7758a113 13118fail:
548ee15b 13119 return ret;
ee7b9f93 13120}
47f1c6c9 13121
ea9d758d 13122static void
4740b0f2 13123intel_modeset_update_crtc_state(struct drm_atomic_state *state)
ea9d758d 13124{
0a9ab303
ACO
13125 struct drm_crtc *crtc;
13126 struct drm_crtc_state *crtc_state;
8a75d157 13127 int i;
ea9d758d 13128
7668851f 13129 /* Double check state. */
8a75d157 13130 for_each_crtc_in_state(state, crtc, crtc_state, i) {
3cb480bc 13131 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
13132
13133 /* Update hwmode for vblank functions */
13134 if (crtc->state->active)
13135 crtc->hwmode = crtc->state->adjusted_mode;
13136 else
13137 crtc->hwmode.crtc_clock = 0;
61067a5e
ML
13138
13139 /*
13140 * Update legacy state to satisfy fbc code. This can
13141 * be removed when fbc uses the atomic state.
13142 */
13143 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
13144 struct drm_plane_state *plane_state = crtc->primary->state;
13145
13146 crtc->primary->fb = plane_state->fb;
13147 crtc->x = plane_state->src_x >> 16;
13148 crtc->y = plane_state->src_y >> 16;
13149 }
ea9d758d 13150 }
ea9d758d
SV
13151}
13152
3bd26263 13153static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 13154{
3bd26263 13155 int diff;
f1f644dc
JB
13156
13157 if (clock1 == clock2)
13158 return true;
13159
13160 if (!clock1 || !clock2)
13161 return false;
13162
13163 diff = abs(clock1 - clock2);
13164
13165 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
13166 return true;
13167
13168 return false;
13169}
13170
cfb23ed6
ML
13171static bool
13172intel_compare_m_n(unsigned int m, unsigned int n,
13173 unsigned int m2, unsigned int n2,
13174 bool exact)
13175{
13176 if (m == m2 && n == n2)
13177 return true;
13178
13179 if (exact || !m || !n || !m2 || !n2)
13180 return false;
13181
13182 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
13183
31d10b57
ML
13184 if (n > n2) {
13185 while (n > n2) {
cfb23ed6
ML
13186 m2 <<= 1;
13187 n2 <<= 1;
13188 }
31d10b57
ML
13189 } else if (n < n2) {
13190 while (n < n2) {
cfb23ed6
ML
13191 m <<= 1;
13192 n <<= 1;
13193 }
13194 }
13195
31d10b57
ML
13196 if (n != n2)
13197 return false;
13198
13199 return intel_fuzzy_clock_check(m, m2);
cfb23ed6
ML
13200}
13201
13202static bool
13203intel_compare_link_m_n(const struct intel_link_m_n *m_n,
13204 struct intel_link_m_n *m2_n2,
13205 bool adjust)
13206{
13207 if (m_n->tu == m2_n2->tu &&
13208 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
13209 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
13210 intel_compare_m_n(m_n->link_m, m_n->link_n,
13211 m2_n2->link_m, m2_n2->link_n, !adjust)) {
13212 if (adjust)
13213 *m2_n2 = *m_n;
13214
13215 return true;
13216 }
13217
13218 return false;
13219}
13220
0e8ffe1b 13221static bool
6315b5d3 13222intel_pipe_config_compare(struct drm_i915_private *dev_priv,
5cec258b 13223 struct intel_crtc_state *current_config,
cfb23ed6
ML
13224 struct intel_crtc_state *pipe_config,
13225 bool adjust)
0e8ffe1b 13226{
cfb23ed6
ML
13227 bool ret = true;
13228
13229#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
13230 do { \
13231 if (!adjust) \
13232 DRM_ERROR(fmt, ##__VA_ARGS__); \
13233 else \
13234 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
13235 } while (0)
13236
66e985c0
SV
13237#define PIPE_CONF_CHECK_X(name) \
13238 if (current_config->name != pipe_config->name) { \
cfb23ed6 13239 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
66e985c0
SV
13240 "(expected 0x%08x, found 0x%08x)\n", \
13241 current_config->name, \
13242 pipe_config->name); \
cfb23ed6 13243 ret = false; \
66e985c0
SV
13244 }
13245
08a24034
SV
13246#define PIPE_CONF_CHECK_I(name) \
13247 if (current_config->name != pipe_config->name) { \
cfb23ed6 13248 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
08a24034
SV
13249 "(expected %i, found %i)\n", \
13250 current_config->name, \
13251 pipe_config->name); \
cfb23ed6
ML
13252 ret = false; \
13253 }
13254
8106ddbd
ACO
13255#define PIPE_CONF_CHECK_P(name) \
13256 if (current_config->name != pipe_config->name) { \
13257 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13258 "(expected %p, found %p)\n", \
13259 current_config->name, \
13260 pipe_config->name); \
13261 ret = false; \
13262 }
13263
cfb23ed6
ML
13264#define PIPE_CONF_CHECK_M_N(name) \
13265 if (!intel_compare_link_m_n(&current_config->name, \
13266 &pipe_config->name,\
13267 adjust)) { \
13268 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13269 "(expected tu %i gmch %i/%i link %i/%i, " \
13270 "found tu %i, gmch %i/%i link %i/%i)\n", \
13271 current_config->name.tu, \
13272 current_config->name.gmch_m, \
13273 current_config->name.gmch_n, \
13274 current_config->name.link_m, \
13275 current_config->name.link_n, \
13276 pipe_config->name.tu, \
13277 pipe_config->name.gmch_m, \
13278 pipe_config->name.gmch_n, \
13279 pipe_config->name.link_m, \
13280 pipe_config->name.link_n); \
13281 ret = false; \
13282 }
13283
55c561a7
SV
13284/* This is required for BDW+ where there is only one set of registers for
13285 * switching between high and low RR.
13286 * This macro can be used whenever a comparison has to be made between one
13287 * hw state and multiple sw state variables.
13288 */
cfb23ed6
ML
13289#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
13290 if (!intel_compare_link_m_n(&current_config->name, \
13291 &pipe_config->name, adjust) && \
13292 !intel_compare_link_m_n(&current_config->alt_name, \
13293 &pipe_config->name, adjust)) { \
13294 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
13295 "(expected tu %i gmch %i/%i link %i/%i, " \
13296 "or tu %i gmch %i/%i link %i/%i, " \
13297 "found tu %i, gmch %i/%i link %i/%i)\n", \
13298 current_config->name.tu, \
13299 current_config->name.gmch_m, \
13300 current_config->name.gmch_n, \
13301 current_config->name.link_m, \
13302 current_config->name.link_n, \
13303 current_config->alt_name.tu, \
13304 current_config->alt_name.gmch_m, \
13305 current_config->alt_name.gmch_n, \
13306 current_config->alt_name.link_m, \
13307 current_config->alt_name.link_n, \
13308 pipe_config->name.tu, \
13309 pipe_config->name.gmch_m, \
13310 pipe_config->name.gmch_n, \
13311 pipe_config->name.link_m, \
13312 pipe_config->name.link_n); \
13313 ret = false; \
88adfff1
SV
13314 }
13315
1bd1bd80
SV
13316#define PIPE_CONF_CHECK_FLAGS(name, mask) \
13317 if ((current_config->name ^ pipe_config->name) & (mask)) { \
cfb23ed6 13318 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
1bd1bd80
SV
13319 "(expected %i, found %i)\n", \
13320 current_config->name & (mask), \
13321 pipe_config->name & (mask)); \
cfb23ed6 13322 ret = false; \
1bd1bd80
SV
13323 }
13324
5e550656
VS
13325#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
13326 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
cfb23ed6 13327 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
5e550656
VS
13328 "(expected %i, found %i)\n", \
13329 current_config->name, \
13330 pipe_config->name); \
cfb23ed6 13331 ret = false; \
5e550656
VS
13332 }
13333
bb760063
SV
13334#define PIPE_CONF_QUIRK(quirk) \
13335 ((current_config->quirks | pipe_config->quirks) & (quirk))
13336
eccb140b
SV
13337 PIPE_CONF_CHECK_I(cpu_transcoder);
13338
08a24034
SV
13339 PIPE_CONF_CHECK_I(has_pch_encoder);
13340 PIPE_CONF_CHECK_I(fdi_lanes);
cfb23ed6 13341 PIPE_CONF_CHECK_M_N(fdi_m_n);
08a24034 13342
90a6b7b0 13343 PIPE_CONF_CHECK_I(lane_count);
95a7a2ae 13344 PIPE_CONF_CHECK_X(lane_lat_optim_mask);
b95af8be 13345
6315b5d3 13346 if (INTEL_GEN(dev_priv) < 8) {
cfb23ed6
ML
13347 PIPE_CONF_CHECK_M_N(dp_m_n);
13348
cfb23ed6
ML
13349 if (current_config->has_drrs)
13350 PIPE_CONF_CHECK_M_N(dp_m2_n2);
13351 } else
13352 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
eb14cb74 13353
253c84c8 13354 PIPE_CONF_CHECK_X(output_types);
a65347ba 13355
2d112de7
ACO
13356 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
13357 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
13358 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
13359 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
13360 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
13361 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 13362
2d112de7
ACO
13363 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
13364 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
13365 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
13366 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
13367 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
13368 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 13369
c93f54cf 13370 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 13371 PIPE_CONF_CHECK_I(has_hdmi_sink);
772c2a51 13372 if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
920a14b2 13373 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
b5a9fa09 13374 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 13375 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 13376
9ed109a7
SV
13377 PIPE_CONF_CHECK_I(has_audio);
13378
2d112de7 13379 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
SV
13380 DRM_MODE_FLAG_INTERLACE);
13381
bb760063 13382 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 13383 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13384 DRM_MODE_FLAG_PHSYNC);
2d112de7 13385 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13386 DRM_MODE_FLAG_NHSYNC);
2d112de7 13387 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 13388 DRM_MODE_FLAG_PVSYNC);
2d112de7 13389 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
SV
13390 DRM_MODE_FLAG_NVSYNC);
13391 }
045ac3b5 13392
333b8ca8 13393 PIPE_CONF_CHECK_X(gmch_pfit.control);
e2ff2d4a 13394 /* pfit ratios are autocomputed by the hw on gen4+ */
6315b5d3 13395 if (INTEL_GEN(dev_priv) < 4)
7f7d8dd6 13396 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
333b8ca8 13397 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9953599b 13398
bfd16b2a
ML
13399 if (!adjust) {
13400 PIPE_CONF_CHECK_I(pipe_src_w);
13401 PIPE_CONF_CHECK_I(pipe_src_h);
13402
13403 PIPE_CONF_CHECK_I(pch_pfit.enabled);
13404 if (current_config->pch_pfit.enabled) {
13405 PIPE_CONF_CHECK_X(pch_pfit.pos);
13406 PIPE_CONF_CHECK_X(pch_pfit.size);
13407 }
2fa2fe9a 13408
7aefe2b5
ML
13409 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
13410 }
a1b2278e 13411
e59150dc 13412 /* BDW+ don't expose a synchronous way to read the state */
772c2a51 13413 if (IS_HASWELL(dev_priv))
e59150dc 13414 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 13415
282740f7
VS
13416 PIPE_CONF_CHECK_I(double_wide);
13417
8106ddbd 13418 PIPE_CONF_CHECK_P(shared_dpll);
66e985c0 13419 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 13420 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
SV
13421 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
13422 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 13423 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
00490c22 13424 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
3f4cd19f
DL
13425 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
13426 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
13427 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 13428
47eacbab
VS
13429 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
13430 PIPE_CONF_CHECK_X(dsi_pll.div);
13431
9beb5fea 13432 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
42571aef
VS
13433 PIPE_CONF_CHECK_I(pipe_bpp);
13434
2d112de7 13435 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 13436 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 13437
66e985c0 13438#undef PIPE_CONF_CHECK_X
08a24034 13439#undef PIPE_CONF_CHECK_I
8106ddbd 13440#undef PIPE_CONF_CHECK_P
1bd1bd80 13441#undef PIPE_CONF_CHECK_FLAGS
5e550656 13442#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 13443#undef PIPE_CONF_QUIRK
cfb23ed6 13444#undef INTEL_ERR_OR_DBG_KMS
88adfff1 13445
cfb23ed6 13446 return ret;
0e8ffe1b
SV
13447}
13448
e3b247da
VS
13449static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
13450 const struct intel_crtc_state *pipe_config)
13451{
13452 if (pipe_config->has_pch_encoder) {
21a727b3 13453 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
e3b247da
VS
13454 &pipe_config->fdi_m_n);
13455 int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
13456
13457 /*
13458 * FDI already provided one idea for the dotclock.
13459 * Yell if the encoder disagrees.
13460 */
13461 WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
13462 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
13463 fdi_dotclock, dotclock);
13464 }
13465}
13466
c0ead703
ML
13467static void verify_wm_state(struct drm_crtc *crtc,
13468 struct drm_crtc_state *new_state)
08db6652 13469{
6315b5d3 13470 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
08db6652 13471 struct skl_ddb_allocation hw_ddb, *sw_ddb;
3de8a14c 13472 struct skl_pipe_wm hw_wm, *sw_wm;
13473 struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
13474 struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
e7c84544
ML
13475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13476 const enum pipe pipe = intel_crtc->pipe;
3de8a14c 13477 int plane, level, max_level = ilk_wm_max_level(dev_priv);
08db6652 13478
6315b5d3 13479 if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
08db6652
DL
13480 return;
13481
3de8a14c 13482 skl_pipe_wm_get_hw_state(crtc, &hw_wm);
03af79e0 13483 sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
3de8a14c 13484
08db6652
DL
13485 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
13486 sw_ddb = &dev_priv->wm.skl_hw.ddb;
13487
e7c84544 13488 /* planes */
8b364b41 13489 for_each_universal_plane(dev_priv, pipe, plane) {
3de8a14c 13490 hw_plane_wm = &hw_wm.planes[plane];
13491 sw_plane_wm = &sw_wm->planes[plane];
08db6652 13492
3de8a14c 13493 /* Watermarks */
13494 for (level = 0; level <= max_level; level++) {
13495 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13496 &sw_plane_wm->wm[level]))
13497 continue;
13498
13499 DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13500 pipe_name(pipe), plane + 1, level,
13501 sw_plane_wm->wm[level].plane_en,
13502 sw_plane_wm->wm[level].plane_res_b,
13503 sw_plane_wm->wm[level].plane_res_l,
13504 hw_plane_wm->wm[level].plane_en,
13505 hw_plane_wm->wm[level].plane_res_b,
13506 hw_plane_wm->wm[level].plane_res_l);
13507 }
08db6652 13508
3de8a14c 13509 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13510 &sw_plane_wm->trans_wm)) {
13511 DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13512 pipe_name(pipe), plane + 1,
13513 sw_plane_wm->trans_wm.plane_en,
13514 sw_plane_wm->trans_wm.plane_res_b,
13515 sw_plane_wm->trans_wm.plane_res_l,
13516 hw_plane_wm->trans_wm.plane_en,
13517 hw_plane_wm->trans_wm.plane_res_b,
13518 hw_plane_wm->trans_wm.plane_res_l);
13519 }
13520
13521 /* DDB */
13522 hw_ddb_entry = &hw_ddb.plane[pipe][plane];
13523 sw_ddb_entry = &sw_ddb->plane[pipe][plane];
13524
13525 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13526 DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
3de8a14c 13527 pipe_name(pipe), plane + 1,
13528 sw_ddb_entry->start, sw_ddb_entry->end,
13529 hw_ddb_entry->start, hw_ddb_entry->end);
13530 }
e7c84544 13531 }
08db6652 13532
27082493
L
13533 /*
13534 * cursor
13535 * If the cursor plane isn't active, we may not have updated it's ddb
13536 * allocation. In that case since the ddb allocation will be updated
13537 * once the plane becomes visible, we can skip this check
13538 */
13539 if (intel_crtc->cursor_addr) {
3de8a14c 13540 hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
13541 sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
13542
13543 /* Watermarks */
13544 for (level = 0; level <= max_level; level++) {
13545 if (skl_wm_level_equals(&hw_plane_wm->wm[level],
13546 &sw_plane_wm->wm[level]))
13547 continue;
13548
13549 DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13550 pipe_name(pipe), level,
13551 sw_plane_wm->wm[level].plane_en,
13552 sw_plane_wm->wm[level].plane_res_b,
13553 sw_plane_wm->wm[level].plane_res_l,
13554 hw_plane_wm->wm[level].plane_en,
13555 hw_plane_wm->wm[level].plane_res_b,
13556 hw_plane_wm->wm[level].plane_res_l);
13557 }
13558
13559 if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
13560 &sw_plane_wm->trans_wm)) {
13561 DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
13562 pipe_name(pipe),
13563 sw_plane_wm->trans_wm.plane_en,
13564 sw_plane_wm->trans_wm.plane_res_b,
13565 sw_plane_wm->trans_wm.plane_res_l,
13566 hw_plane_wm->trans_wm.plane_en,
13567 hw_plane_wm->trans_wm.plane_res_b,
13568 hw_plane_wm->trans_wm.plane_res_l);
13569 }
13570
13571 /* DDB */
13572 hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
13573 sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
27082493 13574
3de8a14c 13575 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
faccd994 13576 DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
27082493 13577 pipe_name(pipe),
3de8a14c 13578 sw_ddb_entry->start, sw_ddb_entry->end,
13579 hw_ddb_entry->start, hw_ddb_entry->end);
27082493 13580 }
08db6652
DL
13581 }
13582}
13583
91d1b4bd 13584static void
677100ce
ML
13585verify_connector_state(struct drm_device *dev,
13586 struct drm_atomic_state *state,
13587 struct drm_crtc *crtc)
8af6cf88 13588{
35dd3c64 13589 struct drm_connector *connector;
677100ce
ML
13590 struct drm_connector_state *old_conn_state;
13591 int i;
8af6cf88 13592
677100ce 13593 for_each_connector_in_state(state, connector, old_conn_state, i) {
35dd3c64
ML
13594 struct drm_encoder *encoder = connector->encoder;
13595 struct drm_connector_state *state = connector->state;
ad3c558f 13596
e7c84544
ML
13597 if (state->crtc != crtc)
13598 continue;
13599
5a21b665 13600 intel_connector_verify_state(to_intel_connector(connector));
8af6cf88 13601
ad3c558f 13602 I915_STATE_WARN(state->best_encoder != encoder,
35dd3c64 13603 "connector's atomic encoder doesn't match legacy encoder\n");
8af6cf88 13604 }
91d1b4bd
SV
13605}
13606
13607static void
c0ead703 13608verify_encoder_state(struct drm_device *dev)
91d1b4bd
SV
13609{
13610 struct intel_encoder *encoder;
13611 struct intel_connector *connector;
8af6cf88 13612
b2784e15 13613 for_each_intel_encoder(dev, encoder) {
8af6cf88 13614 bool enabled = false;
4d20cd86 13615 enum pipe pipe;
8af6cf88
SV
13616
13617 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
13618 encoder->base.base.id,
8e329a03 13619 encoder->base.name);
8af6cf88 13620
3a3371ff 13621 for_each_intel_connector(dev, connector) {
4d20cd86 13622 if (connector->base.state->best_encoder != &encoder->base)
8af6cf88
SV
13623 continue;
13624 enabled = true;
ad3c558f
ML
13625
13626 I915_STATE_WARN(connector->base.state->crtc !=
13627 encoder->base.crtc,
13628 "connector's crtc doesn't match encoder crtc\n");
8af6cf88 13629 }
0e32b39c 13630
e2c719b7 13631 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
SV
13632 "encoder's enabled state mismatch "
13633 "(expected %i, found %i)\n",
13634 !!encoder->base.crtc, enabled);
7c60d198
ML
13635
13636 if (!encoder->base.crtc) {
4d20cd86 13637 bool active;
7c60d198 13638
4d20cd86
ML
13639 active = encoder->get_hw_state(encoder, &pipe);
13640 I915_STATE_WARN(active,
13641 "encoder detached but still enabled on pipe %c.\n",
13642 pipe_name(pipe));
7c60d198 13643 }
8af6cf88 13644 }
91d1b4bd
SV
13645}
13646
13647static void
c0ead703
ML
13648verify_crtc_state(struct drm_crtc *crtc,
13649 struct drm_crtc_state *old_crtc_state,
13650 struct drm_crtc_state *new_crtc_state)
91d1b4bd 13651{
e7c84544 13652 struct drm_device *dev = crtc->dev;
fac5e23e 13653 struct drm_i915_private *dev_priv = to_i915(dev);
91d1b4bd 13654 struct intel_encoder *encoder;
e7c84544
ML
13655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13656 struct intel_crtc_state *pipe_config, *sw_config;
13657 struct drm_atomic_state *old_state;
13658 bool active;
045ac3b5 13659
e7c84544 13660 old_state = old_crtc_state->state;
ec2dc6a0 13661 __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
e7c84544
ML
13662 pipe_config = to_intel_crtc_state(old_crtc_state);
13663 memset(pipe_config, 0, sizeof(*pipe_config));
13664 pipe_config->base.crtc = crtc;
13665 pipe_config->base.state = old_state;
8af6cf88 13666
78108b7c 13667 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
8af6cf88 13668
e7c84544 13669 active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
d62cf62a 13670
e7c84544
ML
13671 /* hw state is inconsistent with the pipe quirk */
13672 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
13673 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
13674 active = new_crtc_state->active;
6c49f241 13675
e7c84544
ML
13676 I915_STATE_WARN(new_crtc_state->active != active,
13677 "crtc active state doesn't match with hw state "
13678 "(expected %i, found %i)\n", new_crtc_state->active, active);
0e8ffe1b 13679
e7c84544
ML
13680 I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
13681 "transitional active state does not match atomic hw state "
13682 "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
4d20cd86 13683
e7c84544
ML
13684 for_each_encoder_on_crtc(dev, crtc, encoder) {
13685 enum pipe pipe;
4d20cd86 13686
e7c84544
ML
13687 active = encoder->get_hw_state(encoder, &pipe);
13688 I915_STATE_WARN(active != new_crtc_state->active,
13689 "[ENCODER:%i] active %i with crtc active %i\n",
13690 encoder->base.base.id, active, new_crtc_state->active);
4d20cd86 13691
e7c84544
ML
13692 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
13693 "Encoder connected to wrong pipe %c\n",
13694 pipe_name(pipe));
4d20cd86 13695
253c84c8
VS
13696 if (active) {
13697 pipe_config->output_types |= 1 << encoder->type;
e7c84544 13698 encoder->get_config(encoder, pipe_config);
253c84c8 13699 }
e7c84544 13700 }
53d9f4e9 13701
e7c84544
ML
13702 if (!new_crtc_state->active)
13703 return;
cfb23ed6 13704
e7c84544 13705 intel_pipe_config_sanity_check(dev_priv, pipe_config);
e3b247da 13706
e7c84544 13707 sw_config = to_intel_crtc_state(crtc->state);
6315b5d3 13708 if (!intel_pipe_config_compare(dev_priv, sw_config,
e7c84544
ML
13709 pipe_config, false)) {
13710 I915_STATE_WARN(1, "pipe state doesn't match!\n");
13711 intel_dump_pipe_config(intel_crtc, pipe_config,
13712 "[hw state]");
13713 intel_dump_pipe_config(intel_crtc, sw_config,
13714 "[sw state]");
8af6cf88
SV
13715 }
13716}
13717
91d1b4bd 13718static void
c0ead703
ML
13719verify_single_dpll_state(struct drm_i915_private *dev_priv,
13720 struct intel_shared_dpll *pll,
13721 struct drm_crtc *crtc,
13722 struct drm_crtc_state *new_state)
91d1b4bd 13723{
91d1b4bd 13724 struct intel_dpll_hw_state dpll_hw_state;
e7c84544
ML
13725 unsigned crtc_mask;
13726 bool active;
5358901f 13727
e7c84544 13728 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
5358901f 13729
e7c84544 13730 DRM_DEBUG_KMS("%s\n", pll->name);
5358901f 13731
e7c84544 13732 active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
5358901f 13733
e7c84544
ML
13734 if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
13735 I915_STATE_WARN(!pll->on && pll->active_mask,
13736 "pll in active use but not on in sw tracking\n");
13737 I915_STATE_WARN(pll->on && !pll->active_mask,
13738 "pll is on but not used by any active crtc\n");
13739 I915_STATE_WARN(pll->on != active,
13740 "pll on state mismatch (expected %i, found %i)\n",
13741 pll->on, active);
13742 }
5358901f 13743
e7c84544 13744 if (!crtc) {
2dd66ebd 13745 I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
e7c84544
ML
13746 "more active pll users than references: %x vs %x\n",
13747 pll->active_mask, pll->config.crtc_mask);
5358901f 13748
e7c84544
ML
13749 return;
13750 }
13751
13752 crtc_mask = 1 << drm_crtc_index(crtc);
13753
13754 if (new_state->active)
13755 I915_STATE_WARN(!(pll->active_mask & crtc_mask),
13756 "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
13757 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
13758 else
13759 I915_STATE_WARN(pll->active_mask & crtc_mask,
13760 "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
13761 pipe_name(drm_crtc_index(crtc)), pll->active_mask);
2dd66ebd 13762
e7c84544
ML
13763 I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
13764 "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
13765 crtc_mask, pll->config.crtc_mask);
66e985c0 13766
e7c84544
ML
13767 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
13768 &dpll_hw_state,
13769 sizeof(dpll_hw_state)),
13770 "pll hw state mismatch\n");
13771}
13772
13773static void
c0ead703
ML
13774verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
13775 struct drm_crtc_state *old_crtc_state,
13776 struct drm_crtc_state *new_crtc_state)
e7c84544 13777{
fac5e23e 13778 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13779 struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
13780 struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
13781
13782 if (new_state->shared_dpll)
c0ead703 13783 verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
e7c84544
ML
13784
13785 if (old_state->shared_dpll &&
13786 old_state->shared_dpll != new_state->shared_dpll) {
13787 unsigned crtc_mask = 1 << drm_crtc_index(crtc);
13788 struct intel_shared_dpll *pll = old_state->shared_dpll;
13789
13790 I915_STATE_WARN(pll->active_mask & crtc_mask,
13791 "pll active mismatch (didn't expect pipe %c in active mask)\n",
13792 pipe_name(drm_crtc_index(crtc)));
13793 I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
13794 "pll enabled crtcs mismatch (found %x in enabled mask)\n",
13795 pipe_name(drm_crtc_index(crtc)));
5358901f 13796 }
8af6cf88
SV
13797}
13798
e7c84544 13799static void
c0ead703 13800intel_modeset_verify_crtc(struct drm_crtc *crtc,
677100ce
ML
13801 struct drm_atomic_state *state,
13802 struct drm_crtc_state *old_state,
13803 struct drm_crtc_state *new_state)
e7c84544 13804{
5a21b665
SV
13805 if (!needs_modeset(new_state) &&
13806 !to_intel_crtc_state(new_state)->update_pipe)
13807 return;
13808
c0ead703 13809 verify_wm_state(crtc, new_state);
677100ce 13810 verify_connector_state(crtc->dev, state, crtc);
c0ead703
ML
13811 verify_crtc_state(crtc, old_state, new_state);
13812 verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
e7c84544
ML
13813}
13814
13815static void
c0ead703 13816verify_disabled_dpll_state(struct drm_device *dev)
e7c84544 13817{
fac5e23e 13818 struct drm_i915_private *dev_priv = to_i915(dev);
e7c84544
ML
13819 int i;
13820
13821 for (i = 0; i < dev_priv->num_shared_dpll; i++)
c0ead703 13822 verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
e7c84544
ML
13823}
13824
13825static void
677100ce
ML
13826intel_modeset_verify_disabled(struct drm_device *dev,
13827 struct drm_atomic_state *state)
e7c84544 13828{
c0ead703 13829 verify_encoder_state(dev);
677100ce 13830 verify_connector_state(dev, state, NULL);
c0ead703 13831 verify_disabled_dpll_state(dev);
e7c84544
ML
13832}
13833
80715b2f
VS
13834static void update_scanline_offset(struct intel_crtc *crtc)
13835{
4f8036a2 13836 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
80715b2f
VS
13837
13838 /*
13839 * The scanline counter increments at the leading edge of hsync.
13840 *
13841 * On most platforms it starts counting from vtotal-1 on the
13842 * first active line. That means the scanline counter value is
13843 * always one less than what we would expect. Ie. just after
13844 * start of vblank, which also occurs at start of hsync (on the
13845 * last active line), the scanline counter will read vblank_start-1.
13846 *
13847 * On gen2 the scanline counter starts counting from 1 instead
13848 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
13849 * to keep the value positive), instead of adding one.
13850 *
13851 * On HSW+ the behaviour of the scanline counter depends on the output
13852 * type. For DP ports it behaves like most other platforms, but on HDMI
13853 * there's an extra 1 line difference. So we need to add two instead of
13854 * one to the value.
13855 */
4f8036a2 13856 if (IS_GEN2(dev_priv)) {
124abe07 13857 const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
13858 int vtotal;
13859
124abe07
VS
13860 vtotal = adjusted_mode->crtc_vtotal;
13861 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
80715b2f
VS
13862 vtotal /= 2;
13863
13864 crtc->scanline_offset = vtotal - 1;
4f8036a2 13865 } else if (HAS_DDI(dev_priv) &&
2d84d2b3 13866 intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
13867 crtc->scanline_offset = 2;
13868 } else
13869 crtc->scanline_offset = 1;
13870}
13871
ad421372 13872static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 13873{
225da59b 13874 struct drm_device *dev = state->dev;
ed6739ef 13875 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 13876 struct intel_shared_dpll_config *shared_dpll = NULL;
0a9ab303
ACO
13877 struct drm_crtc *crtc;
13878 struct drm_crtc_state *crtc_state;
0a9ab303 13879 int i;
ed6739ef
ACO
13880
13881 if (!dev_priv->display.crtc_compute_clock)
ad421372 13882 return;
ed6739ef 13883
0a9ab303 13884 for_each_crtc_in_state(state, crtc, crtc_state, i) {
fb1a38a9 13885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8106ddbd
ACO
13886 struct intel_shared_dpll *old_dpll =
13887 to_intel_crtc_state(crtc->state)->shared_dpll;
0a9ab303 13888
fb1a38a9 13889 if (!needs_modeset(crtc_state))
225da59b
ACO
13890 continue;
13891
8106ddbd 13892 to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
fb1a38a9 13893
8106ddbd 13894 if (!old_dpll)
fb1a38a9 13895 continue;
0a9ab303 13896
ad421372
ML
13897 if (!shared_dpll)
13898 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 13899
8106ddbd 13900 intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
ad421372 13901 }
ed6739ef
ACO
13902}
13903
99d736a2
ML
13904/*
13905 * This implements the workaround described in the "notes" section of the mode
13906 * set sequence documentation. When going from no pipes or single pipe to
13907 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13908 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13909 */
13910static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13911{
13912 struct drm_crtc_state *crtc_state;
13913 struct intel_crtc *intel_crtc;
13914 struct drm_crtc *crtc;
13915 struct intel_crtc_state *first_crtc_state = NULL;
13916 struct intel_crtc_state *other_crtc_state = NULL;
13917 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13918 int i;
13919
13920 /* look at all crtc's that are going to be enabled in during modeset */
13921 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13922 intel_crtc = to_intel_crtc(crtc);
13923
13924 if (!crtc_state->active || !needs_modeset(crtc_state))
13925 continue;
13926
13927 if (first_crtc_state) {
13928 other_crtc_state = to_intel_crtc_state(crtc_state);
13929 break;
13930 } else {
13931 first_crtc_state = to_intel_crtc_state(crtc_state);
13932 first_pipe = intel_crtc->pipe;
13933 }
13934 }
13935
13936 /* No workaround needed? */
13937 if (!first_crtc_state)
13938 return 0;
13939
13940 /* w/a possibly needed, check how many crtc's are already enabled. */
13941 for_each_intel_crtc(state->dev, intel_crtc) {
13942 struct intel_crtc_state *pipe_config;
13943
13944 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13945 if (IS_ERR(pipe_config))
13946 return PTR_ERR(pipe_config);
13947
13948 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13949
13950 if (!pipe_config->base.active ||
13951 needs_modeset(&pipe_config->base))
13952 continue;
13953
13954 /* 2 or more enabled crtcs means no need for w/a */
13955 if (enabled_pipe != INVALID_PIPE)
13956 return 0;
13957
13958 enabled_pipe = intel_crtc->pipe;
13959 }
13960
13961 if (enabled_pipe != INVALID_PIPE)
13962 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13963 else if (other_crtc_state)
13964 other_crtc_state->hsw_workaround_pipe = first_pipe;
13965
13966 return 0;
13967}
13968
8d96561a
VS
13969static int intel_lock_all_pipes(struct drm_atomic_state *state)
13970{
13971 struct drm_crtc *crtc;
13972
13973 /* Add all pipes to the state */
13974 for_each_crtc(state->dev, crtc) {
13975 struct drm_crtc_state *crtc_state;
13976
13977 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13978 if (IS_ERR(crtc_state))
13979 return PTR_ERR(crtc_state);
13980 }
13981
13982 return 0;
13983}
13984
27c329ed
ML
13985static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13986{
13987 struct drm_crtc *crtc;
27c329ed 13988
8d96561a
VS
13989 /*
13990 * Add all pipes to the state, and force
13991 * a modeset on all the active ones.
13992 */
27c329ed 13993 for_each_crtc(state->dev, crtc) {
9780aad5
VS
13994 struct drm_crtc_state *crtc_state;
13995 int ret;
13996
27c329ed
ML
13997 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13998 if (IS_ERR(crtc_state))
13999 return PTR_ERR(crtc_state);
14000
14001 if (!crtc_state->active || needs_modeset(crtc_state))
14002 continue;
14003
14004 crtc_state->mode_changed = true;
14005
14006 ret = drm_atomic_add_affected_connectors(state, crtc);
14007 if (ret)
9780aad5 14008 return ret;
27c329ed
ML
14009
14010 ret = drm_atomic_add_affected_planes(state, crtc);
14011 if (ret)
9780aad5 14012 return ret;
27c329ed
ML
14013 }
14014
9780aad5 14015 return 0;
27c329ed
ML
14016}
14017
c347a676 14018static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd 14019{
565602d7 14020 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14021 struct drm_i915_private *dev_priv = to_i915(state->dev);
565602d7
ML
14022 struct drm_crtc *crtc;
14023 struct drm_crtc_state *crtc_state;
14024 int ret = 0, i;
054518dd 14025
b359283a
ML
14026 if (!check_digital_port_conflicts(state)) {
14027 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
14028 return -EINVAL;
14029 }
14030
565602d7
ML
14031 intel_state->modeset = true;
14032 intel_state->active_crtcs = dev_priv->active_crtcs;
14033
14034 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14035 if (crtc_state->active)
14036 intel_state->active_crtcs |= 1 << i;
14037 else
14038 intel_state->active_crtcs &= ~(1 << i);
8b4a7d05
MR
14039
14040 if (crtc_state->active != crtc->state->active)
14041 intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
565602d7
ML
14042 }
14043
054518dd
ACO
14044 /*
14045 * See if the config requires any additional preparation, e.g.
14046 * to adjust global state with pipes off. We need to do this
14047 * here so we can get the modeset_pipe updated config for the new
14048 * mode set on this crtc. For other crtcs we need to use the
14049 * adjusted_mode bits in the crtc directly.
14050 */
27c329ed 14051 if (dev_priv->display.modeset_calc_cdclk) {
c89e39f3 14052 if (!intel_state->cdclk_pll_vco)
63911d72 14053 intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
b2045352
VS
14054 if (!intel_state->cdclk_pll_vco)
14055 intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
c89e39f3 14056
27c329ed 14057 ret = dev_priv->display.modeset_calc_cdclk(state);
c89e39f3
CT
14058 if (ret < 0)
14059 return ret;
27c329ed 14060
8d96561a
VS
14061 /*
14062 * Writes to dev_priv->atomic_cdclk_freq must protected by
14063 * holding all the crtc locks, even if we don't end up
14064 * touching the hardware
14065 */
14066 if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
14067 ret = intel_lock_all_pipes(state);
14068 if (ret < 0)
14069 return ret;
14070 }
14071
14072 /* All pipes must be switched off while we change the cdclk. */
c89e39f3 14073 if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
8d96561a 14074 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
27c329ed 14075 ret = intel_modeset_all_pipes(state);
8d96561a
VS
14076 if (ret < 0)
14077 return ret;
14078 }
e8788cbc
ML
14079
14080 DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
14081 intel_state->cdclk, intel_state->dev_cdclk);
e0ca7a6b 14082 } else {
1a617b77 14083 to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
e0ca7a6b 14084 }
054518dd 14085
ad421372 14086 intel_modeset_clear_plls(state);
054518dd 14087
565602d7 14088 if (IS_HASWELL(dev_priv))
ad421372 14089 return haswell_mode_set_planes_workaround(state);
99d736a2 14090
ad421372 14091 return 0;
c347a676
ACO
14092}
14093
aa363136
MR
14094/*
14095 * Handle calculation of various watermark data at the end of the atomic check
14096 * phase. The code here should be run after the per-crtc and per-plane 'check'
14097 * handlers to ensure that all derived state has been updated.
14098 */
55994c2c 14099static int calc_watermark_data(struct drm_atomic_state *state)
aa363136
MR
14100{
14101 struct drm_device *dev = state->dev;
98d39494 14102 struct drm_i915_private *dev_priv = to_i915(dev);
98d39494
MR
14103
14104 /* Is there platform-specific watermark information to calculate? */
14105 if (dev_priv->display.compute_global_watermarks)
55994c2c
MR
14106 return dev_priv->display.compute_global_watermarks(state);
14107
14108 return 0;
aa363136
MR
14109}
14110
74c090b1
ML
14111/**
14112 * intel_atomic_check - validate state object
14113 * @dev: drm device
14114 * @state: state to validate
14115 */
14116static int intel_atomic_check(struct drm_device *dev,
14117 struct drm_atomic_state *state)
c347a676 14118{
dd8b3bdb 14119 struct drm_i915_private *dev_priv = to_i915(dev);
aa363136 14120 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
c347a676
ACO
14121 struct drm_crtc *crtc;
14122 struct drm_crtc_state *crtc_state;
14123 int ret, i;
61333b60 14124 bool any_ms = false;
c347a676 14125
74c090b1 14126 ret = drm_atomic_helper_check_modeset(dev, state);
054518dd
ACO
14127 if (ret)
14128 return ret;
14129
c347a676 14130 for_each_crtc_in_state(state, crtc, crtc_state, i) {
cfb23ed6
ML
14131 struct intel_crtc_state *pipe_config =
14132 to_intel_crtc_state(crtc_state);
1ed51de9
SV
14133
14134 /* Catch I915_MODE_FLAG_INHERITED */
14135 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
14136 crtc_state->mode_changed = true;
cfb23ed6 14137
af4a879e 14138 if (!needs_modeset(crtc_state))
c347a676
ACO
14139 continue;
14140
af4a879e
SV
14141 if (!crtc_state->enable) {
14142 any_ms = true;
cfb23ed6 14143 continue;
af4a879e 14144 }
cfb23ed6 14145
26495481
SV
14146 /* FIXME: For only active_changed we shouldn't need to do any
14147 * state recomputation at all. */
14148
1ed51de9
SV
14149 ret = drm_atomic_add_affected_connectors(state, crtc);
14150 if (ret)
14151 return ret;
b359283a 14152
cfb23ed6 14153 ret = intel_modeset_pipe_config(crtc, pipe_config);
25aa1c39
ML
14154 if (ret) {
14155 intel_dump_pipe_config(to_intel_crtc(crtc),
14156 pipe_config, "[failed]");
c347a676 14157 return ret;
25aa1c39 14158 }
c347a676 14159
73831236 14160 if (i915.fastboot &&
6315b5d3 14161 intel_pipe_config_compare(dev_priv,
cfb23ed6 14162 to_intel_crtc_state(crtc->state),
1ed51de9 14163 pipe_config, true)) {
26495481 14164 crtc_state->mode_changed = false;
bfd16b2a 14165 to_intel_crtc_state(crtc_state)->update_pipe = true;
26495481
SV
14166 }
14167
af4a879e 14168 if (needs_modeset(crtc_state))
26495481 14169 any_ms = true;
cfb23ed6 14170
af4a879e
SV
14171 ret = drm_atomic_add_affected_planes(state, crtc);
14172 if (ret)
14173 return ret;
61333b60 14174
26495481
SV
14175 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
14176 needs_modeset(crtc_state) ?
14177 "[modeset]" : "[fastset]");
c347a676
ACO
14178 }
14179
61333b60
ML
14180 if (any_ms) {
14181 ret = intel_modeset_checks(state);
14182
14183 if (ret)
14184 return ret;
e0ca7a6b
VS
14185 } else {
14186 intel_state->cdclk = dev_priv->atomic_cdclk_freq;
14187 }
76305b1a 14188
dd8b3bdb 14189 ret = drm_atomic_helper_check_planes(dev, state);
aa363136
MR
14190 if (ret)
14191 return ret;
14192
f51be2e0 14193 intel_fbc_choose_crtc(dev_priv, state);
55994c2c 14194 return calc_watermark_data(state);
054518dd
ACO
14195}
14196
5008e874 14197static int intel_atomic_prepare_commit(struct drm_device *dev,
d07f0e59 14198 struct drm_atomic_state *state)
5008e874 14199{
fac5e23e 14200 struct drm_i915_private *dev_priv = to_i915(dev);
5008e874
ML
14201 struct drm_crtc_state *crtc_state;
14202 struct drm_crtc *crtc;
14203 int i, ret;
14204
5a21b665
SV
14205 for_each_crtc_in_state(state, crtc, crtc_state, i) {
14206 if (state->legacy_cursor_update)
a6747b73
ML
14207 continue;
14208
5a21b665
SV
14209 ret = intel_crtc_wait_for_pending_flips(crtc);
14210 if (ret)
14211 return ret;
5008e874 14212
5a21b665
SV
14213 if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
14214 flush_workqueue(dev_priv->wq);
d55dbd06
ML
14215 }
14216
f935675f
ML
14217 ret = mutex_lock_interruptible(&dev->struct_mutex);
14218 if (ret)
14219 return ret;
14220
5008e874 14221 ret = drm_atomic_helper_prepare_planes(dev, state);
f7e5838b 14222 mutex_unlock(&dev->struct_mutex);
7580d774 14223
5008e874
ML
14224 return ret;
14225}
14226
a2991414
ML
14227u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
14228{
14229 struct drm_device *dev = crtc->base.dev;
14230
14231 if (!dev->max_vblank_count)
14232 return drm_accurate_vblank_count(&crtc->base);
14233
14234 return dev->driver->get_vblank_counter(dev, crtc->pipe);
14235}
14236
5a21b665
SV
14237static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
14238 struct drm_i915_private *dev_priv,
14239 unsigned crtc_mask)
e8861675 14240{
5a21b665
SV
14241 unsigned last_vblank_count[I915_MAX_PIPES];
14242 enum pipe pipe;
14243 int ret;
e8861675 14244
5a21b665
SV
14245 if (!crtc_mask)
14246 return;
e8861675 14247
5a21b665 14248 for_each_pipe(dev_priv, pipe) {
98187836
VS
14249 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14250 pipe);
e8861675 14251
5a21b665 14252 if (!((1 << pipe) & crtc_mask))
e8861675
ML
14253 continue;
14254
e2af48c6 14255 ret = drm_crtc_vblank_get(&crtc->base);
5a21b665
SV
14256 if (WARN_ON(ret != 0)) {
14257 crtc_mask &= ~(1 << pipe);
14258 continue;
e8861675
ML
14259 }
14260
e2af48c6 14261 last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
e8861675
ML
14262 }
14263
5a21b665 14264 for_each_pipe(dev_priv, pipe) {
98187836
VS
14265 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
14266 pipe);
5a21b665 14267 long lret;
e8861675 14268
5a21b665
SV
14269 if (!((1 << pipe) & crtc_mask))
14270 continue;
d55dbd06 14271
5a21b665
SV
14272 lret = wait_event_timeout(dev->vblank[pipe].queue,
14273 last_vblank_count[pipe] !=
e2af48c6 14274 drm_crtc_vblank_count(&crtc->base),
5a21b665 14275 msecs_to_jiffies(50));
d55dbd06 14276
5a21b665 14277 WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
d55dbd06 14278
e2af48c6 14279 drm_crtc_vblank_put(&crtc->base);
d55dbd06
ML
14280 }
14281}
14282
5a21b665 14283static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
a6747b73 14284{
5a21b665
SV
14285 /* fb updated, need to unpin old fb */
14286 if (crtc_state->fb_changed)
14287 return true;
a6747b73 14288
5a21b665
SV
14289 /* wm changes, need vblank before final wm's */
14290 if (crtc_state->update_wm_post)
14291 return true;
a6747b73 14292
5a21b665
SV
14293 /*
14294 * cxsr is re-enabled after vblank.
14295 * This is already handled by crtc_state->update_wm_post,
14296 * but added for clarity.
14297 */
14298 if (crtc_state->disable_cxsr)
14299 return true;
a6747b73 14300
5a21b665 14301 return false;
e8861675
ML
14302}
14303
896e5bb0
L
14304static void intel_update_crtc(struct drm_crtc *crtc,
14305 struct drm_atomic_state *state,
14306 struct drm_crtc_state *old_crtc_state,
14307 unsigned int *crtc_vblank_mask)
14308{
14309 struct drm_device *dev = crtc->dev;
14310 struct drm_i915_private *dev_priv = to_i915(dev);
14311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14312 struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
14313 bool modeset = needs_modeset(crtc->state);
14314
14315 if (modeset) {
14316 update_scanline_offset(intel_crtc);
14317 dev_priv->display.crtc_enable(pipe_config, state);
14318 } else {
14319 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
14320 }
14321
14322 if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
14323 intel_fbc_enable(
14324 intel_crtc, pipe_config,
14325 to_intel_plane_state(crtc->primary->state));
14326 }
14327
14328 drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
14329
14330 if (needs_vblank_wait(pipe_config))
14331 *crtc_vblank_mask |= drm_crtc_mask(crtc);
14332}
14333
14334static void intel_update_crtcs(struct drm_atomic_state *state,
14335 unsigned int *crtc_vblank_mask)
14336{
14337 struct drm_crtc *crtc;
14338 struct drm_crtc_state *old_crtc_state;
14339 int i;
14340
14341 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14342 if (!crtc->state->active)
14343 continue;
14344
14345 intel_update_crtc(crtc, state, old_crtc_state,
14346 crtc_vblank_mask);
14347 }
14348}
14349
27082493
L
14350static void skl_update_crtcs(struct drm_atomic_state *state,
14351 unsigned int *crtc_vblank_mask)
14352{
0f0f74bc 14353 struct drm_i915_private *dev_priv = to_i915(state->dev);
27082493
L
14354 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
14355 struct drm_crtc *crtc;
ce0ba283 14356 struct intel_crtc *intel_crtc;
27082493 14357 struct drm_crtc_state *old_crtc_state;
ce0ba283 14358 struct intel_crtc_state *cstate;
27082493
L
14359 unsigned int updated = 0;
14360 bool progress;
14361 enum pipe pipe;
5eff503b
ML
14362 int i;
14363
14364 const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
14365
14366 for_each_crtc_in_state(state, crtc, old_crtc_state, i)
14367 /* ignore allocations for crtc's that have been turned off. */
14368 if (crtc->state->active)
14369 entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
27082493
L
14370
14371 /*
14372 * Whenever the number of active pipes changes, we need to make sure we
14373 * update the pipes in the right order so that their ddb allocations
14374 * never overlap with eachother inbetween CRTC updates. Otherwise we'll
14375 * cause pipe underruns and other bad stuff.
14376 */
14377 do {
27082493
L
14378 progress = false;
14379
14380 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14381 bool vbl_wait = false;
14382 unsigned int cmask = drm_crtc_mask(crtc);
ce0ba283
L
14383
14384 intel_crtc = to_intel_crtc(crtc);
14385 cstate = to_intel_crtc_state(crtc->state);
14386 pipe = intel_crtc->pipe;
27082493 14387
5eff503b 14388 if (updated & cmask || !cstate->base.active)
27082493 14389 continue;
5eff503b
ML
14390
14391 if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
27082493
L
14392 continue;
14393
14394 updated |= cmask;
5eff503b 14395 entries[i] = &cstate->wm.skl.ddb;
27082493
L
14396
14397 /*
14398 * If this is an already active pipe, it's DDB changed,
14399 * and this isn't the last pipe that needs updating
14400 * then we need to wait for a vblank to pass for the
14401 * new ddb allocation to take effect.
14402 */
ce0ba283 14403 if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
512b5527 14404 &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
27082493
L
14405 !crtc->state->active_changed &&
14406 intel_state->wm_results.dirty_pipes != updated)
14407 vbl_wait = true;
14408
14409 intel_update_crtc(crtc, state, old_crtc_state,
14410 crtc_vblank_mask);
14411
14412 if (vbl_wait)
0f0f74bc 14413 intel_wait_for_vblank(dev_priv, pipe);
27082493
L
14414
14415 progress = true;
14416 }
14417 } while (progress);
14418}
14419
94f05024 14420static void intel_atomic_commit_tail(struct drm_atomic_state *state)
a6778b3c 14421{
94f05024 14422 struct drm_device *dev = state->dev;
565602d7 14423 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14424 struct drm_i915_private *dev_priv = to_i915(dev);
29ceb0e6 14425 struct drm_crtc_state *old_crtc_state;
7580d774 14426 struct drm_crtc *crtc;
5a21b665 14427 struct intel_crtc_state *intel_cstate;
5a21b665
SV
14428 bool hw_check = intel_state->modeset;
14429 unsigned long put_domains[I915_MAX_PIPES] = {};
14430 unsigned crtc_vblank_mask = 0;
e95433c7 14431 int i;
a6778b3c 14432
ea0000f0
SV
14433 drm_atomic_helper_wait_for_dependencies(state);
14434
c3b32658 14435 if (intel_state->modeset)
5a21b665 14436 intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
565602d7 14437
29ceb0e6 14438 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
a539205a
ML
14439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14440
5a21b665
SV
14441 if (needs_modeset(crtc->state) ||
14442 to_intel_crtc_state(crtc->state)->update_pipe) {
14443 hw_check = true;
14444
14445 put_domains[to_intel_crtc(crtc)->pipe] =
14446 modeset_get_crtc_power_domains(crtc,
14447 to_intel_crtc_state(crtc->state));
14448 }
14449
61333b60
ML
14450 if (!needs_modeset(crtc->state))
14451 continue;
14452
29ceb0e6 14453 intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
460da916 14454
29ceb0e6
VS
14455 if (old_crtc_state->active) {
14456 intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
4a806558 14457 dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
eddfcbcd 14458 intel_crtc->active = false;
58f9c0bc 14459 intel_fbc_disable(intel_crtc);
eddfcbcd 14460 intel_disable_shared_dpll(intel_crtc);
9bbc8258
VS
14461
14462 /*
14463 * Underruns don't always raise
14464 * interrupts, so check manually.
14465 */
14466 intel_check_cpu_fifo_underruns(dev_priv);
14467 intel_check_pch_fifo_underruns(dev_priv);
b9001114 14468
e62929b3
ML
14469 if (!crtc->state->active) {
14470 /*
14471 * Make sure we don't call initial_watermarks
14472 * for ILK-style watermark updates.
14473 */
14474 if (dev_priv->display.atomic_update_watermarks)
14475 dev_priv->display.initial_watermarks(intel_state,
14476 to_intel_crtc_state(crtc->state));
14477 else
14478 intel_update_watermarks(intel_crtc);
14479 }
a539205a 14480 }
b8cecdf5 14481 }
7758a113 14482
ea9d758d
SV
14483 /* Only after disabling all output pipelines that will be changed can we
14484 * update the the output configuration. */
4740b0f2 14485 intel_modeset_update_crtc_state(state);
f6e5b160 14486
565602d7 14487 if (intel_state->modeset) {
4740b0f2 14488 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
33c8df89
ML
14489
14490 if (dev_priv->display.modeset_commit_cdclk &&
c89e39f3 14491 (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
63911d72 14492 intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
33c8df89 14493 dev_priv->display.modeset_commit_cdclk(state);
f6d1973d 14494
656d1b89
L
14495 /*
14496 * SKL workaround: bspec recommends we disable the SAGV when we
14497 * have more then one pipe enabled
14498 */
56feca91 14499 if (!intel_can_enable_sagv(state))
16dcdc4e 14500 intel_disable_sagv(dev_priv);
656d1b89 14501
677100ce 14502 intel_modeset_verify_disabled(dev, state);
4740b0f2 14503 }
47fab737 14504
896e5bb0 14505 /* Complete the events for pipes that have now been disabled */
29ceb0e6 14506 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
f6ac4b2a 14507 bool modeset = needs_modeset(crtc->state);
80715b2f 14508
1f7528c4
SV
14509 /* Complete events for now disable pipes here. */
14510 if (modeset && !crtc->state->active && crtc->state->event) {
14511 spin_lock_irq(&dev->event_lock);
14512 drm_crtc_send_vblank_event(crtc, crtc->state->event);
14513 spin_unlock_irq(&dev->event_lock);
14514
14515 crtc->state->event = NULL;
14516 }
177246a8
MR
14517 }
14518
896e5bb0
L
14519 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
14520 dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
14521
94f05024
SV
14522 /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
14523 * already, but still need the state for the delayed optimization. To
14524 * fix this:
14525 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
14526 * - schedule that vblank worker _before_ calling hw_done
14527 * - at the start of commit_tail, cancel it _synchrously
14528 * - switch over to the vblank wait helper in the core after that since
14529 * we don't need out special handling any more.
14530 */
5a21b665
SV
14531 if (!state->legacy_cursor_update)
14532 intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
14533
14534 /*
14535 * Now that the vblank has passed, we can go ahead and program the
14536 * optimal watermarks on platforms that need two-step watermark
14537 * programming.
14538 *
14539 * TODO: Move this (and other cleanup) to an async worker eventually.
14540 */
14541 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14542 intel_cstate = to_intel_crtc_state(crtc->state);
14543
14544 if (dev_priv->display.optimize_watermarks)
ccf010fb
ML
14545 dev_priv->display.optimize_watermarks(intel_state,
14546 intel_cstate);
5a21b665
SV
14547 }
14548
14549 for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
14550 intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
14551
14552 if (put_domains[i])
14553 modeset_put_power_domains(dev_priv, put_domains[i]);
14554
677100ce 14555 intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
5a21b665
SV
14556 }
14557
56feca91 14558 if (intel_state->modeset && intel_can_enable_sagv(state))
16dcdc4e 14559 intel_enable_sagv(dev_priv);
656d1b89 14560
94f05024
SV
14561 drm_atomic_helper_commit_hw_done(state);
14562
5a21b665
SV
14563 if (intel_state->modeset)
14564 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
14565
14566 mutex_lock(&dev->struct_mutex);
14567 drm_atomic_helper_cleanup_planes(dev, state);
14568 mutex_unlock(&dev->struct_mutex);
14569
ea0000f0
SV
14570 drm_atomic_helper_commit_cleanup_done(state);
14571
0853695c 14572 drm_atomic_state_put(state);
f30da187 14573
75714940
MK
14574 /* As one of the primary mmio accessors, KMS has a high likelihood
14575 * of triggering bugs in unclaimed access. After we finish
14576 * modesetting, see if an error has been flagged, and if so
14577 * enable debugging for the next modeset - and hope we catch
14578 * the culprit.
14579 *
14580 * XXX note that we assume display power is on at this point.
14581 * This might hold true now but we need to add pm helper to check
14582 * unclaimed only when the hardware is on, as atomic commits
14583 * can happen also when the device is completely off.
14584 */
14585 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
94f05024
SV
14586}
14587
14588static void intel_atomic_commit_work(struct work_struct *work)
14589{
c004a90b
CW
14590 struct drm_atomic_state *state =
14591 container_of(work, struct drm_atomic_state, commit_work);
14592
94f05024
SV
14593 intel_atomic_commit_tail(state);
14594}
14595
c004a90b
CW
14596static int __i915_sw_fence_call
14597intel_atomic_commit_ready(struct i915_sw_fence *fence,
14598 enum i915_sw_fence_notify notify)
14599{
14600 struct intel_atomic_state *state =
14601 container_of(fence, struct intel_atomic_state, commit_ready);
14602
14603 switch (notify) {
14604 case FENCE_COMPLETE:
14605 if (state->base.commit_work.func)
14606 queue_work(system_unbound_wq, &state->base.commit_work);
14607 break;
14608
14609 case FENCE_FREE:
14610 drm_atomic_state_put(&state->base);
14611 break;
14612 }
14613
14614 return NOTIFY_DONE;
14615}
14616
6c9c1b38
SV
14617static void intel_atomic_track_fbs(struct drm_atomic_state *state)
14618{
14619 struct drm_plane_state *old_plane_state;
14620 struct drm_plane *plane;
6c9c1b38
SV
14621 int i;
14622
faf5bf0a
CW
14623 for_each_plane_in_state(state, plane, old_plane_state, i)
14624 i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
14625 intel_fb_obj(plane->state->fb),
14626 to_intel_plane(plane)->frontbuffer_bit);
6c9c1b38
SV
14627}
14628
94f05024
SV
14629/**
14630 * intel_atomic_commit - commit validated state object
14631 * @dev: DRM device
14632 * @state: the top-level driver state object
14633 * @nonblock: nonblocking commit
14634 *
14635 * This function commits a top-level state object that has been validated
14636 * with drm_atomic_helper_check().
14637 *
94f05024
SV
14638 * RETURNS
14639 * Zero for success or -errno.
14640 */
14641static int intel_atomic_commit(struct drm_device *dev,
14642 struct drm_atomic_state *state,
14643 bool nonblock)
14644{
14645 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
fac5e23e 14646 struct drm_i915_private *dev_priv = to_i915(dev);
94f05024
SV
14647 int ret = 0;
14648
94f05024
SV
14649 ret = drm_atomic_helper_setup_commit(state, nonblock);
14650 if (ret)
14651 return ret;
14652
c004a90b
CW
14653 drm_atomic_state_get(state);
14654 i915_sw_fence_init(&intel_state->commit_ready,
14655 intel_atomic_commit_ready);
94f05024 14656
d07f0e59 14657 ret = intel_atomic_prepare_commit(dev, state);
94f05024
SV
14658 if (ret) {
14659 DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
c004a90b 14660 i915_sw_fence_commit(&intel_state->commit_ready);
94f05024
SV
14661 return ret;
14662 }
14663
14664 drm_atomic_helper_swap_state(state, true);
14665 dev_priv->wm.distrust_bios_wm = false;
94f05024 14666 intel_shared_dpll_commit(state);
6c9c1b38 14667 intel_atomic_track_fbs(state);
94f05024 14668
c3b32658
ML
14669 if (intel_state->modeset) {
14670 memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
14671 sizeof(intel_state->min_pixclk));
14672 dev_priv->active_crtcs = intel_state->active_crtcs;
14673 dev_priv->atomic_cdclk_freq = intel_state->cdclk;
14674 }
14675
0853695c 14676 drm_atomic_state_get(state);
c004a90b
CW
14677 INIT_WORK(&state->commit_work,
14678 nonblock ? intel_atomic_commit_work : NULL);
14679
14680 i915_sw_fence_commit(&intel_state->commit_ready);
14681 if (!nonblock) {
14682 i915_sw_fence_wait(&intel_state->commit_ready);
94f05024 14683 intel_atomic_commit_tail(state);
c004a90b 14684 }
75714940 14685
74c090b1 14686 return 0;
7f27126e
JB
14687}
14688
c0c36b94
CW
14689void intel_crtc_restore_mode(struct drm_crtc *crtc)
14690{
83a57153
ACO
14691 struct drm_device *dev = crtc->dev;
14692 struct drm_atomic_state *state;
e694eb02 14693 struct drm_crtc_state *crtc_state;
2bfb4627 14694 int ret;
83a57153
ACO
14695
14696 state = drm_atomic_state_alloc(dev);
14697 if (!state) {
78108b7c
VS
14698 DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
14699 crtc->base.id, crtc->name);
83a57153
ACO
14700 return;
14701 }
14702
e694eb02 14703 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
83a57153 14704
e694eb02
ML
14705retry:
14706 crtc_state = drm_atomic_get_crtc_state(state, crtc);
14707 ret = PTR_ERR_OR_ZERO(crtc_state);
14708 if (!ret) {
14709 if (!crtc_state->active)
14710 goto out;
83a57153 14711
e694eb02 14712 crtc_state->mode_changed = true;
74c090b1 14713 ret = drm_atomic_commit(state);
83a57153
ACO
14714 }
14715
e694eb02
ML
14716 if (ret == -EDEADLK) {
14717 drm_atomic_state_clear(state);
14718 drm_modeset_backoff(state->acquire_ctx);
14719 goto retry;
4ed9fb37 14720 }
4be07317 14721
e694eb02 14722out:
0853695c 14723 drm_atomic_state_put(state);
c0c36b94
CW
14724}
14725
a8784875
BP
14726/*
14727 * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
14728 * drm_atomic_helper_legacy_gamma_set() directly.
14729 */
14730static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
14731 u16 *red, u16 *green, u16 *blue,
14732 uint32_t size)
14733{
14734 struct drm_device *dev = crtc->dev;
14735 struct drm_mode_config *config = &dev->mode_config;
14736 struct drm_crtc_state *state;
14737 int ret;
14738
14739 ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
14740 if (ret)
14741 return ret;
14742
14743 /*
14744 * Make sure we update the legacy properties so this works when
14745 * atomic is not enabled.
14746 */
14747
14748 state = crtc->state;
14749
14750 drm_object_property_set_value(&crtc->base,
14751 config->degamma_lut_property,
14752 (state->degamma_lut) ?
14753 state->degamma_lut->base.id : 0);
14754
14755 drm_object_property_set_value(&crtc->base,
14756 config->ctm_property,
14757 (state->ctm) ?
14758 state->ctm->base.id : 0);
14759
14760 drm_object_property_set_value(&crtc->base,
14761 config->gamma_lut_property,
14762 (state->gamma_lut) ?
14763 state->gamma_lut->base.id : 0);
14764
14765 return 0;
14766}
14767
f6e5b160 14768static const struct drm_crtc_funcs intel_crtc_funcs = {
a8784875 14769 .gamma_set = intel_atomic_legacy_gamma_set,
74c090b1 14770 .set_config = drm_atomic_helper_set_config,
82cf435b 14771 .set_property = drm_atomic_helper_crtc_set_property,
f6e5b160 14772 .destroy = intel_crtc_destroy,
527b6abe 14773 .page_flip = intel_crtc_page_flip,
1356837e
MR
14774 .atomic_duplicate_state = intel_crtc_duplicate_state,
14775 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
14776};
14777
6beb8c23
MR
14778/**
14779 * intel_prepare_plane_fb - Prepare fb for usage on plane
14780 * @plane: drm plane to prepare for
14781 * @fb: framebuffer to prepare for presentation
14782 *
14783 * Prepares a framebuffer for usage on a display plane. Generally this
14784 * involves pinning the underlying object and updating the frontbuffer tracking
14785 * bits. Some older platforms need special physical address handling for
14786 * cursor planes.
14787 *
f935675f
ML
14788 * Must be called with struct_mutex held.
14789 *
6beb8c23
MR
14790 * Returns 0 on success, negative error code on failure.
14791 */
14792int
14793intel_prepare_plane_fb(struct drm_plane *plane,
1832040d 14794 struct drm_plane_state *new_state)
465c120c 14795{
c004a90b
CW
14796 struct intel_atomic_state *intel_state =
14797 to_intel_atomic_state(new_state->state);
b7f05d4a 14798 struct drm_i915_private *dev_priv = to_i915(plane->dev);
844f9111 14799 struct drm_framebuffer *fb = new_state->fb;
6beb8c23 14800 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1ee49399 14801 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
c004a90b 14802 int ret;
465c120c 14803
1ee49399 14804 if (!obj && !old_obj)
465c120c
MR
14805 return 0;
14806
5008e874
ML
14807 if (old_obj) {
14808 struct drm_crtc_state *crtc_state =
c004a90b
CW
14809 drm_atomic_get_existing_crtc_state(new_state->state,
14810 plane->state->crtc);
5008e874
ML
14811
14812 /* Big Hammer, we also need to ensure that any pending
14813 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
14814 * current scanout is retired before unpinning the old
14815 * framebuffer. Note that we rely on userspace rendering
14816 * into the buffer attached to the pipe they are waiting
14817 * on. If not, userspace generates a GPU hang with IPEHR
14818 * point to the MI_WAIT_FOR_EVENT.
14819 *
14820 * This should only fail upon a hung GPU, in which case we
14821 * can safely continue.
14822 */
c004a90b
CW
14823 if (needs_modeset(crtc_state)) {
14824 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14825 old_obj->resv, NULL,
14826 false, 0,
14827 GFP_KERNEL);
14828 if (ret < 0)
14829 return ret;
f4457ae7 14830 }
5008e874
ML
14831 }
14832
c004a90b
CW
14833 if (new_state->fence) { /* explicit fencing */
14834 ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
14835 new_state->fence,
14836 I915_FENCE_TIMEOUT,
14837 GFP_KERNEL);
14838 if (ret < 0)
14839 return ret;
14840 }
14841
c37efb99
CW
14842 if (!obj)
14843 return 0;
14844
c004a90b
CW
14845 if (!new_state->fence) { /* implicit fencing */
14846 ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
14847 obj->resv, NULL,
14848 false, I915_FENCE_TIMEOUT,
14849 GFP_KERNEL);
14850 if (ret < 0)
14851 return ret;
6b5e90f5
CW
14852
14853 i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
c004a90b 14854 }
5a21b665 14855
c37efb99 14856 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
b7f05d4a 14857 INTEL_INFO(dev_priv)->cursor_needs_physical) {
50a0bc90 14858 int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
6beb8c23 14859 ret = i915_gem_object_attach_phys(obj, align);
d07f0e59 14860 if (ret) {
6beb8c23 14861 DRM_DEBUG_KMS("failed to attach phys object\n");
d07f0e59
CW
14862 return ret;
14863 }
6beb8c23 14864 } else {
058d88c4
CW
14865 struct i915_vma *vma;
14866
14867 vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
d07f0e59
CW
14868 if (IS_ERR(vma)) {
14869 DRM_DEBUG_KMS("failed to pin object\n");
14870 return PTR_ERR(vma);
14871 }
7580d774 14872 }
fdd508a6 14873
d07f0e59 14874 return 0;
6beb8c23
MR
14875}
14876
38f3ce3a
MR
14877/**
14878 * intel_cleanup_plane_fb - Cleans up an fb after plane use
14879 * @plane: drm plane to clean up for
14880 * @fb: old framebuffer that was on plane
14881 *
14882 * Cleans up a framebuffer that has just been removed from a plane.
f935675f
ML
14883 *
14884 * Must be called with struct_mutex held.
38f3ce3a
MR
14885 */
14886void
14887intel_cleanup_plane_fb(struct drm_plane *plane,
1832040d 14888 struct drm_plane_state *old_state)
38f3ce3a 14889{
b7f05d4a 14890 struct drm_i915_private *dev_priv = to_i915(plane->dev);
7580d774 14891 struct intel_plane_state *old_intel_state;
1ee49399
ML
14892 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
14893 struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
38f3ce3a 14894
7580d774
ML
14895 old_intel_state = to_intel_plane_state(old_state);
14896
1ee49399 14897 if (!obj && !old_obj)
38f3ce3a
MR
14898 return;
14899
1ee49399 14900 if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
b7f05d4a 14901 !INTEL_INFO(dev_priv)->cursor_needs_physical))
3465c580 14902 intel_unpin_fb_obj(old_state->fb, old_state->rotation);
465c120c
MR
14903}
14904
6156a456
CK
14905int
14906skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
14907{
14908 int max_scale;
6156a456
CK
14909 int crtc_clock, cdclk;
14910
bf8a0af0 14911 if (!intel_crtc || !crtc_state->base.enable)
6156a456
CK
14912 return DRM_PLANE_HELPER_NO_SCALING;
14913
6156a456 14914 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 14915 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456 14916
54bf1ce6 14917 if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
6156a456
CK
14918 return DRM_PLANE_HELPER_NO_SCALING;
14919
14920 /*
14921 * skl max scale is lower of:
14922 * close to 3 but not 3, -1 is for that purpose
14923 * or
14924 * cdclk/crtc_clock
14925 */
14926 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
14927
14928 return max_scale;
14929}
14930
465c120c 14931static int
3c692a41 14932intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 14933 struct intel_crtc_state *crtc_state,
3c692a41
GP
14934 struct intel_plane_state *state)
14935{
b63a16f6 14936 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 14937 struct drm_crtc *crtc = state->base.crtc;
6156a456 14938 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
14939 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
14940 bool can_position = false;
b63a16f6 14941 int ret;
465c120c 14942
b63a16f6 14943 if (INTEL_GEN(dev_priv) >= 9) {
693bdc28
VS
14944 /* use scaler when colorkey is not required */
14945 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
14946 min_scale = 1;
14947 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
14948 }
d8106366 14949 can_position = true;
6156a456 14950 }
d8106366 14951
cc926387
SV
14952 ret = drm_plane_helper_check_state(&state->base,
14953 &state->clip,
14954 min_scale, max_scale,
14955 can_position, true);
b63a16f6
VS
14956 if (ret)
14957 return ret;
14958
cc926387 14959 if (!state->base.fb)
b63a16f6
VS
14960 return 0;
14961
14962 if (INTEL_GEN(dev_priv) >= 9) {
14963 ret = skl_check_plane_surface(state);
14964 if (ret)
14965 return ret;
14966 }
14967
14968 return 0;
14af293f
GP
14969}
14970
5a21b665
SV
14971static void intel_begin_crtc_commit(struct drm_crtc *crtc,
14972 struct drm_crtc_state *old_crtc_state)
14973{
14974 struct drm_device *dev = crtc->dev;
62e0fb88 14975 struct drm_i915_private *dev_priv = to_i915(dev);
5a21b665 14976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b707aa50
L
14977 struct intel_crtc_state *intel_cstate =
14978 to_intel_crtc_state(crtc->state);
ccf010fb 14979 struct intel_crtc_state *old_intel_cstate =
5a21b665 14980 to_intel_crtc_state(old_crtc_state);
ccf010fb
ML
14981 struct intel_atomic_state *old_intel_state =
14982 to_intel_atomic_state(old_crtc_state->state);
5a21b665
SV
14983 bool modeset = needs_modeset(crtc->state);
14984
14985 /* Perform vblank evasion around commit operation */
14986 intel_pipe_update_start(intel_crtc);
14987
14988 if (modeset)
e62929b3 14989 goto out;
5a21b665
SV
14990
14991 if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
14992 intel_color_set_csc(crtc->state);
14993 intel_color_load_luts(crtc->state);
14994 }
14995
ccf010fb
ML
14996 if (intel_cstate->update_pipe)
14997 intel_update_pipe_config(intel_crtc, old_intel_cstate);
14998 else if (INTEL_GEN(dev_priv) >= 9)
5a21b665 14999 skl_detach_scalers(intel_crtc);
62e0fb88 15000
e62929b3 15001out:
ccf010fb
ML
15002 if (dev_priv->display.atomic_update_watermarks)
15003 dev_priv->display.atomic_update_watermarks(old_intel_state,
15004 intel_cstate);
5a21b665
SV
15005}
15006
15007static void intel_finish_crtc_commit(struct drm_crtc *crtc,
15008 struct drm_crtc_state *old_crtc_state)
15009{
15010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15011
15012 intel_pipe_update_end(intel_crtc, NULL);
15013}
15014
cf4c7c12 15015/**
4a3b8769
MR
15016 * intel_plane_destroy - destroy a plane
15017 * @plane: plane to destroy
cf4c7c12 15018 *
4a3b8769
MR
15019 * Common destruction function for all types of planes (primary, cursor,
15020 * sprite).
cf4c7c12 15021 */
4a3b8769 15022void intel_plane_destroy(struct drm_plane *plane)
465c120c 15023{
465c120c 15024 drm_plane_cleanup(plane);
69ae561f 15025 kfree(to_intel_plane(plane));
465c120c
MR
15026}
15027
65a3fea0 15028const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
15029 .update_plane = drm_atomic_helper_update_plane,
15030 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 15031 .destroy = intel_plane_destroy,
c196e1d6 15032 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
15033 .atomic_get_property = intel_plane_atomic_get_property,
15034 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
15035 .atomic_duplicate_state = intel_plane_duplicate_state,
15036 .atomic_destroy_state = intel_plane_destroy_state,
465c120c
MR
15037};
15038
b079bd17 15039static struct intel_plane *
580503c7 15040intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
465c120c 15041{
fca0ce2a
VS
15042 struct intel_plane *primary = NULL;
15043 struct intel_plane_state *state = NULL;
465c120c 15044 const uint32_t *intel_primary_formats;
93ca7e00 15045 unsigned int supported_rotations;
45e3743a 15046 unsigned int num_formats;
fca0ce2a 15047 int ret;
465c120c
MR
15048
15049 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
b079bd17
VS
15050 if (!primary) {
15051 ret = -ENOMEM;
fca0ce2a 15052 goto fail;
b079bd17 15053 }
465c120c 15054
8e7d688b 15055 state = intel_create_plane_state(&primary->base);
b079bd17
VS
15056 if (!state) {
15057 ret = -ENOMEM;
fca0ce2a 15058 goto fail;
b079bd17
VS
15059 }
15060
8e7d688b 15061 primary->base.state = &state->base;
ea2c67bb 15062
465c120c
MR
15063 primary->can_scale = false;
15064 primary->max_downscale = 1;
580503c7 15065 if (INTEL_GEN(dev_priv) >= 9) {
6156a456 15066 primary->can_scale = true;
af99ceda 15067 state->scaler_id = -1;
6156a456 15068 }
465c120c 15069 primary->pipe = pipe;
e3c566df
VS
15070 /*
15071 * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
15072 * port is hooked to pipe B. Hence we want plane A feeding pipe B.
15073 */
15074 if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
15075 primary->plane = (enum plane) !pipe;
15076 else
15077 primary->plane = (enum plane) pipe;
b14e5848 15078 primary->id = PLANE_PRIMARY;
a9ff8714 15079 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179 15080 primary->check_plane = intel_check_primary_plane;
465c120c 15081
580503c7 15082 if (INTEL_GEN(dev_priv) >= 9) {
6c0fd451
DL
15083 intel_primary_formats = skl_primary_formats;
15084 num_formats = ARRAY_SIZE(skl_primary_formats);
a8d201af
ML
15085
15086 primary->update_plane = skylake_update_primary_plane;
15087 primary->disable_plane = skylake_disable_primary_plane;
6e266956 15088 } else if (HAS_PCH_SPLIT(dev_priv)) {
a8d201af
ML
15089 intel_primary_formats = i965_primary_formats;
15090 num_formats = ARRAY_SIZE(i965_primary_formats);
15091
15092 primary->update_plane = ironlake_update_primary_plane;
15093 primary->disable_plane = i9xx_disable_primary_plane;
580503c7 15094 } else if (INTEL_GEN(dev_priv) >= 4) {
568db4f2
DL
15095 intel_primary_formats = i965_primary_formats;
15096 num_formats = ARRAY_SIZE(i965_primary_formats);
a8d201af
ML
15097
15098 primary->update_plane = i9xx_update_primary_plane;
15099 primary->disable_plane = i9xx_disable_primary_plane;
6c0fd451
DL
15100 } else {
15101 intel_primary_formats = i8xx_primary_formats;
15102 num_formats = ARRAY_SIZE(i8xx_primary_formats);
a8d201af
ML
15103
15104 primary->update_plane = i9xx_update_primary_plane;
15105 primary->disable_plane = i9xx_disable_primary_plane;
465c120c
MR
15106 }
15107
580503c7
VS
15108 if (INTEL_GEN(dev_priv) >= 9)
15109 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15110 0, &intel_plane_funcs,
38573dc1
VS
15111 intel_primary_formats, num_formats,
15112 DRM_PLANE_TYPE_PRIMARY,
15113 "plane 1%c", pipe_name(pipe));
9beb5fea 15114 else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
580503c7
VS
15115 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15116 0, &intel_plane_funcs,
38573dc1
VS
15117 intel_primary_formats, num_formats,
15118 DRM_PLANE_TYPE_PRIMARY,
15119 "primary %c", pipe_name(pipe));
15120 else
580503c7
VS
15121 ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
15122 0, &intel_plane_funcs,
38573dc1
VS
15123 intel_primary_formats, num_formats,
15124 DRM_PLANE_TYPE_PRIMARY,
15125 "plane %c", plane_name(primary->plane));
fca0ce2a
VS
15126 if (ret)
15127 goto fail;
48404c1e 15128
5481e27f 15129 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00
VS
15130 supported_rotations =
15131 DRM_ROTATE_0 | DRM_ROTATE_90 |
15132 DRM_ROTATE_180 | DRM_ROTATE_270;
4ea7be2b
VS
15133 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
15134 supported_rotations =
15135 DRM_ROTATE_0 | DRM_ROTATE_180 |
15136 DRM_REFLECT_X;
5481e27f 15137 } else if (INTEL_GEN(dev_priv) >= 4) {
93ca7e00
VS
15138 supported_rotations =
15139 DRM_ROTATE_0 | DRM_ROTATE_180;
15140 } else {
15141 supported_rotations = DRM_ROTATE_0;
15142 }
15143
5481e27f 15144 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15145 drm_plane_create_rotation_property(&primary->base,
15146 DRM_ROTATE_0,
15147 supported_rotations);
48404c1e 15148
ea2c67bb
MR
15149 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
15150
b079bd17 15151 return primary;
fca0ce2a
VS
15152
15153fail:
15154 kfree(state);
15155 kfree(primary);
15156
b079bd17 15157 return ERR_PTR(ret);
465c120c
MR
15158}
15159
3d7d6510 15160static int
852e787c 15161intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 15162 struct intel_crtc_state *crtc_state,
852e787c 15163 struct intel_plane_state *state)
3d7d6510 15164{
2b875c22 15165 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 15166 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
b29ec92c 15167 enum pipe pipe = to_intel_plane(plane)->pipe;
757f9a3e
GP
15168 unsigned stride;
15169 int ret;
3d7d6510 15170
f8856a44
VS
15171 ret = drm_plane_helper_check_state(&state->base,
15172 &state->clip,
15173 DRM_PLANE_HELPER_NO_SCALING,
15174 DRM_PLANE_HELPER_NO_SCALING,
15175 true, true);
757f9a3e
GP
15176 if (ret)
15177 return ret;
15178
757f9a3e
GP
15179 /* if we want to turn off the cursor ignore width and height */
15180 if (!obj)
da20eabd 15181 return 0;
757f9a3e 15182
757f9a3e 15183 /* Check for which cursor types we support */
50a0bc90
TU
15184 if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
15185 state->base.crtc_h)) {
ea2c67bb
MR
15186 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
15187 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
15188 return -EINVAL;
15189 }
15190
ea2c67bb
MR
15191 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
15192 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
15193 DRM_DEBUG_KMS("buffer is too small\n");
15194 return -ENOMEM;
15195 }
15196
bae781b2 15197 if (fb->modifier != DRM_FORMAT_MOD_NONE) {
757f9a3e 15198 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 15199 return -EINVAL;
32b7eeec
MR
15200 }
15201
b29ec92c
VS
15202 /*
15203 * There's something wrong with the cursor on CHV pipe C.
15204 * If it straddles the left edge of the screen then
15205 * moving it away from the edge or disabling it often
15206 * results in a pipe underrun, and often that can lead to
15207 * dead pipe (constant underrun reported, and it scans
15208 * out just a solid color). To recover from that, the
15209 * display power well must be turned off and on again.
15210 * Refuse the put the cursor into that compromised position.
15211 */
920a14b2 15212 if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
936e71e3 15213 state->base.visible && state->base.crtc_x < 0) {
b29ec92c
VS
15214 DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
15215 return -EINVAL;
15216 }
15217
da20eabd 15218 return 0;
852e787c 15219}
3d7d6510 15220
a8ad0d8e
ML
15221static void
15222intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 15223 struct drm_crtc *crtc)
a8ad0d8e 15224{
f2858021
ML
15225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15226
15227 intel_crtc->cursor_addr = 0;
55a08b3f 15228 intel_crtc_update_cursor(crtc, NULL);
a8ad0d8e
ML
15229}
15230
f4a2cf29 15231static void
55a08b3f
ML
15232intel_update_cursor_plane(struct drm_plane *plane,
15233 const struct intel_crtc_state *crtc_state,
15234 const struct intel_plane_state *state)
852e787c 15235{
55a08b3f
ML
15236 struct drm_crtc *crtc = crtc_state->base.crtc;
15237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b7f05d4a 15238 struct drm_i915_private *dev_priv = to_i915(plane->dev);
2b875c22 15239 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 15240 uint32_t addr;
852e787c 15241
f4a2cf29 15242 if (!obj)
a912f12f 15243 addr = 0;
b7f05d4a 15244 else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
058d88c4 15245 addr = i915_gem_object_ggtt_offset(obj, NULL);
f4a2cf29 15246 else
a912f12f 15247 addr = obj->phys_handle->busaddr;
852e787c 15248
a912f12f 15249 intel_crtc->cursor_addr = addr;
55a08b3f 15250 intel_crtc_update_cursor(crtc, state);
852e787c
GP
15251}
15252
b079bd17 15253static struct intel_plane *
580503c7 15254intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
3d7d6510 15255{
fca0ce2a
VS
15256 struct intel_plane *cursor = NULL;
15257 struct intel_plane_state *state = NULL;
15258 int ret;
3d7d6510
MR
15259
15260 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
b079bd17
VS
15261 if (!cursor) {
15262 ret = -ENOMEM;
fca0ce2a 15263 goto fail;
b079bd17 15264 }
3d7d6510 15265
8e7d688b 15266 state = intel_create_plane_state(&cursor->base);
b079bd17
VS
15267 if (!state) {
15268 ret = -ENOMEM;
fca0ce2a 15269 goto fail;
b079bd17
VS
15270 }
15271
8e7d688b 15272 cursor->base.state = &state->base;
ea2c67bb 15273
3d7d6510
MR
15274 cursor->can_scale = false;
15275 cursor->max_downscale = 1;
15276 cursor->pipe = pipe;
15277 cursor->plane = pipe;
b14e5848 15278 cursor->id = PLANE_CURSOR;
a9ff8714 15279 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179 15280 cursor->check_plane = intel_check_cursor_plane;
55a08b3f 15281 cursor->update_plane = intel_update_cursor_plane;
a8ad0d8e 15282 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510 15283
580503c7
VS
15284 ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
15285 0, &intel_plane_funcs,
fca0ce2a
VS
15286 intel_cursor_formats,
15287 ARRAY_SIZE(intel_cursor_formats),
38573dc1
VS
15288 DRM_PLANE_TYPE_CURSOR,
15289 "cursor %c", pipe_name(pipe));
fca0ce2a
VS
15290 if (ret)
15291 goto fail;
4398ad45 15292
5481e27f 15293 if (INTEL_GEN(dev_priv) >= 4)
93ca7e00
VS
15294 drm_plane_create_rotation_property(&cursor->base,
15295 DRM_ROTATE_0,
15296 DRM_ROTATE_0 |
15297 DRM_ROTATE_180);
4398ad45 15298
580503c7 15299 if (INTEL_GEN(dev_priv) >= 9)
af99ceda
CK
15300 state->scaler_id = -1;
15301
ea2c67bb
MR
15302 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
15303
b079bd17 15304 return cursor;
fca0ce2a
VS
15305
15306fail:
15307 kfree(state);
15308 kfree(cursor);
15309
b079bd17 15310 return ERR_PTR(ret);
3d7d6510
MR
15311}
15312
65edccce
VS
15313static void skl_init_scalers(struct drm_i915_private *dev_priv,
15314 struct intel_crtc *crtc,
15315 struct intel_crtc_state *crtc_state)
549e2bfb 15316{
65edccce
VS
15317 struct intel_crtc_scaler_state *scaler_state =
15318 &crtc_state->scaler_state;
549e2bfb 15319 int i;
549e2bfb 15320
65edccce
VS
15321 for (i = 0; i < crtc->num_scalers; i++) {
15322 struct intel_scaler *scaler = &scaler_state->scalers[i];
15323
15324 scaler->in_use = 0;
15325 scaler->mode = PS_SCALER_MODE_DYN;
549e2bfb
CK
15326 }
15327
15328 scaler_state->scaler_id = -1;
15329}
15330
5ab0d85b 15331static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
79e53945
JB
15332{
15333 struct intel_crtc *intel_crtc;
f5de6e07 15334 struct intel_crtc_state *crtc_state = NULL;
b079bd17
VS
15335 struct intel_plane *primary = NULL;
15336 struct intel_plane *cursor = NULL;
a81d6fa0 15337 int sprite, ret;
79e53945 15338
955382f3 15339 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
b079bd17
VS
15340 if (!intel_crtc)
15341 return -ENOMEM;
79e53945 15342
f5de6e07 15343 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
b079bd17
VS
15344 if (!crtc_state) {
15345 ret = -ENOMEM;
f5de6e07 15346 goto fail;
b079bd17 15347 }
550acefd
ACO
15348 intel_crtc->config = crtc_state;
15349 intel_crtc->base.state = &crtc_state->base;
07878248 15350 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 15351
549e2bfb 15352 /* initialize shared scalers */
5ab0d85b 15353 if (INTEL_GEN(dev_priv) >= 9) {
549e2bfb
CK
15354 if (pipe == PIPE_C)
15355 intel_crtc->num_scalers = 1;
15356 else
15357 intel_crtc->num_scalers = SKL_NUM_SCALERS;
15358
65edccce 15359 skl_init_scalers(dev_priv, intel_crtc, crtc_state);
549e2bfb
CK
15360 }
15361
580503c7 15362 primary = intel_primary_plane_create(dev_priv, pipe);
b079bd17
VS
15363 if (IS_ERR(primary)) {
15364 ret = PTR_ERR(primary);
3d7d6510 15365 goto fail;
b079bd17 15366 }
d97d7b48 15367 intel_crtc->plane_ids_mask |= BIT(primary->id);
3d7d6510 15368
a81d6fa0 15369 for_each_sprite(dev_priv, pipe, sprite) {
b079bd17
VS
15370 struct intel_plane *plane;
15371
580503c7 15372 plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
d2b2cbce 15373 if (IS_ERR(plane)) {
b079bd17
VS
15374 ret = PTR_ERR(plane);
15375 goto fail;
15376 }
d97d7b48 15377 intel_crtc->plane_ids_mask |= BIT(plane->id);
a81d6fa0
VS
15378 }
15379
580503c7 15380 cursor = intel_cursor_plane_create(dev_priv, pipe);
d2b2cbce 15381 if (IS_ERR(cursor)) {
b079bd17 15382 ret = PTR_ERR(cursor);
3d7d6510 15383 goto fail;
b079bd17 15384 }
d97d7b48 15385 intel_crtc->plane_ids_mask |= BIT(cursor->id);
3d7d6510 15386
5ab0d85b 15387 ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
b079bd17
VS
15388 &primary->base, &cursor->base,
15389 &intel_crtc_funcs,
4d5d72b7 15390 "pipe %c", pipe_name(pipe));
3d7d6510
MR
15391 if (ret)
15392 goto fail;
79e53945 15393
80824003 15394 intel_crtc->pipe = pipe;
e3c566df 15395 intel_crtc->plane = primary->plane;
80824003 15396
4b0e333e
CW
15397 intel_crtc->cursor_base = ~0;
15398 intel_crtc->cursor_cntl = ~0;
dc41c154 15399 intel_crtc->cursor_size = ~0;
8d7849db 15400
852eb00d
VS
15401 intel_crtc->wm.cxsr_allowed = true;
15402
22fd0fab
JB
15403 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
15404 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
e2af48c6
VS
15405 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
15406 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
22fd0fab 15407
79e53945 15408 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101 15409
8563b1e8
LL
15410 intel_color_init(&intel_crtc->base);
15411
87b6b101 15412 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
b079bd17
VS
15413
15414 return 0;
3d7d6510
MR
15415
15416fail:
b079bd17
VS
15417 /*
15418 * drm_mode_config_cleanup() will free up any
15419 * crtcs/planes already initialized.
15420 */
f5de6e07 15421 kfree(crtc_state);
3d7d6510 15422 kfree(intel_crtc);
b079bd17
VS
15423
15424 return ret;
79e53945
JB
15425}
15426
752aa88a
JB
15427enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
15428{
15429 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 15430 struct drm_device *dev = connector->base.dev;
752aa88a 15431
51fd371b 15432 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 15433
d3babd3f 15434 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
15435 return INVALID_PIPE;
15436
15437 return to_intel_crtc(encoder->crtc)->pipe;
15438}
15439
08d7b3d1 15440int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 15441 struct drm_file *file)
08d7b3d1 15442{
08d7b3d1 15443 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 15444 struct drm_crtc *drmmode_crtc;
c05422d5 15445 struct intel_crtc *crtc;
08d7b3d1 15446
7707e653 15447 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
71240ed2 15448 if (!drmmode_crtc)
3f2c2057 15449 return -ENOENT;
08d7b3d1 15450
7707e653 15451 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 15452 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 15453
c05422d5 15454 return 0;
08d7b3d1
CW
15455}
15456
66a9278e 15457static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 15458{
66a9278e
SV
15459 struct drm_device *dev = encoder->base.dev;
15460 struct intel_encoder *source_encoder;
79e53945 15461 int index_mask = 0;
79e53945
JB
15462 int entry = 0;
15463
b2784e15 15464 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 15465 if (encoders_cloneable(encoder, source_encoder))
66a9278e
SV
15466 index_mask |= (1 << entry);
15467
79e53945
JB
15468 entry++;
15469 }
4ef69c7a 15470
79e53945
JB
15471 return index_mask;
15472}
15473
646d5772 15474static bool has_edp_a(struct drm_i915_private *dev_priv)
4d302442 15475{
646d5772 15476 if (!IS_MOBILE(dev_priv))
4d302442
CW
15477 return false;
15478
15479 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
15480 return false;
15481
5db94019 15482 if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
15483 return false;
15484
15485 return true;
15486}
15487
6315b5d3 15488static bool intel_crt_present(struct drm_i915_private *dev_priv)
84b4e042 15489{
6315b5d3 15490 if (INTEL_GEN(dev_priv) >= 9)
884497ed
DL
15491 return false;
15492
50a0bc90 15493 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
84b4e042
JB
15494 return false;
15495
920a14b2 15496 if (IS_CHERRYVIEW(dev_priv))
84b4e042
JB
15497 return false;
15498
4f8036a2
TU
15499 if (HAS_PCH_LPT_H(dev_priv) &&
15500 I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
65e472e4
VS
15501 return false;
15502
70ac54d0 15503 /* DDI E can't be used if DDI A requires 4 lanes */
4f8036a2 15504 if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
70ac54d0
VS
15505 return false;
15506
e4abb733 15507 if (!dev_priv->vbt.int_crt_support)
84b4e042
JB
15508 return false;
15509
15510 return true;
15511}
15512
8090ba8c
ID
15513void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
15514{
15515 int pps_num;
15516 int pps_idx;
15517
15518 if (HAS_DDI(dev_priv))
15519 return;
15520 /*
15521 * This w/a is needed at least on CPT/PPT, but to be sure apply it
15522 * everywhere where registers can be write protected.
15523 */
15524 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15525 pps_num = 2;
15526 else
15527 pps_num = 1;
15528
15529 for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
15530 u32 val = I915_READ(PP_CONTROL(pps_idx));
15531
15532 val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
15533 I915_WRITE(PP_CONTROL(pps_idx), val);
15534 }
15535}
15536
44cb734c
ID
15537static void intel_pps_init(struct drm_i915_private *dev_priv)
15538{
cc3f90f0 15539 if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
44cb734c
ID
15540 dev_priv->pps_mmio_base = PCH_PPS_BASE;
15541 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15542 dev_priv->pps_mmio_base = VLV_PPS_BASE;
15543 else
15544 dev_priv->pps_mmio_base = PPS_BASE;
8090ba8c
ID
15545
15546 intel_pps_unlock_regs_wa(dev_priv);
44cb734c
ID
15547}
15548
c39055b0 15549static void intel_setup_outputs(struct drm_i915_private *dev_priv)
79e53945 15550{
4ef69c7a 15551 struct intel_encoder *encoder;
cb0953d7 15552 bool dpd_is_edp = false;
79e53945 15553
44cb734c
ID
15554 intel_pps_init(dev_priv);
15555
97a824e1
ID
15556 /*
15557 * intel_edp_init_connector() depends on this completing first, to
15558 * prevent the registeration of both eDP and LVDS and the incorrect
15559 * sharing of the PPS.
15560 */
c39055b0 15561 intel_lvds_init(dev_priv);
79e53945 15562
6315b5d3 15563 if (intel_crt_present(dev_priv))
c39055b0 15564 intel_crt_init(dev_priv);
cb0953d7 15565
cc3f90f0 15566 if (IS_GEN9_LP(dev_priv)) {
c776eb2e
VK
15567 /*
15568 * FIXME: Broxton doesn't support port detection via the
15569 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
15570 * detect the ports.
15571 */
c39055b0
ACO
15572 intel_ddi_init(dev_priv, PORT_A);
15573 intel_ddi_init(dev_priv, PORT_B);
15574 intel_ddi_init(dev_priv, PORT_C);
c6c794a2 15575
c39055b0 15576 intel_dsi_init(dev_priv);
4f8036a2 15577 } else if (HAS_DDI(dev_priv)) {
0e72a5b5
ED
15578 int found;
15579
de31facd
JB
15580 /*
15581 * Haswell uses DDI functions to detect digital outputs.
15582 * On SKL pre-D0 the strap isn't connected, so we assume
15583 * it's there.
15584 */
77179400 15585 found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
de31facd 15586 /* WaIgnoreDDIAStrap: skl */
0853723b 15587 if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
c39055b0 15588 intel_ddi_init(dev_priv, PORT_A);
0e72a5b5
ED
15589
15590 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
15591 * register */
15592 found = I915_READ(SFUSE_STRAP);
15593
15594 if (found & SFUSE_STRAP_DDIB_DETECTED)
c39055b0 15595 intel_ddi_init(dev_priv, PORT_B);
0e72a5b5 15596 if (found & SFUSE_STRAP_DDIC_DETECTED)
c39055b0 15597 intel_ddi_init(dev_priv, PORT_C);
0e72a5b5 15598 if (found & SFUSE_STRAP_DDID_DETECTED)
c39055b0 15599 intel_ddi_init(dev_priv, PORT_D);
2800e4c2
RV
15600 /*
15601 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
15602 */
0853723b 15603 if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
2800e4c2
RV
15604 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
15605 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
15606 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
c39055b0 15607 intel_ddi_init(dev_priv, PORT_E);
2800e4c2 15608
6e266956 15609 } else if (HAS_PCH_SPLIT(dev_priv)) {
cb0953d7 15610 int found;
dd11bc10 15611 dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
270b3042 15612
646d5772 15613 if (has_edp_a(dev_priv))
c39055b0 15614 intel_dp_init(dev_priv, DP_A, PORT_A);
cb0953d7 15615
dc0fa718 15616 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 15617 /* PCH SDVOB multiplex with HDMIB */
c39055b0 15618 found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
30ad48b7 15619 if (!found)
c39055b0 15620 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
5eb08b69 15621 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
c39055b0 15622 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
30ad48b7
ZW
15623 }
15624
dc0fa718 15625 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
c39055b0 15626 intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
30ad48b7 15627
dc0fa718 15628 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
c39055b0 15629 intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
30ad48b7 15630
5eb08b69 15631 if (I915_READ(PCH_DP_C) & DP_DETECTED)
c39055b0 15632 intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
5eb08b69 15633
270b3042 15634 if (I915_READ(PCH_DP_D) & DP_DETECTED)
c39055b0 15635 intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
920a14b2 15636 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
22f35042 15637 bool has_edp, has_port;
457c52d8 15638
e17ac6db
VS
15639 /*
15640 * The DP_DETECTED bit is the latched state of the DDC
15641 * SDA pin at boot. However since eDP doesn't require DDC
15642 * (no way to plug in a DP->HDMI dongle) the DDC pins for
15643 * eDP ports may have been muxed to an alternate function.
15644 * Thus we can't rely on the DP_DETECTED bit alone to detect
15645 * eDP ports. Consult the VBT as well as DP_DETECTED to
15646 * detect eDP ports.
22f35042
VS
15647 *
15648 * Sadly the straps seem to be missing sometimes even for HDMI
15649 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
15650 * and VBT for the presence of the port. Additionally we can't
15651 * trust the port type the VBT declares as we've seen at least
15652 * HDMI ports that the VBT claim are DP or eDP.
e17ac6db 15653 */
dd11bc10 15654 has_edp = intel_dp_is_edp(dev_priv, PORT_B);
22f35042
VS
15655 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
15656 if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
c39055b0 15657 has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
22f35042 15658 if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15659 intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
585a94b8 15660
dd11bc10 15661 has_edp = intel_dp_is_edp(dev_priv, PORT_C);
22f35042
VS
15662 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
15663 if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
c39055b0 15664 has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
22f35042 15665 if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
c39055b0 15666 intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
19c03924 15667
920a14b2 15668 if (IS_CHERRYVIEW(dev_priv)) {
22f35042
VS
15669 /*
15670 * eDP not supported on port D,
15671 * so no need to worry about it
15672 */
15673 has_port = intel_bios_is_port_present(dev_priv, PORT_D);
15674 if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
c39055b0 15675 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
22f35042 15676 if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
c39055b0 15677 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
9418c1f1
VS
15678 }
15679
c39055b0 15680 intel_dsi_init(dev_priv);
5db94019 15681 } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
27185ae1 15682 bool found = false;
7d57382e 15683
e2debe91 15684 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15685 DRM_DEBUG_KMS("probing SDVOB\n");
c39055b0 15686 found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
9beb5fea 15687 if (!found && IS_G4X(dev_priv)) {
b01f2c3a 15688 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
c39055b0 15689 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
b01f2c3a 15690 }
27185ae1 15691
9beb5fea 15692 if (!found && IS_G4X(dev_priv))
c39055b0 15693 intel_dp_init(dev_priv, DP_B, PORT_B);
725e30ad 15694 }
13520b05
KH
15695
15696 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 15697
e2debe91 15698 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 15699 DRM_DEBUG_KMS("probing SDVOC\n");
c39055b0 15700 found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
b01f2c3a 15701 }
27185ae1 15702
e2debe91 15703 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 15704
9beb5fea 15705 if (IS_G4X(dev_priv)) {
b01f2c3a 15706 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
c39055b0 15707 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
b01f2c3a 15708 }
9beb5fea 15709 if (IS_G4X(dev_priv))
c39055b0 15710 intel_dp_init(dev_priv, DP_C, PORT_C);
725e30ad 15711 }
27185ae1 15712
9beb5fea 15713 if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
c39055b0 15714 intel_dp_init(dev_priv, DP_D, PORT_D);
5db94019 15715 } else if (IS_GEN2(dev_priv))
c39055b0 15716 intel_dvo_init(dev_priv);
79e53945 15717
56b857a5 15718 if (SUPPORTS_TV(dev_priv))
c39055b0 15719 intel_tv_init(dev_priv);
79e53945 15720
c39055b0 15721 intel_psr_init(dev_priv);
7c8f8a70 15722
c39055b0 15723 for_each_intel_encoder(&dev_priv->drm, encoder) {
4ef69c7a
CW
15724 encoder->base.possible_crtcs = encoder->crtc_mask;
15725 encoder->base.possible_clones =
66a9278e 15726 intel_encoder_clones(encoder);
79e53945 15727 }
47356eb6 15728
c39055b0 15729 intel_init_pch_refclk(dev_priv);
270b3042 15730
c39055b0 15731 drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
79e53945
JB
15732}
15733
15734static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
15735{
60a5ca01 15736 struct drm_device *dev = fb->dev;
79e53945 15737 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 15738
ef2d633e 15739 drm_framebuffer_cleanup(fb);
60a5ca01 15740 mutex_lock(&dev->struct_mutex);
ef2d633e 15741 WARN_ON(!intel_fb->obj->framebuffer_references--);
f8c417cd 15742 i915_gem_object_put(intel_fb->obj);
60a5ca01 15743 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15744 kfree(intel_fb);
15745}
15746
15747static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 15748 struct drm_file *file,
79e53945
JB
15749 unsigned int *handle)
15750{
15751 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 15752 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 15753
cc917ab4
CW
15754 if (obj->userptr.mm) {
15755 DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
15756 return -EINVAL;
15757 }
15758
05394f39 15759 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
15760}
15761
86c98588
RV
15762static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
15763 struct drm_file *file,
15764 unsigned flags, unsigned color,
15765 struct drm_clip_rect *clips,
15766 unsigned num_clips)
15767{
15768 struct drm_device *dev = fb->dev;
15769 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
15770 struct drm_i915_gem_object *obj = intel_fb->obj;
15771
15772 mutex_lock(&dev->struct_mutex);
a6a7cc4b
CW
15773 if (obj->pin_display && obj->cache_dirty)
15774 i915_gem_clflush_object(obj, true);
74b4ea1e 15775 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
86c98588
RV
15776 mutex_unlock(&dev->struct_mutex);
15777
15778 return 0;
15779}
15780
79e53945
JB
15781static const struct drm_framebuffer_funcs intel_fb_funcs = {
15782 .destroy = intel_user_framebuffer_destroy,
15783 .create_handle = intel_user_framebuffer_create_handle,
86c98588 15784 .dirty = intel_user_framebuffer_dirty,
79e53945
JB
15785};
15786
b321803d 15787static
920a14b2
TU
15788u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
15789 uint64_t fb_modifier, uint32_t pixel_format)
b321803d 15790{
920a14b2 15791 u32 gen = INTEL_INFO(dev_priv)->gen;
b321803d
DL
15792
15793 if (gen >= 9) {
ac484963
VS
15794 int cpp = drm_format_plane_cpp(pixel_format, 0);
15795
b321803d
DL
15796 /* "The stride in bytes must not exceed the of the size of 8K
15797 * pixels and 32K bytes."
15798 */
ac484963 15799 return min(8192 * cpp, 32768);
920a14b2
TU
15800 } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
15801 !IS_CHERRYVIEW(dev_priv)) {
b321803d
DL
15802 return 32*1024;
15803 } else if (gen >= 4) {
15804 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15805 return 16*1024;
15806 else
15807 return 32*1024;
15808 } else if (gen >= 3) {
15809 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
15810 return 8*1024;
15811 else
15812 return 16*1024;
15813 } else {
15814 /* XXX DSPC is limited to 4k tiled */
15815 return 8*1024;
15816 }
15817}
15818
b5ea642a
SV
15819static int intel_framebuffer_init(struct drm_device *dev,
15820 struct intel_framebuffer *intel_fb,
15821 struct drm_mode_fb_cmd2 *mode_cmd,
15822 struct drm_i915_gem_object *obj)
79e53945 15823{
7b49f948 15824 struct drm_i915_private *dev_priv = to_i915(dev);
c2ff7370 15825 unsigned int tiling = i915_gem_object_get_tiling(obj);
79e53945 15826 int ret;
b321803d 15827 u32 pitch_limit, stride_alignment;
b3c11ac2 15828 struct drm_format_name_buf format_name;
79e53945 15829
dd4916c5
SV
15830 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
15831
2a80eada 15832 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
c2ff7370
VS
15833 /*
15834 * If there's a fence, enforce that
15835 * the fb modifier and tiling mode match.
15836 */
15837 if (tiling != I915_TILING_NONE &&
15838 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
2a80eada
SV
15839 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
15840 return -EINVAL;
15841 }
15842 } else {
c2ff7370 15843 if (tiling == I915_TILING_X) {
2a80eada 15844 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
c2ff7370 15845 } else if (tiling == I915_TILING_Y) {
2a80eada
SV
15846 DRM_DEBUG("No Y tiling for legacy addfb\n");
15847 return -EINVAL;
15848 }
15849 }
15850
9a8f0a12
TU
15851 /* Passed in modifier sanity checking. */
15852 switch (mode_cmd->modifier[0]) {
15853 case I915_FORMAT_MOD_Y_TILED:
15854 case I915_FORMAT_MOD_Yf_TILED:
6315b5d3 15855 if (INTEL_GEN(dev_priv) < 9) {
9a8f0a12
TU
15856 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
15857 mode_cmd->modifier[0]);
15858 return -EINVAL;
15859 }
15860 case DRM_FORMAT_MOD_NONE:
15861 case I915_FORMAT_MOD_X_TILED:
15862 break;
15863 default:
c0f40428
JB
15864 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
15865 mode_cmd->modifier[0]);
57cd6508 15866 return -EINVAL;
c16ed4be 15867 }
57cd6508 15868
c2ff7370
VS
15869 /*
15870 * gen2/3 display engine uses the fence if present,
15871 * so the tiling mode must match the fb modifier exactly.
15872 */
15873 if (INTEL_INFO(dev_priv)->gen < 4 &&
15874 tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
15875 DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
15876 return -EINVAL;
15877 }
15878
7b49f948
VS
15879 stride_alignment = intel_fb_stride_alignment(dev_priv,
15880 mode_cmd->modifier[0],
b321803d
DL
15881 mode_cmd->pixel_format);
15882 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
15883 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
15884 mode_cmd->pitches[0], stride_alignment);
57cd6508 15885 return -EINVAL;
c16ed4be 15886 }
57cd6508 15887
920a14b2 15888 pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
b321803d 15889 mode_cmd->pixel_format);
a35cdaa0 15890 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
15891 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
15892 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 15893 "tiled" : "linear",
a35cdaa0 15894 mode_cmd->pitches[0], pitch_limit);
5d7bd705 15895 return -EINVAL;
c16ed4be 15896 }
5d7bd705 15897
c2ff7370
VS
15898 /*
15899 * If there's a fence, enforce that
15900 * the fb pitch and fence stride match.
15901 */
15902 if (tiling != I915_TILING_NONE &&
3e510a8e 15903 mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
c16ed4be 15904 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
3e510a8e
CW
15905 mode_cmd->pitches[0],
15906 i915_gem_object_get_stride(obj));
5d7bd705 15907 return -EINVAL;
c16ed4be 15908 }
5d7bd705 15909
57779d06 15910 /* Reject formats not supported by any plane early. */
308e5bcb 15911 switch (mode_cmd->pixel_format) {
57779d06 15912 case DRM_FORMAT_C8:
04b3924d
VS
15913 case DRM_FORMAT_RGB565:
15914 case DRM_FORMAT_XRGB8888:
15915 case DRM_FORMAT_ARGB8888:
57779d06
VS
15916 break;
15917 case DRM_FORMAT_XRGB1555:
6315b5d3 15918 if (INTEL_GEN(dev_priv) > 3) {
b3c11ac2
EE
15919 DRM_DEBUG("unsupported pixel format: %s\n",
15920 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15921 return -EINVAL;
c16ed4be 15922 }
57779d06 15923 break;
57779d06 15924 case DRM_FORMAT_ABGR8888:
920a14b2 15925 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
6315b5d3 15926 INTEL_GEN(dev_priv) < 9) {
b3c11ac2
EE
15927 DRM_DEBUG("unsupported pixel format: %s\n",
15928 drm_get_format_name(mode_cmd->pixel_format, &format_name));
6c0fd451
DL
15929 return -EINVAL;
15930 }
15931 break;
15932 case DRM_FORMAT_XBGR8888:
04b3924d 15933 case DRM_FORMAT_XRGB2101010:
57779d06 15934 case DRM_FORMAT_XBGR2101010:
6315b5d3 15935 if (INTEL_GEN(dev_priv) < 4) {
b3c11ac2
EE
15936 DRM_DEBUG("unsupported pixel format: %s\n",
15937 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15938 return -EINVAL;
c16ed4be 15939 }
b5626747 15940 break;
7531208b 15941 case DRM_FORMAT_ABGR2101010:
920a14b2 15942 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
b3c11ac2
EE
15943 DRM_DEBUG("unsupported pixel format: %s\n",
15944 drm_get_format_name(mode_cmd->pixel_format, &format_name));
7531208b
DL
15945 return -EINVAL;
15946 }
15947 break;
04b3924d
VS
15948 case DRM_FORMAT_YUYV:
15949 case DRM_FORMAT_UYVY:
15950 case DRM_FORMAT_YVYU:
15951 case DRM_FORMAT_VYUY:
6315b5d3 15952 if (INTEL_GEN(dev_priv) < 5) {
b3c11ac2
EE
15953 DRM_DEBUG("unsupported pixel format: %s\n",
15954 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57779d06 15955 return -EINVAL;
c16ed4be 15956 }
57cd6508
CW
15957 break;
15958 default:
b3c11ac2
EE
15959 DRM_DEBUG("unsupported pixel format: %s\n",
15960 drm_get_format_name(mode_cmd->pixel_format, &format_name));
57cd6508
CW
15961 return -EINVAL;
15962 }
15963
90f9a336
VS
15964 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
15965 if (mode_cmd->offsets[0] != 0)
15966 return -EINVAL;
15967
c7d73f6a
SV
15968 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
15969 intel_fb->obj = obj;
15970
6687c906
VS
15971 ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
15972 if (ret)
15973 return ret;
2d7a215f 15974
79e53945
JB
15975 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
15976 if (ret) {
15977 DRM_ERROR("framebuffer init failed %d\n", ret);
15978 return ret;
15979 }
15980
0b05e1e0
VS
15981 intel_fb->obj->framebuffer_references++;
15982
79e53945
JB
15983 return 0;
15984}
15985
79e53945
JB
15986static struct drm_framebuffer *
15987intel_user_framebuffer_create(struct drm_device *dev,
15988 struct drm_file *filp,
1eb83451 15989 const struct drm_mode_fb_cmd2 *user_mode_cmd)
79e53945 15990{
dcb1394e 15991 struct drm_framebuffer *fb;
05394f39 15992 struct drm_i915_gem_object *obj;
76dc3769 15993 struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
79e53945 15994
03ac0642
CW
15995 obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
15996 if (!obj)
cce13ff7 15997 return ERR_PTR(-ENOENT);
79e53945 15998
92907cbb 15999 fb = intel_framebuffer_create(dev, &mode_cmd, obj);
dcb1394e 16000 if (IS_ERR(fb))
f0cd5182 16001 i915_gem_object_put(obj);
dcb1394e
LW
16002
16003 return fb;
79e53945
JB
16004}
16005
79e53945 16006static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 16007 .fb_create = intel_user_framebuffer_create,
0632fef6 16008 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
16009 .atomic_check = intel_atomic_check,
16010 .atomic_commit = intel_atomic_commit,
de419ab6
ML
16011 .atomic_state_alloc = intel_atomic_state_alloc,
16012 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
16013};
16014
88212941
ID
16015/**
16016 * intel_init_display_hooks - initialize the display modesetting hooks
16017 * @dev_priv: device private
16018 */
16019void intel_init_display_hooks(struct drm_i915_private *dev_priv)
e70236a8 16020{
88212941 16021 if (INTEL_INFO(dev_priv)->gen >= 9) {
bc8d7dff 16022 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16023 dev_priv->display.get_initial_plane_config =
16024 skylake_get_initial_plane_config;
bc8d7dff
DL
16025 dev_priv->display.crtc_compute_clock =
16026 haswell_crtc_compute_clock;
16027 dev_priv->display.crtc_enable = haswell_crtc_enable;
16028 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16029 } else if (HAS_DDI(dev_priv)) {
0e8ffe1b 16030 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
16031 dev_priv->display.get_initial_plane_config =
16032 ironlake_get_initial_plane_config;
797d0259
ACO
16033 dev_priv->display.crtc_compute_clock =
16034 haswell_crtc_compute_clock;
4f771f10
PZ
16035 dev_priv->display.crtc_enable = haswell_crtc_enable;
16036 dev_priv->display.crtc_disable = haswell_crtc_disable;
88212941 16037 } else if (HAS_PCH_SPLIT(dev_priv)) {
0e8ffe1b 16038 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
16039 dev_priv->display.get_initial_plane_config =
16040 ironlake_get_initial_plane_config;
3fb37703
ACO
16041 dev_priv->display.crtc_compute_clock =
16042 ironlake_crtc_compute_clock;
76e5a89c
SV
16043 dev_priv->display.crtc_enable = ironlake_crtc_enable;
16044 dev_priv->display.crtc_disable = ironlake_crtc_disable;
65b3d6a9 16045 } else if (IS_CHERRYVIEW(dev_priv)) {
89b667f8 16046 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16047 dev_priv->display.get_initial_plane_config =
16048 i9xx_get_initial_plane_config;
65b3d6a9
ACO
16049 dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
16050 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16051 dev_priv->display.crtc_disable = i9xx_crtc_disable;
16052 } else if (IS_VALLEYVIEW(dev_priv)) {
16053 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16054 dev_priv->display.get_initial_plane_config =
16055 i9xx_get_initial_plane_config;
16056 dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
89b667f8
JB
16057 dev_priv->display.crtc_enable = valleyview_crtc_enable;
16058 dev_priv->display.crtc_disable = i9xx_crtc_disable;
19ec6693
ACO
16059 } else if (IS_G4X(dev_priv)) {
16060 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16061 dev_priv->display.get_initial_plane_config =
16062 i9xx_get_initial_plane_config;
16063 dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
16064 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16065 dev_priv->display.crtc_disable = i9xx_crtc_disable;
70e8aa21
ACO
16066 } else if (IS_PINEVIEW(dev_priv)) {
16067 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16068 dev_priv->display.get_initial_plane_config =
16069 i9xx_get_initial_plane_config;
16070 dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
16071 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16072 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52 16073 } else if (!IS_GEN2(dev_priv)) {
0e8ffe1b 16074 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
16075 dev_priv->display.get_initial_plane_config =
16076 i9xx_get_initial_plane_config;
d6dfee7a 16077 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
SV
16078 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16079 dev_priv->display.crtc_disable = i9xx_crtc_disable;
81c97f52
ACO
16080 } else {
16081 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
16082 dev_priv->display.get_initial_plane_config =
16083 i9xx_get_initial_plane_config;
16084 dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
16085 dev_priv->display.crtc_enable = i9xx_crtc_enable;
16086 dev_priv->display.crtc_disable = i9xx_crtc_disable;
f564048e 16087 }
e70236a8 16088
e70236a8 16089 /* Returns the core display clock speed */
88212941 16090 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1652d19e
VS
16091 dev_priv->display.get_display_clock_speed =
16092 skylake_get_display_clock_speed;
89b3c3c7 16093 else if (IS_GEN9_LP(dev_priv))
acd3f3d3
BP
16094 dev_priv->display.get_display_clock_speed =
16095 broxton_get_display_clock_speed;
88212941 16096 else if (IS_BROADWELL(dev_priv))
1652d19e
VS
16097 dev_priv->display.get_display_clock_speed =
16098 broadwell_get_display_clock_speed;
88212941 16099 else if (IS_HASWELL(dev_priv))
1652d19e
VS
16100 dev_priv->display.get_display_clock_speed =
16101 haswell_get_display_clock_speed;
88212941 16102 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
25eb05fc
JB
16103 dev_priv->display.get_display_clock_speed =
16104 valleyview_get_display_clock_speed;
88212941 16105 else if (IS_GEN5(dev_priv))
b37a6434
VS
16106 dev_priv->display.get_display_clock_speed =
16107 ilk_get_display_clock_speed;
88212941
ID
16108 else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
16109 IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
e70236a8
JB
16110 dev_priv->display.get_display_clock_speed =
16111 i945_get_display_clock_speed;
88212941 16112 else if (IS_GM45(dev_priv))
34edce2f
VS
16113 dev_priv->display.get_display_clock_speed =
16114 gm45_get_display_clock_speed;
88212941 16115 else if (IS_CRESTLINE(dev_priv))
34edce2f
VS
16116 dev_priv->display.get_display_clock_speed =
16117 i965gm_get_display_clock_speed;
88212941 16118 else if (IS_PINEVIEW(dev_priv))
34edce2f
VS
16119 dev_priv->display.get_display_clock_speed =
16120 pnv_get_display_clock_speed;
88212941 16121 else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
34edce2f
VS
16122 dev_priv->display.get_display_clock_speed =
16123 g33_get_display_clock_speed;
88212941 16124 else if (IS_I915G(dev_priv))
e70236a8
JB
16125 dev_priv->display.get_display_clock_speed =
16126 i915_get_display_clock_speed;
88212941 16127 else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
e70236a8
JB
16128 dev_priv->display.get_display_clock_speed =
16129 i9xx_misc_get_display_clock_speed;
88212941 16130 else if (IS_I915GM(dev_priv))
e70236a8
JB
16131 dev_priv->display.get_display_clock_speed =
16132 i915gm_get_display_clock_speed;
88212941 16133 else if (IS_I865G(dev_priv))
e70236a8
JB
16134 dev_priv->display.get_display_clock_speed =
16135 i865_get_display_clock_speed;
88212941 16136 else if (IS_I85X(dev_priv))
e70236a8 16137 dev_priv->display.get_display_clock_speed =
1b1d2716 16138 i85x_get_display_clock_speed;
623e01e5 16139 else { /* 830 */
88212941 16140 WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
16141 dev_priv->display.get_display_clock_speed =
16142 i830_get_display_clock_speed;
623e01e5 16143 }
e70236a8 16144
88212941 16145 if (IS_GEN5(dev_priv)) {
3bb11b53 16146 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
88212941 16147 } else if (IS_GEN6(dev_priv)) {
3bb11b53 16148 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
88212941 16149 } else if (IS_IVYBRIDGE(dev_priv)) {
3bb11b53
SJ
16150 /* FIXME: detect B0+ stepping and use auto training */
16151 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
88212941 16152 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3bb11b53 16153 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
445e780b
VS
16154 }
16155
16156 if (IS_BROADWELL(dev_priv)) {
16157 dev_priv->display.modeset_commit_cdclk =
16158 broadwell_modeset_commit_cdclk;
16159 dev_priv->display.modeset_calc_cdclk =
16160 broadwell_modeset_calc_cdclk;
88212941 16161 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
27c329ed
ML
16162 dev_priv->display.modeset_commit_cdclk =
16163 valleyview_modeset_commit_cdclk;
16164 dev_priv->display.modeset_calc_cdclk =
16165 valleyview_modeset_calc_cdclk;
89b3c3c7 16166 } else if (IS_GEN9_LP(dev_priv)) {
27c329ed 16167 dev_priv->display.modeset_commit_cdclk =
324513c0 16168 bxt_modeset_commit_cdclk;
27c329ed 16169 dev_priv->display.modeset_calc_cdclk =
324513c0 16170 bxt_modeset_calc_cdclk;
c89e39f3
CT
16171 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
16172 dev_priv->display.modeset_commit_cdclk =
16173 skl_modeset_commit_cdclk;
16174 dev_priv->display.modeset_calc_cdclk =
16175 skl_modeset_calc_cdclk;
e70236a8 16176 }
5a21b665 16177
27082493
L
16178 if (dev_priv->info.gen >= 9)
16179 dev_priv->display.update_crtcs = skl_update_crtcs;
16180 else
16181 dev_priv->display.update_crtcs = intel_update_crtcs;
16182
5a21b665
SV
16183 switch (INTEL_INFO(dev_priv)->gen) {
16184 case 2:
16185 dev_priv->display.queue_flip = intel_gen2_queue_flip;
16186 break;
16187
16188 case 3:
16189 dev_priv->display.queue_flip = intel_gen3_queue_flip;
16190 break;
16191
16192 case 4:
16193 case 5:
16194 dev_priv->display.queue_flip = intel_gen4_queue_flip;
16195 break;
16196
16197 case 6:
16198 dev_priv->display.queue_flip = intel_gen6_queue_flip;
16199 break;
16200 case 7:
16201 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
16202 dev_priv->display.queue_flip = intel_gen7_queue_flip;
16203 break;
16204 case 9:
16205 /* Drop through - unsupported since execlist only. */
16206 default:
16207 /* Default just returns -ENODEV to indicate unsupported */
16208 dev_priv->display.queue_flip = intel_default_queue_flip;
16209 }
e70236a8
JB
16210}
16211
b690e96c
JB
16212/*
16213 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
16214 * resume, or other times. This quirk makes sure that's the case for
16215 * affected systems.
16216 */
0206e353 16217static void quirk_pipea_force(struct drm_device *dev)
b690e96c 16218{
fac5e23e 16219 struct drm_i915_private *dev_priv = to_i915(dev);
b690e96c
JB
16220
16221 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 16222 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
16223}
16224
b6b5d049
VS
16225static void quirk_pipeb_force(struct drm_device *dev)
16226{
fac5e23e 16227 struct drm_i915_private *dev_priv = to_i915(dev);
b6b5d049
VS
16228
16229 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
16230 DRM_INFO("applying pipe b force quirk\n");
16231}
16232
435793df
KP
16233/*
16234 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
16235 */
16236static void quirk_ssc_force_disable(struct drm_device *dev)
16237{
fac5e23e 16238 struct drm_i915_private *dev_priv = to_i915(dev);
435793df 16239 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 16240 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
16241}
16242
4dca20ef 16243/*
5a15ab5b
CE
16244 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
16245 * brightness value
4dca20ef
CE
16246 */
16247static void quirk_invert_brightness(struct drm_device *dev)
16248{
fac5e23e 16249 struct drm_i915_private *dev_priv = to_i915(dev);
4dca20ef 16250 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 16251 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
16252}
16253
9c72cc6f
SD
16254/* Some VBT's incorrectly indicate no backlight is present */
16255static void quirk_backlight_present(struct drm_device *dev)
16256{
fac5e23e 16257 struct drm_i915_private *dev_priv = to_i915(dev);
9c72cc6f
SD
16258 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
16259 DRM_INFO("applying backlight present quirk\n");
16260}
16261
b690e96c
JB
16262struct intel_quirk {
16263 int device;
16264 int subsystem_vendor;
16265 int subsystem_device;
16266 void (*hook)(struct drm_device *dev);
16267};
16268
5f85f176
EE
16269/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
16270struct intel_dmi_quirk {
16271 void (*hook)(struct drm_device *dev);
16272 const struct dmi_system_id (*dmi_id_list)[];
16273};
16274
16275static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
16276{
16277 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
16278 return 1;
16279}
16280
16281static const struct intel_dmi_quirk intel_dmi_quirks[] = {
16282 {
16283 .dmi_id_list = &(const struct dmi_system_id[]) {
16284 {
16285 .callback = intel_dmi_reverse_brightness,
16286 .ident = "NCR Corporation",
16287 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
16288 DMI_MATCH(DMI_PRODUCT_NAME, ""),
16289 },
16290 },
16291 { } /* terminating entry */
16292 },
16293 .hook = quirk_invert_brightness,
16294 },
16295};
16296
c43b5634 16297static struct intel_quirk intel_quirks[] = {
b690e96c
JB
16298 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
16299 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
16300
b690e96c
JB
16301 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
16302 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
16303
5f080c0f
VS
16304 /* 830 needs to leave pipe A & dpll A up */
16305 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
16306
b6b5d049
VS
16307 /* 830 needs to leave pipe B & dpll B up */
16308 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
16309
435793df
KP
16310 /* Lenovo U160 cannot use SSC on LVDS */
16311 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
16312
16313 /* Sony Vaio Y cannot use SSC on LVDS */
16314 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 16315
be505f64
AH
16316 /* Acer Aspire 5734Z must invert backlight brightness */
16317 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
16318
16319 /* Acer/eMachines G725 */
16320 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
16321
16322 /* Acer/eMachines e725 */
16323 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
16324
16325 /* Acer/Packard Bell NCL20 */
16326 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
16327
16328 /* Acer Aspire 4736Z */
16329 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
16330
16331 /* Acer Aspire 5336 */
16332 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
16333
16334 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
16335 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 16336
dfb3d47b
SD
16337 /* Acer C720 Chromebook (Core i3 4005U) */
16338 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
16339
b2a9601c 16340 /* Apple Macbook 2,1 (Core 2 T7400) */
16341 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
16342
1b9448b0
JN
16343 /* Apple Macbook 4,1 */
16344 { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
16345
d4967d8c
SD
16346 /* Toshiba CB35 Chromebook (Celeron 2955U) */
16347 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
16348
16349 /* HP Chromebook 14 (Celeron 2955U) */
16350 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
16351
16352 /* Dell Chromebook 11 */
16353 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
9be64eee
JN
16354
16355 /* Dell Chromebook 11 (2015 version) */
16356 { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
16357};
16358
16359static void intel_init_quirks(struct drm_device *dev)
16360{
16361 struct pci_dev *d = dev->pdev;
16362 int i;
16363
16364 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
16365 struct intel_quirk *q = &intel_quirks[i];
16366
16367 if (d->device == q->device &&
16368 (d->subsystem_vendor == q->subsystem_vendor ||
16369 q->subsystem_vendor == PCI_ANY_ID) &&
16370 (d->subsystem_device == q->subsystem_device ||
16371 q->subsystem_device == PCI_ANY_ID))
16372 q->hook(dev);
16373 }
5f85f176
EE
16374 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
16375 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
16376 intel_dmi_quirks[i].hook(dev);
16377 }
b690e96c
JB
16378}
16379
9cce37f4 16380/* Disable the VGA plane that we never use */
29b74b7f 16381static void i915_disable_vga(struct drm_i915_private *dev_priv)
9cce37f4 16382{
52a05c30 16383 struct pci_dev *pdev = dev_priv->drm.pdev;
9cce37f4 16384 u8 sr1;
920a14b2 16385 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
9cce37f4 16386
2b37c616 16387 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
52a05c30 16388 vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 16389 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
16390 sr1 = inb(VGA_SR_DATA);
16391 outb(sr1 | 1<<5, VGA_SR_DATA);
52a05c30 16392 vga_put(pdev, VGA_RSRC_LEGACY_IO);
9cce37f4
JB
16393 udelay(300);
16394
01f5a626 16395 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
16396 POSTING_READ(vga_reg);
16397}
16398
f817586c
SV
16399void intel_modeset_init_hw(struct drm_device *dev)
16400{
fac5e23e 16401 struct drm_i915_private *dev_priv = to_i915(dev);
1a617b77 16402
4c75b940 16403 intel_update_cdclk(dev_priv);
1a617b77
ML
16404
16405 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
16406
46f16e63 16407 intel_init_clock_gating(dev_priv);
f817586c
SV
16408}
16409
d93c0372
MR
16410/*
16411 * Calculate what we think the watermarks should be for the state we've read
16412 * out of the hardware and then immediately program those watermarks so that
16413 * we ensure the hardware settings match our internal state.
16414 *
16415 * We can calculate what we think WM's should be by creating a duplicate of the
16416 * current state (which was constructed during hardware readout) and running it
16417 * through the atomic check code to calculate new watermark values in the
16418 * state object.
16419 */
16420static void sanitize_watermarks(struct drm_device *dev)
16421{
16422 struct drm_i915_private *dev_priv = to_i915(dev);
16423 struct drm_atomic_state *state;
ccf010fb 16424 struct intel_atomic_state *intel_state;
d93c0372
MR
16425 struct drm_crtc *crtc;
16426 struct drm_crtc_state *cstate;
16427 struct drm_modeset_acquire_ctx ctx;
16428 int ret;
16429 int i;
16430
16431 /* Only supported on platforms that use atomic watermark design */
ed4a6a7c 16432 if (!dev_priv->display.optimize_watermarks)
d93c0372
MR
16433 return;
16434
16435 /*
16436 * We need to hold connection_mutex before calling duplicate_state so
16437 * that the connector loop is protected.
16438 */
16439 drm_modeset_acquire_init(&ctx, 0);
16440retry:
0cd1262d 16441 ret = drm_modeset_lock_all_ctx(dev, &ctx);
d93c0372
MR
16442 if (ret == -EDEADLK) {
16443 drm_modeset_backoff(&ctx);
16444 goto retry;
16445 } else if (WARN_ON(ret)) {
0cd1262d 16446 goto fail;
d93c0372
MR
16447 }
16448
16449 state = drm_atomic_helper_duplicate_state(dev, &ctx);
16450 if (WARN_ON(IS_ERR(state)))
0cd1262d 16451 goto fail;
d93c0372 16452
ccf010fb
ML
16453 intel_state = to_intel_atomic_state(state);
16454
ed4a6a7c
MR
16455 /*
16456 * Hardware readout is the only time we don't want to calculate
16457 * intermediate watermarks (since we don't trust the current
16458 * watermarks).
16459 */
ccf010fb 16460 intel_state->skip_intermediate_wm = true;
ed4a6a7c 16461
d93c0372
MR
16462 ret = intel_atomic_check(dev, state);
16463 if (ret) {
16464 /*
16465 * If we fail here, it means that the hardware appears to be
16466 * programmed in a way that shouldn't be possible, given our
16467 * understanding of watermark requirements. This might mean a
16468 * mistake in the hardware readout code or a mistake in the
16469 * watermark calculations for a given platform. Raise a WARN
16470 * so that this is noticeable.
16471 *
16472 * If this actually happens, we'll have to just leave the
16473 * BIOS-programmed watermarks untouched and hope for the best.
16474 */
16475 WARN(true, "Could not determine valid watermarks for inherited state\n");
b9a1b717 16476 goto put_state;
d93c0372
MR
16477 }
16478
16479 /* Write calculated watermark values back */
d93c0372
MR
16480 for_each_crtc_in_state(state, crtc, cstate, i) {
16481 struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
16482
ed4a6a7c 16483 cs->wm.need_postvbl_update = true;
ccf010fb 16484 dev_priv->display.optimize_watermarks(intel_state, cs);
d93c0372
MR
16485 }
16486
b9a1b717 16487put_state:
0853695c 16488 drm_atomic_state_put(state);
0cd1262d 16489fail:
d93c0372
MR
16490 drm_modeset_drop_locks(&ctx);
16491 drm_modeset_acquire_fini(&ctx);
16492}
16493
b079bd17 16494int intel_modeset_init(struct drm_device *dev)
79e53945 16495{
72e96d64
JL
16496 struct drm_i915_private *dev_priv = to_i915(dev);
16497 struct i915_ggtt *ggtt = &dev_priv->ggtt;
8cc87b75 16498 enum pipe pipe;
46f297fb 16499 struct intel_crtc *crtc;
79e53945
JB
16500
16501 drm_mode_config_init(dev);
16502
16503 dev->mode_config.min_width = 0;
16504 dev->mode_config.min_height = 0;
16505
019d96cb
DA
16506 dev->mode_config.preferred_depth = 24;
16507 dev->mode_config.prefer_shadow = 1;
16508
25bab385
TU
16509 dev->mode_config.allow_fb_modifiers = true;
16510
e6ecefaa 16511 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 16512
b690e96c
JB
16513 intel_init_quirks(dev);
16514
62d75df7 16515 intel_init_pm(dev_priv);
1fa61106 16516
b7f05d4a 16517 if (INTEL_INFO(dev_priv)->num_pipes == 0)
b079bd17 16518 return 0;
e3c74757 16519
69f92f67
LW
16520 /*
16521 * There may be no VBT; and if the BIOS enabled SSC we can
16522 * just keep using it to avoid unnecessary flicker. Whereas if the
16523 * BIOS isn't using it, don't assume it will work even if the VBT
16524 * indicates as much.
16525 */
6e266956 16526 if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
69f92f67
LW
16527 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
16528 DREF_SSC1_ENABLE);
16529
16530 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
16531 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
16532 bios_lvds_use_ssc ? "en" : "dis",
16533 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
16534 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
16535 }
16536 }
16537
5db94019 16538 if (IS_GEN2(dev_priv)) {
a6c45cf0
CW
16539 dev->mode_config.max_width = 2048;
16540 dev->mode_config.max_height = 2048;
5db94019 16541 } else if (IS_GEN3(dev_priv)) {
5e4d6fa7
KP
16542 dev->mode_config.max_width = 4096;
16543 dev->mode_config.max_height = 4096;
79e53945 16544 } else {
a6c45cf0
CW
16545 dev->mode_config.max_width = 8192;
16546 dev->mode_config.max_height = 8192;
79e53945 16547 }
068be561 16548
50a0bc90
TU
16549 if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
16550 dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
dc41c154 16551 dev->mode_config.cursor_height = 1023;
5db94019 16552 } else if (IS_GEN2(dev_priv)) {
068be561
DL
16553 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
16554 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
16555 } else {
16556 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
16557 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
16558 }
16559
72e96d64 16560 dev->mode_config.fb_base = ggtt->mappable_base;
79e53945 16561
28c97730 16562 DRM_DEBUG_KMS("%d display pipe%s available.\n",
b7f05d4a
TU
16563 INTEL_INFO(dev_priv)->num_pipes,
16564 INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
79e53945 16565
055e393f 16566 for_each_pipe(dev_priv, pipe) {
b079bd17
VS
16567 int ret;
16568
5ab0d85b 16569 ret = intel_crtc_init(dev_priv, pipe);
b079bd17
VS
16570 if (ret) {
16571 drm_mode_config_cleanup(dev);
16572 return ret;
16573 }
79e53945
JB
16574 }
16575
bfa7df01 16576 intel_update_czclk(dev_priv);
4c75b940 16577 intel_update_cdclk(dev_priv);
6a259b1f 16578 dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
bfa7df01 16579
e72f9fbf 16580 intel_shared_dpll_init(dev);
ee7b9f93 16581
b2045352 16582 if (dev_priv->max_cdclk_freq == 0)
4c75b940 16583 intel_update_max_cdclk(dev_priv);
b2045352 16584
9cce37f4 16585 /* Just disable it once at startup */
29b74b7f 16586 i915_disable_vga(dev_priv);
c39055b0 16587 intel_setup_outputs(dev_priv);
11be49eb 16588
6e9f798d 16589 drm_modeset_lock_all(dev);
043e9bda 16590 intel_modeset_setup_hw_state(dev);
6e9f798d 16591 drm_modeset_unlock_all(dev);
46f297fb 16592
d3fcc808 16593 for_each_intel_crtc(dev, crtc) {
eeebeac5
ML
16594 struct intel_initial_plane_config plane_config = {};
16595
46f297fb
JB
16596 if (!crtc->active)
16597 continue;
16598
46f297fb 16599 /*
46f297fb
JB
16600 * Note that reserving the BIOS fb up front prevents us
16601 * from stuffing other stolen allocations like the ring
16602 * on top. This prevents some ugliness at boot time, and
16603 * can even allow for smooth boot transitions if the BIOS
16604 * fb is large enough for the active pipe configuration.
16605 */
eeebeac5
ML
16606 dev_priv->display.get_initial_plane_config(crtc,
16607 &plane_config);
16608
16609 /*
16610 * If the fb is shared between multiple heads, we'll
16611 * just get the first one.
16612 */
16613 intel_find_initial_plane_obj(crtc, &plane_config);
46f297fb 16614 }
d93c0372
MR
16615
16616 /*
16617 * Make sure hardware watermarks really match the state we read out.
16618 * Note that we need to do this after reconstructing the BIOS fb's
16619 * since the watermark calculation done here will use pstate->fb.
16620 */
16621 sanitize_watermarks(dev);
b079bd17
VS
16622
16623 return 0;
2c7111db
CW
16624}
16625
7fad798e
SV
16626static void intel_enable_pipe_a(struct drm_device *dev)
16627{
16628 struct intel_connector *connector;
16629 struct drm_connector *crt = NULL;
16630 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 16631 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
SV
16632
16633 /* We can't just switch on the pipe A, we need to set things up with a
16634 * proper mode and output configuration. As a gross hack, enable pipe A
16635 * by enabling the load detect pipe once. */
3a3371ff 16636 for_each_intel_connector(dev, connector) {
7fad798e
SV
16637 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
16638 crt = &connector->base;
16639 break;
16640 }
16641 }
16642
16643 if (!crt)
16644 return;
16645
208bf9fd 16646 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 16647 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
SV
16648}
16649
fa555837
SV
16650static bool
16651intel_check_plane_mapping(struct intel_crtc *crtc)
16652{
b7f05d4a 16653 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
649636ef 16654 u32 val;
fa555837 16655
b7f05d4a 16656 if (INTEL_INFO(dev_priv)->num_pipes == 1)
fa555837
SV
16657 return true;
16658
649636ef 16659 val = I915_READ(DSPCNTR(!crtc->plane));
fa555837
SV
16660
16661 if ((val & DISPLAY_PLANE_ENABLE) &&
16662 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
16663 return false;
16664
16665 return true;
16666}
16667
02e93c35
VS
16668static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
16669{
16670 struct drm_device *dev = crtc->base.dev;
16671 struct intel_encoder *encoder;
16672
16673 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
16674 return true;
16675
16676 return false;
16677}
16678
496b0fc3
ML
16679static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
16680{
16681 struct drm_device *dev = encoder->base.dev;
16682 struct intel_connector *connector;
16683
16684 for_each_connector_on_encoder(dev, &encoder->base, connector)
16685 return connector;
16686
16687 return NULL;
16688}
16689
a168f5b3
VS
16690static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
16691 enum transcoder pch_transcoder)
16692{
16693 return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
16694 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
16695}
16696
24929352
SV
16697static void intel_sanitize_crtc(struct intel_crtc *crtc)
16698{
16699 struct drm_device *dev = crtc->base.dev;
fac5e23e 16700 struct drm_i915_private *dev_priv = to_i915(dev);
4d1de975 16701 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
24929352 16702
24929352 16703 /* Clear any frame start delays used for debugging left by the BIOS */
4d1de975
JN
16704 if (!transcoder_is_dsi(cpu_transcoder)) {
16705 i915_reg_t reg = PIPECONF(cpu_transcoder);
16706
16707 I915_WRITE(reg,
16708 I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
16709 }
24929352 16710
d3eaf884 16711 /* restore vblank interrupts to correct state */
9625604c 16712 drm_crtc_vblank_reset(&crtc->base);
d297e103 16713 if (crtc->active) {
f9cd7b88
VS
16714 struct intel_plane *plane;
16715
9625604c 16716 drm_crtc_vblank_on(&crtc->base);
f9cd7b88
VS
16717
16718 /* Disable everything but the primary plane */
16719 for_each_intel_plane_on_crtc(dev, crtc, plane) {
16720 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
16721 continue;
16722
16723 plane->disable_plane(&plane->base, &crtc->base);
16724 }
9625604c 16725 }
d3eaf884 16726
24929352 16727 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
SV
16728 * disable the crtc (and hence change the state) if it is wrong. Note
16729 * that gen4+ has a fixed plane -> pipe mapping. */
6315b5d3 16730 if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
24929352
SV
16731 bool plane;
16732
78108b7c
VS
16733 DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
16734 crtc->base.base.id, crtc->base.name);
24929352
SV
16735
16736 /* Pipe has the wrong plane attached and the plane is active.
16737 * Temporarily change the plane mapping and disable everything
16738 * ... */
16739 plane = crtc->plane;
936e71e3 16740 to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
24929352 16741 crtc->plane = !plane;
b17d48e2 16742 intel_crtc_disable_noatomic(&crtc->base);
24929352 16743 crtc->plane = plane;
24929352 16744 }
24929352 16745
7fad798e
SV
16746 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
16747 crtc->pipe == PIPE_A && !crtc->active) {
16748 /* BIOS forgot to enable pipe A, this mostly happens after
16749 * resume. Force-enable the pipe to fix this, the update_dpms
16750 * call below we restore the pipe to the right state, but leave
16751 * the required bits on. */
16752 intel_enable_pipe_a(dev);
16753 }
16754
24929352
SV
16755 /* Adjust the state of the output pipe according to whether we
16756 * have active connectors/encoders. */
842e0307 16757 if (crtc->active && !intel_crtc_has_encoders(crtc))
b17d48e2 16758 intel_crtc_disable_noatomic(&crtc->base);
24929352 16759
49cff963 16760 if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
4cc31489
SV
16761 /*
16762 * We start out with underrun reporting disabled to avoid races.
16763 * For correct bookkeeping mark this on active crtcs.
16764 *
c5ab3bc0
SV
16765 * Also on gmch platforms we dont have any hardware bits to
16766 * disable the underrun reporting. Which means we need to start
16767 * out with underrun reporting disabled also on inactive pipes,
16768 * since otherwise we'll complain about the garbage we read when
16769 * e.g. coming up after runtime pm.
16770 *
4cc31489
SV
16771 * No protection against concurrent access is required - at
16772 * worst a fifo underrun happens which also sets this to false.
16773 */
16774 crtc->cpu_fifo_underrun_disabled = true;
a168f5b3
VS
16775 /*
16776 * We track the PCH trancoder underrun reporting state
16777 * within the crtc. With crtc for pipe A housing the underrun
16778 * reporting state for PCH transcoder A, crtc for pipe B housing
16779 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
16780 * and marking underrun reporting as disabled for the non-existing
16781 * PCH transcoders B and C would prevent enabling the south
16782 * error interrupt (see cpt_can_enable_serr_int()).
16783 */
16784 if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
16785 crtc->pch_fifo_underrun_disabled = true;
4cc31489 16786 }
24929352
SV
16787}
16788
16789static void intel_sanitize_encoder(struct intel_encoder *encoder)
16790{
16791 struct intel_connector *connector;
24929352
SV
16792
16793 /* We need to check both for a crtc link (meaning that the
16794 * encoder is active and trying to read from a pipe) and the
16795 * pipe itself being active. */
16796 bool has_active_crtc = encoder->base.crtc &&
16797 to_intel_crtc(encoder->base.crtc)->active;
16798
496b0fc3
ML
16799 connector = intel_encoder_find_connector(encoder);
16800 if (connector && !has_active_crtc) {
24929352
SV
16801 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
16802 encoder->base.base.id,
8e329a03 16803 encoder->base.name);
24929352
SV
16804
16805 /* Connector is active, but has no active pipe. This is
16806 * fallout from our resume register restoring. Disable
16807 * the encoder manually again. */
16808 if (encoder->base.crtc) {
fd6bbda9
ML
16809 struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
16810
24929352
SV
16811 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
16812 encoder->base.base.id,
8e329a03 16813 encoder->base.name);
fd6bbda9 16814 encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
a62d1497 16815 if (encoder->post_disable)
fd6bbda9 16816 encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
24929352 16817 }
7f1950fb 16818 encoder->base.crtc = NULL;
24929352
SV
16819
16820 /* Inconsistent output/port/pipe state happens presumably due to
16821 * a bug in one of the get_hw_state functions. Or someplace else
16822 * in our code, like the register restore mess on resume. Clamp
16823 * things to off as a safer default. */
fd6bbda9
ML
16824
16825 connector->base.dpms = DRM_MODE_DPMS_OFF;
16826 connector->base.encoder = NULL;
24929352
SV
16827 }
16828 /* Enabled encoders without active connectors will be fixed in
16829 * the crtc fixup. */
16830}
16831
29b74b7f 16832void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
0fde901f 16833{
920a14b2 16834 i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
0fde901f 16835
04098753
ID
16836 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
16837 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
29b74b7f 16838 i915_disable_vga(dev_priv);
04098753
ID
16839 }
16840}
16841
29b74b7f 16842void i915_redisable_vga(struct drm_i915_private *dev_priv)
04098753 16843{
8dc8a27c
PZ
16844 /* This function can be called both from intel_modeset_setup_hw_state or
16845 * at a very early point in our resume sequence, where the power well
16846 * structures are not yet restored. Since this function is at a very
16847 * paranoid "someone might have enabled VGA while we were not looking"
16848 * level, just check if the power well is enabled instead of trying to
16849 * follow the "don't touch the power well if we don't need it" policy
16850 * the rest of the driver uses. */
6392f847 16851 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
16852 return;
16853
29b74b7f 16854 i915_redisable_vga_power_on(dev_priv);
6392f847
ID
16855
16856 intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
0fde901f
KM
16857}
16858
f9cd7b88 16859static bool primary_get_hw_state(struct intel_plane *plane)
98ec7739 16860{
f9cd7b88 16861 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
98ec7739 16862
f9cd7b88 16863 return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
d032ffa0
ML
16864}
16865
f9cd7b88
VS
16866/* FIXME read out full plane state for all planes */
16867static void readout_plane_state(struct intel_crtc *crtc)
d032ffa0 16868{
b26d3ea3 16869 struct drm_plane *primary = crtc->base.primary;
f9cd7b88 16870 struct intel_plane_state *plane_state =
b26d3ea3 16871 to_intel_plane_state(primary->state);
d032ffa0 16872
936e71e3 16873 plane_state->base.visible = crtc->active &&
b26d3ea3
ML
16874 primary_get_hw_state(to_intel_plane(primary));
16875
936e71e3 16876 if (plane_state->base.visible)
b26d3ea3 16877 crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
98ec7739
VS
16878}
16879
30e984df 16880static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352 16881{
fac5e23e 16882 struct drm_i915_private *dev_priv = to_i915(dev);
24929352 16883 enum pipe pipe;
24929352
SV
16884 struct intel_crtc *crtc;
16885 struct intel_encoder *encoder;
16886 struct intel_connector *connector;
5358901f 16887 int i;
24929352 16888
565602d7
ML
16889 dev_priv->active_crtcs = 0;
16890
d3fcc808 16891 for_each_intel_crtc(dev, crtc) {
565602d7
ML
16892 struct intel_crtc_state *crtc_state = crtc->config;
16893 int pixclk = 0;
3b117c8f 16894
ec2dc6a0 16895 __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
565602d7
ML
16896 memset(crtc_state, 0, sizeof(*crtc_state));
16897 crtc_state->base.crtc = &crtc->base;
24929352 16898
565602d7
ML
16899 crtc_state->base.active = crtc_state->base.enable =
16900 dev_priv->display.get_pipe_config(crtc, crtc_state);
16901
16902 crtc->base.enabled = crtc_state->base.enable;
16903 crtc->active = crtc_state->base.active;
16904
16905 if (crtc_state->base.active) {
16906 dev_priv->active_crtcs |= 1 << crtc->pipe;
16907
c89e39f3 16908 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
565602d7 16909 pixclk = ilk_pipe_pixel_rate(crtc_state);
9558d15d 16910 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
565602d7
ML
16911 pixclk = crtc_state->base.adjusted_mode.crtc_clock;
16912 else
16913 WARN_ON(dev_priv->display.modeset_calc_cdclk);
9558d15d
VS
16914
16915 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
16916 if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
16917 pixclk = DIV_ROUND_UP(pixclk * 100, 95);
565602d7
ML
16918 }
16919
16920 dev_priv->min_pixclk[crtc->pipe] = pixclk;
b70709a6 16921
f9cd7b88 16922 readout_plane_state(crtc);
24929352 16923
78108b7c
VS
16924 DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
16925 crtc->base.base.id, crtc->base.name,
08c4d7fc 16926 enableddisabled(crtc->active));
24929352
SV
16927 }
16928
5358901f
SV
16929 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
16930 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
16931
2edd6443
ACO
16932 pll->on = pll->funcs.get_hw_state(dev_priv, pll,
16933 &pll->config.hw_state);
3e369b76 16934 pll->config.crtc_mask = 0;
d3fcc808 16935 for_each_intel_crtc(dev, crtc) {
2dd66ebd 16936 if (crtc->active && crtc->config->shared_dpll == pll)
3e369b76 16937 pll->config.crtc_mask |= 1 << crtc->pipe;
5358901f 16938 }
2dd66ebd 16939 pll->active_mask = pll->config.crtc_mask;
5358901f 16940
1e6f2ddc 16941 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 16942 pll->name, pll->config.crtc_mask, pll->on);
5358901f
SV
16943 }
16944
b2784e15 16945 for_each_intel_encoder(dev, encoder) {
24929352
SV
16946 pipe = 0;
16947
16948 if (encoder->get_hw_state(encoder, &pipe)) {
98187836 16949 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 16950
045ac3b5 16951 encoder->base.crtc = &crtc->base;
253c84c8 16952 crtc->config->output_types |= 1 << encoder->type;
6e3c9717 16953 encoder->get_config(encoder, crtc->config);
24929352
SV
16954 } else {
16955 encoder->base.crtc = NULL;
16956 }
16957
6f2bcceb 16958 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
08c4d7fc
TU
16959 encoder->base.base.id, encoder->base.name,
16960 enableddisabled(encoder->base.crtc),
6f2bcceb 16961 pipe_name(pipe));
24929352
SV
16962 }
16963
3a3371ff 16964 for_each_intel_connector(dev, connector) {
24929352
SV
16965 if (connector->get_hw_state(connector)) {
16966 connector->base.dpms = DRM_MODE_DPMS_ON;
2aa974c9
ML
16967
16968 encoder = connector->encoder;
16969 connector->base.encoder = &encoder->base;
16970
16971 if (encoder->base.crtc &&
16972 encoder->base.crtc->state->active) {
16973 /*
16974 * This has to be done during hardware readout
16975 * because anything calling .crtc_disable may
16976 * rely on the connector_mask being accurate.
16977 */
16978 encoder->base.crtc->state->connector_mask |=
16979 1 << drm_connector_index(&connector->base);
e87a52b3
ML
16980 encoder->base.crtc->state->encoder_mask |=
16981 1 << drm_encoder_index(&encoder->base);
2aa974c9
ML
16982 }
16983
24929352
SV
16984 } else {
16985 connector->base.dpms = DRM_MODE_DPMS_OFF;
16986 connector->base.encoder = NULL;
16987 }
16988 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
08c4d7fc
TU
16989 connector->base.base.id, connector->base.name,
16990 enableddisabled(connector->base.encoder));
24929352 16991 }
7f4c6284
VS
16992
16993 for_each_intel_crtc(dev, crtc) {
16994 crtc->base.hwmode = crtc->config->base.adjusted_mode;
16995
16996 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
16997 if (crtc->base.state->active) {
16998 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
16999 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
17000 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
17001
17002 /*
17003 * The initial mode needs to be set in order to keep
17004 * the atomic core happy. It wants a valid mode if the
17005 * crtc's enabled, so we do the above call.
17006 *
17007 * At this point some state updated by the connectors
17008 * in their ->detect() callback has not run yet, so
17009 * no recalculation can be done yet.
17010 *
17011 * Even if we could do a recalculation and modeset
17012 * right now it would cause a double modeset if
17013 * fbdev or userspace chooses a different initial mode.
17014 *
17015 * If that happens, someone indicated they wanted a
17016 * mode change, which means it's safe to do a full
17017 * recalculation.
17018 */
17019 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
9eca6832
VS
17020
17021 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
17022 update_scanline_offset(crtc);
7f4c6284 17023 }
e3b247da
VS
17024
17025 intel_pipe_config_sanity_check(dev_priv, crtc->config);
7f4c6284 17026 }
30e984df
SV
17027}
17028
043e9bda
ML
17029/* Scan out the current hw modeset state,
17030 * and sanitizes it to the current state
17031 */
17032static void
17033intel_modeset_setup_hw_state(struct drm_device *dev)
30e984df 17034{
fac5e23e 17035 struct drm_i915_private *dev_priv = to_i915(dev);
30e984df 17036 enum pipe pipe;
30e984df
SV
17037 struct intel_crtc *crtc;
17038 struct intel_encoder *encoder;
35c95375 17039 int i;
30e984df
SV
17040
17041 intel_modeset_readout_hw_state(dev);
24929352
SV
17042
17043 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 17044 for_each_intel_encoder(dev, encoder) {
24929352
SV
17045 intel_sanitize_encoder(encoder);
17046 }
17047
055e393f 17048 for_each_pipe(dev_priv, pipe) {
98187836 17049 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
e2af48c6 17050
24929352 17051 intel_sanitize_crtc(crtc);
6e3c9717
ACO
17052 intel_dump_pipe_config(crtc, crtc->config,
17053 "[setup_hw_state]");
24929352 17054 }
9a935856 17055
d29b2f9d
ACO
17056 intel_modeset_update_connector_atomic_state(dev);
17057
35c95375
SV
17058 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
17059 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
17060
2dd66ebd 17061 if (!pll->on || pll->active_mask)
35c95375
SV
17062 continue;
17063
17064 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
17065
2edd6443 17066 pll->funcs.disable(dev_priv, pll);
35c95375
SV
17067 pll->on = false;
17068 }
17069
920a14b2 17070 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
6eb1a681 17071 vlv_wm_get_hw_state(dev);
5db94019 17072 else if (IS_GEN9(dev_priv))
3078999f 17073 skl_wm_get_hw_state(dev);
6e266956 17074 else if (HAS_PCH_SPLIT(dev_priv))
243e6a44 17075 ilk_wm_get_hw_state(dev);
292b990e
ML
17076
17077 for_each_intel_crtc(dev, crtc) {
17078 unsigned long put_domains;
17079
74bff5f9 17080 put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
292b990e
ML
17081 if (WARN_ON(put_domains))
17082 modeset_put_power_domains(dev_priv, put_domains);
17083 }
17084 intel_display_set_init_power(dev_priv, false);
010cf73d
PZ
17085
17086 intel_fbc_init_pipe_state(dev_priv);
043e9bda 17087}
7d0bc1ea 17088
043e9bda
ML
17089void intel_display_resume(struct drm_device *dev)
17090{
e2c8b870
ML
17091 struct drm_i915_private *dev_priv = to_i915(dev);
17092 struct drm_atomic_state *state = dev_priv->modeset_restore_state;
17093 struct drm_modeset_acquire_ctx ctx;
043e9bda 17094 int ret;
f30da187 17095
e2c8b870 17096 dev_priv->modeset_restore_state = NULL;
73974893
ML
17097 if (state)
17098 state->acquire_ctx = &ctx;
043e9bda 17099
ea49c9ac
ML
17100 /*
17101 * This is a cludge because with real atomic modeset mode_config.mutex
17102 * won't be taken. Unfortunately some probed state like
17103 * audio_codec_enable is still protected by mode_config.mutex, so lock
17104 * it here for now.
17105 */
17106 mutex_lock(&dev->mode_config.mutex);
e2c8b870 17107 drm_modeset_acquire_init(&ctx, 0);
043e9bda 17108
73974893
ML
17109 while (1) {
17110 ret = drm_modeset_lock_all_ctx(dev, &ctx);
17111 if (ret != -EDEADLK)
17112 break;
043e9bda 17113
e2c8b870 17114 drm_modeset_backoff(&ctx);
e2c8b870 17115 }
043e9bda 17116
73974893
ML
17117 if (!ret)
17118 ret = __intel_display_resume(dev, state);
17119
e2c8b870
ML
17120 drm_modeset_drop_locks(&ctx);
17121 drm_modeset_acquire_fini(&ctx);
ea49c9ac 17122 mutex_unlock(&dev->mode_config.mutex);
043e9bda 17123
0853695c 17124 if (ret)
e2c8b870 17125 DRM_ERROR("Restoring old state failed with %i\n", ret);
0853695c 17126 drm_atomic_state_put(state);
2c7111db
CW
17127}
17128
17129void intel_modeset_gem_init(struct drm_device *dev)
17130{
dc97997a 17131 struct drm_i915_private *dev_priv = to_i915(dev);
484b41dd 17132 struct drm_crtc *c;
2ff8fde1 17133 struct drm_i915_gem_object *obj;
484b41dd 17134
dc97997a 17135 intel_init_gt_powersave(dev_priv);
ae48434c 17136
1833b134 17137 intel_modeset_init_hw(dev);
02e792fb 17138
1ee8da6d 17139 intel_setup_overlay(dev_priv);
484b41dd
JB
17140
17141 /*
17142 * Make sure any fbs we allocated at startup are properly
17143 * pinned & fenced. When we do the allocation it's too early
17144 * for this.
17145 */
70e1e0ec 17146 for_each_crtc(dev, c) {
058d88c4
CW
17147 struct i915_vma *vma;
17148
2ff8fde1
MR
17149 obj = intel_fb_obj(c->primary->fb);
17150 if (obj == NULL)
484b41dd
JB
17151 continue;
17152
e0d6149b 17153 mutex_lock(&dev->struct_mutex);
058d88c4 17154 vma = intel_pin_and_fence_fb_obj(c->primary->fb,
3465c580 17155 c->primary->state->rotation);
e0d6149b 17156 mutex_unlock(&dev->struct_mutex);
058d88c4 17157 if (IS_ERR(vma)) {
484b41dd
JB
17158 DRM_ERROR("failed to pin boot fb on pipe %d\n",
17159 to_intel_crtc(c)->pipe);
66e514c1 17160 drm_framebuffer_unreference(c->primary->fb);
5a21b665 17161 c->primary->fb = NULL;
36750f28 17162 c->primary->crtc = c->primary->state->crtc = NULL;
5a21b665 17163 update_state_fb(c->primary);
36750f28 17164 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
17165 }
17166 }
1ebaa0b9
CW
17167}
17168
17169int intel_connector_register(struct drm_connector *connector)
17170{
17171 struct intel_connector *intel_connector = to_intel_connector(connector);
17172 int ret;
17173
17174 ret = intel_backlight_device_register(intel_connector);
17175 if (ret)
17176 goto err;
17177
17178 return 0;
0962c3c9 17179
1ebaa0b9
CW
17180err:
17181 return ret;
79e53945
JB
17182}
17183
c191eca1 17184void intel_connector_unregister(struct drm_connector *connector)
4932e2c3 17185{
e63d87c0 17186 struct intel_connector *intel_connector = to_intel_connector(connector);
4932e2c3 17187
e63d87c0 17188 intel_backlight_device_unregister(intel_connector);
4932e2c3 17189 intel_panel_destroy_backlight(connector);
4932e2c3
ID
17190}
17191
79e53945
JB
17192void intel_modeset_cleanup(struct drm_device *dev)
17193{
fac5e23e 17194 struct drm_i915_private *dev_priv = to_i915(dev);
652c393a 17195
dc97997a 17196 intel_disable_gt_powersave(dev_priv);
2eb5252e 17197
fd0c0642
SV
17198 /*
17199 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 17200 * Too much stuff here (turning of connectors, ...) would
fd0c0642
SV
17201 * experience fancy races otherwise.
17202 */
2aeb7d3a 17203 intel_irq_uninstall(dev_priv);
eb21b92b 17204
fd0c0642
SV
17205 /*
17206 * Due to the hpd irq storm handling the hotplug work can re-arm the
17207 * poll handlers. Hence disable polling after hpd handling is shut down.
17208 */
f87ea761 17209 drm_kms_helper_poll_fini(dev);
fd0c0642 17210
723bfd70
JB
17211 intel_unregister_dsm_handler();
17212
c937ab3e 17213 intel_fbc_global_disable(dev_priv);
69341a5e 17214
1630fe75
CW
17215 /* flush any delayed tasks or pending work */
17216 flush_scheduled_work();
17217
79e53945 17218 drm_mode_config_cleanup(dev);
4d7bb011 17219
1ee8da6d 17220 intel_cleanup_overlay(dev_priv);
ae48434c 17221
dc97997a 17222 intel_cleanup_gt_powersave(dev_priv);
f5949141 17223
40196446 17224 intel_teardown_gmbus(dev_priv);
79e53945
JB
17225}
17226
df0e9248
CW
17227void intel_connector_attach_encoder(struct intel_connector *connector,
17228 struct intel_encoder *encoder)
17229{
17230 connector->encoder = encoder;
17231 drm_mode_connector_attach_encoder(&connector->base,
17232 &encoder->base);
79e53945 17233}
28d52043
DA
17234
17235/*
17236 * set vga decode state - true == enable VGA decode
17237 */
6315b5d3 17238int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
28d52043 17239{
6315b5d3 17240 unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
17241 u16 gmch_ctrl;
17242
75fa041d
CW
17243 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
17244 DRM_ERROR("failed to read control word\n");
17245 return -EIO;
17246 }
17247
c0cc8a55
CW
17248 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
17249 return 0;
17250
28d52043
DA
17251 if (state)
17252 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
17253 else
17254 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
17255
17256 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
17257 DRM_ERROR("failed to write control word\n");
17258 return -EIO;
17259 }
17260
28d52043
DA
17261 return 0;
17262}
c4a1d9e4 17263
98a2f411
CW
17264#if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
17265
c4a1d9e4 17266struct intel_display_error_state {
ff57f1b0
PZ
17267
17268 u32 power_well_driver;
17269
63b66e5b
CW
17270 int num_transcoders;
17271
c4a1d9e4
CW
17272 struct intel_cursor_error_state {
17273 u32 control;
17274 u32 position;
17275 u32 base;
17276 u32 size;
52331309 17277 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
17278
17279 struct intel_pipe_error_state {
ddf9c536 17280 bool power_domain_on;
c4a1d9e4 17281 u32 source;
f301b1e1 17282 u32 stat;
52331309 17283 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
17284
17285 struct intel_plane_error_state {
17286 u32 control;
17287 u32 stride;
17288 u32 size;
17289 u32 pos;
17290 u32 addr;
17291 u32 surface;
17292 u32 tile_offset;
52331309 17293 } plane[I915_MAX_PIPES];
63b66e5b
CW
17294
17295 struct intel_transcoder_error_state {
ddf9c536 17296 bool power_domain_on;
63b66e5b
CW
17297 enum transcoder cpu_transcoder;
17298
17299 u32 conf;
17300
17301 u32 htotal;
17302 u32 hblank;
17303 u32 hsync;
17304 u32 vtotal;
17305 u32 vblank;
17306 u32 vsync;
17307 } transcoder[4];
c4a1d9e4
CW
17308};
17309
17310struct intel_display_error_state *
c033666a 17311intel_display_capture_error_state(struct drm_i915_private *dev_priv)
c4a1d9e4 17312{
c4a1d9e4 17313 struct intel_display_error_state *error;
63b66e5b
CW
17314 int transcoders[] = {
17315 TRANSCODER_A,
17316 TRANSCODER_B,
17317 TRANSCODER_C,
17318 TRANSCODER_EDP,
17319 };
c4a1d9e4
CW
17320 int i;
17321
c033666a 17322 if (INTEL_INFO(dev_priv)->num_pipes == 0)
63b66e5b
CW
17323 return NULL;
17324
9d1cb914 17325 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
17326 if (error == NULL)
17327 return NULL;
17328
c033666a 17329 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
ff57f1b0
PZ
17330 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
17331
055e393f 17332 for_each_pipe(dev_priv, i) {
ddf9c536 17333 error->pipe[i].power_domain_on =
f458ebbc
SV
17334 __intel_display_power_is_enabled(dev_priv,
17335 POWER_DOMAIN_PIPE(i));
ddf9c536 17336 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
17337 continue;
17338
5efb3e28
VS
17339 error->cursor[i].control = I915_READ(CURCNTR(i));
17340 error->cursor[i].position = I915_READ(CURPOS(i));
17341 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
17342
17343 error->plane[i].control = I915_READ(DSPCNTR(i));
17344 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
c033666a 17345 if (INTEL_GEN(dev_priv) <= 3) {
51889b35 17346 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
17347 error->plane[i].pos = I915_READ(DSPPOS(i));
17348 }
c033666a 17349 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
ca291363 17350 error->plane[i].addr = I915_READ(DSPADDR(i));
c033666a 17351 if (INTEL_GEN(dev_priv) >= 4) {
c4a1d9e4
CW
17352 error->plane[i].surface = I915_READ(DSPSURF(i));
17353 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
17354 }
17355
c4a1d9e4 17356 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 17357
c033666a 17358 if (HAS_GMCH_DISPLAY(dev_priv))
f301b1e1 17359 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
17360 }
17361
4d1de975 17362 /* Note: this does not include DSI transcoders. */
c033666a 17363 error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
2d1fe073 17364 if (HAS_DDI(dev_priv))
63b66e5b
CW
17365 error->num_transcoders++; /* Account for eDP. */
17366
17367 for (i = 0; i < error->num_transcoders; i++) {
17368 enum transcoder cpu_transcoder = transcoders[i];
17369
ddf9c536 17370 error->transcoder[i].power_domain_on =
f458ebbc 17371 __intel_display_power_is_enabled(dev_priv,
38cc1daf 17372 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 17373 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
17374 continue;
17375
63b66e5b
CW
17376 error->transcoder[i].cpu_transcoder = cpu_transcoder;
17377
17378 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
17379 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
17380 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
17381 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
17382 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
17383 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
17384 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
17385 }
17386
17387 return error;
17388}
17389
edc3d884
MK
17390#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
17391
c4a1d9e4 17392void
edc3d884 17393intel_display_print_error_state(struct drm_i915_error_state_buf *m,
5f56d5f9 17394 struct drm_i915_private *dev_priv,
c4a1d9e4
CW
17395 struct intel_display_error_state *error)
17396{
17397 int i;
17398
63b66e5b
CW
17399 if (!error)
17400 return;
17401
b7f05d4a 17402 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
8652744b 17403 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
edc3d884 17404 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 17405 error->power_well_driver);
055e393f 17406 for_each_pipe(dev_priv, i) {
edc3d884 17407 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536 17408 err_printf(m, " Power: %s\n",
87ad3212 17409 onoff(error->pipe[i].power_domain_on));
edc3d884 17410 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 17411 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
17412
17413 err_printf(m, "Plane [%d]:\n", i);
17414 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
17415 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
5f56d5f9 17416 if (INTEL_GEN(dev_priv) <= 3) {
edc3d884
MK
17417 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
17418 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 17419 }
772c2a51 17420 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
edc3d884 17421 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
5f56d5f9 17422 if (INTEL_GEN(dev_priv) >= 4) {
edc3d884
MK
17423 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
17424 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
17425 }
17426
edc3d884
MK
17427 err_printf(m, "Cursor [%d]:\n", i);
17428 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
17429 err_printf(m, " POS: %08x\n", error->cursor[i].position);
17430 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 17431 }
63b66e5b
CW
17432
17433 for (i = 0; i < error->num_transcoders; i++) {
da205630 17434 err_printf(m, "CPU transcoder: %s\n",
63b66e5b 17435 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536 17436 err_printf(m, " Power: %s\n",
87ad3212 17437 onoff(error->transcoder[i].power_domain_on));
63b66e5b
CW
17438 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
17439 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
17440 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
17441 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
17442 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
17443 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
17444 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
17445 }
c4a1d9e4 17446}
98a2f411
CW
17447
17448#endif
This page took 7.932677 seconds and 4 git commands to generate.